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

namespace JinDouYun\Controller\NewCommission;

use Exception;
use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\ResultWrapper;
use Mall\Framework\Core\StatusCode;
use JinDouYun\Controller\BaseController;

use Util\Common\AgentTools;
use JinDouYun\Model\Customer\MCustomer;

class NewAgent extends BaseController
{
    
    private $customerData;
    private $agentTool;

    public function __construct($isCheckAcl = true, $isMustLogin = true)
    {
        parent::__construct($isCheckAcl, $isMustLogin);
        $this->agentTool = new AgentTools($this->onlineEnterpriseId);
        $this->customerData = $this->agentTool->getCustomerDataByUserCenterId($this->onlineUserId);

    }
    
    /**
     * 获取分销推荐代理数据列表
     */
    public function getDataList(){
        $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['search']) && !empty($params['search'])){
            $selectParams['search'] = $params['search'];
        }
        $res = $this->agentTool->getPartnerList($selectParams);
        $pageData = [
            'pageIndex' => $params['page'],
            'pageSize' => $params['pageSize'],
            'pageTotal' => $res['total'],
        ];
        parent::sendOutput($res['data'], 0, $pageData);

        
    }
    
    public function getBalanceDetailList(){
        $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['search']) && !empty($params['search'])){
            $selectParams['search'] = $params['search'];
        }
        if(isset($params['agentId'])){
            $selectParams["agentId"] = $params['agentId'];
        }
        $res = $this->agentTool->getBalanceDetailList($selectParams);
        $pageData = [
            'pageIndex' => $params['page'],
            'pageSize' => $params['pageSize'],
            'pageTotal' => $res['total'],
        ];
        parent::sendOutput($res['data'], 0, $pageData);
    }
    
    public function getDetailList(){
        $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['search']) && !empty($params['search'])){
            $selectParams['search'] = $params['search'];
        }
        if(isset($params['agentId'])){
            $selectParams["agentId"] = $params['agentId'];
        }
        $res = $this->agentTool->getDetailList($selectParams);
        $pageData = [
            'pageIndex' => $params['page'],
            'pageSize' => $params['pageSize'],
            'pageTotal' => $res['total'],
        ];
        parent::sendOutput($res['data'], 0, $pageData);
    }
    
    public function getPartnerCashList(){
        $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['search']) && !empty($params['search'])){
            $selectParams['search'] = $params['search'];
        }
        if(isset($params['listStatus']) && in_array($params['listStatus'], ["ok","no","wait"])){
            switch ($params['listStatus']) {
                case "wait":
                    $selectParams["status"] = 0;
                    break;
                case "ok":
                    $selectParams["status"] = 1;
                    break;
                case "no":
                    $selectParams["status"] = -1;
                    break;
                default:
                    break;
            }
        }
        if(isset($params['bank_type_code'])){
            $selectParams["bank_type_code"] = $params['bank_type_code'];
        }
        if(isset($params['time']) && is_array($params['time'])){
            $selectParams["start_time"] = (int)$params['time'][0] / 1000;
            $selectParams["end_time"]   = (int)$params['time'][1] / 1000;
        }
        if(isset($params['agentId'])){
            $selectParams["agentId"] = $params['agentId'];
        }
        
        
        $res = $this->agentTool->getPartnerCashList($selectParams);
        $pageData = [
            'pageIndex' => $params['page'],
            'pageSize' => $params['pageSize'],
            'pageTotal' => $res['total'],
        ];
        parent::sendOutput($res['data'], 0, $pageData);
    }
    
    /**
     * 处理提现
     */
    public function partnerCashAudit(){
        $params = $this->request->getRawJson();
        if(empty($params)){
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        $selectParams = [];
        if(empty($params["id"]) || empty($params["type"])){
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        if(!in_array($params["type"],[1,-1])){
            $this->sendOutput('参数错误', ErrorCode::$paramError);
        }
        if($params["type"]==-1 && empty($params["audit_mono"])){
            $this->sendOutput('请输入驳回说明', ErrorCode::$paramError);
        }
        if($params["type"]==1 && empty($params["audit_mono"])){
            $params["audit_mono"] = "打款成功";
        }
        $res = $this->agentTool->partnerCashAudit($params["id"],$params["audit_mono"],$params["type"],$this->onlineUserId);
        if(empty($res) || empty($res["code"])){
            parent::sendOutput('系统繁忙,请稍后重试001', ErrorCode::$paramError);
        }
        if($res["code"]==-1){
            parent::sendOutput($res["msg"], ErrorCode::$paramError);
        }
        parent::sendOutput("提现申请处理成功");
    }
    
    /**
     * 获取配置信息
     */
    public function getSettingData(){
        $data = AgentTools::getCommissionSettingData($this->onlineEnterpriseId);
        parent::sendOutput($data);
    }
    public function setSettingData(){
        $params = $this->request->getRawJson();
        if(empty($params) || empty($this->onlineEnterpriseId)){
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        $res = AgentTools::setCommissionSettingData($this->onlineEnterpriseId, $params);
        if(empty($res) || empty($res["code"])){
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        if($res["code"]==-1){
            $this->sendOutput($res["msg"], ErrorCode::$paramError);
        }
        parent::sendOutput("配置提交成功");
    }
    
    
    /**
     * 获取提现方式
     */
    public function getCashBankType(){
        $data = $this->agentTool->getBankType(1);
        parent::sendOutput($data);
    }
    
    
}
