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("OwnerService")->getQuery()); } $this->afterFilter(); } private function afterFilter() { $user = Auth::user(); $logistic_ids = App('UserService')->getPermittingLogisticIds($user); $owner_ids = app("OwnerService")->getIdArr(); $this->afterFilterLogistic($logistic_ids); $this->afterFilterOwner($owner_ids); $this->afterFileIssueType(); $this->filterStatus(); } private function filterStatus() { $status = []; if (Gate::allows('订单管理-工单处理-宝时编辑')) { array_push($status, 1, 4); } if (Gate::allows('订单管理-工单处理-货主编辑')) { array_push($status, 1, 2, 3, 4, 6); } if (Gate::allows('订单管理-工单处理-承运商编辑')) { array_push($status, 1, 2, 3, 4, 6); } if (!isset($this->params['is_end'])) { $this->queryBuilder->where('status', '!=', 5); // 过滤已完成 } else { $status = [5]; // array_push($status, 5); } $this->queryBuilder->whereIn('status', array_unique($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->getOrderIssueTypeQuery()->whereIn('name', ['拦截', '取消拦截', '信息更改', '其他', '快递异常', '错漏发', '破损', '快递丢件']); } else if (Gate::allows('订单管理-工单处理-承运商编辑')) { $this->getOrderIssueTypeQuery()->whereIn('name', ['拦截', '取消拦截', '信息更改', '破损', '快递丢件', '快递异常']); } } public function beforeApply() { if ($this->shopQuery) { $this->getOrderQuery()->whereIn('orders.shop_id', $this->shopQuery); } if ($this->orderPackageQuery) { $this->queryBuilder->whereIn('order_id', $this->orderPackageQuery); } if($this->orderDetailQuery){ $this->queryBuilder->whereIn('order_id',$this->orderDetailQuery); } if ($this->orderQuery) { $this->queryBuilder->whereIn('order_id', $this->orderQuery); } if ($this->issueTypeQuery) { $this->queryBuilder->whereIn('order_issue_type_id', $this->issueTypeQuery); } if ($this->orderIssueQuery) { $this->queryBuilder->whereIn('work_orders.order_id', $this->orderIssueQuery); } if ($this->workOrderProcessLogQuery) { $this->queryBuilder->whereIn('work_orders.id', $this->workOrderProcessLogQuery); } $this->orderByTag(); } public function orderByTag() { $this->queryBuilder->orderByDesc('work_order_status'); if (Gate::allows('订单管理-工单处理-客服编辑')) { $this->queryBuilder->orderByDesc("bao_shi_tag"); } else if (Gate::allows('订单管理-工单处理-货主编辑')) { $this->queryBuilder->orderByDesc("owner_tag"); } else if (Gate::allows('订单管理-工单处理-承运商编辑')) { $this->queryBuilder->orderByDesc("logistic_tag"); } $this->queryBuilder->orderBy('created_at'); } 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 getOrderIssueTypeQuery(): Builder { if (!$this->issueTypeQuery) { $this->issueTypeQuery = OrderIssueType::query()->select('id'); } return $this->issueTypeQuery; } public function getOrderIssueQuery(): Builder { if (!$this->orderIssueQuery) { $this->orderIssueQuery = OrderIssue::query()->select('order_issues.order_id'); } return $this->orderIssueQuery; } public function getOrderIssueLogQuery(): Builder { if (!$this->orderIssueLogQuery) { $this->orderIssueLogQuery = OrderIssueProcessLog::query()->select('order_issue_id'); } return $this->orderIssueLogQuery; } public function getShopQuery(): Builder { if (!$this->shopQuery) { $this->shopQuery = Shop::query()->select('id'); } return $this->shopQuery; } public function getWorkOrderProcessLogQuery(): Builder { if (!$this->workOrderProcessLogQuery) { $this->workOrderProcessLogQuery = WorkOrderProcessLog::query()->select('work_order_id'); } return $this->workOrderProcessLogQuery; } public function getOrderDetailQuery(): Builder { if (!$this->orderDetailQuery){ $this->orderDetailQuery = OrderDetail::query()->select('order_id'); } return $this->orderDetailQuery; } 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->searchWay($this->queryBuilder,$order_issue_type,'order_issue_type_id'); } // 快递单号 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'); } public function process_progress($process_progress) { $this->queryBuilder->where('work_orders.process_progress',$process_progress); // $this->searchWay($this->queryBuilder, $process_progress, 'work_orders.process_progress'); } public function order_issue_log($log_content) { $order_issue_process_log_query = OrderIssueProcessLog::query()->select('order_issue_id')->where('content', 'like', $log_content); $order_issue_query = OrderIssue::query()->select('order_id')->whereIn('id', $order_issue_process_log_query); $this->queryBuilder->whereIn('order_id', $order_issue_query); } public function log_content($log_content) { $order_issue_process_log_query = OrderIssueProcessLog::query()->selectRaw('order_issue_id')->where('content', 'like', $log_content); if (!array_key_exists('addtime', $this->params)) { $order_issue_process_log_query->where('created_at', '>=', Carbon::now()->subDays(31)); } else { $order_issue_process_log_query->where('created_at', '>=', Carbon::now()->subDays($this->params['addtime'])); } $this->getOrderIssueQuery()->whereIn('id', $order_issue_process_log_query); } public function status($status) { $status = array_filter(preg_split('/[,, ]+/u', $status)); $status_list = []; if (in_array('承运商处理',$status)) { array_push($status_list, 3); } if (in_array('宝时处理',$status)) { array_push($status_list, 4, 1); } if (in_array('货主处理',$status)) { array_push($status_list, 2, 6); } $this->queryBuilder->whereIn('status', $status_list); } public function tags($tag) { $status = $this->array_filter['status'] ?? null; if ($status) { switch ($status) { case '宝时处理': $this->queryBuilder->where('bao_shi_tag', $tag); break; case '货主处理': $this->queryBuilder->where('owner_tag', $tag); break; case '承运商处理': $this->queryBuilder->where('logistic_tag', $tag); break; } return; } if (Gate::allows('订单管理-工单处理-宝时编辑')) { $this->queryBuilder->where('bao_shi_tag', $tag); } else if (Gate::allows('订单管理-工单处理-货主编辑')) { $this->queryBuilder->where('owner_tag', $tag); } else if (Gate::allows('订单管理-工单处理-承运商编辑')) { $this->queryBuilder->where('logistic_tag', $tag); } } public function shop_name($shop_name) { $this->searchWay($this->getShopQuery(), $shop_name, 'shops.name'); } public function work_order_process_log($work_order_process_log) { $this->searchWay($this->getWorkOrderProcessLogQuery(), $work_order_process_log, 'work_order_process_logs.content'); } public function logistic_indemnity_money($logistic_indemnity_money) { $this->queryBuilder->where('logistic_indemnity_money',$logistic_indemnity_money); } public function logistic_express_remission($logistic_express_remission) { $this->queryBuilder->where('logistic_express_remission',$logistic_express_remission); } public function bao_shi_indemnity_money($bao_shi_indemnity_money) { $this->queryBuilder->where('bao_shi_indemnity_money',$bao_shi_indemnity_money); } public function bao_shi_express_remission($logistic_indemnity_money) { $this->queryBuilder->where('bao_shi_express_remission',$logistic_indemnity_money); } public function user_owner_group_id($user_owner_group_id) { $this->queryBuilder->where('user_owner_group_id',$user_owner_group_id); } public function user_work_group_id($user_work_group_id) { $this->queryBuilder->whereHas('userWorkGroups',function($query)use($user_work_group_id){ $this->searchWay($query,$user_work_group_id,'user_workgroup_id'); }); } public function rejecting_status($rejecting_status) { $query = OrderDetail::query()->select('order_id')->where('rejecting_status',$rejecting_status); $this->queryBuilder->whereIn('order_id',$query); } public function rejectingStatus($rejectingStatus) { $this->getOrderDetailQuery()->where('rejecting_status',$rejectingStatus); } public function custom_rejected_status($custom_rejected_status) { $this->queryBuilder->where('custom_rejected_status',$custom_rejected_status); } }