123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323 |
- <?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\CommissionPartner\DNewCommissionBalanceDetail;
- use JinDouYun\Dao\CommissionPartner\DNewCommissionCash;
- use JinDouYun\Dao\CommissionPartner\DNewCommissionDetail;
- use JinDouYun\Dao\CommissionPartner\DNewCommissionPartner;
- use JinDouYun\Dao\CommissionPartner\DNewCommissionTask;
- use JinDouYun\Dao\CommissionPartner\DNewCommissionSetting;
- class PartnerTools{
- private $dbCustomer;
- private $dbUserCenter;
- private $dbOrderIndex;
- private $dbNewCommissionPartner;
- private $dbNewCommissionDetail;
- private $dbNewCommissionCash;
- private $dbNewCommissionBalanceDetail;
- private $dbNewCommissionTask;
- private $dbNewCommissionSetting;
-
- private $enterpriseId;
- private $cache;
- private $cutTable = 1;//客户按照企业id分表
- private $commissionSettingData = null;
- private $levMoney = 20000;//升级条件
- private $levPer = 0.05;//升级佣金比例
- private $incomePer = 0.1;//佣金比例
- private $errorMsg = "";
- private $childrenData = [];
- private $codeAr = [
- "order_calc"=>"子级用户消费分佣",
- "partner_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->dbNewCommissionSetting = new DNewCommissionSetting('default');
- $this->commissionSettingData = $this->dbNewCommissionSetting->get(["enterpriseId"=>$enterpriseId]);
-
- //订单索引表
- $this->dbOrderIndex = new DOrderIndex();
- $this->dbOrderIndex->setTable('qianniao_order_index_' . $this->enterpriseId);
-
- //新分销合伙人表
- $this->dbNewCommissionPartner = new DNewCommissionPartner('default');
- $newCommissionPartnerTableName = $this->dbNewCommissionPartner->getTableName($this->dbNewCommissionPartner->get_Table(), $this->enterpriseId, $this->cutTable);
- $this->dbNewCommissionPartner->setTable($newCommissionPartnerTableName);
-
- //新分销佣金明细表
- $this->dbNewCommissionDetail = new DNewCommissionDetail('default');
- $newCommissionDetailTableName = $this->dbNewCommissionDetail->getTableName($this->dbNewCommissionDetail->get_Table(), $this->enterpriseId, $this->cutTable);
- $this->dbNewCommissionDetail->setTable($newCommissionDetailTableName);
-
- //新分销合伙人提现表
- $this->dbNewCommissionCash = new DNewCommissionCash('default');
- $newCommissionCashTableName = $this->dbNewCommissionCash->getTableName($this->dbNewCommissionCash->get_Table(), $this->enterpriseId, $this->cutTable);
- $this->dbNewCommissionCash->setTable($newCommissionCashTableName);
-
- //新分销合伙人余额明细表
- $this->dbNewCommissionBalanceDetail = new DNewCommissionBalanceDetail('default');
- $newCommissionBalanceDetailTableName = $this->dbNewCommissionBalanceDetail->getTableName($this->dbNewCommissionBalanceDetail->get_Table(), $this->enterpriseId, $this->cutTable);
- $this->dbNewCommissionBalanceDetail->setTable($newCommissionBalanceDetailTableName);
-
-
- //新分销合伙人分佣任务表
- $this->dbNewCommissionTask = new DNewCommissionTask('default');
- $newCommissionTaskTableName = $this->dbNewCommissionTask->getTableName($this->dbNewCommissionTask->get_Table(), $this->enterpriseId, $this->cutTable);
- $this->dbNewCommissionTask->setTable($newCommissionTaskTableName);
- }
- public function cacheTest(){
- $key = "partnertaskcache".'::'.$this->enterpriseId;
- $result = $this->cache->get($key);
- if(!empty($result)){
- return "重复操作";
- }
- $this->cache->set($key,1,10);
- return "开始执行操作";
- }
-
- /**
- * 添加任务
- * @param type $orderId
- * @param type $enterpriseId
- * @return bool
- */
- public static function addTask($orderId,$enterpriseId,$ckeckData){
- try{
- if(empty($orderId) || empty($enterpriseId) || empty($ckeckData)){
- return ["code"=>-1,"msg"=>"参数为空"];
- }
- $ckeckData["type"] = empty($ckeckData["type"])?"order":$ckeckData["type"];
- $dbSetting = new DNewCommissionSetting('default');
- $settingData = $dbSetting->get(["enterpriseId"=>$enterpriseId]);
- if(empty($settingData) || empty($settingData["is_open"]) || $settingData["levMoney"]<=0){
- return ["code"=>-1,"msg"=>"还未进行分佣配置"];
- }
- if(empty($ckeckData["parentPath"]) && $ckeckData["payAmount"]<$settingData["levMoney"]){
- return ["code"=>-1,"msg"=>"用户不存在父级,且消费金额不够成为合伙人"];
- }
-
- $dbTask = new DNewCommissionTask('default');
- $taskTableName = $dbTask->getTableName($dbTask->get_Table(), $enterpriseId, 1);
- $dbTask->setTable($taskTableName);
- $count = $dbTask->count(["orderId"=>$orderId,"enterpriseId"=>$enterpriseId,"type"=>$ckeckData["type"]]);
- if($count>0){
- return ["code"=>-1,"msg"=>"任务订单已存在"];
- }
- $r = $dbTask->insert([
- "orderId"=>$orderId,
- "enterpriseId"=>$enterpriseId,
- "title"=>"订单完成计算分佣",
- "mono"=>"",
- "status"=>$ckeckData["type"] == "shopCardOrder"?-3:0,//服务优惠卡暂时不计算
- "run_time"=>0,
- "time"=>time(),
- "type"=>$ckeckData["type"],
- ]);
- return ["code"=>1,"msg"=>"任务已插入[{$r}]"];
- } catch (\Exception $e){
- return ["code"=>-1,"msg"=>"系统错误"];
- }
- }
-
- public function runTask(){
- if(empty($this->commissionSettingData) || empty($this->commissionSettingData["is_open"]) || $this->commissionSettingData["levMoney"]<=0){
- return ["code"=>-1,"msg"=>"未开启配置"];
- }
- $key = "partnertaskcache".'::'.$this->enterpriseId;
- $result = $this->cache->get($key);
- if(!empty($result)){
- return ["code"=>-1,"msg"=>"操作频繁"];
- }
- $this->cache->set($key,1,10);
- $dbTask = new DNewCommissionTask('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["orderId"],$v["type"]);
- $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"=>"执行结束"];
-
- }
-
-
- public function getCustomerDataByUserCenterId($userCenterId){
- if(empty($userCenterId)){
- return false;
- }
- $data = $this->dbCustomer->get(["userCenterId"=>$userCenterId]);
- if(empty($data)){
- return false;
- }
- return $data;
-
- }
-
- public function setPushCustomerByUserCenterId($parentId=0,$childId=0){
- //子级用户信息
- $childData = $this->dbCustomer->get(["userCenterId"=>$childId]);
- if (empty($childData) || $childData["parentId"]>0 || $childData["isPartner"]==1 || $childData["isParentHead"]==1) {
- return ["code"=>-1,"msg"=>"用户不能被绑定","data"=>$childData];
- }
- //父级用户信息
- $parentData = $this->dbCustomer->get(["userCenterId"=>$parentId]);
- if(empty($parentData)){
- return ["code"=>-1,"msg"=>"父级用户不存在","data"=>null];
- }
- return $this->setPushCustomer($parentData["id"],$childData["id"]);
- }
-
- /**
- * 设置推广用户
- * @param type $parentId
- * @param type $childId
- * @return bool
- */
- public function setPushCustomer($parentId=0,$childId=0){
- if($parentId == 0 || $childId == 0 || $parentId == $childId){
- return ["code"=>-1,"msg"=>"参数错误","data"=>null];
- }
- //子级用户信息
- $childData = $this->dbCustomer->get($childId);
- if (empty($childData) || $childData["parentId"]>0 || $childData["isPartner"]==1 || $childData["isParentHead"]==1) {
- return ["code"=>-1,"msg"=>"用户不能被绑定","data"=>$childData];
- }
- //父级用户信息
- $parentData = $this->dbCustomer->get($parentId);
- if(empty($parentData)){
- return ["code"=>-1,"msg"=>"父级用户不存在","data"=>null];
- }
- $beginStatus = $this->dbCustomer->beginTransaction();
- //保存父级数据
- if($parentData["isParentHead"] == 0){
- $parentSave=["isParentHead"=>1];
- $pres = $this->dbCustomer->update($parentSave, ["id"=>$parentId]);
- if(empty($pres)){
- $this->dbCustomer->rollBack();
- return ["code"=>-1,"msg"=>"父级数据保存失败01","data"=>$parentSave];
- }
- }
- //保存子级数据
- $parentPath = empty($parentData["parentPath"]) ? $parentId : $parentData["parentPath"] . "," . $parentId;
- $childSave=["parentId"=>$parentId,"parentPath"=>$parentPath];
- $res = $this->dbCustomer->update($childSave, ["id"=>$childId]);
- if(empty($res)){
- $this->dbCustomer->rollBack();
- return ["code"=>-1,"msg"=>"绑定失败","data"=>$childSave];
- }else{
- $beginStatus && $this->dbCustomer->commit();
- // $this->delCustomerCache();
- return ["code"=>1,"msg"=>"绑定成功","data"=>null];
- }
- }
- /**
- * 设置用户为合伙人
- * @param type $customerId
- */
- public function setPartner($customerId){
- $userData = $this->dbCustomer->get($customerId);
- if (empty($userData)) {
- return false;
- }
- if($userData["isPartner"]==1){
- return true;
- }
- $save=["isPartner"=>1];
- $res = $this->dbCustomer->update($save,["id"=>$customerId]);
- if(empty($res)){
- return false;
- }else{
- $this->getPartnerData($userData["id"], $userData["userCenterId"]);
- return true;
- }
- }
-
-
- /**
- * 【新】新升级合伙人
- */
- public function apiSetPartner($parms){
- if(empty($parms)){
- return false;
- }
- $where=[];
- if(!empty($parms["customerId"])){
- $where["id"] = $parms["customerId"];
- }else if(!empty($parms["userCenterId"])){
- $where["userCenterId"] = $parms["userCenterId"];
- }else{
- return false;
- }
- $userData = $this->dbCustomer->get($where);
- if(empty($userData) || empty($userData["id"])){
- return false;
- }
- if($userData["isPartner"]==1){
- return true;
- }
-
- $this->dbNewCommissionPartner->beginTransaction();
-
- $save=["isPartner"=>1];
- $res = $this->dbCustomer->update($save,["id"=>$userData["id"]]);
- if(empty($res)){
- $this->dbNewCommissionPartner->rollBack();
- return false;
- }
- //添加合伙人账号
- $partnerData = $this->dbNewCommissionPartner->get(["customerId"=>$userData["id"]]);
- if(empty($partnerData)){
- $nowTime = time();
- $partnerId = $this->dbNewCommissionPartner->insert([
- "customerId"=>$userData["userCenterId"],
- "userCenterId"=>$userData["id"],
- "balance"=>0,
- "waitMoney"=>0,
- "withdraw"=>0,
- "totalMoney"=>0,
- "deleteStatus"=>5,
- "createTime"=>$nowTime,
- "updateTime"=>$nowTime,
- "expand"=>"",
- ]);
- if(empty($partnerId)){
- $this->dbNewCommissionPartner->rollBack();
- return false;
- }
- $partnerData["id"] = $partnerId;
- }
- //更新子级的父合伙人id
- // $partnerId = intval($partnerData["id"]);
- // $customerTableName = 'qianniao_customer_'.$this->enterpriseId;
- // $sql = "select * from {$customerTableName} where FIND_IN_SET(". intval($userData["id"]).",parentPath)";
-
- return true;
- }
-
-
- /**
- * 【新】对外查询接口
- * @param type $parentId
- * @return type
- */
- public function getApiChildrenTree($parentId){
- $customerTableName = 'qianniao_customer_'.$this->enterpriseId;
- $sql = "select count(*) as count from {$customerTableName} where FIND_IN_SET(". intval($parentId).",`parentPath`)";
- $count = $this->dbCustomer->query($sql);
- if(empty($count) || empty($count[0]) || empty($count[0]["count"])){
- return [];
- }
- $type = 1;
- if(intval($count[0]["count"])<=100){
- $type = 2;
- }
- $res = $this->getChildrenTree($parentId,0,$type);
- return empty($res) ? [] : $res;
-
- }
- /**
- * 【新】获取子级
- * @param type $parentId
- * @param type $lev
- * @return type
- */
- public function getChildrenTree($parentId,$lev=0,$type=1){
- $data = [];
- if($type==2){
- $data = $this->getChildrenData($parentId);//第二种方法:适和子级数量少
- }else{
- $data = $this->dbCustomer->select(["parentId"=>$parentId],"id,name,parentPath,parentId,isPartner");//第一种方法:适合子级一次性数据比较多
- }
- if(empty($data)){
- $data=[];
- }
- $childData = [];
- foreach($data as $k=>$v){
- $data[$k]["lev"] = $lev;
- if($v["isPartner"]==0){
- $itemChildData = $this->getChildrenTree($v["id"],$lev+1,$type);
- $childData = array_merge($childData,$itemChildData);
- }
- }
- $allData = array_merge($data,$childData);
- return $allData;
- }
- /**
- * 【新】获取子级数据表数据
- * @param type $parentId
- * @return type
- */
- public function getChildrenData($parentId){
- if(empty($this->childrenData)){
- $customerTableName = 'qianniao_customer_'.$this->enterpriseId;
- $sql = "select id,name,parentPath,parentId,isPartner from {$customerTableName} where FIND_IN_SET(". intval($parentId).",parentPath)";
- $res = $this->dbCustomer->query($sql);
- $this->childrenData = empty($res) ? [] : $res;
- }
- $data=[];
- foreach($this->childrenData as $k=>$v){
- if($v["parentId"] == $parentId){
- $data[]=$v;
- }
- }
- return $data;
- }
-
- /**
- * 获取用户父级合伙人
- * @param type $customerId
- * @return bool
- */
- public function getParentPartner($userData){
- if (empty($userData) || empty($userData["parentPath"])) {
- return false;
- }
- //逆序祖先,从最近的祖先先查
- $pathData = array_reverse(explode(",", trim($userData["parentPath"])));
- $where=[];
- $where[]=["isPartner","=",1];
- $where[]=["id","in",$pathData];
- $parentData = $this->dbCustomer->select($where);
- if(empty($parentData)){
- return false;
- }
- $parentPartner = null;
- for($i=0;$i<count($pathData);$i++){
- $itemData = [];
- foreach($parentData as $k=>$v){
- if($pathData[$i]==$v["id"]){
- $itemData = $v;
- break;
- }
- }
- if(!empty($itemData) && $itemData["isPartner"]==1){
- $parentPartner = $itemData;
- break;
- }
- }
- if(empty($parentPartner)){
- return false;
- }
- return $parentPartner;
- }
-
- /**
- * 佣金计算
- * @param type $customerId
- * @param type $money
- * @param type $type 1表示子级消费,2表示合伙人收益
- * @return bool
- */
- public function calcMoney($customerData,$money){
- if(empty($this->commissionSettingData) || empty($this->commissionSettingData["is_open"]) || $this->commissionSettingData["levMoney"]<=0){
- return false;
- }
-
- $parentData = $this->getParentPartner($customerData);
- if(empty($parentData) || $parentData["isPartner"]==0){
- //上级合伙人不存在
- if($money>=$this->commissionSettingData["levMoney"] && $customerData["isPartner"]==0){
- $this->setPartner($customerData["id"]);
- }
- return false;
- }
- $per = $this->commissionSettingData["incomePer"];
- $isUpgrade = 0;
- if($money>=$this->commissionSettingData["levMoney"] && $customerData["isPartner"]==0){
- $per = $this->commissionSettingData["levPer"];
- $isUpgrade = 1;
- }
- $resData=[
- "childId"=>$customerData["id"],
- "childData"=>$customerData,
- "parentId"=>$parentData["id"],
- "parentData"=>$parentData,
- "per"=>$per,
- "money"=>$money,
- "commission"=>bcmul($money,$per,2),
- "isUpgrade"=>$isUpgrade
- ];
- return $resData;
- }
-
- /**
- * 订单消费计算佣金
- * @param type $orderCustomerId
- * @param type $orderMoney
- * @param type $isPart 订单用户如果是合伙人是否计算
- * @return bool
- */
- public function runCalcMoneyData($orderId,$taskType=""){
- if(empty($taskType) || !in_array($taskType, ["order","shopCardOrder"])){
- return ["code"=>-1,"msg"=>"任务类型不存在"];
- }
- if(empty($this->commissionSettingData) || empty($this->commissionSettingData["is_open"]) || $this->commissionSettingData["levMoney"]<=0){
- return ["code"=>-1,"msg"=>"配置信息错误"];
- }
-
- //获取订单信息
- if(empty($orderId)){
- return ["code"=>-1,"msg"=>"订单id错误"];
- }
- $orderIndexData = $this->dbOrderIndex->get(['id'=>$orderId]);
- if(empty($orderIndexData)){
- return ["code"=>-1,"msg"=>"订单索引数据不存在"];
- }
- $orderData=null;
- //商品订单
- if($taskType=="order"){
- // 切换订单分表,查询订单主单据数据
- $dbOrder = new DOrder('default');
- $fix = ceil($orderIndexData['userCenterId'] / 200000);
- $dbOrder->setTable('qianniao_order_' . $this->enterpriseId . '_' . $fix);
- $orderData = $dbOrder->get(['id' => $orderId]);
- }
- //服务优惠卡订单
- if($taskType=="shopCardOrder"){
- $dbOrder = new \JinDouYun\Dao\Shop\DShopCardOrder('default');
- $orderData = $dbOrder->get(['id'=>$orderId,"enterprise_id"=>$this->enterpriseId]);
-
- if(!empty($orderData)){
- $orderData["customerId"] = $orderData["customer_id"];
- $orderData["payAmount"] = $orderData["pay_price"];
- $orderData["payType"] = $orderData["pay_type"]==2 ? 1 : 0;//微信支付
- $orderData["source"] = 3;
- $orderData["orderStatus"] = 5;
- $orderData["no"] = $orderData["order_id"];
- }
- }
-
- if(empty($orderData)){
- return ["code"=>-1,"msg"=>"订单数据不存在"];
- }
- //开始计算
- $isPart=false;//订单用户如果是合伙人是否计算
- $orderCustomerId = $orderData["customerId"];
- $orderMoney = $orderData["payAmount"];
- //只计算小程序已完成订单
- if(empty($orderCustomerId) || empty($orderMoney) || $orderData["payType"]!=1 || $orderData["source"]!=3 || $orderData["orderStatus"]!=5){
- return ["code"=>-1,"msg"=>"只计算小程序微信支付已完成订单"];
- }
- $customerData = $this->dbCustomer->get($orderCustomerId);
- if (empty($customerData)) {
- return ["code"=>-1,"msg"=>"用户不存在"];
- }
- if(empty($customerData["parentPath"])){
- if($orderMoney>=$this->commissionSettingData["levMoney"] && $customerData["isPartner"]==0){
- $this->setPartner($orderCustomerId);
- }
- return ["code"=>-1,"msg"=>"用户不存在父级"];
- }
- //订单用户如果是合伙人消费则不计算收益
- if(!$isPart && $customerData["isPartner"]==1){
- return ["code"=>-1,"msg"=>"合伙人消费不计算上级收益"];
- }
- $data=[];
- $nowTime = time();
- //计算低层收益
- $bottomData = $this->calcMoney($customerData,$orderMoney);
- if(empty($bottomData)){
- return ["code"=>-1,"msg"=>"上级合伙人不存在"];
- }
- //合伙人账户
- $bottomPartnerData = $this->getPartnerData($bottomData["parentData"]["id"], $bottomData["parentData"]["userCenterId"]);
- if(empty($bottomPartnerData)){
- return ["code"=>-1,"msg"=>"上级合伙人账户错误"];
- }
-
- //记录底层收益
- $data[]=[
- "orderMoney"=>$orderMoney,//订单支付金额
- "calcMoney"=>$bottomData["money"],//佣金计算金额
- "sourceCustomerId"=>$bottomData["childId"],//来源客户id
- "partnerId"=>$bottomPartnerData["id"],//收钱合伙人账号id
- "customerId"=>$bottomPartnerData["customerId"],//收钱合伙人
- "userCenterId"=>$bottomPartnerData["userCenterId"],//收钱合伙人
- "commission"=>$bottomData["commission"],//佣金金额
- "per"=>$bottomData["per"],//佣金比例
- "type"=>0,
- "orderId"=>$orderData["id"],
- "orderNo"=>$orderData["no"],
- "status"=>0,//暂时不需要
- "title"=>"子级用户消费分佣",
- "isUpgrade"=>$bottomData["isUpgrade"],
- "mono"=>"",
- "time"=>$nowTime,
- "taskType"=>$taskType,
- ];
-
-
- //计算顶层收益
- $topData = $this->calcMoney($bottomData["parentData"],$bottomData["commission"]);
- if(!empty($topData)){
- $topPartnerData = $this->getPartnerData($topData["parentData"]["id"], $topData["parentData"]["userCenterId"]);
- if(empty($topPartnerData)){
- return ["code"=>-1,"msg"=>"顶级合伙人账户错误"];
- }
- //记录顶层收益
- $data[]=[
- "orderMoney"=>$orderMoney,//订单支付金额
- "calcMoney"=>$topData["money"],//佣金计算金额
- "sourceCustomerId"=>$topData["childId"],//来源客户id
- "partnerId"=>$topPartnerData["id"],//收钱合伙人账号id
- "customerId"=>$topPartnerData["customerId"],//收钱合伙人
- "userCenterId"=>$topPartnerData["userCenterId"],//收钱合伙人
- "commission"=>$topData["commission"],//佣金金额
- "per"=>$topData["per"],//佣金比例
- "type"=>1,//1表示子级合伙人收益计算收益,0表示子级消费计算收益
- "orderId"=>$orderData["id"],
- "orderNo"=>$orderData["no"],
- "status"=>0,
- "title"=>"子级合伙人收益分佣",
- "isUpgrade"=>$topData["isUpgrade"],
- "mono"=>"",
- "time"=>$nowTime,
- "taskType"=>$taskType,
- ];
- }
-
- $num=0;
- //开启事务
- $this->dbNewCommissionDetail->beginTransaction();
- $lms = "";
- foreach($data as $k=>$v){
- $count = $this->dbNewCommissionDetail->count(["orderId"=>$v["orderId"],"partnerId"=>$v["partnerId"]]);
- if($count>0){
- $lms.="当前订单已计算佣金[{$v['orderId']}];";
- continue;
- }
- $nid = $this->dbNewCommissionDetail->insert($v);
- if(empty($nid)){
- $lms.="佣金明细插入失败[{$v['orderId']}];";
- continue;
- }
- //查询合伙人账户
- $partnerItem = $this->dbNewCommissionPartner->get(["id"=>$v["partnerId"]]);
- if(empty($partnerItem)){
- $num = 0;
- $lms.="合伙人账户不存在[{$v['orderId']}];";
- break;
- }
- $update = [
- 'balance' => bcadd($partnerItem['balance'], $v["commission"], 2),
- 'totalMoney' => bcadd($partnerItem['totalMoney'], $v["commission"], 2),
- 'updateTime' => $nowTime,
- ];
- //添加账户余额明细记录
- $dres = $this->dbNewCommissionBalanceDetail->insert([
- "type"=>1,
- "title"=>$v["title"],
- "code"=>$v["type"]==1 ? "partner_calc" : "order_calc",
- "money"=>$v["commission"],
- "content"=>$v["title"],
- "admin_id"=>0,
- "customer_id"=>$v["customerId"],
- "partner_id"=>$v["partnerId"],
- "user_center_id"=>$v["userCenterId"],
- "cash_id"=>0,
- "old_balance"=>$partnerItem["balance"],
- "now_balance"=>$update["balance"],
- "time"=>$nowTime,
- "expand"=>"",
- "detail_id"=>$nid,
- ]);
- if(empty($dres)){
- $num = 0;
- $lms.="账户余额明细插入失败[{$v['orderId']}];";
- break;
- }
- //更新账户余额
- $upRes = $this->dbNewCommissionPartner->update($update, ['id' => $v["partnerId"]]);
- if(empty($upRes)){
- $num = 0;
- $lms.="更新账户余额失败[{$v['orderId']}];";
- break;
- }
- $num++;
- }
- if($num<=0){
- $this->dbNewCommissionDetail->rollBack();
- return ["code"=>-1,"msg"=>"数据更新失败:".$lms];
- }
- $this->dbNewCommissionDetail->commit();
- //佣金更新完后验证设置合伙人
- if($bottomData["isUpgrade"]==1){
- $this->setPartner($bottomData["childId"]);
- }
- return ["code"=>1,"msg"=>"执行成功".$lms];
- }
-
- public function getPartnerData($customerId,$userCenterId){
- if(empty($customerId)){
- return false;
- }
- $partnerData = $this->dbNewCommissionPartner->get(["customerId"=>$customerId]);
- if(empty($partnerData)){
- $nowTime = time();
- $partnerData=[
- "userCenterId"=>$userCenterId,
- "customerId"=>$customerId,
- "balance"=>0,
- "waitMoney"=>0,
- "withdraw"=>0,
- "totalMoney"=>0,
- "deleteStatus"=>5,
- "createTime"=>$nowTime,
- "updateTime"=>$nowTime,
- "expand"=>"",
- ];
- $partnerId = $this->dbNewCommissionPartner->insert($partnerData);
- if(empty($partnerId)){
- return false;
- }
- $partnerData["id"] = $partnerId;
- $partnerData["orderNum"] = 0;
- }else{
- $partnerData["orderNum"] = $this->getCommissionDetailCount($partnerData["id"]);
- }
- return $partnerData;
- }
- public function getCommissionDetailCount($partnerId,$type=0){
- if(empty($partnerId) && $type=0){
- return 0;
- }
- $where=[];
- if(!empty($partnerId)){
- $where["partnerId"] = $partnerId;
- }
- $count = $this->dbNewCommissionDetail->count($where);
- return $count?$count:0;
- }
-
- /**
- * 获取分佣明细
- */
- public function getCommissionDetail($customerId,$parms){
- $where=[];
- if(!empty($parms["where"])){
- $where= $parms["where"];
- }
- if(!empty($customerId)){
- $where["customerId"] = $customerId;
- }
-
- $data = $this->dbNewCommissionDetail->select($where, "*", "id desc", $parms["limit"], $parms["offset"]);
- $total = $this->dbNewCommissionDetail->count($where);
- if(empty($data)){
- $data = [];
- }
- foreach($data as $k=>$v){
- $itemUser = $this->dbCustomer->get(["id"=>$v["sourceCustomerId"]],"id,name,avatar");
- if(empty($itemUser)){
- $data[$k]["name"] = "";
- $data[$k]["avatar"] = "";
- }else{
- $data[$k]["name"] = $itemUser["name"];
- $data[$k]["avatar"] = $itemUser["avatar"];
- }
- }
- $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->dbNewCommissionBalanceDetail->select($where, "*", "id desc", $parms["limit"], $parms["offset"]);
- $total = $this->dbNewCommissionBalanceDetail->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 = "partnerCashCache_".$data["customerId"].'::'.$this->enterpriseId;
- $result = $this->cache->get($key);
- if(!empty($result)){
- return ["code"=>-1,"msg"=>"操作频繁,请稍后尝试!"];
- }
-
- $count = $this->dbNewCommissionCash->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"=>"提现金额错误"];
- }
- $partnerData = $this->dbNewCommissionPartner->get(["customerId"=>$data["customerId"]]);
- if(empty($partnerData) || empty($partnerData["id"])){
- return ["code"=>-1,"msg"=>"合伙人不存在"];
- }
- if($data["money"]>$partnerData["balance"] || $partnerData["balance"]<=0){
- return ["code"=>-1,"msg"=>"提现金额不足"];
- }
- $nowTime = time();
- $save=[
- "customer_id" => $partnerData["customerId"],
- "user_center_id" => $partnerData["userCenterId"],
- "partner_id" => $partnerData["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->dbNewCommissionPartner->beginTransaction();
- //插入提现申请
- $applyId = $this->dbNewCommissionCash->insert($save);
- if(empty($applyId)){
- return ["code"=>-1,"msg"=>"数据提交失败001"];
- }
- //更新提现账户余额并添加明细
- $update = [
- 'balance' => bcsub($partnerData["balance"], $save["money"], 2),//账户余额
- 'withdraw' => bcadd($partnerData["withdraw"], $save["money"],2),//已提现金额
- 'updateTime' => $nowTime,
- ];
- if($update["balance"]<0){
- $this->dbNewCommissionPartner->rollBack();
- return ["code"=>-1,"msg"=>"数据提交失败002"];
- }
- //添加账户余额明细记录
- $dres = $this->dbNewCommissionBalanceDetail->insert([
- "type"=>-1,
- "title"=>"余额提现",
- "code"=>"cash_balance",
- "money"=>$save["money"],
- "content"=>"合伙人余额提现",
- "admin_id"=>0,
- "customer_id"=>$partnerData["customerId"],
- "partner_id"=>$partnerData["id"],
- "user_center_id"=>$partnerData["userCenterId"],
- "cash_id"=>$applyId,
- "old_balance"=>$partnerData["balance"],
- "now_balance"=>$update["balance"],
- "time"=>$nowTime,
- "expand"=>"",
- "detail_id"=>0,
- ]);
- if(empty($dres)){
- $this->dbNewCommissionPartner->rollBack();
- return ["code"=>-1,"msg"=>"账户余额明细插入失败"];
- }
- //更新账户余额
- $upRes = $this->dbNewCommissionPartner->update($update, ['id' => $partnerData["id"]]);
- if(empty($upRes)){
- $this->dbNewCommissionPartner->rollBack();
- return ["code"=>-1,"msg"=>"更新账户余额失败"];
- }
- $this->dbNewCommissionPartner->commit();
- $this->cache->set($key,null);
- return ["code"=>1,"msg"=>"提现申请提交成功"];
- } catch (\Exception $e){
- $this->dbNewCommissionPartner->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->dbNewCommissionCash->select($where, "*", "id desc", $parms["limit"], $parms["offset"]);
- $total = $this->dbNewCommissionCash->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->dbNewCommissionCash->get($where);
- return $data;
- }
-
- /**
- * 获取合伙人列表
- * @param type $params
- */
- public function getPartnerList($params = []){
- $customerTableName = 'qianniao_customer_'.$this->enterpriseId;
- $userCenterTableName = 'qianniao_user_center';
- $newCommissionPartnerTableName = 'qianniao_new_commission_partner_'.$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->dbNewCommissionPartner->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->dbNewCommissionPartner->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';
- // $newCommissionPartnerTableName = 'qianniao_new_commission_partner_'.$this->enterpriseId;
- $newCommissionBalanceDetailTableName = 'qianniao_new_commission_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['partnerId'])){
- $whereSql .= ' and p.partner_id = '.$params['partnerId'];
- }
-
- 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->dbNewCommissionPartner->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->dbNewCommissionPartner->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';
- // $newCommissionPartnerTableName = 'qianniao_new_commission_partner_'.$this->enterpriseId;
- // $newCommissionBalanceDetailTableName = 'qianniao_new_commission_balance_detail_'.$this->enterpriseId;
- $newCommissionDetailTableName = 'qianniao_new_commission_detail_'.$this->enterpriseId;
- //查询数据
- $field = "p.*,u.mobile,c.name,c.avatar,c2.name as sourceName,c2.userCenterId as sourceUserCenterId ";
- $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 {$customerTableName} c2 on c2.id = p.sourceCustomerId";
-
- $whereSql = ' where 1=1 ';
- if(!empty($params['partnerId'])){
- $whereSql .= ' and p.partnerId = '.$params['partnerId'];
- }
- 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->dbNewCommissionPartner->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 {$customerTableName} c2 on c2.id = p.sourceCustomerId";
- $countData = $this->dbNewCommissionPartner->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';
- // $newCommissionPartnerTableName = 'qianniao_new_commission_partner_'.$this->enterpriseId;
- // $newCommissionBalanceDetailTableName = 'qianniao_new_commission_balance_detail_'.$this->enterpriseId;
- // $newCommissionDetailTableName = 'qianniao_new_commission_detail_'.$this->enterpriseId;
- $newCommissionCashTableName = 'qianniao_new_commission_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['partnerId'])){
- $whereSql .= ' and p.partner_id = '.$params['partnerId'];
- }
- 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->dbNewCommissionPartner->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->dbNewCommissionPartner->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 = "partnerCashDealCache_".$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->dbNewCommissionCash->get($where);
- if(empty($data)){
- $this->cache->set($key,null);
- return ["code"=>-1,"msg"=>"数据不存在"];
- }
- //合伙人账户信息
- $partnerData = $this->dbNewCommissionPartner->get(["id"=>$data["partner_id"]]);
- if(empty($partnerData)){
- $this->cache->set($key,null);
- return ["code"=>-1,"msg"=>"合伙人账户不存在"];
- }
- //打款成功
- if($type==1){
- $res = $this->dbNewCommissionCash->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->dbNewCommissionCash->beginTransaction();
- $res = $this->dbNewCommissionCash->update([
- "status"=>-1,
- "audit_time"=>time(),
- "audit_mono"=>$mono,
- "audit_id"=>$audit_id,
- "audit_name"=>$adminData["mobile"],
- ], $where);
- if(empty($res)){
- $this->dbNewCommissionCash->rollBack();
- $this->cache->set($key,null);
- return ["code"=>-1,"msg"=>"操作失败001!"];
- }
- //添加账户余额明细记录
- $dres = $this->dbNewCommissionBalanceDetail->insert([
- "type"=>1,
- "title"=>"提现驳回返还余额",
- "code"=>"cash_no_balance",
- "money"=>$data["money"],
- "content"=>"提现驳回返还余额:".$mono,
- "admin_id"=>$audit_id,
- "customer_id"=>$partnerData["customerId"],
- "partner_id"=>$partnerData["id"],
- "user_center_id"=>$partnerData["userCenterId"],
- "cash_id"=>$data["id"],
- "old_balance"=>$partnerData["balance"],
- "now_balance"=>$partnerData["balance"]+$data["money"],
- "time"=>time(),
- "expand"=>"",
- "detail_id"=>0,
- ]);
- if(empty($dres)){
- $this->dbNewCommissionCash->rollBack();
- $this->cache->set($key,null);
- return ["code"=>-1,"msg"=>"操作失败002!"];
- }
- //更新账户数据
- $withdrawRes = $this->dbNewCommissionPartner->set_dec("withdraw", ["id"=>$partnerData["id"]], $data["money"]);
- if(empty($withdrawRes)){
- $this->dbNewCommissionCash->rollBack();
- $this->cache->set($key,null);
- return ["code"=>-1,"msg"=>"操作失败003"];
- }
- $balanceRes = $this->dbNewCommissionPartner->set_inc("balance", ["id"=>$partnerData["id"]], $data["money"]);
- if(empty($balanceRes)){
- $this->dbNewCommissionCash->rollBack();
- $this->cache->set($key,null);
- return ["code"=>-1,"msg"=>"操作失败004"];
- }
- //提交事务
- $this->dbNewCommissionCash->commit();
- $this->cache->set($key,null);
- return ["code"=>1,"msg"=>"提现申请处理成功!"];
- } catch (\Exception $e){
- $this->dbNewCommissionCash->rollBack();
- $this->cache->set($key,null);
- return ["code"=>-1,"msg"=>"系统繁忙,请稍后重试!"];
- }
- }
-
-
-
- public static function getCommissionSettingData($enterpriseId){
- $dbSetting = new DNewCommissionSetting('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;
- }
- public static function setCommissionSettingData($enterpriseId,$parms){
- if(empty($enterpriseId) || empty($parms)){
- return ["code"=>"-1","msg"=>"参数错误"];
- }
- $dbSetting = new DNewCommissionSetting('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"=>"配置成功"];
- }
-
-
-
- /**
- * 删除用户缓存
- * @param type $customerId
- * @param type $userCenterId
- */
- public function delCustomerCache($customerId,$userCenterId){
- $objCustomerCache = new CustomerCache();
- $objCustomerCache->delCustomerData($this->enterpriseId, $customerId);
- $objCustomerCache->delCustomerUserData($this->enterpriseId, $userCenterId);
- }
- }
|