AgentTools.Class.php 41 KB

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