AgentTools.Class.php 61 KB

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