yingzi 2 éve
szülő
commit
c0028ad38b

+ 167 - 0
Controller/NewCommission/ApiNewAgent.Class.php

@@ -0,0 +1,167 @@
+<?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 ApiNewAgent extends BaseController
+{
+    
+    private $customerData;
+    private $agentTool;
+
+    public function __construct($isCheckAcl = false, $isMustLogin = true, $checkToken=true)
+    {
+        parent::__construct($isCheckAcl, $isMustLogin, $checkToken);
+        $this->agentTool = new AgentTools($this->onlineEnterpriseId);
+        $this->customerData = $this->agentTool->getCustomerDataByUserCenterId($this->onlineUserId);
+
+    }
+    
+    
+    /**
+     * 获取账户信息
+     */
+    public function getApiAgentData(){
+
+        $data = $this->agentTool->getAndSetAgentData($this->customerData["id"]);
+        if(empty($data) || $data["deleteStatus"]!=5){
+            parent::sendOutput('数据不存在', ErrorCode::$paramError);
+        }
+        parent::sendOutput($data);
+    }
+    /**
+     * 获取分佣明细
+     */
+    public function getApiAgentDetail(){
+        $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;
+        
+        $data = $this->agentTool->getCommissionDetail($this->customerData["id"],$selectParams);
+        parent::sendOutput($data);
+    }
+    /**
+     * 获取分佣账户余额明细
+     */
+    public function getApiAgentBalanceDetail(){
+        $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->agentTool->getCommissionBalanceDetail($this->customerData["id"],$selectParams);
+        parent::sendOutput($data);
+    }
+    /**
+     * 获取提现记录
+     */
+    public function getApiAgentApplyCashList(){
+        $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->agentTool->getPartnerApplyCashList($this->customerData["id"],$selectParams);
+        parent::sendOutput($data);
+    }
+    /**
+     * 获取提现详情
+     */
+    public function getApiAgentApplyCashInfo(){
+        $params = $this->request->getRawJson();
+        if(empty($params) || empty($params["id"])){
+            parent::sendOutput('参数为空', ErrorCode::$paramError);
+        }
+        $where["id"] = $params["id"];
+        $selectParams["where"] = $where;
+        $data = $this->agentTool->getPartnerApplyCashInfo($this->customerData["id"],$selectParams);
+        parent::sendOutput($data);
+            
+    }
+    
+    public function getApiAgentCashType(){
+        $data = $this->agentTool->getBankType(1);
+        parent::sendOutput($data);
+        
+    }
+    
+    public function apiAgentApplyCash(){
+        $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->agentTool->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("提现申请已提交,请耐心等待处理");
+        
+    }
+    
+    
+
+    
+
+
+}

+ 232 - 0
Controller/NewCommission/NewAgent.Class.php

@@ -0,0 +1,232 @@
+<?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);
+    }
+    
+    
+}

+ 1 - 0
Controller/Purchase/Purchase.Class.php

@@ -356,6 +356,7 @@ class Purchase extends BaseController
         isset($params['end']) && $selectParams['end'] = $params['end'];
         isset($params['supplierId']) && $selectParams['supplierId'] = $params['supplierId'];//供应商id
         isset($params['buyerId']) && $selectParams['buyerId'] = $params['buyerId'];//采购员id
+        isset($params['shopId']) && $selectParams['shopId'] = $params['shopId'];//门店id
 
 
         $selectParams['onReturn'] = isset($params['onReturn']) ? $params['onReturn'] : 0;

+ 22 - 0
Controller/Shop/Shop.Class.php

@@ -18,6 +18,8 @@ use JinDouYun\Controller\BaseController;
 use JinDouYun\Model\Shop\MShop;
 use JinDouYun\Cache\TempSaveCache;
 
+use Util\Common\AgentTools;
+
 class Shop extends BaseController
 {
     private $objMShop;
@@ -94,6 +96,26 @@ class Shop extends BaseController
         $shopData['updateTime'] = time();
         $shopData['modelType'] = getArrayItem($params,'modelType',0);
         $shopData['describe'] = isset($params['describe']) ? $params['describe'] : '';
+        
+        
+        
+        //推荐代理设置
+        if(empty($params["agentCustomerId"])){
+            $shopData["agentId"]=0;
+            $shopData["agentCustomerId"]=0;
+        }else{
+            $agentTool = new AgentTools($this->onlineEnterpriseId);
+            $agentData = $agentTool->getAndSetAgentData($params["agentCustomerId"],-1);
+            if(empty($agentData)){
+                $shopData["agentId"]=0;
+                $shopData["agentCustomerId"]=0;
+            }else{
+                $shopData["agentId"]=$agentData["id"];
+                $shopData["agentCustomerId"]=$agentData["customerId"];
+            }
+        }
+        
+        
         return $shopData;
     }
 

+ 33 - 0
Dao/CommissionPartner/DNewAgent.Class.php

@@ -0,0 +1,33 @@
+<?php
+
+/**
+ * 门店代理账户表
+ */
+
+namespace JinDouYun\Dao\CommissionPartner;
+
+use JinDouYun\Dao\BaseDao;
+
+class DNewAgent extends BaseDao
+{
+    public function __construct($serviceDB = 'default')
+    {
+        $this->_table = 'new_agent';
+        $this->_primary = 'id';
+        $this->_fields = [
+            "id",
+            "userCenterId",
+            "customerId",
+            "balance",
+            "waitMoney",
+            "withdraw",
+            "totalMoney",
+            "deleteStatus",
+            "createTime",
+            "updateTime",
+            "expand",
+        ];
+        $this->_readonly = ['id'];
+        parent::__construct($serviceDB);
+    }
+}

+ 38 - 0
Dao/CommissionPartner/DNewAgentBalanceDetail.Class.php

@@ -0,0 +1,38 @@
+<?php
+
+/**
+ * 新分销合伙人余额明细表
+ */
+
+namespace JinDouYun\Dao\CommissionPartner;
+
+use JinDouYun\Dao\BaseDao;
+
+class DNewAgentBalanceDetail extends BaseDao
+{
+    public function __construct($serviceDB = 'default')
+    {
+        $this->_table = 'new_agent_balance_detail';
+        $this->_primary = 'id';
+        $this->_fields = [
+            "id",
+            "type",
+            "title",
+            "code",
+            "money",
+            "content",
+            "admin_id",
+            "customer_id",
+            "agent_id",
+            "user_center_id",
+            "cash_id",
+            "old_balance",
+            "now_balance",
+            "time",
+            "expand",
+            "detail_id",
+        ];
+        $this->_readonly = ['id'];
+        parent::__construct($serviceDB);
+    }
+}

+ 39 - 0
Dao/CommissionPartner/DNewAgentCash.Class.php

