StatisticalResourceCriteria.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace App\Repositories\Criteria\Dwbs;
  3. use Illuminate\Http\Request;
  4. use Prettus\Repository\Contracts\CriteriaInterface;
  5. use Prettus\Repository\Contracts\RepositoryInterface;
  6. class StatisticalResourceCriteria implements CriteriaInterface
  7. {
  8. /**
  9. * @var \Illuminate\Http\Request
  10. */
  11. protected $request;
  12. public function __construct(Request $request)
  13. {
  14. $this->request = $request;
  15. }
  16. /**
  17. * @param $model
  18. * @param RepositoryInterface $repository
  19. *
  20. * @return mixed
  21. */
  22. public function apply($model, RepositoryInterface $repository)
  23. {
  24. if ($this->request->filled('day')) {
  25. $val = $this->request->get('day');
  26. if (is_string($val)) $model = $model->where('day', '=', $val);
  27. if (is_array($val)) {
  28. $model = $model->where('day', '>=', $val[0])->where('day', '<=', $val[1]);
  29. }
  30. }
  31. if ($this->request->filled('day_type')) {
  32. $val = $this->request->get('day_type');
  33. $model = $model->where('day_type', '=', $val);
  34. }
  35. if ($this->request->filled('type')) {
  36. $val = $this->request->get('type');
  37. $model = $model->where('type', '=', $val);
  38. }
  39. if ($this->request->filled('group_id')) {
  40. $val = $this->request->get('group_id');
  41. $model = $model->where('group_id', '=', $val);
  42. }
  43. if ($this->request->filled('status')) {
  44. $status = $this->request->get('status');
  45. $model = $model->where('status', '=', $status);
  46. }
  47. if ($this->request->filled('ids')) {
  48. $ids = $this->request->get('ids');
  49. $model = $model->whereIn('id', $ids);
  50. }
  51. if (!$this->request->filled('orderBy')) {
  52. $model = $model->orderByDesc('id');
  53. }
  54. return $model;
  55. }
  56. }