OrderCriteria.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace App\Repositories\Criteria;
  3. use App\Models\Role;
  4. use Illuminate\Http\Request;
  5. use Prettus\Repository\Contracts\CriteriaInterface;
  6. use Prettus\Repository\Contracts\RepositoryInterface;
  7. class OrderCriteria extends RequestCriteria implements CriteriaInterface
  8. {
  9. /**
  10. * @var \Illuminate\Http\Request
  11. */
  12. protected $request;
  13. public function __construct(Request $request)
  14. {
  15. $this->request = $request;
  16. }
  17. /**
  18. * @inheritDoc
  19. */
  20. public function apply($model, RepositoryInterface $repository)
  21. {
  22. if ($no = $this->request->get('no', false)) {
  23. $model = $model->where('no', $no);
  24. }
  25. if ($this->request->filled('status')) {
  26. $model = $model->where('status', $this->request->get('status'));
  27. }
  28. if ($this->request->filled('device_id')) {
  29. $model = $model->where('device_id', $this->request->get('device_id'));
  30. }
  31. if ($this->request->filled('shop_id')) {
  32. $model = $model->where('shop_id', $this->request->get('shop_id'));
  33. }
  34. if ($this->request->filled('mobile')) {
  35. $mobile = $this->request->get('mobile');
  36. $model = $model->whereHas('user', function ($query) use ($mobile) {
  37. return $query->where('mobile', 'like', "%{$mobile}%");
  38. });
  39. }
  40. $model = $model->orderBy("id", 'desc');
  41. //用户
  42. if (is_user_login()) {
  43. // if ($this->request->filled('user_id')) {
  44. // $model = $model->where('user_id', $this->request->get('user_id'));
  45. // } else {
  46. $model = $model->where('user_id', login_user_id());
  47. // }
  48. }
  49. //管理员
  50. if (is_admin_login()) {
  51. $admin = login_admin();
  52. switch ($admin->type) {
  53. case Role::Merchants:
  54. $model = $model->where('merchant_id', $admin->id);
  55. break;
  56. case Role::Admin:
  57. break;
  58. case Role::Agency:
  59. $model = $model->where('agency_id', $admin->id);
  60. break;
  61. case Role::Invest:
  62. $model = $model->where('invest_id', $admin->id);
  63. break;
  64. }
  65. }
  66. return parent::apply($model, $repository);
  67. }
  68. }