PartnerTools.Class.php 42 KB

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