<?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\MCommissionGrade;

class CommissionGrade extends BaseController
{
    private $objMCommissionGrade;

    public function __construct($isCheckAcl = true, $isMustLogin = true)
    {
        parent::__construct($isCheckAcl, $isMustLogin);
        $this->objMCommissionGrade = new MCommissionGrade($this->onlineEnterpriseId, $this->onlineUserId);
    }

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

        return $data;
    }

    /**
     * 新增等级
     */
    public function addGrade()
    {
        $data = self::commonFieldFilter();
        $modelResult = $this->objMCommissionGrade->addGrade($data);
        if(!$modelResult->isSuccess()){
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }

    /**
     * 删除等级
     */
    public function delGrade()
    {
        $id = $this->request->param('request_id');
        if(empty($id)){
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $update = [
            'deleteStatus' => StatusCode::$delete
        ];
        $where = [
            'id' => $id
        ];
        $modelResult = $this->objMCommissionGrade->updateGrade($update, $where);
        if(!$modelResult->isSuccess()){
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }

    /**
     * 修改等级
     */
    public function updateGrade()
    {
        $id = $this->request->param('request_id');
        if(empty($id)){
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $data = self::commonFieldFilter();
        $modelResult = $this->objMCommissionGrade->updateGrade($data, ['id' => $id]);
        if(!$modelResult->isSuccess()){
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }

    /**
     * 等级列表
     */
    public function getAllGrade()
    {
        $modelResult = $this->objMCommissionGrade->getAllGrade();
        if(!$modelResult->isSuccess()){
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }

    /**
     * 等级详情
     */
    public function getInfoGrade()
    {
        $id = $this->request->param('request_id');
        if(empty($id)){
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $where = [
            'id' => $id
        ];
        $modelResult = $this->objMCommissionGrade->getInfoGrade($where);
        if(!$modelResult->isSuccess()){
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }

    /**
     * 升级条件
     */
    public function getAllGradeCondition()
    {
        $modelResult = $this->objMCommissionGrade->getAllGradeCondition(['type' => StatusCode::$delete]);
        if(!$modelResult->isSuccess()){
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }

}
