UserService.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace App\Services;
  3. use App\Jobs\ServerLogJob;
  4. use App\Jobs\TrafficFetchJob;
  5. use App\Models\InviteCode;
  6. use App\Models\Order;
  7. use App\Models\ServerV2ray;
  8. use App\Models\Ticket;
  9. use App\Models\User;
  10. use Illuminate\Support\Facades\DB;
  11. class UserService
  12. {
  13. public function isAvailable(User $user)
  14. {
  15. if (!$user->banned && $user->transfer_enable && ($user->expired_at > time() || $user->expired_at === NULL)) {
  16. return true;
  17. }
  18. return false;
  19. }
  20. public function getAvailableUsers()
  21. {
  22. return User::whereRaw('u + d < transfer_enable')
  23. ->where(function ($query) {
  24. $query->where('expired_at', '>=', time())
  25. ->orWhere('expired_at', NULL);
  26. })
  27. ->where('banned', 0)
  28. ->get();
  29. }
  30. public function getUnAvailbaleUsers()
  31. {
  32. return User::where(function ($query) {
  33. $query->where('expired_at', '<', time())
  34. ->orWhere('expired_at', 0);
  35. })
  36. ->where(function ($query) {
  37. $query->where('plan_id', NULL)
  38. ->orWhere('transfer_enable', 0);
  39. })
  40. ->get();
  41. }
  42. public function getUsersByIds($ids)
  43. {
  44. return User::whereIn('id', $ids)->get();
  45. }
  46. public function getAllUsers()
  47. {
  48. return User::all();
  49. }
  50. public function addBalance(int $userId, int $balance):bool
  51. {
  52. $user = User::lockForUpdate()->find($userId);
  53. if (!$user) {
  54. return false;
  55. }
  56. $user->balance = $user->balance + $balance;
  57. if ($user->balance < 0) {
  58. return false;
  59. }
  60. if (!$user->save()) {
  61. return false;
  62. }
  63. return true;
  64. }
  65. public function isNotCompleteOrderByUserId(int $userId):bool
  66. {
  67. $order = Order::whereIn('status', [0, 1])
  68. ->where('user_id', $userId)
  69. ->first();
  70. if (!$order) {
  71. return false;
  72. }
  73. return true;
  74. }
  75. public function trafficFetch(int $u, int $d, int $userId, object $server, string $protocol)
  76. {
  77. TrafficFetchJob::dispatch($u, $d, $userId, $server, $protocol);
  78. ServerLogJob::dispatch($u, $d, $userId, $server, $protocol);
  79. }
  80. }