WorkOrderFilters.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. <?php
  2. namespace App\Filters;
  3. use App\Order;
  4. use App\OrderDetail;
  5. use App\OrderIssue;
  6. use App\OrderIssueProcessLog;
  7. use App\OrderIssueType;
  8. use App\OrderPackage;
  9. use App\Shop;
  10. use App\Traits\ModelSearchWay;
  11. use App\User;
  12. use App\WorkOrder;
  13. use App\WorkOrderProcessLog;
  14. use Carbon\Carbon;
  15. use Illuminate\Database\Eloquent\Builder;
  16. use Illuminate\Http\Request;
  17. use Illuminate\Support\Facades\Auth;
  18. use Illuminate\Support\Facades\DB;
  19. use Illuminate\Support\Facades\Gate;
  20. class WorkOrderFilters
  21. {
  22. use ModelSearchWay;
  23. /** @var Builder $queryBuilder */
  24. protected $request;
  25. protected $queryBuilder;
  26. protected $filters = [
  27. 'ids',
  28. 'creator',
  29. 'remake',
  30. 'created_at_start', 'created_at_end',
  31. 'review_at_start', 'review_at_end',
  32. 'reviewer',
  33. 'word_order_types',
  34. 'word_order_child_types',
  35. 'logistic',
  36. 'is_review',
  37. 'logistic_number',
  38. 'is_issue_order',
  39. 'order_issue_type',
  40. 'owner',
  41. 'client_code',
  42. 'is_end',
  43. 'status',
  44. 'process_progress',
  45. 'order_issue_log',
  46. 'log_content',
  47. 'tags',
  48. 'shop_name',
  49. 'work_order_process_log',
  50. 'rejectingStatus',
  51. 'logistic_indemnity_money',
  52. 'logistic_express_remission',
  53. 'bao_shi_indemnity_money',
  54. 'bao_shi_express_remission',
  55. 'user_owner_group_id',
  56. 'user_work_group_id',
  57. 'rejecting_status',
  58. 'custom_rejected_status',
  59. ];
  60. protected $array_filter;
  61. protected $params = [];
  62. protected $workOrderTypeQuery;
  63. protected $orderQuery;
  64. protected $orderPackageQuery;
  65. protected $issueTypeQuery;
  66. protected $orderIssueLogQuery;
  67. protected $orderIssueQuery;
  68. protected $shopQuery;
  69. protected $workOrderProcessLogQuery;
  70. protected $orderDetailQuery;
  71. public function __construct(Request $request)
  72. {
  73. $this->request = $request;
  74. $this->params = $request->all();
  75. $this->array_filter = array_filter($this->request->only($this->filters));
  76. }
  77. public function apply($builder)
  78. {
  79. $this->queryBuilder = $builder;
  80. $this->afterApply();
  81. foreach ($this->array_filter as $filter => $value) {
  82. if (method_exists($this, $filter)) {
  83. $this->$filter($value, $this->queryBuilder);
  84. }
  85. }
  86. $this->beforeApply();
  87. return $this->queryBuilder;
  88. }
  89. public function afterApply()
  90. {
  91. if (isset($this->params['data']))
  92. $this->id(explode(',', $this->params['data']));
  93. if (Gate::allows('订单管理-工单处理-货主编辑') || Gate::allows('订单管理-工单处理-客服编辑')) {
  94. $this->queryBuilder->whereIn('owner_id', app("OwnerService")->getQuery());
  95. }
  96. $this->afterFilter();
  97. }
  98. private function afterFilter()
  99. {
  100. $user = Auth::user();
  101. $logistic_ids = App('UserService')->getPermittingLogisticIds($user);
  102. $owner_ids = app("OwnerService")->getIdArr();
  103. $this->afterFilterLogistic($logistic_ids);
  104. $this->afterFilterOwner($owner_ids);
  105. $this->afterFileIssueType();
  106. $this->filterStatus();
  107. }
  108. private function filterStatus()
  109. {
  110. $status = [];
  111. if (Gate::allows('订单管理-工单处理-宝时编辑')) {
  112. array_push($status, 1, 4);
  113. }
  114. if (Gate::allows('订单管理-工单处理-货主编辑')) {
  115. array_push($status, 1, 2, 3, 4, 6);
  116. }
  117. if (Gate::allows('订单管理-工单处理-承运商编辑')) {
  118. array_push($status, 1, 2, 3, 4, 6);
  119. }
  120. if (!isset($this->params['is_end'])) {
  121. $this->queryBuilder->where('status', '!=', 5); // 过滤已完成
  122. } else {
  123. $status = [5];
  124. // array_push($status, 5);
  125. }
  126. $this->queryBuilder->whereIn('status', array_unique($status));
  127. }
  128. private function afterFilterOwner($owner_ids)
  129. {
  130. if (Gate::allows('订单管理-工单处理-货主编辑') || Gate::allows('订单管理-工单处理-客服编辑')) {
  131. $this->queryBuilder->whereIn('owner_id', $owner_ids);
  132. }else if(Gate::allows('订单管理-工单处理-承运商编辑')){
  133. $this->queryBuilder->whereIn('owner_id', $owner_ids);
  134. }
  135. }
  136. private function afterFilterLogistic($logistic_ids)
  137. {
  138. if (Gate::allows('订单管理-工单处理-承运商编辑') && !Gate::allows('订单管理-工单处理-宝时编辑')) {
  139. $this->queryBuilder->whereIn('logistic_id', array_values($logistic_ids));
  140. }
  141. }
  142. public function afterFileIssueType()
  143. {
  144. if (Gate::allows('订单管理-工单处理-客服编辑') || Gate::allows('订单管理-工单处理-货主编辑')) {
  145. $this->getOrderIssueTypeQuery()->whereIn('name', ['拦截', '取消拦截', '信息更改', '其他', '快递异常', '错漏发', '破损', '快递丢件']);
  146. } else if (Gate::allows('订单管理-工单处理-承运商编辑')) {
  147. $this->getOrderIssueTypeQuery()->whereIn('name', ['拦截', '取消拦截', '信息更改', '破损', '快递丢件', '快递异常']);
  148. }
  149. }
  150. public function beforeApply()
  151. {
  152. if ($this->shopQuery) {
  153. $this->getOrderQuery()->whereIn('orders.shop_id', $this->shopQuery);
  154. }
  155. if ($this->orderPackageQuery) {
  156. $this->queryBuilder->whereIn('order_id', $this->orderPackageQuery);
  157. }
  158. if($this->orderDetailQuery){
  159. $this->queryBuilder->whereIn('order_id',$this->orderDetailQuery);
  160. }
  161. if ($this->orderQuery) {
  162. $this->queryBuilder->whereIn('order_id', $this->orderQuery);
  163. }
  164. if ($this->issueTypeQuery) {
  165. $this->queryBuilder->whereIn('order_issue_type_id', $this->issueTypeQuery);
  166. }
  167. if ($this->orderIssueQuery) {
  168. $this->queryBuilder->whereIn('work_orders.order_id', $this->orderIssueQuery);
  169. }
  170. if ($this->workOrderProcessLogQuery) {
  171. $this->queryBuilder->whereIn('work_orders.id', $this->workOrderProcessLogQuery);
  172. }
  173. $this->orderByTag();
  174. }
  175. public function orderByTag()
  176. {
  177. $this->queryBuilder->orderByDesc('work_order_status');
  178. if (Gate::allows('订单管理-工单处理-客服编辑')) {
  179. $this->queryBuilder->orderByDesc("bao_shi_tag");
  180. } else if (Gate::allows('订单管理-工单处理-货主编辑')) {
  181. $this->queryBuilder->orderByDesc("owner_tag");
  182. } else if (Gate::allows('订单管理-工单处理-承运商编辑')) {
  183. $this->queryBuilder->orderByDesc("logistic_tag");
  184. }
  185. $this->queryBuilder->orderBy('created_at');
  186. }
  187. public function getOrderQuery(): Builder
  188. {
  189. if (!$this->orderQuery) {
  190. $this->orderQuery = Order::query()->select('id');
  191. }
  192. return $this->orderQuery;
  193. }
  194. public function getOrderPackageQuery(): Builder
  195. {
  196. if (!$this->orderPackageQuery) {
  197. $this->orderPackageQuery = OrderPackage::query()->select('order_id');
  198. }
  199. return $this->orderPackageQuery;
  200. }
  201. public function getOrderIssueTypeQuery(): Builder
  202. {
  203. if (!$this->issueTypeQuery) {
  204. $this->issueTypeQuery = OrderIssueType::query()->select('id');
  205. }
  206. return $this->issueTypeQuery;
  207. }
  208. public function getOrderIssueQuery(): Builder
  209. {
  210. if (!$this->orderIssueQuery) {
  211. $this->orderIssueQuery = OrderIssue::query()->select('order_issues.order_id');
  212. }
  213. return $this->orderIssueQuery;
  214. }
  215. public function getOrderIssueLogQuery(): Builder
  216. {
  217. if (!$this->orderIssueLogQuery) {
  218. $this->orderIssueLogQuery = OrderIssueProcessLog::query()->select('order_issue_id');
  219. }
  220. return $this->orderIssueLogQuery;
  221. }
  222. public function getShopQuery(): Builder
  223. {
  224. if (!$this->shopQuery) {
  225. $this->shopQuery = Shop::query()->select('id');
  226. }
  227. return $this->shopQuery;
  228. }
  229. public function getWorkOrderProcessLogQuery(): Builder
  230. {
  231. if (!$this->workOrderProcessLogQuery) {
  232. $this->workOrderProcessLogQuery = WorkOrderProcessLog::query()->select('work_order_id');
  233. }
  234. return $this->workOrderProcessLogQuery;
  235. }
  236. public function getOrderDetailQuery(): Builder
  237. {
  238. if (!$this->orderDetailQuery){
  239. $this->orderDetailQuery = OrderDetail::query()->select('order_id');
  240. }
  241. return $this->orderDetailQuery;
  242. }
  243. public function id($id)
  244. {
  245. if (is_array($id)) $this->queryBuilder->whereIn('work_orders.id', $id);
  246. else $this->queryBuilder->where('work_orders.id', $id);
  247. }
  248. // 创建开始时间
  249. public function created_at_start($create_at_start)
  250. {
  251. $this->queryBuilder->where('work_orders.created_at', '>=', $create_at_start);
  252. }
  253. // 创建结束时间
  254. public function created_at_end($created_at_end)
  255. {
  256. $this->queryBuilder->where('work_orders.created_at', '<=', $created_at_end);
  257. }
  258. // 终审开始时间
  259. public function review_at_start($review_at_start)
  260. {
  261. $this->queryBuilder->where('work_orders.review_at', '>=', $review_at_start);
  262. }
  263. // 终审结束时间
  264. public function review_at_end($review_at_end)
  265. {
  266. $this->queryBuilder->where('work_orders.review_at', '<=', $review_at_end);
  267. }
  268. // 创建人
  269. public function creator($creator)
  270. {
  271. $userQuery = User::query()->select('id')->where('name', 'like', "%{$creator}%");;
  272. $this->queryBuilder->whereIn('creator_id', $userQuery);
  273. }
  274. // 终审人
  275. public function reviewer($id)
  276. {
  277. if (is_array($id))
  278. $this->queryBuilder->whereIn('work_orders.reviewer_id', $id);
  279. else
  280. $this->queryBuilder->where('work_orders.reviewer_id', $id);
  281. }
  282. // 类型
  283. public function word_order_types($word_order_types)
  284. {
  285. if (!$this->workOrderTypeQuery)
  286. $this->workOrderTypeQuery = WorkOrder::query()->select('id');
  287. if (is_array($word_order_types))
  288. $this->workOrderTypeQuery->whereIn('id', $word_order_types);
  289. else $this->workOrderTypeQuery->where('id', $word_order_types);
  290. }
  291. public function order_issue_type($order_issue_type)
  292. {
  293. $this->searchWay($this->queryBuilder,$order_issue_type,'order_issue_type_id');
  294. }
  295. // 快递单号
  296. public function logistic_number($logistic_number)
  297. {
  298. $this->searchWay($this->getOrderPackageQuery(), $logistic_number, 'order_packages.logistic_number');
  299. }
  300. // 对应问题件
  301. public function is_issue_order($is_issue_order)
  302. {
  303. $orderIssueQuery = OrderIssue::query()->select('order_id')->whereIn('order_id', WorkOrder::query()->select('order_id'));
  304. if ($is_issue_order == 'true') {
  305. $this->queryBuilder->whereIn('order_id', $orderIssueQuery);
  306. } else {
  307. $this->queryBuilder->whereNotIn('order_id', $orderIssueQuery);
  308. }
  309. }
  310. // 承运商筛选
  311. public function logistic($logistic)
  312. {
  313. $this->searchWay($this->queryBuilder, $logistic, 'logistic_id');
  314. }
  315. // 货主
  316. public function owner($owner)
  317. {
  318. $this->searchWay($this->queryBuilder, $owner, 'work_orders.owner_id');
  319. }
  320. public function client_code($client_code)
  321. {
  322. $this->searchWay($this->getOrderQuery(), $client_code, 'orders.client_code');
  323. }
  324. public function process_progress($process_progress)
  325. {
  326. $this->queryBuilder->where('work_orders.process_progress',$process_progress);
  327. // $this->searchWay($this->queryBuilder, $process_progress, 'work_orders.process_progress');
  328. }
  329. public function order_issue_log($log_content)
  330. {
  331. $order_issue_process_log_query = OrderIssueProcessLog::query()->select('order_issue_id')->where('content', 'like', $log_content);
  332. $order_issue_query = OrderIssue::query()->select('order_id')->whereIn('id', $order_issue_process_log_query);
  333. $this->queryBuilder->whereIn('order_id', $order_issue_query);
  334. }
  335. public function log_content($log_content)
  336. {
  337. $order_issue_process_log_query = OrderIssueProcessLog::query()->selectRaw('order_issue_id')->where('content', 'like', $log_content);
  338. if (!array_key_exists('addtime', $this->params)) {
  339. $order_issue_process_log_query->where('created_at', '>=', Carbon::now()->subDays(31));
  340. } else {
  341. $order_issue_process_log_query->where('created_at', '>=', Carbon::now()->subDays($this->params['addtime']));
  342. }
  343. $this->getOrderIssueQuery()->whereIn('id', $order_issue_process_log_query);
  344. }
  345. public function status($status)
  346. {
  347. $status = array_filter(preg_split('/[,, ]+/u', $status));
  348. $status_list = [];
  349. if (in_array('承运商处理',$status)) {
  350. array_push($status_list, 3);
  351. }
  352. if (in_array('宝时处理',$status)) {
  353. array_push($status_list, 4, 1);
  354. }
  355. if (in_array('货主处理',$status)) {
  356. array_push($status_list, 2, 6);
  357. }
  358. $this->queryBuilder->whereIn('status', $status_list);
  359. }
  360. public function tags($tag)
  361. {
  362. $status = $this->array_filter['status'] ?? null;
  363. if ($status) {
  364. switch ($status) {
  365. case '宝时处理':
  366. $this->queryBuilder->where('bao_shi_tag', $tag);
  367. break;
  368. case '货主处理':
  369. $this->queryBuilder->where('owner_tag', $tag);
  370. break;
  371. case '承运商处理':
  372. $this->queryBuilder->where('logistic_tag', $tag);
  373. break;
  374. }
  375. return;
  376. }
  377. if (Gate::allows('订单管理-工单处理-宝时编辑')) {
  378. $this->queryBuilder->where('bao_shi_tag', $tag);
  379. } else if (Gate::allows('订单管理-工单处理-货主编辑')) {
  380. $this->queryBuilder->where('owner_tag', $tag);
  381. } else if (Gate::allows('订单管理-工单处理-承运商编辑')) {
  382. $this->queryBuilder->where('logistic_tag', $tag);
  383. }
  384. }
  385. public function shop_name($shop_name)
  386. {
  387. $this->searchWay($this->getShopQuery(), $shop_name, 'shops.name');
  388. }
  389. public function work_order_process_log($work_order_process_log)
  390. {
  391. $this->searchWay($this->getWorkOrderProcessLogQuery(), $work_order_process_log, 'work_order_process_logs.content');
  392. }
  393. public function logistic_indemnity_money($logistic_indemnity_money)
  394. {
  395. $this->queryBuilder->where('logistic_indemnity_money',$logistic_indemnity_money);
  396. }
  397. public function logistic_express_remission($logistic_express_remission)
  398. {
  399. $this->queryBuilder->where('logistic_express_remission',$logistic_express_remission);
  400. }
  401. public function bao_shi_indemnity_money($bao_shi_indemnity_money)
  402. {
  403. $this->queryBuilder->where('bao_shi_indemnity_money',$bao_shi_indemnity_money);
  404. }
  405. public function bao_shi_express_remission($logistic_indemnity_money)
  406. {
  407. $this->queryBuilder->where('bao_shi_express_remission',$logistic_indemnity_money);
  408. }
  409. public function user_owner_group_id($user_owner_group_id)
  410. {
  411. $this->queryBuilder->where('user_owner_group_id',$user_owner_group_id);
  412. }
  413. public function user_work_group_id($user_work_group_id)
  414. {
  415. $this->queryBuilder->whereHas('userWorkGroups',function($query)use($user_work_group_id){
  416. $this->searchWay($query,$user_work_group_id,'user_workgroup_id');
  417. });
  418. }
  419. public function rejecting_status($rejecting_status)
  420. {
  421. $query = OrderDetail::query()->select('order_id')->where('rejecting_status',$rejecting_status);
  422. $this->queryBuilder->whereIn('order_id',$query);
  423. }
  424. public function rejectingStatus($rejectingStatus)
  425. {
  426. $this->getOrderDetailQuery()->where('rejecting_status',$rejectingStatus);
  427. }
  428. public function custom_rejected_status($custom_rejected_status)
  429. {
  430. $this->queryBuilder->where('custom_rejected_status',$custom_rejected_status);
  431. }
  432. }