AgentTools.Class.php 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  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 $customerId
  216. * @param type $userCenterId
  217. * @return bool
  218. */
  219. public function getAndSetAgentData($customerId,$userCenterId=0){
  220. if(empty($customerId)){
  221. return false;
  222. }
  223. if($userCenterId==-1){
  224. $customerData = $this->dbCustomer->get($customerId);
  225. if(!empty($customerData)){
  226. $userCenterId = $customerData["userCenterId"];
  227. }else{
  228. $userCenterId = 0;
  229. }
  230. }
  231. $agentData = $this->dbNewAgent->get(["customerId"=>$customerId]);
  232. if(empty($userCenterId) && empty($agentData)){
  233. return false;
  234. }
  235. if(!empty($userCenterId) && empty($agentData)){
  236. $nowTime = time();
  237. $agentData=[
  238. "userCenterId"=>$userCenterId,
  239. "customerId"=>$customerId,
  240. "balance"=>0,
  241. "waitMoney"=>0,
  242. "withdraw"=>0,
  243. "totalMoney"=>0,
  244. "deleteStatus"=>5,
  245. "createTime"=>$nowTime,
  246. "updateTime"=>$nowTime,
  247. "expand"=>"",
  248. ];
  249. $agentId = $this->dbNewAgent->insert($agentData);
  250. if(empty($agentId)){
  251. return false;
  252. }
  253. $agentData["id"] = $agentId;
  254. $agentData["purchaseNum"] = 0;
  255. }else{
  256. $agentData["purchaseNum"] = $this->getCommissionDetailCount($agentData["id"]);
  257. }
  258. return $agentData;
  259. }
  260. public function getCommissionDetailCount($agentId){
  261. if(empty($agentId) && $type=0){
  262. return 0;
  263. }
  264. $where=[];
  265. if(!empty($agentId)){
  266. $where["agentId"] = $agentId;
  267. }
  268. $count = $this->dbNewAgentDetail->count($where);
  269. return $count?$count:0;
  270. }
  271. /**
  272. * 采购单消费计算佣金
  273. * @param type $purchaseId
  274. * @param type $orderMoney
  275. * @param type $isPart 采购单用户如果是门店代理是否计算
  276. * @return bool
  277. */
  278. public function runCalcMoneyData($purchaseId){
  279. if(empty($this->agentSettingData) || empty($this->agentSettingData["is_open"])){
  280. return ["code"=>-1,"msg"=>"配置信息错误"];
  281. }
  282. //获取采购单信息
  283. if(empty($purchaseId)){
  284. return ["code"=>-1,"msg"=>"采购单id错误"];
  285. }
  286. $pwhere=[];
  287. $pwhere["id"]=$purchaseId;
  288. $pwhere["auditStatus"]=2;//审核状态
  289. $pwhere["deleteStatus"]=5;//删除状态
  290. $pwhere["inStatus"]=5;//入库状态
  291. $pwhere["purchaseType"]=4;//采购订单
  292. $purchaseData = $this->dbPurchase->get($pwhere);
  293. if(empty($purchaseData) || empty($purchaseData["shopId"])){
  294. return ["code"=>-1,"msg"=>"采购单数据不存在或不是门店采购单".$purchaseData["shopId"]];
  295. }
  296. $purchaseShopId = $purchaseData["shopId"];
  297. $shopData = $this->dbShop->get(["id"=>$purchaseShopId,"enterpriseId"=>$this->enterpriseId,"deleteStatus"=>5]);
  298. if(empty($shopData) || empty($shopData["agentId"])){
  299. return ["code"=>-1,"msg"=>"门店被删除或者没有推荐代理"];
  300. }
  301. //开始计算
  302. $purchaseAmount = $purchaseData["purchaseAmount"];//采购金额
  303. $couponAmount = $purchaseData["couponAmount"];//优惠金额
  304. $otherAmount = $purchaseData["otherAmount"];//其它金额
  305. // $purchaseMoney = $purchaseAmount;
  306. $purchaseMoney = $purchaseAmount + $otherAmount - $couponAmount;
  307. if($purchaseMoney<=0){
  308. return ["code"=>-1,"msg"=>"采购实际金额小于等于0"];
  309. }
  310. $agentData = $this->dbNewAgent->get(["id"=>$shopData["agentId"]]);
  311. if(empty($agentData)){
  312. return ["code"=>-1,"msg"=>"推荐代理账号不存在"];
  313. }
  314. $data=[];
  315. $nowTime = time();
  316. //计算收益
  317. $per = $this->agentSettingData["incomePer"];
  318. $commission = bcmul($purchaseMoney,$per,2);
  319. //记录收益
  320. $data[]=[
  321. "purchaseMoney"=>$purchaseMoney,//采购单支付金额
  322. "calcMoney"=>$purchaseMoney,//佣金计算金额
  323. "sourceShopId"=>$purchaseShopId,//来源门店id
  324. "agentId"=>$agentData["id"],//收钱门店代理账号id
  325. "customerId"=>$agentData["customerId"],//收钱门店代理
  326. "userCenterId"=>$agentData["userCenterId"],//收钱门店代理
  327. "commission"=>$commission,//佣金金额
  328. "per"=>$per,//佣金比例
  329. "type"=>0,
  330. "purchaseId"=>$purchaseData["id"],
  331. "purchaseNo"=>$purchaseData["no"],
  332. "status"=>0,//暂时不需要
  333. "title"=>"门店采购单分佣",
  334. "isUpgrade"=>0,
  335. "mono"=>"",
  336. "time"=>$nowTime,
  337. ];
  338. $num=0;
  339. //开启事务
  340. $this->dbNewAgentDetail->beginTransaction();
  341. $lms = "";
  342. foreach($data as $k=>$v){
  343. $count = $this->dbNewAgentDetail->count(["purchaseId"=>$v["purchaseId"],"agentId"=>$v["agentId"]]);
  344. if($count>0){
  345. $lms.="当前采购单已计算佣金[{$v['purchaseId']}];";
  346. continue;
  347. }
  348. $nid = $this->dbNewAgentDetail->insert($v);
  349. if(empty($nid)){
  350. $lms.="佣金明细插入失败[{$v['purchaseId']}];";
  351. continue;
  352. }
  353. //查询门店代理账户
  354. $agentItem = $this->dbNewAgent->get(["id"=>$v["agentId"]]);
  355. if(empty($agentItem)){
  356. $num = 0;
  357. $lms.="门店代理账户不存在[{$v['purchaseId']}];";
  358. break;
  359. }
  360. $update = [
  361. 'balance' => bcadd($agentItem['balance'], $v["commission"], 2),
  362. 'totalMoney' => bcadd($agentItem['totalMoney'], $v["commission"], 2),
  363. 'updateTime' => $nowTime,
  364. ];
  365. //添加账户余额明细记录
  366. $dres = $this->dbNewAgentBalanceDetail->insert([
  367. "type"=>1,
  368. "title"=>$v["title"],
  369. "code"=>$v["type"]==1 ? "agent_calc" : "purchase_calc",
  370. "money"=>$v["commission"],
  371. "content"=>$v["title"],
  372. "admin_id"=>0,
  373. "customer_id"=>$v["customerId"],
  374. "agent_id"=>$v["agentId"],
  375. "user_center_id"=>$v["userCenterId"],
  376. "cash_id"=>0,
  377. "old_balance"=>$agentItem["balance"],
  378. "now_balance"=>$update["balance"],
  379. "time"=>$nowTime,
  380. "expand"=>"",
  381. "detail_id"=>$nid,
  382. ]);
  383. if(empty($dres)){
  384. $num = 0;
  385. $lms.="账户余额明细插入失败[{$v['purchaseId']}];";
  386. break;
  387. }
  388. //更新账户余额
  389. $upRes = $this->dbNewAgent->update($update, ['id' => $v["agentId"]]);
  390. if(empty($upRes)){
  391. $num = 0;
  392. $lms.="更新账户余额失败[{$v['purchaseId']}];";
  393. break;
  394. }
  395. $num++;
  396. }
  397. if($num<=0){
  398. $this->dbNewAgentDetail->rollBack();
  399. return ["code"=>-1,"msg"=>"数据更新失败:".$lms];
  400. }
  401. $this->dbNewAgentDetail->commit();
  402. return ["code"=>1,"msg"=>"执行成功".$lms];
  403. }
  404. /**
  405. * 获取分佣明细
  406. */
  407. public function getCommissionDetail($customerId,$parms){
  408. $where=[];
  409. if(!empty($parms["where"])){
  410. $where= $parms["where"];
  411. }
  412. if(!empty($customerId)){
  413. $where["customerId"] = $customerId;
  414. }
  415. $data = $this->dbNewAgentDetail->select($where, "*", "id desc", $parms["limit"], $parms["offset"]);
  416. $total = $this->dbNewAgentDetail->count($where);
  417. if(empty($data)){
  418. $data = [];
  419. }
  420. foreach($data as $k=>$v){
  421. $itemShop = $this->dbCustomer->get(["id"=>$v["sourceShopId"]],"id,name,logo");
  422. if(empty($itemShop)){
  423. $data[$k]["name"] = "";
  424. $data[$k]["logo"] = "";
  425. }else{
  426. $data[$k]["name"] = $itemShop["name"];
  427. $data[$k]["logo"] = $itemShop["logo"];
  428. }
  429. }
  430. $return = [
  431. 'data' => $data,
  432. 'total' => (isset($total)&&!empty($total)) ? intval($total) : 0,
  433. ];
  434. return $return;
  435. }
  436. /**
  437. * 获取门店代理账户余额明细
  438. */
  439. public function getCommissionBalanceDetail($customerId,$parms){
  440. $where=[];
  441. if(!empty($customerId)){
  442. $where["customer_id"] = $customerId;
  443. }
  444. $data = $this->dbNewAgentBalanceDetail->select($where, "*", "id desc", $parms["limit"], $parms["offset"]);
  445. $total = $this->dbNewAgentBalanceDetail->count($where);
  446. if(empty($data)){
  447. $data = [];
  448. }
  449. $return = [
  450. 'data' => $data,
  451. 'total' => (isset($total)&&!empty($total)) ? intval($total) : 0,
  452. ];
  453. return $return;
  454. }
  455. /**
  456. *
  457. * @param type $type 0返回标题数组,1范围完整数组
  458. * @return type
  459. */
  460. public static function getBankType($type=0,$title=""){
  461. $data = [
  462. ["title"=>"支付宝","code"=>"alipay","icon"=>"ibonumidd17","addefault"=>"支付宝"],
  463. ["title"=>"微信钱包","code"=>"wxpay","icon"=>"ibonweixinzhifu","addefault"=>"微信"],
  464. ["title"=>"银行卡","code"=>"bank","icon"=>"ibonyinhangqia","addefault"=>""],
  465. ];
  466. if(!empty($title)){
  467. foreach($data as $k=>$v){
  468. if($v["title"]==$title){
  469. return $v;
  470. }
  471. }
  472. return [];
  473. }
  474. if($type==1){
  475. return $data;
  476. }
  477. $nameAr= [];
  478. foreach($data as $k=>$v){
  479. $nameAr[]=$v["title"];
  480. }
  481. return $nameAr;
  482. }
  483. /**
  484. * 提现申请
  485. */
  486. public function applyCash($data){
  487. 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"])){
  488. return ["code"=>-1,"msg"=>"请填写完整信息"];
  489. }
  490. //频繁操作限制
  491. $key = "agentCashCache_".$data["customerId"].'::'.$this->enterpriseId;
  492. $result = $this->cache->get($key);
  493. if(!empty($result)){
  494. return ["code"=>-1,"msg"=>"操作频繁,请稍后尝试!"];
  495. }
  496. $count = $this->dbNewAgentCash->count(["status"=>0,"customer_id"=>$data["customerId"]]);
  497. if($count>0){
  498. return ["code"=>-1,"msg"=>"上次申请还在处理中"];
  499. }
  500. $this->cache->set($key,1,5);
  501. if(!in_array($data["bank_type"], self::getBankType())){
  502. return ["code"=>-1,"msg"=>"提现方式不存在"];
  503. }
  504. $bankTypeData = self::getBankType(1, $data["bank_type"]);
  505. if(empty($bankTypeData)){
  506. return ["code"=>-1,"msg"=>"提现方式不存在"];
  507. }
  508. if(!is_numeric($data["money"])){
  509. return ["code"=>-1,"msg"=>"提现金额错误"];
  510. }
  511. $data["money"] = bcadd($data["money"],0,2);
  512. if($data["money"]<=0){
  513. return ["code"=>-1,"msg"=>"提现金额错误"];
  514. }
  515. $agentData = $this->dbNewAgent->get(["customerId"=>$data["customerId"]]);
  516. if(empty($agentData) || empty($agentData["id"])){
  517. return ["code"=>-1,"msg"=>"门店代理不存在"];
  518. }
  519. if($data["money"]>$agentData["balance"] || $agentData["balance"]<=0){
  520. return ["code"=>-1,"msg"=>"提现金额不足"];
  521. }
  522. $nowTime = time();
  523. $save=[
  524. "customer_id" => $agentData["customerId"],
  525. "user_center_id" => $agentData["userCenterId"],
  526. "agent_id" => $agentData["id"],
  527. "bank_type" => $data["bank_type"],
  528. "bank_type_code" => $bankTypeData["code"],
  529. "bank_name" => $data["bank_name"],
  530. "bank_num" => $data["bank_num"],
  531. "bank_ad" => $data["bank_ad"],
  532. "money" => $data["money"],
  533. "status" => 0,
  534. "time" => $nowTime
  535. ];
  536. try{
  537. $this->dbNewAgent->beginTransaction();
  538. //插入提现申请
  539. $applyId = $this->dbNewAgentCash->insert($save);
  540. if(empty($applyId)){
  541. return ["code"=>-1,"msg"=>"数据提交失败001"];
  542. }
  543. //更新提现账户余额并添加明细
  544. $update = [
  545. 'balance' => bcsub($agentData["balance"], $save["money"], 2),//账户余额
  546. 'withdraw' => bcadd($agentData["withdraw"], $save["money"],2),//已提现金额
  547. 'updateTime' => $nowTime,
  548. ];
  549. if($update["balance"]<=0){
  550. $this->dbNewAgent->rollBack();
  551. return ["code"=>-1,"msg"=>"数据提交失败002"];
  552. }
  553. //添加账户余额明细记录
  554. $dres = $this->dbNewAgentBalanceDetail->insert([
  555. "type"=>-1,
  556. "title"=>"余额提现",
  557. "code"=>"cash_balance",
  558. "money"=>$save["money"],
  559. "content"=>"门店代理余额提现",
  560. "admin_id"=>0,
  561. "customer_id"=>$agentData["customerId"],
  562. "agent_id"=>$agentData["id"],
  563. "user_center_id"=>$agentData["userCenterId"],
  564. "cash_id"=>$applyId,
  565. "old_balance"=>$agentData["balance"],
  566. "now_balance"=>$update["balance"],
  567. "time"=>$nowTime,
  568. "expand"=>"",
  569. "detail_id"=>0,
  570. ]);
  571. if(empty($dres)){
  572. $this->dbNewAgent->rollBack();
  573. return ["code"=>-1,"msg"=>"账户余额明细插入失败"];
  574. }
  575. //更新账户余额
  576. $upRes = $this->dbNewAgent->update($update, ['id' => $agentData["id"]]);
  577. if(empty($upRes)){
  578. $this->dbNewAgent->rollBack();
  579. return ["code"=>-1,"msg"=>"更新账户余额失败"];
  580. }
  581. $this->dbNewAgent->commit();
  582. $this->cache->set($key,null);
  583. return ["code"=>1,"msg"=>"提现申请提交成功"];
  584. } catch (\Exception $e){
  585. $this->dbNewAgent->rollBack();
  586. $this->cache->set($key,null);
  587. return ["code"=>1,"msg"=>"提现申请提交失败"];
  588. }
  589. }
  590. /**
  591. * 获取提现记录
  592. * @param type $customerId
  593. * @param type $parms
  594. * @return type
  595. */
  596. public function getPartnerApplyCashList($customerId,$parms){
  597. $where=[];
  598. if(!empty($parms["where"])){
  599. $where= $parms["where"];
  600. }
  601. if(!empty($customerId)){
  602. $where["customer_id"] = $customerId;
  603. }
  604. $data = $this->dbNewAgentCash->select($where, "*", "id desc", $parms["limit"], $parms["offset"]);
  605. $total = $this->dbNewAgentCash->count($where);
  606. if(empty($data)){
  607. $data = [];
  608. }
  609. $return = [
  610. 'data' => $data,
  611. 'total' => (isset($total)&&!empty($total)) ? intval($total) : 0,
  612. ];
  613. return $return;
  614. }
  615. /**
  616. * 获取提现详情
  617. */
  618. public function getPartnerApplyCashInfo($customerId,$parms){
  619. $where=[];
  620. if(!empty($parms["where"])){
  621. $where= $parms["where"];
  622. }
  623. if(!empty($customerId)){
  624. $where["customer_id"] = $customerId;
  625. }
  626. $data = $this->dbNewAgentCash->get($where);
  627. return $data;
  628. }
  629. /**
  630. * 获取门店代理列表
  631. * @param type $params
  632. */
  633. public function getPartnerList($params = []){
  634. $customerTableName = 'qianniao_customer_'.$this->enterpriseId;
  635. $userCenterTableName = 'qianniao_user_center';
  636. $newCommissionPartnerTableName = 'qianniao_new_agent_'.$this->enterpriseId;
  637. //查询数据
  638. $field = "p.*,u.mobile,c.name,c.avatar";
  639. $sql = "select {$field} from {$newCommissionPartnerTableName} p "
  640. . "left join {$userCenterTableName} u on u.id = p.userCenterId "
  641. . "left join {$customerTableName} c on c.id = p.customerId";
  642. $whereSql = ' where p.deleteStatus = 5 ';
  643. if(isset($params['search'])){
  644. $whereSql .= ' and (u.mobile like "%'.$params['search'].'%" or c.name like "%'.$params['search'].'%") ';
  645. }
  646. $orderSql = ' order by p.id desc ';
  647. $limitSql = ' limit '.$params['offset'].','.$params['limit'];
  648. $querySql = $sql.$whereSql.$orderSql.$limitSql;
  649. $data = $this->dbNewAgent->query($querySql);
  650. if(empty($data)){
  651. $data=[];
  652. }
  653. //查询总数
  654. $countSql = "select count(*) as `count` from {$newCommissionPartnerTableName} p "
  655. . "left join {$userCenterTableName} u on u.id = p.userCenterId "
  656. . "left join {$customerTableName} c on c.id = p.customerId";
  657. $countData = $this->dbNewAgent->query($countSql.$whereSql);
  658. $total = 0;
  659. if(!empty($countData)){
  660. $total =array_shift($countData)['count'];
  661. }
  662. return ['data'=>$data,'total'=>($total) ? intval($total) : 0];
  663. }
  664. /**
  665. * 获取门店代理余额明细列表
  666. * @param type $params
  667. */
  668. public function getBalanceDetailList($params = []){
  669. $customerTableName = 'qianniao_customer_'.$this->enterpriseId;
  670. $userCenterTableName = 'qianniao_user_center';
  671. $newCommissionBalanceDetailTableName = 'qianniao_new_agent_balance_detail_'.$this->enterpriseId;
  672. //查询数据
  673. $field = "p.*,u.mobile,c.name,c.avatar";
  674. $sql = "select {$field} from {$newCommissionBalanceDetailTableName} p "
  675. . "left join {$userCenterTableName} u on u.id = p.user_center_id "
  676. . "left join {$customerTableName} c on c.id = p.customer_id";
  677. $whereSql = ' where 1=1 ';
  678. if(!empty($params['agentId'])){
  679. $whereSql .= ' and p.agent_id = '.$params['agentId'];
  680. }
  681. if(isset($params['search'])){
  682. $whereSql .= ' and (u.mobile like "%'.$params['search'].'%" or c.name like "%'.$params['search'].'%") ';
  683. }
  684. $orderSql = ' order by p.id desc ';
  685. $limitSql = ' limit '.$params['offset'].','.$params['limit'];
  686. $querySql = $sql.$whereSql.$orderSql.$limitSql;
  687. $data = $this->dbNewAgent->query($querySql);
  688. if(empty($data)){
  689. $data=[];
  690. }
  691. //查询总数
  692. $countSql = "select count(*) as `count` 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. $countData = $this->dbNewAgent->query($countSql.$whereSql);
  696. $total = 0;
  697. if(!empty($countData)){
  698. $total =array_shift($countData)['count'];
  699. }
  700. return ['data'=>$data,'total'=>($total) ? intval($total) : 0];
  701. }
  702. /**
  703. * 获取门店代理佣金记录
  704. * @param type $params
  705. */
  706. public function getDetailList($params = []){
  707. $customerTableName = 'qianniao_customer_'.$this->enterpriseId;
  708. $userCenterTableName = 'qianniao_user_center';
  709. $newCommissionDetailTableName = 'qianniao_new_agent_detail_'.$this->enterpriseId;
  710. $shopTableName = "qianniao_shop_1";
  711. //查询数据
  712. $field = "p.*,u.mobile,c.name,c.avatar,s.name as sourceName ";
  713. $sql = "select {$field} from {$newCommissionDetailTableName} p "
  714. . "left join {$userCenterTableName} u on u.id = p.userCenterId "
  715. . "left join {$customerTableName} c on c.id = p.customerId "
  716. . "left join {$shopTableName} s on s.id = p.sourceShopId";
  717. $whereSql = ' where 1=1 ';
  718. if(!empty($params['agentId'])){
  719. $whereSql .= ' and p.agentId = '.$params['agentId'];
  720. }
  721. if(isset($params['search'])){
  722. $whereSql .= ' and (u.mobile like "%'.$params['search'].'%" or c.name like "%'.$params['search'].'%") ';
  723. }
  724. $orderSql = ' order by p.id desc ';
  725. $limitSql = ' limit '.$params['offset'].','.$params['limit'];
  726. $querySql = $sql.$whereSql.$orderSql.$limitSql;
  727. $data = $this->dbNewAgent->query($querySql);
  728. if(empty($data)){
  729. $data=[];
  730. }
  731. //查询总数
  732. $countSql = "select count(*) as `count` from {$newCommissionDetailTableName} p "
  733. . "left join {$userCenterTableName} u on u.id = p.userCenterId "
  734. . "left join {$customerTableName} c on c.id = p.customerId "
  735. . "left join {$shopTableName} s on s.id = p.sourceShopId";
  736. $countData = $this->dbNewAgent->query($countSql.$whereSql);
  737. $total = 0;
  738. if(!empty($countData)){
  739. $total =array_shift($countData)['count'];
  740. }
  741. return ['data'=>$data,'total'=>($total) ? intval($total) : 0];
  742. }
  743. /**
  744. * 获取提现记录
  745. * @param type $params
  746. */
  747. public function getPartnerCashList($params = []){
  748. $customerTableName = 'qianniao_customer_'.$this->enterpriseId;
  749. $userCenterTableName = 'qianniao_user_center';
  750. $newCommissionCashTableName = 'qianniao_new_agent_cash_'.$this->enterpriseId;
  751. //查询数据
  752. $field = "p.*,u.mobile,c.name,c.avatar ";
  753. $sql = "select {$field} from {$newCommissionCashTableName} p "
  754. . "left join {$userCenterTableName} u on u.id = p.user_center_id "
  755. . "left join {$customerTableName} c on c.id = p.customer_id ";
  756. $whereSql = ' where 1=1 ';
  757. if(!empty($params['agentId'])){
  758. $whereSql .= ' and p.agent_id = '.$params['agentId'];
  759. }
  760. if(isset($params['search'])){
  761. $whereSql .= ' and (u.mobile like "%'.$params['search'].'%" or c.name like "%'.$params['search'].'%") ';
  762. }
  763. if(isset($params['status'])){
  764. $whereSql .= ' and p.status = '.$params['status'];
  765. }
  766. if(!empty($params['bank_type_code'])){
  767. $whereSql .= " and p.bank_type_code = '{$params['bank_type_code']}'";
  768. }
  769. if(!empty($params['start_time']) && !empty($params['end_time'])){
  770. $whereSql .= " and p.time >= {$params['start_time']} and p.time < {$params['end_time']}";
  771. }
  772. $orderSql = ' order by p.id desc ';
  773. $limitSql = ' limit '.$params['offset'].','.$params['limit'];
  774. $querySql = $sql.$whereSql.$orderSql.$limitSql;
  775. $data = $this->dbNewAgent->query($querySql);
  776. if(empty($data)){
  777. $data=[];
  778. }
  779. //查询总数
  780. $countSql = "select count(*) as `count` from {$newCommissionCashTableName} p "
  781. . "left join {$userCenterTableName} u on u.id = p.user_center_id "
  782. . "left join {$customerTableName} c on c.id = p.customer_id ";
  783. $countData = $this->dbNewAgent->query($countSql.$whereSql);
  784. $total = 0;
  785. if(!empty($countData)){
  786. $total =array_shift($countData)['count'];
  787. }
  788. return ['data'=>$data,'total'=>($total) ? intval($total) : 0];
  789. }
  790. /**
  791. * 提现处理
  792. * @param type $id
  793. * @param type $mono
  794. * @param type $type
  795. */
  796. public function partnerCashAudit($id,$mono,$type,$audit_id=0){
  797. if(empty($id)|| empty($mono) || empty($type) || !in_array($type, [1,-1])){
  798. return ["code"=>-1,"msg"=>"参数错误"];
  799. }
  800. $adminData = $this->dbUserCenter->get($this->onlineUserId);
  801. if(empty($adminData)){
  802. $adminData=["audit_name"=>"管理人员"];
  803. }
  804. //频繁操作限制
  805. $key = "agentCashDealCache_".$id.'::'.$this->enterpriseId;
  806. $result = $this->cache->get($key);
  807. if(!empty($result)){
  808. return ["code"=>-1,"msg"=>"处理中,请耐心等待!"];
  809. }
  810. $this->cache->set($key,1,5);
  811. $where = ["id"=>$id,"status"=>0];
  812. $data = $this->dbNewAgentCash->get($where);
  813. if(empty($data)){
  814. $this->cache->set($key,null);
  815. return ["code"=>-1,"msg"=>"数据不存在"];
  816. }
  817. //门店代理账户信息
  818. $agentData = $this->dbNewAgent->get(["id"=>$data["agent_id"]]);
  819. if(empty($agentData)){
  820. $this->cache->set($key,null);
  821. return ["code"=>-1,"msg"=>"门店代理账户不存在"];
  822. }
  823. //打款成功
  824. if($type==1){
  825. $res = $this->dbNewAgentCash->update([
  826. "status"=>1,
  827. "audit_time"=>time(),
  828. "audit_mono"=>$mono,
  829. "audit_id"=>$audit_id,
  830. "audit_name"=>$adminData["mobile"],
  831. ], $where);
  832. $this->cache->set($key,null);
  833. if(empty($res)){
  834. return ["code"=>-1,"msg"=>"系统繁忙,请稍后重试001!"];
  835. }
  836. return ["code"=>1,"msg"=>"处理完成"];
  837. }
  838. //审核驳回
  839. try{
  840. $this->dbNewAgentCash->beginTransaction();
  841. $res = $this->dbNewAgentCash->update([
  842. "status"=>-1,
  843. "audit_time"=>time(),
  844. "audit_mono"=>$mono,
  845. "audit_id"=>$audit_id,
  846. "audit_name"=>$adminData["mobile"],
  847. ], $where);
  848. if(empty($res)){
  849. $this->dbNewAgentCash->rollBack();
  850. $this->cache->set($key,null);
  851. return ["code"=>-1,"msg"=>"操作失败001!"];
  852. }
  853. //添加账户余额明细记录
  854. $dres = $this->dbNewAgentBalanceDetail->insert([
  855. "type"=>1,
  856. "title"=>"提现驳回返还余额",
  857. "code"=>"cash_no_balance",
  858. "money"=>$data["money"],
  859. "content"=>"提现驳回返还余额:".$mono,
  860. "admin_id"=>$audit_id,
  861. "customer_id"=>$agentData["customerId"],
  862. "agent_id"=>$agentData["id"],
  863. "user_center_id"=>$agentData["userCenterId"],
  864. "cash_id"=>$data["id"],
  865. "old_balance"=>$agentData["balance"],
  866. "now_balance"=>$agentData["balance"]+$data["money"],
  867. "time"=>time(),
  868. "expand"=>"",
  869. "detail_id"=>0,
  870. ]);
  871. if(empty($dres)){
  872. $this->dbNewAgentCash->rollBack();
  873. $this->cache->set($key,null);
  874. return ["code"=>-1,"msg"=>"操作失败002!"];
  875. }
  876. //更新账户数据
  877. $withdrawRes = $this->dbNewAgent->set_dec("withdraw", ["id"=>$agentData["id"]], $data["money"]);
  878. if(empty($withdrawRes)){
  879. $this->dbNewAgentCash->rollBack();
  880. $this->cache->set($key,null);
  881. return ["code"=>-1,"msg"=>"操作失败003"];
  882. }
  883. $balanceRes = $this->dbNewAgent->set_inc("balance", ["id"=>$agentData["id"]], $data["money"]);
  884. if(empty($balanceRes)){
  885. $this->dbNewAgentCash->rollBack();
  886. $this->cache->set($key,null);
  887. return ["code"=>-1,"msg"=>"操作失败004"];
  888. }
  889. //提交事务
  890. $this->dbNewAgentCash->commit();
  891. $this->cache->set($key,null);
  892. return ["code"=>1,"msg"=>"提现申请处理成功!"];
  893. } catch (\Exception $e){
  894. $this->dbNewAgentCash->rollBack();
  895. $this->cache->set($key,null);
  896. return ["code"=>-1,"msg"=>"系统繁忙,请稍后重试!"];
  897. }
  898. }
  899. /**
  900. * 获取设置信息
  901. * @param type $enterpriseId
  902. * @return string
  903. */
  904. public static function getCommissionSettingData($enterpriseId){
  905. $dbSetting = new DNewAgentSetting('default');
  906. $data = $dbSetting->get(["enterpriseId"=>$enterpriseId]);
  907. if(empty($data)){
  908. $data=[
  909. "id"=>0,
  910. "enterpriseId"=>$enterpriseId,
  911. "levMoney"=>0,
  912. "levPer"=>0,
  913. "incomePer"=>0,
  914. "is_open"=>0,
  915. "bankData"=>"",
  916. ];
  917. }
  918. return $data;
  919. }
  920. /**
  921. * 推荐代理分佣设置
  922. * @param type $enterpriseId
  923. * @param type $parms
  924. * @return type
  925. */
  926. public static function setCommissionSettingData($enterpriseId,$parms){
  927. if(empty($enterpriseId) || empty($parms)){
  928. return ["code"=>"-1","msg"=>"参数错误"];
  929. }
  930. $dbSetting = new DNewAgentSetting('default');
  931. $data = $dbSetting->get(["enterpriseId"=>$enterpriseId]);
  932. $parms["levMoney"] = empty($parms["levMoney"]) ? 0 : $parms["levMoney"];
  933. $parms["levPer"] = empty($parms["levPer"]) ? 0 : $parms["levPer"];
  934. $parms["incomePer"] = empty($parms["incomePer"])? 0 : $parms["incomePer"];
  935. $parms["is_open"] = empty($parms["is_open"]) ? 0 : 1;
  936. // if(!is_numeric($parms["levMoney"]) || $parms["levMoney"]<=0){
  937. // return ["code"=>"-1","msg"=>"升级门店代理金额必须大于0"];
  938. // }
  939. // if(!is_numeric($parms["levPer"]) || $parms["levPer"]<0 || $parms["levPer"]>=1){
  940. // return ["code"=>"-1","msg"=>"子级升级门店代理消费佣金比例必须0到1之间"];
  941. // }
  942. if(!is_numeric($parms["incomePer"]) || $parms["incomePer"]<0 || $parms["incomePer"]>=1){
  943. return ["code"=>"-1","msg"=>"门店采购佣金比例必须0到1之间"];
  944. }
  945. $saveData = [
  946. "enterpriseId" => $enterpriseId,
  947. "levMoney" => $parms["levMoney"],
  948. "levPer" => $parms["levPer"],
  949. "incomePer" => $parms["incomePer"],
  950. "is_open" => $parms["is_open"],
  951. "updateTime" => time()
  952. ];
  953. $res = false;
  954. if(empty($data)){
  955. $saveData["enterpriseId"] = $enterpriseId;
  956. $saveData["time"] = time();
  957. $res = $dbSetting->insert($saveData);
  958. }else{
  959. $res = $dbSetting->update($saveData, ["id"=>$data["id"]]);
  960. }
  961. if(empty($res)){
  962. return ["code"=>"-1","msg"=>"系统繁忙,请稍后重试"];
  963. }
  964. return ["code"=>1,"msg"=>"配置成功"];
  965. }
  966. public function getCustomerDataByUserCenterId($userCenterId){
  967. if(empty($userCenterId)){
  968. return false;
  969. }
  970. $data = $this->dbCustomer->get(["userCenterId"=>$userCenterId]);
  971. if(empty($data)){
  972. return false;
  973. }
  974. return $data;
  975. }
  976. }