V2boardStatistics.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. ini_set('memory_limit', -1);
  40. $this->statOrder();
  41. $this->statServer();
  42. }
  43. private function statOrder()
  44. {
  45. $endAt = strtotime(date('Y-m-d'));
  46. $startAt = strtotime('-1 day', $endAt);
  47. $builder = Order::where('paid_at', '>=', $startAt)
  48. ->where('paid_at', '<', $endAt)
  49. ->whereNotIn('status', [0, 2]);
  50. $orderCount = $builder->count();
  51. $orderAmount = $builder->sum('total_amount');
  52. $builder = $builder->where('commission_balance', '!=', 0);
  53. $commissionCount = $builder->count();
  54. $commissionAmount = $builder->sum('commission_balance');
  55. $data = [
  56. 'order_count' => $orderCount,
  57. 'order_amount' => $orderAmount,
  58. 'commission_count' => $commissionCount,
  59. 'commission_amount' => $commissionAmount,
  60. 'record_type' => 'd',
  61. 'record_at' => $startAt
  62. ];
  63. $statistic = StatOrder::where('record_at', $startAt)
  64. ->where('record_type', 'd')
  65. ->first();
  66. if ($statistic) {
  67. $statistic->update($data);
  68. return;
  69. }
  70. StatOrder::create($data);
  71. }
  72. private function statServer()
  73. {
  74. $endAt = strtotime(date('Y-m-d'));
  75. $startAt = strtotime('-1 day', $endAt);
  76. $statistics = ServerLog::select([
  77. 'server_id',
  78. 'method as server_type',
  79. DB::raw("sum(u) as u"),
  80. DB::raw("sum(d) as d"),
  81. ])
  82. ->where('log_at', '>=', $startAt)
  83. ->where('log_at', '<', $endAt)
  84. ->groupBy('server_id', 'method')
  85. ->get()
  86. ->toArray();
  87. foreach ($statistics as $statistic) {
  88. $statistic['record_type'] = 'd';
  89. $statistic['record_at'] = $startAt;
  90. StatServerJob::dispatch($statistic);
  91. }
  92. }
  93. }