<?php

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


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

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

    /**
     * 店主首页
     */
    public function indexAction() {
        $result = $this->shopkeeperService->getShopkeeper($this->memberId);
        $this->success($result);
    }

    /**
     * 获得店铺统计数据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 getDeliveriersAction(){
        $result = $this->shopkeeperService->getDiliveryManByStoreId($this->memberId);
        if($result!==false){
            $this->success($result,\Our\DescribeConst::successMessage,\Our\DescribeConst::successMessage);
        }
        ErrorModel::throwException(CodeConfigModel::commonError);
    }

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

    /**
     * 获得订单数量
     */
    public function countAction(){
        $result = $this->shopkeeperService->getCount($this->memberId);
        if(!empty($result)){
            $this->success($result);
        }
        ErrorModel::throwException(CodeConfigModel::commonError);

    }

    public function receivePaymentAction(){
        $shopkeeperService=\Business\Store\ShopkeeperServiceModel::getInstance();
        $res=$shopkeeperService->confirmReceivePayment($this->memberId,$this->req['data']['orderId']);
        if($res){
            $this->success(array(),\Our\DescribeConst::successMessage);
        }else{
            ErrorModel::throwException(CodeConfigModel::confirmReceiveError);
        }
    }

    /**
     * 查询配送员
     *
     */
    public function searchDeliveryAction(){
        $shopkeeperService=\Business\Store\ShopkeeperServiceModel::getInstance();
        $res=$shopkeeperService->getDiliveryMan($this->req['data']['mobile']);
        $res=$res?$res:new \stdClass();
        $this->success($res,\Our\DescribeConst::successMessage);
    }
    /**
     * 单一属性商品库存预警
     * @author king
     */
    public function warningAction(){
        $goodsCommonService = \Business\Goods\GoodsCommonServiceModel::getInstance();
        $pageIndex=$this->getPageIndex();
        $pageSize=$this->getPageSize();

        $res = $goodsCommonService->getGoodsStorageList($this->storeId, $pageIndex, $pageSize);
        $this->success($res);
    }

    /**
     * 添加库存
     * @author king
     */
    public function addStockAction(){
        $goodsCommonService = \Business\Goods\GoodsCommonServiceModel::getInstance();
        $goodsId = (int)$this->req['data']['goodsId'];
        $num = (int)$this->req['data']['num'];
        $type = trim($this->req['data']['type']);
        $res = $goodsCommonService->setGoodsStorage($this->storeId, $goodsId, $num, $type);
        $this->success($res);
    }

    /**
     * 获得售后列表
     */
    public function getRefundsAction(){
        $type = intval($this->req['data']['type']);
        $refundService=\Business\Order\RefundServiceModel::getInstance();
        $pageIndex=$this->getPageIndex();
        $pageSize=$this->getPageSize();
        $res=$refundService->getStoreRefundList($this->storeId, $type, $pageIndex, $pageSize);
        $this->success($res);
    }



    /**
     * 同意退款
     */
    public function agreeAction(){
        $refundId= intval($this->req['data']['refundId']);
        if(!$refundId) {
            ErrorModel::throwException(CodeConfigModel::notExistRefund);
        }
        $refundService=\Business\Order\RefundServiceModel::getInstance();
        $refundAmount = floatval($this->req['data']['refundAmount']);
        $text = trim($this->req['data']['text']);
        $isGiveUp=$this->req['data']['isGiveUp']?$this->req['data']['isGiveUp']:\Our\ApiConst::zero;
        $res = $refundService->refund($this->storeId, $this->sellerId, $this->sellerName, $refundId,$refundAmount, $text,\Our\ApiConst::refundSellerAgree,$isGiveUp);
        if($res){
            $this->success('操作成功');
        }
        ErrorModel::throwException(CodeConfigModel::commonError);

    }

    //标记为已退款
    public function makeRefundAction(){
        $refundService=\Business\Order\RefundServiceModel::getInstance();
       if( $refundService->makeRefund($this->storeId,$this->req['data']['refundId'])){
           $this->success('操作成功');
       };
        ErrorModel::throwException(CodeConfigModel::commonError);
    }
    /**
     * 拒绝退款
     */
    public function refuseAction(){
        $refundId= intval($this->req['data']['refundId']);
        if(!$refundId) {
            ErrorModel::throwException(CodeConfigModel::notExistRefund);
        }
        $refundService=\Business\Order\RefundServiceModel::getInstance();
        $text = trim($this->req['data']['text']);
        $res = $refundService->refund($this->storeId, $this->sellerId, $this->sellerName, $refundId,\Our\ApiConst::zero,$text,\Our\ApiConst::refundSellerRefuse);
        $this->success($res);
    }

    /**
     * 确认收货
     * @throws Exception
     */
    public function confirmReceiveAction(){
        $refundId= intval($this->req['data']['refundId']);
        if(!$refundId) {
            ErrorModel::throwException(CodeConfigModel::notExistRefund);
        }
        $refundService=\Business\Order\RefundServiceModel::getInstance();
        $goodsState= intval($this->req['data']['goodsState']);
        $res = $refundService->receive($this->storeId, $this->sellerId, $this->sellerName, $refundId, $goodsState);
        if($res){
            $this->success($res);
        }


    }



    /**
     * 获取应用版本
     */
    public function getVersionAction(){
        $version = \Business\Common\SettingServiceModel::getInstance()->getAppVersion();
        $this->success($version);
    }

    /**
     * 设置收货时间
     * @throws Exception
     */
    public function setReceiveTimeAction(){
        $refundId= intval($this->req['data']['refundId']);
        if(!$refundId) {
            ErrorModel::throwException(CodeConfigModel::notExistRefund);
        }
        $refundService=\Business\Order\RefundServiceModel::getInstance();
        $receiveTime = trim($this->req['data']['receiveTime']);
        $res = $refundService->setReceiveTime($this->storeId, $refundId, $receiveTime);
        $this->success($res);
    }


    /**
     * 获得退款详情接口
     *
     * @ccw
     */
    public function getRefundDetailAction(){
        $refundService=\Business\Order\RefundServiceModel::getInstance();
        $res=$refundService->getStoreRefundDetail($this->storeId,$this->req[\Our\NameConst::data]['refundId']);
        $this->success($res);
    }


    /**
     * 修改订单金额
     * @throws Exception
     */
    public function modifyOrderAmountAction(){
        $orderId= intval($this->req['data']['orderId']);
        $orderAmount= intval($this->req['data']['orderAmount']);
        if(empty($orderId) && empty($orderAmount)) {
            ErrorModel::throwException(CodeConfigModel::paramsError);
        }
        $orderService=\Business\Order\OrderServiceModel::getInstance();
        $res = $orderService->updateOrderAmount($this->storeId, $orderId,$orderAmount,$this->memberName);
        if($res !==false){
            $this->success($res,\Our\DescribeConst::updateSuccess);
        }
        ErrorModel::throwException(CodeConfigModel::commonError);



    }
}


