UserService.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 ($user->plan->reset_traffic_method === 2) return null;
  52. switch (true) {
  53. case ($user->plan->reset_traffic_method === NULL): {
  54. $resetTrafficMethod = config('v2board.reset_traffic_method', 0);
  55. switch ((int)$resetTrafficMethod) {
  56. // month first day
  57. case 0:
  58. return $this->calcResetDayByMonthFirstDay();
  59. // expire day
  60. case 1:
  61. return $this->calcResetDayByExpireDay($user->expired_at);
  62. // no action
  63. case 2:
  64. return null;
  65. // year first day
  66. case 3:
  67. return $this->calcResetDayByYearFirstDay();
  68. // year expire day
  69. case 4:
  70. return $this->calcResetDayByYearExpiredAt($user->expired_at);
  71. }
  72. break;
  73. }
  74. case ($user->plan->reset_traffic_method === 0): {
  75. return $this->calcResetDayByMonthFirstDay();
  76. }
  77. case ($user->plan->reset_traffic_method === 1): {
  78. return $this->calcResetDayByExpireDay($user->expired_at);
  79. }
  80. case ($user->plan->reset_traffic_method === 2): {
  81. return null;
  82. }
  83. case ($user->plan->reset_traffic_method === 3): {
  84. return $this->calcResetDayByYearFirstDay();
  85. }
  86. case ($user->plan->reset_traffic_method === 4): {
  87. return $this->calcResetDayByYearExpiredAt($user->expired_at);
  88. }
  89. }
  90. return null;
  91. }
  92. public function isAvailable(User $user)
  93. {
  94. if (!$user->banned && $user->transfer_enable && ($user->expired_at > time() || $user->expired_at === NULL)) {
  95. return true;
  96. }
  97. return false;
  98. }
  99. public function getAvailableUsers()
  100. {
  101. return User::whereRaw('u + d < transfer_enable')
  102. ->where(function ($query) {
  103. $query->where('expired_at', '>=', time())
  104. ->orWhere('expired_at', NULL);
  105. })
  106. ->where('banned', 0)
  107. ->get();
  108. }
  109. public function getUnAvailbaleUsers()
  110. {
  111. return User::where(function ($query) {
  112. $query->where('expired_at', '<', time())
  113. ->orWhere('expired_at', 0);
  114. })
  115. ->where(function ($query) {
  116. $query->where('plan_id', NULL)
  117. ->orWhere('transfer_enable', 0);
  118. })
  119. ->get();
  120. }
  121. public function getUsersByIds($ids)
  122. {
  123. return User::whereIn('id', $ids)->get();
  124. }
  125. public function getAllUsers()
  126. {
  127. return User::all();
  128. }
  129. public function addBalance(int $userId, int $balance):bool
  130. {
  131. $user = User::lockForUpdate()->find($userId);
  132. if (!$user) {
  133. return false;
  134. }
  135. $user->balance = $user->balance + $balance;
  136. if ($user->balance < 0) {
  137. return false;
  138. }
  139. if (!$user->save()) {
  140. return false;
  141. }
  142. return true;
  143. }
  144. public function isNotCompleteOrderByUserId(int $userId):bool
  145. {
  146. $order = Order::whereIn('status', [0, 1])
  147. ->where('user_id', $userId)
  148. ->first();
  149. if (!$order) {
  150. return false;
  151. }
  152. return true;
  153. }
  154. public function trafficFetch(int $u, int $d, int $userId, array $server, string $protocol)
  155. {
  156. TrafficFetchJob::dispatch($u, $d, $userId, $server, $protocol);
  157. StatServerJob::dispatch($u, $d, $server, $protocol, 'd');
  158. StatUserJob::dispatch($u, $d, $userId, $server, $protocol, 'd');
  159. }
  160. }