AgentTools.Class.php 59 KB

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