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