WorkOrderFilters.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace App\Filters;
  3. use App\Order;
  4. use App\Traits\ModelSearchWay;
  5. use App\WorkOrder;
  6. use Illuminate\Http\Request;
  7. class WorkOrderFilters
  8. {
  9. use ModelSearchWay;
  10. protected $request;
  11. protected $queryBuilder;
  12. protected $filters = [
  13. 'ids','creator','name','remake','created_at_start','created_at_end','review_at_start','review_at_end','reviewer','word_order_types','word_order_child_types','logistic'
  14. ];
  15. protected $array_filter;
  16. protected $params = [
  17. 'ids','creator','name','remake','created_at_start','created_at_end','review_at_start','review_at_end','reviewer','word_order_types','word_order_child_types','logistic'
  18. ];
  19. protected $workOrderTypeQuery;
  20. protected $orderQuery;
  21. public function __construct(Request $request)
  22. {
  23. $this->request = $request;
  24. $this->params = $request->all();
  25. $this->array_filter = array_filter($this->request->only($this->filters));
  26. }
  27. public function apply($builder)
  28. {
  29. $this->queryBuilder = $builder;
  30. $this->afterApply();
  31. foreach ($this->array_filter as $filter => $value) {
  32. if (method_exists($this, $filter)) {
  33. $this->$filter($value, $this->queryBuilder);
  34. }
  35. }
  36. $this->beforeApply();
  37. return $this->queryBuilder;
  38. }
  39. public function afterApply()
  40. {
  41. if(isset($this->params['data']))
  42. $this->id(explode(',',$this->params['data']));
  43. }
  44. public function beforeApply()
  45. {
  46. if ($this->orderQuery) {
  47. $this->queryBuilder->whereIn('order_id',$this->getOrderQuery());
  48. }
  49. }
  50. public function getOrderQuery()
  51. {
  52. if (!$this->orderQuery){
  53. $this->orderQuery = Order::query()->select('id');
  54. }
  55. return $this->orderQuery;
  56. }
  57. public function id($id)
  58. {
  59. if(is_array($id))$this->queryBuilder->whereIn('work_orders.id',$id);
  60. else $this->queryBuilder->where('work_orders.id',$id);
  61. }
  62. // 创建开始时间
  63. public function created_at_start($create_at_start)
  64. {
  65. $this->queryBuilder->where('work_orders.created_at','>=',$create_at_start);
  66. }
  67. // 创建结束时间
  68. public function created_at_end($created_at_end)
  69. {
  70. $this->queryBuilder->where('work_orders.created_at','<=',$created_at_end);
  71. }
  72. // 审核开始时间
  73. public function review_at_start($review_at_start)
  74. {
  75. $this->queryBuilder->where('work_orders.review_at','>=',$review_at_start);
  76. }
  77. // 审核结束时间
  78. public function review_at_end($review_at_end)
  79. {
  80. $this->queryBuilder->where('work_orders.review_at','<=',$review_at_end);
  81. }
  82. // 创建人
  83. public function creator($id)
  84. {
  85. if (is_array($id))
  86. $this->queryBuilder->whereIn('work_orders.creator_id',$id);
  87. else
  88. $this->queryBuilder->where('work_orders.creator_id',$id);
  89. }
  90. // 审核人
  91. public function reviewer($id)
  92. {
  93. if (is_array($id))
  94. $this->queryBuilder->whereIn('work_orders.reviewer_id',$id);
  95. else
  96. $this->queryBuilder->where('work_orders.reviewer_id',$id);
  97. }
  98. // 类型
  99. public function word_order_types($word_order_types)
  100. {
  101. if(!$this->workOrderTypeQuery)
  102. $this->workOrderTypeQuery = WorkOrder::query()->select('id');
  103. if (is_array($word_order_types))
  104. $this->workOrderTypeQuery->whereIn('id',$word_order_types);
  105. else $this->workOrderTypeQuery->where('id',$word_order_types);
  106. }
  107. public function logistic($logistic)
  108. {
  109. $orderQuery = $this->getOrderQuery()->whereIn('id',WorkOrder::query()->select('order_id'));
  110. $this->searchWay($orderQuery,$logistic,'logistic_id');
  111. }
  112. }