PartnerTools.Class.php 47 KB

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