AgentTools.Class.php 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487
  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. */
  89. public function pushShop($parentShopId=0,$childShopId=0){
  90. if($parentShopId == 0 || $childShopId == 0 || $parentShopId == $childShopId){
  91. return ["code"=>-1,"msg"=>"参数错误","data"=>null];
  92. }
  93. //子级门店
  94. $childShopData = $this->dbShop->get($childShopId);
  95. if(empty($childShopData)){
  96. return ["code"=>-1,"msg"=>"子门店不存在","data"=>null];
  97. }
  98. if($childShopData["level"]>0){
  99. return ["code"=>-1,"msg"=>"当前门店不能被绑定为子级门店","data"=>null];
  100. }
  101. if(!empty($childShopData["pTwoShopId"])){
  102. return ["code"=>-1,"msg"=>"子门店已被其他门店绑定","data"=>null];
  103. }
  104. //父级门店
  105. $parentShopData = $this->dbShop->get($parentShopId);
  106. if(empty($parentShopData)){
  107. return ["code"=>-1,"msg"=>"父门店不存在","data"=>null];
  108. }
  109. $pOneShopId = 0;
  110. $pTwoShopId = $parentShopId;
  111. if(!$parentShopData["pTwoShopId"]){
  112. $pOneShopId = $parentShopData["pTwoShopId"];
  113. }
  114. //更新子级门店数据
  115. $save=[
  116. "level"=>1,//门店级别,1为社区门店,2为代理门店,3为董事门店
  117. "pOneShopId"=>$pOneShopId,//爷爷级门店id
  118. "pTwoShopId"=>$pTwoShopId,//父级门店id
  119. ];
  120. $this->dbShop->update($save,["id"=>$childShopId]);
  121. //如果父级门店没绑定过其他子级则更新为1级社区门店
  122. if($parentShopData["level"]<=0){
  123. $this->dbShop->update(["level"=>1],["id"=>$parentShopId]);
  124. }
  125. //更新父级等级
  126. $this->updateShopLevel($parentShopId);
  127. if($pOneShopId>0){
  128. $this->updateShopLevel($pOneShopId);
  129. }
  130. return ["code"=>1,"msg"=>"绑定成功","data"=>null];
  131. }
  132. /**
  133. * 更新门店等级
  134. * @param type $shopId
  135. */
  136. public function updateShopLevel($shopId){
  137. if(empty($shopId)){
  138. return false;
  139. }
  140. $shopData = $this->dbShop->get($shopId);
  141. if(empty($shopData) || $shopData["level"]==3 || $shopData["level"]==0){
  142. return false;
  143. }
  144. //父级下的社区店数
  145. $level = 1;
  146. $levelCount_1 = $this->dbShop->count([["level","=",1],["pOneShopId|pTwoShopId","=",$shopId]]);
  147. if($levelCount_1 && $levelCount_1>=$this->agentSettingData["oneToTwoNum"]){
  148. $level = 2;//5个升级为代理
  149. }
  150. //父级下的代理店数
  151. $levelCount_2 = $this->dbShop->count([["level","=",2],["pOneShopId|pTwoShopId","=",$shopId]]);
  152. if($levelCount_2 && $levelCount_2>=$this->agentSettingData["twoToThreeNum"]){
  153. $level = 3;//5个代理升级为董事
  154. }
  155. if($level<=$shopData["level"]){
  156. return false;
  157. }
  158. //更新门店等级
  159. $r = $this->dbShop->update(["level"=>$level],["id"=>$shopId]);
  160. if(empty($r)){
  161. return fasle;
  162. }
  163. return true;
  164. }
  165. /**
  166. * 【新】门店推广添加任务
  167. * @param type $purchaseId
  168. * @param type $enterpriseId
  169. * @return bool
  170. */
  171. public static function addTaskNew($purchaseId,$enterpriseId){
  172. try{
  173. if(empty($purchaseId) || empty($enterpriseId)){
  174. return ["code"=>-1,"msg"=>"参数为空"];
  175. }
  176. //获取配置
  177. $dbSetting = new DNewAgentSetting('default');
  178. $settingData = $dbSetting->get(["enterpriseId"=>$enterpriseId]);
  179. if(empty($settingData) || empty($settingData["is_open"]) || $settingData["oneOnePer"]<=0){
  180. return ["code"=>-1,"msg"=>"还未进行分佣配置"];
  181. }
  182. //获取采购单信息
  183. $dbPurchase = new DPurchase();
  184. $dbPurchase->setTable('qianniao_purchase_'.$enterpriseId);
  185. $pwhere=[];
  186. $pwhere["id"]=$purchaseId;
  187. $pwhere["auditStatus"]=2;//审核状态
  188. $pwhere["deleteStatus"]=5;//删除状态
  189. $pwhere["inStatus"]=5;//入库状态
  190. $pwhere["purchaseType"]=4;//采购订单
  191. $purchaseData = $dbPurchase->get($pwhere);
  192. if(empty($purchaseData) || empty($purchaseData["shopId"])){
  193. return ["code"=>-1,"msg"=>"采购单不符合条件".$purchaseData["shopId"]];
  194. }
  195. //验证门店
  196. $shopDb = new DShop();
  197. $shopDb->setTable('qianniao_shop_1');
  198. $shopData = $shopDb->get(["id"=>$purchaseData["shopId"],"enterpriseId"=>$enterpriseId]);
  199. if(empty($shopData) || empty($shopData["pTwoShopId"])){
  200. return ["code"=>-1,"msg"=>"当前门店没有上级门店"];
  201. }
  202. //验证上级门店是否存在
  203. $pShopData = $shopDb->get($shopData["pTwoShopId"]);
  204. if(empty($pShopData) || empty($pShopData["agentId"])){
  205. return ["code"=>-1,"msg"=>"当前门店的上级门店数据配置错误"];
  206. }
  207. //添加任务
  208. $dbTask = new DNewAgentTask('default');
  209. $taskTableName = $dbTask->getTableName($dbTask->get_Table(), $enterpriseId, 1);
  210. $dbTask->setTable($taskTableName);
  211. //验证采购单是否已经添加过任务
  212. $count = $dbTask->count(["purchaseId"=>$purchaseId]);
  213. if($count>0){
  214. return ["code"=>-1,"msg"=>"任务采购单已存在"];
  215. }
  216. $r = $dbTask->insert([
  217. "purchaseId"=>$purchaseId,
  218. "enterpriseId"=>$enterpriseId,
  219. "title"=>"采购单完成计算分佣",
  220. "mono"=>"",
  221. "status"=>0,
  222. "run_time"=>0,
  223. "time"=>time(),
  224. ]);
  225. return ["code"=>1,"msg"=>"任务已插入[{$r}]"];
  226. } catch (\Exception $e){
  227. return ["code"=>-1,"msg"=>"系统错误"];
  228. }
  229. }
  230. /**
  231. * 添加任务
  232. * @param type $purchaseId
  233. * @param type $enterpriseId
  234. * @return bool
  235. */
  236. public static function addTask($purchaseId,$enterpriseId){
  237. try{
  238. if(empty($purchaseId) || empty($enterpriseId)){
  239. return ["code"=>-1,"msg"=>"参数为空"];
  240. }
  241. //获取配置
  242. $dbSetting = new DNewAgentSetting('default');
  243. $settingData = $dbSetting->get(["enterpriseId"=>$enterpriseId]);
  244. if(empty($settingData) || empty($settingData["is_open"]) || $settingData["incomePer"]<=0){
  245. return ["code"=>-1,"msg"=>"还未进行代理推荐分佣配置"];
  246. }
  247. //获取采购单信息
  248. $dbPurchase = new DPurchase();
  249. $dbPurchase->setTable('qianniao_purchase_'.$enterpriseId);
  250. $pwhere=[];
  251. $pwhere["id"]=$purchaseId;
  252. $pwhere["auditStatus"]=2;//审核状态
  253. $pwhere["deleteStatus"]=5;//删除状态
  254. $pwhere["inStatus"]=5;//入库状态
  255. $pwhere["purchaseType"]=4;//采购订单
  256. $purchaseData = $dbPurchase->get($pwhere);
  257. if(empty($purchaseData) || empty($purchaseData["shopId"])){
  258. return ["code"=>-1,"msg"=>"采购单不符合条件".$purchaseData["shopId"]];
  259. }
  260. //验证门店
  261. $shopDb = new DShop();
  262. $shopDb->setTable('qianniao_shop_1');
  263. $shopData = $shopDb->get(["id"=>$purchaseData["shopId"],"enterpriseId"=>$enterpriseId]);
  264. if(empty($shopData) || empty($shopData["agentId"])){
  265. return ["code"=>-1,"msg"=>"当前门店没有代理推荐"];
  266. }
  267. //添加任务
  268. $dbTask = new DNewAgentTask('default');
  269. $taskTableName = $dbTask->getTableName($dbTask->get_Table(), $enterpriseId, 1);
  270. $dbTask->setTable($taskTableName);
  271. //验证采购单是否已经添加过任务
  272. $count = $dbTask->count(["purchaseId"=>$purchaseId]);
  273. if($count>0){
  274. return ["code"=>-1,"msg"=>"任务采购单已存在"];
  275. }
  276. $r = $dbTask->insert([
  277. "purchaseId"=>$purchaseId,
  278. "enterpriseId"=>$enterpriseId,
  279. "title"=>"采购单完成计算分佣",
  280. "mono"=>"",
  281. "status"=>0,
  282. "run_time"=>0,
  283. "time"=>time(),
  284. ]);
  285. return ["code"=>1,"msg"=>"任务已插入[{$r}]"];
  286. } catch (\Exception $e){
  287. return ["code"=>-1,"msg"=>"系统错误"];
  288. }
  289. }
  290. public function runTask(){
  291. if(empty($this->agentSettingData) || empty($this->agentSettingData["is_open"])){
  292. return ["code"=>-1,"msg"=>"未开启配置"];
  293. }
  294. $key = "agenttaskcache".'::'.$this->enterpriseId;
  295. $result = $this->cache->get($key);
  296. if(!empty($result)){
  297. return ["code"=>-1,"msg"=>"操作频繁"];
  298. }
  299. $this->cache->set($key,1,10);
  300. $dbTask = new DNewAgentTask('default');
  301. $taskTableName = $dbTask->getTableName($dbTask->get_Table(), $this->enterpriseId, 1);
  302. $dbTask->setTable($taskTableName);
  303. $data = $dbTask->select(["status"=>0],"*","id asc");
  304. if(empty($data)){
  305. return ["code"=>-1,"msg"=>"没有可执行任务"];
  306. }
  307. $nowTime=time();
  308. foreach($data as $k=>$v){
  309. $res = $this->runCalcMoneyData($v["purchaseId"]);
  310. $save=[];
  311. $save=["status"=>1,"mono"=>"执行成功","run_time"=>$nowTime];
  312. if(empty($res) || $res["code"]==-1){
  313. $save["mono"] = empty($res["msg"])?"系统错误执行失败":$res["msg"];
  314. $save["status"] = -1;
  315. }
  316. $dbTask->update($save, ["id"=>$v["id"]]);
  317. }
  318. $this->cache->set($key,null);
  319. return ["code"=>1,"msg"=>"执行结束"];
  320. }
  321. /**
  322. * 【新】执行任务
  323. * @return type
  324. */
  325. public function runTaskNew(){
  326. if(empty($this->agentSettingData) || empty($this->agentSettingData["is_open"])){
  327. return ["code"=>-1,"msg"=>"未开启配置"];
  328. }
  329. $key = "agenttaskcache".'::'.$this->enterpriseId;
  330. $result = $this->cache->get($key);
  331. if(!empty($result)){
  332. return ["code"=>-1,"msg"=>"操作频繁"];
  333. }
  334. $this->cache->set($key,1,10);
  335. $dbTask = new DNewAgentTask('default');
  336. $taskTableName = $dbTask->getTableName($dbTask->get_Table(), $this->enterpriseId, 1);
  337. $dbTask->setTable($taskTableName);
  338. $data = $dbTask->select(["status"=>0],"*","id asc");
  339. if(empty($data)){
  340. return ["code"=>-1,"msg"=>"没有可执行任务"];
  341. }
  342. $nowTime=time();
  343. foreach($data as $k=>$v){
  344. $res = $this->runCalcMoneyDataNew($v["purchaseId"]);
  345. $save=[];
  346. $save=["status"=>1,"mono"=>"执行成功","run_time"=>$nowTime];
  347. if(empty($res) || $res["code"]==-1){
  348. $save["mono"] = empty($res["msg"])?"系统错误执行失败":$res["msg"];
  349. $save["status"] = -1;
  350. }
  351. $dbTask->update($save, ["id"=>$v["id"]]);
  352. }
  353. $this->cache->set($key,null);
  354. return ["code"=>1,"msg"=>"执行结束"];
  355. }
  356. /**
  357. * 设置门店推广用户
  358. * @param type $parentId
  359. * @param type $shopId
  360. * @return bool
  361. */
  362. public function setPushCustomer($agentCustomerId=0,$shopId=0){
  363. if($agentCustomerId == 0 || $shopId == 0){
  364. return ["code"=>-1,"msg"=>"参数错误","data"=>null];
  365. }
  366. //门店信息
  367. $shopData = $this->dbShop->get(["id"=>$shopId,"enterpriseId"=>$this->enterpriseId]);
  368. if(empty($shopData) || $shopData["agentId"]>0){
  369. return ["code"=>-1,"msg"=>"门店不能被绑定","data"=>$shopData];
  370. }
  371. //父级用户信息
  372. $customeerData = $this->dbCustomer->get($agentCustomerId);
  373. if(empty($customeerData)){
  374. return ["code"=>-1,"msg"=>"代理用户不存在","data"=>null];
  375. }
  376. $this->dbShop->beginTransaction();
  377. $agentData = $this->getAndSetAgentData($customeerData["id"],$customeerData["userCenterId"]);
  378. if(empty($agentData)){
  379. $this->dbShop->rollBack();
  380. return ["code"=>-1,"msg"=>"代理商账户创建失败","data"=>null];
  381. }
  382. //保存门店数据
  383. $res = $this->dbShop->update(["agentId"=>$agentData["id"],"agentCustomerId"=>$agentData["customerId"]], ["id"=>$shopId]);
  384. if(empty($res)){
  385. $this->dbShop->rollBack();
  386. return ["code"=>-1,"msg"=>"绑定失败","data"=>$childSave];
  387. }else{
  388. $this->dbShop->commit();
  389. return ["code"=>1,"msg"=>"绑定成功","data"=>null];
  390. }
  391. }
  392. /**
  393. * 获取代理信息
  394. * @param type $userCenterId
  395. */
  396. public function apiAgentInfoData($userCenterId){
  397. $agentData = $this->dbNewAgent->get(["userCenterId"=>$userCenterId]);
  398. if(empty($agentData)){
  399. return false;
  400. }
  401. $where=[];
  402. $where["agentId"]=$agentData["id"];
  403. $where["deleteStatus"]=5;
  404. $shopData = $this->dbShop->select($where,"id,name");
  405. $agentData["shopData"] = $shopData ? $shopData : [];
  406. $detailCount = $this->dbNewAgentDetail->count(["agentId"=>$agentData["id"]]);
  407. $agentData["detailCount"] = $detailCount?$detailCount:0;
  408. return $agentData;
  409. }
  410. /**
  411. * 获取代理商信息如果不存在则创建
  412. * @param type $customerId
  413. * @param type $userCenterId
  414. * @return bool
  415. */
  416. public function getAndSetAgentData($customerId,$userCenterId=0){
  417. if(empty($customerId)){
  418. return false;
  419. }
  420. if($userCenterId==-1){
  421. $customerData = $this->dbCustomer->get($customerId);
  422. if(!empty($customerData)){
  423. $userCenterId = $customerData["userCenterId"];
  424. }else{
  425. $userCenterId = 0;
  426. }
  427. }
  428. $agentData = $this->dbNewAgent->get(["customerId"=>$customerId]);
  429. if(empty($userCenterId) && empty($agentData)){
  430. return false;
  431. }
  432. if(!empty($userCenterId) && empty($agentData)){
  433. $nowTime = time();
  434. $agentData=[
  435. "userCenterId"=>$userCenterId,
  436. "customerId"=>$customerId,
  437. "balance"=>0,
  438. "waitMoney"=>0,
  439. "withdraw"=>0,
  440. "totalMoney"=>0,
  441. "deleteStatus"=>5,
  442. "createTime"=>$nowTime,
  443. "updateTime"=>$nowTime,
  444. "expand"=>"",
  445. ];
  446. $agentId = $this->dbNewAgent->insert($agentData);
  447. if(empty($agentId)){
  448. return false;
  449. }
  450. $agentData["id"] = $agentId;
  451. $agentData["purchaseNum"] = 0;
  452. }else{
  453. $agentData["purchaseNum"] = $this->getCommissionDetailCount($agentData["id"]);
  454. }
  455. return $agentData;
  456. }
  457. public function getCommissionDetailCount($agentId){
  458. if(empty($agentId) && $type=0){
  459. return 0;
  460. }
  461. $where=[];
  462. if(!empty($agentId)){
  463. $where["agentId"] = $agentId;
  464. }
  465. $count = $this->dbNewAgentDetail->count($where);
  466. return $count?$count:0;
  467. }
  468. /**
  469. * 采购单消费计算佣金
  470. * @param type $purchaseId
  471. * @param type $orderMoney
  472. * @param type $isPart 采购单用户如果是门店代理是否计算
  473. * @return bool
  474. */
  475. public function runCalcMoneyData($purchaseId){
  476. if(empty($this->agentSettingData) || empty($this->agentSettingData["is_open"])){
  477. return ["code"=>-1,"msg"=>"配置信息错误"];
  478. }
  479. //获取采购单信息
  480. if(empty($purchaseId)){
  481. return ["code"=>-1,"msg"=>"采购单id错误"];
  482. }
  483. $pwhere=[];
  484. $pwhere["id"]=$purchaseId;
  485. $pwhere["auditStatus"]=2;//审核状态
  486. $pwhere["deleteStatus"]=5;//删除状态
  487. $pwhere["inStatus"]=5;//入库状态
  488. $pwhere["purchaseType"]=4;//采购订单
  489. $purchaseData = $this->dbPurchase->get($pwhere);
  490. if(empty($purchaseData) || empty($purchaseData["shopId"])){
  491. return ["code"=>-1,"msg"=>"采购单数据不存在或不是门店采购单".$purchaseData["shopId"]];
  492. }
  493. $purchaseShopId = $purchaseData["shopId"];
  494. $shopData = $this->dbShop->get(["id"=>$purchaseShopId,"enterpriseId"=>$this->enterpriseId,"deleteStatus"=>5]);
  495. if(empty($shopData) || empty($shopData["agentId"])){
  496. return ["code"=>-1,"msg"=>"门店被删除或者没有推荐代理"];
  497. }
  498. //开始计算
  499. $purchaseAmount = $purchaseData["purchaseAmount"];//采购金额
  500. $couponAmount = $purchaseData["couponAmount"];//优惠金额
  501. $otherAmount = $purchaseData["otherAmount"];//其它金额
  502. // $purchaseMoney = $purchaseAmount;
  503. $purchaseMoney = $purchaseAmount + $otherAmount - $couponAmount;
  504. if($purchaseMoney<=0){
  505. return ["code"=>-1,"msg"=>"采购实际金额小于等于0"];
  506. }
  507. $agentData = $this->dbNewAgent->get(["id"=>$shopData["agentId"]]);
  508. if(empty($agentData)){
  509. return ["code"=>-1,"msg"=>"推荐代理账号不存在"];
  510. }
  511. $data=[];
  512. $nowTime = time();
  513. //计算收益
  514. $per = $this->agentSettingData["incomePer"];
  515. $commission = bcmul($purchaseMoney,$per,2);
  516. //记录收益
  517. $data[]=[
  518. "purchaseMoney"=>$purchaseMoney,//采购单支付金额
  519. "calcMoney"=>$purchaseMoney,//佣金计算金额
  520. "sourceShopId"=>$purchaseShopId,//来源门店id
  521. "agentId"=>$agentData["id"],//收钱门店代理账号id
  522. "customerId"=>$agentData["customerId"],//收钱门店代理
  523. "userCenterId"=>$agentData["userCenterId"],//收钱门店代理
  524. "commission"=>$commission,//佣金金额
  525. "per"=>$per,//佣金比例
  526. "type"=>0,
  527. "purchaseId"=>$purchaseData["id"],
  528. "purchaseNo"=>$purchaseData["no"],
  529. "status"=>0,//暂时不需要
  530. "title"=>"门店采购单分佣",
  531. "isUpgrade"=>0,
  532. "mono"=>"",
  533. "time"=>$nowTime,
  534. ];
  535. $num=0;
  536. //开启事务
  537. $this->dbNewAgentDetail->beginTransaction();
  538. $lms = "";
  539. foreach($data as $k=>$v){
  540. $count = $this->dbNewAgentDetail->count(["purchaseId"=>$v["purchaseId"],"agentId"=>$v["agentId"]]);
  541. if($count>0){
  542. $lms.="当前采购单已计算佣金[{$v['purchaseId']}];";
  543. continue;
  544. }
  545. $nid = $this->dbNewAgentDetail->insert($v);
  546. if(empty($nid)){
  547. $lms.="佣金明细插入失败[{$v['purchaseId']}];";
  548. continue;
  549. }
  550. //查询门店代理账户
  551. $agentItem = $this->dbNewAgent->get(["id"=>$v["agentId"]]);
  552. if(empty($agentItem)){
  553. $num = 0;
  554. $lms.="门店代理账户不存在[{$v['purchaseId']}];";
  555. break;
  556. }
  557. $update = [
  558. 'balance' => bcadd($agentItem['balance'], $v["commission"], 2),
  559. 'totalMoney' => bcadd($agentItem['totalMoney'], $v["commission"], 2),
  560. 'updateTime' => $nowTime,
  561. ];
  562. //添加账户余额明细记录
  563. $dres = $this->dbNewAgentBalanceDetail->insert([
  564. "type"=>1,
  565. "title"=>$v["title"],
  566. "code"=>$v["type"]==1 ? "agent_calc" : "purchase_calc",
  567. "money"=>$v["commission"],
  568. "content"=>$v["title"],
  569. "admin_id"=>0,
  570. "customer_id"=>$v["customerId"],
  571. "agent_id"=>$v["agentId"],
  572. "user_center_id"=>$v["userCenterId"],
  573. "cash_id"=>0,
  574. "old_balance"=>$agentItem["balance"],
  575. "now_balance"=>$update["balance"],
  576. "time"=>$nowTime,
  577. "expand"=>"",
  578. "detail_id"=>$nid,
  579. ]);
  580. if(empty($dres)){
  581. $num = 0;
  582. $lms.="账户余额明细插入失败[{$v['purchaseId']}];";
  583. break;
  584. }
  585. //更新账户余额
  586. $upRes = $this->dbNewAgent->update($update, ['id' => $v["agentId"]]);
  587. if(empty($upRes)){
  588. $num = 0;
  589. $lms.="更新账户余额失败[{$v['purchaseId']}];";
  590. break;
  591. }
  592. $num++;
  593. }
  594. if($num<=0){
  595. $this->dbNewAgentDetail->rollBack();
  596. return ["code"=>-1,"msg"=>"数据更新失败:".$lms];
  597. }
  598. $this->dbNewAgentDetail->commit();
  599. return ["code"=>1,"msg"=>"执行成功".$lms];
  600. }
  601. /**
  602. * 【新】采购单消费计算佣金
  603. * @param type $purchaseId
  604. * @param type $orderMoney
  605. * @param type $isPart 采购单用户如果是门店代理是否计算
  606. * @return bool
  607. */
  608. public function runCalcMoneyDataNew($purchaseId){
  609. if(empty($this->agentSettingData) || empty($this->agentSettingData["is_open"])){
  610. return ["code"=>-1,"msg"=>"配置信息错误"];
  611. }
  612. //获取采购单信息
  613. if(empty($purchaseId)){
  614. return ["code"=>-1,"msg"=>"采购单id错误"];
  615. }
  616. $pwhere=[];
  617. $pwhere["id"]=$purchaseId;
  618. $pwhere["auditStatus"]=2;//审核状态
  619. $pwhere["deleteStatus"]=5;//删除状态
  620. $pwhere["inStatus"]=5;//入库状态
  621. $pwhere["purchaseType"]=4;//采购订单
  622. $purchaseData = $this->dbPurchase->get($pwhere);
  623. if(empty($purchaseData) || empty($purchaseData["shopId"])){
  624. return ["code"=>-1,"msg"=>"采购单数据不存在或不是门店采购单".$purchaseData["shopId"]];
  625. }
  626. $purchaseShopId = $purchaseData["shopId"];
  627. $shopData = $this->dbShop->get(["id"=>$purchaseShopId,"enterpriseId"=>$this->enterpriseId,"deleteStatus"=>5]);
  628. if(empty($shopData) || empty($shopData["pTwoShopId"])){
  629. return ["code"=>-1,"msg"=>"门店被删除或者没有父级门店"];
  630. }
  631. //开始计算
  632. $purchaseAmount = $purchaseData["purchaseAmount"];//采购金额
  633. $couponAmount = $purchaseData["couponAmount"];//优惠金额
  634. $otherAmount = $purchaseData["otherAmount"];//其它金额
  635. // $purchaseMoney = $purchaseAmount;
  636. $purchaseMoney = $purchaseAmount + $otherAmount - $couponAmount;
  637. if($purchaseMoney<=0){
  638. return ["code"=>-1,"msg"=>"采购实际金额小于等于0"];
  639. }
  640. $data=[];
  641. $nowTime = time();
  642. $levelAr = ["xxxx","one","two"];
  643. //先计算父级门店
  644. $twoShopData = $this->dbShop->get(["id"=>$shopData["pTwoShopId"],"enterpriseId"=>$this->enterpriseId,"deleteStatus"=>5]);
  645. if(!empty($twoShopData) && !empty($twoShopData["agentId"])){
  646. $twoAgentData = $this->dbNewAgent->get(["id"=>$twoShopData["agentId"]]);
  647. if(!empty($twoAgentData)){
  648. //董事
  649. $twoPer=0;
  650. if($twoShopData["level"]==3){
  651. $twoPer = empty($this->agentSettingData["threePer"])?0:$this->agentSettingData["threePer"];
  652. }else{
  653. $twoPerKey = $levelAr[(int)$twoShopData["level"]].ucfirst($levelAr[(int)$shopData["level"]])."Per";
  654. var_dump($twoPerKey);
  655. if(!empty($twoPerKey)){
  656. $twoPer = empty($this->agentSettingData[$twoPerKey]) ? 0 : $this->agentSettingData[$twoPerKey];
  657. }
  658. }
  659. if(!empty($twoPer) && $twoPer>0 && $twoPer<1){
  660. //记录收益
  661. $data[]=[
  662. "purchaseMoney"=>$purchaseMoney,//采购单支付金额
  663. "calcMoney"=>$purchaseMoney,//佣金计算金额
  664. "sourceShopId"=>$purchaseShopId,//来源门店id
  665. "agentId"=>$twoAgentData["id"],//收钱门店代理账号id
  666. "shopId"=>$twoShopData["id"],//收钱门店id
  667. "customerId"=>$twoAgentData["customerId"],//收钱门店代理
  668. "userCenterId"=>$twoAgentData["userCenterId"],//收钱门店代理
  669. "commission"=>bcmul($purchaseMoney,$twoPer,2),//佣金金额
  670. "per"=>$twoPer,//佣金比例
  671. "type"=>0,
  672. "purchaseId"=>$purchaseData["id"],
  673. "purchaseNo"=>$purchaseData["no"],
  674. "status"=>0,//暂时不需要
  675. "title"=>"门店采购单分佣",
  676. "isUpgrade"=>0,
  677. "mono"=>"",
  678. "time"=>$nowTime,
  679. ];
  680. }
  681. }
  682. }
  683. //再计算爷爷级门店
  684. if(!empty($shopData["pOneShopId"])){
  685. $oneShopData = $this->dbShop->get(["id"=>$shopData["pOneShopId"],"enterpriseId"=>$this->enterpriseId,"deleteStatus"=>5]);
  686. if(!empty($oneShopData) && !empty($oneShopData["agentId"])){
  687. $oneAgentData = $this->dbNewAgent->get(["id"=>$oneShopData["agentId"]]);
  688. if(!empty($oneAgentData)){
  689. //董事
  690. $onePer=0;
  691. if($oneShopData["level"]==3){
  692. $onePer = empty($this->agentSettingData["threePer"])?0:$this->agentSettingData["threePer"];
  693. }else{
  694. $onePerKey = $levelAr[(int)$oneShopData["level"]].ucfirst($levelAr[(int)$shopData["level"]])."Per";
  695. if(!empty($onePerKey)){
  696. $onePer = empty($this->agentSettingData[$onePerKey]) ? 0 : $this->agentSettingData[$onePerKey];
  697. }
  698. }
  699. if(!empty($onePer) && $onePer>0 && $onePer<1){
  700. //记录收益
  701. $data[]=[
  702. "purchaseMoney"=>$purchaseMoney,//采购单支付金额
  703. "calcMoney"=>$purchaseMoney,//佣金计算金额
  704. "sourceShopId"=>$purchaseShopId,//来源门店id
  705. "agentId"=>$oneAgentData["id"],//收钱门店代理账号id
  706. "shopId"=>$oneShopData["id"],//收钱门店id
  707. "customerId"=>$oneAgentData["customerId"],//收钱门店代理
  708. "userCenterId"=>$oneAgentData["userCenterId"],//收钱门店代理
  709. "commission"=>bcmul($purchaseMoney,$onePer,2),//佣金金额
  710. "per"=>$onePer,//佣金比例
  711. "type"=>0,
  712. "purchaseId"=>$purchaseData["id"],
  713. "purchaseNo"=>$purchaseData["no"],
  714. "status"=>0,//暂时不需要
  715. "title"=>"门店采购单分佣",
  716. "isUpgrade"=>0,
  717. "mono"=>"",
  718. "time"=>$nowTime,
  719. ];
  720. }
  721. }
  722. }
  723. }
  724. if(empty($data)){
  725. return ["code"=>-1,"msg"=>"父级绑定或者配置错误导致数据更新失败"];
  726. }
  727. $num=0;
  728. //开启事务
  729. $this->dbNewAgentDetail->beginTransaction();
  730. $lms = "";
  731. foreach($data as $k=>$v){
  732. $count = $this->dbNewAgentDetail->count(["purchaseId"=>$v["purchaseId"],"agentId"=>$v["agentId"]]);
  733. if($count>0){
  734. $lms.="当前采购单已计算佣金[{$v['purchaseId']}];";
  735. continue;
  736. }
  737. $nid = $this->dbNewAgentDetail->insert($v);
  738. if(empty($nid)){
  739. $lms.="佣金明细插入失败[{$v['purchaseId']}];";
  740. continue;
  741. }
  742. //查询门店代理账户
  743. $agentItem = $this->dbNewAgent->get(["id"=>$v["agentId"]]);
  744. if(empty($agentItem)){
  745. $num = 0;
  746. $lms.="门店代理账户不存在[{$v['purchaseId']}];";
  747. break;
  748. }
  749. $shopItem = $this->dbShop->get($v["shopId"]);
  750. if(empty($shopItem)){
  751. $num = 0;
  752. $lms.="门店不存在[{$v['purchaseId']}];";
  753. break;
  754. }
  755. $update = [
  756. 'balance' => bcadd($agentItem['balance'], $v["commission"], 2),
  757. 'totalMoney' => bcadd($agentItem['totalMoney'], $v["commission"], 2),
  758. 'updateTime' => $nowTime,
  759. ];
  760. //添加账户余额明细记录
  761. $dres = $this->dbNewAgentBalanceDetail->insert([
  762. "type"=>1,
  763. "title"=>$v["title"],
  764. "code"=>$v["type"]==1 ? "agent_calc" : "purchase_calc",
  765. "money"=>$v["commission"],
  766. "content"=>$v["title"],
  767. "admin_id"=>0,
  768. "customer_id"=>$v["customerId"],
  769. "agent_id"=>$v["agentId"],
  770. "user_center_id"=>$v["userCenterId"],
  771. "shop_id"=>$v["shopId"],
  772. "cash_id"=>0,
  773. "old_balance"=>$agentItem["balance"],
  774. "now_balance"=>$update["balance"],
  775. "time"=>$nowTime,
  776. "expand"=>"",
  777. "detail_id"=>$nid,
  778. ]);
  779. if(empty($dres)){
  780. $num = 0;
  781. $lms.="账户余额明细插入失败[{$v['purchaseId']}];";
  782. break;
  783. }
  784. //更新账户余额
  785. $upRes = $this->dbNewAgent->update($update, ['id' => $v["agentId"]]);
  786. if(empty($upRes)){
  787. $num = 0;
  788. $lms.="更新账户余额失败[{$v['purchaseId']}];";
  789. break;
  790. }
  791. $num++;
  792. }
  793. if($num<=0){
  794. $this->dbNewAgentDetail->rollBack();
  795. return ["code"=>-1,"msg"=>"数据更新失败:".$lms];
  796. }
  797. $this->dbNewAgentDetail->commit();
  798. return ["code"=>1,"msg"=>"执行成功".$lms];
  799. }
  800. /**
  801. * 获取分佣明细
  802. */
  803. public function getCommissionDetail($customerId,$parms){
  804. $where=[];
  805. if(!empty($parms["where"])){
  806. $where= $parms["where"];
  807. }
  808. if(!empty($customerId)){
  809. $where["customerId"] = $customerId;
  810. }
  811. $data = $this->dbNewAgentDetail->select($where, "*", "id desc", $parms["limit"], $parms["offset"]);
  812. $total = $this->dbNewAgentDetail->count($where);
  813. if(empty($data)){
  814. $data = [];
  815. }
  816. foreach($data as $k=>$v){
  817. $itemShop = $this->dbShop->get(["id"=>$v["sourceShopId"]],"id,name,logo");
  818. if(empty($itemShop)){
  819. $data[$k]["name"] = "";
  820. $data[$k]["logo"] = "";
  821. }else{
  822. $data[$k]["name"] = $itemShop["name"];
  823. $data[$k]["logo"] = $itemShop["logo"];
  824. }
  825. }
  826. $return = [
  827. 'data' => $data,
  828. 'total' => (isset($total)&&!empty($total)) ? intval($total) : 0,
  829. ];
  830. return $return;
  831. }
  832. /**
  833. * 获取门店代理账户余额明细
  834. */
  835. public function getCommissionBalanceDetail($customerId,$parms){
  836. $where=[];
  837. if(!empty($customerId)){
  838. $where["customer_id"] = $customerId;
  839. }
  840. $data = $this->dbNewAgentBalanceDetail->select($where, "*", "id desc", $parms["limit"], $parms["offset"]);
  841. $total = $this->dbNewAgentBalanceDetail->count($where);
  842. if(empty($data)){
  843. $data = [];
  844. }
  845. $return = [
  846. 'data' => $data,
  847. 'total' => (isset($total)&&!empty($total)) ? intval($total) : 0,
  848. ];
  849. return $return;
  850. }
  851. /**
  852. *
  853. * @param type $type 0返回标题数组,1范围完整数组
  854. * @return type
  855. */
  856. public static function getBankType($type=0,$title=""){
  857. $data = [
  858. ["title"=>"支付宝","code"=>"alipay","icon"=>"ibonumidd17","addefault"=>"支付宝"],
  859. ["title"=>"微信钱包","code"=>"wxpay","icon"=>"ibonweixinzhifu","addefault"=>"微信"],
  860. ["title"=>"银行卡","code"=>"bank","icon"=>"ibonyinhangqia","addefault"=>""],
  861. ];
  862. if(!empty($title)){
  863. foreach($data as $k=>$v){
  864. if($v["title"]==$title){
  865. return $v;
  866. }
  867. }
  868. return [];
  869. }
  870. if($type==1){
  871. return $data;
  872. }
  873. $nameAr= [];
  874. foreach($data as $k=>$v){
  875. $nameAr[]=$v["title"];
  876. }
  877. return $nameAr;
  878. }
  879. /**
  880. * 提现申请
  881. */
  882. public function applyCash($data){
  883. 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"])){
  884. return ["code"=>-1,"msg"=>"请填写完整信息"];
  885. }
  886. //频繁操作限制
  887. $key = "agentCashCache_".$data["customerId"].'::'.$this->enterpriseId;
  888. $result = $this->cache->get($key);
  889. if(!empty($result)){
  890. return ["code"=>-1,"msg"=>"操作频繁,请稍后尝试!"];
  891. }
  892. $count = $this->dbNewAgentCash->count(["status"=>0,"customer_id"=>$data["customerId"]]);
  893. if($count>0){
  894. return ["code"=>-1,"msg"=>"上次申请还在处理中"];
  895. }
  896. $this->cache->set($key,1,5);
  897. if(!in_array($data["bank_type"], self::getBankType())){
  898. return ["code"=>-1,"msg"=>"提现方式不存在"];
  899. }
  900. $bankTypeData = self::getBankType(1, $data["bank_type"]);
  901. if(empty($bankTypeData)){
  902. return ["code"=>-1,"msg"=>"提现方式不存在"];
  903. }
  904. if(!is_numeric($data["money"])){
  905. return ["code"=>-1,"msg"=>"提现金额错误"];
  906. }
  907. $data["money"] = bcadd($data["money"],0,2);
  908. if($data["money"]<=0){
  909. return ["code"=>-1,"msg"=>"提现金额错误"];
  910. }
  911. $agentData = $this->dbNewAgent->get(["customerId"=>$data["customerId"]]);
  912. if(empty($agentData) || empty($agentData["id"])){
  913. return ["code"=>-1,"msg"=>"门店代理不存在"];
  914. }
  915. if($data["money"]>$agentData["balance"] || $agentData["balance"]<=0){
  916. return ["code"=>-1,"msg"=>"提现金额不足"];
  917. }
  918. $nowTime = time();
  919. $save=[
  920. "customer_id" => $agentData["customerId"],
  921. "user_center_id" => $agentData["userCenterId"],
  922. "agent_id" => $agentData["id"],
  923. "bank_type" => $data["bank_type"],
  924. "bank_type_code" => $bankTypeData["code"],
  925. "bank_name" => $data["bank_name"],
  926. "bank_num" => $data["bank_num"],
  927. "bank_ad" => $data["bank_ad"],
  928. "money" => $data["money"],
  929. "status" => 0,
  930. "time" => $nowTime
  931. ];
  932. try{
  933. $this->dbNewAgent->beginTransaction();
  934. //插入提现申请
  935. $applyId = $this->dbNewAgentCash->insert($save);
  936. if(empty($applyId)){
  937. return ["code"=>-1,"msg"=>"数据提交失败001"];
  938. }
  939. //更新提现账户余额并添加明细
  940. $update = [
  941. 'balance' => bcsub($agentData["balance"], $save["money"], 2),//账户余额
  942. 'withdraw' => bcadd($agentData["withdraw"], $save["money"],2),//已提现金额
  943. 'updateTime' => $nowTime,
  944. ];
  945. if($update["balance"]<=0){
  946. $this->dbNewAgent->rollBack();
  947. return ["code"=>-1,"msg"=>"数据提交失败002"];
  948. }
  949. //添加账户余额明细记录
  950. $dres = $this->dbNewAgentBalanceDetail->insert([
  951. "type"=>-1,
  952. "title"=>"余额提现",
  953. "code"=>"cash_balance",
  954. "money"=>$save["money"],
  955. "content"=>"门店代理余额提现",
  956. "admin_id"=>0,
  957. "customer_id"=>$agentData["customerId"],
  958. "agent_id"=>$agentData["id"],
  959. "user_center_id"=>$agentData["userCenterId"],
  960. "cash_id"=>$applyId,
  961. "old_balance"=>$agentData["balance"],
  962. "now_balance"=>$update["balance"],
  963. "time"=>$nowTime,
  964. "expand"=>"",
  965. "detail_id"=>0,
  966. ]);
  967. if(empty($dres)){
  968. $this->dbNewAgent->rollBack();
  969. return ["code"=>-1,"msg"=>"账户余额明细插入失败"];
  970. }
  971. //更新账户余额
  972. $upRes = $this->dbNewAgent->update($update, ['id' => $agentData["id"]]);
  973. if(empty($upRes)){
  974. $this->dbNewAgent->rollBack();
  975. return ["code"=>-1,"msg"=>"更新账户余额失败"];
  976. }
  977. $this->dbNewAgent->commit();
  978. $this->cache->set($key,null);
  979. return ["code"=>1,"msg"=>"提现申请提交成功"];
  980. } catch (\Exception $e){
  981. $this->dbNewAgent->rollBack();
  982. $this->cache->set($key,null);
  983. return ["code"=>1,"msg"=>"提现申请提交失败"];
  984. }
  985. }
  986. /**
  987. * 获取提现记录
  988. * @param type $customerId
  989. * @param type $parms
  990. * @return type
  991. */
  992. public function getPartnerApplyCashList($customerId,$parms){
  993. $where=[];
  994. if(!empty($parms["where"])){
  995. $where= $parms["where"];
  996. }
  997. if(!empty($customerId)){
  998. $where["customer_id"] = $customerId;
  999. }
  1000. $data = $this->dbNewAgentCash->select($where, "*", "id desc", $parms["limit"], $parms["offset"]);
  1001. $total = $this->dbNewAgentCash->count($where);
  1002. if(empty($data)){
  1003. $data = [];
  1004. }
  1005. $return = [
  1006. 'data' => $data,
  1007. 'total' => (isset($total)&&!empty($total)) ? intval($total) : 0,
  1008. ];
  1009. return $return;
  1010. }
  1011. /**
  1012. * 获取提现详情
  1013. */
  1014. public function getPartnerApplyCashInfo($customerId,$parms){
  1015. $where=[];
  1016. if(!empty($parms["where"])){
  1017. $where= $parms["where"];
  1018. }
  1019. if(!empty($customerId)){
  1020. $where["customer_id"] = $customerId;
  1021. }
  1022. $data = $this->dbNewAgentCash->get($where);
  1023. return $data;
  1024. }
  1025. /**
  1026. * 获取门店代理列表
  1027. * @param type $params
  1028. */
  1029. public function getPartnerList($params = []){
  1030. $customerTableName = 'qianniao_customer_'.$this->enterpriseId;
  1031. $userCenterTableName = 'qianniao_user_center';
  1032. $newCommissionPartnerTableName = 'qianniao_new_agent_'.$this->enterpriseId;
  1033. //查询数据
  1034. $field = "p.*,u.mobile,c.name,c.avatar";
  1035. $sql = "select {$field} from {$newCommissionPartnerTableName} p "
  1036. . "left join {$userCenterTableName} u on u.id = p.userCenterId "
  1037. . "left join {$customerTableName} c on c.id = p.customerId";
  1038. $whereSql = ' where p.deleteStatus = 5 ';
  1039. if(isset($params['search'])){
  1040. $whereSql .= ' and (u.mobile like "%'.$params['search'].'%" or c.name like "%'.$params['search'].'%") ';
  1041. }
  1042. $orderSql = ' order by p.id desc ';
  1043. $limitSql = ' limit '.$params['offset'].','.$params['limit'];
  1044. $querySql = $sql.$whereSql.$orderSql.$limitSql;
  1045. $data = $this->dbNewAgent->query($querySql);
  1046. if(empty($data)){
  1047. $data=[];
  1048. }
  1049. //查询总数
  1050. $countSql = "select count(*) as `count` from {$newCommissionPartnerTableName} p "
  1051. . "left join {$userCenterTableName} u on u.id = p.userCenterId "
  1052. . "left join {$customerTableName} c on c.id = p.customerId";
  1053. $countData = $this->dbNewAgent->query($countSql.$whereSql);
  1054. $total = 0;
  1055. if(!empty($countData)){
  1056. $total =array_shift($countData)['count'];
  1057. }
  1058. return ['data'=>$data,'total'=>($total) ? intval($total) : 0];
  1059. }
  1060. /**
  1061. * 获取门店代理余额明细列表
  1062. * @param type $params
  1063. */
  1064. public function getBalanceDetailList($params = []){
  1065. $customerTableName = 'qianniao_customer_'.$this->enterpriseId;
  1066. $userCenterTableName = 'qianniao_user_center';
  1067. $newCommissionBalanceDetailTableName = 'qianniao_new_agent_balance_detail_'.$this->enterpriseId;
  1068. //查询数据
  1069. $field = "p.*,u.mobile,c.name,c.avatar";
  1070. $sql = "select {$field} from {$newCommissionBalanceDetailTableName} p "
  1071. . "left join {$userCenterTableName} u on u.id = p.user_center_id "
  1072. . "left join {$customerTableName} c on c.id = p.customer_id";
  1073. $whereSql = ' where 1=1 ';
  1074. if(!empty($params['agentId'])){
  1075. $whereSql .= ' and p.agent_id = '.$params['agentId'];
  1076. }
  1077. if(isset($params['search'])){
  1078. $whereSql .= ' and (u.mobile like "%'.$params['search'].'%" or c.name like "%'.$params['search'].'%") ';
  1079. }
  1080. $orderSql = ' order by p.id desc ';
  1081. $limitSql = ' limit '.$params['offset'].','.$params['limit'];
  1082. $querySql = $sql.$whereSql.$orderSql.$limitSql;
  1083. $data = $this->dbNewAgent->query($querySql);
  1084. if(empty($data)){
  1085. $data=[];
  1086. }
  1087. //查询总数
  1088. $countSql = "select count(*) as `count` from {$newCommissionBalanceDetailTableName} p "
  1089. . "left join {$userCenterTableName} u on u.id = p.user_center_id "
  1090. . "left join {$customerTableName} c on c.id = p.customer_id";
  1091. $countData = $this->dbNewAgent->query($countSql.$whereSql);
  1092. $total = 0;
  1093. if(!empty($countData)){
  1094. $total =array_shift($countData)['count'];
  1095. }
  1096. return ['data'=>$data,'total'=>($total) ? intval($total) : 0];
  1097. }
  1098. /**
  1099. * 获取门店代理佣金记录
  1100. * @param type $params
  1101. */
  1102. public function getDetailList($params = []){
  1103. $customerTableName = 'qianniao_customer_'.$this->enterpriseId;
  1104. $userCenterTableName = 'qianniao_user_center';
  1105. $newCommissionDetailTableName = 'qianniao_new_agent_detail_'.$this->enterpriseId;
  1106. $shopTableName = "qianniao_shop_1";
  1107. //查询数据
  1108. $field = "p.*,u.mobile,c.name,c.avatar,s.name as sourceName ";
  1109. $sql = "select {$field} from {$newCommissionDetailTableName} p "
  1110. . "left join {$userCenterTableName} u on u.id = p.userCenterId "
  1111. . "left join {$customerTableName} c on c.id = p.customerId "
  1112. . "left join {$shopTableName} s on s.id = p.sourceShopId";
  1113. $whereSql = ' where 1=1 ';
  1114. if(!empty($params['agentId'])){
  1115. $whereSql .= ' and p.agentId = '.$params['agentId'];
  1116. }
  1117. if(isset($params['search'])){
  1118. $whereSql .= ' and (u.mobile like "%'.$params['search'].'%" or c.name like "%'.$params['search'].'%") ';
  1119. }
  1120. $orderSql = ' order by p.id desc ';
  1121. $limitSql = ' limit '.$params['offset'].','.$params['limit'];
  1122. $querySql = $sql.$whereSql.$orderSql.$limitSql;
  1123. $data = $this->dbNewAgent->query($querySql);
  1124. if(empty($data)){
  1125. $data=[];
  1126. }
  1127. //查询总数
  1128. $countSql = "select count(*) as `count` from {$newCommissionDetailTableName} p "
  1129. . "left join {$userCenterTableName} u on u.id = p.userCenterId "
  1130. . "left join {$customerTableName} c on c.id = p.customerId "
  1131. . "left join {$shopTableName} s on s.id = p.sourceShopId";
  1132. $countData = $this->dbNewAgent->query($countSql.$whereSql);
  1133. $total = 0;
  1134. if(!empty($countData)){
  1135. $total =array_shift($countData)['count'];
  1136. }
  1137. return ['data'=>$data,'total'=>($total) ? intval($total) : 0];
  1138. }
  1139. /**
  1140. * 获取提现记录
  1141. * @param type $params
  1142. */
  1143. public function getPartnerCashList($params = []){
  1144. $customerTableName = 'qianniao_customer_'.$this->enterpriseId;
  1145. $userCenterTableName = 'qianniao_user_center';
  1146. $newCommissionCashTableName = 'qianniao_new_agent_cash_'.$this->enterpriseId;
  1147. //查询数据
  1148. $field = "p.*,u.mobile,c.name,c.avatar ";
  1149. $sql = "select {$field} from {$newCommissionCashTableName} p "
  1150. . "left join {$userCenterTableName} u on u.id = p.user_center_id "
  1151. . "left join {$customerTableName} c on c.id = p.customer_id ";
  1152. $whereSql = ' where 1=1 ';
  1153. if(!empty($params['agentId'])){
  1154. $whereSql .= ' and p.agent_id = '.$params['agentId'];
  1155. }
  1156. if(isset($params['search'])){
  1157. $whereSql .= ' and (u.mobile like "%'.$params['search'].'%" or c.name like "%'.$params['search'].'%") ';
  1158. }
  1159. if(isset($params['status'])){
  1160. $whereSql .= ' and p.status = '.$params['status'];
  1161. }
  1162. if(!empty($params['bank_type_code'])){
  1163. $whereSql .= " and p.bank_type_code = '{$params['bank_type_code']}'";
  1164. }
  1165. if(!empty($params['start_time']) && !empty($params['end_time'])){
  1166. $whereSql .= " and p.time >= {$params['start_time']} and p.time < {$params['end_time']}";
  1167. }
  1168. $orderSql = ' order by p.id desc ';
  1169. $limitSql = ' limit '.$params['offset'].','.$params['limit'];
  1170. $querySql = $sql.$whereSql.$orderSql.$limitSql;
  1171. $data = $this->dbNewAgent->query($querySql);
  1172. if(empty($data)){
  1173. $data=[];
  1174. }
  1175. //查询总数
  1176. $countSql = "select count(*) as `count` from {$newCommissionCashTableName} p "
  1177. . "left join {$userCenterTableName} u on u.id = p.user_center_id "
  1178. . "left join {$customerTableName} c on c.id = p.customer_id ";
  1179. $countData = $this->dbNewAgent->query($countSql.$whereSql);
  1180. $total = 0;
  1181. if(!empty($countData)){
  1182. $total =array_shift($countData)['count'];
  1183. }
  1184. return ['data'=>$data,'total'=>($total) ? intval($total) : 0];
  1185. }
  1186. /**
  1187. * 提现处理
  1188. * @param type $id
  1189. * @param type $mono
  1190. * @param type $type
  1191. */
  1192. public function partnerCashAudit($id,$mono,$type,$audit_id=0){
  1193. if(empty($id)|| empty($mono) || empty($type) || !in_array($type, [1,-1])){
  1194. return ["code"=>-1,"msg"=>"参数错误"];
  1195. }
  1196. $adminData = $this->dbUserCenter->get($this->onlineUserId);
  1197. if(empty($adminData)){
  1198. $adminData=["audit_name"=>"管理人员"];
  1199. }
  1200. //频繁操作限制
  1201. $key = "agentCashDealCache_".$id.'::'.$this->enterpriseId;
  1202. $result = $this->cache->get($key);
  1203. if(!empty($result)){
  1204. return ["code"=>-1,"msg"=>"处理中,请耐心等待!"];
  1205. }
  1206. $this->cache->set($key,1,5);
  1207. $where = ["id"=>$id,"status"=>0];
  1208. $data = $this->dbNewAgentCash->get($where);
  1209. if(empty($data)){
  1210. $this->cache->set($key,null);
  1211. return ["code"=>-1,"msg"=>"数据不存在"];
  1212. }
  1213. //门店代理账户信息
  1214. $agentData = $this->dbNewAgent->get(["id"=>$data["agent_id"]]);
  1215. if(empty($agentData)){
  1216. $this->cache->set($key,null);
  1217. return ["code"=>-1,"msg"=>"门店代理账户不存在"];
  1218. }
  1219. //打款成功
  1220. if($type==1){
  1221. $res = $this->dbNewAgentCash->update([
  1222. "status"=>1,
  1223. "audit_time"=>time(),
  1224. "audit_mono"=>$mono,
  1225. "audit_id"=>$audit_id,
  1226. "audit_name"=>$adminData["mobile"],
  1227. ], $where);
  1228. $this->cache->set($key,null);
  1229. if(empty($res)){
  1230. return ["code"=>-1,"msg"=>"系统繁忙,请稍后重试001!"];
  1231. }
  1232. return ["code"=>1,"msg"=>"处理完成"];
  1233. }
  1234. //审核驳回
  1235. try{
  1236. $this->dbNewAgentCash->beginTransaction();
  1237. $res = $this->dbNewAgentCash->update([
  1238. "status"=>-1,
  1239. "audit_time"=>time(),
  1240. "audit_mono"=>$mono,
  1241. "audit_id"=>$audit_id,
  1242. "audit_name"=>$adminData["mobile"],
  1243. ], $where);
  1244. if(empty($res)){
  1245. $this->dbNewAgentCash->rollBack();
  1246. $this->cache->set($key,null);
  1247. return ["code"=>-1,"msg"=>"操作失败001!"];
  1248. }
  1249. //添加账户余额明细记录
  1250. $dres = $this->dbNewAgentBalanceDetail->insert([
  1251. "type"=>1,
  1252. "title"=>"提现驳回返还余额",
  1253. "code"=>"cash_no_balance",
  1254. "money"=>$data["money"],
  1255. "content"=>"提现驳回返还余额:".$mono,
  1256. "admin_id"=>$audit_id,
  1257. "customer_id"=>$agentData["customerId"],
  1258. "agent_id"=>$agentData["id"],
  1259. "user_center_id"=>$agentData["userCenterId"],
  1260. "cash_id"=>$data["id"],
  1261. "old_balance"=>$agentData["balance"],
  1262. "now_balance"=>$agentData["balance"]+$data["money"],
  1263. "time"=>time(),
  1264. "expand"=>"",
  1265. "detail_id"=>0,
  1266. ]);
  1267. if(empty($dres)){
  1268. $this->dbNewAgentCash->rollBack();
  1269. $this->cache->set($key,null);
  1270. return ["code"=>-1,"msg"=>"操作失败002!"];
  1271. }
  1272. //更新账户数据
  1273. $withdrawRes = $this->dbNewAgent->set_dec("withdraw", ["id"=>$agentData["id"]], $data["money"]);
  1274. if(empty($withdrawRes)){
  1275. $this->dbNewAgentCash->rollBack();
  1276. $this->cache->set($key,null);
  1277. return ["code"=>-1,"msg"=>"操作失败003"];
  1278. }
  1279. $balanceRes = $this->dbNewAgent->set_inc("balance", ["id"=>$agentData["id"]], $data["money"]);
  1280. if(empty($balanceRes)){
  1281. $this->dbNewAgentCash->rollBack();
  1282. $this->cache->set($key,null);
  1283. return ["code"=>-1,"msg"=>"操作失败004"];
  1284. }
  1285. //提交事务
  1286. $this->dbNewAgentCash->commit();
  1287. $this->cache->set($key,null);
  1288. return ["code"=>1,"msg"=>"提现申请处理成功!"];
  1289. } catch (\Exception $e){
  1290. $this->dbNewAgentCash->rollBack();
  1291. $this->cache->set($key,null);
  1292. return ["code"=>-1,"msg"=>"系统繁忙,请稍后重试!"];
  1293. }
  1294. }
  1295. /**
  1296. * 获取设置信息
  1297. * @param type $enterpriseId
  1298. * @return string
  1299. */
  1300. public static function getCommissionSettingData($enterpriseId){
  1301. $dbSetting = new DNewAgentSetting('default');
  1302. $data = $dbSetting->get(["enterpriseId"=>$enterpriseId]);
  1303. if(empty($data)){
  1304. $data=[
  1305. "id"=>0,
  1306. "enterpriseId"=>$enterpriseId,
  1307. "levMoney"=>0,
  1308. "levPer"=>0,
  1309. "incomePer"=>0,
  1310. "is_open"=>0,
  1311. "bankData"=>"",
  1312. "oneToTwoNum"=>0,
  1313. "twoToThreeNum"=>0,
  1314. "oneOnePer"=>0,
  1315. "oneTwoPer"=>0,
  1316. "twoOnePer"=>0,
  1317. "twoTwoPer"=>0,
  1318. "threePer"=>0,
  1319. ];
  1320. }
  1321. return $data;
  1322. }
  1323. /**
  1324. * 推荐代理分佣设置
  1325. * @param type $enterpriseId
  1326. * @param type $parms
  1327. * @return type
  1328. */
  1329. public static function setCommissionSettingData($enterpriseId,$parms){
  1330. if(empty($enterpriseId) || empty($parms)){
  1331. return ["code"=>"-1","msg"=>"参数错误"];
  1332. }
  1333. $dbSetting = new DNewAgentSetting('default');
  1334. $data = $dbSetting->get(["enterpriseId"=>$enterpriseId]);
  1335. $parms["levMoney"] = empty($parms["levMoney"]) ? 0 : $parms["levMoney"];
  1336. $parms["levPer"] = empty($parms["levPer"]) ? 0 : $parms["levPer"];
  1337. $parms["incomePer"] = empty($parms["incomePer"])? 0 : $parms["incomePer"];
  1338. $parms["is_open"] = empty($parms["is_open"]) ? 0 : 1;
  1339. // if(!is_numeric($parms["levMoney"]) || $parms["levMoney"]<=0){
  1340. // return ["code"=>"-1","msg"=>"升级门店代理金额必须大于0"];
  1341. // }
  1342. // if(!is_numeric($parms["levPer"]) || $parms["levPer"]<0 || $parms["levPer"]>=1){
  1343. // return ["code"=>"-1","msg"=>"子级升级门店代理消费佣金比例必须0到1之间"];
  1344. // }
  1345. if(!is_numeric($parms["oneOnePer"]) || $parms["oneOnePer"]<0 || $parms["oneOnePer"]>=1){
  1346. return ["code"=>"-1","msg"=>"佣金比例必须0到1之间"];
  1347. }
  1348. if(!is_numeric($parms["oneTwoPer"]) || $parms["oneTwoPer"]<0 || $parms["oneTwoPer"]>=1){
  1349. return ["code"=>"-1","msg"=>"佣金比例必须0到1之间"];
  1350. }
  1351. if(!is_numeric($parms["twoOnePer"]) || $parms["twoOnePer"]<0 || $parms["twoOnePer"]>=1){
  1352. return ["code"=>"-1","msg"=>"佣金比例必须0到1之间"];
  1353. }
  1354. if(!is_numeric($parms["threePer"]) || $parms["threePer"]<0 || $parms["threePer"]>=1){
  1355. return ["code"=>"-1","msg"=>"佣金比例必须0到1之间"];
  1356. }
  1357. $saveData = [
  1358. "enterpriseId" => $enterpriseId,
  1359. "levMoney" => $parms["levMoney"],
  1360. "levPer" => $parms["levPer"],
  1361. "incomePer" => $parms["incomePer"],
  1362. "is_open" => $parms["is_open"],
  1363. "oneToTwoNum" => $parms["oneToTwoNum"]?:0,
  1364. "twoToThreeNum"=> $parms["twoToThreeNum"]?:0,
  1365. "oneOnePer" => $parms["oneOnePer"]?:0,
  1366. "oneTwoPer" => $parms["oneTwoPer"]?:0,
  1367. "twoOnePer" => $parms["twoOnePer"]?:0,
  1368. "twoTwoPer" => $parms["twoTwoPer"]?:0,
  1369. "threePer" => $parms["threePer"]?:0,
  1370. "updateTime" => time()
  1371. ];
  1372. $res = false;
  1373. if(empty($data)){
  1374. $saveData["enterpriseId"] = $enterpriseId;
  1375. $saveData["time"] = time();
  1376. $res = $dbSetting->insert($saveData);
  1377. }else{
  1378. $res = $dbSetting->update($saveData, ["id"=>$data["id"]]);
  1379. }
  1380. if(empty($res)){
  1381. return ["code"=>"-1","msg"=>"系统繁忙,请稍后重试"];
  1382. }
  1383. return ["code"=>1,"msg"=>"配置成功"];
  1384. }
  1385. public function getCustomerDataByUserCenterId($userCenterId){
  1386. if(empty($userCenterId)){
  1387. return false;
  1388. }
  1389. $data = $this->dbCustomer->get(["userCenterId"=>$userCenterId]);
  1390. if(empty($data)){
  1391. return false;
  1392. }
  1393. return $data;
  1394. }
  1395. }