PartnerTools.Class.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  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. ];
  33. public function __construct($enterpriseId){
  34. $this->enterpriseId = $enterpriseId;
  35. $this->cache = Factory::cache('systask');
  36. //用户表
  37. $this->dbCustomer = new DCustomer('default');
  38. $customerTableName = $this->dbCustomer->getTableName($this->dbCustomer->get_Table(), $this->enterpriseId, $this->cutTable);
  39. $this->dbCustomer->setTable($customerTableName);
  40. //订单索引表
  41. $this->dbOrderIndex = new DOrderIndex();
  42. $this->dbOrderIndex->setTable('qianniao_order_index_' . $this->enterpriseId);
  43. //新分销合伙人表
  44. $this->dbNewCommissionPartner = new DNewCommissionPartner('default');
  45. $newCommissionPartnerTableName = $this->dbNewCommissionPartner->getTableName($this->dbNewCommissionPartner->get_Table(), $this->enterpriseId, $this->cutTable);
  46. $this->dbNewCommissionPartner->setTable($newCommissionPartnerTableName);
  47. //新分销佣金明细表
  48. $this->dbNewCommissionDetail = new DNewCommissionDetail('default');
  49. $newCommissionDetailTableName = $this->dbNewCommissionDetail->getTableName($this->dbNewCommissionDetail->get_Table(), $this->enterpriseId, $this->cutTable);
  50. $this->dbNewCommissionDetail->setTable($newCommissionDetailTableName);
  51. //新分销合伙人提现表
  52. $this->dbNewCommissionCash = new DNewCommissionCash('default');
  53. $newCommissionCashTableName = $this->dbNewCommissionCash->getTableName($this->dbNewCommissionCash->get_Table(), $this->enterpriseId, $this->cutTable);
  54. $this->dbNewCommissionCash->setTable($newCommissionCashTableName);
  55. //新分销合伙人余额明细表
  56. $this->dbNewCommissionBalanceDetail = new DNewCommissionBalanceDetail('default');
  57. $newCommissionBalanceDetailTableName = $this->dbNewCommissionBalanceDetail->getTableName($this->dbNewCommissionBalanceDetail->get_Table(), $this->enterpriseId, $this->cutTable);
  58. $this->dbNewCommissionBalanceDetail->setTable($newCommissionBalanceDetailTableName);
  59. //新分销合伙人分佣任务表
  60. $this->dbNewCommissionTask = new DNewCommissionTask('default');
  61. $newCommissionTaskTableName = $this->dbNewCommissionTask->getTableName($this->dbNewCommissionTask->get_Table(), $this->enterpriseId, $this->cutTable);
  62. $this->dbNewCommissionTask->setTable($newCommissionTaskTableName);
  63. }
  64. public function cacheTest(){
  65. $key = "partnertaskcache".'::'.$this->enterpriseId;
  66. $result = $this->cache->get($key);
  67. if(!empty($result)){
  68. return "重复操作";
  69. }
  70. $this->cache->set($key,1,10);
  71. return "开始执行操作";
  72. }
  73. /**
  74. * 添加任务
  75. * @param type $orderId
  76. * @param type $enterpriseId
  77. * @return bool
  78. */
  79. public static function addTask($orderId,$enterpriseId){
  80. try{
  81. if(empty($orderId) || empty($enterpriseId)){
  82. return ["code"=>-1,"msg"=>"参数为空"];
  83. }
  84. $dbTask = new DNewCommissionTask('default');
  85. $taskTableName = $dbTask->getTableName($dbTask->get_Table(), $enterpriseId, 1);
  86. $dbTask->setTable($taskTableName);
  87. $count = $dbTask->count(["orderId"=>$orderId]);
  88. if($count>0){
  89. return ["code"=>-1,"msg"=>"任务订单已存在"];
  90. }
  91. $r = $dbTask->insert([
  92. "orderId"=>$orderId,
  93. "enterpriseId"=>$enterpriseId,
  94. "title"=>"订单完成计算分佣",
  95. "mono"=>"",
  96. "status"=>0,
  97. "run_time"=>0,
  98. "time"=>time(),
  99. ]);
  100. return ["code"=>1,"msg"=>"任务已插入[{$r}]"];
  101. } catch (\Exception $e){
  102. return ["code"=>-1,"msg"=>"系统错误"];
  103. }
  104. }
  105. public function runTask(){
  106. $key = "partnertaskcache".'::'.$this->enterpriseId;
  107. $result = $this->cache->get($key);
  108. if(!empty($result)){
  109. return ["code"=>-1,"msg"=>"操作频繁"];
  110. }
  111. $this->cache->set($key,1,10);
  112. $dbTask = new DNewCommissionTask('default');
  113. $taskTableName = $dbTask->getTableName($dbTask->get_Table(), $this->enterpriseId, 1);
  114. $dbTask->setTable($taskTableName);
  115. $data = $dbTask->select(["status"=>0],"*","id asc");
  116. if(empty($data)){
  117. return ["code"=>-1,"msg"=>"没有可执行任务"];
  118. }
  119. $nowTime=time();
  120. foreach($data as $k=>$v){
  121. $res = $this->runCalcMoneyData($v["orderId"]);
  122. $save=[];
  123. $save=["status"=>1,"mono"=>"执行成功","run_time"=>$nowTime];
  124. if(empty($res) || $res["code"]==-1){
  125. $save["mono"] = empty($res["msg"])?"系统错误执行失败":$res["msg"];
  126. $save["status"] = -1;
  127. }
  128. $dbTask->update($save, ["id"=>$v["id"]]);
  129. }
  130. $this->cache->set($key,null);
  131. return ["code"=>1,"msg"=>"执行结束"];
  132. }
  133. public function getCustomerDataByUserCenterId($userCenterId){
  134. if(empty($userCenterId)){
  135. return false;
  136. }
  137. $data = $this->dbCustomer->get(["userCenterId"=>$userCenterId]);
  138. if(empty($data)){
  139. return false;
  140. }
  141. return $data;
  142. }
  143. public function setPushCustomerByUserCenterId($parentId=0,$childId=0){
  144. //子级用户信息
  145. $childData = $this->dbCustomer->get(["userCenterId"=>$childId]);
  146. if (empty($childData) || $childData["parentId"]>0 || $childData["isPartner"]==1 || $childData["isParentHead"]==1) {
  147. return ["code"=>-1,"msg"=>"用户不能被绑定","data"=>$childData];
  148. }
  149. //父级用户信息
  150. $parentData = $this->dbCustomer->get(["userCenterId"=>$parentId]);
  151. if(empty($parentData)){
  152. return ["code"=>-1,"msg"=>"父级用户不存在","data"=>null];
  153. }
  154. return $this->setPushCustomer($parentData["id"],$childData["id"]);
  155. }
  156. /**
  157. * 设置推广用户
  158. * @param type $parentId
  159. * @param type $childId
  160. * @return bool
  161. */
  162. public function setPushCustomer($parentId=0,$childId=0){
  163. if($parentId == 0 || $childId == 0 || $parentId == $childId){
  164. return ["code"=>-1,"msg"=>"参数错误","data"=>null];
  165. }
  166. //子级用户信息
  167. $childData = $this->dbCustomer->get($childId);
  168. if (empty($childData) || $childData["parentId"]>0 || $childData["isPartner"]==1 || $childData["isParentHead"]==1) {
  169. return ["code"=>-1,"msg"=>"用户不能被绑定","data"=>$childData];
  170. }
  171. //父级用户信息
  172. $parentData = $this->dbCustomer->get($parentId);
  173. if(empty($parentData)){
  174. return ["code"=>-1,"msg"=>"父级用户不存在","data"=>null];
  175. }
  176. $beginStatus = $this->dbCustomer->beginTransaction();
  177. //保存父级数据
  178. if($parentData["isParentHead"] == 0){
  179. $parentSave=["isParentHead"=>1];
  180. $pres = $this->dbCustomer->update($parentSave, ["id"=>$parentId]);
  181. if(empty($pres)){
  182. $this->dbCustomer->rollBack();
  183. return ["code"=>-1,"msg"=>"父级数据保存失败01","data"=>$parentSave];
  184. }
  185. }
  186. //保存子级数据
  187. $parentPath = empty($parentData["parentPath"]) ? $parentId : $parentData["parentPath"] . "," . $parentId;
  188. $childSave=["parentId"=>$parentId,"parentPath"=>$parentPath];
  189. $res = $this->dbCustomer->update($childSave, ["id"=>$childId]);
  190. if(empty($res)){
  191. $this->dbCustomer->rollBack();
  192. return ["code"=>-1,"msg"=>"绑定失败","data"=>$childSave];
  193. }else{
  194. $beginStatus && $this->dbCustomer->commit();
  195. // $this->delCustomerCache();
  196. return ["code"=>1,"msg"=>"绑定成功","data"=>null];
  197. }
  198. }
  199. /**
  200. * 设置用户为合伙人
  201. * @param type $customerId
  202. */
  203. public function setPartner($customerId){
  204. $userData = $this->dbCustomer->get($customerId);
  205. if (empty($userData)) {
  206. return false;
  207. }
  208. if($userData["isPartner"]==1){
  209. return true;
  210. }
  211. $save=["isPartner"=>1];
  212. $res = $this->dbCustomer->update($save,["id"=>$customerId]);
  213. if(empty($res)){
  214. return false;
  215. }else{
  216. $this->getPartnerData($userData["id"], $userData["userCenterId"]);
  217. return true;
  218. }
  219. }
  220. /**
  221. * 获取用户父级合伙人
  222. * @param type $customerId
  223. * @return bool
  224. */
  225. public function getParentPartner($userData){
  226. if (empty($userData) || empty($userData["parentPath"])) {
  227. return false;
  228. }
  229. //逆序祖先,从最近的祖先先查
  230. $pathData = array_reverse(explode(",", trim($userData["parentPath"])));
  231. $where=[];
  232. $where[]=["isPartner","=",1];
  233. $where[]=["id","in",$pathData];
  234. $parentData = $this->dbCustomer->select($where);
  235. if(empty($parentData)){
  236. return false;
  237. }
  238. $parentPartner = null;
  239. for($i=0;$i<count($pathData);$i++){
  240. $itemData = [];
  241. foreach($parentData as $k=>$v){
  242. if($pathData[$i]==$v["id"]){
  243. $itemData = $v;
  244. break;
  245. }
  246. }
  247. if(!empty($itemData) && $itemData["isPartner"]==1){
  248. $parentPartner = $itemData;
  249. break;
  250. }
  251. }
  252. if(empty($parentPartner)){
  253. return false;
  254. }
  255. return $parentPartner;
  256. }
  257. /**
  258. * 佣金计算
  259. * @param type $customerId
  260. * @param type $money
  261. * @param type $type 1表示子级消费,2表示合伙人收益
  262. * @return bool
  263. */
  264. public function calcMoney($customerData,$money){
  265. $parentData = $this->getParentPartner($customerData);
  266. if(empty($parentData) || $parentData["isPartner"]==0){
  267. //上级合伙人不存在
  268. if($money>=$this->levMoney && $customerData["isPartner"]==0){
  269. $this->setPartner($customerData["id"]);
  270. }
  271. return false;
  272. }
  273. $per = $this->incomePer;
  274. $isUpgrade = 0;
  275. if($money>=$this->levMoney && $customerData["isPartner"]==0){
  276. $per = $this->levPer;
  277. $isUpgrade = 1;
  278. }
  279. $resData=[
  280. "childId"=>$customerData["id"],
  281. "childData"=>$customerData,
  282. "parentId"=>$parentData["id"],
  283. "parentData"=>$parentData,
  284. "per"=>$per,
  285. "money"=>$money,
  286. "commission"=>bcmul($money,$per,2),
  287. "isUpgrade"=>$isUpgrade
  288. ];
  289. return $resData;
  290. }
  291. /**
  292. * 订单消费计算佣金
  293. * @param type $orderCustomerId
  294. * @param type $orderMoney
  295. * @param type $isPart 订单用户如果是合伙人是否计算
  296. * @return bool
  297. */
  298. public function runCalcMoneyData($orderId){
  299. //获取订单信息
  300. if(empty($orderId)){
  301. return ["code"=>-1,"msg"=>"订单id错误"];
  302. }
  303. $orderIndexData = $this->dbOrderIndex->get(['id'=>$orderId]);
  304. if(empty($orderIndexData)){
  305. return ["code"=>-1,"msg"=>"订单索引数据不存在"];
  306. }
  307. // 切换订单分表,查询订单主单据数据
  308. $dbOrder = new DOrder('default');
  309. $fix = ceil($orderIndexData['userCenterId'] / 200000);
  310. $dbOrder->setTable('qianniao_order_' . $this->enterpriseId . '_' . $fix);
  311. $orderData = $dbOrder->get(['id' => $orderId]);
  312. if(empty($orderData)){
  313. return ["code"=>-1,"msg"=>"订单数据不存在"];
  314. }
  315. //开始计算
  316. $isPart=false;//订单用户如果是合伙人是否计算
  317. $orderCustomerId = $orderData["customerId"];
  318. $orderMoney = $orderData["payAmount"];
  319. //只计算小程序已完成订单
  320. if(empty($orderCustomerId) || empty($orderMoney) || $orderData["payType"]!=1 || $orderData["source"]!=3 || $orderData["orderStatus"]!=5){
  321. return ["code"=>-1,"msg"=>"只计算小程序微信支付已完成订单"];
  322. }
  323. $customerData = $this->dbCustomer->get($orderCustomerId);
  324. if (empty($customerData)) {
  325. return ["code"=>-1,"msg"=>"用户不存在"];
  326. }
  327. if(empty($customerData["parentPath"])){
  328. if($orderMoney>=$this->levMoney && $customerData["isPartner"]==0){
  329. $this->setPartner($orderCustomerId);
  330. }
  331. return ["code"=>-1,"msg"=>"用户不存在父级"];
  332. }
  333. //订单用户如果是合伙人消费则不计算收益
  334. if(!$isPart && $customerData["isPartner"]==1){
  335. return ["code"=>-1,"msg"=>"合伙人消费不计算上级收益"];
  336. }
  337. $data=[];
  338. $nowTime = time();
  339. //计算低层收益
  340. $bottomData = $this->calcMoney($customerData,$orderMoney);
  341. if(empty($bottomData)){
  342. return ["code"=>-1,"msg"=>"上级合伙人不存在"];
  343. }
  344. //合伙人账户
  345. $bottomPartnerData = $this->getPartnerData($bottomData["parentData"]["id"], $bottomData["parentData"]["userCenterId"]);
  346. if(empty($bottomPartnerData)){
  347. return ["code"=>-1,"msg"=>"上级合伙人账户错误"];
  348. }
  349. //记录底层收益
  350. $data[]=[
  351. "orderMoney"=>$orderMoney,//订单支付金额
  352. "calcMoney"=>$bottomData["money"],//佣金计算金额
  353. "sourceCustomerId"=>$bottomData["childId"],//来源客户id
  354. "partnerId"=>$bottomPartnerData["id"],//收钱合伙人账号id
  355. "customerId"=>$bottomPartnerData["customerId"],//收钱合伙人
  356. "userCenterId"=>$bottomPartnerData["userCenterId"],//收钱合伙人
  357. "commission"=>$bottomData["commission"],//佣金金额
  358. "per"=>$bottomData["per"],//佣金比例
  359. "type"=>0,
  360. "orderId"=>$orderData["id"],
  361. "orderNo"=>$orderData["no"],
  362. "status"=>0,//暂时不需要
  363. "title"=>"子级用户消费分佣",
  364. "isUpgrade"=>$bottomData["isUpgrade"],
  365. "mono"=>"",
  366. "time"=>$nowTime,
  367. ];
  368. //计算顶层收益
  369. $topData = $this->calcMoney($bottomData["parentData"],$bottomData["commission"]);
  370. if(!empty($topData)){
  371. $topPartnerData = $this->getPartnerData($topData["parentData"]["id"], $topData["parentData"]["userCenterId"]);
  372. if(empty($topPartnerData)){
  373. return ["code"=>-1,"msg"=>"顶级合伙人账户错误"];
  374. }
  375. //记录顶层收益
  376. $data[]=[
  377. "orderMoney"=>$orderMoney,//订单支付金额
  378. "calcMoney"=>$topData["money"],//佣金计算金额
  379. "sourceCustomerId"=>$topData["childId"],//来源客户id
  380. "partnerId"=>$topPartnerData["id"],//收钱合伙人账号id
  381. "customerId"=>$topPartnerData["customerId"],//收钱合伙人
  382. "userCenterId"=>$topPartnerData["userCenterId"],//收钱合伙人
  383. "commission"=>$topData["commission"],//佣金金额
  384. "per"=>$topData["per"],//佣金比例
  385. "type"=>1,//1表示子级合伙人收益计算收益,0表示子级消费计算收益
  386. "orderId"=>$orderData["id"],
  387. "orderNo"=>$orderData["no"],
  388. "status"=>0,
  389. "title"=>"子级合伙人收益分佣",
  390. "isUpgrade"=>$topData["isUpgrade"],
  391. "mono"=>"",
  392. "time"=>$nowTime,
  393. ];
  394. }
  395. $num=0;
  396. //开启事务
  397. $this->dbNewCommissionDetail->beginTransaction();
  398. $lms = "";
  399. foreach($data as $k=>$v){
  400. $count = $this->dbNewCommissionDetail->count(["orderId"=>$v["orderId"],"partnerId"=>$v["partnerId"]]);
  401. if($count>0){
  402. $lms.="当前订单已计算佣金[{$v['orderId']}];";
  403. continue;
  404. }
  405. $nid = $this->dbNewCommissionDetail->insert($v);
  406. if(empty($nid)){
  407. $lms.="佣金明细插入失败[{$v['orderId']}];";
  408. continue;
  409. }
  410. //查询合伙人账户
  411. $partnerItem = $this->dbNewCommissionPartner->get(["id"=>$v["partnerId"]]);
  412. if(empty($partnerItem)){
  413. $num = 0;
  414. $lms.="合伙人账户不存在[{$v['orderId']}];";
  415. break;
  416. }
  417. $update = [
  418. 'balance' => bcadd($partnerItem['balance'], $v["commission"], 2),
  419. 'totalMoney' => bcadd($partnerItem['totalMoney'], $v["commission"], 2),
  420. 'updateTime' => $nowTime,
  421. ];
  422. //添加账户余额明细记录
  423. $dres = $this->dbNewCommissionBalanceDetail->insert([
  424. "type"=>1,
  425. "title"=>$v["title"],
  426. "code"=>$v["type"]==1 ? "partner_calc" : "order_calc",
  427. "money"=>$v["commission"],
  428. "content"=>$v["title"],
  429. "admin_id"=>0,
  430. "customer_id"=>$v["customerId"],
  431. "partner_id"=>$v["partnerId"],
  432. "user_center_id"=>$v["userCenterId"],
  433. "cash_id"=>0,
  434. "old_balance"=>$partnerItem["balance"],
  435. "now_balance"=>$update["balance"],
  436. "time"=>$nowTime,
  437. "expand"=>"",
  438. "detail_id"=>$nid,
  439. ]);
  440. if(empty($dres)){
  441. $num = 0;
  442. $lms.="账户余额明细插入失败[{$v['orderId']}];";
  443. break;
  444. }
  445. //更新账户余额
  446. $upRes = $this->dbNewCommissionPartner->update($update, ['id' => $v["partnerId"]]);
  447. if(empty($upRes)){
  448. $num = 0;
  449. $lms.="更新账户余额失败[{$v['orderId']}];";
  450. break;
  451. }
  452. $num++;
  453. }
  454. if($num<=0){
  455. $this->dbNewCommissionDetail->rollBack();
  456. return ["code"=>-1,"msg"=>"数据更新失败:".$lms];
  457. }
  458. $this->dbNewCommissionDetail->commit();
  459. //佣金更新完后验证设置合伙人
  460. if($bottomData["isUpgrade"]==1){
  461. $this->setPartner($bottomData["childId"]);
  462. }
  463. return ["code"=>1,"msg"=>"执行成功".$lms];
  464. }
  465. public function getPartnerData($customerId,$userCenterId){
  466. if(empty($customerId)){
  467. return false;
  468. }
  469. $partnerData = $this->dbNewCommissionPartner->get(["customerId"=>$customerId]);
  470. if(empty($partnerData)){
  471. $nowTime = time();
  472. $partnerData=[
  473. "userCenterId"=>$userCenterId,
  474. "customerId"=>$customerId,
  475. "balance"=>0,
  476. "waitMoney"=>0,
  477. "withdraw"=>0,
  478. "totalMoney"=>0,
  479. "deleteStatus"=>5,
  480. "createTime"=>$nowTime,
  481. "updateTime"=>$nowTime,
  482. "expand"=>"",
  483. ];
  484. $partnerId = $this->dbNewCommissionPartner->insert($partnerData);
  485. if(empty($partnerId)){
  486. return false;
  487. }
  488. $partnerData["id"] = $partnerId;
  489. $partnerData["orderNum"] = 0;
  490. }else{
  491. $partnerData["orderNum"] = $this->getCommissionDetailCount($partnerData["id"]);
  492. }
  493. return $partnerData;
  494. }
  495. public function getCommissionDetailCount($partnerId,$type=0){
  496. if(empty($partnerId) && $type=0){
  497. return 0;
  498. }
  499. $where=[];
  500. if(!empty($partnerId)){
  501. $where["partnerId"] = $partnerId;
  502. }
  503. $count = $this->dbNewCommissionDetail->count($where);
  504. return $count?$count:0;
  505. }
  506. /**
  507. * 获取分佣明细
  508. */
  509. public function getCommissionDetail($customerId,$parms){
  510. $where=[];
  511. if(!empty($parms["where"])){
  512. $where= $parms["where"];
  513. }
  514. if(!empty($customerId)){
  515. $where["customerId"] = $customerId;
  516. }
  517. $data = $this->dbNewCommissionDetail->select($where, "*", "id desc", $parms["limit"], $parms["offset"]);
  518. $total = $this->dbNewCommissionDetail->count($where);
  519. if(empty($data)){
  520. $data = [];
  521. }
  522. foreach($data as $k=>$v){
  523. $itemUser = $this->dbCustomer->get(["id"=>$v["sourceCustomerId"]],"id,name,avatar");
  524. if(empty($itemUser)){
  525. $data[$k]["name"] = "";
  526. $data[$k]["avatar"] = "";
  527. }else{
  528. $data[$k]["name"] = $itemUser["name"];
  529. $data[$k]["avatar"] = $itemUser["avatar"];
  530. }
  531. }
  532. $return = [
  533. 'data' => $data,
  534. 'total' => (isset($total)&&!empty($total)) ? intval($total) : 0,
  535. ];
  536. return $return;
  537. }
  538. /**
  539. * 获取合伙人账户余额明细
  540. */
  541. public function getCommissionBalanceDetail($customerId,$parms){
  542. $where=[];
  543. if(!empty($customerId)){
  544. $where["customer_id"] = $customerId;
  545. }
  546. $data = $this->dbNewCommissionBalanceDetail->select($where, "*", "id desc", $parms["limit"], $parms["offset"]);
  547. $total = $this->dbNewCommissionBalanceDetail->count($where);
  548. if(empty($data)){
  549. $data = [];
  550. }
  551. $return = [
  552. 'data' => $data,
  553. 'total' => (isset($total)&&!empty($total)) ? intval($total) : 0,
  554. ];
  555. return $return;
  556. }
  557. /**
  558. *
  559. * @param type $type 0返回标题数组,1范围完整数组
  560. * @return type
  561. */
  562. public static function getBankType($type=0){
  563. $data = [
  564. ["title"=>"支付宝","code"=>"alipay","icon"=>"ibonumidd17","addefault"=>"支付宝"],
  565. ["title"=>"微信钱包","code"=>"wxpay","icon"=>"ibonweixinzhifu","addefault"=>"微信"],
  566. ["title"=>"银行卡","code"=>"bank","icon"=>"ibonyinhangqia","addefault"=>""],
  567. ];
  568. if($type==1){
  569. return $data;
  570. }
  571. $nameAr= [];
  572. foreach($data as $k=>$v){
  573. $nameAr[]=$v["title"];
  574. }
  575. return $nameAr;
  576. }
  577. /**
  578. * 提现申请
  579. */
  580. public function applyCash($data){
  581. 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"])){
  582. return ["code"=>-1,"msg"=>"请填写完整信息"];
  583. }
  584. //频繁操作限制
  585. $key = "partnerCashCache_".$data["customerId"].'::'.$this->enterpriseId;
  586. $result = $this->cache->get($key);
  587. if(!empty($result)){
  588. return ["code"=>-1,"msg"=>"操作频繁,请稍后尝试!"];
  589. }
  590. $count = $this->dbNewCommissionCash->count(["status"=>0,"customer_id"=>$data["customerId"]]);
  591. if($count>0){
  592. return ["code"=>-1,"msg"=>"上次申请还在处理中"];
  593. }
  594. $this->cache->set($key,1,5);
  595. if(!in_array($data["bank_type"], self::getBankType())){
  596. return ["code"=>-1,"msg"=>"提现方式不存在"];
  597. }
  598. if(!is_numeric($data["money"])){
  599. return ["code"=>-1,"msg"=>"提现金额错误"];
  600. }
  601. $data["money"] = bcadd($data["money"],0,2);
  602. if($data["money"]<=0){
  603. return ["code"=>-1,"msg"=>"提现金额错误"];
  604. }
  605. $partnerData = $this->dbNewCommissionPartner->get(["customerId"=>$data["customerId"]]);
  606. if(empty($partnerData) || empty($partnerData["id"])){
  607. return ["code"=>-1,"msg"=>"合伙人不存在"];
  608. }
  609. if($data["money"]>$partnerData["balance"] || $partnerData["balance"]<=0){
  610. return ["code"=>-1,"msg"=>"提现金额不足"];
  611. }
  612. $nowTime = time();
  613. $save=[
  614. "customer_id"=>$partnerData["customerId"],
  615. "user_center_id"=>$partnerData["userCenterId"],
  616. "partner_id" => $partnerData["id"],
  617. "bank_type" => $data["bank_type"],
  618. "bank_name" => $data["bank_name"],
  619. "bank_num" => $data["bank_num"],
  620. "bank_ad" => $data["bank_ad"],
  621. "money" => $data["money"],
  622. "status" => 0,
  623. "time" => $nowTime
  624. ];
  625. try{
  626. $this->dbNewCommissionPartner->beginTransaction();
  627. //插入提现申请
  628. $applyId = $this->dbNewCommissionCash->insert($save);
  629. if(empty($applyId)){
  630. return ["code"=>-1,"msg"=>"数据提交失败001"];
  631. }
  632. //更新提现账户余额并添加明细
  633. $update = [
  634. 'balance' => bcsub($partnerData["balance"], $save["money"], 2),//账户余额
  635. 'withdraw' => bcadd($partnerData["withdraw"], $save["money"],2),//已提现金额
  636. 'updateTime' => $nowTime,
  637. ];
  638. if($update["balance"]<=0){
  639. $this->dbNewCommissionPartner->rollBack();
  640. return ["code"=>-1,"msg"=>"数据提交失败002"];
  641. }
  642. //添加账户余额明细记录
  643. $dres = $this->dbNewCommissionBalanceDetail->insert([
  644. "type"=>-1,
  645. "title"=>"余额提现",
  646. "code"=>"cash_balance",
  647. "money"=>$save["money"],
  648. "content"=>"合伙人余额提现",
  649. "admin_id"=>0,
  650. "customer_id"=>$partnerData["customerId"],
  651. "partner_id"=>$partnerData["id"],
  652. "user_center_id"=>$partnerData["userCenterId"],
  653. "cash_id"=>$applyId,
  654. "old_balance"=>$partnerData["balance"],
  655. "now_balance"=>$update["balance"],
  656. "time"=>$nowTime,
  657. "expand"=>"",
  658. "detail_id"=>0,
  659. ]);
  660. if(empty($dres)){
  661. $this->dbNewCommissionPartner->rollBack();
  662. return ["code"=>-1,"msg"=>"账户余额明细插入失败"];
  663. }
  664. //更新账户余额
  665. $upRes = $this->dbNewCommissionPartner->update($update, ['id' => $partnerData["id"]]);
  666. if(empty($upRes)){
  667. $this->dbNewCommissionPartner->rollBack();
  668. return ["code"=>-1,"msg"=>"更新账户余额失败"];
  669. }
  670. $this->dbNewCommissionPartner->commit();
  671. $this->cache->set($key,null);
  672. return ["code"=>1,"msg"=>"提现申请提交成功"];
  673. } catch (\Exception $e){
  674. $this->dbNewCommissionPartner->rollBack();
  675. $this->cache->set($key,null);
  676. return ["code"=>1,"msg"=>"提现申请提交失败"];
  677. }
  678. }
  679. /**
  680. * 获取提现记录
  681. * @param type $customerId
  682. * @param type $parms
  683. * @return type
  684. */
  685. public function getPartnerApplyCashList($customerId,$parms){
  686. $where=[];
  687. if(!empty($parms["where"])){
  688. $where= $parms["where"];
  689. }
  690. if(!empty($customerId)){
  691. $where["customer_id"] = $customerId;
  692. }
  693. $data = $this->dbNewCommissionCash->select($where, "*", "id desc", $parms["limit"], $parms["offset"]);
  694. $total = $this->dbNewCommissionCash->count($where);
  695. if(empty($data)){
  696. $data = [];
  697. }
  698. $return = [
  699. 'data' => $data,
  700. 'total' => (isset($total)&&!empty($total)) ? intval($total) : 0,
  701. ];
  702. return $return;
  703. }
  704. /**
  705. * 获取提现详情
  706. */
  707. public function getPartnerApplyCashInfo($customerId,$parms){
  708. $where=[];
  709. if(!empty($parms["where"])){
  710. $where= $parms["where"];
  711. }
  712. if(!empty($customerId)){
  713. $where["customer_id"] = $customerId;
  714. }
  715. $data = $this->dbNewCommissionCash->get($where);
  716. return $data;
  717. }
  718. /**
  719. * 获取合伙人列表
  720. * @param type $params
  721. */
  722. public function getPartnerList($params = []){
  723. $customerTableName = 'qianniao_customer_'.$this->enterpriseId;
  724. $userCenterTableName = 'qianniao_user_center';
  725. $newCommissionPartnerTableName = 'qianniao_new_commission_partner_'.$this->enterpriseId;
  726. //查询数据
  727. $field = "p.*,u.mobile,c.name,c.avatar";
  728. $sql = "select {$field} from {$newCommissionPartnerTableName} p "
  729. . "left join {$userCenterTableName} u on u.id = p.userCenterId "
  730. . "left join {$customerTableName} c on c.id = p.customerId";
  731. $whereSql = 'p.deleteStatus = 5';
  732. if(isset($params['search'])){
  733. $whereSql .= ' and (u.mobile like "%'.$params['search'].'%" or c.name like "%'.$params['search'].'%") ';
  734. }
  735. $orderSql = ' order by p.id desc ';
  736. $limitSql = ' limit '.$params['offset'].','.$params['limit'];
  737. $querySql = $sql.$whereSql.$orderSql.$limitSql;
  738. $data = $this->dbNewCommissionPartner->query($querySql);
  739. if(empty($data)){
  740. $data=[];
  741. }
  742. //查询总数
  743. $countSql = "select count(*) as `count` from {$newCommissionPartnerTableName} p "
  744. . "left join {$userCenterTableName} u on u.id = p.userCenterId "
  745. . "left join {$customerTableName} c on c.id = p.customerId";
  746. $countData = $this->dbNewCommissionPartner->query($countSql.$whereSql);
  747. $total = 0;
  748. if(!empty($countData)){
  749. $total =array_shift($countData)['count'];
  750. }
  751. return ['data'=>$data,'total'=>($total) ? intval($total) : 0];
  752. }
  753. /**
  754. * 删除用户缓存
  755. * @param type $customerId
  756. * @param type $userCenterId
  757. */
  758. public function delCustomerCache($customerId,$userCenterId){
  759. $objCustomerCache = new CustomerCache();
  760. $objCustomerCache->delCustomerData($this->enterpriseId, $customerId);
  761. $objCustomerCache->delCustomerUserData($this->enterpriseId, $userCenterId);
  762. }
  763. }