UserService.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 isAvailable(User $user)
  16. {
  17. if (!$user->banned && $user->transfer_enable && ($user->expired_at > time() || $user->expired_at === NULL)) {
  18. return true;
  19. }
  20. return false;
  21. }
  22. public function getAvailableUsers()
  23. {
  24. return User::whereRaw('u + d < transfer_enable')
  25. ->where(function ($query) {
  26. $query->where('expired_at', '>=', time())
  27. ->orWhere('expired_at', NULL);
  28. })
  29. ->where('banned', 0)
  30. ->get();
  31. }
  32. public function getUnAvailbaleUsers()
  33. {
  34. return User::where(function ($query) {
  35. $query->where('expired_at', '<', time())
  36. ->orWhere('expired_at', 0);
  37. })
  38. ->where(function ($query) {
  39. $query->where('plan_id', NULL)
  40. ->orWhere('transfer_enable', 0);
  41. })
  42. ->get();
  43. }
  44. public function getUsersByIds($ids)
  45. {
  46. return User::whereIn('id', $ids)->get();
  47. }
  48. public function getAllUsers()
  49. {
  50. return User::all();
  51. }
  52. public function addBalance(int $userId, int $balance):bool
  53. {
  54. $user = User::lockForUpdate()->find($userId);
  55. if (!$user) {
  56. return false;
  57. }
  58. $user->balance = $user->balance + $balance;
  59. if ($user->balance < 0) {
  60. return false;
  61. }
  62. if (!$user->save()) {
  63. return false;
  64. }
  65. return true;
  66. }
  67. public function isNotCompleteOrderByUserId(int $userId):bool
  68. {
  69. $order = Order::whereIn('status', [0, 1])
  70. ->where('user_id', $userId)
  71. ->first();
  72. if (!$order) {
  73. return false;
  74. }
  75. return true;
  76. }
  77. public function trafficFetch(int $u, int $d, int $userId, object $server, string $protocol)
  78. {
  79. TrafficFetchJob::dispatch($u, $d, $userId, $server, $protocol);
  80. StatServerJob::dispatch($u, $d, $server, $protocol, 'd');
  81. StatUserJob::dispatch($u, $d, $userId, $server, $protocol, 'd');
  82. }
  83. }