ChannelDayStatistics.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace App\Console\Channel;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Log;
  6. class ChannelDayStatistics extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'channelDayStatistics {--channel_id=} {--day=} {--start_day=} {--end_day=}';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = '站点统计日数据';
  20. private $channelUserService;
  21. public function __construct(
  22. )
  23. {
  24. parent::__construct();
  25. }
  26. public function handle()
  27. {
  28. // 获取全部站点
  29. $distribution_channel_ids = DB::table('distribution_channels')->orderBy('id')->select('id')->get()->pluck('id')->toArray();
  30. // 传参
  31. $option_day = trim($this->option('day'));
  32. if (!$option_day) {
  33. $day = date('Y-m-d', strtotime('-1 day'));
  34. }else {
  35. $day = $option_day;
  36. }
  37. $channel_id = trim($this->option('channel_id'));
  38. $start_day = trim($this->option('start_day'));
  39. $end_day = trim($this->option('end_day'));
  40. if ($channel_id) {
  41. if (!$start_day && !$option_day) {
  42. // 指定站点按站点创建日期开始统计
  43. $created_at = DB::table('distribution_channels')->where('id', $channel_id)->value('created_at');
  44. if (!$created_at) dd('该站点不存在');
  45. $start_day = transDate($created_at, 'Y-m-d');
  46. }
  47. $distribution_channel_ids = [$channel_id];
  48. }
  49. if ($start_day) { // 指定开始日期统计
  50. if (!$end_day) { // 未指定结束日期则默认截止到昨天
  51. $end_day = date('Y-m-d', strtotime('-1 day'));
  52. }
  53. $day = $start_day;
  54. while (true) {
  55. if (strtotime($day) > strtotime($end_day)) break;
  56. $this->runStatistics($day, $distribution_channel_ids);
  57. $day = date('Y-m-d', strtotime($day.' +1 day'));
  58. }
  59. }else {
  60. $this->runStatistics($day, $distribution_channel_ids);
  61. }
  62. }
  63. // 执行统计
  64. private function runStatistics($day, $distribution_channel_ids) {
  65. dLog('command_logs')->info('~~~~~~~~~~~~~~~~~~~~~~~~~~开始统计站点('.$day.')数据~~~~~~~~~~~~~~~~~~~~~~~~~~');
  66. $executeStart = microtime(true);
  67. $month = date('Ym', strtotime($day));
  68. $day_start = $day.' 00:00:00';
  69. $day_end = $day.' 23:59:59';
  70. foreach ($distribution_channel_ids as $channel_id) {
  71. $total_order_num = DB::table('orders')->where('distribution_channel_id', $channel_id)->whereBetween('created_at', [$day_start, $day_end])->count('id');
  72. $paid_order = DB::table('orders')->where('distribution_channel_id', $channel_id)->whereBetween('created_at', [$day_start, $day_end])->where('status', 'PAID')
  73. ->selectRaw("count(id) as paid_order_num, count(distinct uid) as total_pay_num, sum(price) as total_pay_amount")->first();
  74. $paid_order_num = getProp($paid_order, 'paid_order_num', 0);
  75. $total_pay_num = getProp($paid_order, 'total_pay_num', 0);
  76. $total_pay_amount = getProp($paid_order, 'total_pay_amount', 0);
  77. $register_uids = DB::table('users')->where('distribution_channel_id', $channel_id)->whereBetween('created_at', [$day_start, $day_end])->select('id')->get()->pluck('id')->toArray();
  78. $register_orders = DB::table('orders')->where('distribution_channel_id', $channel_id)->whereBetween('created_at', [$day_start, $day_end])
  79. ->whereIn('uid', $register_uids)->where('status', 'PAID')
  80. ->selectRaw('count(distinct uid) as register_pay_num, sum(price) as register_pay_amount')->first();
  81. $register_num = count($register_uids);
  82. $register_pay_num = getProp($register_orders, 'register_pay_num', 0);
  83. $register_pay_amount = getProp($register_orders, 'register_pay_amount', 0);
  84. $statistics = [
  85. 'distribution_channel_id' => $channel_id,
  86. 'day' => $day,
  87. 'month' => $month,
  88. 'total_order_num' => $total_order_num,
  89. 'paid_order_num' => $paid_order_num,
  90. 'register_num' => $register_num,
  91. 'register_pay_num' => $register_pay_num,
  92. 'register_pay_amount' => $register_pay_amount,
  93. 'total_pay_num' => $total_pay_num,
  94. 'total_pay_amount' => $total_pay_amount,
  95. 'created_at' => date('Y-m-d H:i:s'),
  96. 'updated_at' => date('Y-m-d H:i:s'),
  97. ];
  98. $boolen = DB::table('channel_day_statistics')->updateOrInsert([
  99. 'distribution_channel_id' => $channel_id,
  100. 'day' => $day,
  101. ], $statistics);
  102. if (!$boolen) {
  103. Log::info('站点日统计失败, 失败记录: '.json_encode($statistics, 256));
  104. }
  105. $executeEnd = microtime(true);
  106. dLog('command_logs')->info('站点('.$channel_id.')--'.$day.'执行时间: ', ['execute_time'=>round(($executeEnd - $executeStart), 6).'s']);
  107. }
  108. $executeEnd = microtime(true);
  109. dLog('command_logs')->info('脚本运行时间: ', ['execute_time'=>round(($executeEnd - $executeStart), 6).'s']);
  110. dLog('command_logs')->info('~~~~~~~~~~~~~~~~~~~~~~~~~~结束统计站点('.$day.')数据~~~~~~~~~~~~~~~~~~~~~~~~~~');
  111. }
  112. }