OrderCriteria.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace App\Repositories\Criteria\Dwbs;
  3. use App\Repositories\Models\Base\Menu;
  4. use App\Repositories\Models\Base\User;
  5. use Illuminate\Http\Request;
  6. use Prettus\Repository\Contracts\CriteriaInterface;
  7. use Prettus\Repository\Contracts\RepositoryInterface;
  8. class OrderCriteria 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('user_id')) {
  27. $val = $this->request->get('user_id');
  28. $model = $model->where('user_id', $val);
  29. }
  30. if ($this->request->filled('day')) {
  31. $val = $this->request->get('day');
  32. if (is_string($val)) $model = $model->where('day', '=', $val);
  33. if (is_array($val)) {
  34. $model = $model->where('day', '>=', $val[0])->where('day', '<=', $val[1]);
  35. }
  36. }
  37. if ($this->request->filled('order_time')) {
  38. $val = $this->request->get('order_time');
  39. if (is_string($val)) $model = $model->where('order_time', '=', $val);
  40. if (is_array($val)) {
  41. $model = $model->where('order_time', '>=', $val[0])->where('order_time', '<=', $val[1]);
  42. }
  43. }
  44. if ($this->request->filled('order_no')) {
  45. $val = $this->request->get('order_no');
  46. $model = $model->where('order_no', 'like', "%{$val}");
  47. }
  48. if ($this->request->filled('user_mobile')) {
  49. $val = $this->request->get('user_mobile');
  50. $ids = User::byMobileGetIds($val);
  51. $model = $model->whereIn('user_id', $ids);
  52. }
  53. if ($this->request->filled('user_nickname')) {
  54. $val = $this->request->get('user_nickname');
  55. $model = $model->whereHas('user', function ($query) use ($val) {
  56. return $query->where('nickname', 'like', "%{$val}%");
  57. });
  58. }
  59. if ($this->request->filled('status')) {
  60. $status = $this->request->get('status');
  61. $model = $model->where('status', '=', $status);
  62. }
  63. if ($this->request->filled('ids')) {
  64. $ids = $this->request->get('ids');
  65. $model = $model->whereIn('id', $ids);
  66. }
  67. if (!$this->request->filled('orderBy')) {
  68. $model = $model->orderByDesc('id');
  69. }
  70. if (!isAdminModule()) {
  71. $model = $model->where('user_id', login_user_id());
  72. } else {
  73. if (Menu::checkUserIsBtn(151)) {
  74. //全部数据
  75. } elseif (Menu::checkUserIsBtn(150)) {
  76. $admin = login_admin();
  77. $model = $model->whereHas('user', function ($q) use ($admin) {
  78. return $q->where('kefu_tag', $admin['user_tag']);
  79. });
  80. } else {
  81. $model = $model->where('id', 0);
  82. }
  83. }
  84. return $model;
  85. }
  86. }