SendOrderRechargeDayStatsCommand.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace App\Console\DyReport;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Redis;
  6. class SendOrderRechargeDayStatsCommand extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'SendOrderRechargeDayStats {--date=} {--begin_date=} {--end_date=}';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = '注册用户派单数据日统计';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return mixed
  33. */
  34. public function handle()
  35. {
  36. dLog('command_logs')->info('~~~~~~~~~~~~~~~~~~~~~~~~~~开始统计注册用户派单日数据~~~~~~~~~~~~~~~~~~~~~~~~~~');
  37. $executeStart = microtime(true);
  38. $begin_date = $this->option('begin_date');
  39. $end_date = $this->option('end_date');
  40. if ($begin_date) {
  41. $date = $begin_date;
  42. if (!$end_date) { // 未传截止日期则默认截止到今天0点
  43. $end_date = date('Y-m-d');
  44. }else {
  45. if (strtotime($end_date) > strtotime(date('Y-m-d'))) { // 截止日期如果超过今天0点,则将截止日期重置为今天0点
  46. $end_date = date('Y-m-d');
  47. }
  48. }
  49. while (strtotime($date) < strtotime($end_date)) {
  50. dLog('command_logs')->info("【任务执行中】~~~~~~ 日期:" . $date);
  51. $this->statistic($date);
  52. $date = date('Y-m-d', strtotime('+1 days', strtotime($date)));
  53. }
  54. } else {
  55. $date = $this->option('date');
  56. $date = $date ? $date : date('Y-m-d', strtotime('-1 days'));
  57. $this->statistic($date);
  58. }
  59. $executeEnd = microtime(true);
  60. dLog('command_logs')->info('脚本运行时间: ', ['execute_time'=>round(($executeEnd - $executeStart), 6).'s']);
  61. dLog('command_logs')->info('~~~~~~~~~~~~~~~~~~~~~~~~~~结束统计注册用户派单日数据~~~~~~~~~~~~~~~~~~~~~~~~~~');
  62. }
  63. /**
  64. * 新用户数据统计
  65. * @param string $date 统计日期
  66. */
  67. public function statistic(string $date)
  68. {
  69. $statistic_info = $this->runSql($date);
  70. foreach ($statistic_info as $item) {
  71. $this->saveStatistic($item);
  72. }
  73. }
  74. public function runSql(string $date)
  75. {
  76. $end_date = date('Y-m-d', strtotime('+1 days', strtotime($date)));
  77. return DB::connection('mysql::write')->table('orders')->leftJoin('users', 'orders.uid', 'users.id')
  78. ->where('orders.created_at', '>=', $date)
  79. ->where('orders.created_at', '<', $end_date)
  80. ->where('orders.status', 'PAID')
  81. ->selectRaw("orders.send_order_id,DATE_FORMAT(users.created_at, '%Y-%m-%d') as register_date,
  82. timestampdiff(day,DATE_FORMAT(users.created_at, '%Y-%m-%d'),DATE_FORMAT(orders.created_at, '%Y-%m-%d')) as diff_day,
  83. DATE_FORMAT(orders.created_at, '%Y-%m-%d') as pay_date,
  84. sum(orders.price) as pay_amount,
  85. count(distinct orders.uid) as pay_num,
  86. count(orders.id) as order_num,
  87. count(orders.pay_num = 1 or null) as first_pay_num")
  88. ->groupBy('send_order_id', 'register_date')
  89. ->get()->map(function ($value) {
  90. return (array)$value;
  91. })->toArray();
  92. }
  93. private function saveStatistic($item)
  94. {
  95. try{
  96. DB::table('send_order_recharge_day_stats')->updateOrInsert(
  97. [
  98. 'send_order_id' => $item['send_order_id'], // 派单id
  99. 'register_date' => $item['register_date'], // 注册日期
  100. 'day_num' => $item['diff_day'], // 第几天
  101. ],
  102. [
  103. 'pay_date' => $item['pay_date'], // 支付日期
  104. 'pay_amount' => $item['pay_amount'], // 支付金额
  105. 'pay_num' => $item['pay_num'], // 支付人数
  106. 'order_num' => $item['order_num'], // 订单数
  107. 'first_pay_num' => $item['first_pay_num'], //首单人数
  108. 'created_at' => date('Y-m-d H:i:s'),
  109. 'updated_at' => date('Y-m-d H:i:s'),
  110. ]
  111. );
  112. }catch (\Exception $e){
  113. dLog('command_logs')->info("【任务执行失败】~~~~~~ 错误信息:" . $e->getMessage());
  114. }
  115. }
  116. }