2023-11-17 14:44:01 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
2025-01-21 14:57:54 +08:00
|
|
|
use App\Jobs\StatServerJob;
|
|
|
|
|
use App\Jobs\StatUserJob;
|
|
|
|
|
use App\Jobs\TrafficFetchJob;
|
2023-11-17 14:44:01 +08:00
|
|
|
use App\Models\Order;
|
|
|
|
|
use App\Models\Plan;
|
|
|
|
|
use App\Models\User;
|
2025-02-06 17:48:17 +08:00
|
|
|
use App\Services\Plugin\HookManager;
|
2025-06-22 01:18:38 +08:00
|
|
|
use App\Services\TrafficResetService;
|
|
|
|
|
use App\Models\TrafficResetLog;
|
2023-11-17 14:44:01 +08:00
|
|
|
|
|
|
|
|
class UserService
|
|
|
|
|
{
|
2025-06-22 01:18:38 +08:00
|
|
|
/**
|
|
|
|
|
* Get the remaining days until the next traffic reset for a user.
|
|
|
|
|
* This method reuses the TrafficResetService logic for consistency.
|
|
|
|
|
*/
|
2025-05-07 19:48:19 +08:00
|
|
|
public function getResetDay(User $user): ?int
|
2023-11-17 14:44:01 +08:00
|
|
|
{
|
2025-06-22 01:18:38 +08:00
|
|
|
// Use TrafficResetService to calculate the next reset time
|
|
|
|
|
$trafficResetService = app(TrafficResetService::class);
|
|
|
|
|
$nextResetTime = $trafficResetService->calculateNextResetTime($user);
|
|
|
|
|
|
|
|
|
|
if (!$nextResetTime) {
|
2024-04-27 17:06:57 +08:00
|
|
|
return null;
|
2025-05-07 19:48:19 +08:00
|
|
|
}
|
2025-06-22 01:18:38 +08:00
|
|
|
|
|
|
|
|
// Calculate the remaining days from now to the next reset time
|
|
|
|
|
$now = time();
|
|
|
|
|
$resetTimestamp = $nextResetTime->timestamp;
|
|
|
|
|
|
|
|
|
|
if ($resetTimestamp <= $now) {
|
|
|
|
|
return 0; // Reset time has passed or is now
|
2023-11-17 14:44:01 +08:00
|
|
|
}
|
2025-05-07 19:48:19 +08:00
|
|
|
|
2025-06-22 01:18:38 +08:00
|
|
|
// Calculate the difference in days (rounded up)
|
|
|
|
|
$daysDifference = ceil(($resetTimestamp - $now) / 86400);
|
|
|
|
|
|
|
|
|
|
return (int) $daysDifference;
|
2023-11-17 14:44:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function isAvailable(User $user)
|
|
|
|
|
{
|
|
|
|
|
if (!$user->banned && $user->transfer_enable && ($user->expired_at > time() || $user->expired_at === NULL)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getAvailableUsers()
|
|
|
|
|
{
|
|
|
|
|
return User::whereRaw('u + d < transfer_enable')
|
|
|
|
|
->where(function ($query) {
|
|
|
|
|
$query->where('expired_at', '>=', time())
|
|
|
|
|
->orWhere('expired_at', NULL);
|
|
|
|
|
})
|
|
|
|
|
->where('banned', 0)
|
|
|
|
|
->get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getUnAvailbaleUsers()
|
|
|
|
|
{
|
|
|
|
|
return User::where(function ($query) {
|
|
|
|
|
$query->where('expired_at', '<', time())
|
|
|
|
|
->orWhere('expired_at', 0);
|
|
|
|
|
})
|
|
|
|
|
->where(function ($query) {
|
2024-04-27 17:06:57 +08:00
|
|
|
$query->where('plan_id', NULL)
|
|
|
|
|
->orWhere('transfer_enable', 0);
|
|
|
|
|
})
|
2023-11-17 14:44:01 +08:00
|
|
|
->get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getUsersByIds($ids)
|
|
|
|
|
{
|
|
|
|
|
return User::whereIn('id', $ids)->get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getAllUsers()
|
|
|
|
|
{
|
|
|
|
|
return User::all();
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-27 17:06:57 +08:00
|
|
|
public function addBalance(int $userId, int $balance): bool
|
2023-11-17 14:44:01 +08:00
|
|
|
{
|
|
|
|
|
$user = User::lockForUpdate()->find($userId);
|
|
|
|
|
if (!$user) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$user->balance = $user->balance + $balance;
|
|
|
|
|
if ($user->balance < 0) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (!$user->save()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-27 17:06:57 +08:00
|
|
|
public function isNotCompleteOrderByUserId(int $userId): bool
|
2023-11-17 14:44:01 +08:00
|
|
|
{
|
|
|
|
|
$order = Order::whereIn('status', [0, 1])
|
|
|
|
|
->where('user_id', $userId)
|
|
|
|
|
->first();
|
|
|
|
|
if (!$order) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-21 14:57:54 +08:00
|
|
|
public function trafficFetch(array $server, string $protocol, array $data)
|
2023-11-17 14:44:01 +08:00
|
|
|
{
|
2025-02-06 17:48:17 +08:00
|
|
|
list($server, $protocol, $data) = HookManager::filter('traffic.before_process', [
|
|
|
|
|
$server,
|
|
|
|
|
$protocol,
|
|
|
|
|
$data
|
|
|
|
|
]);
|
2024-04-27 17:06:57 +08:00
|
|
|
|
2023-11-21 15:59:06 +08:00
|
|
|
$timestamp = strtotime(date('Y-m-d'));
|
2025-01-21 14:57:54 +08:00
|
|
|
collect($data)->chunk(1000)->each(function ($chunk) use ($timestamp, $server, $protocol) {
|
|
|
|
|
TrafficFetchJob::dispatch($server, $chunk->toArray(), $protocol, $timestamp);
|
|
|
|
|
StatUserJob::dispatch($server, $chunk->toArray(), $protocol, 'd');
|
|
|
|
|
StatServerJob::dispatch($server, $chunk->toArray(), $protocol, 'd');
|
2023-11-21 15:59:06 +08:00
|
|
|
});
|
2023-11-17 14:44:01 +08:00
|
|
|
}
|
2025-06-22 01:18:38 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取用户流量信息(增加重置检查)
|
|
|
|
|
*/
|
|
|
|
|
public function getUserTrafficInfo(User $user): array
|
|
|
|
|
{
|
|
|
|
|
// 检查是否需要重置流量
|
|
|
|
|
app(TrafficResetService::class)->checkAndReset($user, TrafficResetLog::SOURCE_USER_ACCESS);
|
|
|
|
|
|
|
|
|
|
// 重新获取用户数据(可能已被重置)
|
|
|
|
|
$user->refresh();
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'upload' => $user->u ?? 0,
|
|
|
|
|
'download' => $user->d ?? 0,
|
|
|
|
|
'total_used' => $user->getTotalUsedTraffic(),
|
|
|
|
|
'total_available' => $user->transfer_enable ?? 0,
|
|
|
|
|
'remaining' => $user->getRemainingTraffic(),
|
|
|
|
|
'usage_percentage' => $user->getTrafficUsagePercentage(),
|
2025-06-22 17:38:17 +08:00
|
|
|
'next_reset_at' => $user->next_reset_at,
|
|
|
|
|
'last_reset_at' => $user->last_reset_at,
|
2025-06-22 01:18:38 +08:00
|
|
|
'reset_count' => $user->reset_count,
|
|
|
|
|
];
|
|
|
|
|
}
|
2023-11-17 14:44:01 +08:00
|
|
|
}
|