<?php
/**
 * api分销商Controller
 * Created by PhpStorm.
 * User: haoren
 * Date: 2020/07/22
 * Time: 15:00
 */

namespace JinDouYun\Controller\Commission;

use Exception;
use JinDouYun\Model\Commission\MCommissionGrade;
use JinDouYun\Model\Commission\MCommissionOrder;
use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\ResultWrapper;
use Mall\Framework\Core\StatusCode;

use JinDouYun\Controller\BaseController;

use JinDouYun\Model\Commission\MCommissionBusinessman;

class ApiCommissionBusinessman extends BaseController
{
    /**
     * @var MCommissionBusinessman
     */
    private $objMCommissionBusinessman;

    /**
     * @var MCommissionOrder
     */
    private $objMCommissionOrder;

    /**
     * ApiCommissionBusinessman constructor.
     * @param bool $isCheckAcl
     * @param bool $isMustLogin
     * @param bool $checkToken
     * @throws Exception
     */
    public function __construct($isCheckAcl = false, $isMustLogin = true, $checkToken=true)
    {
        parent::__construct($isCheckAcl, $isMustLogin, $checkToken);
        $this->objMCommissionBusinessman = new MCommissionBusinessman($this->onlineEnterpriseId, $this->onlineUserId);
        $this->objMCommissionOrder = new MCommissionOrder($this->onlineEnterpriseId,$this->onlineUserId);
    }

    /**
     * 获取参数
     */
    public function commonFieldFilter()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $data = [
            'customerId' => isset($params['customerId']) ? $params['customerId'] : '',
        ];
        foreach($data as $key => $value){
            if(empty($value) && $value !== 0){
                parent::sendOutput($key.'参数错误', ErrorCode::$paramError);
            }
        }
        (isset($params['info']) && !empty($params['info'])) && $data['info'] = $params['info'];
        (isset($params['gradeId']) && !empty($params['gradeId'])) && $data['gradeId'] = $params['gradeId'];
        (isset($params['grade']) && !empty($params['grade'])) && $data['grade'] = $params['grade'];

        return $data;
    }

    /**
     * 申请分销商
     * @throws Exception
     */
    public function applicationBusinessman()
    {
        $data = self::commonFieldFilter();
        $modelResult = $this->objMCommissionBusinessman->addBusinessman($data);
        if(!$modelResult->isSuccess()){
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }

    /**
     * 添加上下级关系
     * @throws Exception
     */
    public function relationshipBusinessman()
    {
        $params = $this->request->getRawJson();
        if(empty($params)){
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }

        $data = [
            'businessmanId' => getArrayItem($params, 'businessmanId'),
            'source' => getArrayItem($params, 'source'),
        ];
        foreach($data as $key => $value){
            if(empty($value) && $value !== 0){
                parent::sendOutput($key.'参数错误', ErrorCode::$paramError);
            }
        }

        $modelResult = $this->objMCommissionBusinessman->relationshipBusinessman($data);
        if(!$modelResult->isSuccess()){
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }

    /**
     * 修改分销商
     * @throws Exception
     */
    public function updateBusinessman()
    {
        $id = $this->request->param('request_id');
        if(empty($id)){
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $data = self::commonFieldFilter();
        $modelResult = $this->objMCommissionBusinessman->updateBusinessman($data, ['id' => $id]);
        if(!$modelResult->isSuccess()){
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }

    /**
     * 分销商详情
     */
    public function getInfoBusinessman()
    {
        $where['userCenterId'] = $this->onlineUserId;
        $modelResult = $this->objMCommissionBusinessman->getInfoBusinessman($where);
        if(!$modelResult->isSuccess()){
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }

    /**
     * 查询分销商的所有下级
     * @throws Exception
     */
    public function getAllSub()
    {
        $params = $this->request->getRawJson();
        if(empty($params)){
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $data = [
            'id' => isset($params['id']) ? $params['id'] : '',
            'level' => isset($params['level']) ? $params['level'] : '',
            'page' => isset($params['page']) ? $params['page'] : '',
            'pageSize' => isset($params['pageSize']) ? $params['pageSize'] : '',
        ];
        foreach($data as $key => $value){
            if(empty($value) && $value !== 0){
                parent::sendOutput($key.'参数错误', ErrorCode::$paramError);
            }
        }
        $pageParams = pageToOffset($params['page'],$params['pageSize']);
        $data['limit'] = $pageParams['limit'];
        $data['offset'] = $pageParams['offset'];

        $modelResult = $this->objMCommissionBusinessman->getAllSub($data);
        if(!$modelResult->isSuccess()){
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        $returnData = $modelResult->getData();

        $pageData = [
            'pageIndex' => $params['page'],
            'pageSize' => $params['pageSize'],
            'pageTotal' => $returnData['total'],
            'subTotal' => $returnData['subTotal'],
            'businessmanTotal' => $returnData['businessmanTotal'],
            'subBusinessmanTotal' => $returnData['subBusinessmanTotal'],
        ];
        parent::sendOutput($returnData['data'], 0, $pageData);
    }

    /**
     * 排行榜
     */
    public function commissionTop()
    {
        $modelResult = $this->objMCommissionBusinessman->statisticsCommissionThree();
        if(!$modelResult->isSuccess()){
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }

    /**
     * 等级说明
     */
    public function getAllGrade()
    {
        $modelResult = $this->objMCommissionBusinessman->getBusinessmanGradeInfo();
        if(!$modelResult->isSuccess()){
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }

    /**
     * Doc: (des="分销中心订单列表")
     * User: XMing
     * Date: 2020/7/30
     * Time: 10:47 上午
     * @throws Exception
     */
    public function getCommissionOrder()
    {
        $params = $this->request->getRawJson();
        if(empty($params)){
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }

        $pageParams = pageToOffset($params['page'] ? $params['page'] : 1, $params['pageSize'] ? $params['pageSize'] : 10);
        $params['limit'] = $pageParams['limit'];
        $params['offset'] = $pageParams['offset'];

        $modelResult = $this->objMCommissionOrder->getCommissionOrder($params);
        if(!$modelResult->isSuccess()){
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        $returnData = $modelResult->getData();
        $pageData = [
            'pageIndex' => $params['page'],
            'pageSize' => $params['pageSize'],
            'pageTotal' => $returnData['total'],
        ];
        parent::sendOutput($returnData['data'], 0, $pageData);
    }

    /**
     * Doc: (des="分销订单详情")
     * User: XMing
     * Date: 2020/7/30
     * Time: 4:06 下午
     * @throws Exception
     */
    public function getCommissionOrderInfo()
    {
        $id = $this->request->param('request_id');
        if(empty($id)){
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $modelResult = $this->objMCommissionOrder->getCommissionOrderInfo($id);
        if(!$modelResult->isSuccess()){
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }


    /**
     * 修改直系上级关系
     * @throws Exception
     * 客户id，上级id
     */
    public function editRelationship()
    {
        $params = $this->request->getRawJson();
        if(empty($params['id']) || empty($params['superiorId']) ){
            parent::sendOutput('分销商参数错误', ErrorCode::$paramError);
        }
        $result = $this->objMCommissionBusinessman->editRelationship($params);
        if(!$result->isSuccess()){
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
        parent::sendOutput($result->getData());
    }
}
