CompanyMonthCharge.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace App\Console\Commands\Statistic;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\DB;
  5. class CompanyMonthCharge extends Command
  6. {
  7. /**
  8. * The name and signature of the console command.
  9. *
  10. * @var string
  11. */
  12. protected $signature = 'Statistic:CompanyMonthCharge {--month= : 统计月份} {--company_uids= : 公司管理员ids}';
  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. $company_uids = $this->option('company_uids');
  30. if($company_uids) {
  31. $companyUids = explode(',', $company_uids);
  32. } else {
  33. $companyUids = DB::table('user_has_roles')
  34. ->where(['role_id' => 1])
  35. ->select('user_id')
  36. ->get()->pluck('user_id');
  37. }
  38. $now = date('Y-m-d H:i:s');
  39. foreach ($companyUids as $companyUid) {
  40. $info = DB::table('tj_company_day_charge')
  41. ->whereBetween('day_at', [$monthStart, $montEnd])
  42. ->where(['company_uid' => $companyUid])
  43. ->select(
  44. DB::raw("sum(pay_money) as pay_money"),
  45. DB::raw("sum(common_pay_count) as common_pay_count"),
  46. DB::raw("sum(common_unpay_count) as common_unpay_count"),
  47. DB::raw("sum(vip_unpay_count) as vip_unpay_count"),
  48. DB::raw("sum(vip_pay_count) as vip_pay_count"),
  49. )->first();
  50. DB::table('tj_company_month_charge')
  51. ->insert([
  52. 'month_at' => $month,
  53. 'pay_money' => $info->pay_money ?? 0,
  54. 'common_pay_count' => $info->common_pay_count ?? 0,
  55. 'common_unpay_count' => $info->common_unpay_count ?? 0,
  56. 'vip_pay_count' => $info->vip_pay_count ?? 0,
  57. 'vip_unpay_count' => $info->vip_unpay_count ?? 0,
  58. 'company_uid' => $companyUid,
  59. 'created_at' => $now,
  60. 'updated_at' => $now,
  61. ]);
  62. }
  63. }
  64. }