AgentTools.Class.php 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481
  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 = $this->dbShop->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. $twoShopData = $this->dbShop->get(["id"=>$shopData["pTwoShopId"],"enterpriseId"=>$this->enterpriseId,"deleteStatus"=>5]);
  642. if(!empty($twoShopData) && !empty($twoShopData["agentId"])){
  643. $twoAgentData = $this->dbNewAgent->get(["id"=>$twoShopData["agentId"]]);
  644. if(!empty($twoAgentData)){
  645. //董事
  646. $twoPer=0;
  647. if($twoShopData["level"]==3){
  648. $twoPer = empty($this->agentSettingData["threePer"])?0:$this->agentSettingData["threePer"];
  649. }else{
  650. $twoPerKey = $levelAr[(int)$twoShopData["level"]].ucfirst($levelAr[(int)$shopData["level"]])."Per";
  651. if(!empty($twoPerKey)){
  652. $twoPer = empty($this->agentSettingData[$twoPerKey]) ? 0 : $this->agentSettingData[$twoPerKey];
  653. }
  654. }
  655. if(!empty($twoPer) && $twoPer>0 && $twoPer<1){
  656. //记录收益
  657. $data[]=[
  658. "purchaseMoney"=>$purchaseMoney,//采购单支付金额
  659. "calcMoney"=>$purchaseMoney,//佣金计算金额
  660. "sourceShopId"=>$purchaseShopId,//来源门店id
  661. "agentId"=>$twoAgentData["id"],//收钱门店代理账号id
  662. "shopId"=>$twoShopData["id"],//收钱门店id
  663. "customerId"=>$twoAgentData["customerId"],//收钱门店代理
  664. "userCenterId"=>$twoAgentData["userCenterId"],//收钱门店代理
  665. "commission"=>bcmul($purchaseMoney,$twoPer,2),//佣金金额
  666. "per"=>$twoPer,//佣金比例
  667. "type"=>0,
  668. "purchaseId"=>$purchaseData["id"],
  669. "purchaseNo"=>$purchaseData["no"],
  670. "status"=>0,//暂时不需要
  671. "title"=>"门店采购单分佣",
  672. "isUpgrade"=>0,
  673. "mono"=>"",
  674. "time"=>$nowTime,
  675. ];
  676. }
  677. }
  678. }
  679. //再计算爷爷级门店
  680. if(!empty($shopData["pOneShopId"])){
  681. $oneShopData = $this->dbShop->get(["id"=>$shopData["pOneShopId"],"enterpriseId"=>$this->enterpriseId,"deleteStatus"=>5]);
  682. if(!empty($oneShopData) && !empty($oneShopData["agentId"])){
  683. $oneAgentData = $this->dbNewAgent->get(["id"=>$oneShopData["agentId"]]);
  684. if(!empty($oneAgentData)){
  685. //董事
  686. $onePer=0;
  687. if($oneShopData["level"]==3){
  688. $onePer = empty($this->agentSettingData["threePer"])?0:$this->agentSettingData["threePer"];
  689. }else{
  690. $onePerKey = $levelAr[(int)$oneShopData["level"]].ucfirst($levelAr[(int)$shopData["level"]])."Per";
  691. if(!empty($onePerKey)){
  692. $onePer = empty($this->agentSettingData[$onePerKey]) ? 0 : $this->agentSettingData[$onePerKey];
  693. }
  694. }
  695. if(!empty($onePer) && $onePer>0 && $onePer<1){
  696. //记录收益
  697. $data[]=[
  698. "purchaseMoney"=>$purchaseMoney,//采购单支付金额
  699. "calcMoney"=>$purchaseMoney,//佣金计算金额
  700. "sourceShopId"=>$purchaseShopId,//来源门店id
  701. "agentId"=>$oneAgentData["id"],//收钱门店代理账号id
  702. "shopId"=>$oneShopData["id"],//收钱门店id
  703. "customerId"=>$oneAgentData["customerId"],//收钱门店代理
  704. "userCenterId"=>$oneAgentData["userCenterId"],//收钱门店代理
  705. "commission"=>bcmul($purchaseMoney,$onePer,2),//佣金金额
  706. "per"=>$onePer,//佣金比例
  707. "type"=>0,
  708. "purchaseId"=>$purchaseData["id"],
  709. "purchaseNo"=>$purchaseData["no"],
  710. "status"=>0,//暂时不需要
  711. "title"=>"门店采购单分佣",
  712. "isUpgrade"=>0,
  713. "mono"=>"",
  714. "time"=>$nowTime,
  715. ];
  716. }
  717. }
  718. }
  719. }
  720. if(empty($data)){
  721. return ["code"=>-1,"msg"=>"父级绑定或者配置错误导致数据更新失败"];
  722. }
  723. $num=0;
  724. //开启事务
  725. $this->dbNewAgentDetail->beginTransaction();
  726. $lms = "";
  727. foreach($data as $k=>$v){
  728. $count = $this->dbNewAgentDetail->count(["purchaseId"=>$v["purchaseId"],"agentId"=>$v["agentId"]]);
  729. if($count>0){
  730. $lms.="当前采购单已计算佣金[{$v['purchaseId']}];";
  731. continue;
  732. }
  733. $nid = $this->dbNewAgentDetail->insert($v);
  734. if(empty($nid)){
  735. $lms.="佣金明细插入失败[{$v['purchaseId']}];";
  736. continue;
  737. }
  738. //查询门店代理账户
  739. $agentItem = $this->dbNewAgent->get(["id"=>$v["agentId"]]);
  740. if(empty($agentItem)){
  741. $num = 0;
  742. $lms.="门店代理账户不存在[{$v['purchaseId']}];";
  743. break;
  744. }
  745. $shopItem = $this->dbShop->get($v["shopId"]);
  746. if(empty($shopItem)){
  747. $num = 0;
  748. $lms.="门店不存在[{$v['purchaseId']}];";
  749. break;
  750. }
  751. $update = [
  752. 'balance' => bcadd($agentItem['balance'], $v["commission"], 2),
  753. 'totalMoney' => bcadd($agentItem['totalMoney'], $v["commission"], 2),
  754. 'updateTime' => $nowTime,
  755. ];
  756. //添加账户余额明细记录
  757. $dres = $this->dbNewAgentBalanceDetail->insert([
  758. "type"=>1,
  759. "title"=>$v["title"],
  760. "code"=>$v["type"]==1 ? "agent_calc" : "purchase_calc",
  761. "money"=>$v["commission"],
  762. "content"=>$v["title"],
  763. "admin_id"=>0,
  764. "customer_id"=>$v["customerId"],
  765. "agent_id"=>$v["agentId"],
  766. "user_center_id"=>$v["userCenterId"],
  767. "shop_id"=>$v["shopId"],
  768. "cash_id"=>0,
  769. "old_balance"=>$agentItem["balance"],
  770. "now_balance"=>$update["balance"],
  771. "time"=>$nowTime,
  772. "expand"=>"",
  773. "detail_id"=>$nid,
  774. ]);
  775. if(empty($dres)){
  776. $num = 0;
  777. $lms.="账户余额明细插入失败[{$v['purchaseId']}];";
  778. break;
  779. }
  780. //更新账户余额
  781. $upRes = $this->dbNewAgent->update($update, ['id' => $v["agentId"]]);
  782. if(empty($upRes)){
  783. $num = 0;
  784. $lms.="更新账户余额失败[{$v['purchaseId']}];";
  785. break;
  786. }
  787. $num++;
  788. }
  789. if($num<=0){
  790. $this->dbNewAgentDetail->rollBack();
  791. return ["code"=>-1,"msg"=>"数据更新失败:".$lms];
  792. }
  793. $this->dbNewAgentDetail->commit();
  794. return ["code"=>1,"msg"=>"执行成功".$lms];
  795. }
  796. /**
  797. * 获取分佣明细
  798. */
  799. public function getCommissionDetail($customerId,$parms){
  800. $where=[];
  801. if(!empty($parms["where"])){
  802. $where= $parms["where"];
  803. }
  804. if(!empty($customerId)){
  805. $where["customerId"] = $customerId;
  806. }
  807. $data = $this->dbNewAgentDetail->select($where, "*", "id desc", $parms["limit"], $parms["offset"]);
  808. $total = $this->dbNewAgentDetail->count($where);
  809. if(empty($data)){
  810. $data = [];
  811. }
  812. foreach($data as $k=>$v){
  813. $itemShop = $this->dbShop->get(["id"=>$v["sourceShopId"]],"id,name,logo");
  814. if(empty($itemShop)){
  815. $data[$k]["name"] = "";
  816. $data[$k]["logo"] = "";
  817. }else{
  818. $data[$k]["name"] = $itemShop["name"];
  819. $data[$k]["logo"] = $itemShop["logo"];
  820. }
  821. }
  822. $return = [
  823. 'data' => $data,
  824. 'total' => (isset($total)&&!empty($total)) ? intval($total) : 0,
  825. ];
  826. return $return;
  827. }
  828. /**
  829. * 获取门店代理账户余额明细
  830. */
  831. public function getCommissionBalanceDetail($customerId,$parms){
  832. $where=[];
  833. if(!empty($customerId)){
  834. $where["customer_id"] = $customerId;
  835. }
  836. $data = $this->dbNewAgentBalanceDetail->select($where, "*", "id desc", $parms["limit"], $parms["offset"]);
  837. $total = $this->dbNewAgentBalanceDetail->count($where);
  838. if(empty($data)){
  839. $data = [];
  840. }
  841. $return = [
  842. 'data' => $data,
  843. 'total' => (isset($total)&&!empty($total)) ? intval($total) : 0,
  844. ];
  845. return $return;
  846. }
  847. /**
  848. *
  849. * @param type $type 0返回标题数组,1范围完整数组
  850. * @return type
  851. */
  852. public static function getBankType($type=0,$title=""){
  853. $data = [
  854. ["title"=>"支付宝","code"=>"alipay","icon"=>"ibonumidd17","addefault"=>"支付宝"],
  855. ["title"=>"微信钱包","code"=>"wxpay","icon"=>"ibonweixinzhifu","addefault"=>"微信"],
  856. ["title"=>"银行卡","code"=>"bank","icon"=>"ibonyinhangqia","addefault"=>""],
  857. ];
  858. if(!empty($title)){
  859. foreach($data as $k=>$v){
  860. if($v["title"]==$title){
  861. return $v;
  862. }
  863. }
  864. return [];
  865. }
  866. if($type==1){
  867. return $data;
  868. }
  869. $nameAr= [];
  870. foreach($data as $k=>$v){
  871. $nameAr[]=$v["title"];
  872. }
  873. return $nameAr;
  874. }
  875. /**
  876. * 提现申请
  877. */
  878. public function applyCash($data){
  879. 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"])){
  880. return ["code"=>-1,"msg"=>"请填写完整信息"];
  881. }
  882. //频繁操作限制
  883. $key = "agentCashCache_".$data["customerId"].'::'.$this->enterpriseId;
  884. $result = $this->cache->get($key);
  885. if(!empty($result)){
  886. return ["code"=>-1,"msg"=>"操作频繁,请稍后尝试!"];
  887. }
  888. $count = $this->dbNewAgentCash->count(["status"=>0,"customer_id"=>$data["customerId"]]);
  889. if($count>0){
  890. return ["code"=>-1,"msg"=>"上次申请还在处理中"];
  891. }
  892. $this->cache->set($key,1,5);
  893. if(!in_array($data["bank_type"], self::getBankType())){
  894. return ["code"=>-1,"msg"=>"提现方式不存在"];
  895. }
  896. $bankTypeData = self::getBankType(1, $data["bank_type"]);
  897. if(empty($bankTypeData)){
  898. return ["code"=>-1,"msg"=>"提现方式不存在"];
  899. }
  900. if(!is_numeric($data["money"])){
  901. return ["code"=>-1,"msg"=>"提现金额错误"];
  902. }
  903. $data["money"] = bcadd($data["money"],0,2);
  904. if($data["money"]<=0){
  905. return ["code"=>-1,"msg"=>"提现金额错误"];
  906. }
  907. $agentData = $this->dbNewAgent->get(["customerId"=>$data["customerId"]]);
  908. if(empty($agentData) || empty($agentData["id"])){
  909. return ["code"=>-1,"msg"=>"门店代理不存在"];
  910. }
  911. if($data["money"]>$agentData["balance"] || $agentData["balance"]<=0){
  912. return ["code"=>-1,"msg"=>"提现金额不足"];
  913. }
  914. $nowTime = time();
  915. $save=[
  916. "customer_id" => $agentData["customerId"],
  917. "user_center_id" => $agentData["userCenterId"],
  918. "agent_id" => $agentData["id"],
  919. "bank_type" => $data["bank_type"],
  920. "bank_type_code" => $bankTypeData["code"],
  921. "bank_name" => $data["bank_name"],
  922. "bank_num" => $data["bank_num"],
  923. "bank_ad" => $data["bank_ad"],
  924. "money" => $data["money"],
  925. "status" => 0,
  926. "time" => $nowTime
  927. ];
  928. try{
  929. $this->dbNewAgent->beginTransaction();
  930. //插入提现申请
  931. $applyId = $this->dbNewAgentCash->insert($save);
  932. if(empty($applyId)){
  933. return ["code"=>-1,"msg"=>"数据提交失败001"];
  934. }
  935. //更新提现账户余额并添加明细
  936. $update = [
  937. 'balance' => bcsub($agentData["balance"], $save["money"], 2),//账户余额
  938. 'withdraw' => bcadd($agentData["withdraw"], $save["money"],2),//已提现金额
  939. 'updateTime' => $nowTime,
  940. ];
  941. if($update["balance"]<=0){
  942. $this->dbNewAgent->rollBack();
  943. return ["code"=>-1,"msg"=>"数据提交失败002"];
  944. }
  945. //添加账户余额明细记录
  946. $dres = $this->dbNewAgentBalanceDetail->insert([
  947. "type"=>-1,
  948. "title"=>"余额提现",
  949. "code"=>"cash_balance",
  950. "money"=>$save["money"],
  951. "content"=>"门店代理余额提现",
  952. "admin_id"=>0,
  953. "customer_id"=>$agentData["customerId"],
  954. "agent_id"=>$agentData["id"],
  955. "user_center_id"=>$agentData["userCenterId"],
  956. "cash_id"=>$applyId,
  957. "old_balance"=>$agentData["balance"],
  958. "now_balance"=>$update["balance"],
  959. "time"=>$nowTime,
  960. "expand"=>"",
  961. "detail_id"=>0,
  962. ]);
  963. if(empty($dres)){
  964. $this->dbNewAgent->rollBack();
  965. return ["code"=>-1,"msg"=>"账户余额明细插入失败"];
  966. }
  967. //更新账户余额
  968. $upRes = $this->dbNewAgent->update($update, ['id' => $agentData["id"]]);
  969. if(empty($upRes)){
  970. $this->dbNewAgent->rollBack();
  971. return ["code"=>-1,"msg"=>"更新账户余额失败"];
  972. }
  973. $this->dbNewAgent->commit();
  974. $this->cache->set($key,null);
  975. return ["code"=>1,"msg"=>"提现申请提交成功"];
  976. } catch (\Exception $e){
  977. $this->dbNewAgent->rollBack();
  978. $this->cache->set($key,null);
  979. return ["code"=>1,"msg"=>"提现申请提交失败"];
  980. }
  981. }
  982. /**
  983. * 获取提现记录
  984. * @param type $customerId
  985. * @param type $parms
  986. * @return type
  987. */
  988. public function getPartnerApplyCashList($customerId,$parms){
  989. $where=[];
  990. if(!empty($parms["where"])){
  991. $where= $parms["where"];
  992. }
  993. if(!empty($customerId)){
  994. $where["customer_id"] = $customerId;
  995. }
  996. $data = $this->dbNewAgentCash->select($where, "*", "id desc", $parms["limit"], $parms["offset"]);
  997. $total = $this->dbNewAgentCash->count($where);
  998. if(empty($data)){
  999. $data = [];
  1000. }
  1001. $return = [
  1002. 'data' => $data,
  1003. 'total' => (isset($total)&&!empty($total)) ? intval($total) : 0,
  1004. ];
  1005. return $return;
  1006. }
  1007. /**
  1008. * 获取提现详情
  1009. */
  1010. public function getPartnerApplyCashInfo($customerId,$parms){
  1011. $where=[];
  1012. if(!empty($parms["where"])){
  1013. $where= $parms["where"];
  1014. }
  1015. if(!empty($customerId)){
  1016. $where["customer_id"] = $customerId;
  1017. }
  1018. $data = $this->dbNewAgentCash->get($where);
  1019. return $data;
  1020. }
  1021. /**
  1022. * 获取门店代理列表
  1023. * @param type $params
  1024. */
  1025. public function getPartnerList($params = []){
  1026. $customerTableName = 'qianniao_customer_'.$this->enterpriseId;
  1027. $userCenterTableName = 'qianniao_user_center';
  1028. $newCommissionPartnerTableName = 'qianniao_new_agent_'.$this->enterpriseId;
  1029. //查询数据
  1030. $field = "p.*,u.mobile,c.name,c.avatar";
  1031. $sql = "select {$field} from {$newCommissionPartnerTableName} p "
  1032. . "left join {$userCenterTableName} u on u.id = p.userCenterId "
  1033. . "left join {$customerTableName} c on c.id = p.customerId";
  1034. $whereSql = ' where p.deleteStatus = 5 ';
  1035. if(isset($params['search'])){
  1036. $whereSql .= ' and (u.mobile like "%'.$params['search'].'%" or c.name like "%'.$params['search'].'%") ';
  1037. }
  1038. $orderSql = ' order by p.id desc ';
  1039. $limitSql = ' limit '.$params['offset'].','.$params['limit'];
  1040. $querySql = $sql.$whereSql.$orderSql.$limitSql;
  1041. $data = $this->dbNewAgent->query($querySql);
  1042. if(empty($data)){
  1043. $data=[];
  1044. }
  1045. //查询总数
  1046. $countSql = "select count(*) as `count` from {$newCommissionPartnerTableName} p "
  1047. . "left join {$userCenterTableName} u on u.id = p.userCenterId "
  1048. . "left join {$customerTableName} c on c.id = p.customerId";
  1049. $countData = $this->dbNewAgent->query($countSql.$whereSql);
  1050. $total = 0;
  1051. if(!empty($countData)){
  1052. $total =array_shift($countData)['count'];
  1053. }
  1054. return ['data'=>$data,'total'=>($total) ? intval($total) : 0];
  1055. }
  1056. /**
  1057. * 获取门店代理余额明细列表
  1058. * @param type $params
  1059. */
  1060. public function getBalanceDetailList($params = []){
  1061. $customerTableName = 'qianniao_customer_'.$this->enterpriseId;
  1062. $userCenterTableName = 'qianniao_user_center';
  1063. $newCommissionBalanceDetailTableName = 'qianniao_new_agent_balance_detail_'.$this->enterpriseId;
  1064. //查询数据
  1065. $field = "p.*,u.mobile,c.name,c.avatar";
  1066. $sql = "select {$field} from {$newCommissionBalanceDetailTableName} p "
  1067. . "left join {$userCenterTableName} u on u.id = p.user_center_id "
  1068. . "left join {$customerTableName} c on c.id = p.customer_id";
  1069. $whereSql = ' where 1=1 ';
  1070. if(!empty($params['agentId'])){
  1071. $whereSql .= ' and p.agent_id = '.$params['agentId'];
  1072. }
  1073. if(isset($params['search'])){
  1074. $whereSql .= ' and (u.mobile like "%'.$params['search'].'%" or c.name like "%'.$params['search'].'%") ';
  1075. }
  1076. $orderSql = ' order by p.id desc ';
  1077. $limitSql = ' limit '.$params['offset'].','.$params['limit'];
  1078. $querySql = $sql.$whereSql.$orderSql.$limitSql;
  1079. $data = $this->dbNewAgent->query($querySql);
  1080. if(empty($data)){
  1081. $data=[];
  1082. }
  1083. //查询总数
  1084. $countSql = "select count(*) as `count` from {$newCommissionBalanceDetailTableName} p "
  1085. . "left join {$userCenterTableName} u on u.id = p.user_center_id "
  1086. . "left join {$customerTableName} c on c.id = p.customer_id";
  1087. $countData = $this->dbNewAgent->query($countSql.$whereSql);
  1088. $total = 0;
  1089. if(!empty($countData)){
  1090. $total =array_shift($countData)['count'];
  1091. }
  1092. return ['data'=>$data,'total'=>($total) ? intval($total) : 0];
  1093. }
  1094. /**
  1095. * 获取门店代理佣金记录
  1096. * @param type $params
  1097. */
  1098. public function getDetailList($params = []){
  1099. $customerTableName = 'qianniao_customer_'.$this->enterpriseId;
  1100. $userCenterTableName = 'qianniao_user_center';
  1101. $newCommissionDetailTableName = 'qianniao_new_agent_detail_'.$this->enterpriseId;
  1102. $shopTableName = "qianniao_shop_1";
  1103. //查询数据
  1104. $field = "p.*,u.mobile,c.name,c.avatar,s.name as sourceName ";
  1105. $sql = "select {$field} from {$newCommissionDetailTableName} p "
  1106. . "left join {$userCenterTableName} u on u.id = p.userCenterId "
  1107. . "left join {$customerTableName} c on c.id = p.customerId "
  1108. . "left join {$shopTableName} s on s.id = p.sourceShopId";
  1109. $whereSql = ' where 1=1 ';
  1110. if(!empty($params['agentId'])){
  1111. $whereSql .= ' and p.agentId = '.$params['agentId'];
  1112. }
  1113. if(isset($params['search'])){
  1114. $whereSql .= ' and (u.mobile like "%'.$params['search'].'%" or c.name like "%'.$params['search'].'%") ';
  1115. }
  1116. $orderSql = ' order by p.id desc ';
  1117. $limitSql = ' limit '.$params['offset'].','.$params['limit'];
  1118. $querySql = $sql.$whereSql.$orderSql.$limitSql;
  1119. $data = $this->dbNewAgent->query($querySql);
  1120. if(empty($data)){
  1121. $data=[];
  1122. }
  1123. //查询总数
  1124. $countSql = "select count(*) as `count` from {$newCommissionDetailTableName} p "
  1125. . "left join {$userCenterTableName} u on u.id = p.userCenterId "
  1126. . "left join {$customerTableName} c on c.id = p.customerId "
  1127. . "left join {$shopTableName} s on s.id = p.sourceShopId";
  1128. $countData = $this->dbNewAgent->query($countSql.$whereSql);
  1129. $total = 0;
  1130. if(!empty($countData)){
  1131. $total =array_shift($countData)['count'];
  1132. }
  1133. return ['data'=>$data,'total'=>($total) ? intval($total) : 0];
  1134. }
  1135. /**
  1136. * 获取提现记录
  1137. * @param type $params
  1138. */
  1139. public function getPartnerCashList($params = []){
  1140. $customerTableName = 'qianniao_customer_'.$this->enterpriseId;
  1141. $userCenterTableName = 'qianniao_user_center';
  1142. $newCommissionCashTableName = 'qianniao_new_agent_cash_'.$this->enterpriseId;
  1143. //查询数据
  1144. $field = "p.*,u.mobile,c.name,c.avatar ";
  1145. $sql = "select {$field} from {$newCommissionCashTableName} p "
  1146. . "left join {$userCenterTableName} u on u.id = p.user_center_id "
  1147. . "left join {$customerTableName} c on c.id = p.customer_id ";
  1148. $whereSql = ' where 1=1 ';
  1149. if(!empty($params['agentId'])){
  1150. $whereSql .= ' and p.agent_id = '.$params['agentId'];
  1151. }
  1152. if(isset($params['search'])){
  1153. $whereSql .= ' and (u.mobile like "%'.$params['search'].'%" or c.name like "%'.$params['search'].'%") ';
  1154. }
  1155. if(isset($params['status'])){
  1156. $whereSql .= ' and p.status = '.$params['status'];
  1157. }
  1158. if(!empty($params['bank_type_code'])){
  1159. $whereSql .= " and p.bank_type_code = '{$params['bank_type_code']}'";
  1160. }
  1161. if(!empty($params['start_time']) && !empty($params['end_time'])){
  1162. $whereSql .= " and p.time >= {$params['start_time']} and p.time < {$params['end_time']}";
  1163. }
  1164. $orderSql = ' order by p.id desc ';
  1165. $limitSql = ' limit '.$params['offset'].','.$params['limit'];
  1166. $querySql = $sql.$whereSql.$orderSql.$limitSql;
  1167. $data = $this->dbNewAgent->query($querySql);
  1168. if(empty($data)){
  1169. $data=[];
  1170. }
  1171. //查询总数
  1172. $countSql = "select count(*) as `count` from {$newCommissionCashTableName} p "
  1173. . "left join {$userCenterTableName} u on u.id = p.user_center_id "
  1174. . "left join {$customerTableName} c on c.id = p.customer_id ";
  1175. $countData = $this->dbNewAgent->query($countSql.$whereSql);
  1176. $total = 0;
  1177. if(!empty($countData)){
  1178. $total =array_shift($countData)['count'];
  1179. }
  1180. return ['data'=>$data,'total'=>($total) ? intval($total) : 0];
  1181. }
  1182. /**
  1183. * 提现处理
  1184. * @param type $id
  1185. * @param type $mono
  1186. * @param type $type
  1187. */
  1188. public function partnerCashAudit($id,$mono,$type,$audit_id=0){
  1189. if(empty($id)|| empty($mono) || empty($type) || !in_array($type, [1,-1])){
  1190. return ["code"=>-1,"msg"=>"参数错误"];
  1191. }
  1192. $adminData = $this->dbUserCenter->get($this->onlineUserId);
  1193. if(empty($adminData)){
  1194. $adminData=["audit_name"=>"管理人员"];
  1195. }
  1196. //频繁操作限制
  1197. $key = "agentCashDealCache_".$id.'::'.$this->enterpriseId;
  1198. $result = $this->cache->get($key);
  1199. if(!empty($result)){
  1200. return ["code"=>-1,"msg"=>"处理中,请耐心等待!"];
  1201. }
  1202. $this->cache->set($key,1,5);
  1203. $where = ["id"=>$id,"status"=>0];
  1204. $data = $this->dbNewAgentCash->get($where);
  1205. if(empty($data)){
  1206. $this->cache->set($key,null);
  1207. return ["code"=>-1,"msg"=>"数据不存在"];
  1208. }
  1209. //门店代理账户信息
  1210. $agentData = $this->dbNewAgent->get(["id"=>$data["agent_id"]]);
  1211. if(empty($agentData)){
  1212. $this->cache->set($key,null);
  1213. return ["code"=>-1,"msg"=>"门店代理账户不存在"];
  1214. }
  1215. //打款成功
  1216. if($type==1){
  1217. $res = $this->dbNewAgentCash->update([
  1218. "status"=>1,
  1219. "audit_time"=>time(),
  1220. "audit_mono"=>$mono,
  1221. "audit_id"=>$audit_id,
  1222. "audit_name"=>$adminData["mobile"],
  1223. ], $where);
  1224. $this->cache->set($key,null);
  1225. if(empty($res)){
  1226. return ["code"=>-1,"msg"=>"系统繁忙,请稍后重试001!"];
  1227. }
  1228. return ["code"=>1,"msg"=>"处理完成"];
  1229. }
  1230. //审核驳回
  1231. try{
  1232. $this->dbNewAgentCash->beginTransaction();
  1233. $res = $this->dbNewAgentCash->update([
  1234. "status"=>-1,
  1235. "audit_time"=>time(),
  1236. "audit_mono"=>$mono,
  1237. "audit_id"=>$audit_id,
  1238. "audit_name"=>$adminData["mobile"],
  1239. ], $where);
  1240. if(empty($res)){
  1241. $this->dbNewAgentCash->rollBack();
  1242. $this->cache->set($key,null);
  1243. return ["code"=>-1,"msg"=>"操作失败001!"];
  1244. }
  1245. //添加账户余额明细记录
  1246. $dres = $this->dbNewAgentBalanceDetail->insert([
  1247. "type"=>1,
  1248. "title"=>"提现驳回返还余额",
  1249. "code"=>"cash_no_balance",
  1250. "money"=>$data["money"],
  1251. "content"=>"提现驳回返还余额:".$mono,
  1252. "admin_id"=>$audit_id,
  1253. "customer_id"=>$agentData["customerId"],
  1254. "agent_id"=>$agentData["id"],
  1255. "user_center_id"=>$agentData["userCenterId"],
  1256. "cash_id"=>$data["id"],
  1257. "old_balance"=>$agentData["balance"],
  1258. "now_balance"=>$agentData["balance"]+$data["money"],
  1259. "time"=>time(),
  1260. "expand"=>"",
  1261. "detail_id"=>0,
  1262. ]);
  1263. if(empty($dres)){
  1264. $this->dbNewAgentCash->rollBack();
  1265. $this->cache->set($key,null);
  1266. return ["code"=>-1,"msg"=>"操作失败002!"];
  1267. }
  1268. //更新账户数据
  1269. $withdrawRes = $this->dbNewAgent->set_dec("withdraw", ["id"=>$agentData["id"]], $data["money"]);
  1270. if(empty($withdrawRes)){
  1271. $this->dbNewAgentCash->rollBack();
  1272. $this->cache->set($key,null);
  1273. return ["code"=>-1,"msg"=>"操作失败003"];
  1274. }
  1275. $balanceRes = $this->dbNewAgent->set_inc("balance", ["id"=>$agentData["id"]], $data["money"]);
  1276. if(empty($balanceRes)){
  1277. $this->dbNewAgentCash->rollBack();
  1278. $this->cache->set($key,null);
  1279. return ["code"=>-1,"msg"=>"操作失败004"];
  1280. }
  1281. //提交事务
  1282. $this->dbNewAgentCash->commit();
  1283. $this->cache->set($key,null);
  1284. return ["code"=>1,"msg"=>"提现申请处理成功!"];
  1285. } catch (\Exception $e){
  1286. $this->dbNewAgentCash->rollBack();
  1287. $this->cache->set($key,null);
  1288. return ["code"=>-1,"msg"=>"系统繁忙,请稍后重试!"];
  1289. }
  1290. }
  1291. /**
  1292. * 获取设置信息
  1293. * @param type $enterpriseId
  1294. * @return string
  1295. */
  1296. public static function getCommissionSettingData($enterpriseId){
  1297. $dbSetting = new DNewAgentSetting('default');
  1298. $data = $dbSetting->get(["enterpriseId"=>$enterpriseId]);
  1299. if(empty($data)){
  1300. $data=[
  1301. "id"=>0,
  1302. "enterpriseId"=>$enterpriseId,
  1303. "levMoney"=>0,
  1304. "levPer"=>0,
  1305. "incomePer"=>0,
  1306. "is_open"=>0,
  1307. "bankData"=>"",
  1308. "oneToTwoNum"=>0,
  1309. "twoToThreeNum"=>0,
  1310. "oneonePer"=>0,
  1311. "oneTwoPer"=>0,
  1312. "twoOnePer"=>0,
  1313. "twoTwoPer"=>0,
  1314. "threePer"=>0,
  1315. ];
  1316. }
  1317. return $data;
  1318. }
  1319. /**
  1320. * 推荐代理分佣设置
  1321. * @param type $enterpriseId
  1322. * @param type $parms
  1323. * @return type
  1324. */
  1325. public static function setCommissionSettingData($enterpriseId,$parms){
  1326. if(empty($enterpriseId) || empty($parms)){
  1327. return ["code"=>"-1","msg"=>"参数错误"];
  1328. }
  1329. $dbSetting = new DNewAgentSetting('default');
  1330. $data = $dbSetting->get(["enterpriseId"=>$enterpriseId]);
  1331. $parms["levMoney"] = empty($parms["levMoney"]) ? 0 : $parms["levMoney"];
  1332. $parms["levPer"] = empty($parms["levPer"]) ? 0 : $parms["levPer"];
  1333. $parms["incomePer"] = empty($parms["incomePer"])? 0 : $parms["incomePer"];
  1334. $parms["is_open"] = empty($parms["is_open"]) ? 0 : 1;
  1335. // if(!is_numeric($parms["levMoney"]) || $parms["levMoney"]<=0){
  1336. // return ["code"=>"-1","msg"=>"升级门店代理金额必须大于0"];
  1337. // }
  1338. // if(!is_numeric($parms["levPer"]) || $parms["levPer"]<0 || $parms["levPer"]>=1){
  1339. // return ["code"=>"-1","msg"=>"子级升级门店代理消费佣金比例必须0到1之间"];
  1340. // }
  1341. if(!is_numeric($parms["oneonePer"]) || $parms["oneonePer"]<0 || $parms["oneonePer"]>=1){
  1342. return ["code"=>"-1","msg"=>"佣金比例必须0到1之间"];
  1343. }
  1344. if(!is_numeric($parms["oneTwoPer"]) || $parms["oneTwoPer"]<0 || $parms["oneTwoPer"]>=1){
  1345. return ["code"=>"-1","msg"=>"佣金比例必须0到1之间"];
  1346. }
  1347. if(!is_numeric($parms["twoOnePer"]) || $parms["twoOnePer"]<0 || $parms["twoOnePer"]>=1){
  1348. return ["code"=>"-1","msg"=>"佣金比例必须0到1之间"];
  1349. }
  1350. if(!is_numeric($parms["threePer"]) || $parms["threePer"]<0 || $parms["threePer"]>=1){
  1351. return ["code"=>"-1","msg"=>"佣金比例必须0到1之间"];
  1352. }
  1353. $saveData = [
  1354. "enterpriseId" => $enterpriseId,
  1355. "levMoney" => $parms["levMoney"],
  1356. "levPer" => $parms["levPer"],
  1357. "incomePer" => $parms["incomePer"],
  1358. "is_open" => $parms["is_open"],
  1359. "oneToTwoNum" => $parms["oneToTwoNum"]?:0,
  1360. "twoToThreeNum"=> $parms["twoToThreeNum"]?:0,
  1361. "oneonePer" => $parms["oneonePer"]?:0,
  1362. "oneTwoPer" => $parms["oneTwoPer"]?:0,
  1363. "twoOnePer" => $parms["twoOnePer"]?:0,
  1364. "twoTwoPer" => $parms["twoTwoPer"]?:0,
  1365. "threePer" => $parms["threePer"]?:0,
  1366. "updateTime" => time()
  1367. ];
  1368. $res = false;
  1369. if(empty($data)){
  1370. $saveData["enterpriseId"] = $enterpriseId;
  1371. $saveData["time"] = time();
  1372. $res = $dbSetting->insert($saveData);
  1373. }else{
  1374. $res = $dbSetting->update($saveData, ["id"=>$data["id"]]);
  1375. }
  1376. if(empty($res)){
  1377. return ["code"=>"-1","msg"=>"系统繁忙,请稍后重试"];
  1378. }
  1379. return ["code"=>1,"msg"=>"配置成功"];
  1380. }
  1381. public function getCustomerDataByUserCenterId($userCenterId){
  1382. if(empty($userCenterId)){
  1383. return false;
  1384. }
  1385. $data = $this->dbCustomer->get(["userCenterId"=>$userCenterId]);
  1386. if(empty($data)){
  1387. return false;
  1388. }
  1389. return $data;
  1390. }
  1391. }