PointCriteria.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace App\Repositories\Criteria\Area;
  3. use App\Repositories\Enums\Base\RoleEnum;
  4. use App\Repositories\Enums\ModelStatusEnum;
  5. use Illuminate\Http\Request;
  6. use Prettus\Repository\Contracts\CriteriaInterface;
  7. use Prettus\Repository\Contracts\RepositoryInterface;
  8. /**
  9. * Class PointCriteria.
  10. *
  11. * @package namespace App\Repositories\Criteria\Area;
  12. */
  13. class PointCriteria implements CriteriaInterface
  14. {
  15. /**
  16. * Apply criteria in query repository
  17. *
  18. * @param string $model
  19. * @param RepositoryInterface $repository
  20. *
  21. * @return mixed
  22. */
  23. public function __construct(Request $request = null)
  24. {
  25. if (is_null($request)) {
  26. $this->request = \request();
  27. } else {
  28. $this->request = $request;
  29. }
  30. }
  31. /**
  32. * Apply criteria in query repository
  33. *
  34. * @param string $model
  35. * @param RepositoryInterface $repository
  36. *
  37. * @return mixed
  38. */
  39. public function apply($model, RepositoryInterface $repository)
  40. {
  41. if ($this->request->filled('name')) {
  42. $name = $this->request->get('name');
  43. $model = $model->where('name', 'like', "%{$name}%");
  44. }
  45. if ($this->request->filled('status')) {
  46. $status = $this->request->get('status');
  47. $model = $model->where('status', '=', $status);
  48. }
  49. if ($this->request->filled('project_id')) {
  50. $project_id = $this->request->get('project_id', false);
  51. $model = $model->where('project_id', '=', $project_id);
  52. }
  53. if ($this->request->filled('parent_id')) {
  54. $parent_id = $this->request->get('parent_id', false);
  55. $model = $model->where('parent_id', '=', $parent_id);
  56. }
  57. if ($this->request->filled('area_id')) {
  58. $area_id = $this->request->get('area_id', false);
  59. $model = $model->where('area_id', '=', $area_id);
  60. }
  61. if ($this->request->filled('type')) {
  62. $type = $this->request->get('type', false);
  63. $model = $model->where('type', '=', $type);
  64. }
  65. if (is_admin_login()) {
  66. $admin = login_admin();
  67. if ($admin->role_id > RoleEnum::ADMIN) {
  68. $model = $model->where('shop_id', $admin->shop_id);
  69. if ($admin->role_id == RoleEnum::PROJECT) {
  70. $model = $model->whereIn('project_id', $admin->project_ids);
  71. }
  72. }
  73. }
  74. $model = $model->orderByDesc('id');
  75. return $model;
  76. }
  77. }