WorkOrderFilters.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. array_push($status, 5);
  124. }
  125. $this->queryBuilder->whereIn('status', array_unique($status));
  126. }
  127. private function afterFilterOwner($owner_ids)
  128. {
  129. if (Gate::allows('订单管理-工单处理-货主编辑') || Gate::allows('订单管理-工单处理-客服编辑')) {
  130. $this->queryBuilder->whereIn('owner_id', $owner_ids);
  131. }
  132. }
  133. private function afterFilterLogistic($logistic_ids)
  134. {
  135. if (Gate::allows('订单管理-工单处理-承运商编辑') && !Gate::allows('订单管理-工单处理-宝时编辑')) {
  136. $this->queryBuilder->whereIn('logistic_id', array_values($logistic_ids));
  137. }
  138. }
  139. public function afterFileIssueType()
  140. {
  141. if (Gate::allows('订单管理-工单处理-客服编辑') || Gate::allows('订单管理-工单处理-货主编辑')) {
  142. $this->getOrderIssueTypeQuery()->whereIn('name', ['拦截', '取消拦截', '信息更改', '其他', '快递异常', '错漏发', '破损', '快递丢件']);
  143. } else if (Gate::allows('订单管理-工单处理-承运商编辑')) {
  144. $this->getOrderIssueTypeQuery()->whereIn('name', ['拦截', '取消拦截', '信息更改', '破损', '快递丢件', '快递异常']);
  145. }
  146. }
  147. public function beforeApply()
  148. {
  149. if ($this->shopQuery) {
  150. $this->getOrderQuery()->whereIn('orders.shop_id', $this->shopQuery);
  151. }
  152. if ($this->orderPackageQuery) {
  153. $this->queryBuilder->whereIn('order_id', $this->orderPackageQuery);
  154. }
  155. if($this->orderDetailQuery){
  156. $this->queryBuilder->whereIn('order_id',$this->orderDetailQuery);
  157. }
  158. if ($this->orderQuery) {
  159. $this->queryBuilder->whereIn('order_id', $this->orderQuery);
  160. }
  161. if ($this->issueTypeQuery) {
  162. $this->queryBuilder->whereIn('order_issue_type_id', $this->issueTypeQuery);
  163. }
  164. if ($this->orderIssueQuery) {
  165. $this->queryBuilder->whereIn('work_orders.order_id', $this->orderIssueQuery);
  166. }
  167. if ($this->workOrderProcessLogQuery) {
  168. $this->queryBuilder->whereIn('work_orders.id', $this->workOrderProcessLogQuery);
  169. }
  170. $this->orderByTag();
  171. }
  172. public function orderByTag()
  173. {
  174. $this->queryBuilder->orderByDesc('work_order_status');
  175. if (Gate::allows('订单管理-工单处理-客服编辑')) {
  176. $this->queryBuilder->orderByDesc("bao_shi_tag");
  177. } else if (Gate::allows('订单管理-工单处理-货主编辑')) {
  178. $this->queryBuilder->orderByDesc("owner_tag");
  179. } else if (Gate::allows('订单管理-工单处理-承运商编辑')) {
  180. $this->queryBuilder->orderByDesc("logistic_tag");
  181. }
  182. $this->queryBuilder->orderBy('created_at');
  183. }
  184. public function getOrderQuery(): Builder
  185. {
  186. if (!$this->orderQuery) {
  187. $this->orderQuery = Order::query()->select('id');
  188. }
  189. return $this->orderQuery;
  190. }
  191. public function getOrderPackageQuery(): Builder
  192. {
  193. if (!$this->orderPackageQuery) {
  194. $this->orderPackageQuery = OrderPackage::query()->select('order_id');
  195. }
  196. return $this->orderPackageQuery;
  197. }
  198. public function getOrderIssueTypeQuery(): Builder
  199. {
  200. if (!$this->issueTypeQuery) {
  201. $this->issueTypeQuery = OrderIssueType::query()->select('id');
  202. }
  203. return $this->issueTypeQuery;
  204. }
  205. public function getOrderIssueQuery(): Builder
  206. {
  207. if (!$this->orderIssueQuery) {
  208. $this->orderIssueQuery = OrderIssue::query()->select('order_issues.order_id');
  209. }
  210. return $this->orderIssueQuery;
  211. }
  212. public function getOrderIssueLogQuery(): Builder
  213. {
  214. if (!$this->orderIssueLogQuery) {
  215. $this->orderIssueLogQuery = OrderIssueProcessLog::query()->select('order_issue_id');
  216. }
  217. return $this->orderIssueLogQuery;
  218. }
  219. public function getShopQuery(): Builder
  220. {
  221. if (!$this->shopQuery) {
  222. $this->shopQuery = Shop::query()->select('id');
  223. }
  224. return $this->shopQuery;
  225. }
  226. public function getWorkOrderProcessLogQuery(): Builder
  227. {
  228. if (!$this->workOrderProcessLogQuery) {
  229. $this->workOrderProcessLogQuery = WorkOrderProcessLog::query()->select('work_order_id');
  230. }
  231. return $this->workOrderProcessLogQuery;
  232. }
  233. public function getOrderDetailQuery(): Builder
  234. {
  235. if (!$this->orderDetailQuery){
  236. $this->orderDetailQuery = OrderDetail::query()->select('order_id');
  237. }
  238. return $this->orderDetailQuery;
  239. }
  240. public function id($id)
  241. {
  242. if (is_array($id)) $this->queryBuilder->whereIn('work_orders.id', $id);
  243. else $this->queryBuilder->where('work_orders.id', $id);
  244. }
  245. // 创建开始时间
  246. public function created_at_start($create_at_start)
  247. {
  248. $this->queryBuilder->where('work_orders.created_at', '>=', $create_at_start);
  249. }
  250. // 创建结束时间
  251. public function created_at_end($created_at_end)
  252. {
  253. $this->queryBuilder->where('work_orders.created_at', '<=', $created_at_end);
  254. }
  255. // 终审开始时间
  256. public function review_at_start($review_at_start)
  257. {
  258. $this->queryBuilder->where('work_orders.review_at', '>=', $review_at_start);
  259. }
  260. // 终审结束时间
  261. public function review_at_end($review_at_end)
  262. {
  263. $this->queryBuilder->where('work_orders.review_at', '<=', $review_at_end);
  264. }
  265. // 创建人
  266. public function creator($creator)
  267. {
  268. $userQuery = User::query()->select('id')->where('name', 'like', "%{$creator}%");;
  269. $this->queryBuilder->whereIn('creator_id', $userQuery);
  270. }
  271. // 终审人
  272. public function reviewer($id)
  273. {
  274. if (is_array($id))
  275. $this->queryBuilder->whereIn('work_orders.reviewer_id', $id);
  276. else
  277. $this->queryBuilder->where('work_orders.reviewer_id', $id);
  278. }
  279. // 类型
  280. public function word_order_types($word_order_types)
  281. {
  282. if (!$this->workOrderTypeQuery)
  283. $this->workOrderTypeQuery = WorkOrder::query()->select('id');
  284. if (is_array($word_order_types))
  285. $this->workOrderTypeQuery->whereIn('id', $word_order_types);
  286. else $this->workOrderTypeQuery->where('id', $word_order_types);
  287. }
  288. public function order_issue_type($order_issue_type)
  289. {
  290. $this->searchWay($this->queryBuilder,$order_issue_type,'order_issue_type_id');
  291. }
  292. // 快递单号
  293. public function logistic_number($logistic_number)
  294. {
  295. $this->searchWay($this->getOrderPackageQuery(), $logistic_number, 'order_packages.logistic_number');
  296. }
  297. // 对应问题件
  298. public function is_issue_order($is_issue_order)
  299. {
  300. $orderIssueQuery = OrderIssue::query()->select('order_id')->whereIn('order_id', WorkOrder::query()->select('order_id'));
  301. if ($is_issue_order == 'true') {
  302. $this->queryBuilder->whereIn('order_id', $orderIssueQuery);
  303. } else {
  304. $this->queryBuilder->whereNotIn('order_id', $orderIssueQuery);
  305. }
  306. }
  307. // 承运商筛选
  308. public function logistic($logistic)
  309. {
  310. $this->searchWay($this->queryBuilder, $logistic, 'logistic_id');
  311. }
  312. // 货主
  313. public function owner($owner)
  314. {
  315. $this->searchWay($this->queryBuilder, $owner, 'work_orders.owner_id');
  316. }
  317. public function client_code($client_code)
  318. {
  319. $this->searchWay($this->getOrderQuery(), $client_code, 'orders.client_code');
  320. }
  321. public function process_progress($process_progress)
  322. {
  323. $this->queryBuilder->where('work_orders.process_progress',$process_progress);
  324. // $this->searchWay($this->queryBuilder, $process_progress, 'work_orders.process_progress');
  325. }
  326. public function order_issue_log($log_content)
  327. {
  328. $order_issue_process_log_query = OrderIssueProcessLog::query()->select('order_issue_id')->where('content', 'like', $log_content);
  329. $order_issue_query = OrderIssue::query()->select('order_id')->whereIn('id', $order_issue_process_log_query);
  330. $this->queryBuilder->whereIn('order_id', $order_issue_query);
  331. }
  332. public function log_content($log_content)
  333. {
  334. $order_issue_process_log_query = OrderIssueProcessLog::query()->selectRaw('order_issue_id')->where('content', 'like', $log_content);
  335. if (!array_key_exists('addtime', $this->params)) {
  336. $order_issue_process_log_query->where('created_at', '>=', Carbon::now()->subDays(31));
  337. } else {
  338. $order_issue_process_log_query->where('created_at', '>=', Carbon::now()->subDays($this->params['addtime']));
  339. }
  340. $this->getOrderIssueQuery()->whereIn('id', $order_issue_process_log_query);
  341. }
  342. public function status($status)
  343. {
  344. $status = array_filter(preg_split('/[,, ]+/u', $status));
  345. $status_list = [];
  346. if (in_array('承运商处理',$status)) {
  347. array_push($status_list, 3);
  348. }
  349. if (in_array('宝时处理',$status)) {
  350. array_push($status_list, 4, 1);
  351. }
  352. if (in_array('货主处理',$status)) {
  353. array_push($status_list, 2, 6);
  354. }
  355. $this->queryBuilder->whereIn('status', $status_list);
  356. }
  357. public function tags($tag)
  358. {
  359. $status = $this->array_filter['status'] ?? null;
  360. if ($status) {
  361. switch ($status) {
  362. case '宝时处理':
  363. $this->queryBuilder->where('bao_shi_tag', $tag);
  364. break;
  365. case '货主处理':
  366. $this->queryBuilder->where('owner_tag', $tag);
  367. break;
  368. case '承运商处理':
  369. $this->queryBuilder->where('logistic_tag', $tag);
  370. break;
  371. }
  372. return;
  373. }
  374. if (Gate::allows('订单管理-工单处理-宝时编辑')) {
  375. $this->queryBuilder->where('bao_shi_tag', $tag);
  376. } else if (Gate::allows('订单管理-工单处理-货主编辑')) {
  377. $this->queryBuilder->where('owner_tag', $tag);
  378. } else if (Gate::allows('订单管理-工单处理-承运商编辑')) {
  379. $this->queryBuilder->where('logistic_tag', $tag);
  380. }
  381. }
  382. public function shop_name($shop_name)
  383. {
  384. $this->searchWay($this->getShopQuery(), $shop_name, 'shops.name');
  385. }
  386. public function work_order_process_log($work_order_process_log)
  387. {
  388. $this->searchWay($this->getWorkOrderProcessLogQuery(), $work_order_process_log, 'work_order_process_logs.content');
  389. }
  390. public function logistic_indemnity_money($logistic_indemnity_money)
  391. {
  392. $this->queryBuilder->where('logistic_indemnity_money',$logistic_indemnity_money);
  393. }
  394. public function logistic_express_remission($logistic_express_remission)
  395. {
  396. $this->queryBuilder->where('logistic_express_remission',$logistic_express_remission);
  397. }
  398. public function bao_shi_indemnity_money($bao_shi_indemnity_money)
  399. {
  400. $this->queryBuilder->where('bao_shi_indemnity_money',$bao_shi_indemnity_money);
  401. }
  402. public function bao_shi_express_remission($logistic_indemnity_money)
  403. {
  404. $this->queryBuilder->where('bao_shi_express_remission',$logistic_indemnity_money);
  405. }
  406. public function user_owner_group_id($user_owner_group_id)
  407. {
  408. $this->queryBuilder->where('user_owner_group_id',$user_owner_group_id);
  409. }
  410. public function user_work_group_id($user_work_group_id)
  411. {
  412. $this->queryBuilder->whereHas('userWorkGroups',function($query)use($user_work_group_id){
  413. $this->searchWay($query,$user_work_group_id,'user_workgroup_id');
  414. });
  415. }
  416. public function rejecting_status($rejecting_status)
  417. {
  418. $query = OrderDetail::query()->select('order_id')->where('rejecting_status',$rejecting_status);
  419. $this->queryBuilder->whereIn('order_id',$query);
  420. }
  421. public function rejectingStatus($rejectingStatus)
  422. {
  423. $this->getOrderDetailQuery()->where('rejecting_status',$rejectingStatus);
  424. }
  425. public function custom_rejected_status($custom_rejected_status)
  426. {
  427. $this->queryBuilder->where('custom_rejected_status',$custom_rejected_status);
  428. }
  429. }