UserService.php 5.4 KB

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