UserService.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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') === 3 ||
  44. (isset($user->plan->reset_traffic_method) && $user->plan->reset_traffic_method === 3))
  45. {
  46. $nextYear = strtotime(date("Y-01-01", strtotime('+1 year')));
  47. return (int)(($nextYear - time()) / 86400);
  48. }
  49. if ((int)config('v2board.reset_traffic_method') === 4 ||
  50. (isset($user->plan->reset_traffic_method) && $user->plan->reset_traffic_method === 4))
  51. {
  52. $md = date('m-d', $user->expired_at);
  53. $nowYear = strtotime(date("Y-{$md}"));
  54. $nextYear = strtotime('+1 year', $nowYear);
  55. return (int)(($nextYear - time()) / 86400);
  56. }
  57. return null;
  58. }
  59. public function isAvailable(User $user)
  60. {
  61. if (!$user->banned && $user->transfer_enable && ($user->expired_at > time() || $user->expired_at === NULL)) {
  62. return true;
  63. }
  64. return false;
  65. }
  66. public function getAvailableUsers()
  67. {
  68. return User::whereRaw('u + d < transfer_enable')
  69. ->where(function ($query) {
  70. $query->where('expired_at', '>=', time())
  71. ->orWhere('expired_at', NULL);
  72. })
  73. ->where('banned', 0)
  74. ->get();
  75. }
  76. public function getUnAvailbaleUsers()
  77. {
  78. return User::where(function ($query) {
  79. $query->where('expired_at', '<', time())
  80. ->orWhere('expired_at', 0);
  81. })
  82. ->where(function ($query) {
  83. $query->where('plan_id', NULL)
  84. ->orWhere('transfer_enable', 0);
  85. })
  86. ->get();
  87. }
  88. public function getUsersByIds($ids)
  89. {
  90. return User::whereIn('id', $ids)->get();
  91. }
  92. public function getAllUsers()
  93. {
  94. return User::all();
  95. }
  96. public function addBalance(int $userId, int $balance):bool
  97. {
  98. $user = User::lockForUpdate()->find($userId);
  99. if (!$user) {
  100. return false;
  101. }
  102. $user->balance = $user->balance + $balance;
  103. if ($user->balance < 0) {
  104. return false;
  105. }
  106. if (!$user->save()) {
  107. return false;
  108. }
  109. return true;
  110. }
  111. public function isNotCompleteOrderByUserId(int $userId):bool
  112. {
  113. $order = Order::whereIn('status', [0, 1])
  114. ->where('user_id', $userId)
  115. ->first();
  116. if (!$order) {
  117. return false;
  118. }
  119. return true;
  120. }
  121. public function trafficFetch(int $u, int $d, int $userId, object $server, string $protocol)
  122. {
  123. TrafficFetchJob::dispatch($u, $d, $userId, $server, $protocol);
  124. StatServerJob::dispatch($u, $d, $server, $protocol, 'd');
  125. StatUserJob::dispatch($u, $d, $userId, $server, $protocol, 'd');
  126. }
  127. }