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