AgentTools.Class.php 61 KB

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