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

namespace JinDouYun\Controller\Commission;

use Exception;
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 CommissionBusinessman extends BaseController
{
    private $objMCommissionBusinessman;

    public function __construct($isCheckAcl = true, $isMustLogin = true)
    {
        parent::__construct($isCheckAcl, $isMustLogin);
        $this->objMCommissionBusinessman = new MCommissionBusinessman($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 addBusinessman()
    {
        $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 delBusinessman()
    {
        $params = $this->request->getRawJson();
        $where = [];
        isset($params['userCenterId']) && $where['userCenterId'] = $params['userCenterId'];
        if(!isset($where['userCenterId'])){
            parent::sendOutput('userCenterId参数错误', ErrorCode::$paramError);
        }

        $update = [
            'deleteStatus' => StatusCode::$delete
        ];

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

    /**
     * 分销商审核
     * @throws Exception
     */
    public function auditBusinessman()
    {
        $params = $this->request->getRawJson();
        $where = [];
        isset($params['userCenterId']) && $where['userCenterId'] = $params['userCenterId'];
        $update = [
            'auditStatus' => StatusCode::$auditStatus['auditPass']
        ];
        if(empty($where)){
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        if(!isset($where['userCenterId'])){
            parent::sendOutput('userCenterId参数错误', ErrorCode::$paramError);
        }
        $modelResult = $this->objMCommissionBusinessman->updateBusinessman($update, $where);
        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, ['userCenterId' => $id]);
        if(!$modelResult->isSuccess()){
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }

    /**
     * 分销商列表
     */
    public function getAllBusinessman()
    {
        $params = $this->request->getRawJson();
        if(empty($params)){
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        $selectParams = [];
        $params['page'] = isset($params['page']) ? $params['page'] : 1;
        $params['pageSize'] = isset($params['pageSize']) ? $params['pageSize'] : 10;
        $pageParams = pageToOffset($params['page'], $params['pageSize']);
        $selectParams['limit'] = $pageParams['limit'];
        $selectParams['offset'] = $pageParams['offset'];

        if(isset($params['auditStatus']) && !empty($params['auditStatus'])){
            $selectParams['auditStatus'] = $params['auditStatus'];
        }

        if(isset($params['search']) && !empty($params['search'])){
            $selectParams['search'] = $params['search'];
        }

        if(isset($params['grade']) && !empty($params['grade'])){
            $selectParams['grade'] = $params['grade'];
        }

        if(isset($params['createStar']) && !empty($params['createStar']) && isset($params['createEnd']) && !empty($params['createEnd'])){
            $selectParams['createStar'] = $params['createStar'];
            $selectParams['createEnd'] = $params['createEnd'];
        }

        if(isset($params['applicationStar']) && !empty($params['applicationStar']) && isset($params['applicationEnd']) && !empty($params['applicationEnd'])){
            $selectParams['applicationStar'] = $params['applicationStar'];
            $selectParams['applicationEnd'] = $params['applicationEnd'];
        }

        if(isset($params['auditStar']) && !empty($params['auditStar']) && isset($params['auditEnd']) && !empty($params['auditEnd'])){
            $selectParams['auditStar'] = $params['auditStar'];
        }
        $export = isset($params['export']) ? $params['export'] : 0;// 导出未审核
        $distribution = isset($params['distribution']) ? $params['distribution'] : 0; // 导出分销商
        $modelResult = $this->objMCommissionBusinessman->getAllBusinessman($selectParams,$export,$distribution);
        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);
    }

    /**
     * 查询分销商的所有下级
     * @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 getInfoBusinessman()
    {
        $where = [];
        $params = $this->request->getRawJson();
        isset($params['userCenterId']) && $where['userCenterId'] = $params['userCenterId'];
        if(empty($where)){
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $modelResult = $this->objMCommissionBusinessman->getInfoBusinessman($where);
        if(!$modelResult->isSuccess()){
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }

    /**
     * 首页统计
     */
    public function statisticsCommissionOne()
    {
        $modelResult = $this->objMCommissionBusinessman->statisticsCommissionOne();
        if(!$modelResult->isSuccess()){
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }


    /**
     * 首页统计(新增分销商数)
     */
    public function statisticsCommissionTwo()
    {
        $modelResult = $this->objMCommissionBusinessman->statisticsCommissionTwo();
        if(!$modelResult->isSuccess()){
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }


    /**
     * 首页统计(TOP)
     */
    public function statisticsCommissionThree()
    {
        $modelResult = $this->objMCommissionBusinessman->statisticsCommissionThree();
        if(!$modelResult->isSuccess()){
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }

    /**
     * 查询分销商下级人数
     */
    public function getBusinessmanSubLevelNum()
    {
        $modelResult = $this->objMCommissionBusinessman->getBusinessmanSubLevelNum();
        if(!$modelResult->isSuccess()){
            parent::sendOutput($modelResult->getData(),$modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }
}
