GenNewUserChargeDayStatistic.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace App\Console\Commands\Trade;
  3. use App\Modules\Trade\Models\NewUserChargeDayStatistic;
  4. use App\Modules\Trade\Models\Order;
  5. use Illuminate\Console\Command;
  6. use Illuminate\Support\Collection;
  7. use Illuminate\Support\Facades\Log;
  8. use stdClass;
  9. use DB;
  10. class GenNewUserChargeDayStatistic extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'new_user_charge_day_statistic {--begin_date=} {--end_date=} {--date=}';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = '新注册用户充值日统计';
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. }
  33. /**
  34. * Execute the console command.
  35. *
  36. * @return mixed
  37. */
  38. public function handle()
  39. {
  40. print_r("======新注册用户充值统计 【任务执行开始】=====" . date("y-m-d H:i:s" . "\n"));
  41. Log::info("======新注册用户充值统计 【任务执行开始】=====" . date("y-m-d H:i:s" . "\n"));
  42. ini_set('memory_limit', '256M');
  43. $begin_date = $this->option('begin_date');
  44. $end_date = $this->option('end_date');
  45. if ($begin_date) {
  46. $date = $begin_date;
  47. while (strtotime($date) < strtotime($end_date)) {
  48. Log::info("======新注册用户充值日统计 【任务执行中】===== 日期:" . $date . ' ' . date("y-m-d H:i:s" . "\n"));
  49. print_r("======新注册用户充值日统计 【任务执行中】===== 日期:" . $date . ' ' . date("y-m-d H:i:s" . "\n"));
  50. $this->statistic($date);
  51. $date = date('Y-m-d', strtotime('+1 days', strtotime($date)));
  52. }
  53. } else {
  54. $date = $this->option('date');
  55. $date = $date ? $date : date('Y-m-d', strtotime('-1 days'));
  56. $this->statistic($date);
  57. }
  58. print_r("======新注册用户充值日统计 【任务执行结束】=====" . date("y-m-d H:i:s" . "\n"));
  59. Log::info("======新注册用户充值日统计 【任务执行结束】=====" . date("y-m-d H:i:s" . "\n"));
  60. }
  61. /**
  62. * 新用户数据统计
  63. * @param string $date 统计日期
  64. */
  65. public function statistic(string $date)
  66. {
  67. $statistic_info = $this->runSql($date);
  68. foreach ($statistic_info as $item) {
  69. $this->saveStatistic($item);
  70. }
  71. }
  72. public function runSql(string $date)
  73. {
  74. $end_date = date('Y-m-d', strtotime('+1 days', strtotime($date)));
  75. return Order::join('users', 'orders.uid', 'users.id')
  76. ->where('orders.created_at', '>=', $date)
  77. ->where('orders.created_at', '<', $end_date)
  78. ->where('status', 'PAID')
  79. ->selectRaw("orders.distribution_channel_id as channel_id,sum(price) as charge_amount,
  80. timestampdiff(day,DATE_FORMAT(users.created_at, '%Y-%m-%d'),DATE_FORMAT(orders.created_at, '%Y-%m-%d')) as diff_day,
  81. DATE_FORMAT(users.created_at, '%Y-%m-%d') as register_date,
  82. DATE_FORMAT(orders.created_at, '%Y-%m-%d') as pay_date,
  83. count(distinct uid) as paid_user_num,
  84. count(orders.id) as paid_num,
  85. count(orders.pay_type = 1 or null) as first_order_users
  86. ")
  87. ->groupBy('channel_id', 'register_date')
  88. ->get();
  89. }
  90. /**
  91. * 保存统计数据
  92. * @param string $date 统计日期
  93. * @param Collection $collect 统计数据
  94. * @param int $day 第几天
  95. */
  96. private function saveStatistic($item)
  97. {
  98. try{
  99. //DB::connection('new_yunqi')->table('new_user_charge_day_statistic')->updateOrInsert(
  100. NewUserChargeDayStatistic::updateOrCreate(
  101. [
  102. 'channel_id' => $item->channel_id,
  103. 'date' => $item->register_date,
  104. 'day_num' => $item->diff_day,
  105. ],
  106. [
  107. 'pay_date' => $item->pay_date,
  108. 'amount' => $item->charge_amount,
  109. 'users' => $item->paid_user_num,
  110. 'count' => $item->paid_num,
  111. 'first_order_users' => $item->first_order_users//首单人数
  112. ]
  113. );
  114. }catch (\Exception $e){
  115. //
  116. }
  117. }
  118. }