OptimizerMonthCharge.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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= : 统计月份} {--user_ids= : 投手uids}';
  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. $month = $this->option('month') ?? date('Y-m', strtotime('last month'));
  25. $monthStart = $month. '-01';
  26. $montEnd = date_sub(date_add(date_create($monthStart),
  27. date_interval_create_from_date_string('1 month')),
  28. date_interval_create_from_date_string('1 day'))->format('Y-m-d');
  29. $user_ids = $this->option('user_ids');
  30. if($user_ids) {
  31. $userIds = explode(',', $user_ids);
  32. } else {
  33. $userIds = DB::table('user_has_roles')
  34. ->where(['role_id' => 2])
  35. ->select('user_id')
  36. ->get()->pluck('user_id');
  37. }
  38. $now = date('Y-m-d H:i:s');
  39. foreach ($userIds as $userId) {
  40. $puser_id = DB::table('users')->where('id', $userId)->value('pid');
  41. $result = DB::table('tj_optimizer_day_charge')
  42. ->whereBetween('day_at', [$monthStart, $montEnd])
  43. ->where('user_id', $userId)
  44. ->select(
  45. 'miniprogram_id',
  46. DB::raw("sum(pay_money) as pay_money"),
  47. DB::raw("sum(common_pay_count) as common_pay_count"),
  48. DB::raw("sum(common_unpay_count) as common_unpay_count"),
  49. DB::raw("sum(vip_unpay_count) as vip_unpay_count"),
  50. DB::raw("sum(vip_pay_count) as vip_pay_count"),
  51. )->groupBy('miniprogram_id')
  52. ->get();
  53. $insertData = [];
  54. foreach ($result as $item) {
  55. $info['pay_money'] = $item->pay_money ?? 0;
  56. $info['common_pay_count'] = $item->common_pay_count ?? 0;
  57. $info['common_unpay_count'] = $item->common_unpay_count ?? 0;
  58. $info['vip_unpay_count'] = $item->vip_unpay_count ?? 0;
  59. $info['vip_pay_count'] = $item->vip_pay_count ?? 0;
  60. $info['miniprogram_id'] = $item->miniprogram_id ?? 0;
  61. $info['created_at'] = $info['updated_at'] = $now;
  62. $info['month_at'] = $month;
  63. $info['user_id'] = $userId;
  64. $info['puser_id'] = $puser_id;
  65. $insertData[] = $info;
  66. }
  67. DB::table('tj_optimizer_month_charge')
  68. ->insert($insertData);
  69. }
  70. }
  71. }