UserService.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace App\Services;
  3. use App\Models\InviteCode;
  4. use App\Models\Order;
  5. use App\Models\Server;
  6. use App\Models\Ticket;
  7. use App\Models\User;
  8. use Illuminate\Support\Facades\DB;
  9. class UserService
  10. {
  11. public function isAvailable(User $user)
  12. {
  13. if (!$user->banned && $user->transfer_enable && ($user->expired_at > time() || $user->expired_at === NULL)) {
  14. return true;
  15. }
  16. return false;
  17. }
  18. public function getAvailableUsers()
  19. {
  20. return User::whereRaw('u + d < transfer_enable')
  21. ->where(function ($query) {
  22. $query->where('expired_at', '>=', time())
  23. ->orWhere('expired_at', NULL);
  24. })
  25. ->where('banned', 0)
  26. ->get();
  27. }
  28. public function getUnAvailbaleUsers()
  29. {
  30. return User::where(function ($query) {
  31. $query->where('expired_at', '<', time())
  32. ->orWhere('expired_at', 0);
  33. })
  34. ->where(function ($query) {
  35. $query->where('plan_id', NULL)
  36. ->orWhere('transfer_enable', 0);
  37. })
  38. ->get();
  39. }
  40. public function getUsersByIds($ids)
  41. {
  42. return User::whereIn('id', $ids)->get();
  43. }
  44. public function getAllUsers()
  45. {
  46. return User::all();
  47. }
  48. public function addBalance(int $userId, int $balance):bool
  49. {
  50. $user = User::lockForUpdate()->find($userId);
  51. if (!$user) {
  52. return false;
  53. }
  54. $user->balance = $user->balance + $balance;
  55. if ($user->balance < 0) {
  56. return false;
  57. }
  58. if (!$user->save()) {
  59. return false;
  60. }
  61. return true;
  62. }
  63. public function isNotCompleteOrderByUserId(int $userId):bool
  64. {
  65. $order = Order::whereIn('status', [0, 1])
  66. ->where('user_id', $userId)
  67. ->first();
  68. if (!$order) {
  69. return false;
  70. }
  71. return true;
  72. }
  73. public function trafficFetch(int $u, int $d, int $userId, object $server, string $protocol):bool
  74. {
  75. $user = User::lockForUpdate()
  76. ->find($userId);
  77. if (!$user) {
  78. return true;
  79. }
  80. $user->t = time();
  81. $user->u = $user->u + $u;
  82. $user->d = $user->d + $d;
  83. if (!$user->save()) {
  84. return false;
  85. }
  86. $mailService = new MailService();
  87. $serverService = new ServerService();
  88. try {
  89. $mailService->remindTraffic($user);
  90. $serverService->log(
  91. $userId,
  92. $server->id,
  93. $u,
  94. $d,
  95. $server->rate,
  96. $protocol
  97. );
  98. } catch (\Exception $e) {
  99. }
  100. return true;
  101. }
  102. }