<?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\PartnerTools;
use JinDouYun\Model\Customer\MCustomer;

class ApiNewCommissionPartner extends BaseController
{
    
    private $customerData;
    private $partnerTool;

    public function __construct($isCheckAcl = false, $isMustLogin = true, $checkToken=true)
    {
        parent::__construct($isCheckAcl, $isMustLogin, $checkToken);
        $this->partnerTool = new PartnerTools($this->onlineEnterpriseId);
        $this->customerData = $this->partnerTool->getCustomerDataByUserCenterId($this->onlineUserId);

    }
    
    
    /**
     * 获取账户信息
     */
    public function getCommissionPartnerData(){
        if($this->customerData["isPartner"]==0){
            parent::sendOutput('您还不是合伙人', ErrorCode::$paramError);
        }
        $data = $this->partnerTool->getPartnerData($this->customerData["id"],$this->onlineUserId);
        if(empty($data)){
            parent::sendOutput('数据不存在', ErrorCode::$paramError);
        }
        parent::sendOutput($data);
    }
    /**
     * 获取分佣明细
     */
    public function getCommissionPartnerDetail(){
        $params = $this->request->getRawJson();
        if(empty($params)){
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
        $selectParams['limit'] = $pageParams['limit'];
        $selectParams['offset'] = $pageParams['offset'];
        $where=[];
        if(!empty($params['code']) && in_array($params['code'], ["consume","income"])){
            if($params['code']=="consume"){
                $where["type"] = 0;
            }
            if($params['code']=="income"){
                $where["type"] = 1;
            }
        }
        $selectParams["where"] = $where;
        
//        $testData=[
//            "total"=>1,
//            "data"=>[
//                [
//                    "avatar"=>"https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132",
//                    "title"=>"子级合伙人收益分佣",
//                    "type"=>1,
//                    "calcMoney"=>100,
//                    "commission"=>10,
//                    "name"=>"影子",
//                    "time"=>1000000,
//                ],
//                [
//                    "avatar"=>"https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132",
//                    "title"=>"子级消费分佣",
//                    "type"=>0,
//                    "calcMoney"=>100,
//                    "commission"=>10,
//                    "name"=>"影子",
//                    "time"=>1000000,
//                ],
//            ],
//        ];
//        parent::sendOutput($testData);
        
        $data = $this->partnerTool->getCommissionDetail($this->customerData["id"],$selectParams);
        parent::sendOutput($data);
    }
    /**
     * 获取分佣账户余额明细
     */
    public function getCommissionPartnerBalanceDetail(){
        $params = $this->request->getRawJson();
        if(empty($params)){
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
        $selectParams['limit'] = $pageParams['limit'];
        $selectParams['offset'] = $pageParams['offset'];
        $data = $this->partnerTool->getCommissionBalanceDetail($this->customerData["id"],$selectParams);
        parent::sendOutput($data);
    }
    /**
     * 获取提现记录
     */
    public function getPartnerApplyCashList(){
        $params = $this->request->getRawJson();
        if(empty($params)){
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
        $selectParams['limit'] = $pageParams['limit'];
        $selectParams['offset'] = $pageParams['offset'];
        $where=[];
        if(!empty($params['status'])){
            if($params['status']=="wait"){
                $where["status"] = 0;
            }
            if($params['status']=="ok"){
                $where["status"] = 1;
            }
            if($params['status']=="no"){
                $where["status"] = -1;
            }
        }
        $selectParams["where"] = $where;
        $data = $this->partnerTool->getPartnerApplyCashList($this->customerData["id"],$selectParams);
        parent::sendOutput($data);
    }
    /**
     * 获取提现详情
     */
    public function getPartnerApplyCashInfo(){
        $params = $this->request->getRawJson();
        if(empty($params) || empty($params["id"])){
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $where["id"] = $params["id"];
        $selectParams["where"] = $where;
        $data = $this->partnerTool->getPartnerApplyCashInfo($this->customerData["id"],$selectParams);
        parent::sendOutput($data);
            
    }
    
    public function getPartnerCashType(){
        $data = $this->partnerTool->getBankType(1);
        parent::sendOutput($data);
        
    }
    
    public function partnerApplyCash(){
        $params = $this->request->getRawJson();
        if(empty($params)){
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        
        
        $data=[];
        $data["customerId"] = $this->customerData["id"];
        $data["money"]     = empty($params["money"])    ? 0  : floatval($params["money"]);
        $data["bank_type"] = empty($params["bank_type"])? "" : $params["bank_type"];
        $data["bank_name"] = empty($params["bank_name"])? "" : $params["bank_name"];
        $data["bank_num"]  = empty($params["bank_num"]) ? "" : $params["bank_num"];
        $data["bank_ad"]   = empty($params["bank_ad"])  ? "" : $params["bank_ad"];
 
        
        
        $res = $this->partnerTool->applyCash($data);
        if(empty($res) || empty($res["code"])){
            parent::sendOutput('系统繁忙,请稍后重试001', ErrorCode::$paramError);
        }
        if($res["code"]==-1){
            parent::sendOutput($res["msg"], ErrorCode::$paramError);
        }
        parent::sendOutput("提现申请已提交,请耐心等待处理");
        
    }
    
    

    

    /**
     * 添加上下级关系
     * @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());
    }

    

    /**
     * 分销商详情
     */
    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);
    }

}
