UserService.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. private function calcResetDayByMonthFirstDay()
  16. {
  17. $today = date('d');
  18. $lastDay = date('d', strtotime('last day of +0 months'));
  19. return $lastDay - $today;
  20. }
  21. private function calcResetDayByExpireDay(int $expiredAt)
  22. {
  23. $day = date('d', $expiredAt);
  24. $today = date('d');
  25. $lastDay = date('d', strtotime('last day of +0 months'));
  26. if ((int)$day >= (int)$today && (int)$day >= (int)$lastDay) {
  27. return $lastDay - $today;
  28. }
  29. if ((int)$day >= (int)$today) {
  30. return $day - $today;
  31. } else {
  32. return $lastDay - $today + $day;
  33. }
  34. }
  35. private function calcResetDayByYearFirstDay()
  36. {
  37. $nextYear = strtotime(date("Y-01-01", strtotime('+1 year')));
  38. return (int)(($nextYear - time()) / 86400);
  39. }
  40. private function calcResetDayByYearExpiredAt(int $expiredAt)
  41. {
  42. $md = date('m-d', $expiredAt);
  43. $nowYear = strtotime(date("Y-{$md}"));
  44. $nextYear = strtotime('+1 year', $nowYear);
  45. return (int)(($nextYear - time()) / 86400);
  46. }
  47. public function getResetDay(User $user)
  48. {
  49. if ($user->expired_at <= time() || $user->expired_at === NULL) return null;
  50. // if reset method is not reset
  51. if (!isset($user->plan->reset_traffic_method)) return null;
  52. if ($user->plan->reset_traffic_method === 2) return null;
  53. switch (true) {
  54. case ($user->plan->reset_traffic_method === NULL): {
  55. $resetTrafficMethod = config('v2board.reset_traffic_method', 0);
  56. switch ((int)$resetTrafficMethod) {
  57. // month first day
  58. case 0:
  59. return $this->calcResetDayByMonthFirstDay();
  60. // expire day
  61. case 1:
  62. return $this->calcResetDayByExpireDay($user->expired_at);
  63. // no action
  64. case 2:
  65. return null;
  66. // year first day
  67. case 3:
  68. return $this->calcResetDayByYearFirstDay();
  69. // year expire day
  70. case 4:
  71. return $this->calcResetDayByYearExpiredAt($user->expired_at);
  72. }
  73. break;
  74. }
  75. case ($user->plan->reset_traffic_method === 0): {
  76. return $this->calcResetDayByMonthFirstDay();
  77. }
  78. case ($user->plan->reset_traffic_method === 1): {
  79. return $this->calcResetDayByExpireDay($user->expired_at);
  80. }
  81. case ($user->plan->reset_traffic_method === 2): {
  82. return null;
  83. }
  84. case ($user->plan->reset_traffic_method === 3): {
  85. return $this->calcResetDayByYearFirstDay();
  86. }
  87. case ($user->plan->reset_traffic_method === 4): {
  88. return $this->calcResetDayByYearExpiredAt($user->expired_at);
  89. }
  90. }
  91. return null;
  92. }
  93. public function isAvailable(User $user)
  94. {
  95. if (!$user->banned && $user->transfer_enable && ($user->expired_at > time() || $user->expired_at === NULL)) {
  96. return true;
  97. }
  98. return false;
  99. }
  100. public function getAvailableUsers()
  101. {
  102. return User::whereRaw('u + d < transfer_enable')
  103. ->where(function ($query) {
  104. $query->where('expired_at', '>=', time())
  105. ->orWhere('expired_at', NULL);
  106. })
  107. ->where('banned', 0)
  108. ->get();
  109. }
  110. public function getUnAvailbaleUsers()
  111. {
  112. return User::where(function ($query) {
  113. $query->where('expired_at', '<', time())
  114. ->orWhere('expired_at', 0);
  115. })
  116. ->where(function ($query) {
  117. $query->where('plan_id', NULL)
  118. ->orWhere('transfer_enable', 0);
  119. })
  120. ->get();
  121. }
  122. public function getUsersByIds($ids)
  123. {
  124. return User::whereIn('id', $ids)->get();
  125. }
  126. public function getAllUsers()
  127. {
  128. return User::all();
  129. }
  130. public function addBalance(int $userId, int $balance):bool
  131. {
  132. $user = User::lockForUpdate()->find($userId);
  133. if (!$user) {
  134. return false;
  135. }
  136. $user->balance = $user->balance + $balance;
  137. if ($user->balance < 0) {
  138. return false;
  139. }
  140. if (!$user->save()) {
  141. return false;
  142. }
  143. return true;
  144. }
  145. public function isNotCompleteOrderByUserId(int $userId):bool
  146. {
  147. $order = Order::whereIn('status', [0, 1])
  148. ->where('user_id', $userId)
  149. ->first();
  150. if (!$order) {
  151. return false;
  152. }
  153. return true;
  154. }
  155. public function trafficFetch(int $u, int $d, int $userId, array $server, string $protocol)
  156. {
  157. TrafficFetchJob::dispatch($u, $d, $userId, $server, $protocol);
  158. StatServerJob::dispatch($u, $d, $server, $protocol, 'd');
  159. StatUserJob::dispatch($u, $d, $userId, $server, $protocol, 'd');
  160. }
  161. }