UserService.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace App\Services;
  3. use App\Jobs\ServerLogJob;
  4. use App\Jobs\StatServerJob;
  5. use App\Jobs\StatUserJob;
  6. use App\Jobs\TrafficFetchJob;
  7. use App\Models\InviteCode;
  8. use App\Models\Order;
  9. use App\Models\ServerV2ray;
  10. use App\Models\Ticket;
  11. use App\Models\User;
  12. use Illuminate\Support\Facades\DB;
  13. class UserService
  14. {
  15. public function getResetDay(User $user)
  16. {
  17. if ($user->expired_at <= time() || $user->expired_at === NULL) return null;
  18. // if reset method is not reset
  19. if (isset($user->plan->reset_traffic_method) && $user->plan->reset_traffic_method === 2) return null;
  20. if ((int)config('v2board.reset_traffic_method') === 0 ||
  21. (isset($user->plan->reset_traffic_method) && $user->plan->reset_traffic_method === 0))
  22. {
  23. $day = date('d', $user->expired_at);
  24. $today = date('d');
  25. $lastDay = date('d', strtotime('last day of +0 months'));
  26. return $lastDay - $today;
  27. }
  28. if ((int)config('v2board.reset_traffic_method') === 1 ||
  29. (isset($user->plan->reset_traffic_method) && $user->plan->reset_traffic_method === 1))
  30. {
  31. $day = date('d', $user->expired_at);
  32. $today = date('d');
  33. $lastDay = date('d', strtotime('last day of +0 months'));
  34. if ((int)$day >= (int)$today && (int)$day >= (int)$lastDay) {
  35. return $lastDay - $today;
  36. }
  37. if ((int)$day >= (int)$today) {
  38. return $day - $today;
  39. } else {
  40. return $lastDay - $today + $day;
  41. }
  42. }
  43. if ((int)config('v2board.reset_traffic_method') === 2 ||
  44. (isset($user->plan->reset_traffic_method) && $user->plan->reset_traffic_method === 2))
  45. {
  46. $nextYear = strtotime(date("Y-01-01", strtotime('+1 year')));
  47. return (int)(($nextYear - time()) / 86400);
  48. }
  49. return null;
  50. }
  51. public function isAvailable(User $user)
  52. {
  53. if (!$user->banned && $user->transfer_enable && ($user->expired_at > time() || $user->expired_at === NULL)) {
  54. return true;
  55. }
  56. return false;
  57. }
  58. public function getAvailableUsers()
  59. {
  60. return User::whereRaw('u + d < transfer_enable')
  61. ->where(function ($query) {
  62. $query->where('expired_at', '>=', time())
  63. ->orWhere('expired_at', NULL);
  64. })
  65. ->where('banned', 0)
  66. ->get();
  67. }
  68. public function getUnAvailbaleUsers()
  69. {
  70. return User::where(function ($query) {
  71. $query->where('expired_at', '<', time())
  72. ->orWhere('expired_at', 0);
  73. })
  74. ->where(function ($query) {
  75. $query->where('plan_id', NULL)
  76. ->orWhere('transfer_enable', 0);
  77. })
  78. ->get();
  79. }
  80. public function getUsersByIds($ids)
  81. {
  82. return User::whereIn('id', $ids)->get();
  83. }
  84. public function getAllUsers()
  85. {
  86. return User::all();
  87. }
  88. public function addBalance(int $userId, int $balance):bool
  89. {
  90. $user = User::lockForUpdate()->find($userId);
  91. if (!$user) {
  92. return false;
  93. }
  94. $user->balance = $user->balance + $balance;
  95. if ($user->balance < 0) {
  96. return false;
  97. }
  98. if (!$user->save()) {
  99. return false;
  100. }
  101. return true;
  102. }
  103. public function isNotCompleteOrderByUserId(int $userId):bool
  104. {
  105. $order = Order::whereIn('status', [0, 1])
  106. ->where('user_id', $userId)
  107. ->first();
  108. if (!$order) {
  109. return false;
  110. }
  111. return true;
  112. }
  113. public function trafficFetch(int $u, int $d, int $userId, object $server, string $protocol)
  114. {
  115. TrafficFetchJob::dispatch($u, $d, $userId, $server, $protocol);
  116. StatServerJob::dispatch($u, $d, $server, $protocol, 'd');
  117. StatUserJob::dispatch($u, $d, $userId, $server, $protocol, 'd');
  118. }
  119. }