@@ -0,0 +1,39 @@
+<?php
+
+/**
+ * 新分销合伙人提现申请表
+ */
+
+namespace JinDouYun\Dao\CommissionPartner;
+
+use JinDouYun\Dao\BaseDao;
+
+class DNewAgentCash extends BaseDao
+{
+    public function __construct($serviceDB = 'default')
+    {
+        $this->_table = 'new_agent_cash';
+        $this->_primary = 'id';
+        $this->_fields = [
+            "id",
+            "customer_id",
+            "user_center_id",
+            "agent_id",
+            "bank_type_code",
+            "bank_type",
+            "bank_num",
+            "bank_name",
+            "bank_ad",
+            "audit_mono",
+            "audit_name",
+            "audit_time",
+            "audit_id",
+            "status",
+            "time",
+            "expand",
+            "money",
+        ];
+        $this->_readonly = ['id'];
+        parent::__construct($serviceDB);
+    }
+}

+ 40 - 0
Dao/CommissionPartner/DNewAgentDetail.Class.php

@@ -0,0 +1,40 @@
+<?php
+
+/**
+ * 新分销合伙人佣金明细表
+ */
+
+namespace JinDouYun\Dao\CommissionPartner;
+
+use JinDouYun\Dao\BaseDao;
+
+class DNewAgentDetail extends BaseDao
+{
+    public function __construct($serviceDB = 'default')
+    {
+        $this->_table = 'new_agent_detail';
+        $this->_primary = 'id';
+        $this->_fields = [
+            "id",
+            "customerId",
+            "userCenterId",
+            "agentId",
+            "sourceShopId",
+            "title",
+            "purchaseMoney",
+            "calcMoney",
+            "commission",
+            "per",
+            "type",
+            "purchaseId",
+            "status",
+            "isUpgrade",
+            "mono",
+            "time",
+            "expand",
+            "purchaseNo"
+        ];
+        $this->_readonly = ['id'];
+        parent::__construct($serviceDB);
+    }
+}

+ 31 - 0
Dao/CommissionPartner/DNewAgentSetting.Class.php

@@ -0,0 +1,31 @@
+<?php
+
+/**
+ * 新分销合伙人表
+ */
+
+namespace JinDouYun\Dao\CommissionPartner;
+
+use JinDouYun\Dao\BaseDao;
+
+class DNewAgentSetting extends BaseDao
+{
+    public function __construct($serviceDB = 'default')
+    {
+        $this->_table = 'new_agent_setting';
+        $this->_primary = 'id';
+        $this->_fields = [
+            "id",
+            "enterpriseId",
+            "levMoney",
+            "levPer",
+            "incomePer",
+            "bankData",
+            "is_open",
+            "updateTime",
+            "time",
+        ];
+        $this->_readonly = ['id'];
+        parent::__construct($serviceDB);
+    }
+}

+ 30 - 0
Dao/CommissionPartner/DNewAgentTask.Class.php

@@ -0,0 +1,30 @@
+<?php
+
+/**
+ * 新分销合伙人表
+ */
+
+namespace JinDouYun\Dao\CommissionPartner;
+
+use JinDouYun\Dao\BaseDao;
+
+class DNewAgentTask extends BaseDao
+{
+    public function __construct($serviceDB = 'default')
+    {
+        $this->_table = 'new_agent_task';
+        $this->_primary = 'id';
+        $this->_fields = [
+            "id",
+            "purchaseId",
+            "title",
+            "mono",
+            "status",
+            "run_time",
+            "time",
+            "enterpriseId",
+        ];
+        $this->_readonly = ['id'];
+        parent::__construct($serviceDB);
+    }
+}

+ 2 - 1
Dao/Shop/DShop.Class.php

@@ -47,7 +47,8 @@ class DShop extends BaseDao
             "describe", //varchar(255) DEFAULT '' COMMENT '店铺描述',
             "extends",
             "modelType",
