UserService.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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['like_first_name']) && !empty($search['like_first_name'])) {
  47. $where[] = ['first_name', 'like', "%" . $search['like_first_name'] . "%"];
  48. }
  49. return $where;
  50. }
  51. /**
  52. * @description: 查询单条数据
  53. * @param array $search
  54. * @return \App\Models\User|null
  55. */
  56. public static function findOne(array $search): ?User
  57. {
  58. return self::model()::where(self::getWhere($search))->first();
  59. }
  60. /**
  61. * @description: 查询所有数据
  62. * @param array $search
  63. * @return \Illuminate\Database\Eloquent\Collection
  64. */
  65. public static function findAll(array $search = [])
  66. {
  67. return self::model()::where(self::getWhere($search))->get();
  68. }
  69. /**
  70. * @description: 分页查询
  71. * @param array $search
  72. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  73. */
  74. public static function paginate(array $search = [])
  75. {
  76. $limit = isset($search['limit']) ? $search['limit'] : 15;
  77. $paginator = self::model()::where(self::getWhere($search))->with('wallet:user_id,member_id,address,available_balance')->paginate($limit);
  78. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  79. }
  80. //设置游戏ID
  81. public function setGameID($chatId, $gameId)
  82. {
  83. $gameId = trim($gameId);
  84. if(!$gameId)return ['chat_id' => $chatId, 'text' => '❌游戏ID设置失败,请输入正确的游戏ID'];
  85. if (User::where('game_id', $gameId)->where('member_id', '<>', $chatId)->first()) {
  86. return ['chat_id' => $chatId, 'text' => '❌游戏ID设置失败,游戏ID已绑定其他用户'];
  87. }
  88. $user = User::where('member_id', $chatId)->first();
  89. $user->game_id = $gameId;
  90. $user->save();
  91. return ['chat_id' => $chatId, 'text' => "✅ 游戏ID设置成功\n游戏ID:{$gameId}"];
  92. }
  93. }