OptimizerMonthCharge.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace App\Console\Commands\Statistic;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\DB;
  5. class OptimizerMonthCharge extends Command
  6. {
  7. /**
  8. * The name and signature of the console command.
  9. *
  10. * @var string
  11. */
  12. protected $signature = 'Statistic:OptimizerMonthCharge {--month= : 统计月份}';
  13. /**
  14. * The console command description.
  15. *
  16. * @var string
  17. */
  18. protected $description = '优化师充值月统计';
  19. /**
  20. * Execute the console command.
  21. */
  22. public function handle(): void
  23. {
  24. //tj_optimizer_month_charge
  25. $month = $this->option('month');
  26. $monthStart = $month. '-01';
  27. $montEnd = date_sub(date_add(date_create($monthStart),
  28. date_interval_create_from_date_string('1 month')),
  29. date_interval_create_from_date_string('1 day'))->format('Y-m-d');
  30. $result = DB::table('tj_optimizer_day_charge')
  31. ->whereBetween('day_at', [$monthStart, $montEnd])
  32. ->select(
  33. 'user_id', 'miniprogram_id','puser_id',
  34. DB::raw("sum(pay_money) as pay_money"),
  35. DB::raw("sum(common_pay_count) as common_pay_count"),
  36. DB::raw("sum(common_unpay_count) as common_unpay_count"),
  37. DB::raw("sum(vip_unpay_count) as vip_unpay_count"),
  38. DB::raw("sum(vip_pay_count) as vip_pay_count"),
  39. )->groupBy(['user_id', 'puser_id','miniprogram_id'])
  40. ->get();
  41. $insertData = [];
  42. $now = date('Y-m-d H:i:s');
  43. foreach ($result as $item) {
  44. $info = (array)$item;
  45. $info['created_at'] = $info['updated_at'] = $now;
  46. $info['month_at'] = $month;
  47. $insertData[] = $info;
  48. }
  49. DB::table('tj_optimizer_month_charge')
  50. ->insert($insertData);
  51. }
  52. }