PartnerTools.Class.php 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  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"=>$ckeckData["type"] == "shopCardOrder"?-3: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){
  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($money>=$this->commissionSettingData["levMoney"] && $customerData["isPartner"]==0){
  420. $this->setPartner($customerData["id"]);
  421. }
  422. return false;
  423. }
  424. $per = $this->commissionSettingData["incomePer"];
  425. $isUpgrade = 0;
  426. if($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. $orderIndexData = $this->dbOrderIndex->get(['id'=>$orderId]);
  461. if(empty($orderIndexData)){
  462. return ["code"=>-1,"msg"=>"订单索引数据不存在"];
  463. }
  464. $orderData=null;
  465. //商品订单
  466. if($taskType=="order"){
  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);
  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"]);
  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"]]);
  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. "is_open"=>0,
  1193. "bankData"=>"",
  1194. ];
  1195. }
  1196. return $data;
  1197. }
  1198. public static function setCommissionSettingData($enterpriseId,$parms){
  1199. if(empty($enterpriseId) || empty($parms)){
  1200. return ["code"=>"-1","msg"=>"参数错误"];
  1201. }
  1202. $dbSetting = new DNewCommissionSetting('default');
  1203. $data = $dbSetting->get(["enterpriseId"=>$enterpriseId]);
  1204. $parms["levMoney"] = empty($parms["levMoney"]) ? 0 : $parms["levMoney"];
  1205. $parms["levPer"] = empty($parms["levPer"]) ? 0 : $parms["levPer"];
  1206. $parms["incomePer"] = empty($parms["incomePer"])? 0 : $parms["incomePer"];
  1207. $parms["is_open"] = empty($parms["is_open"]) ? 0 : 1;
  1208. if(!is_numeric($parms["levMoney"]) || $parms["levMoney"]<=0){
  1209. return ["code"=>"-1","msg"=>"升级合伙人金额必须大于0"];
  1210. }
  1211. if(!is_numeric($parms["levPer"]) || $parms["levPer"]<0 || $parms["levPer"]>=1){
  1212. return ["code"=>"-1","msg"=>"子级升级合伙人消费佣金比例必须0到1之间"];
  1213. }
  1214. if(!is_numeric($parms["incomePer"]) || $parms["incomePer"]<0 || $parms["incomePer"]>=1){
  1215. return ["code"=>"-1","msg"=>"子级消费佣金比例必须0到1之间"];
  1216. }
  1217. $saveData = [
  1218. "enterpriseId" => $enterpriseId,
  1219. "levMoney" => $parms["levMoney"],
  1220. "levPer" => $parms["levPer"],
  1221. "incomePer" => $parms["incomePer"],
  1222. "is_open" => $parms["is_open"],
  1223. "updateTime" => time()
  1224. ];
  1225. $res = false;
  1226. if(empty($data)){
  1227. $saveData["enterpriseId"] = $enterpriseId;
  1228. $saveData["time"] = time();
  1229. $res = $dbSetting->insert($saveData);
  1230. }else{
  1231. $res = $dbSetting->update($saveData, ["id"=>$data["id"]]);
  1232. }
  1233. if(empty($res)){
  1234. return ["code"=>"-1","msg"=>"系统繁忙,请稍后重试"];
  1235. }
  1236. return ["code"=>1,"msg"=>"配置成功"];
  1237. }
  1238. /**
  1239. * 删除用户缓存
  1240. * @param type $customerId
  1241. * @param type $userCenterId
  1242. */
  1243. public function delCustomerCache($customerId,$userCenterId){
  1244. $objCustomerCache = new CustomerCache();
  1245. $objCustomerCache->delCustomerData($this->enterpriseId, $customerId);
  1246. $objCustomerCache->delCustomerUserData($this->enterpriseId, $userCenterId);
  1247. }
  1248. }