PartnerTools.Class.php 51 KB

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