request = $request; $this->params = $request->all(); $this->array_filter = array_filter($this->request->only($this->filters)); } public function apply($builder) { $this->queryBuilder = $builder; $this->afterApply(); foreach ($this->array_filter as $filter => $value) { if (method_exists($this, $filter)) { $this->$filter($value, $this->queryBuilder); } } $this->beforeApply(); return $this->queryBuilder; } public function afterApply() { if (isset($this->params['data'])) $this->id(explode(',', $this->params['data'])); if (Gate::allows('订单管理-工单处理-货主编辑') || Gate::allows('订单管理-工单处理-客服编辑')) { $this->queryBuilder->whereIn('owner_id', app('UserService')->getPermittingOwnerIds(Auth::user()) ?? []); } $this->afterFilter(); } private function afterFilter() { $user = Auth::user(); $logistic_ids = App('UserService')->getPermittingLogisticIds($user); $owner_ids = app('UserService')->getPermittingOwnerIds($user); $this->afterFilterLogistic($logistic_ids); $this->afterFilterOwner($owner_ids); $this->afterFileIssueType(); $this->filterWorkOrderStatus(); } private function filterWorkOrderStatus() { $status = []; if (Gate::allows('订单管理-工单处理-宝时编辑')) { $status[]= 4; } if (Gate::allows('订单管理-工单处理-承运商编辑')) { $status[]= 3; } if (Gate::allows('订单管理-工单处理-货主编辑')) { $status[]= 1; } if (!isset($this->params['is_end'])) { $this->queryBuilder->where('status', '!=', 5); // 过滤已完成 } else { $status[]= 5; } $this->queryBuilder->whereIn('status',$status); } private function afterFilterOwner($owner_ids) { if (Gate::allows('订单管理-工单处理-货主编辑') || Gate::allows('订单管理-工单处理-客服编辑')) { $this->queryBuilder->whereIn('owner_id', $owner_ids); } } private function afterFilterLogistic($logistic_ids) { if (Gate::allows('订单管理-工单处理-承运商编辑') && !Gate::allows('订单管理-工单处理-宝时编辑')) { $this->queryBuilder->whereIn('logistic_id', array_values($logistic_ids)); } } public function afterFileIssueType() { if (Gate::allows('订单管理-工单处理-客服编辑') || Gate::allows('订单管理-工单处理-货主编辑')) { $this->getOrderIssueQuery()->whereIn('name', ['拦截', '信息更改', '其他', '快递异常', '错漏发', '破损', '快递丢件']); } else if (Gate::allows('订单管理-工单处理-承运商编辑')) { $this->getOrderIssueQuery()->whereIn('name', ['拦截', '信息更改', '破损', '快递丢件', '快递异常']); } } public function beforeApply() { if ($this->orderPackageQuery) { $this->queryBuilder->whereIn('order_id', $this->orderPackageQuery); } if ($this->orderQuery) { $this->queryBuilder->whereIn('order_id', $this->orderQuery); } if ($this->issueTypeQuery) { $this->queryBuilder->whereIn('order_issue_type_id', $this->issueTypeQuery); } } public function getOrderQuery(): Builder { if (!$this->orderQuery) { $this->orderQuery = Order::query()->select('id'); } return $this->orderQuery; } public function getOrderPackageQuery(): Builder { if (!$this->orderPackageQuery) { $this->orderPackageQuery = OrderPackage::query()->select('order_id'); } return $this->orderPackageQuery; } public function getOrderIssueQuery(): Builder { if (!$this->issueTypeQuery) { $this->issueTypeQuery = OrderIssueType::query()->select('id'); } return $this->issueTypeQuery; } public function id($id) { if (is_array($id)) $this->queryBuilder->whereIn('work_orders.id', $id); else $this->queryBuilder->where('work_orders.id', $id); } // 创建开始时间 public function created_at_start($create_at_start) { $this->queryBuilder->where('work_orders.created_at', '>=', $create_at_start); } // 创建结束时间 public function created_at_end($created_at_end) { $this->queryBuilder->where('work_orders.created_at', '<=', $created_at_end); } // 终审开始时间 public function review_at_start($review_at_start) { $this->queryBuilder->where('work_orders.review_at', '>=', $review_at_start); } // 终审结束时间 public function review_at_end($review_at_end) { $this->queryBuilder->where('work_orders.review_at', '<=', $review_at_end); } // 创建人 public function creator($creator) { $userQuery = User::query()->select('id')->where('name', 'like', "%{$creator}%");; $this->queryBuilder->whereIn('creator_id', $userQuery); } // 终审人 public function reviewer($id) { if (is_array($id)) $this->queryBuilder->whereIn('work_orders.reviewer_id', $id); else $this->queryBuilder->where('work_orders.reviewer_id', $id); } // 类型 public function word_order_types($word_order_types) { if (!$this->workOrderTypeQuery) $this->workOrderTypeQuery = WorkOrder::query()->select('id'); if (is_array($word_order_types)) $this->workOrderTypeQuery->whereIn('id', $word_order_types); else $this->workOrderTypeQuery->where('id', $word_order_types); } public function order_issue_type($order_issue_type) { $this->queryBuilder->where('order_issue_type_id', $order_issue_type); } // 快递单号 public function logistic_number($logistic_number) { $this->searchWay($this->getOrderPackageQuery(), $logistic_number, 'order_packages.logistic_number'); } // 对应问题件 public function is_issue_order($is_issue_order) { $orderIssueQuery = OrderIssue::query()->select('order_id')->whereIn('order_id', WorkOrder::query()->select('order_id')); if ($is_issue_order == 'true') { $this->queryBuilder->whereIn('order_id', $orderIssueQuery); } else { $this->queryBuilder->whereNotIn('order_id', $orderIssueQuery); } } // 承运商筛选 public function logistic($logistic) { $this->searchWay($this->queryBuilder, $logistic, 'logistic_id'); } // 货主 public function owner($owner) { $this->searchWay($this->queryBuilder, $owner, 'work_orders.owner_id'); } public function client_code($client_code) { $this->searchWay($this->getOrderQuery(), $client_code, 'orders.client_code'); } }