PensionTasks.php 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\command;
  4. use app\common\model\merchant\order\OrderMerchant;
  5. use app\common\model\store\order\StoreOrder;
  6. use app\common\model\user\User;
  7. use app\common\model\user\UserCertification;
  8. use app\common\repositories\merchant\order\OrderGzcRepository;
  9. use app\common\repositories\merchant\order\OrderMerchantRepository;
  10. use app\common\repositories\store\order\StoreOrderStatusRepository;
  11. use app\common\repositories\store\product\ProductRepository;
  12. use app\common\repositories\user\UserBillRepository;
  13. use app\traits\GzcApiRequest;
  14. use Carbon\Carbon;
  15. use think\console\Command;
  16. use think\console\Input;
  17. use think\console\Output;
  18. use think\Exception;
  19. use think\facade\Db;
  20. use think\facade\Log;
  21. use app\common\repositories\store\order\StoreOrderRepository;
  22. /**
  23. * Class PensionTasks
  24. * @package app\command
  25. * @author: php迷途小书童
  26. * @created: 2021/3/19 9:14
  27. */
  28. class PensionTasks extends Command
  29. {
  30. use GzcApiRequest;
  31. protected function configure()
  32. {
  33. // 指令配置
  34. $this->setName('pensiontask')
  35. ->setDescription('the pensiontask command');
  36. }
  37. protected function execute(Input $input, Output $output)
  38. {
  39. //1线下2在线3拼多多4京东5苏宁6淘宝7VIP
  40. $nowTime = time();
  41. try {
  42. //线下符合条件的养老金收集(线下交易后第8天)
  43. $this->doOfflineOrderGzc($nowTime, $timeLimit = 1);
  44. //在线订单符合条件的养老金收集(线上收货后第8天)
  45. $this->doOnlineOrderGzc($timeLimit = 8);
  46. //在线订单符合条件的话费养老金收集(话费到账后第1天)
  47. $this->doOnlineHuuafeiOrderGzc($timeLimit = 1);
  48. //第三方订单符合条件的养老金收集(三方结算后第2天)
  49. $this->doThridPddOrderGzc($nowTime, $timeLimit = 2);
  50. $this->doThridJdOrderGzc($nowTime, $timeLimit = 2);
  51. $this->doThridSnOrderGzc($nowTime, $timeLimit = 2);
  52. $this->doThridTaobaoOrderGzc($nowTime, $timeLimit = 2);
  53. $this->doThridVipOrderGzc($nowTime, $timeLimit = 2);
  54. $this->doThridDyOrderGzc($nowTime, $timeLimit = 2);
  55. $this->doThridHfOrderGzc($nowTime, $timeLimit = 2);
  56. //升级
  57. // $this -> doUpgrade();
  58. } catch (\Throwable $e) {
  59. Log::info('公证处入账:' . $e->getFile() . ':' . $e->getLine() . '【' . $e->getMessage() . '】');
  60. var_dump('公证处入账:' . $e->getFile() . ':' . $e->getLine() . '【' . $e->getMessage() . '】');
  61. }
  62. // 指令输出
  63. $output->writeln('pensiontask');
  64. }
  65. /**
  66. * 线下支付订单公证处IN10入账
  67. * @param $nowTime
  68. * @param $timeLimit
  69. * author: php迷途小书童
  70. * created: 2021/3/20 9:26
  71. */
  72. public function doOfflineOrderGzc($nowTime, $timeLimit)
  73. {
  74. $cursor = Db::table('rrx_order_merchant')
  75. ->whereNotNull('pay_time')
  76. ->where('paid', 1)
  77. ->where('gzc', 0)
  78. ->whereTime('pay_time', '<', $nowTime - bcmul((string)$timeLimit, (string)86400))
  79. ->cursor();
  80. $OrderGzcRepository = app()->make(OrderGzcRepository::class);
  81. $OrderMerchantRepository = app()->make(OrderMerchantRepository::class);
  82. $StoreOrderRepository = app()->make(StoreOrderRepository::class);
  83. foreach ($cursor as $offline) {
  84. if ($offline['mer_id'] == 496) {
  85. continue;
  86. }
  87. $uid = 0;
  88. switch ($offline['trade_type']) {
  89. case 'WX':
  90. $uid = User::getInstance()->where('wechat_user_id', $offline['pay_user_id'])->value('uid');
  91. $accountType = 3;
  92. break;
  93. case 'ALI':
  94. $uid = User::getInstance()->where('ali_user_id', $offline['pay_user_id'])->value('uid');
  95. $accountType = 2;
  96. break;
  97. default:
  98. break;
  99. }
  100. if (!$uid) {
  101. Log::info($offline['pay_user_id'] . '未绑定用户');
  102. continue;
  103. }
  104. $certification = UserCertification::getInstance()->where('uid', $uid)->find();
  105. if (!$certification) {
  106. Log::info($offline['pay_user_id'] . '未实名认证');
  107. continue;
  108. }
  109. //查询养老金金额
  110. $UserBillRepository = app()->make(UserBillRepository::class);
  111. $bill = $UserBillRepository->getWhere(['uid' => $uid, 'link_id' => $offline['id'], 'order_sn' => $offline['out_trade_no'], 'title' => '养老金']);
  112. // if ($bill->status == 0) {
  113. // Log::info($offline['out_trade_no'] . 'pension未交年费');
  114. // continue;
  115. // }
  116. $pension = $bill->number ?? 0;
  117. if ($pension <= 0) {
  118. Log::info($offline['out_trade_no'] . 'pension' . $pension);
  119. continue;
  120. }
  121. Db::transaction(function () use ($uid, $certification, $accountType, $offline, $OrderGzcRepository, $OrderMerchantRepository, $pension, $StoreOrderRepository) {
  122. $order = OrderMerchant::getInstance()->find($offline['id']);
  123. $order->gzc = 1;
  124. $order->is_settlement = 1;
  125. $order->settlement_time = date('Y-m-d H:i:s');
  126. $order->save();
  127. //新增入账明细记录
  128. $orderId = $OrderMerchantRepository->getNewOrderId('IN10');
  129. $create = [
  130. 'uid' => $uid,
  131. 'platform_no' => $orderId,
  132. 'account_type' => $accountType,
  133. 'user_name' => $certification['user_name'],
  134. 'mobile' => $certification['mobile'],
  135. 'user_card' => $certification['user_card'],
  136. 'pension' => $pension,
  137. 'order_type' => 1,
  138. 'out_trade_no' => $offline['out_trade_no']
  139. ];
  140. $test_user = Db::name('user')->where('uid', $uid)->value('test_user');
  141. if ($test_user == 0) {
  142. $OrderGzcRepository->create($create);
  143. }
  144. // 养老金区域奖励
  145. $order_data = [
  146. 'pay_price' => $pension,
  147. 'user_address_ids' => $offline['user_address_ids'],
  148. 'uid' => $offline['uid'],
  149. 'order_sn' => $offline['out_trade_no']
  150. ];
  151. // $StoreOrderRepository->area_manage_details($order_data, 0, 2);
  152. });
  153. }
  154. }
  155. /**
  156. * 话费支付订单公证处IN10入账
  157. * @param $nowTime
  158. * @param $timeLimit
  159. * author: php迷途小书童
  160. * created: 2021/3/20 9:26
  161. */
  162. public function doOnlineHuuafeiOrderGzc($timeLimit)
  163. {
  164. // 普通订单
  165. $cursor = Db::table('rrx_order_huafei')->where('status', 3)->where('gzc', 0)->cursor();
  166. $StoreOrderStatusRepository = app()->make(StoreOrderStatusRepository::class);
  167. $OrderMerchantRepository = app()->make(OrderMerchantRepository::class);
  168. $OrderGzcRepository = app()->make(OrderGzcRepository::class);
  169. foreach ($cursor as $online) {
  170. if (time() < strtotime($online['finish_time']) + 3600 * 24) {
  171. Log::info('还没有话费到账后' . $timeLimit . '天');
  172. continue;
  173. }
  174. $certification = UserCertification::getInstance()->where('uid', $online['uid'])->find();
  175. if (!$certification) {
  176. Log::info($online['uid'] . '没有实名认证');
  177. continue;
  178. }
  179. //查询养老金金额
  180. $UserBillRepository = app()->make(UserBillRepository::class);
  181. $bill = $UserBillRepository->getWhere(['uid' => $online['uid'], 'order_sn' => $online['order_sn'], 'commission_type' => 5]);
  182. $pension = $bill->number ?? 0;
  183. if ($pension <= 0) {
  184. Log::info($online['order_sn'] . 'pension' . $pension);
  185. continue;
  186. }
  187. //首信易分账
  188. // $StoreOrderRepository = app()->make(StoreOrderRepository::class);
  189. // if ($online['pay_type'] > 0) {
  190. // $StoreOrderRepository->splitOrder($online);
  191. // }
  192. $StoreOrderRepository = [];
  193. Db::transaction(function () use ($certification, $online, $pension, $OrderGzcRepository, $OrderMerchantRepository, $StoreOrderRepository) {
  194. //新增入账明细记录
  195. $orderId = $OrderMerchantRepository->getNewOrderId('IN10');
  196. $create = [
  197. 'uid' => $online['uid'],
  198. 'platform_no' => $orderId,
  199. 'account_type' => 3,//账号类型1银行卡2支付宝3微信
  200. 'user_name' => $certification['user_name'],
  201. 'mobile' => $certification['mobile'],
  202. 'user_card' => $certification['user_card'],
  203. 'pension' => $pension,
  204. 'order_type' => 2,
  205. 'out_trade_no' => $online['order_sn']
  206. ];
  207. $test_user = Db::name('user')->where('uid', $online['uid'])->value('test_user');
  208. $check = Db::name("gzc_logs")->where("out_trade_no", $online['order_sn'])->where("order_type", 2)->count();
  209. if ($test_user == 0 && $check <= 0) {
  210. $OrderGzcRepository->create($create);
  211. }
  212. $order = StoreOrder::getInstance()->find($online['order_id']);
  213. $order->gzc = 1;
  214. $order->save();
  215. });
  216. }
  217. // 普通订单(红包,积分,京东,佣金,复购金,养老金,贡献值)
  218. $cursor = Db::table('rrx_order_huafei')->where('status', 3)->where('bill_status', 0)->cursor();
  219. $StoreOrderStatusRepository = app()->make(StoreOrderStatusRepository::class);
  220. $OrderMerchantRepository = app()->make(OrderMerchantRepository::class);
  221. $OrderGzcRepository = app()->make(OrderGzcRepository::class);
  222. foreach ($cursor as $online) {
  223. if (time() < strtotime($online['finish_time']) + 3600 * 24) {
  224. Log::info('还没有话费到账后' . $timeLimit . '天');
  225. continue;
  226. }
  227. Db::transaction(function () use ($online) {
  228. //订单状态改为已结算
  229. Db::name('rrx_order_huafei')
  230. ->where('status', 3)->where('bill_status', 0)
  231. ->where('order_sn', $online['order_sn'])
  232. ->update([
  233. 'bill_status'=> 1,
  234. 'bill_create_time' => time()
  235. ]);
  236. // 结算积分
  237. $user_sign_score_list = Db::name('user_sign_score')
  238. ->where('status', -1)
  239. ->where('order_id', $online['order_sn'])
  240. ->whereIn('type', [1, 2])
  241. ->select()
  242. ->toArray();
  243. foreach ($user_sign_score_list as $user_sign_score) {
  244. // 修改积分
  245. Db::name('user')
  246. ->where('uid', $user_sign_score['uid'])
  247. ->inc('score', (float)$user_sign_score['reward_socre'])
  248. ->update();
  249. Db::name('user_sign_score')
  250. ->where('status', -1)
  251. ->where('id', $user_sign_score['id'])
  252. ->update([
  253. 'status' => 1,
  254. 'settlement_time' => date('Y-m-d H:i:s')
  255. ]);
  256. }
  257. // 结算复购金
  258. $user_sign_fugou_list = Db::name('user_sign_fugou')
  259. ->where('status', -1)
  260. ->where('order_id', $online['order_sn'])
  261. ->whereIn('type', [1, 2])
  262. ->select()
  263. ->toArray();
  264. foreach ($user_sign_fugou_list as $user_sign_fugou) {
  265. // 修改复购金
  266. Db::name('user')
  267. ->where('uid', $user_sign_fugou['uid'])
  268. ->inc('fugou', (float)$user_sign_fugou['number'])
  269. ->update();
  270. Db::name('user_sign_fugou')
  271. ->where('status', -1)
  272. ->where('id', $user_sign_fugou['id'])
  273. ->update([
  274. 'status' => 1,
  275. 'settlement_time' => date('Y-m-d H:i:s')
  276. ]);
  277. }
  278. // 结算贡献值
  279. $user_sign_gongxian_list = Db::name('user_sign_gongxian')
  280. ->where('status', -1)
  281. ->where('order_id', $online['order_sn'])
  282. ->whereIn('type', [1, 2])
  283. ->select()
  284. ->toArray();
  285. foreach ($user_sign_gongxian_list as $user_sign_gongxian) {
  286. // 修改贡献值
  287. Db::name('user')
  288. ->where('uid', $user_sign_gongxian['uid'])
  289. ->inc('contribute', (float)$user_sign_gongxian['number'])
  290. ->update();
  291. Db::name('user_sign_gongxian')
  292. ->where('status', -1)
  293. ->where('id', $user_sign_gongxian['id'])
  294. ->update([
  295. 'status' => 1,
  296. 'settlement_time' => date('Y-m-d H:i:s')
  297. ]);
  298. }
  299. });
  300. }
  301. }
  302. /**
  303. * 在线支付公证处IN10入账 (红包,积分,京东,佣金,复购金,养老金,贡献值)
  304. * @param $nowTime
  305. * @param $timeLimit
  306. * author: php迷途小书童
  307. * created: 2021/3/20 9:25
  308. */
  309. public function doOnlineOrderGzc($timeLimit)
  310. {
  311. // 普通订单
  312. $cursor = Db::table('rrx_store_order')->where('paid', 1)->where('gzc', 0)->where('pay_type', 'in', '0,1,2,4')->cursor();
  313. $StoreOrderStatusRepository = app()->make(StoreOrderStatusRepository::class);
  314. $OrderMerchantRepository = app()->make(OrderMerchantRepository::class);
  315. $OrderGzcRepository = app()->make(OrderGzcRepository::class);
  316. foreach ($cursor as $online) {
  317. $status = $StoreOrderStatusRepository->getWhere(['order_id' => $online['order_id'], 'change_type' => 'take']);
  318. if ($online['mer_id'] == 496) {
  319. if (!in_array($online['status'], [0, 1, 2, 3])) {
  320. continue;
  321. }
  322. if (time() < strtotime($online['pay_time']) + 3600 * 24) {
  323. Log::info('还没有到收货后' . $timeLimit . '天');
  324. continue;
  325. }
  326. } else {
  327. if (!in_array($online['status'], [2, 3])) {
  328. continue;
  329. }
  330. if (Carbon::now()->modify("-{$timeLimit} days")->lt($status->change_time ?? $status['change_time'])) {
  331. Log::info('还没有到收货后' . $timeLimit . '天');
  332. continue;
  333. }
  334. }
  335. $certification = UserCertification::getInstance()->where('uid', $online['uid'])->find();
  336. if (!$certification) {
  337. Log::info($online['uid'] . '没有实名认证');
  338. continue;
  339. }
  340. //查询养老金金额
  341. $UserBillRepository = app()->make(UserBillRepository::class);
  342. $bill = $UserBillRepository->getWhere(['uid' => $online['uid'], 'link_id' => $online['order_id'], 'order_sn' => $online['order_sn'], 'commission_type' => 5]);
  343. $pension = $bill->number ?? 0;
  344. if ($pension <= 0) {
  345. Log::info($online['order_sn'] . 'pension' . $pension);
  346. continue;
  347. }
  348. //首信易分账
  349. $StoreOrderRepository = app()->make(StoreOrderRepository::class);
  350. if ($online['pay_type'] > 0) {
  351. $StoreOrderRepository->splitOrder($online);
  352. }
  353. Db::transaction(function () use ($certification, $online, $pension, $OrderGzcRepository, $OrderMerchantRepository, $StoreOrderRepository) {
  354. //新增入账明细记录
  355. $orderId = $OrderMerchantRepository->getNewOrderId('IN10');
  356. $create = [
  357. 'uid' => $online['uid'],
  358. 'platform_no' => $orderId,
  359. 'account_type' => $online['pay_type'] == 1 ? 3 : 2,
  360. 'user_name' => $certification['user_name'],
  361. 'mobile' => $certification['mobile'],
  362. 'user_card' => $certification['user_card'],
  363. 'pension' => $pension,
  364. 'order_type' => 2,
  365. 'out_trade_no' => $online['order_sn']
  366. ];
  367. $test_user = Db::name('user')->where('uid', $online['uid'])->value('test_user');
  368. $check = Db::name("gzc_logs")->where("out_trade_no", $online['order_sn'])->where("order_type", 2)->count();
  369. if ($test_user == 0 && $check <= 0) {
  370. $OrderGzcRepository->create($create);
  371. }
  372. $order = StoreOrder::getInstance()->find($online['order_id']);
  373. $order->gzc = 1;
  374. $order->save();
  375. // 养老金区域奖励
  376. // $order_data = [
  377. // 'pay_price' => $pension,
  378. // 'user_address_ids' => $online['user_address_ids'],
  379. // 'uid' => $online['uid'],
  380. // 'order_sn' => $online['order_sn']
  381. // ];
  382. // $StoreOrderRepository->area_manage_details($order_data, 8, 2);
  383. });
  384. }
  385. // 普通订单(红包,积分,京东,佣金,复购金,养老金,贡献值)
  386. $cursor = Db::table('rrx_store_order')->where('paid', 1)->where('is_settlement', 0)->where('pay_type', 'in', '0,1,2,4')->cursor();
  387. $StoreOrderStatusRepository = app()->make(StoreOrderStatusRepository::class);
  388. $OrderMerchantRepository = app()->make(OrderMerchantRepository::class);
  389. $OrderGzcRepository = app()->make(OrderGzcRepository::class);
  390. foreach ($cursor as $online) {
  391. $status = $StoreOrderStatusRepository->getWhere(['order_id' => $online['order_id'], 'change_type' => 'take']);
  392. if ($online['mer_id'] == 496) {
  393. if (!in_array($online['status'], [0, 1, 2, 3])) {
  394. continue;
  395. }
  396. if (time() < strtotime($online['pay_time']) + 3600 * 24) {
  397. Log::info('还没有到收货后' . $timeLimit . '天');
  398. continue;
  399. }
  400. } else {
  401. if (!in_array($online['status'], [2, 3])) {
  402. continue;
  403. }
  404. if (Carbon::now()->modify("-{$timeLimit} days")->lt($status->change_time ?? $status['change_time'])) {
  405. Log::info('还没有到收货后' . $timeLimit . '天');
  406. continue;
  407. }
  408. }
  409. $StoreOrderRepository = app()->make(StoreOrderRepository::class);
  410. Db::transaction(function () use ($online, $StoreOrderRepository) {
  411. $order = StoreOrder::getInstance()->find($online['order_id']);
  412. $order->is_settlement = 1;
  413. $order->settlement_time = date('Y-m-d H:i:s');
  414. $order->save();
  415. // 结算积分
  416. $user_sign_score_list = Db::name('user_sign_score')
  417. ->where('status', -1)
  418. ->where('order_id', $online['order_id'])
  419. ->whereIn('type', [1, 2])
  420. ->select()
  421. ->toArray();
  422. foreach ($user_sign_score_list as $user_sign_score) {
  423. // 修改积分
  424. Db::name('user')
  425. ->where('uid', $user_sign_score['uid'])
  426. ->inc('score', (float)$user_sign_score['reward_socre'])
  427. ->update();
  428. Db::name('user_sign_score')
  429. ->where('status', -1)
  430. ->where('id', $user_sign_score['id'])
  431. ->update([
  432. 'status' => 1,
  433. 'settlement_time' => date('Y-m-d H:i:s')
  434. ]);
  435. }
  436. // 结算红包
  437. $user_sign_hongbao_list = Db::name('user_sign_hongbao')
  438. ->where('status', -1)
  439. ->where('order_id', $online['order_id'])
  440. ->whereIn('type', [1, 2])
  441. ->select()
  442. ->toArray();
  443. foreach ($user_sign_hongbao_list as $user_sign_hongbao) {
  444. // 修改红包
  445. Db::name('user')
  446. ->where('uid', $user_sign_hongbao['uid'])
  447. ->inc('hongbao', (float)$user_sign_hongbao['number'])
  448. ->update();
  449. Db::name('user_sign_hongbao')
  450. ->where('status', -1)
  451. ->where('id', $user_sign_hongbao['id'])
  452. ->update([
  453. 'status' => 1,
  454. 'settlement_time' => date('Y-m-d H:i:s')
  455. ]);
  456. }
  457. // 结算京豆
  458. $user_sign_jingdou_list = Db::name('user_sign_jingdou')
  459. ->where('status', -1)
  460. ->where('order_id', $online['order_id'])
  461. ->whereIn('type', [1, 2])
  462. ->select()
  463. ->toArray();
  464. foreach ($user_sign_jingdou_list as $user_sign_jingdou) {
  465. // 修改京豆
  466. Db::name('user')
  467. ->where('uid', $user_sign_jingdou['uid'])
  468. ->inc('jingdou', (float)$user_sign_jingdou['number'])
  469. ->update();
  470. Db::name('user_sign_jingdou')
  471. ->where('status', -1)
  472. ->where('id', $user_sign_jingdou['id'])
  473. ->update([
  474. 'status' => 1,
  475. 'settlement_time' => date('Y-m-d H:i:s')
  476. ]);
  477. }
  478. // 结算复购金
  479. $user_sign_fugou_list = Db::name('user_sign_fugou')
  480. ->where('status', -1)
  481. ->where('order_id', $online['order_id'])
  482. ->whereIn('type', [1, 2])
  483. ->select()
  484. ->toArray();
  485. foreach ($user_sign_fugou_list as $user_sign_fugou) {
  486. // 修改复购金
  487. Db::name('user')
  488. ->where('uid', $user_sign_fugou['uid'])
  489. ->inc('fugou', (float)$user_sign_fugou['number'])
  490. ->update();
  491. Db::name('user_sign_fugou')
  492. ->where('status', -1)
  493. ->where('id', $user_sign_fugou['id'])
  494. ->update([
  495. 'status' => 1,
  496. 'settlement_time' => date('Y-m-d H:i:s')
  497. ]);
  498. }
  499. // 结算贡献值
  500. $user_sign_gongxian_list = Db::name('user_sign_gongxian')
  501. ->where('status', -1)
  502. ->where('order_id', $online['order_id'])
  503. ->whereIn('type', [1, 2])
  504. ->select()
  505. ->toArray();
  506. foreach ($user_sign_gongxian_list as $user_sign_gongxian) {
  507. // 修改贡献值
  508. Db::name('user')
  509. ->where('uid', $user_sign_gongxian['uid'])
  510. ->inc('contribute', (float)$user_sign_gongxian['number'])
  511. ->update();
  512. Db::name('user_sign_gongxian')
  513. ->where('status', -1)
  514. ->where('id', $user_sign_gongxian['id'])
  515. ->update([
  516. 'status' => 1,
  517. 'settlement_time' => date('Y-m-d H:i:s')
  518. ]);
  519. }
  520. // 结算佣金
  521. $user_bill_list = Db::name('user_bill')
  522. ->where('status', 0)
  523. ->where('commission_type','<>', 5)
  524. ->where([ 'link_id' => $online['order_id'], 'order_sn' => $online['order_sn']])
  525. ->select()
  526. ->toArray();
  527. foreach ($user_bill_list as $user_bill) {
  528. if ($user_bill['commission_type'] != 5) {
  529. if (in_array($user_bill['commission_type'], [7,12,19])) {
  530. Db::name("merchant")
  531. ->where("mer_id", $user_bill['mer_id'])
  532. ->inc('brokerage_price', (float)$user_bill['number'])
  533. ->update();
  534. } else {
  535. Db::name('user')
  536. ->where('uid', $user_bill['uid'])
  537. ->inc('brokerage_price', (float)$user_bill['number'])
  538. ->update();
  539. }
  540. }
  541. if ($online['pay_type'] == 0 && $user_bill['commission_type'] == 5) {
  542. } else {
  543. // 虚拟支付的养老金不修改不结算
  544. Db::name('user_bill')
  545. ->where('status', 0)
  546. ->where('bill_id', $user_bill['bill_id'])
  547. ->update([
  548. 'status' => 1,
  549. 'take_time' => time()
  550. ]);
  551. }
  552. if ($online['pay_type'] == 0 && $user_bill['commission_type'] == 5) {
  553. } else {
  554. // 虚拟支付的养老金不修改不结算
  555. Db::name('user_bill')
  556. ->where('status', 0)
  557. ->where('bill_id', $user_bill['bill_id'])
  558. ->update([
  559. 'status' => 1,
  560. 'take_time' => time()
  561. ]);
  562. }
  563. }
  564. //按角色结算
  565. $StoreOrderRepository->close_order($online['order_id']);
  566. });
  567. }
  568. }
  569. /**
  570. * 第三方订单拼多多IN10入账
  571. * @param $nowTime
  572. * @param $timeLimit
  573. * author: php迷途小书童
  574. * created: 2021/3/20 9:26
  575. */
  576. public function doThridPddOrderGzc($nowTime, $timeLimit)
  577. {
  578. $cursor = Db::table('rrx_order_pdd')
  579. ->where('gzc', 0)
  580. ->where('bill_status', 1)
  581. ->where('commission', '>', 0)
  582. ->whereTime('bill_end_time', '<', $nowTime - bcmul((string)$timeLimit, (string)86400))
  583. ->cursor();
  584. $ProductRepository = app()->make(ProductRepository::class);
  585. $OrderGzcRepository = app()->make(OrderGzcRepository::class);
  586. $OrderMerchantRepository = app()->make(OrderMerchantRepository::class);
  587. $StoreOrderRepository = app()->make(StoreOrderRepository::class);
  588. foreach ($cursor as $thrid) {
  589. $uid = $thrid['uid'];
  590. if (!$uid)
  591. continue;
  592. $certification = UserCertification::getInstance()->where('uid', $uid)->find();
  593. if (!$certification) {
  594. Log::info('用户ID' . $uid . '未实名认证');
  595. continue;
  596. }
  597. $pension = $ProductRepository->yanglaojin(0, sprintf('%.2f', $thrid['commission']));
  598. Db::transaction(function () use ($uid, $certification, $thrid, $pension, $OrderGzcRepository, $OrderMerchantRepository, $StoreOrderRepository) {
  599. //新增入账明细记录
  600. $orderId = $OrderMerchantRepository->getNewOrderId('IN10');
  601. $create = [
  602. 'uid' => $uid,
  603. 'platform_no' => $orderId,
  604. 'user_name' => $certification['user_name'],
  605. 'mobile' => $certification['mobile'],
  606. 'user_card' => $certification['user_card'],
  607. 'pension' => $pension,
  608. 'order_type' => 3,
  609. 'out_trade_no' => $thrid['order_sn']
  610. ];
  611. $test_user = Db::name('user')->where('uid', $uid)->value('test_user');
  612. if ($test_user == 0) {
  613. $OrderGzcRepository->create($create);
  614. }
  615. // $OrderGzcRepository->create($create);
  616. Db::table('rrx_order_pdd')
  617. ->update(['gzc' => 1, 'order_pdd_id' => $thrid['order_pdd_id']]);
  618. // 养老金区域奖励
  619. $order_data = [
  620. 'pay_price' => $pension,
  621. 'user_address_ids' => $thrid['user_address_ids'],
  622. 'uid' => $thrid['uid'],
  623. 'order_sn' => $thrid['order_sn']
  624. ];
  625. // $StoreOrderRepository->area_manage_details($order_data, 1, 2);
  626. });
  627. }
  628. }
  629. /**
  630. * 第三方订单京东IN10入账
  631. * @param $nowTime
  632. * @param $timeLimit
  633. * author: php迷途小书童
  634. * created: 2021/3/20 9:26
  635. */
  636. public function doThridJdOrderGzc($nowTime, $timeLimit)
  637. {
  638. $cursor = Db::table('rrx_order_jd')
  639. ->where('gzc', 0)
  640. ->where('bill_status', 1)
  641. ->where('commission', '>', 0)
  642. ->whereTime('bill_end_time', '<', $nowTime - bcmul((string)$timeLimit, (string)86400))
  643. ->cursor();
  644. // Db::name('log')->insert(['data'=>'abbbb入库语句'.$nowTime - bcmul((string)$timeLimit, (string)86400)]);
  645. // Db::name('log')->insert(['data'=>'abbbb'.json_encode($cursor)]);
  646. $ProductRepository = app()->make(ProductRepository::class);
  647. $OrderGzcRepository = app()->make(OrderGzcRepository::class);
  648. $OrderMerchantRepository = app()->make(OrderMerchantRepository::class);
  649. $StoreOrderRepository = app()->make(StoreOrderRepository::class);
  650. foreach ($cursor as $thrid) {
  651. $uid = $thrid['uid'];
  652. if (!$uid)
  653. continue;
  654. $certification = UserCertification::getInstance()->where('uid', $uid)->find();
  655. if (!$certification) {
  656. Log::info('用户ID' . $uid . '未实名认证');
  657. continue;
  658. }
  659. $pension = $ProductRepository->yanglaojin(0, sprintf('%.2f', $thrid['commission']));
  660. Db::transaction(function () use ($uid, $certification, $thrid, $pension, $OrderGzcRepository, $OrderMerchantRepository, $StoreOrderRepository) {
  661. //新增入账明细记录
  662. $orderId = $OrderMerchantRepository->getNewOrderId('IN10');
  663. $create = [
  664. 'uid' => $uid,
  665. 'platform_no' => $orderId,
  666. 'user_name' => $certification['user_name'],
  667. 'mobile' => $certification['mobile'],
  668. 'user_card' => $certification['user_card'],
  669. 'pension' => $pension,
  670. 'order_type' => 4,
  671. 'out_trade_no' => $thrid['order_sn']
  672. ];
  673. $test_user = Db::name('user')->where('uid', $uid)->value('test_user');
  674. if ($test_user == 0) {
  675. $OrderGzcRepository->create($create);
  676. }
  677. // $OrderGzcRepository->create($create);
  678. Db::table('rrx_order_jd')
  679. ->update(['gzc' => 1, 'order_jd_id' => $thrid['order_jd_id']]);
  680. // 养老金区域奖励
  681. $order_data = [
  682. 'pay_price' => $pension,
  683. 'user_address_ids' => $thrid['user_address_ids'],
  684. 'uid' => $thrid['uid'],
  685. 'order_sn' => $thrid['order_sn']
  686. ];
  687. // $StoreOrderRepository->area_manage_details($order_data, 6, 2);
  688. });
  689. }
  690. }
  691. /**
  692. * 第三方订单苏宁IN10入账
  693. * @param $nowTime
  694. * @param $timeLimit
  695. * author: php迷途小书童
  696. * created: 2021/3/20 9:26
  697. */
  698. public function doThridSnOrderGzc($nowTime, $timeLimit)
  699. {
  700. $cursor = Db::table('rrx_order_su')
  701. ->where('gzc', 0)
  702. ->where('bill_status', 1)
  703. ->where('commission', '>', 0)
  704. ->whereTime('bill_end_time', '<', $nowTime - bcmul((string)$timeLimit, (string)86400))
  705. ->cursor();
  706. $ProductRepository = app()->make(ProductRepository::class);
  707. $OrderGzcRepository = app()->make(OrderGzcRepository::class);
  708. $OrderMerchantRepository = app()->make(OrderMerchantRepository::class);
  709. $StoreOrderRepository = app()->make(StoreOrderRepository::class);
  710. foreach ($cursor as $thrid) {
  711. $uid = $thrid['uid'];
  712. if (!$uid)
  713. continue;
  714. $certification = UserCertification::getInstance()->where('uid', $uid)->find();
  715. if (!$certification) {
  716. Log::info('用户ID' . $uid . '未实名认证');
  717. continue;
  718. }
  719. $pension = $ProductRepository->yanglaojin(0, sprintf('%.2f', $thrid['commission']));
  720. Db::transaction(function () use ($uid, $certification, $thrid, $pension, $OrderGzcRepository, $OrderMerchantRepository, $StoreOrderRepository) {
  721. //新增入账明细记录
  722. $orderId = $OrderMerchantRepository->getNewOrderId('IN10');
  723. $create = [
  724. 'uid' => $uid,
  725. 'platform_no' => $orderId,
  726. 'user_name' => $certification['user_name'],
  727. 'mobile' => $certification['mobile'],
  728. 'user_card' => $certification['user_card'],
  729. 'pension' => $pension,
  730. 'order_type' => 5,
  731. 'out_trade_no' => $thrid['order_sn']
  732. ];
  733. $test_user = Db::name('user')->where('uid', $uid)->value('test_user');
  734. if ($test_user == 0) {
  735. $OrderGzcRepository->create($create);
  736. }
  737. // $OrderGzcRepository->create($create);
  738. Db::table('rrx_order_su')
  739. ->update(['gzc' => 1, 'order_su_id' => $thrid['order_su_id']]);
  740. // 养老金区域奖励
  741. $order_data = [
  742. 'pay_price' => $pension,
  743. 'user_address_ids' => $thrid['user_address_ids'],
  744. 'uid' => $thrid['uid'],
  745. 'order_sn' => $thrid['order_sn']
  746. ];
  747. // $StoreOrderRepository->area_manage_details($order_data, 3, 2);
  748. });
  749. }
  750. }
  751. /**
  752. * 第三方订单话费IN10入账
  753. * @param $nowTime
  754. * @param $timeLimit
  755. * author: php迷途小书童
  756. * created: 2021/3/20 9:26
  757. */
  758. public function doThridHfOrderGzc($nowTime, $timeLimit)
  759. {
  760. $cursor = Db::table('rrx_order_huafei')
  761. ->where('gzc', 0)
  762. ->where('bill_status', 1)
  763. ->where('commission', '>', 0)
  764. ->whereTime('bill_end_time', '<', $nowTime - bcmul((string)$timeLimit, (string)86400))
  765. ->cursor();
  766. $ProductRepository = app()->make(ProductRepository::class);
  767. $OrderGzcRepository = app()->make(OrderGzcRepository::class);
  768. $OrderMerchantRepository = app()->make(OrderMerchantRepository::class);
  769. foreach ($cursor as $thrid) {
  770. $uid = $thrid['uid'];
  771. if (!$uid)
  772. continue;
  773. $certification = UserCertification::getInstance()->where('uid', $uid)->find();
  774. if (!$certification) {
  775. Log::info('用户ID' . $uid . '未实名认证');
  776. continue;
  777. }
  778. $pension = $ProductRepository->yanglaojin(0, sprintf('%.2f', $thrid['commission']));
  779. Db::transaction(function () use ($uid, $certification, $thrid, $pension, $OrderGzcRepository, $OrderMerchantRepository) {
  780. //新增入账明细记录
  781. $orderId = $OrderMerchantRepository->getNewOrderId('IN10');
  782. $create = [
  783. 'uid' => $uid,
  784. 'platform_no' => $orderId,
  785. 'user_name' => $certification['user_name'],
  786. 'mobile' => $certification['mobile'],
  787. 'user_card' => $certification['user_card'],
  788. 'pension' => $pension,
  789. 'order_type' => 6,
  790. 'out_trade_no' => $thrid['order_sn']
  791. ];
  792. $test_user = Db::name('user')->where('uid', $uid)->value('test_user');
  793. if ($test_user == 0) {
  794. $OrderGzcRepository->create($create);
  795. }
  796. Db::table('rrx_order_huafei')->update(['gzc' => 1, 'order_tb_id' => $thrid['order_tb_id']]);
  797. });
  798. }
  799. }
  800. /**
  801. * 第三方订单淘宝IN10入账
  802. * @param $nowTime
  803. * @param $timeLimit
  804. * author: php迷途小书童
  805. * created: 2021/3/20 9:26
  806. */
  807. public function doThridTaobaoOrderGzc($nowTime, $timeLimit)
  808. {
  809. $cursor = Db::table('rrx_order_tb')
  810. ->where('gzc', 0)
  811. ->where('bill_status', 1)
  812. ->where('commission', '>', 0)
  813. ->whereTime('bill_end_time', '<', $nowTime - bcmul((string)$timeLimit, (string)86400))
  814. ->cursor();
  815. $ProductRepository = app()->make(ProductRepository::class);
  816. $OrderGzcRepository = app()->make(OrderGzcRepository::class);
  817. $OrderMerchantRepository = app()->make(OrderMerchantRepository::class);
  818. $StoreOrderRepository = app()->make(StoreOrderRepository::class);
  819. foreach ($cursor as $thrid) {
  820. $uid = $thrid['uid'];
  821. if (!$uid)
  822. continue;
  823. $certification = UserCertification::getInstance()->where('uid', $uid)->find();
  824. if (!$certification) {
  825. Log::info('用户ID' . $uid . '未实名认证');
  826. continue;
  827. }
  828. $pension = $ProductRepository->yanglaojin(0, sprintf('%.2f', $thrid['commission']));
  829. Db::transaction(function () use ($uid, $certification, $thrid, $pension, $OrderGzcRepository, $OrderMerchantRepository, $StoreOrderRepository) {
  830. //新增入账明细记录
  831. $orderId = $OrderMerchantRepository->getNewOrderId('IN10');
  832. $create = [
  833. 'uid' => $uid,
  834. 'platform_no' => $orderId,
  835. 'user_name' => $certification['user_name'],
  836. 'mobile' => $certification['mobile'],
  837. 'user_card' => $certification['user_card'],
  838. 'pension' => $pension,
  839. 'order_type' => 6,
  840. 'out_trade_no' => $thrid['order_sn']
  841. ];
  842. $test_user = Db::name('user')->where('uid', $uid)->value('test_user');
  843. if ($test_user == 0) {
  844. $OrderGzcRepository->create($create);
  845. }
  846. // $OrderGzcRepository->create($create);
  847. Db::table('rrx_order_tb')
  848. ->update(['gzc' => 1, 'order_tb_id' => $thrid['order_tb_id']]);
  849. // 养老金区域奖励
  850. $order_data = [
  851. 'pay_price' => $pension,
  852. 'user_address_ids' => $thrid['user_address_ids'],
  853. 'uid' => $thrid['uid'],
  854. 'order_sn' => $thrid['order_sn']
  855. ];
  856. // $StoreOrderRepository->area_manage_details($order_data, 5, 2);
  857. });
  858. }
  859. }
  860. /**
  861. * 第三方订单唯品会IN10入账
  862. * @param $nowTime
  863. * @param $timeLimit
  864. * author: php迷途小书童
  865. * created: 2021/3/20 9:26
  866. */
  867. public function doThridVipOrderGzc($nowTime, $timeLimit)
  868. {
  869. $cursor = Db::table('rrx_order_vip')
  870. ->where('gzc', 0)
  871. ->where('bill_status', 1)
  872. ->where('commission', '>', 0)
  873. ->whereTime('bill_end_time', '<', $nowTime - bcmul((string)$timeLimit, (string)86400))
  874. ->cursor();
  875. $ProductRepository = app()->make(ProductRepository::class);
  876. $OrderGzcRepository = app()->make(OrderGzcRepository::class);
  877. $OrderMerchantRepository = app()->make(OrderMerchantRepository::class);
  878. $StoreOrderRepository = app()->make(StoreOrderRepository::class);
  879. foreach ($cursor as $thrid) {
  880. $uid = $thrid['uid'];
  881. if (!$uid)
  882. continue;
  883. $certification = UserCertification::getInstance()->where('uid', $uid)->find();
  884. if (!$certification) {
  885. Log::info('用户ID' . $uid . '未实名认证');
  886. continue;
  887. }
  888. $pension = $ProductRepository->yanglaojin(0, sprintf('%.2f', $thrid['commission']));
  889. Db::transaction(function () use ($uid, $certification, $thrid, $pension, $OrderGzcRepository, $OrderMerchantRepository, $StoreOrderRepository) {
  890. //新增入账明细记录
  891. $orderId = $OrderMerchantRepository->getNewOrderId('IN10');
  892. $create = [
  893. 'uid' => $uid,
  894. 'platform_no' => $orderId,
  895. 'user_name' => $certification['user_name'],
  896. 'mobile' => $certification['mobile'],
  897. 'user_card' => $certification['user_card'],
  898. 'pension' => $pension,
  899. 'order_type' => 7,
  900. 'out_trade_no' => $thrid['order_sn']
  901. ];
  902. $test_user = Db::name('user')->where('uid', $uid)->value('test_user');
  903. if ($test_user == 0) {
  904. $OrderGzcRepository->create($create);
  905. }
  906. // $OrderGzcRepository->create($create);
  907. Db::table('rrx_order_vip')
  908. ->update(['gzc' => 1, 'order_vip_id' => $thrid['order_vip_id']]);
  909. // 养老金区域奖励
  910. $order_data = [
  911. 'pay_price' => $pension,
  912. 'user_address_ids' => $thrid['user_address_ids'],
  913. 'uid' => $thrid['uid'],
  914. 'order_sn' => $thrid['order_sn']
  915. ];
  916. // $StoreOrderRepository->area_manage_details($order_data, 2, 2);
  917. });
  918. }
  919. }
  920. /**
  921. * 第三方订单抖音IN10入账
  922. * @param $nowTime
  923. * @param $timeLimit
  924. * author: php迷途小书童
  925. * created: 2021/3/20 9:26
  926. */
  927. public function doThridDyOrderGzc($nowTime, $timeLimit)
  928. {
  929. $cursor = Db::table('rrx_order_dy')
  930. ->where('gzc', 0)
  931. ->where('bill_status', 1)
  932. ->where('commission', '>', 0)
  933. ->whereTime('bill_end_time', '<', $nowTime - bcmul((string)$timeLimit, (string)86400))
  934. ->cursor();
  935. $ProductRepository = app()->make(ProductRepository::class);
  936. $OrderGzcRepository = app()->make(OrderGzcRepository::class);
  937. $OrderMerchantRepository = app()->make(OrderMerchantRepository::class);
  938. $StoreOrderRepository = app()->make(StoreOrderRepository::class);
  939. foreach ($cursor as $thrid) {
  940. $uid = $thrid['uid'];
  941. if (!$uid)
  942. continue;
  943. $certification = UserCertification::getInstance()->where('uid', $uid)->find();
  944. if (!$certification) {
  945. Log::info('用户ID' . $uid . '未实名认证');
  946. continue;
  947. }
  948. $pension = $ProductRepository->yanglaojin(0, sprintf('%.2f', $thrid['commission']));
  949. Db::transaction(function () use ($uid, $certification, $thrid, $pension, $OrderGzcRepository, $OrderMerchantRepository, $StoreOrderRepository) {
  950. //新增入账明细记录
  951. $orderId = $OrderMerchantRepository->getNewOrderId('IN10');
  952. $create = [
  953. 'uid' => $uid,
  954. 'platform_no' => $orderId,
  955. 'user_name' => $certification['user_name'],
  956. 'mobile' => $certification['mobile'],
  957. 'user_card' => $certification['user_card'],
  958. 'pension' => $pension,
  959. 'order_type' => 8,
  960. 'out_trade_no' => $thrid['order_sn']
  961. ];
  962. $test_user = Db::name('user')->where('uid', $uid)->value('test_user');
  963. if ($test_user == 0) {
  964. $OrderGzcRepository->create($create);
  965. }
  966. Db::table('rrx_order_dy')
  967. ->update(['gzc' => 1, 'order_dy_id' => $thrid['order_dy_id']]);
  968. // 养老金区域奖励
  969. $order_data = [
  970. 'pay_price' => $pension,
  971. 'user_address_ids' => $thrid['user_address_ids'],
  972. 'uid' => $thrid['uid'],
  973. 'order_sn' => $thrid['order_sn']
  974. ];
  975. // $StoreOrderRepository->area_manage_details($order_data, 9, 2);
  976. });
  977. }
  978. }
  979. /**
  980. * 志愿者升级
  981. * */
  982. public function doUpgrade()
  983. {
  984. set_time_limit(0);
  985. try {
  986. //志愿者条件
  987. $volunteer_achievement = systemConfig("volunteer_achievement"); //volunteer_achievement:用户本人满足养老金要求可升级为志愿者
  988. //一星条件
  989. $a_volunteer_achievement = systemConfig("a_volunteer_achievement");//团队养老金要求
  990. $a_volunteer_number = systemConfig("a_volunteer_number"); //团队人数要求
  991. //二星条件
  992. $b_volunteer_achievement = systemConfig("b_volunteer_achievement");//团队养老金要求
  993. $b_volunteer_number = systemConfig("b_volunteer_number"); //团队人数要求
  994. //三星条件
  995. $c_volunteer_achievement = systemConfig("c_volunteer_achievement");//团队养老金要求
  996. $c_volunteer_number = systemConfig("c_volunteer_number"); //团队人数要求
  997. //董事条件
  998. $d_volunteer_achievement = systemConfig("d_volunteer_achievement");//团队养老金要求
  999. $d_volunteer_number = systemConfig("d_volunteer_number"); //团队人数要求
  1000. //用户列表
  1001. $userList = Db::name("User")
  1002. ->field("uid")
  1003. ->select();
  1004. foreach ($userList as $key => $value) {
  1005. $volunteer = $this->volunteer($value['uid']);
  1006. //本人养老金小于志愿者要求
  1007. if ($volunteer['self_yanglao'] < $volunteer_achievement) {
  1008. $this->grade($value['uid'], 0, $volunteer);
  1009. }
  1010. //满足志愿者要求
  1011. if ($volunteer['self_yanglao'] >= $volunteer_achievement) {
  1012. //志愿者
  1013. if ($volunteer['team'] < $a_volunteer_number || $volunteer['yanglao'] < $a_volunteer_achievement) {
  1014. $this->grade($value['uid'], 1, $volunteer);
  1015. }
  1016. //一星
  1017. if ($volunteer['team'] >= $a_volunteer_number && $volunteer['yanglao'] >= $a_volunteer_achievement//满足一星条件
  1018. &&
  1019. ($volunteer['team'] < $b_volunteer_number || $volunteer['yanglao'] < $b_volunteer_achievement)//未满足二星条件
  1020. ) {
  1021. $this->grade($value['uid'], 2, $volunteer);
  1022. }
  1023. //二星
  1024. if ($volunteer['team'] >= $b_volunteer_number && $volunteer['yanglao'] >= $b_volunteer_achievement//满足二星条件
  1025. &&
  1026. ($volunteer['team'] < $c_volunteer_number || $volunteer['yanglao'] < $c_volunteer_achievement)//未满足三星条件
  1027. ) {
  1028. $this->grade($value['uid'], 3, $volunteer);
  1029. }
  1030. //三星
  1031. if ($volunteer['team'] >= $c_volunteer_number && $volunteer['yanglao'] >= $c_volunteer_achievement//满足三星条件
  1032. &&
  1033. ($volunteer['team'] < $d_volunteer_number || $volunteer['yanglao'] < $d_volunteer_achievement)//未满足董事条件
  1034. ) {
  1035. $this->grade($value['uid'], 4, $volunteer);
  1036. }
  1037. //董事
  1038. if ($volunteer['team'] >= $d_volunteer_number && $volunteer['yanglao'] >= $d_volunteer_achievement) {
  1039. $this->grade($value['uid'], 5, $volunteer);
  1040. }
  1041. }
  1042. //团队各等级数量统计
  1043. $this->statistics($value['uid']);
  1044. }
  1045. } catch (Exception $e) {
  1046. Log::info('志愿者升级:' . $e->getMessage());
  1047. }
  1048. }
  1049. //志愿者信息
  1050. public function volunteer($uid)
  1051. {
  1052. if (!$uid) {
  1053. return false;
  1054. }
  1055. //本人养老金
  1056. $data['self_yanglao'] = $this->getYanglao($uid);
  1057. //团队人数
  1058. $data['team'] = $this->volunteer_number($uid);
  1059. //团队养老金
  1060. $data['yanglao'] = $this->volunteer_achievement($uid);
  1061. //直推人数
  1062. $data['extension'] = Db::name("user_identity_log")->where(["uid" => $uid])->count();
  1063. //直推商户数量
  1064. $data['shop'] = Db::name("user_identity_log")->where(["uid" => $uid, 'identity' => 4])->whereNotIn("identity", "0")->count();
  1065. return $data;
  1066. }
  1067. //团队人数
  1068. public function volunteer_number($uid)
  1069. {
  1070. $number = 0;
  1071. $number1 = Db::name("user_identity_log")->where(["uid" => $uid])->field("link_id")->select();
  1072. $number += count($number1);
  1073. foreach ($number1 as $key => $value) {
  1074. $number += Db::name("user_identity_log")->where(["uid" => $value['link_id']])->field("link_id")->count();
  1075. }
  1076. return $number;
  1077. }
  1078. //团队养老金
  1079. public function volunteer_achievement($uid)
  1080. {
  1081. $yanglao = 0;
  1082. $number1 = Db::name("user_identity_log")->where(["uid" => $uid])->field("link_id")->select();
  1083. foreach ($number1 as $key => $value) {
  1084. $yanglao += $this->getYanglao($value['link_id']);
  1085. $number2 = Db::name("user_identity_log")->where(["uid" => $value['link_id']])->field("link_id")->select();
  1086. foreach ($number2 as $k => $v) {
  1087. $yanglao += $this->getYanglao($v['link_id']);
  1088. }
  1089. unset($number2);
  1090. }
  1091. return $yanglao;
  1092. }
  1093. //用户养老金
  1094. public function getYanglao($uid)
  1095. {
  1096. return Db::name("UserBill")
  1097. ->where([
  1098. 'uid' => $uid,
  1099. 'commission_type' => 5,
  1100. 'status' => 1,
  1101. 'pm' => 1
  1102. ])
  1103. ->sum("number");
  1104. }
  1105. //升级
  1106. public function grade($uid, $grade, $volunteer)
  1107. {
  1108. $exist = Db::name("Volunteer")->where(["uid" => $uid])->find();
  1109. if ($exist) {
  1110. Db::name("Volunteer")
  1111. ->where(["uid" => $uid])
  1112. ->update([
  1113. "grade" => $grade,
  1114. "team" => $volunteer['team'],
  1115. "yanglao" => $volunteer['yanglao'],
  1116. "extension" => $volunteer['extension'],
  1117. "shop" => $volunteer['shop'],
  1118. "time" => time()
  1119. ]);
  1120. return true;
  1121. }
  1122. Db::name("Volunteer")
  1123. ->insert([
  1124. "uid" => $uid,
  1125. "grade" => $grade,
  1126. "team" => $volunteer['team'],
  1127. "yanglao" => $volunteer['yanglao'],
  1128. "extension" => $volunteer['extension'],
  1129. "shop" => $volunteer['shop'],
  1130. "time" => time()
  1131. ]
  1132. );
  1133. return true;
  1134. }
  1135. //下级各等级数量统计
  1136. public function statistics($uid)
  1137. {
  1138. $data = [
  1139. 'v' => 0,
  1140. 'v1' => 0,
  1141. 'v2' => 0,
  1142. 'v3' => 0,
  1143. 'v4' => 0
  1144. ];
  1145. $fns = function ($uid) use (&$fns, &$data) {
  1146. $identity = Db::name("user_identity_log")
  1147. ->alias("u")
  1148. ->join("Volunteer v", "u.link_id = v.uid")
  1149. ->where(["u.uid" => $uid])
  1150. ->field("u.link_id,v.grade")
  1151. ->select();
  1152. if ($identity) {
  1153. foreach ($identity as $key => $value) {
  1154. if ($value['grade'] == 1) {
  1155. $data['v']++;
  1156. }
  1157. if ($value['grade'] == 2) {
  1158. $data['v1']++;
  1159. }
  1160. if ($value['grade'] == 3) {
  1161. $data['v2']++;
  1162. }
  1163. if ($value['grade'] == 4) {
  1164. $data['v3']++;
  1165. }
  1166. if ($value['grade'] == 5) {
  1167. $data['v4']++;
  1168. }
  1169. $fns($value['link_id']);
  1170. }
  1171. }
  1172. };
  1173. $fns($uid);
  1174. Db::name("Volunteer")
  1175. ->where(["uid" => $uid])
  1176. ->update([
  1177. "v" => $data['v'],
  1178. "v1" => $data['v1'],
  1179. "v2" => $data['v2'],
  1180. "v3" => $data['v3'],
  1181. "v4" => $data['v4'],
  1182. "time" => time()
  1183. ]);
  1184. return $data;
  1185. }
  1186. }