GameService.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Game;
  4. class GameService
  5. {
  6. /**
  7. * @description: 模型
  8. * @return {string}
  9. */
  10. public static function model(): string
  11. {
  12. return Game::class;
  13. }
  14. /**
  15. * @description: 枚举
  16. * @return {*}
  17. */
  18. public static function enum(): string
  19. {
  20. return '';
  21. }
  22. /**
  23. * @description: 获取查询条件
  24. * @param {array} $search 查询内容
  25. * @return {array}
  26. */
  27. public static function getWhere(array $search = []): array
  28. {
  29. $where = [];
  30. if (isset($search['id']) && !empty($search['id'])) {
  31. $where[] = ['id', '=', $search['id']];
  32. }
  33. if (isset($search['name']) && !empty($search['name'])) {
  34. $where[] = ['name', '=', $search['name']];
  35. }
  36. if (isset($search['display_name']) && !empty($search['display_name'])) {
  37. $where[] = ['display_name', 'like', '%' . $search['display_name'] . '%'];
  38. }
  39. return $where;
  40. }
  41. /**
  42. * @description: 查询单条数据
  43. * @param array $search
  44. * @return \App\Models\Coin|null
  45. */
  46. public static function findOne(array $search): ?Game
  47. {
  48. return self::model()::where(self::getWhere($search))->first();
  49. }
  50. /**
  51. * @description: 查询所有数据
  52. * @param array $search
  53. * @return \Illuminate\Database\Eloquent\Collection
  54. */
  55. public static function findAll(array $search = [])
  56. {
  57. return self::model()::where(self::getWhere($search))->get();
  58. }
  59. /**
  60. * @description: 分页查询
  61. * @param array $search
  62. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  63. */
  64. public static function paginate(array $search = [])
  65. {
  66. $limit = isset($search['limit']) ? $search['limit'] : 15;
  67. $paginator = self::model()::where(self::getWhere($search))
  68. ->paginate($limit);
  69. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  70. }
  71. }