
<?php

use Error\ErrorModel;
use Error\CodeConfigModel;
use Our\NameConst;
use Our\Common;
use Business\User\MemberServiceModel;
use Business\Store\ShopkeeperServiceModel;


/**
 * 我是店主
 *
 * @date 2018-5-10
 * @author csw <993768343@qq.com>
 */
class ShopkeeperController extends \Our\Controller_AbstractApi {
    public $memberService;
    public $shopkeeperService;

    public function init(){
        parent::init();
        $this->memberService = MemberServiceModel::getInstance();
        $this->shopkeeperService  = ShopkeeperServiceModel::getInstance();
    }

    /**
     * 店主首页
     */
    public function indexAction() {
        $result = $this->shopkeeperService->getShopkeeper($this->memberId);
        $this->success($result);
        if($result){
            $this->success(array(),\Our\DescribeConst::successMessage,\Our\DescribeConst::successMessage);
        }
    }

    /**
     * 获得店铺统计数据ccw:18305954587
     */
    public function statisticsAction(){
        $result = $this->shopkeeperService->getStatistics($this->memberId);
        $this->success($result);
        if($result){
            $this->success(array(),\Our\DescribeConst::successMessage,\Our\DescribeConst::successMessage);
        }
    }
    /**
     * 分类店铺销量排行(及列表第一页)
     */
    public function myStoreClassRankAction() {
        $gcId = isset($this->req['data']['gcId'])?$this->req['data']['gcId']:\Our\ApiConst::zero;
        if(empty($gcId)){
            ErrorModel::throwException(CodeConfigModel::emptyGcId);
        }
        $pageIndex = $this->getPageIndex();
        $pageSize=$this->getPageSize(\Our\ApiConst::salePageSize);
        $result = $this->shopkeeperService->getMyStoreClassRank($this->memberId,$gcId,$pageIndex,$pageSize);
        $this->success($result);
        if($result){
            $this->success(array(),\Our\DescribeConst::successMessage,\Our\DescribeConst::successMessage);
        }
    }

    /**
     * 分类店铺销量排行分页
     */
    public function storeClassRankAction() {
        $gcId = $this->req['data']['gcId'];
        $pageIndex = (int)$this->req['data']['pageIndex'];
        $pageSize=isset($this->req['data']['pageSize'])?$this->req['data']['pageSize']:10;
        if(empty($gcId)){
            ErrorModel::throwException(CodeConfigModel::illegalAccess);
        }
        $result = $this->shopkeeperService->getStoreClassRankList($gcId,$pageIndex,$pageSize);
        $this->success($result);
        if($result){
            $this->success(array(),\Our\DescribeConst::successMessage,\Our\DescribeConst::successMessage);
        }
    }

    /**
     * 店铺关注会员统计(及列表第一页)
     */
    public function storeMemberStatsAction(){
        $pageIndex = $this->getPageIndex();
        $pageSize=$this->getPageSize(\Our\ApiConst::pageSizeTen);
        $keyword = (isset($this->req['data']['keyword'])&&!empty($this->req['data']['keyword']))?$this->req['data']['keyword']:'';
        $sort = isset($this->req['data']['sort'])?$this->req['data']['sort']:\Our\ApiConst::one;
        $result = $this->shopkeeperService->getStoreMemberStats($this->memberId,$pageIndex,$pageSize,$sort,$keyword);
        $this->success($result);
        if($result){
            $this->success(array(),\Our\DescribeConst::successMessage,\Our\DescribeConst::successMessage);
        }
    }

    /**
     * 店铺关注会员统计列表
     */
    public function membersStatsAction(){
        $pageIndex = (int)$this->getPageIndex();
        $pageSize=$this->getPageSize(\Our\ApiConst::pageSizeTen);
        $keyword = trim($this->req['data']['keyword']);
        $sort = isset($this->req['data']['sort'])?$this->req['data']['sort']:\Our\ApiConst::one;
        $result = $this->shopkeeperService->getMemberStats($this->memberId,$pageIndex,$pageSize,$sort,$keyword);
        $this->success($result);
        if($result){
            $this->success(array(),\Our\DescribeConst::successMessage,\Our\DescribeConst::successMessage);
        }
    }

    /**
     * 接单
     */
    public function orderReceiveAction(){
        $orderId = $this->req['data']['orderId'];
        $result = $this->shopkeeperService->orderReceive($this->memberId, $orderId);
        if ($result) {
            $this->success(array(), \Our\DescribeConst::successMessage, \Our\DescribeConst::successMessage);
        }else{
            ErrorModel::throwException(CodeConfigModel::receiveError);
        }
    }

    /**
     * 店铺订单列表（第一页数据）
     */
    public function storeOrdersAction(){
        $pageIndex = (int)$this->req['data']['pageIndex'];
        $pageIndex = $pageIndex?$pageIndex:0;
        $pageSize=isset($this->req['data']['pageSize'])?$this->req['data']['pageSize']:10;
        $orderState = isset($this->req['data']['orderState'])?$this->req['data']['orderState']:'';
        $keyword = isset($this->req['data']['keyword'])?$this->req['data']['keyword']:'';
        $result = $this->shopkeeperService->getStoreOrders($this->memberId,$pageIndex,$pageSize,$orderState,$keyword);
        $this->success($result);
        if($result){
            $this->success(array(),\Our\DescribeConst::successMessage,\Our\DescribeConst::successMessage);
        }
    }

    /**
     * 店铺订单列表（分页）
     */
    public function ordersAction(){
        $pageIndex = $this->getPageIndex();
        $pageSize=$this->getPageSize();
        $orderState = isset($this->req['data']['orderState'])?$this->req['data']['orderState']:'';
        $keyword = isset($this->req['data']['keyword'])?$this->req['data']['keyword']:'';
        $result = $this->shopkeeperService->getOrders($this->memberId,$pageIndex,$pageSize,$orderState,$keyword);
        $this->success($result);
        if($result){
            $this->success(array(),\Our\DescribeConst::successMessage,\Our\DescribeConst::successMessage);
        }
    }

    /**
     * 订单详情
     */
    public function orderDetailAction(){
        $orderId = $this->req['data']['orderId'];
        if(empty($orderId)){
            ErrorModel::throwException(CodeConfigModel::emptyOrderIdForPay);
        }
        $result = $this->shopkeeperService->getOrderDetail($this->memberId,$orderId);
        $this->success($result);
        if($result){
            $this->success(array(),\Our\DescribeConst::successMessage,\Our\DescribeConst::successMessage);
        }
    }


}
