AgentTools.Class.php 59 KB

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