v2board/app/Console/Commands/V2boardStatistics.php

132 lines
3.6 KiB
PHP
Raw Normal View History

2020-12-20 16:57:33 +08:00
<?php
namespace App\Console\Commands;
2023-05-03 15:37:19 +08:00
use App\Models\StatServer;
2023-04-15 19:34:54 +08:00
use App\Models\StatUser;
2023-05-03 15:37:19 +08:00
use App\Models\User;
2023-04-15 19:34:54 +08:00
use App\Services\StatisticalService;
2020-12-20 16:57:33 +08:00
use Illuminate\Console\Command;
2020-12-22 15:21:18 +08:00
use App\Models\Order;
2023-05-03 15:37:19 +08:00
use App\Models\Stat;
2022-03-11 13:34:10 +08:00
use App\Models\CommissionLog;
2023-04-15 19:34:54 +08:00
use Illuminate\Support\Facades\DB;
2020-12-20 16:57:33 +08:00
class V2boardStatistics extends Command
2020-12-20 16:57:33 +08:00
{
/**
* The name and signature of the console command.
*
* @var string
*/
2020-12-20 21:06:23 +08:00
protected $signature = 'v2board:statistics';
2020-12-20 16:57:33 +08:00
/**
* The console command description.
*
* @var string
*/
2020-12-20 21:06:23 +08:00
protected $description = '统计任务';
2020-12-20 16:57:33 +08:00
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
2020-12-20 21:06:23 +08:00
{
2023-05-03 15:37:19 +08:00
$startAt = microtime(true);
2022-01-22 02:06:53 +08:00
ini_set('memory_limit', -1);
2023-04-17 21:02:57 +08:00
$this->statUser();
2023-05-03 15:37:19 +08:00
$this->statServer();
$this->stat();
$this->info('耗时' . (microtime(true) - $startAt));
}
private function statServer()
{
$createdAt = time();
$recordAt = strtotime('-1 day', strtotime(date('Y-m-d')));
$statService = new StatisticalService();
$statService->setStartAt($recordAt);
2023-05-03 15:43:20 +08:00
$statService->setServerStats();
$stats = $statService->getStatServer();
2023-05-03 15:37:19 +08:00
DB::beginTransaction();
foreach ($stats as $stat) {
if (!StatServer::insert([
'server_id' => $stat['server_id'],
'server_type' => $stat['server_type'],
'u' => $stat['u'],
'd' => $stat['d'],
'created_at' => $createdAt,
'updated_at' => $createdAt,
'record_type' => 'd',
'record_at' => $recordAt
])) {
DB::rollback();
throw new \Exception('stat server fail');
}
}
DB::commit();
$statService->clearStatServer();
2020-12-20 21:06:23 +08:00
}
2023-04-15 19:34:54 +08:00
private function statUser()
{
$createdAt = time();
$recordAt = strtotime('-1 day', strtotime(date('Y-m-d')));
2023-05-03 15:37:19 +08:00
$statService = new StatisticalService();
$statService->setStartAt($recordAt);
2023-05-03 15:43:20 +08:00
$statService->setUserStats();
2023-04-15 19:34:54 +08:00
$stats = $statService->getStatUser();
DB::beginTransaction();
foreach ($stats as $stat) {
if (!StatUser::insert([
2023-04-17 21:02:57 +08:00
'user_id' => $stat['user_id'],
2023-04-15 19:34:54 +08:00
'u' => $stat['u'],
'd' => $stat['d'],
'server_rate' => $stat['server_rate'],
'created_at' => $createdAt,
'updated_at' => $createdAt,
'record_type' => 'd',
'record_at' => $recordAt
])) {
DB::rollback();
2023-04-15 19:43:19 +08:00
throw new \Exception('stat user fail');
2023-04-15 19:34:54 +08:00
}
}
DB::commit();
2023-04-15 19:43:19 +08:00
$statService->clearStatUser();
2023-04-15 19:34:54 +08:00
}
2023-05-03 15:37:19 +08:00
private function stat()
2020-12-21 17:11:48 +08:00
{
2023-05-03 15:43:20 +08:00
$endAt = strtotime(date('Y-m-d'));
$startAt = strtotime('-1 day', $endAt);
2023-05-03 15:37:19 +08:00
$statisticalService = new StatisticalService();
2023-05-03 15:43:20 +08:00
$statisticalService->setStartAt($startAt);
$statisticalService->setEndAt($endAt);
2023-05-03 15:37:19 +08:00
$data = $statisticalService->generateStatData();
2023-05-03 21:34:29 +08:00
$data['record_at'] = $startAt;
$data['record_type'] = 'd';
2023-05-03 15:37:19 +08:00
$statistic = Stat::where('record_at', $startAt)
2020-12-24 23:41:32 +08:00
->where('record_type', 'd')
->first();
if ($statistic) {
$statistic->update($data);
return;
}
2023-05-03 15:37:19 +08:00
Stat::create($data);
2020-12-21 17:11:48 +08:00
}
2020-12-20 16:57:33 +08:00
}