ManagerPagesCriteria.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * 管理员分页查询.
  4. *
  5. * @author jim <jim458734573@gmail.com>
  6. * @date 2020-01-23 15:35:04
  7. */
  8. declare(strict_types=1);
  9. namespace App\Repositories\Criterias;
  10. use Hyperf\Database\Query\Builder;
  11. use KZ2\Repositories\Contracts\RepositoryInterface;
  12. use KZ2\Repositories\Criteria\Criteria;
  13. class ManagerPagesCriteria extends Criteria
  14. {
  15. private $params = [];
  16. public function __construct($params = [])
  17. {
  18. $this->params = $params;
  19. }
  20. /**
  21. * @param Builder $model
  22. * @param RepositoryInterface $repository
  23. * @return mixed|void
  24. */
  25. public function apply(
  26. $model,
  27. RepositoryInterface $repository
  28. ) {
  29. if (!empty($this->params['manager_id'])) {
  30. $model = $model->where('manager_id', $this->params['manager_id']);
  31. }
  32. if (!empty($this->params['username'])) {
  33. $model = $model->where('username', 'LIKE', '%'. $this->params['username'] .'%');
  34. }
  35. if (!empty($this->params['nick'])) {
  36. $model = $model->where('nick', 'LIKE', '%'. $this->params['nick'] .'%');
  37. }
  38. if (!empty($this->params['is_locked'])) {
  39. $is_locked = intval($this->params['is_locked']);
  40. if ($is_locked == 1) {
  41. $model = $model->whereNotNull('locked_at');
  42. } elseif ($is_locked == -1) {
  43. $model = $model->whereNull('locked_at');
  44. }
  45. }
  46. if (!empty($this->params['role_id'])) {
  47. $model = $model->where('role_id', $this->params['role_id']);
  48. }
  49. if (!empty($this->params['email'])) {
  50. $model = $model->where('email', $this->params['email']);
  51. }
  52. return $model;
  53. }
  54. }