| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493 |
- <?php
- namespace App\Filters;
- use App\Order;
- use App\OrderDetail;
- use App\OrderIssue;
- use App\OrderIssueProcessLog;
- use App\OrderIssueType;
- use App\OrderPackage;
- use App\Shop;
- use App\Traits\ModelSearchWay;
- use App\User;
- use App\WorkOrder;
- use App\WorkOrderProcessLog;
- use Carbon\Carbon;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Gate;
- class WorkOrderFilters
- {
- use ModelSearchWay;
- /** @var Builder $queryBuilder */
- protected $request;
- protected $queryBuilder;
- protected $filters = [
- 'ids',
- 'creator',
- 'remake',
- 'created_at_start', 'created_at_end',
- 'review_at_start', 'review_at_end',
- 'reviewer',
- 'word_order_types',
- 'word_order_child_types',
- 'logistic',
- 'is_review',
- 'logistic_number',
- 'is_issue_order',
- 'order_issue_type',
- 'owner',
- 'client_code',
- 'is_end',
- 'status',
- 'process_progress',
- 'order_issue_log',
- 'log_content',
- 'tags',
- 'shop_name',
- 'work_order_process_log',
- 'rejectingStatus'
- 'logistic_indemnity_money',
- 'logistic_express_remission',
- 'bao_shi_indemnity_money',
- 'bao_shi_express_remission',
- 'user_owner_group_id',
- 'user_work_group_id',
- 'rejecting_status',
- ];
- protected $array_filter;
- protected $params = [];
- protected $workOrderTypeQuery;
- protected $orderQuery;
- protected $orderPackageQuery;
- protected $issueTypeQuery;
- protected $orderIssueLogQuery;
- protected $orderIssueQuery;
- protected $shopQuery;
- protected $workOrderProcessLogQuery;
- protected $orderDetailQuery;
- public function __construct(Request $request)
- {
- $this->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->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 {
- array_push($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->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->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');
- }
- public function 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_list = [];
- if ($status == '承运商处理') {
- array_push($status_list, 3);
- } elseif ($status == '宝时处理') {
- array_push($status_list, 4, 1);
- } elseif ($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);
- }
- }
|