v2board/app/Services/UserService.php

187 lines
5.7 KiB
PHP
Raw Normal View History

2020-02-28 01:12:55 +08:00
<?php
namespace App\Services;
2022-02-20 01:06:02 +08:00
use App\Jobs\StatServerJob;
use App\Jobs\StatUserJob;
2021-08-31 23:55:07 +08:00
use App\Jobs\TrafficFetchJob;
2020-04-22 16:57:00 +08:00
use App\Models\Order;
2022-08-07 02:32:57 +08:00
use App\Models\Plan;
2020-02-28 01:12:55 +08:00
use App\Models\User;
class UserService
{
2022-07-28 15:05:48 +08:00
private function calcResetDayByMonthFirstDay()
2022-05-30 15:42:25 +08:00
{
2022-07-28 15:05:48 +08:00
$today = date('d');
$lastDay = date('d', strtotime('last day of +0 months'));
return $lastDay - $today;
}
2022-05-30 15:42:25 +08:00
2022-07-28 15:05:48 +08:00
private function calcResetDayByExpireDay(int $expiredAt)
{
$day = date('d', $expiredAt);
$today = date('d');
$lastDay = date('d', strtotime('last day of +0 months'));
if ((int)$day >= (int)$today && (int)$day >= (int)$lastDay) {
2022-05-30 15:42:25 +08:00
return $lastDay - $today;
}
2022-07-28 15:05:48 +08:00
if ((int)$day >= (int)$today) {
return $day - $today;
}
2022-12-22 21:57:20 +08:00
return $lastDay - $today + $day;
2022-07-28 15:05:48 +08:00
}
2022-08-07 02:32:57 +08:00
private function calcResetDayByYearFirstDay(): int
2022-07-28 15:05:48 +08:00
{
$nextYear = strtotime(date("Y-01-01", strtotime('+1 year')));
return (int)(($nextYear - time()) / 86400);
}
2022-08-07 02:32:57 +08:00
private function calcResetDayByYearExpiredAt(int $expiredAt): int
2022-07-28 15:05:48 +08:00
{
$md = date('m-d', $expiredAt);
$nowYear = strtotime(date("Y-{$md}"));
$nextYear = strtotime('+1 year', $nowYear);
if ($nowYear > time()) {
return (int)(($nowYear - time()) / 86400);
}
2022-07-28 15:05:48 +08:00
return (int)(($nextYear - time()) / 86400);
}
public function getResetDay(User $user)
{
2022-08-07 02:32:57 +08:00
if (!isset($user->plan)) {
$user->plan = Plan::find($user->plan_id);
}
2022-07-28 15:05:48 +08:00
if ($user->expired_at <= time() || $user->expired_at === NULL) return null;
// if reset method is not reset
if ($user->plan->reset_traffic_method === 2) return null;
switch (true) {
case ($user->plan->reset_traffic_method === NULL): {
$resetTrafficMethod = config('v2board.reset_traffic_method', 0);
switch ((int)$resetTrafficMethod) {
// month first day
case 0:
return $this->calcResetDayByMonthFirstDay();
// expire day
case 1:
return $this->calcResetDayByExpireDay($user->expired_at);
// no action
case 2:
return null;
// year first day
case 3:
return $this->calcResetDayByYearFirstDay();
// year expire day
case 4:
return $this->calcResetDayByYearExpiredAt($user->expired_at);
}
break;
2022-05-30 15:42:25 +08:00
}
2022-07-28 15:05:48 +08:00
case ($user->plan->reset_traffic_method === 0): {
return $this->calcResetDayByMonthFirstDay();
}
case ($user->plan->reset_traffic_method === 1): {
return $this->calcResetDayByExpireDay($user->expired_at);
}
case ($user->plan->reset_traffic_method === 2): {
return null;
}
case ($user->plan->reset_traffic_method === 3): {
return $this->calcResetDayByYearFirstDay();
}
case ($user->plan->reset_traffic_method === 4): {
return $this->calcResetDayByYearExpiredAt($user->expired_at);
2022-05-30 15:42:25 +08:00
}
2022-06-08 02:19:19 +08:00
}
2022-05-30 15:42:25 +08:00
return null;
}
2020-02-28 17:16:05 +08:00
public function isAvailable(User $user)
{
2020-03-02 20:29:17 +08:00
if (!$user->banned && $user->transfer_enable && ($user->expired_at > time() || $user->expired_at === NULL)) {
2020-02-28 17:16:05 +08:00
return true;
}
return false;
}
2020-02-28 01:12:55 +08:00
2020-02-28 17:16:05 +08:00
public function getAvailableUsers()
2020-02-28 01:12:55 +08:00
{
2020-02-28 17:16:05 +08:00
return User::whereRaw('u + d < transfer_enable')
->where(function ($query) {
$query->where('expired_at', '>=', time())
2020-03-01 23:29:49 +08:00
->orWhere('expired_at', NULL);
2020-02-28 17:16:05 +08:00
})
2020-03-02 20:29:17 +08:00
->where('banned', 0)
2020-02-28 17:16:05 +08:00
->get();
2020-02-28 01:12:55 +08:00
}
2020-02-28 17:16:05 +08:00
public function getUnAvailbaleUsers()
2020-02-28 01:12:55 +08:00
{
2020-02-28 17:16:05 +08:00
return User::where(function ($query) {
$query->where('expired_at', '<', time())
->orWhere('expired_at', 0);
})
->where(function ($query) {
$query->where('plan_id', NULL)
->orWhere('transfer_enable', 0);
})
->get();
}
public function getUsersByIds($ids)
{
return User::whereIn('id', $ids)->get();
}
public function getAllUsers()
{
return User::all();
2020-02-28 01:12:55 +08:00
}
2020-03-17 20:00:46 +08:00
public function addBalance(int $userId, int $balance):bool
{
2021-07-11 00:13:09 +08:00
$user = User::lockForUpdate()->find($userId);
2020-03-17 20:00:46 +08:00
if (!$user) {
return false;
}
$user->balance = $user->balance + $balance;
if ($user->balance < 0) {
return false;
}
if (!$user->save()) {
return false;
}
return true;
}
2020-04-22 16:57:00 +08:00
public function isNotCompleteOrderByUserId(int $userId):bool
{
$order = Order::whereIn('status', [0, 1])
->where('user_id', $userId)
->first();
if (!$order) {
return false;
}
return true;
}
2020-04-25 19:44:47 +08:00
2023-05-17 11:28:59 +08:00
public function trafficFetch(array $server, string $protocol, array $data)
2020-04-25 19:44:47 +08:00
{
2023-05-17 11:28:59 +08:00
$statService = new StatisticalService();
$statService->setStartAt(strtotime(date('Y-m-d')));
$statService->setUserStats();
$statService->setServerStats();
foreach (array_keys($data) as $userId) {
2023-08-20 03:21:33 +08:00
$u = (int)$data[$userId][0] ?? 0;
$d = (int)$data[$userId][1] ?? 0;
if (!$u && !$d) continue;
2023-05-17 11:28:59 +08:00
TrafficFetchJob::dispatch($u, $d, $userId, $server, $protocol);
$statService->statServer($server['id'], $protocol, $u, $d);
$statService->statUser($server['rate'], $userId, $u, $d);
}
2020-04-25 19:44:47 +08:00
}
2020-02-28 01:12:55 +08:00
}