<?php
namespace JInDouYun\Controller\Cashier;

use JinDouYun\Controller\BaseController;
use JinDouYun\Model\Cashier\MCashierGuide;
use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\StatusCode;

/**
 * Description: 导购员 废弃
 * Class Guide
 * @package JInDouYun\Controller\Cashier
 */
class Guide extends BaseController
{
    /**
     * @var MCashierGuide
     */
    private $objMCashierGuide;

    /**
     * Guide constructor.
     * @param bool $isCheckAcl
     * @param bool $isMustLogin
     * @param bool $checkToken
     * @param bool $getAreaCode
     * @throws \Exception
     */
    public function __construct($isCheckAcl = true, $isMustLogin = true, $checkToken = true, $getAreaCode = false)
    {
        parent::__construct($isCheckAcl, $isMustLogin, $checkToken, $getAreaCode);
        $this->objMCashierGuide = new MCashierGuide($this->onlineUserId, $this->onlineEnterpriseId);
    }

    /**
     * Doc: (des="导购员列表")
     * User: XMing
     * Date: 2020/8/31
     * Time: 3:46 下午
     */
    public function getAll()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
        $params['limit'] = $pageParams['limit'];
        $params['offset'] = $pageParams['offset'];
        $result = $this->objMCashierGuide->getAll($params);
        if (!$result->isSuccess()) {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
        $returnData = $result->getData();
        $pageData = [
            'pageIndex' => $params['page'],
            'pageSize'  => $params['pageSize'],
            'pageTotal' => $returnData['total'],
        ];
        parent::sendOutput($returnData['data'], 0, $pageData);
    }

    /**
     * Doc: (des="导购员添加")
     * User: XMing
     * Date: 2020/8/31
     * Time: 4:23 下午
     */
    public function add()
    {
        $params = $this->request->getRawJson();
        if (empty($params)){
            parent::sendOutput('参数为空',ErrorCode::$paramError);
        }
        $data = [
            'name' => isset($params['name']) ? $params['name'] : null,
            'mobile' => isset($params['mobile']) ? $params['mobile'] : null,
            'isSetRule' => isset($params['isSetRule']) ? $params['isSetRule'] : null,
            'enableStatus' => isset($params['enableStatus']) ? $params['enableStatus'] : null,
        ];
        foreach ($data as $key => $value){
            if(empty($value)){
                parent::sendOutput($key.'参数错误', ErrorCode::$paramError);
            }
        }
        if($data['isSetRule'] == StatusCode::$standard){
            if (!isset($params['rule']) || empty($params['rule'])){
                parent::sendOutput('rule参数错误',ErrorCode::$paramError);
            }
            $data['rule'] = json_encode($params['rule']);
        }
        $result = $this->objMCashierGuide->add($data);
        if (!$result->isSuccess()){
            parent::sendOutput($result->getData(),$result->getErrorCode());
        }
        parent::sendOutput($result->getData());
    }

    /**
     * Doc: (des="")
     * User: XMing
     * Date: 2020/9/1
     * Time: 9:30 上午
     */
    public function getInfo()
    {
        $id = $this->request->param('request_id');
        if (empty($id)){
            parent::sendOutput('id参数错误',ErrorCode::$paramError);
        }
        $result = $this->objMCashierGuide->getInfo($id);
        if (!$result->isSuccess()){
            parent::sendOutput($result->getData(),$result->getErrorCode());
        }
        parent::sendOutput($result->getData());
    }
}