v2board/app/Services/UserService.php

113 lines
2.7 KiB
PHP
Raw Normal View History

2020-02-28 01:12:55 +08:00
<?php
namespace App\Services;
2021-07-01 19:06:09 +08:00
use App\Models\InviteCode;
2020-04-22 16:57:00 +08:00
use App\Models\Order;
2020-11-09 00:12:19 +08:00
use App\Models\Server;
2021-07-01 19:06:09 +08:00
use App\Models\Ticket;
2020-02-28 01:12:55 +08:00
use App\Models\User;
2021-07-01 19:06:09 +08:00
use Illuminate\Support\Facades\DB;
2020-02-28 01:12:55 +08:00
class UserService
{
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
{
$user = User::find($userId);
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
2020-11-09 20:44:03 +08:00
public function trafficFetch(int $u, int $d, int $userId, object $server, string $protocol):bool
2020-04-25 19:44:47 +08:00
{
2020-11-09 00:12:19 +08:00
$user = User::lockForUpdate()
->find($userId);
2020-04-25 19:44:47 +08:00
if (!$user) {
2020-10-13 23:55:17 +08:00
return true;
2020-04-25 19:44:47 +08:00
}
$user->t = time();
$user->u = $user->u + $u;
$user->d = $user->d + $d;
if (!$user->save()) {
return false;
}
2020-11-09 00:12:19 +08:00
$mailService = new MailService();
$serverService = new ServerService();
2020-11-07 16:04:42 +08:00
try {
$mailService->remindTraffic($user);
2020-11-09 00:12:19 +08:00
$serverService->log(
$userId,
$server->id,
$u,
$d,
$server->rate,
$protocol
);
2020-11-07 16:04:42 +08:00
} catch (\Exception $e) {
}
2020-04-25 19:44:47 +08:00
return true;
}
2020-02-28 01:12:55 +08:00
}