V2boardStatistics.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Jobs\StatServerJob;
  4. use Illuminate\Console\Command;
  5. use App\Models\Order;
  6. use App\Models\StatOrder;
  7. use App\Models\ServerLog;
  8. use Illuminate\Support\Facades\DB;
  9. class V2boardStatistics extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'v2board:statistics';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = '统计任务';
  23. /**
  24. * Create a new command instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct()
  29. {
  30. parent::__construct();
  31. }
  32. /**
  33. * Execute the console command.
  34. *
  35. * @return mixed
  36. */
  37. public function handle()
  38. {
  39. $this->statOrder();
  40. $this->statServer();
  41. }
  42. private function statOrder()
  43. {
  44. $endAt = strtotime(date('Y-m-d'));
  45. $startAt = strtotime('-1 day', $endAt);
  46. $builder = Order::where('paid_at', '>=', $startAt)
  47. ->where('paid_at', '<', $endAt)
  48. ->whereNotIn('status', [0, 2]);
  49. $orderCount = $builder->count();
  50. $orderAmount = $builder->sum('total_amount');
  51. $builder = $builder->where('commission_balance', '!=', 0);
  52. $commissionCount = $builder->count();
  53. $commissionAmount = $builder->sum('commission_balance');
  54. $data = [
  55. 'order_count' => $orderCount,
  56. 'order_amount' => $orderAmount,
  57. 'commission_count' => $commissionCount,
  58. 'commission_amount' => $commissionAmount,
  59. 'record_type' => 'd',
  60. 'record_at' => $startAt
  61. ];
  62. $statistic = StatOrder::where('record_at', $startAt)
  63. ->where('record_type', 'd')
  64. ->first();
  65. if ($statistic) {
  66. $statistic->update($data);
  67. return;
  68. }
  69. StatOrder::create($data);
  70. }
  71. private function statServer()
  72. {
  73. $endAt = strtotime(date('Y-m-d'));
  74. $startAt = strtotime('-1 day', $endAt);
  75. $statistics = ServerLog::select([
  76. 'server_id',
  77. 'method as server_type',
  78. DB::raw("sum(u) as u"),
  79. DB::raw("sum(d) as d"),
  80. ])
  81. ->where('log_at', '>=', $startAt)
  82. ->where('log_at', '<', $endAt)
  83. ->groupBy('server_id', 'method')
  84. ->get()
  85. ->toArray();
  86. foreach ($statistics as $statistic) {
  87. $statistic['record_type'] = 'd';
  88. $statistic['record_at'] = $startAt;
  89. StatServerJob::dispatch($statistic);
  90. }
  91. }
  92. }