AgentTools.Class.php 60 KB

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