PartnerTools.Class.php 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  1. <?php
  2. namespace Util\Common;
  3. use Mall\Framework\Factory;
  4. use JinDouYun\Dao\Customer\DCustomer;
  5. use JinDouYun\Cache\CustomerCache;
  6. use JinDouYun\Dao\Order\DOrderIndex;
  7. use JinDouYun\Dao\Order\DOrder;
  8. use JinDouYun\Dao\CommissionPartner\DNewCommissionBalanceDetail;
  9. use JinDouYun\Dao\CommissionPartner\DNewCommissionCash;
  10. use JinDouYun\Dao\CommissionPartner\DNewCommissionDetail;
  11. use JinDouYun\Dao\CommissionPartner\DNewCommissionPartner;
  12. use JinDouYun\Dao\CommissionPartner\DNewCommissionTask;
  13. class PartnerTools{
  14. private $dbCustomer;
  15. private $dbOrderIndex;
  16. private $dbNewCommissionPartner;
  17. private $dbNewCommissionDetail;
  18. private $dbNewCommissionCash;
  19. private $dbNewCommissionBalanceDetail;
  20. private $dbNewCommissionTask;
  21. private $enterpriseId;
  22. private $cache;
  23. private $cutTable = 1;//客户按照企业id分表
  24. private $levMoney = 20000;//升级条件
  25. private $levPer = 0.05;//升级佣金比例
  26. private $incomePer = 0.1;//佣金比例
  27. private $errorMsg = "";
  28. private $codeAr = [
  29. "order_calc"=>"子级用户消费分佣",
  30. "partner_calc"=>"子级合伙人收益分佣",
  31. "cash_balance"=>"合伙人余额提现",
  32. "cash_no_balance"=>"提现驳回返还余额",
  33. ];
  34. public function __construct($enterpriseId){
  35. $this->enterpriseId = $enterpriseId;
  36. $this->cache = Factory::cache('systask');
  37. //用户表
  38. $this->dbCustomer = new DCustomer('default');
  39. $customerTableName = $this->dbCustomer->getTableName($this->dbCustomer->get_Table(), $this->enterpriseId, $this->cutTable);
  40. $this->dbCustomer->setTable($customerTableName);
  41. //订单索引表
  42. $this->dbOrderIndex = new DOrderIndex();
  43. $this->dbOrderIndex->setTable('qianniao_order_index_' . $this->enterpriseId);
  44. //新分销合伙人表
  45. $this->dbNewCommissionPartner = new DNewCommissionPartner('default');
  46. $newCommissionPartnerTableName = $this->dbNewCommissionPartner->getTableName($this->dbNewCommissionPartner->get_Table(), $this->enterpriseId, $this->cutTable);
  47. $this->dbNewCommissionPartner->setTable($newCommissionPartnerTableName);
  48. //新分销佣金明细表
  49. $this->dbNewCommissionDetail = new DNewCommissionDetail('default');
  50. $newCommissionDetailTableName = $this->dbNewCommissionDetail->getTableName($this->dbNewCommissionDetail->get_Table(), $this->enterpriseId, $this->cutTable);
  51. $this->dbNewCommissionDetail->setTable($newCommissionDetailTableName);
  52. //新分销合伙人提现表
  53. $this->dbNewCommissionCash = new DNewCommissionCash('default');
  54. $newCommissionCashTableName = $this->dbNewCommissionCash->getTableName($this->dbNewCommissionCash->get_Table(), $this->enterpriseId, $this->cutTable);
  55. $this->dbNewCommissionCash->setTable($newCommissionCashTableName);
  56. //新分销合伙人余额明细表
  57. $this->dbNewCommissionBalanceDetail = new DNewCommissionBalanceDetail('default');
  58. $newCommissionBalanceDetailTableName = $this->dbNewCommissionBalanceDetail->getTableName($this->dbNewCommissionBalanceDetail->get_Table(), $this->enterpriseId, $this->cutTable);
  59. $this->dbNewCommissionBalanceDetail->setTable($newCommissionBalanceDetailTableName);
  60. //新分销合伙人分佣任务表
  61. $this->dbNewCommissionTask = new DNewCommissionTask('default');
  62. $newCommissionTaskTableName = $this->dbNewCommissionTask->getTableName($this->dbNewCommissionTask->get_Table(), $this->enterpriseId, $this->cutTable);
  63. $this->dbNewCommissionTask->setTable($newCommissionTaskTableName);
  64. }
  65. public function cacheTest(){
  66. $key = "partnertaskcache".'::'.$this->enterpriseId;
  67. $result = $this->cache->get($key);
  68. if(!empty($result)){
  69. return "重复操作";
  70. }
  71. $this->cache->set($key,1,10);
  72. return "开始执行操作";
  73. }
  74. /**
  75. * 添加任务
  76. * @param type $orderId
  77. * @param type $enterpriseId
  78. * @return bool
  79. */
  80. public static function addTask($orderId,$enterpriseId){
  81. try{
  82. if(empty($orderId) || empty($enterpriseId)){
  83. return ["code"=>-1,"msg"=>"参数为空"];
  84. }
  85. $dbTask = new DNewCommissionTask('default');
  86. $taskTableName = $dbTask->getTableName($dbTask->get_Table(), $enterpriseId, 1);
  87. $dbTask->setTable($taskTableName);
  88. $count = $dbTask->count(["orderId"=>$orderId]);
  89. if($count>0){
  90. return ["code"=>-1,"msg"=>"任务订单已存在"];
  91. }
  92. $r = $dbTask->insert([
  93. "orderId"=>$orderId,
  94. "enterpriseId"=>$enterpriseId,
  95. "title"=>"订单完成计算分佣",
  96. "mono"=>"",
  97. "status"=>0,
  98. "run_time"=>0,
  99. "time"=>time(),
  100. ]);
  101. return ["code"=>1,"msg"=>"任务已插入[{$r}]"];
  102. } catch (\Exception $e){
  103. return ["code"=>-1,"msg"=>"系统错误"];
  104. }
  105. }
  106. public function runTask(){
  107. $key = "partnertaskcache".'::'.$this->enterpriseId;
  108. $result = $this->cache->get($key);
  109. if(!empty($result)){
  110. return ["code"=>-1,"msg"=>"操作频繁"];
  111. }
  112. $this->cache->set($key,1,10);
  113. $dbTask = new DNewCommissionTask('default');
  114. $taskTableName = $dbTask->getTableName($dbTask->get_Table(), $this->enterpriseId, 1);
  115. $dbTask->setTable($taskTableName);
  116. $data = $dbTask->select(["status"=>0],"*","id asc");
  117. if(empty($data)){
  118. return ["code"=>-1,"msg"=>"没有可执行任务"];
  119. }
  120. $nowTime=time();
  121. foreach($data as $k=>$v){
  122. $res = $this->runCalcMoneyData($v["orderId"]);
  123. $save=[];
  124. $save=["status"=>1,"mono"=>"执行成功","run_time"=>$nowTime];
  125. if(empty($res) || $res["code"]==-1){
  126. $save["mono"] = empty($res["msg"])?"系统错误执行失败":$res["msg"];
  127. $save["status"] = -1;
  128. }
  129. $dbTask->update($save, ["id"=>$v["id"]]);
  130. }
  131. $this->cache->set($key,null);
  132. return ["code"=>1,"msg"=>"执行结束"];
  133. }
  134. public function getCustomerDataByUserCenterId($userCenterId){
  135. if(empty($userCenterId)){
  136. return false;
  137. }
  138. $data = $this->dbCustomer->get(["userCenterId"=>$userCenterId]);
  139. if(empty($data)){
  140. return false;
  141. }
  142. return $data;
  143. }
  144. public function setPushCustomerByUserCenterId($parentId=0,$childId=0){
  145. //子级用户信息
  146. $childData = $this->dbCustomer->get(["userCenterId"=>$childId]);
  147. if (empty($childData) || $childData["parentId"]>0 || $childData["isPartner"]==1 || $childData["isParentHead"]==1) {
  148. return ["code"=>-1,"msg"=>"用户不能被绑定","data"=>$childData];
  149. }
  150. //父级用户信息
  151. $parentData = $this->dbCustomer->get(["userCenterId"=>$parentId]);
  152. if(empty($parentData)){
  153. return ["code"=>-1,"msg"=>"父级用户不存在","data"=>null];
  154. }
  155. return $this->setPushCustomer($parentData["id"],$childData["id"]);
  156. }
  157. /**
  158. * 设置推广用户
  159. * @param type $parentId
  160. * @param type $childId
  161. * @return bool
  162. */
  163. public function setPushCustomer($parentId=0,$childId=0){
  164. if($parentId == 0 || $childId == 0 || $parentId == $childId){
  165. return ["code"=>-1,"msg"=>"参数错误","data"=>null];
  166. }
  167. //子级用户信息
  168. $childData = $this->dbCustomer->get($childId);
  169. if (empty($childData) || $childData["parentId"]>0 || $childData["isPartner"]==1 || $childData["isParentHead"]==1) {
  170. return ["code"=>-1,"msg"=>"用户不能被绑定","data"=>$childData];
  171. }
  172. //父级用户信息
  173. $parentData = $this->dbCustomer->get($parentId);
  174. if(empty($parentData)){
  175. return ["code"=>-1,"msg"=>"父级用户不存在","data"=>null];
  176. }
  177. $beginStatus = $this->dbCustomer->beginTransaction();
  178. //保存父级数据
  179. if($parentData["isParentHead"] == 0){
  180. $parentSave=["isParentHead"=>1];
  181. $pres = $this->dbCustomer->update($parentSave, ["id"=>$parentId]);
  182. if(empty($pres)){
  183. $this->dbCustomer->rollBack();
  184. return ["code"=>-1,"msg"=>"父级数据保存失败01","data"=>$parentSave];
  185. }
  186. }
  187. //保存子级数据
  188. $parentPath = empty($parentData["parentPath"]) ? $parentId : $parentData["parentPath"] . "," . $parentId;
  189. $childSave=["parentId"=>$parentId,"parentPath"=>$parentPath];
  190. $res = $this->dbCustomer->update($childSave, ["id"=>$childId]);
  191. if(empty($res)){
  192. $this->dbCustomer->rollBack();
  193. return ["code"=>-1,"msg"=>"绑定失败","data"=>$childSave];
  194. }else{
  195. $beginStatus && $this->dbCustomer->commit();
  196. // $this->delCustomerCache();
  197. return ["code"=>1,"msg"=>"绑定成功","data"=>null];
  198. }
  199. }
  200. /**
  201. * 设置用户为合伙人
  202. * @param type $customerId
  203. */
  204. public function setPartner($customerId){
  205. $userData = $this->dbCustomer->get($customerId);
  206. if (empty($userData)) {
  207. return false;
  208. }
  209. if($userData["isPartner"]==1){
  210. return true;
  211. }
  212. $save=["isPartner"=>1];
  213. $res = $this->dbCustomer->update($save,["id"=>$customerId]);
  214. if(empty($res)){
  215. return false;
  216. }else{
  217. $this->getPartnerData($userData["id"], $userData["userCenterId"]);
  218. return true;
  219. }
  220. }
  221. /**
  222. * 获取用户父级合伙人
  223. * @param type $customerId
  224. * @return bool
  225. */
  226. public function getParentPartner($userData){
  227. if (empty($userData) || empty($userData["parentPath"])) {
  228. return false;
  229. }
  230. //逆序祖先,从最近的祖先先查
  231. $pathData = array_reverse(explode(",", trim($userData["parentPath"])));
  232. $where=[];
  233. $where[]=["isPartner","=",1];
  234. $where[]=["id","in",$pathData];
  235. $parentData = $this->dbCustomer->select($where);
  236. if(empty($parentData)){
  237. return false;
  238. }
  239. $parentPartner = null;
  240. for($i=0;$i<count($pathData);$i++){
  241. $itemData = [];
  242. foreach($parentData as $k=>$v){
  243. if($pathData[$i]==$v["id"]){
  244. $itemData = $v;
  245. break;
  246. }
  247. }
  248. if(!empty($itemData) && $itemData["isPartner"]==1){
  249. $parentPartner = $itemData;
  250. break;
  251. }
  252. }
  253. if(empty($parentPartner)){
  254. return false;
  255. }
  256. return $parentPartner;
  257. }
  258. /**
  259. * 佣金计算
  260. * @param type $customerId
  261. * @param type $money
  262. * @param type $type 1表示子级消费,2表示合伙人收益
  263. * @return bool
  264. */
  265. public function calcMoney($customerData,$money){
  266. $parentData = $this->getParentPartner($customerData);
  267. if(empty($parentData) || $parentData["isPartner"]==0){
  268. //上级合伙人不存在
  269. if($money>=$this->levMoney && $customerData["isPartner"]==0){
  270. $this->setPartner($customerData["id"]);
  271. }
  272. return false;
  273. }
  274. $per = $this->incomePer;
  275. $isUpgrade = 0;
  276. if($money>=$this->levMoney && $customerData["isPartner"]==0){
  277. $per = $this->levPer;
  278. $isUpgrade = 1;
  279. }
  280. $resData=[
  281. "childId"=>$customerData["id"],
  282. "childData"=>$customerData,
  283. "parentId"=>$parentData["id"],
  284. "parentData"=>$parentData,
  285. "per"=>$per,
  286. "money"=>$money,
  287. "commission"=>bcmul($money,$per,2),
  288. "isUpgrade"=>$isUpgrade
  289. ];
  290. return $resData;
  291. }
  292. /**
  293. * 订单消费计算佣金
  294. * @param type $orderCustomerId
  295. * @param type $orderMoney
  296. * @param type $isPart 订单用户如果是合伙人是否计算
  297. * @return bool
  298. */
  299. public function runCalcMoneyData($orderId){
  300. //获取订单信息
  301. if(empty($orderId)){
  302. return ["code"=>-1,"msg"=>"订单id错误"];
  303. }
  304. $orderIndexData = $this->dbOrderIndex->get(['id'=>$orderId]);
  305. if(empty($orderIndexData)){
  306. return ["code"=>-1,"msg"=>"订单索引数据不存在"];
  307. }
  308. // 切换订单分表,查询订单主单据数据
  309. $dbOrder = new DOrder('default');
  310. $fix = ceil($orderIndexData['userCenterId'] / 200000);
  311. $dbOrder->setTable('qianniao_order_' . $this->enterpriseId . '_' . $fix);
  312. $orderData = $dbOrder->get(['id' => $orderId]);
  313. if(empty($orderData)){
  314. return ["code"=>-1,"msg"=>"订单数据不存在"];
  315. }
  316. //开始计算
  317. $isPart=false;//订单用户如果是合伙人是否计算
  318. $orderCustomerId = $orderData["customerId"];
  319. $orderMoney = $orderData["payAmount"];
  320. //只计算小程序已完成订单
  321. if(empty($orderCustomerId) || empty($orderMoney) || $orderData["payType"]!=1 || $orderData["source"]!=3 || $orderData["orderStatus"]!=5){
  322. return ["code"=>-1,"msg"=>"只计算小程序微信支付已完成订单"];
  323. }
  324. $customerData = $this->dbCustomer->get($orderCustomerId);
  325. if (empty($customerData)) {
  326. return ["code"=>-1,"msg"=>"用户不存在"];
  327. }
  328. if(empty($customerData["parentPath"])){
  329. if($orderMoney>=$this->levMoney && $customerData["isPartner"]==0){
  330. $this->setPartner($orderCustomerId);
  331. }
  332. return ["code"=>-1,"msg"=>"用户不存在父级"];
  333. }
  334. //订单用户如果是合伙人消费则不计算收益
  335. if(!$isPart && $customerData["isPartner"]==1){
  336. return ["code"=>-1,"msg"=>"合伙人消费不计算上级收益"];
  337. }
  338. $data=[];
  339. $nowTime = time();
  340. //计算低层收益
  341. $bottomData = $this->calcMoney($customerData,$orderMoney);
  342. if(empty($bottomData)){
  343. return ["code"=>-1,"msg"=>"上级合伙人不存在"];
  344. }
  345. //合伙人账户
  346. $bottomPartnerData = $this->getPartnerData($bottomData["parentData"]["id"], $bottomData["parentData"]["userCenterId"]);
  347. if(empty($bottomPartnerData)){
  348. return ["code"=>-1,"msg"=>"上级合伙人账户错误"];
  349. }
  350. //记录底层收益
  351. $data[]=[
  352. "orderMoney"=>$orderMoney,//订单支付金额
  353. "calcMoney"=>$bottomData["money"],//佣金计算金额
  354. "sourceCustomerId"=>$bottomData["childId"],//来源客户id
  355. "partnerId"=>$bottomPartnerData["id"],//收钱合伙人账号id
  356. "customerId"=>$bottomPartnerData["customerId"],//收钱合伙人
  357. "userCenterId"=>$bottomPartnerData["userCenterId"],//收钱合伙人
  358. "commission"=>$bottomData["commission"],//佣金金额
  359. "per"=>$bottomData["per"],//佣金比例
  360. "type"=>0,
  361. "orderId"=>$orderData["id"],
  362. "orderNo"=>$orderData["no"],
  363. "status"=>0,//暂时不需要
  364. "title"=>"子级用户消费分佣",
  365. "isUpgrade"=>$bottomData["isUpgrade"],
  366. "mono"=>"",
  367. "time"=>$nowTime,
  368. ];
  369. //计算顶层收益
  370. $topData = $this->calcMoney($bottomData["parentData"],$bottomData["commission"]);
  371. if(!empty($topData)){
  372. $topPartnerData = $this->getPartnerData($topData["parentData"]["id"], $topData["parentData"]["userCenterId"]);
  373. if(empty($topPartnerData)){
  374. return ["code"=>-1,"msg"=>"顶级合伙人账户错误"];
  375. }
  376. //记录顶层收益
  377. $data[]=[
  378. "orderMoney"=>$orderMoney,//订单支付金额
  379. "calcMoney"=>$topData["money"],//佣金计算金额
  380. "sourceCustomerId"=>$topData["childId"],//来源客户id
  381. "partnerId"=>$topPartnerData["id"],//收钱合伙人账号id
  382. "customerId"=>$topPartnerData["customerId"],//收钱合伙人
  383. "userCenterId"=>$topPartnerData["userCenterId"],//收钱合伙人
  384. "commission"=>$topData["commission"],//佣金金额
  385. "per"=>$topData["per"],//佣金比例
  386. "type"=>1,//1表示子级合伙人收益计算收益,0表示子级消费计算收益
  387. "orderId"=>$orderData["id"],
  388. "orderNo"=>$orderData["no"],
  389. "status"=>0,
  390. "title"=>"子级合伙人收益分佣",
  391. "isUpgrade"=>$topData["isUpgrade"],
  392. "mono"=>"",
  393. "time"=>$nowTime,
  394. ];
  395. }
  396. $num=0;
  397. //开启事务
  398. $this->dbNewCommissionDetail->beginTransaction();
  399. $lms = "";
  400. foreach($data as $k=>$v){
  401. $count = $this->dbNewCommissionDetail->count(["orderId"=>$v["orderId"],"partnerId"=>$v["partnerId"]]);
  402. if($count>0){
  403. $lms.="当前订单已计算佣金[{$v['orderId']}];";
  404. continue;
  405. }
  406. $nid = $this->dbNewCommissionDetail->insert($v);
  407. if(empty($nid)){
  408. $lms.="佣金明细插入失败[{$v['orderId']}];";
  409. continue;
  410. }
  411. //查询合伙人账户
  412. $partnerItem = $this->dbNewCommissionPartner->get(["id"=>$v["partnerId"]]);
  413. if(empty($partnerItem)){
  414. $num = 0;
  415. $lms.="合伙人账户不存在[{$v['orderId']}];";
  416. break;
  417. }
  418. $update = [
  419. 'balance' => bcadd($partnerItem['balance'], $v["commission"], 2),
  420. 'totalMoney' => bcadd($partnerItem['totalMoney'], $v["commission"], 2),
  421. 'updateTime' => $nowTime,
  422. ];
  423. //添加账户余额明细记录
  424. $dres = $this->dbNewCommissionBalanceDetail->insert([
  425. "type"=>1,
  426. "title"=>$v["title"],
  427. "code"=>$v["type"]==1 ? "partner_calc" : "order_calc",
  428. "money"=>$v["commission"],
  429. "content"=>$v["title"],
  430. "admin_id"=>0,
  431. "customer_id"=>$v["customerId"],
  432. "partner_id"=>$v["partnerId"],
  433. "user_center_id"=>$v["userCenterId"],
  434. "cash_id"=>0,
  435. "old_balance"=>$partnerItem["balance"],
  436. "now_balance"=>$update["balance"],
  437. "time"=>$nowTime,
  438. "expand"=>"",
  439. "detail_id"=>$nid,
  440. ]);
  441. if(empty($dres)){
  442. $num = 0;
  443. $lms.="账户余额明细插入失败[{$v['orderId']}];";
  444. break;
  445. }
  446. //更新账户余额
  447. $upRes = $this->dbNewCommissionPartner->update($update, ['id' => $v["partnerId"]]);
  448. if(empty($upRes)){
  449. $num = 0;
  450. $lms.="更新账户余额失败[{$v['orderId']}];";
  451. break;
  452. }
  453. $num++;
  454. }
  455. if($num<=0){
  456. $this->dbNewCommissionDetail->rollBack();
  457. return ["code"=>-1,"msg"=>"数据更新失败:".$lms];
  458. }
  459. $this->dbNewCommissionDetail->commit();
  460. //佣金更新完后验证设置合伙人
  461. if($bottomData["isUpgrade"]==1){
  462. $this->setPartner($bottomData["childId"]);
  463. }
  464. return ["code"=>1,"msg"=>"执行成功".$lms];
  465. }
  466. public function getPartnerData($customerId,$userCenterId){
  467. if(empty($customerId)){
  468. return false;
  469. }
  470. $partnerData = $this->dbNewCommissionPartner->get(["customerId"=>$customerId]);
  471. if(empty($partnerData)){
  472. $nowTime = time();
  473. $partnerData=[
  474. "userCenterId"=>$userCenterId,
  475. "customerId"=>$customerId,
  476. "balance"=>0,
  477. "waitMoney"=>0,
  478. "withdraw"=>0,
  479. "totalMoney"=>0,
  480. "deleteStatus"=>5,
  481. "createTime"=>$nowTime,
  482. "updateTime"=>$nowTime,
  483. "expand"=>"",
  484. ];
  485. $partnerId = $this->dbNewCommissionPartner->insert($partnerData);
  486. if(empty($partnerId)){
  487. return false;
  488. }
  489. $partnerData["id"] = $partnerId;
  490. $partnerData["orderNum"] = 0;
  491. }else{
  492. $partnerData["orderNum"] = $this->getCommissionDetailCount($partnerData["id"]);
  493. }
  494. return $partnerData;
  495. }
  496. public function getCommissionDetailCount($partnerId,$type=0){
  497. if(empty($partnerId) && $type=0){
  498. return 0;
  499. }
  500. $where=[];
  501. if(!empty($partnerId)){
  502. $where["partnerId"] = $partnerId;
  503. }
  504. $count = $this->dbNewCommissionDetail->count($where);
  505. return $count?$count:0;
  506. }
  507. /**
  508. * 获取分佣明细
  509. */
  510. public function getCommissionDetail($customerId,$parms){
  511. $where=[];
  512. if(!empty($parms["where"])){
  513. $where= $parms["where"];
  514. }
  515. if(!empty($customerId)){
  516. $where["customerId"] = $customerId;
  517. }
  518. $data = $this->dbNewCommissionDetail->select($where, "*", "id desc", $parms["limit"], $parms["offset"]);
  519. $total = $this->dbNewCommissionDetail->count($where);
  520. if(empty($data)){
  521. $data = [];
  522. }
  523. foreach($data as $k=>$v){
  524. $itemUser = $this->dbCustomer->get(["id"=>$v["sourceCustomerId"]],"id,name,avatar");
  525. if(empty($itemUser)){
  526. $data[$k]["name"] = "";
  527. $data[$k]["avatar"] = "";
  528. }else{
  529. $data[$k]["name"] = $itemUser["name"];
  530. $data[$k]["avatar"] = $itemUser["avatar"];
  531. }
  532. }
  533. $return = [
  534. 'data' => $data,
  535. 'total' => (isset($total)&&!empty($total)) ? intval($total) : 0,
  536. ];
  537. return $return;
  538. }
  539. /**
  540. * 获取合伙人账户余额明细
  541. */
  542. public function getCommissionBalanceDetail($customerId,$parms){
  543. $where=[];
  544. if(!empty($customerId)){
  545. $where["customer_id"] = $customerId;
  546. }
  547. $data = $this->dbNewCommissionBalanceDetail->select($where, "*", "id desc", $parms["limit"], $parms["offset"]);
  548. $total = $this->dbNewCommissionBalanceDetail->count($where);
  549. if(empty($data)){
  550. $data = [];
  551. }
  552. $return = [
  553. 'data' => $data,
  554. 'total' => (isset($total)&&!empty($total)) ? intval($total) : 0,
  555. ];
  556. return $return;
  557. }
  558. /**
  559. *
  560. * @param type $type 0返回标题数组,1范围完整数组
  561. * @return type
  562. */
  563. public static function getBankType($type=0,$title=""){
  564. $data = [
  565. ["title"=>"支付宝","code"=>"alipay","icon"=>"ibonumidd17","addefault"=>"支付宝"],
  566. ["title"=>"微信钱包","code"=>"wxpay","icon"=>"ibonweixinzhifu","addefault"=>"微信"],
  567. ["title"=>"银行卡","code"=>"bank","icon"=>"ibonyinhangqia","addefault"=>""],
  568. ];
  569. if(!empty($title)){
  570. foreach($data as $k=>$v){
  571. if($v["title"]==$title){
  572. return $v;
  573. }
  574. }
  575. return [];
  576. }
  577. if($type==1){
  578. return $data;
  579. }
  580. $nameAr= [];
  581. foreach($data as $k=>$v){
  582. $nameAr[]=$v["title"];
  583. }
  584. return $nameAr;
  585. }
  586. /**
  587. * 提现申请
  588. */
  589. public function applyCash($data){
  590. 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"])){
  591. return ["code"=>-1,"msg"=>"请填写完整信息"];
  592. }
  593. //频繁操作限制
  594. $key = "partnerCashCache_".$data["customerId"].'::'.$this->enterpriseId;
  595. $result = $this->cache->get($key);
  596. if(!empty($result)){
  597. return ["code"=>-1,"msg"=>"操作频繁,请稍后尝试!"];
  598. }
  599. $count = $this->dbNewCommissionCash->count(["status"=>0,"customer_id"=>$data["customerId"]]);
  600. if($count>0){
  601. return ["code"=>-1,"msg"=>"上次申请还在处理中"];
  602. }
  603. $this->cache->set($key,1,5);
  604. if(!in_array($data["bank_type"], self::getBankType())){
  605. return ["code"=>-1,"msg"=>"提现方式不存在"];
  606. }
  607. $bankTypeData = self::getBankType(1, $data["bank_type"]);
  608. if(empty($bankTypeData)){
  609. return ["code"=>-1,"msg"=>"提现方式不存在"];
  610. }
  611. if(!is_numeric($data["money"])){
  612. return ["code"=>-1,"msg"=>"提现金额错误"];
  613. }
  614. $data["money"] = bcadd($data["money"],0,2);
  615. if($data["money"]<=0){
  616. return ["code"=>-1,"msg"=>"提现金额错误"];
  617. }
  618. $partnerData = $this->dbNewCommissionPartner->get(["customerId"=>$data["customerId"]]);
  619. if(empty($partnerData) || empty($partnerData["id"])){
  620. return ["code"=>-1,"msg"=>"合伙人不存在"];
  621. }
  622. if($data["money"]>$partnerData["balance"] || $partnerData["balance"]<=0){
  623. return ["code"=>-1,"msg"=>"提现金额不足"];
  624. }
  625. $nowTime = time();
  626. $save=[
  627. "customer_id" => $partnerData["customerId"],
  628. "user_center_id" => $partnerData["userCenterId"],
  629. "partner_id" => $partnerData["id"],
  630. "bank_type" => $data["bank_type"],
  631. "bank_type_code" => $bankTypeData["code"],
  632. "bank_name" => $data["bank_name"],
  633. "bank_num" => $data["bank_num"],
  634. "bank_ad" => $data["bank_ad"],
  635. "money" => $data["money"],
  636. "status" => 0,
  637. "time" => $nowTime
  638. ];
  639. try{
  640. $this->dbNewCommissionPartner->beginTransaction();
  641. //插入提现申请
  642. $applyId = $this->dbNewCommissionCash->insert($save);
  643. if(empty($applyId)){
  644. return ["code"=>-1,"msg"=>"数据提交失败001"];
  645. }
  646. //更新提现账户余额并添加明细
  647. $update = [
  648. 'balance' => bcsub($partnerData["balance"], $save["money"], 2),//账户余额
  649. 'withdraw' => bcadd($partnerData["withdraw"], $save["money"],2),//已提现金额
  650. 'updateTime' => $nowTime,
  651. ];
  652. if($update["balance"]<=0){
  653. $this->dbNewCommissionPartner->rollBack();
  654. return ["code"=>-1,"msg"=>"数据提交失败002"];
  655. }
  656. //添加账户余额明细记录
  657. $dres = $this->dbNewCommissionBalanceDetail->insert([
  658. "type"=>-1,
  659. "title"=>"余额提现",
  660. "code"=>"cash_balance",
  661. "money"=>$save["money"],
  662. "content"=>"合伙人余额提现",
  663. "admin_id"=>0,
  664. "customer_id"=>$partnerData["customerId"],
  665. "partner_id"=>$partnerData["id"],
  666. "user_center_id"=>$partnerData["userCenterId"],
  667. "cash_id"=>$applyId,
  668. "old_balance"=>$partnerData["balance"],
  669. "now_balance"=>$update["balance"],
  670. "time"=>$nowTime,
  671. "expand"=>"",
  672. "detail_id"=>0,
  673. ]);
  674. if(empty($dres)){
  675. $this->dbNewCommissionPartner->rollBack();
  676. return ["code"=>-1,"msg"=>"账户余额明细插入失败"];
  677. }
  678. //更新账户余额
  679. $upRes = $this->dbNewCommissionPartner->update($update, ['id' => $partnerData["id"]]);
  680. if(empty($upRes)){
  681. $this->dbNewCommissionPartner->rollBack();
  682. return ["code"=>-1,"msg"=>"更新账户余额失败"];
  683. }
  684. $this->dbNewCommissionPartner->commit();
  685. $this->cache->set($key,null);
  686. return ["code"=>1,"msg"=>"提现申请提交成功"];
  687. } catch (\Exception $e){
  688. $this->dbNewCommissionPartner->rollBack();
  689. $this->cache->set($key,null);
  690. return ["code"=>1,"msg"=>"提现申请提交失败"];
  691. }
  692. }
  693. /**
  694. * 获取提现记录
  695. * @param type $customerId
  696. * @param type $parms
  697. * @return type
  698. */
  699. public function getPartnerApplyCashList($customerId,$parms){
  700. $where=[];
  701. if(!empty($parms["where"])){
  702. $where= $parms["where"];
  703. }
  704. if(!empty($customerId)){
  705. $where["customer_id"] = $customerId;
  706. }
  707. $data = $this->dbNewCommissionCash->select($where, "*", "id desc", $parms["limit"], $parms["offset"]);
  708. $total = $this->dbNewCommissionCash->count($where);
  709. if(empty($data)){
  710. $data = [];
  711. }
  712. $return = [
  713. 'data' => $data,
  714. 'total' => (isset($total)&&!empty($total)) ? intval($total) : 0,
  715. ];
  716. return $return;
  717. }
  718. /**
  719. * 获取提现详情
  720. */
  721. public function getPartnerApplyCashInfo($customerId,$parms){
  722. $where=[];
  723. if(!empty($parms["where"])){
  724. $where= $parms["where"];
  725. }
  726. if(!empty($customerId)){
  727. $where["customer_id"] = $customerId;
  728. }
  729. $data = $this->dbNewCommissionCash->get($where);
  730. return $data;
  731. }
  732. /**
  733. * 获取合伙人列表
  734. * @param type $params
  735. */
  736. public function getPartnerList($params = []){
  737. $customerTableName = 'qianniao_customer_'.$this->enterpriseId;
  738. $userCenterTableName = 'qianniao_user_center';
  739. $newCommissionPartnerTableName = 'qianniao_new_commission_partner_'.$this->enterpriseId;
  740. //查询数据
  741. $field = "p.*,u.mobile,c.name,c.avatar";
  742. $sql = "select {$field} from {$newCommissionPartnerTableName} p "
  743. . "left join {$userCenterTableName} u on u.id = p.userCenterId "
  744. . "left join {$customerTableName} c on c.id = p.customerId";
  745. $whereSql = ' where p.deleteStatus = 5 ';
  746. if(isset($params['search'])){
  747. $whereSql .= ' and (u.mobile like "%'.$params['search'].'%" or c.name like "%'.$params['search'].'%") ';
  748. }
  749. $orderSql = ' order by p.id desc ';
  750. $limitSql = ' limit '.$params['offset'].','.$params['limit'];
  751. $querySql = $sql.$whereSql.$orderSql.$limitSql;
  752. $data = $this->dbNewCommissionPartner->query($querySql);
  753. if(empty($data)){
  754. $data=[];
  755. }
  756. //查询总数
  757. $countSql = "select count(*) as `count` from {$newCommissionPartnerTableName} p "
  758. . "left join {$userCenterTableName} u on u.id = p.userCenterId "
  759. . "left join {$customerTableName} c on c.id = p.customerId";
  760. $countData = $this->dbNewCommissionPartner->query($countSql.$whereSql);
  761. $total = 0;
  762. if(!empty($countData)){
  763. $total =array_shift($countData)['count'];
  764. }
  765. return ['data'=>$data,'total'=>($total) ? intval($total) : 0];
  766. }
  767. /**
  768. * 获取合伙人余额明细列表
  769. * @param type $params
  770. */
  771. public function getBalanceDetailList($params = []){
  772. $customerTableName = 'qianniao_customer_'.$this->enterpriseId;
  773. $userCenterTableName = 'qianniao_user_center';
  774. // $newCommissionPartnerTableName = 'qianniao_new_commission_partner_'.$this->enterpriseId;
  775. $newCommissionBalanceDetailTableName = 'qianniao_new_commission_balance_detail_'.$this->enterpriseId;
  776. //查询数据
  777. $field = "p.*,u.mobile,c.name,c.avatar";
  778. $sql = "select {$field} from {$newCommissionBalanceDetailTableName} p "
  779. . "left join {$userCenterTableName} u on u.id = p.user_center_id "
  780. . "left join {$customerTableName} c on c.id = p.customer_id";
  781. $whereSql = ' where p.id>0 ';
  782. if(isset($params['search'])){
  783. $whereSql .= ' and (u.mobile like "%'.$params['search'].'%" or c.name like "%'.$params['search'].'%") ';
  784. }
  785. $orderSql = ' order by p.id desc ';
  786. $limitSql = ' limit '.$params['offset'].','.$params['limit'];
  787. $querySql = $sql.$whereSql.$orderSql.$limitSql;
  788. $data = $this->dbNewCommissionPartner->query($querySql);
  789. if(empty($data)){
  790. $data=[];
  791. }
  792. //查询总数
  793. $countSql = "select count(*) as `count` from {$newCommissionBalanceDetailTableName} p "
  794. . "left join {$userCenterTableName} u on u.id = p.user_center_id "
  795. . "left join {$customerTableName} c on c.id = p.customer_id";
  796. $countData = $this->dbNewCommissionPartner->query($countSql.$whereSql);
  797. $total = 0;
  798. if(!empty($countData)){
  799. $total =array_shift($countData)['count'];
  800. }
  801. return ['data'=>$data,'total'=>($total) ? intval($total) : 0];
  802. }
  803. /**
  804. * 获取合伙人佣金记录
  805. * @param type $params
  806. */
  807. public function getDetailList($params = []){
  808. $customerTableName = 'qianniao_customer_'.$this->enterpriseId;
  809. $userCenterTableName = 'qianniao_user_center';
  810. // $newCommissionPartnerTableName = 'qianniao_new_commission_partner_'.$this->enterpriseId;
  811. // $newCommissionBalanceDetailTableName = 'qianniao_new_commission_balance_detail_'.$this->enterpriseId;
  812. $newCommissionDetailTableName = 'qianniao_new_commission_detail_'.$this->enterpriseId;
  813. //查询数据
  814. $field = "p.*,u.mobile,c.name,c.avatar,c2.name as sourceName,c2.userCenterId as sourceUserCenterId ";
  815. $sql = "select {$field} from {$newCommissionDetailTableName} p "
  816. . "left join {$userCenterTableName} u on u.id = p.userCenterId "
  817. . "left join {$customerTableName} c on c.id = p.customerId "
  818. . "left join {$customerTableName} c2 on c2.id = p.sourceCustomerId";
  819. $whereSql = ' where p.id>0 ';
  820. if(isset($params['search'])){
  821. $whereSql .= ' and (u.mobile like "%'.$params['search'].'%" or c.name like "%'.$params['search'].'%") ';
  822. }
  823. $orderSql = ' order by p.id desc ';
  824. $limitSql = ' limit '.$params['offset'].','.$params['limit'];
  825. $querySql = $sql.$whereSql.$orderSql.$limitSql;
  826. $data = $this->dbNewCommissionPartner->query($querySql);
  827. if(empty($data)){
  828. $data=[];
  829. }
  830. //查询总数
  831. $countSql = "select count(*) as `count` from {$newCommissionDetailTableName} p "
  832. . "left join {$userCenterTableName} u on u.id = p.userCenterId "
  833. . "left join {$customerTableName} c on c.id = p.customerId "
  834. . "left join {$customerTableName} c2 on c2.id = p.sourceCustomerId";
  835. $countData = $this->dbNewCommissionPartner->query($countSql.$whereSql);
  836. $total = 0;
  837. if(!empty($countData)){
  838. $total =array_shift($countData)['count'];
  839. }
  840. return ['data'=>$data,'total'=>($total) ? intval($total) : 0];
  841. }
  842. /**
  843. * 获取提现记录
  844. * @param type $params
  845. */
  846. public function getPartnerCashList($params = []){
  847. $customerTableName = 'qianniao_customer_'.$this->enterpriseId;
  848. $userCenterTableName = 'qianniao_user_center';
  849. // $newCommissionPartnerTableName = 'qianniao_new_commission_partner_'.$this->enterpriseId;
  850. // $newCommissionBalanceDetailTableName = 'qianniao_new_commission_balance_detail_'.$this->enterpriseId;
  851. // $newCommissionDetailTableName = 'qianniao_new_commission_detail_'.$this->enterpriseId;
  852. $newCommissionCashTableName = 'qianniao_new_commission_cash_'.$this->enterpriseId;
  853. //查询数据
  854. $field = "p.*,u.mobile,c.name,c.avatar ";
  855. $sql = "select {$field} from {$newCommissionCashTableName} p "
  856. . "left join {$userCenterTableName} u on u.id = p.user_center_id "
  857. . "left join {$customerTableName} c on c.id = p.customer_id ";
  858. $whereSql = ' where 1=1 ';
  859. if(isset($params['search'])){
  860. $whereSql .= ' and (u.mobile like "%'.$params['search'].'%" or c.name like "%'.$params['search'].'%") ';
  861. }
  862. if(isset($params['status'])){
  863. $whereSql .= ' and p.status = '.$params['status'];
  864. }
  865. if(!empty($params['bank_type_code'])){
  866. $whereSql .= " and p.bank_type_code = '{$params['bank_type_code']}'";
  867. }
  868. if(!empty($params['start_time']) && !empty($params['end_time'])){
  869. $whereSql .= " and p.time >= {$params['start_time']} and p.time < {$params['end_time']}";
  870. }
  871. $orderSql = ' order by p.id desc ';
  872. $limitSql = ' limit '.$params['offset'].','.$params['limit'];
  873. $querySql = $sql.$whereSql.$orderSql.$limitSql;
  874. $data = $this->dbNewCommissionPartner->query($querySql);
  875. if(empty($data)){
  876. $data=[];
  877. }
  878. //查询总数
  879. $countSql = "select count(*) as `count` from {$newCommissionCashTableName} p "
  880. . "left join {$userCenterTableName} u on u.id = p.user_center_id "
  881. . "left join {$customerTableName} c on c.id = p.customer_id ";
  882. $countData = $this->dbNewCommissionPartner->query($countSql.$whereSql);
  883. $total = 0;
  884. if(!empty($countData)){
  885. $total =array_shift($countData)['count'];
  886. }
  887. return ['data'=>$data,'total'=>($total) ? intval($total) : 0];
  888. }
  889. /**
  890. * 提现处理
  891. * @param type $id
  892. * @param type $mono
  893. * @param type $type
  894. */
  895. public function partnerCashAudit($id,$mono,$type,$audit_id=0){
  896. if(empty($id)|| empty($mono) || empty($type) || !in_array($type, [1,-1])){
  897. return ["code"=>-1,"msg"=>"参数错误"];
  898. }
  899. //频繁操作限制
  900. $key = "partnerCashDealCache_".$id.'::'.$this->enterpriseId;
  901. $result = $this->cache->get($key);
  902. if(!empty($result)){
  903. return ["code"=>-1,"msg"=>"处理中,请耐心等待!"];
  904. }
  905. $this->cache->set($key,1,5);
  906. $where = ["id"=>$id,"status"=>0];
  907. $data = $this->dbNewCommissionCash->get($where);
  908. if(empty($data)){
  909. $this->cache->set($key,null);
  910. return ["code"=>-1,"msg"=>"数据不存在"];
  911. }
  912. //合伙人账户信息
  913. $partnerData = $this->dbNewCommissionPartner->get(["id"=>$data["partner_id"]]);
  914. if(empty($partnerData)){
  915. $this->cache->set($key,null);
  916. return ["code"=>-1,"msg"=>"合伙人账户不存在"];
  917. }
  918. //打款成功
  919. if($type==1){
  920. $res = $this->dbNewCommissionCash->update([
  921. "status"=>1,
  922. "audit_time"=>time(),
  923. "audti_mono"=>$mono,
  924. "audit_id"=>$audit_id
  925. ], $where);
  926. $this->cache->set($key,null);
  927. if(empty($res )){
  928. return ["code"=>-1,"msg"=>"系统繁忙,请稍后重试!"];
  929. }
  930. return ["code"=>1,"msg"=>"处理完成"];
  931. }
  932. //审核驳回
  933. try{
  934. $this->dbNewCommissionCash->beginTransaction();
  935. $res = $this->dbNewCommissionCash->update([
  936. "status"=>-1,
  937. "audit_time"=>time(),
  938. "audti_mono"=>$mono,
  939. "audit_id"=>$audit_id
  940. ], $where);
  941. if(empty($res)){
  942. $this->dbNewCommissionCash->rollBack();
  943. $this->cache->set($key,null);
  944. return ["code"=>-1,"msg"=>"操作失败001!"];
  945. }
  946. //添加账户余额明细记录
  947. $dres = $this->dbNewCommissionBalanceDetail->insert([
  948. "type"=>1,
  949. "title"=>"提现驳回返还余额",
  950. "code"=>"cash_no_balance",
  951. "money"=>$data["money"],
  952. "content"=>"提现驳回返还余额:".$mono,
  953. "admin_id"=>$audit_id,
  954. "customer_id"=>$partnerData["customerId"],
  955. "partner_id"=>$partnerData["id"],
  956. "user_center_id"=>$partnerData["userCenterId"],
  957. "cash_id"=>$data["id"],
  958. "old_balance"=>$partnerData["balance"],
  959. "now_balance"=>$partnerData["balance"]+$data["money"],
  960. "time"=>time(),
  961. "expand"=>"",
  962. "detail_id"=>0,
  963. ]);
  964. if(empty($dres)){
  965. $this->dbNewCommissionCash->rollBack();
  966. $this->cache->set($key,null);
  967. return ["code"=>-1,"msg"=>"操作失败002!"];
  968. }
  969. //更新账户数据
  970. $withdrawRes = $this->dbNewCommissionPartner->set_dec("withdraw", ["id"=>$partnerData["id"]], $data["money"]);
  971. if(empty($withdrawRes)){
  972. $this->dbNewCommissionCash->rollBack();
  973. $this->cache->set($key,null);
  974. return ["code"=>-1,"msg"=>"操作失败003"];
  975. }
  976. $balanceRes = $this->dbNewCommissionPartner->set_inc("balance", ["id"=>$partnerData["id"]], $data["money"]);
  977. if(empty($balanceRes)){
  978. $this->dbNewCommissionCash->rollBack();
  979. $this->cache->set($key,null);
  980. return ["code"=>-1,"msg"=>"操作失败004"];
  981. }
  982. //提交事务
  983. $this->dbNewCommissionCash->commit();
  984. $this->cache->set($key,null);
  985. return ["code"=>1,"msg"=>"提现申请处理成功!"];
  986. } catch (\Exception $e){
  987. $this->dbNewCommissionCash->rollBack();
  988. $this->cache->set($key,null);
  989. return ["code"=>-1,"msg"=>"系统繁忙,请稍后重试!"];
  990. }
  991. }
  992. /**
  993. * 删除用户缓存
  994. * @param type $customerId
  995. * @param type $userCenterId
  996. */
  997. public function delCustomerCache($customerId,$userCenterId){
  998. $objCustomerCache = new CustomerCache();
  999. $objCustomerCache->delCustomerData($this->enterpriseId, $customerId);
  1000. $objCustomerCache->delCustomerUserData($this->enterpriseId, $userCenterId);
  1001. }
  1002. }