AgentTools.Class.php 61 KB

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