-            "agentId"
+            "agentId",
+            "agentCustomerId"
         ];
         $this->_readonly = ['id'];
         $this->_create_autofill = [

+ 9 - 0
Model/Purchase/MPurchase.Class.php

@@ -42,6 +42,11 @@ use JinDouYun\Dao\Shop\DShop;
 use Mall\Framework\Factory;
 use const Grpc\STATUS_ABORTED;
 
+
+use Util\Common\AgentTools;
+
+
+
 class MPurchase extends MBaseModel
 {
     private $objDPurchase;
@@ -368,6 +373,10 @@ class MPurchase extends MBaseModel
         $returnData = $dbResult;
         unset($dbResult);
 
+        if($updateData['inStatus']==5){
+            AgentTools::addTask($where['id'], $this->onlineEnterpriseId);
+        }
+        
         return ResultWrapper::success($returnData);
     }
 

+ 1039 - 0
Util/Common/AgentTools.Class.php

@@ -0,0 +1,1039 @@
+<?php
+
+namespace Util\Common;
+use Mall\Framework\Factory;
+use JinDouYun\Dao\Customer\DCustomer;
+use JinDouYun\Dao\UserCenter\DUserCenter;
+use JinDouYun\Cache\CustomerCache;
+use JinDouYun\Dao\Order\DOrderIndex;
+use JinDouYun\Dao\Order\DOrder;
+use JinDouYun\Dao\Shop\DShop;
+use JinDouYun\Dao\Purchase\DPurchase;
+
+use JinDouYun\Dao\CommissionPartner\DNewAgentBalanceDetail;
+use JinDouYun\Dao\CommissionPartner\DNewAgentCash;
+use JinDouYun\Dao\CommissionPartner\DNewAgentDetail;
+use JinDouYun\Dao\CommissionPartner\DNewAgent;
+use JinDouYun\Dao\CommissionPartner\DNewAgentTask;
+use JinDouYun\Dao\CommissionPartner\DNewAgentSetting;
+
+
+class AgentTools{
+    private $dbCustomer;
+    private $dbUserCenter;
+    private $dbShop;
+    private $dbPurchase;
+
+    private $dbNewAgent;
+    private $dbNewAgentDetail;
+    private $dbNewAgentCash;
+    private $dbNewAgentBalanceDetail;
+    private $dbNewAgentTask;
+    private $dbNewAgentSetting;
+    
+    private $enterpriseId;
+    private $cache;
+    private $cutTable = 1;//客户按照企业id分表
+    private $agentSettingData = null;
+
+
+    private $codeAr = [
+        "purchase_calc"=>"采购单完成分佣",
+        "agent_calc"=>"子级代理收益分佣",
+        "cash_balance"=>"门店代理余额提现",
+        "cash_no_balance"=>"提现驳回返还余额",
+    ];
+    
+    
+    
+    public function __construct($enterpriseId){
+        $this->enterpriseId = $enterpriseId;
+        $this->cache = Factory::cache('systask');
+        //用户表
+        $this->dbCustomer = new DCustomer('default');
+        $customerTableName = $this->dbCustomer->getTableName($this->dbCustomer->get_Table(), $this->enterpriseId, $this->cutTable);
+        $this->dbCustomer->setTable($customerTableName);
+        //用户账号表
+        $this->dbUserCenter = new DUserCenter();
+        
+        //设置表
+        $this->dbNewAgentSetting = new DNewAgentSetting('default');
+        $this->agentSettingData = $this->dbNewAgentSetting->get(["enterpriseId"=>$this->enterpriseId]);
+        
+        //门店表
+        $this->dbShop = new DShop();
+        $this->dbShop->setTable('qianniao_shop_1');
+        
+        //采购单表
+        $this->dbPurchase = new DPurchase();
+        $this->dbPurchase->setTable('qianniao_purchase_'.$this->enterpriseId);
+        
+        //新分销门店代理表
+        $this->dbNewAgent = new DNewAgent('default');
+        $newCommissionPartnerTableName = $this->dbNewAgent->getTableName($this->dbNewAgent->get_Table(), $this->enterpriseId, $this->cutTable);
+        $this->dbNewAgent->setTable($newCommissionPartnerTableName);
+        
+        //新分销佣金明细表
+        $this->dbNewAgentDetail = new DNewAgentDetail('default');
+        $newCommissionDetailTableName = $this->dbNewAgentDetail->getTableName($this->dbNewAgentDetail->get_Table(), $this->enterpriseId, $this->cutTable);
+        $this->dbNewAgentDetail->setTable($newCommissionDetailTableName);
+        
+        //新分销门店代理提现表
+        $this->dbNewAgentCash = new DNewAgentCash('default');
+        $newCommissionCashTableName = $this->dbNewAgentCash->getTableName($this->dbNewAgentCash->get_Table(), $this->enterpriseId, $this->cutTable);
+        $this->dbNewAgentCash->setTable($newCommissionCashTableName);
+        
+        //新分销门店代理余额明细表
+        $this->dbNewAgentBalanceDetail = new DNewAgentBalanceDetail('default');
+        $newCommissionBalanceDetailTableName = $this->dbNewAgentBalanceDetail->getTableName($this->dbNewAgentBalanceDetail->get_Table(), $this->enterpriseId, $this->cutTable);
+        $this->dbNewAgentBalanceDetail->setTable($newCommissionBalanceDetailTableName);
+        
+        
+        //新分销门店代理分佣任务表
+        $this->dbNewAgentTask = new DNewAgentTask('default');
+        $newCommissionTaskTableName = $this->dbNewAgentTask->getTableName($this->dbNewAgentTask->get_Table(), $this->enterpriseId, $this->cutTable);
+        $this->dbNewAgentTask->setTable($newCommissionTaskTableName);
+    }
+    public function cacheTest(){
+        $key = "agenttaskcache".'::'.$this->enterpriseId;
+        $result = $this->cache->get($key);
+        if(!empty($result)){
+            return "重复操作";
+        }
+        $this->cache->set($key,1,10);
+        return "开始执行操作";
+    }
+    
+    /**
+     * 添加任务
+     * @param type $purchaseId
+     * @param type $enterpriseId
+     * @return bool
+     */
+    public static function addTask($purchaseId,$enterpriseId){
+        try{
+            if(empty($purchaseId) || empty($enterpriseId)){
+                return ["code"=>-1,"msg"=>"参数为空"];
+            }
+            //获取配置
+            $dbSetting = new DNewAgentSetting('default');
+            $settingData = $dbSetting->get(["enterpriseId"=>$enterpriseId]);
+            if(empty($settingData) || empty($settingData["is_open"]) || $settingData["incomePer"]<=0){
+                return ["code"=>-1,"msg"=>"还未进行代理推荐分佣配置"];
+            }
+            //获取采购单信息
+            $dbPurchase = new DPurchase();
+            $dbPurchase->setTable('qianniao_purchase_'.$enterpriseId);
+            $pwhere=[];
+            $pwhere["id"]=$purchaseId;
+            $pwhere["auditStatus"]=2;//审核状态
+            $pwhere["deleteStatus"]=5;//删除状态
+            $pwhere["inStatus"]=5;//入库状态
+            $pwhere["purchaseType"]=4;//采购订单
+            $purchaseData = $dbPurchase->get($pwhere);
+            if(empty($purchaseData) || empty($purchaseData["shopId"])){
+                return ["code"=>-1,"msg"=>"采购单不符合条件".$purchaseData["shopId"]];
+            }
+            //验证门店
+            $shopDb = new DShop();
+            $shopDb->setTable('qianniao_shop_1');
+            $shopData = $shopDb->get(["id"=>$purchaseData["shop_id"],"enterpriseId"=>$enterpriseId]);
+            if(empty($shopData) || empty($shopData["agentId"])){
+                return ["code"=>-1,"msg"=>"当前门店没有代理推荐"];
+            }
+            //添加任务
+            $dbTask = new DNewAgentTask('default');
+            $taskTableName = $dbTask->getTableName($dbTask->get_Table(), $enterpriseId, 1);
+            $dbTask->setTable($taskTableName);
+            //验证采购单是否已经添加过任务
+            $count = $dbTask->count(["purchaseId"=>$purchaseId]);
+            if($count>0){
+                return ["code"=>-1,"msg"=>"任务采购单已存在"];
+            }
+            $r = $dbTask->insert([
+                "purchaseId"=>$purchaseId,
+                "enterpriseId"=>$enterpriseId,
+                "title"=>"采购单完成计算分佣",
+                "mono"=>"",
+                "status"=>0,
+                "run_time"=>0,
+                "time"=>time(),
+            ]);
+            return ["code"=>1,"msg"=>"任务已插入[{$r}]"];
+        } catch (\Exception $e){
+            return ["code"=>-1,"msg"=>"系统错误"];
+        }
+    }
+    
+    public function runTask(){
+        if(empty($this->agentSettingData) || empty($this->agentSettingData["is_open"])){
+            return ["code"=>-1,"msg"=>"未开启配置"];
+        }
+        $key = "agenttaskcache".'::'.$this->enterpriseId;
+        $result = $this->cache->get($key);
+        if(!empty($result)){
+            return ["code"=>-1,"msg"=>"操作频繁"];
+        }
+        $this->cache->set($key,1,10);
+        $dbTask = new DNewAgentTask('default');
+        $taskTableName = $dbTask->getTableName($dbTask->get_Table(), $this->enterpriseId, 1);
+        $dbTask->setTable($taskTableName);
+        $data = $dbTask->select(["status"=>0],"*","id asc");
+        if(empty($data)){
+            return ["code"=>-1,"msg"=>"没有可执行任务"];
+        }
+        $nowTime=time();
+        foreach($data as $k=>$v){
+            $res = $this->runCalcMoneyData($v["purchaseId"]);
+            $save=[];
+            $save=["status"=>1,"mono"=>"执行成功","run_time"=>$nowTime];
+            if(empty($res) || $res["code"]==-1){
+                $save["mono"] = empty($res["msg"])?"系统错误执行失败":$res["msg"];
+                $save["status"] = -1;
+            }
+            $dbTask->update($save, ["id"=>$v["id"]]);
+        }
+        $this->cache->set($key,null);
+        return ["code"=>1,"msg"=>"执行结束"];
+        
+    }
+    
+    /**
+     * 设置门店推广用户
+     * @param type $parentId
+     * @param type $shopId
+     * @return bool
+     */
+    public function setPushCustomer($agentCustomerId=0,$shopId=0){
+        if($agentCustomerId == 0 || $shopId == 0){
+            return ["code"=>-1,"msg"=>"参数错误","data"=>null];
+        }
+        //门店信息
+        $shopData = $this->dbShop->get(["id"=>$shopId,"enterpriseId"=>$this->enterpriseId]);
+        if(empty($shopData) || $shopData["agentId"]>0){
+            return ["code"=>-1,"msg"=>"门店不能被绑定","data"=>$shopData];
+        }
+        
+        //父级用户信息
+        $customeerData = $this->dbCustomer->get($agentCustomerId);
+        if(empty($customeerData)){
+            return ["code"=>-1,"msg"=>"代理用户不存在","data"=>null];
+        }
+        $this->dbShop->beginTransaction();
+        $agentData = $this->getAndSetAgentData($customeerData["id"],$customeerData["userCenterId"]);
+        if(empty($agentData)){
+            $this->dbShop->rollBack();
+            return ["code"=>-1,"msg"=>"代理商账户创建失败","data"=>null];
+        }
+        //保存门店数据
+        $res = $this->dbShop->update(["agentId"=>$agentData["id"],"agentCustomerId"=>$agentData["customerId"]], ["id"=>$shopId]);
+        if(empty($res)){
+            $this->dbShop->rollBack();
+            return ["code"=>-1,"msg"=>"绑定失败","data"=>$childSave];
+        }else{
+            $this->dbShop->commit();
+            return ["code"=>1,"msg"=>"绑定成功","data"=>null];
+        }
+    }
+    /**
+     * 获取代理商信息如果不存在则创建
+     * @param type $customerId
+     * @param type $userCenterId
+     * @return bool
+     */
+    public function getAndSetAgentData($customerId,$userCenterId=0){
+        if(empty($customerId)){
+            return false;
+        }
+        if($userCenterId==-1){
+            $customerData = $this->dbCustomer->get($customerId);
+            if(!empty($customerData)){
+                $userCenterId = $customerData["userCenterId"];
+            }else{
+                $userCenterId = 0;
+            }
+        }
+        $agentData = $this->dbNewAgent->get(["customerId"=>$customerId]);
+        if(empty($userCenterId) && empty($agentData)){
+            return false;
+        }
+        if(!empty($userCenterId) && empty($agentData)){
+            $nowTime = time();
+            $agentData=[
+                "userCenterId"=>$userCenterId,
+                "customerId"=>$customerId,
+                "balance"=>0,
+                "waitMoney"=>0,
+                "withdraw"=>0,
+                "totalMoney"=>0,
+                "deleteStatus"=>5,
+                "createTime"=>$nowTime,
+                "updateTime"=>$nowTime,
+                "expand"=>"",
+            ];
+            $agentId = $this->dbNewAgent->insert($agentData);
+            if(empty($agentId)){
+                return false;
+            }
+            $agentData["id"] = $agentId;
+            $agentData["purchaseNum"] = 0;
+        }else{
+            $agentData["purchaseNum"] = $this->getCommissionDetailCount($agentData["id"]);
+        }
+        return $agentData;
+    }
+    public function getCommissionDetailCount($agentId){
+        if(empty($agentId) && $type=0){
+            return 0;
+        }
+        $where=[];
+        if(!empty($agentId)){
+            $where["agentId"] = $agentId;
+        }
+        $count = $this->dbNewAgentDetail->count($where);
+        return $count?$count:0;
+    }
+
+    /**
+     * 采购单消费计算佣金
+     * @param type $purchaseId
+     * @param type $orderMoney
+     * @param type $isPart 采购单用户如果是门店代理是否计算
+     * @return bool
+     */
+    public function runCalcMoneyData($purchaseId){
+        
+        if(empty($this->agentSettingData) || empty($this->agentSettingData["is_open"])){
+            return ["code"=>-1,"msg"=>"配置信息错误"];
+        }
+        //获取采购单信息
+        if(empty($purchaseId)){
+            return ["code"=>-1,"msg"=>"采购单id错误"];
+        }
+        $pwhere=[];
+        $pwhere["id"]=$purchaseId;
+        $pwhere["auditStatus"]=2;//审核状态
+        $pwhere["deleteStatus"]=5;//删除状态
+        $pwhere["inStatus"]=5;//入库状态
+        $pwhere["purchaseType"]=4;//采购订单
+        $purchaseData = $this->dbPurchase->get($pwhere);
+        if(empty($purchaseData) || empty($purchaseData["shopId"])){
+            return ["code"=>-1,"msg"=>"采购单数据不存在或不是门店采购单".$purchaseData["shopId"]];
+        }
+        $purchaseShopId = $purchaseData["shopId"];
+        $shopData = $this->dbShop->get(["id"=>$purchaseShopId,"enterpriseId"=>$this->enterpriseId,"deleteStatus"=>5]);
+        if(empty($shopData) || empty($shopData["agentId"])){
+            return ["code"=>-1,"msg"=>"门店被删除或者没有推荐代理"];
+        }
+        //开始计算
+        $purchaseAmount = $purchaseData["purchaseAmount"];//采购金额
+        $couponAmount   = $purchaseData["couponAmount"];//优惠金额
+        $otherAmount    = $purchaseData["otherAmount"];//其它金额
+//        $purchaseMoney = $purchaseAmount;
+        $purchaseMoney = $purchaseAmount + $otherAmount - $couponAmount;
+        if($purchaseMoney<=0){
+            return ["code"=>-1,"msg"=>"采购实际金额小于等于0"];
+        }
+        $agentData = $this->dbNewAgent->get(["id"=>$shopData["agentId"]]);
+        if(empty($agentData)){
+            return ["code"=>-1,"msg"=>"推荐代理账号不存在"];
+        }
+        $data=[];
+        $nowTime = time();
+        //计算收益
+        $per = $this->agentSettingData["incomePer"];
+        $commission = bcmul($purchaseMoney,$per,2);
+        //记录收益
+        $data[]=[
+            "purchaseMoney"=>$purchaseMoney,//采购单支付金额
+            "calcMoney"=>$purchaseMoney,//佣金计算金额
+            "sourceShopId"=>$purchaseShopId,//来源门店id
+            "agentId"=>$agentData["id"],//收钱门店代理账号id
+            "customerId"=>$agentData["customerId"],//收钱门店代理
+            "userCenterId"=>$agentData["userCenterId"],//收钱门店代理
+            "commission"=>$commission,//佣金金额
+            "per"=>$per,//佣金比例
+            "type"=>0,
+            "purchaseId"=>$purchaseData["id"],
+            "purchaseNo"=>$purchaseData["no"],
+            "status"=>0,//暂时不需要
+            "title"=>"门店采购单分佣",
+            "isUpgrade"=>0,
+            "mono"=>"",
+            "time"=>$nowTime,
+        ];
+        $num=0;
+        //开启事务
+        $this->dbNewAgentDetail->beginTransaction();
+        $lms = "";
+        foreach($data as $k=>$v){
+            $count = $this->dbNewAgentDetail->count(["purchaseId"=>$v["purchaseId"],"agentId"=>$v["agentId"]]);
+            if($count>0){
+                $lms.="当前采购单已计算佣金[{$v['purchaseId']}];";
+                continue;
+            }
+            $nid = $this->dbNewAgentDetail->insert($v);
+            if(empty($nid)){
+                $lms.="佣金明细插入失败[{$v['purchaseId']}];";
+                continue;
+            }
+            //查询门店代理账户
+            $agentItem = $this->dbNewAgent->get(["id"=>$v["agentId"]]);
+            if(empty($agentItem)){
+                $num = 0;
+                $lms.="门店代理账户不存在[{$v['purchaseId']}];";
+                break;
+            }
+            $update = [
+                'balance'    => bcadd($agentItem['balance'], $v["commission"], 2),
+                'totalMoney' => bcadd($agentItem['totalMoney'], $v["commission"], 2),
+                'updateTime' => $nowTime,
+            ];
+            //添加账户余额明细记录
+            $dres = $this->dbNewAgentBalanceDetail->insert([
+                "type"=>1,
+                "title"=>$v["title"],
+                "code"=>$v["type"]==1 ? "agent_calc" : "purchase_calc",
+                "money"=>$v["commission"],
+                "content"=>$v["title"],
+                "admin_id"=>0,
+                "customer_id"=>$v["customerId"],
+                "agent_id"=>$v["agentId"],
+                "user_center_id"=>$v["userCenterId"],
+                "cash_id"=>0,
+                "old_balance"=>$agentItem["balance"],
+                "now_balance"=>$update["balance"],
+                "time"=>$nowTime,
+                "expand"=>"",
+                "detail_id"=>$nid,
+            ]);
+            if(empty($dres)){
+                $num = 0;
+                $lms.="账户余额明细插入失败[{$v['purchaseId']}];";
+                break;
+            }
+            //更新账户余额
+            $upRes = $this->dbNewAgent->update($update, ['id' => $v["agentId"]]);
+            if(empty($upRes)){
+                $num = 0;
+                $lms.="更新账户余额失败[{$v['purchaseId']}];";
+                break;
+            }
+            $num++;
+        }
+        if($num<=0){
+            $this->dbNewAgentDetail->rollBack();
+            return ["code"=>-1,"msg"=>"数据更新失败:".$lms];
+        }
+        $this->dbNewAgentDetail->commit();
+        return ["code"=>1,"msg"=>"执行成功".$lms];
+    }
+    
+   
+    
+    
+    /**
+     * 获取分佣明细
+     */
+    public function getCommissionDetail($customerId,$parms){
+        $where=[];
+        if(!empty($parms["where"])){
+            $where= $parms["where"];
+        }
+        if(!empty($customerId)){
+            $where["customerId"] = $customerId;
+        }
+        
+        $data = $this->dbNewAgentDetail->select($where, "*", "id desc", $parms["limit"], $parms["offset"]);
+        $total = $this->dbNewAgentDetail->count($where);
+        if(empty($data)){
+            $data = [];
+        }
+        foreach($data as $k=>$v){
+            $itemShop = $this->dbCustomer->get(["id"=>$v["sourceShopId"]],"id,name,logo");
+            if(empty($itemShop)){
+                $data[$k]["name"] = "";
+                $data[$k]["logo"] = "";
+            }else{
+                $data[$k]["name"] = $itemShop["name"];
+                $data[$k]["logo"] = $itemShop["logo"];
+            }
+        }
+        $return = [
+            'data'   => $data,
+            'total'  => (isset($total)&&!empty($total)) ? intval($total) : 0,
+        ];
+        return $return;
+    }
+    /**
+     * 获取门店代理账户余额明细
+     */
+    public function getCommissionBalanceDetail($customerId,$parms){
+        $where=[];
+        if(!empty($customerId)){
+            $where["customer_id"] = $customerId;
+        }
+        $data = $this->dbNewAgentBalanceDetail->select($where, "*", "id desc", $parms["limit"], $parms["offset"]);
+        $total = $this->dbNewAgentBalanceDetail->count($where);
+        if(empty($data)){
+            $data = [];
+        }
+        $return = [
+            'data'   => $data,
+            'total'  => (isset($total)&&!empty($total)) ? intval($total) : 0,
+        ];
+        return $return;
+    }
+    
+    /**
+     * 
+     * @param type $type 0返回标题数组,1范围完整数组
+     * @return type
+     */
+    public static function getBankType($type=0,$title=""){
+        $data = [
+            ["title"=>"支付宝","code"=>"alipay","icon"=>"ibonumidd17","addefault"=>"支付宝"],
+            ["title"=>"微信钱包","code"=>"wxpay","icon"=>"ibonweixinzhifu","addefault"=>"微信"],
+            ["title"=>"银行卡","code"=>"bank","icon"=>"ibonyinhangqia","addefault"=>""],
+        ];
+        
+        if(!empty($title)){
+            foreach($data as $k=>$v){
+                if($v["title"]==$title){
+                    return $v;
+                }
+            }
+            return [];
+        }
+        if($type==1){
+            return $data;
+        }
+        $nameAr= [];
+        foreach($data as $k=>$v){
+            $nameAr[]=$v["title"];
+        }
+        return $nameAr;
+    }
+    
+    
+    /**
+     * 提现申请
+     */
+    public function applyCash($data){
+        if(empty($data) || empty($data["customerId"]) || empty($data["money"]) || empty($data["bank_type"]) || empty($data["bank_name"]) || empty($data["bank_num"]) || empty($data["bank_ad"])){
+            return ["code"=>-1,"msg"=>"请填写完整信息"];
+        }
+        //频繁操作限制
+        $key = "agentCashCache_".$data["customerId"].'::'.$this->enterpriseId;
+        $result = $this->cache->get($key);
+        if(!empty($result)){
+            return ["code"=>-1,"msg"=>"操作频繁,请稍后尝试!"];
+        }
+        
+        $count = $this->dbNewAgentCash->count(["status"=>0,"customer_id"=>$data["customerId"]]);
+        if($count>0){
+            return ["code"=>-1,"msg"=>"上次申请还在处理中"];
+        }
+        
+        $this->cache->set($key,1,5);
+        if(!in_array($data["bank_type"], self::getBankType())){
+            return ["code"=>-1,"msg"=>"提现方式不存在"];
+        }
+        $bankTypeData = self::getBankType(1, $data["bank_type"]);
+        if(empty($bankTypeData)){
+            return ["code"=>-1,"msg"=>"提现方式不存在"];
+        }
+        if(!is_numeric($data["money"])){
+            return ["code"=>-1,"msg"=>"提现金额错误"];
+        }
+        $data["money"] = bcadd($data["money"],0,2);
+        if($data["money"]<=0){
+            return ["code"=>-1,"msg"=>"提现金额错误"];
+        }
+        $agentData = $this->dbNewAgent->get(["customerId"=>$data["customerId"]]);
+        if(empty($agentData) || empty($agentData["id"])){
+            return ["code"=>-1,"msg"=>"门店代理不存在"];
+        }
+        if($data["money"]>$agentData["balance"]  || $agentData["balance"]<=0){
+            return ["code"=>-1,"msg"=>"提现金额不足"];
+        }
+        $nowTime = time();
+        $save=[
+            "customer_id"    => $agentData["customerId"],
+            "user_center_id" => $agentData["userCenterId"],
+            "agent_id"       => $agentData["id"],
+            "bank_type"      => $data["bank_type"],
+            "bank_type_code" => $bankTypeData["code"],
+            "bank_name"      => $data["bank_name"],
+            "bank_num"       => $data["bank_num"],
+            "bank_ad"        => $data["bank_ad"],
+            "money"          => $data["money"],
+            "status"         => 0,
+            "time"           => $nowTime
+        ];
+        
+        try{
+            $this->dbNewAgent->beginTransaction();
+            //插入提现申请
+            $applyId = $this->dbNewAgentCash->insert($save);
+            if(empty($applyId)){
+                return ["code"=>-1,"msg"=>"数据提交失败001"];
+            }
+            //更新提现账户余额并添加明细
+            $update = [
+                'balance'    => bcsub($agentData["balance"], $save["money"], 2),//账户余额
+                'withdraw'   => bcadd($agentData["withdraw"], $save["money"],2),//已提现金额
+                'updateTime' => $nowTime,
+            ];
+            if($update["balance"]<=0){
+                $this->dbNewAgent->rollBack();
+                return ["code"=>-1,"msg"=>"数据提交失败002"];
+            }
+            //添加账户余额明细记录
+            $dres = $this->dbNewAgentBalanceDetail->insert([
+                "type"=>-1,
+                "title"=>"余额提现",
+                "code"=>"cash_balance",
+                "money"=>$save["money"],
+                "content"=>"门店代理余额提现",
+                "admin_id"=>0,
+                "customer_id"=>$agentData["customerId"],
+                "agent_id"=>$agentData["id"],
+                "user_center_id"=>$agentData["userCenterId"],
+                "cash_id"=>$applyId,
+                "old_balance"=>$agentData["balance"],
+                "now_balance"=>$update["balance"],
+                "time"=>$nowTime,
+                "expand"=>"",
+                "detail_id"=>0,
+            ]);
+            if(empty($dres)){
+                $this->dbNewAgent->rollBack();
+                return ["code"=>-1,"msg"=>"账户余额明细插入失败"];
+            }
+            //更新账户余额
+            $upRes = $this->dbNewAgent->update($update, ['id' => $agentData["id"]]);
+            if(empty($upRes)){
+                $this->dbNewAgent->rollBack();
+                return ["code"=>-1,"msg"=>"更新账户余额失败"];
+            }
+            $this->dbNewAgent->commit();
+            $this->cache->set($key,null);
+            return ["code"=>1,"msg"=>"提现申请提交成功"];
+        } catch (\Exception $e){
+            $this->dbNewAgent->rollBack();
+            $this->cache->set($key,null);
+            return ["code"=>1,"msg"=>"提现申请提交失败"];
+        }
+        
+    }
+    /**
+     * 获取提现记录
+     * @param type $customerId
+     * @param type $parms
+     * @return type
+     */
+    public function getPartnerApplyCashList($customerId,$parms){
+        $where=[];
+        if(!empty($parms["where"])){
+            $where= $parms["where"];
+        }
+        if(!empty($customerId)){
+            $where["customer_id"] = $customerId;
+        }
+        $data = $this->dbNewAgentCash->select($where, "*", "id desc", $parms["limit"], $parms["offset"]);
+        $total = $this->dbNewAgentCash->count($where);
+        if(empty($data)){
+            $data = [];
+        }
+        $return = [
+            'data'   => $data,
+            'total'  => (isset($total)&&!empty($total)) ? intval($total) : 0,
+        ];
+        return $return;
+    }
+    /**
+     * 获取提现详情
+     */
+    public function getPartnerApplyCashInfo($customerId,$parms){
+        $where=[];
+        if(!empty($parms["where"])){
+            $where= $parms["where"];
+        }
+        if(!empty($customerId)){
+            $where["customer_id"] = $customerId;
+        }
+        $data = $this->dbNewAgentCash->get($where);
+        return $data;
+    }
+    
+    /**
+     * 获取门店代理列表
+     * @param type $params
+     */
+    public function getPartnerList($params = []){
+        $customerTableName = 'qianniao_customer_'.$this->enterpriseId;
+        $userCenterTableName = 'qianniao_user_center';
+        $newCommissionPartnerTableName = 'qianniao_new_agent_'.$this->enterpriseId;
+        //查询数据
+        $field = "p.*,u.mobile,c.name,c.avatar";
+        $sql = "select {$field} from {$newCommissionPartnerTableName} p "
+        . "left join {$userCenterTableName} u on u.id = p.userCenterId "
+        . "left join {$customerTableName} c on c.id = p.customerId";
+        $whereSql = ' where p.deleteStatus = 5 ';
+        if(isset($params['search'])){
+            $whereSql .= ' and (u.mobile like "%'.$params['search'].'%" or c.name like "%'.$params['search'].'%") ';
+        }
+        $orderSql = ' order by p.id desc ';
+        $limitSql = ' limit '.$params['offset'].','.$params['limit'];
+        $querySql = $sql.$whereSql.$orderSql.$limitSql;
+        $data = $this->dbNewAgent->query($querySql);
+        if(empty($data)){
+            $data=[];
+        }
+        //查询总数
+        $countSql = "select count(*) as `count` from {$newCommissionPartnerTableName} p "
+        . "left join {$userCenterTableName} u on u.id = p.userCenterId "
+        . "left join {$customerTableName} c on c.id = p.customerId";
+        $countData = $this->dbNewAgent->query($countSql.$whereSql);
+        $total = 0;
+        if(!empty($countData)){
+            $total =array_shift($countData)['count'];
+        }
+        return ['data'=>$data,'total'=>($total) ? intval($total) : 0];
+    }
+    
+    /**
+     * 获取门店代理余额明细列表
+     * @param type $params
+     */
+    public function getBalanceDetailList($params = []){
+        $customerTableName = 'qianniao_customer_'.$this->enterpriseId;
+        $userCenterTableName = 'qianniao_user_center';
+        $newCommissionBalanceDetailTableName = 'qianniao_new_agent_balance_detail_'.$this->enterpriseId;
+        //查询数据
+        $field = "p.*,u.mobile,c.name,c.avatar";
+        $sql = "select {$field} from {$newCommissionBalanceDetailTableName} p "
+        . "left join {$userCenterTableName} u on u.id = p.user_center_id "
+        . "left join {$customerTableName} c on c.id = p.customer_id";
+        $whereSql = ' where 1=1 ';
+        if(!empty($params['agentId'])){
+            $whereSql .= ' and p.agent_id = '.$params['agentId'];
+        }
+        
+        if(isset($params['search'])){
+            $whereSql .= ' and (u.mobile like "%'.$params['search'].'%" or c.name like "%'.$params['search'].'%") ';
+        }
+        $orderSql = ' order by p.id desc ';
+        $limitSql = ' limit '.$params['offset'].','.$params['limit'];
+        $querySql = $sql.$whereSql.$orderSql.$limitSql;
+        $data = $this->dbNewAgent->query($querySql);
+        if(empty($data)){
+            $data=[];
+        }
+        //查询总数
+        $countSql = "select count(*) as `count` from {$newCommissionBalanceDetailTableName} p "
+        . "left join {$userCenterTableName} u on u.id = p.user_center_id "
+        . "left join {$customerTableName} c on c.id = p.customer_id";
+        $countData = $this->dbNewAgent->query($countSql.$whereSql);
+        $total = 0;
+        if(!empty($countData)){
+            $total =array_shift($countData)['count'];
+        }
+        return ['data'=>$data,'total'=>($total) ? intval($total) : 0];
+    }
+    /**
+     * 获取门店代理佣金记录
+     * @param type $params
+     */
+    public function getDetailList($params = []){
+        $customerTableName = 'qianniao_customer_'.$this->enterpriseId;
+        $userCenterTableName = 'qianniao_user_center';
+        $newCommissionDetailTableName = 'qianniao_new_agent_detail_'.$this->enterpriseId;
+        $shopTableName = "qianniao_new_shop_1";
+        //查询数据
+        $field = "p.*,u.mobile,c.name,c.avatar,s.name as sourceName ";
+        $sql = "select {$field} from {$newCommissionDetailTableName} p "
+        . "left join {$userCenterTableName} u on u.id = p.userCenterId "
+        . "left join {$customerTableName} c on c.id = p.customerId "
+        . "left join {$shopTableName} s on s.id = p.sourceShopId";
+        
+        $whereSql = ' where 1=1 ';
+        if(!empty($params['agentId'])){
+            $whereSql .= ' and p.agentId = '.$params['agentId'];
+        }
+        if(isset($params['search'])){
+            $whereSql .= ' and (u.mobile like "%'.$params['search'].'%" or c.name like "%'.$params['search'].'%") ';
+        }
+        $orderSql = ' order by p.id desc ';
+        $limitSql = ' limit '.$params['offset'].','.$params['limit'];
+        $querySql = $sql.$whereSql.$orderSql.$limitSql;
+        $data = $this->dbNewAgent->query($querySql);
+        if(empty($data)){
+            $data=[];
+        }
+        //查询总数
+        $countSql = "select count(*) as `count` from {$newCommissionDetailTableName} p "
+        . "left join {$userCenterTableName} u on u.id = p.userCenterId "
+        . "left join {$customerTableName} c on c.id = p.customerId "
+        . "left join {$shopTableName} s on s.id = p.sourceShopId";
+        $countData = $this->dbNewAgent->query($countSql.$whereSql);
+        $total = 0;
+        if(!empty($countData)){
+            $total =array_shift($countData)['count'];
+        }
+        return ['data'=>$data,'total'=>($total) ? intval($total) : 0];
+    }
+    
+    /**
+     * 获取提现记录
+     * @param type $params
+     */
+    public function getPartnerCashList($params = []){
+        $customerTableName = 'qianniao_customer_'.$this->enterpriseId;
+        $userCenterTableName = 'qianniao_user_center';
+        $newCommissionCashTableName = 'qianniao_new_agent_cash_'.$this->enterpriseId;
+        //查询数据
+        $field = "p.*,u.mobile,c.name,c.avatar ";
+        $sql = "select {$field} from {$newCommissionCashTableName} p "
+        . "left join {$userCenterTableName} u on u.id = p.user_center_id "
+        . "left join {$customerTableName} c on c.id = p.customer_id ";
+        
+        $whereSql = ' where 1=1 ';
+        if(!empty($params['agentId'])){
+            $whereSql .= ' and p.agent_id = '.$params['agentId'];
+        }
+        if(isset($params['search'])){
+            $whereSql .= ' and (u.mobile like "%'.$params['search'].'%" or c.name like "%'.$params['search'].'%") ';
+        }
+        if(isset($params['status'])){
+            $whereSql .= ' and p.status = '.$params['status'];
+        }
+        if(!empty($params['bank_type_code'])){
+            $whereSql .= " and p.bank_type_code = '{$params['bank_type_code']}'";
+        }
+        if(!empty($params['start_time']) && !empty($params['end_time'])){
+            $whereSql .= " and p.time >= {$params['start_time']} and p.time < {$params['end_time']}";
+        }
+        
+        $orderSql = ' order by p.id desc ';
+        $limitSql = ' limit '.$params['offset'].','.$params['limit'];
+        $querySql = $sql.$whereSql.$orderSql.$limitSql;
+        $data = $this->dbNewAgent->query($querySql);
+        if(empty($data)){
+            $data=[];
+        }
+        //查询总数
+        $countSql = "select count(*) as `count` from {$newCommissionCashTableName} p "
+        . "left join {$userCenterTableName} u on u.id = p.user_center_id "
+        . "left join {$customerTableName} c on c.id = p.customer_id ";
+        $countData = $this->dbNewAgent->query($countSql.$whereSql);
+        $total = 0;
+        if(!empty($countData)){
+            $total =array_shift($countData)['count'];
+        }
+        return ['data'=>$data,'total'=>($total) ? intval($total) : 0];
+    }
+    
+    /**
+     * 提现处理
+     * @param type $id
+     * @param type $mono
+     * @param type $type
+     */
+    public function partnerCashAudit($id,$mono,$type,$audit_id=0){
+        if(empty($id)|| empty($mono) || empty($type) || !in_array($type, [1,-1])){
+            return ["code"=>-1,"msg"=>"参数错误"];
+        }
+        
+        $adminData = $this->dbUserCenter->get($this->onlineUserId);
+        if(empty($adminData)){
+            $adminData=["audit_name"=>"管理人员"];
+        }     
+        
+        //频繁操作限制
+        $key = "agentCashDealCache_".$id.'::'.$this->enterpriseId;
+        $result = $this->cache->get($key);
+        if(!empty($result)){
+            return ["code"=>-1,"msg"=>"处理中,请耐心等待!"];
+        }
+        $this->cache->set($key,1,5);
+        $where = ["id"=>$id,"status"=>0];
+        $data = $this->dbNewAgentCash->get($where);
+        if(empty($data)){
+            $this->cache->set($key,null);
+            return ["code"=>-1,"msg"=>"数据不存在"];
+        }
+        //门店代理账户信息
+        $agentData = $this->dbNewAgent->get(["id"=>$data["agent_id"]]);
+        if(empty($agentData)){
+            $this->cache->set($key,null);
+            return ["code"=>-1,"msg"=>"门店代理账户不存在"];
+        }
+        //打款成功
+        if($type==1){
+            $res = $this->dbNewAgentCash->update([
+                "status"=>1,
+                "audit_time"=>time(),
+                "audit_mono"=>$mono,
+                "audit_id"=>$audit_id,
+                "audit_name"=>$adminData["mobile"],
+            ], $where);
+            $this->cache->set($key,null);
+            if(empty($res)){
+                return ["code"=>-1,"msg"=>"系统繁忙,请稍后重试001!"];
+            }
+            return ["code"=>1,"msg"=>"处理完成"];
+        }
+        //审核驳回
+        try{
+            $this->dbNewAgentCash->beginTransaction();
+            $res = $this->dbNewAgentCash->update([
+                "status"=>-1,
+                "audit_time"=>time(),
+                "audit_mono"=>$mono,
+                "audit_id"=>$audit_id,
+                "audit_name"=>$adminData["mobile"],
+            ], $where);
+            if(empty($res)){
+                $this->dbNewAgentCash->rollBack();
+                $this->cache->set($key,null);
+                return ["code"=>-1,"msg"=>"操作失败001!"];
+            }
+            //添加账户余额明细记录
+            $dres = $this->dbNewAgentBalanceDetail->insert([
+                "type"=>1,
+                "title"=>"提现驳回返还余额",
+                "code"=>"cash_no_balance",
+                "money"=>$data["money"],
+                "content"=>"提现驳回返还余额:".$mono,
+                "admin_id"=>$audit_id,
+                "customer_id"=>$agentData["customerId"],
+                "agent_id"=>$agentData["id"],
+                "user_center_id"=>$agentData["userCenterId"],
+                "cash_id"=>$data["id"],
+                "old_balance"=>$agentData["balance"],
+                "now_balance"=>$agentData["balance"]+$data["money"],
+                "time"=>time(),
+                "expand"=>"",
+                "detail_id"=>0,
+            ]);
+            if(empty($dres)){
+                $this->dbNewAgentCash->rollBack();
+                $this->cache->set($key,null);
+                return ["code"=>-1,"msg"=>"操作失败002!"];
+            }
+            //更新账户数据
+            $withdrawRes = $this->dbNewAgent->set_dec("withdraw", ["id"=>$agentData["id"]], $data["money"]);
+            if(empty($withdrawRes)){
+                $this->dbNewAgentCash->rollBack();
+                $this->cache->set($key,null);
+                return ["code"=>-1,"msg"=>"操作失败003"];
+            }
+            $balanceRes = $this->dbNewAgent->set_inc("balance", ["id"=>$agentData["id"]], $data["money"]);
+            if(empty($balanceRes)){
+                $this->dbNewAgentCash->rollBack();
+                $this->cache->set($key,null);
+                return ["code"=>-1,"msg"=>"操作失败004"];
+            }
+            //提交事务
+            $this->dbNewAgentCash->commit();
+            $this->cache->set($key,null);
+            return ["code"=>1,"msg"=>"提现申请处理成功!"];
+        } catch (\Exception $e){
+            $this->dbNewAgentCash->rollBack();
+            $this->cache->set($key,null);
+            return ["code"=>-1,"msg"=>"系统繁忙,请稍后重试!"];
+        }
+    }
+    
+    
+    /**
+     * 获取设置信息
+     * @param type $enterpriseId
+     * @return string
+     */
+    public static function getCommissionSettingData($enterpriseId){
+        $dbSetting = new DNewAgentSetting('default');
+        $data = $dbSetting->get(["enterpriseId"=>$enterpriseId]);
+        if(empty($data)){
+            $data=[
+                "id"=>0,
+                "enterpriseId"=>$enterpriseId,
+                "levMoney"=>0,
+                "levPer"=>0,
+                "incomePer"=>0,
+                "is_open"=>0,
+                "bankData"=>"",
+            ];
+        }
+        return $data;
+    }
+    
+    
+    
+    /**
+     * 推荐代理分佣设置
+     * @param type $enterpriseId
+     * @param type $parms
+     * @return type
+     */
+    public static function setCommissionSettingData($enterpriseId,$parms){
+        if(empty($enterpriseId) || empty($parms)){
+            return ["code"=>"-1","msg"=>"参数错误"];
+        }
+        $dbSetting = new DNewAgentSetting('default');
+        $data = $dbSetting->get(["enterpriseId"=>$enterpriseId]);
+        $parms["levMoney"]  = empty($parms["levMoney"]) ? 0 : $parms["levMoney"];
+        $parms["levPer"]    = empty($parms["levPer"])   ? 0 : $parms["levPer"];
+        $parms["incomePer"] = empty($parms["incomePer"])? 0 : $parms["incomePer"];
+        $parms["is_open"]   = empty($parms["is_open"])  ? 0 : 1;
+//        if(!is_numeric($parms["levMoney"]) || $parms["levMoney"]<=0){
+//            return ["code"=>"-1","msg"=>"升级门店代理金额必须大于0"];
+//        }
+//        if(!is_numeric($parms["levPer"]) || $parms["levPer"]<0 || $parms["levPer"]>=1){
+//            return ["code"=>"-1","msg"=>"子级升级门店代理消费佣金比例必须0到1之间"];
+//        }
+        if(!is_numeric($parms["incomePer"]) || $parms["incomePer"]<0 || $parms["incomePer"]>=1){
+            return ["code"=>"-1","msg"=>"门店采购佣金比例必须0到1之间"];
+        }
+        $saveData = [
+            "enterpriseId" => $enterpriseId,
+            "levMoney"     => $parms["levMoney"],
+            "levPer"       => $parms["levPer"],
+            "incomePer"    => $parms["incomePer"],
+            "is_open"      => $parms["is_open"],
+            "updateTime"   => time()
+        ];
+        $res = false;
+        if(empty($data)){
+            $saveData["enterpriseId"] = $enterpriseId;
+            $saveData["time"] = time();
+            $res = $dbSetting->insert($saveData);
+        }else{
+            $res = $dbSetting->update($saveData, ["id"=>$data["id"]]);
+        }
+        if(empty($res)){
+            return ["code"=>"-1","msg"=>"系统繁忙,请稍后重试"];
+        }
+        return ["code"=>1,"msg"=>"配置成功"];
+    }
+    
+    
+    
+    
+    
+    public function getCustomerDataByUserCenterId($userCenterId){
+        if(empty($userCenterId)){
+            return false;
+        }
+        $data = $this->dbCustomer->get(["userCenterId"=>$userCenterId]);
+        if(empty($data)){
+            return false;
+        }
+        return $data;
+    }
+    
+    
+}
+