V2boardStatistics.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\StatServer;
  4. use App\Models\StatUser;
  5. use App\Models\User;
  6. use App\Services\StatisticalService;
  7. use Illuminate\Console\Command;
  8. use App\Models\Order;
  9. use App\Models\Stat;
  10. use App\Models\CommissionLog;
  11. use Illuminate\Support\Facades\DB;
  12. class V2boardStatistics extends Command
  13. {
  14. /**
  15. * The name and signature of the console command.
  16. *
  17. * @var string
  18. */
  19. protected $signature = 'v2board:statistics';
  20. /**
  21. * The console command description.
  22. *
  23. * @var string
  24. */
  25. protected $description = '统计任务';
  26. /**
  27. * Create a new command instance.
  28. *
  29. * @return void
  30. */
  31. public function __construct()
  32. {
  33. parent::__construct();
  34. }
  35. /**
  36. * Execute the console command.
  37. *
  38. * @return mixed
  39. */
  40. public function handle()
  41. {
  42. $startAt = microtime(true);
  43. ini_set('memory_limit', -1);
  44. $this->statUser();
  45. $this->statServer();
  46. $this->stat();
  47. $this->info('耗时' . (microtime(true) - $startAt));
  48. }
  49. private function statServer()
  50. {
  51. $createdAt = time();
  52. $recordAt = strtotime('-1 day', strtotime(date('Y-m-d')));
  53. $statService = new StatisticalService();
  54. $statService->setStartAt($recordAt);
  55. $statService->setServerStats();
  56. $stats = $statService->getStatServer();
  57. DB::beginTransaction();
  58. foreach ($stats as $stat) {
  59. if (!StatServer::insert([
  60. 'server_id' => $stat['server_id'],
  61. 'server_type' => $stat['server_type'],
  62. 'u' => $stat['u'],
  63. 'd' => $stat['d'],
  64. 'created_at' => $createdAt,
  65. 'updated_at' => $createdAt,
  66. 'record_type' => 'd',
  67. 'record_at' => $recordAt
  68. ])) {
  69. DB::rollback();
  70. throw new \Exception('stat server fail');
  71. }
  72. }
  73. DB::commit();
  74. $statService->clearStatServer();
  75. }
  76. private function statUser()
  77. {
  78. $createdAt = time();
  79. $recordAt = strtotime('-1 day', strtotime(date('Y-m-d')));
  80. $statService = new StatisticalService();
  81. $statService->setStartAt($recordAt);
  82. $statService->setUserStats();
  83. $stats = $statService->getStatUser();
  84. DB::beginTransaction();
  85. foreach ($stats as $stat) {
  86. if (!StatUser::insert([
  87. 'user_id' => $stat['user_id'],
  88. 'u' => $stat['u'],
  89. 'd' => $stat['d'],
  90. 'server_rate' => $stat['server_rate'],
  91. 'created_at' => $createdAt,
  92. 'updated_at' => $createdAt,
  93. 'record_type' => 'd',
  94. 'record_at' => $recordAt
  95. ])) {
  96. DB::rollback();
  97. throw new \Exception('stat user fail');
  98. }
  99. }
  100. DB::commit();
  101. $statService->clearStatUser();
  102. }
  103. private function stat()
  104. {
  105. $endAt = strtotime(date('Y-m-d'));
  106. $startAt = strtotime('-1 day', $endAt);
  107. $statisticalService = new StatisticalService();
  108. $statisticalService->setStartAt($startAt);
  109. $statisticalService->setEndAt($endAt);
  110. $data = $statisticalService->generateStatData();
  111. $statistic = Stat::where('record_at', $startAt)
  112. ->where('record_type', 'd')
  113. ->first();
  114. if ($statistic) {
  115. $statistic->update($data);
  116. return;
  117. }
  118. Stat::create($data);
  119. }
  120. }