WorkOrderFilters.php 13 KB

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