AdminService.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace App\Services;
  3. use App\Services\BaseService;
  4. use App\Models\Admin;
  5. class AdminService extends BaseService
  6. {
  7. const STATUS_YES = 1; // 状态正常
  8. const STATUS_NOT = 0; // 状态禁用
  9. public static $MODEL= Admin::class;
  10. /**
  11. * @description: 获取查询条件
  12. * @param {array} $search 查询内容
  13. * @return {array}
  14. */
  15. public static function getWhere(array $search = []): array
  16. {
  17. $where = [];
  18. if (isset($search['id']) && $search['id'] !== '') {
  19. $where[] = ['id', '=', $search['id']];
  20. }
  21. if (isset($search['account']) && $search['account'] !== '') {
  22. $where[] = ['account', '=', $search['account']];
  23. }
  24. if (isset($search['nickname']) && $search['nickname'] !== '') {
  25. $where[] = ['nickname', 'like', '%' . $search['nickname'] . '%'];
  26. }
  27. if (isset($search['status']) && $search['status'] !== '') {
  28. $where[] = ['status', '=', intval($search['status'])];
  29. }
  30. return $where;
  31. }
  32. /**
  33. * @description: 查询单条数据
  34. * @param array $search
  35. * @return
  36. */
  37. public static function findOne(array $search)
  38. {
  39. return static::model()::where(static::getWhere($search))->first();
  40. }
  41. /**
  42. * @description: 查询所有数据
  43. * @param array $search
  44. * @return \Illuminate\Database\Eloquent\Collection
  45. */
  46. public static function findAll(array $search = [])
  47. {
  48. return static::model()::where(static::getWhere($search))->get();
  49. }
  50. /**
  51. * @description: 分页查询
  52. * @param array $search
  53. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  54. */
  55. public static function paginate(array $search = [])
  56. {
  57. $limit = isset($search['limit']) ? $search['limit'] : 15;
  58. $paginator = static::model()::where(static::getWhere($search))
  59. ->orderBy('updated_at', 'desc')
  60. ->paginate($limit);
  61. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  62. }
  63. }