UserService.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace App\Services;
  3. use App\Services\BaseService;
  4. use App\Models\User;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Collection;
  7. use Illuminate\Support\Facades\Cache;
  8. class UserService extends BaseService
  9. {
  10. /**
  11. * @description: 模型
  12. * @return {string}
  13. */
  14. public static function model(): string
  15. {
  16. return User::class;
  17. }
  18. /**
  19. * @description: 枚举
  20. * @return {*}
  21. */
  22. public static function enum(): string
  23. {
  24. return '';
  25. }
  26. /**
  27. * @description: 获取查询条件
  28. * @param {array} $search 查询内容
  29. * @return {array}
  30. */
  31. public static function getWhere(array $search = []): array
  32. {
  33. $where = [];
  34. if (isset($search['id']) && !empty($search['id'])) {
  35. $where[] = ['id', '=', $search['id']];
  36. }
  37. if (isset($search['game_id']) && !empty($search['game_id'])) {
  38. $where[] = ['game_id', '=', $search['game_id']];
  39. }
  40. if (isset($search['member_id']) && !empty($search['member_id'])) {
  41. $where[] = ['member_id', '=', $search['member_id']];
  42. }
  43. if (isset($search['first_name']) && !empty($search['first_name'])) {
  44. $where[] = ['first_name', '=', $search['first_name']];
  45. }
  46. if (isset($search['username']) && !empty($search['username'])) {
  47. $where[] = ['username', '=', $search['username']];
  48. }
  49. if (isset($search['like_first_name']) && !empty($search['like_first_name'])) {
  50. $where[] = ['first_name', 'like', "%" . $search['like_first_name'] . "%"];
  51. }
  52. return $where;
  53. }
  54. /**
  55. * @description: 查询单条数据
  56. * @param array $search
  57. * @return \App\Models\User|null
  58. */
  59. public static function findOne(array $search): ?User
  60. {
  61. return self::model()::where(self::getWhere($search))->first();
  62. }
  63. /**
  64. * @description: 查询所有数据
  65. * @param array $search
  66. * @return \Illuminate\Database\Eloquent\Collection
  67. */
  68. public static function findAll(array $search = [])
  69. {
  70. return self::model()::where(self::getWhere($search))->get();
  71. }
  72. /**
  73. * @description: 分页查询
  74. * @param array $search
  75. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  76. */
  77. public static function paginate(array $search = [])
  78. {
  79. $limit = isset($search['limit']) ? $search['limit'] : 15;
  80. $paginator = self::model()::where(self::getWhere($search))->with('wallet:user_id,member_id,address,available_balance')->paginate($limit);
  81. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  82. }
  83. //设置游戏ID
  84. public function setGameID($chatId, $gameId)
  85. {
  86. $gameId = trim($gameId);
  87. if(!$gameId)return ['chat_id' => $chatId, 'text' => '❌游戏ID设置失败,请输入正确的游戏ID'];
  88. if (User::where('game_id', $gameId)->where('member_id', '<>', $chatId)->first()) {
  89. return ['chat_id' => $chatId, 'text' => '❌游戏ID设置失败,游戏ID已绑定其他用户'];
  90. }
  91. $user = User::where('member_id', $chatId)->first();
  92. $user->game_id = $gameId;
  93. $user->save();
  94. return ['chat_id' => $chatId, 'text' => "✅ 游戏ID设置成功\n游戏ID:{$gameId}"];
  95. }
  96. }