WorkOrderFilters.php 16 KB

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