GenerateUserRegistStat.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Hardy
  5. * Date: 2020/7/31
  6. * Time: 21:37
  7. */
  8. namespace App\Console\Commands\SendOrder;
  9. use App\Modules\Order\Services\OrderService;
  10. use App\Modules\SendOrder\Models\SendOrderByRegist;
  11. use App\Modules\SendOrder\Services\SendOrderByRegistService;
  12. use Illuminate\Console\Command;
  13. class GenerateUserRegistStat extends Command
  14. {
  15. /**
  16. * The name and signature of the console command.
  17. * php artisan GenerateUserRegistStat
  18. * @var string
  19. */
  20. protected $signature = 'GenerateUserRegistStat {--begin_date=} {--end_date=}';
  21. /**
  22. * The console command description.
  23. *
  24. * @var string
  25. */
  26. protected $description = '每日生成派单数据';
  27. /**
  28. * Create a new command instance.
  29. *
  30. * @return void
  31. */
  32. public function __construct()
  33. {
  34. parent::__construct();
  35. }
  36. /**
  37. * Execute the console command.
  38. *
  39. * @return mixed
  40. */
  41. public function handle()
  42. {
  43. $this->start();
  44. }
  45. public function start()
  46. {
  47. $begin_date = $this->option('begin_date');
  48. $end_date = $this->option('end_date');
  49. if(!isset($begin_date))
  50. {
  51. $begin_date=date('Y-m-d', strtotime('-1 day',strtotime(now())));
  52. $end_date =date('Y-m-d', strtotime(now()));
  53. }
  54. myLog('SendOrderByRegist')->notice('【' . $begin_date . '->' . $end_date . '】------- 开始');
  55. $endTime = $end_date;
  56. $runDay = $begin_date;
  57. while (strtotime($runDay) < strtotime($endTime)) {
  58. var_dump($runDay);
  59. $this->processOneDayNew($runDay);
  60. $runDay = date('Y-m-d', strtotime('+1 day', strtotime($runDay)));
  61. }
  62. myLog('SendOrderByRegist')->notice('【' . $begin_date . '->' . $end_date . '】------- 结束');
  63. }
  64. public function processOneDayNew($day)
  65. {
  66. //清理当天的派单按日注册
  67. myLog('SendOrderByRegist')->notice($day . '------- 开始:');
  68. $startTime = date('Y-m-d', strtotime($day));
  69. $endTime = date('Y-m-d', strtotime('+1 day', strtotime($startTime)));
  70. //查询派单
  71. $orderService = new SendOrderByRegistService();
  72. $sendOrderIDs = $orderService->getSendOrderIdFromOrigin($startTime, $endTime);
  73. //dd(collect($sendOrderIDs)->implode('send_order_id', ', '));
  74. $bitch =500;
  75. $partIds = [];
  76. for ($i = 1; $i <= count($sendOrderIDs); $i++) {
  77. $partIds[] = $sendOrderIDs[$i - 1]->send_order_id;
  78. if ($i > 1 && $i % $bitch == 0) {
  79. $orderService->deleteRecs($partIds);
  80. $partIds = [];
  81. }
  82. }
  83. if(count($partIds)>0)
  84. {
  85. $orderService->deleteRecs($partIds);
  86. }
  87. myLog('SendOrderByRegist')->notice($day . '------- 清理历史记录完成');
  88. myLog('SendOrderByRegist')->notice('有效派单数'.count($sendOrderIDs));
  89. foreach ($sendOrderIDs as $item) {
  90. myLog('SendOrderByRegist')->notice('当前派单ID'.$item->send_order_id);
  91. self:: processSendOrderById($item->send_order_id, $endTime);
  92. }
  93. myLog('SendOrderByRegist')->notice($day . '------- 结束');
  94. }
  95. /**
  96. * @param int $send_order_id
  97. * @param string $endTime 不包含
  98. */
  99. public function processSendOrderById($send_order_id, $endTime)
  100. {
  101. $modelList['send_order_id'] = $send_order_id;
  102. //查询首充数及首充金额
  103. $orderService = new OrderService();
  104. $find = $orderService->getSendOrderFirstPayCountAndPriceByID($send_order_id, $endTime);
  105. $modelList['first_pay_count'] = empty($find) ? 0 : $find['first'];
  106. $modelList['first_pay_amount'] = empty($find) ? 0 : $find['price'];
  107. //付费人数,付费次数和充值总额
  108. $find = $orderService->getSendOrderSuccessPayCountByID($send_order_id, $endTime);
  109. $modelList['success_user_count'] = empty($find) ? 0 : $find['u_num'];
  110. $modelList['success_order_count'] = empty($find) ? 0 : $find['num'];
  111. $modelList['total_amount'] = empty($find) ? 0 : $find['price'];
  112. //24小时充值充值金额
  113. $hour = 24;
  114. $find = $orderService->getSendOrderPayPriceByIdAndHour($send_order_id, $endTime, $hour);
  115. $modelList['hour_24'] = empty($find) ? 0 : $find['price'];
  116. //24小时首充用户数
  117. $find = $orderService->getSendOrderPayUserCountByIdAndHour($send_order_id, $endTime, $hour, 1);
  118. $modelList['hour_24_first_count'] = empty($find) ? 0 : $find['num'];
  119. //24小时非首充用户数
  120. $find = $orderService->getSendOrderPayUserCountByIdAndHour($send_order_id, $endTime, $hour, 0);
  121. $modelList['hour_24_not_first_count'] = empty($find) ? 0 : $find['num'];
  122. //3天充值充值金额
  123. $hour = 24 * 3;
  124. $find = $orderService->getSendOrderPayPriceByIdAndHour($send_order_id, $endTime, $hour);
  125. $modelList['day_3'] = empty($find) ? 0 : $find['price'];
  126. //3天首充用户数
  127. $find = $orderService->getSendOrderPayUserCountByIdAndHour($send_order_id, $endTime, $hour, 1);
  128. $modelList['day_3_first_count'] = empty($find) ? 0 : $find['num'];
  129. //3天非首充用户数
  130. $find = $orderService->getSendOrderPayUserCountByIdAndHour($send_order_id, $endTime, $hour, 0);
  131. $modelList['day_3_not_first_count'] = empty($find) ? 0 : $find['num'];
  132. //7天充值充值金额
  133. $hour = 24 * 7;
  134. $find = $orderService->getSendOrderPayPriceByIdAndHour($send_order_id, $endTime, $hour);
  135. $modelList['day_7'] = empty($find) ? 0 : $find['price'];
  136. //7天首充用户数
  137. $find = $orderService->getSendOrderPayUserCountByIdAndHour($send_order_id, $endTime, $hour, 1);
  138. $modelList['day_7_first_count'] = empty($find) ? 0 : $find['num'];
  139. //7天非首充用户数
  140. $find = $orderService->getSendOrderPayUserCountByIdAndHour($send_order_id, $endTime, $hour, 0);
  141. $modelList['day_7_not_first_count'] = empty($find) ? 0 : $find['num'];
  142. myLog('SendOrderByRegist')->notice('【' . $send_order_id . '】------- 开始保存数据');
  143. $helper = new SendOrderByRegistService();
  144. $find = $helper->search(['send_order_id' => $send_order_id], true)->first();
  145. if (!empty($find)) {
  146. $find->first_pay_count += $modelList['first_pay_count'];
  147. $find->first_pay_amount += $modelList['first_pay_amount'];
  148. $find->success_user_count += $modelList['success_user_count'];
  149. $find->success_order_count += $modelList['success_order_count'];
  150. $find->total_amount += $modelList['total_amount'];
  151. $find->hour_24 += $modelList['hour_24'];
  152. $find->hour_24_first_count += $modelList['hour_24_first_count'];
  153. $find->hour_24_not_first_count += $modelList['hour_24_not_first_count'];
  154. $find->day_3 += $modelList['day_3'];
  155. $find->day_3_first_count += $modelList['day_3_first_count'];
  156. $find->day_3_not_first_count += $modelList['day_3_not_first_count'];
  157. $find->day_7 += $modelList['day_7'];
  158. $find->day_7_first_count += $modelList['day_7_first_count'];
  159. $find->day_7_not_first_count += $modelList['day_7_not_first_count'];
  160. $find->save();
  161. } else {
  162. SendOrderByRegist::insert($modelList);
  163. }
  164. myLog('SendOrderByRegist')->notice('【' . $send_order_id . '】------- 保存数据完成');
  165. }
  166. }