V2boardStatistics.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. $startAt = strtotime(date('Y-m-d'));
  74. $statistics = ServerLog::select([
  75. 'server_id',
  76. 'method as server_type',
  77. DB::raw("sum(u) as u"),
  78. DB::raw("sum(d) as d"),
  79. ])
  80. ->where('log_at', $startAt)
  81. ->groupBy('server_id', 'method')
  82. ->get()
  83. ->toArray();
  84. foreach ($statistics as $statistic) {
  85. $statistic['record_type'] = 'd';
  86. $statistic['record_at'] = $startAt;
  87. StatServerJob::dispatch($statistic);
  88. }
  89. }
  90. }