From f7959dcd9382c5fdddaf1d51fc04fdcb0589d2c3 Mon Sep 17 00:00:00 2001 From: Tokumeikoi Date: Mon, 11 May 2020 17:19:58 +0800 Subject: [PATCH] update --- app/Models/ServerLog.php | 1 + app/Services/ServerService.php | 29 ++++++++++++++++++++++------- database/install.sql | 1 + database/update.sql | 3 +++ 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/app/Models/ServerLog.php b/app/Models/ServerLog.php index a97f4054..635f6266 100644 --- a/app/Models/ServerLog.php +++ b/app/Models/ServerLog.php @@ -3,6 +3,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Facades\DB; class ServerLog extends Model { diff --git a/app/Services/ServerService.php b/app/Services/ServerService.php index 34405ca2..6684603b 100644 --- a/app/Services/ServerService.php +++ b/app/Services/ServerService.php @@ -138,12 +138,27 @@ class ServerService public function log(int $userId, int $serverId, int $u, int $d, float $rate) { - $serverLog = new ServerLog(); - $serverLog->user_id = $userId; - $serverLog->server_id = $serverId; - $serverLog->u = $u; - $serverLog->d = $d; - $serverLog->rate = $rate; - $serverLog->save(); + if (($u + $d) <= 10240) return; + $timestamp = strtotime(date('Y-m-d H:0')); + $serverLog = ServerLog::where('log_at', '>=', $timestamp) + ->where('log_at', '<', $timestamp + 3600) + ->where('server_id', $serverId) + ->where('user_id', $userId) + ->where('rate', $rate) + ->first(); + if ($serverLog) { + $serverLog->u = $serverLog->u + $u; + $serverLog->d = $serverLog->d + $u; + $serverLog->save(); + } else { + $serverLog = new ServerLog(); + $serverLog->user_id = $userId; + $serverLog->server_id = $serverId; + $serverLog->u = $u; + $serverLog->d = $d; + $serverLog->rate = $rate; + $serverLog->log_at = $timestamp; + $serverLog->save(); + } } } diff --git a/database/install.sql b/database/install.sql index f728268f..32c8c6e9 100644 --- a/database/install.sql +++ b/database/install.sql @@ -165,6 +165,7 @@ CREATE TABLE `v2_server_log` ( `u` varchar(255) NOT NULL, `d` varchar(255) NOT NULL, `rate` decimal(10,2) NOT NULL, + `log_at` int(11) NOT NULL, `created_at` int(11) NOT NULL, `updated_at` int(11) NOT NULL, PRIMARY KEY (`id`) diff --git a/database/update.sql b/database/update.sql index cd4116ed..da3cb9ee 100644 --- a/database/update.sql +++ b/database/update.sql @@ -238,3 +238,6 @@ ADD `reset_price` int(11) NULL AFTER `onetime_price`; ALTER TABLE `v2_server_log` ADD `id` bigint NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST; + +ALTER TABLE `v2_server_log` +ADD `log_at` int(11) NOT NULL AFTER `rate`;