UserMonthOrderDayStats.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use DB;
  5. class UserMonthOrderDayStats extends Command
  6. {
  7. /**
  8. * The name and signature of the console command.
  9. *
  10. * @var string
  11. */
  12. protected $signature = 'UserMonthOrderDayStats';
  13. /**
  14. * The console command description.
  15. *
  16. * @var string
  17. */
  18. protected $description = '包月按天数据';
  19. /**
  20. * Create a new command instance.
  21. *
  22. * @return void
  23. */
  24. public function __construct()
  25. {
  26. parent::__construct();
  27. }
  28. /**
  29. * Execute the console command.
  30. *
  31. * @return mixed
  32. */
  33. public function handle()
  34. {
  35. $this->start();
  36. }
  37. private function start(){
  38. $sql = "INSERT INTO user_month_day_stats(`day`,type,exposure_num,increment_user_num,cancel_user_num,created_at,updated_at)
  39. select
  40. `day`,
  41. '%s' as type,
  42. count(distinct uid) as exposure_num,
  43. (select count(*) from user_month_order
  44. where date(user_month_order.created_at) = user_month_visit_record.day and user_month_order.uid in (
  45. SELECT uid FROM user_month_sign where date(user_month_sign.created_at) = date(user_month_order.created_at ) and type='%s'
  46. ) and type='%s' ) as increment_user_num,
  47. (select count(*) from user_month_order
  48. where date(user_month_order.created_at) = user_month_visit_record.day and user_month_order.uid in (
  49. SELECT uid FROM user_month_sign where date(user_month_sign.created_at) = date(user_month_order.created_at) and type='%s'
  50. and change_type = 'DELETE'
  51. )and type='%s' ) as cancel_user_num,
  52. NOW(),NOW()
  53. from user_month_visit_record where type='%s' and created_at >= '%s' and created_at < '%s'
  54. group by `day`
  55. ";
  56. $month_sql = sprintf($sql,'MONTH','MONTH','MONTH','MONTH','MONTH','MONTH',date('Y-m-d',time()-86400),date('Y-m-d'));
  57. $month_old = DB::table('user_month_day_stats')->where('day',date('Y-m-d',time()-86400))->where('type','MONTH')->first();
  58. if(!$month_old){
  59. DB::insert($month_sql);
  60. }
  61. $week_sql = sprintf($sql,'WEEK','WEEK','WEEK','WEEK','WEEK','WEEK',date('Y-m-d',time()-86400),date('Y-m-d'));
  62. $week_old = DB::table('user_month_day_stats')->where('day',date('Y-m-d',time()-86400))->where('type','WEEK')->first();
  63. if(!$week_old){
  64. DB::insert($week_sql);
  65. }
  66. }
  67. }