ManagerPagesCriteria.php 1.7 KB

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