PartnerTools.Class.php 53 KB

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