UserCriteria.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace App\Repositories\Criteria\Base;
  3. use App\Repositories\Enums\Base\AdminTypeEnum;
  4. use App\Repositories\Models\BikeManage\Bike;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\DB;
  7. use Prettus\Repository\Contracts\CriteriaInterface;
  8. use Prettus\Repository\Contracts\RepositoryInterface;
  9. class UserCriteria implements CriteriaInterface
  10. {
  11. /**
  12. * @var \Illuminate\Http\Request
  13. */
  14. protected $request;
  15. public function __construct(Request $request)
  16. {
  17. $this->request = $request;
  18. }
  19. /**
  20. * @param $model
  21. * @param RepositoryInterface $repository
  22. *
  23. * @return mixed
  24. */
  25. public function apply($model, RepositoryInterface $repository)
  26. {
  27. if ($this->request->filled('name')) {
  28. $model = $model->where('name', 'like', '%' . $this->request->get('name') . '%');
  29. }
  30. if ($this->request->filled('user_no')) {
  31. $model = $model->where('user_no', 'like', '%' . $this->request->get('user_no'));
  32. }
  33. if ($this->request->filled('mobile')) {
  34. $model = $model->where('mobile', 'like', '%' . $this->request->get('mobile'));
  35. }
  36. if ($this->request->filled('remark')) {
  37. $model = $model->where('remark', 'like', '%' . $this->request->get('remark') . '%');
  38. }
  39. if ($this->request->filled('type')) {
  40. $model = $model->where('type', '=', $this->request->get('type'));
  41. }
  42. if ($this->request->filled('department_id')) {
  43. $model = $model->where('department_id', '=', $this->request->get('department_id'));
  44. }
  45. if ($this->request->filled('bike_nums')) {
  46. $bike_nums = $this->request->get('bike_nums');
  47. $userIds = Bike::query()->select(DB::raw("user_id,count(id) as nums"))->groupBy('user_id')->having('nums', $bike_nums)->pluck('user_id');
  48. $model = $model->whereIn('id', $userIds);
  49. }
  50. if ($this->request->filled('status')) {
  51. $model = $model->where('status', '=', $this->request->get('status'));
  52. }
  53. $model = $model->where('type', '>', AdminTypeEnum::ADMIN);
  54. return $model;
  55. }
  56. }