BannerCriteria.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace App\Repositories\Criteria\Base;
  3. use App\Repositories\Enums\ModelStatusEnum;
  4. use Carbon\Carbon;
  5. use Illuminate\Http\Request;
  6. use Prettus\Repository\Contracts\CriteriaInterface;
  7. use Prettus\Repository\Contracts\RepositoryInterface;
  8. class BannerCriteria implements CriteriaInterface
  9. {
  10. /**
  11. * @var \Illuminate\Http\Request
  12. */
  13. protected $request;
  14. public function __construct(Request $request)
  15. {
  16. $this->request = $request;
  17. }
  18. /**
  19. * @param $model
  20. * @param RepositoryInterface $repository
  21. *
  22. * @return mixed
  23. */
  24. public function apply($model, RepositoryInterface $repository)
  25. {
  26. if ($this->request->filled('type')) {
  27. $type = $this->request->get('type');
  28. $model = $model->where('type', '=', $type);
  29. }
  30. if ($this->request->filled('name')) {
  31. $name = $this->request->get('name');
  32. $model = $model->where('name', 'like', "%{$name}%");
  33. }
  34. if ($this->request->filled('is_show')) {
  35. $time = Carbon::now()->toDateTimeString();
  36. $model = $model->where('start_time', '<=', $time)->where('end_time', '>', $time)->where('status', '=', ModelStatusEnum::OK);
  37. }
  38. if ($this->request->filled('status')) {
  39. $status = $this->request->get('status');
  40. $model = $model->where('status', '=', $status);
  41. }
  42. if (!$this->request->filled('orderBy')) {
  43. $model = $model->orderByDesc('sort')->orderByDesc('id');
  44. }
  45. return $model;
  46. }
  47. }