WorkOrderFilters.php 17 KB

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