<?php
namespace JInDouYun\Controller\Cashier;


use JinDouYun\Controller\BaseController;
use JinDouYun\Model\Goods\MGoods;
use Mall\Framework\Core\ErrorCode;

/**
 * Description:
 * Class CashierGoods
 * @package JInDouYun\Controller\Cashier
 */
class CashierGoods extends BaseController
{
    public function __construct($isCheckAcl = false, $isMustLogin = false, $checkToken = true, $getAreaCode = false)
    {
        parent::__construct($isCheckAcl, $isMustLogin, $checkToken, $getAreaCode);
    }

    /**
     * Doc: (des="")
     * User: XMing
     * Date: 2020/9/7
     * Time: 9:27 上午
     * @throws \Exception
     */
    public function getGoodsByCategory()
    {
        $params = $this->request->getRawJson();
        if( empty($params) ){
            $this->sendOutput('参数为空', ErrorCode::$paramError );
        }
        $selectParams = [
            'categoryId' => isset($params['categoryId']) ? $params['categoryId'] : '',
            'keyword' => isset($params['keyword']) ? $params['keyword'] : '',
            'areaCode'=> '',
            'shopId'  => isset($params['shopId']) ? $params['shopId'] : '',
        ];
        if(empty($selectParams["shopId"])){
            $this->sendOutput('请先选择门店', ErrorCode::$paramError );
        }
        $pageParams = pageToOffset($params['page']?:1, $params['pageSize']?:10);
        $selectParams['limit'] = $pageParams['limit'];
        $selectParams['offset'] = $pageParams['offset'];

        if (isset($params['userCenterId']) && !empty($params['userCenterId'])){
            $objMGoods = new MGoods($this->onlineEnterpriseId,true,$params['userCenterId']);
        }else{
            $objMGoods = new MGoods($this->onlineEnterpriseId,true,$this->onlineUserId);
        }
        $selectParams['userCenterId'] = isset($params['userCenterId']) ? $params['userCenterId'] : $this->onlineUserId;
        $result = $objMGoods->search($selectParams);
        if($result->isSuccess()){
            $returnData = $result->getData();
            $pageData = [
                'pageIndex' => $params['page'],
                'pageSize' => $params['pageSize'],
                'pageTotal' => $returnData['total'],
            ];
            parent::sendOutput($returnData['data'], 0, $pageData);
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }

    /**
     * Doc: (des="")
     * User: XMing
     * Date: 2020/9/30
     * Time: 3:08 下午
     * @throws \Exception
     */
    public function getGoodsByBarCode()
    {
        $userCenterId = $this->request->param('userCenterId');
        if (!empty($userCenterId)){
            $objMGoods = new MGoods($this->onlineEnterpriseId,true,$userCenterId);
        }else{
            $objMGoods = new MGoods($this->onlineEnterpriseId,true,$this->onlineUserId);
        }
        $params = $this->request->getRawJson();
        if (empty($params['barCode'])) {
            $this->sendOutput('barCode参数为空', ErrorCode::$paramError);
        }
        $result = $objMGoods->getGoodsByBarCode($params['barCode']);
        if (!$result->isSuccess()) {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
        parent::sendOutput($result->getData());
    }

    /**
     * Doc: (des="")
     * User: XMing
     * Date: 2020/9/7
     * Time: 9:44 上午
     * @throws \Exception
     */
    public function getGoodsDetail()
    {
        $goodsId  = $this->request->param('request_id');
        $userCenterId = $this->request->param('userCenterId');
        if ( !$goodsId ) {
            self::sendOutput('参数错误', ErrorCode::$paramError );
        }
        if (!empty($userCenterId)){
            $objMGoods = new MGoods($this->onlineEnterpriseId,true,$userCenterId);
        }else{
            $objMGoods = new MGoods($this->onlineEnterpriseId,true,$this->onlineUserId);
        }
        $result = $objMGoods->getApiGoodsInfo($goodsId, '');
        if($result->isSuccess()){
            $resultData = $result->getData();
            self::sendOutput($resultData);
        }
        self::sendOutput($result->getData(), $result->getErrorCode());
    }
}