WorkOrderFilters.php 16 KB

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