WorkOrderFilters.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. namespace App\Filters;
  3. use App\Order;
  4. use App\OrderIssue;
  5. use App\OrderIssueType;
  6. use App\OrderPackage;
  7. use App\Traits\ModelSearchWay;
  8. use App\User;
  9. use App\WorkOrder;
  10. use Illuminate\Database\Eloquent\Builder;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Facades\Auth;
  13. use Illuminate\Support\Facades\Gate;
  14. class WorkOrderFilters
  15. {
  16. use ModelSearchWay;
  17. /** @var Builder $queryBuilder */
  18. protected $request;
  19. protected $queryBuilder;
  20. protected $filters = [
  21. 'ids',
  22. 'creator',
  23. 'remake',
  24. 'created_at_start', 'created_at_end',
  25. 'review_at_start', 'review_at_end',
  26. 'reviewer',
  27. 'word_order_types',
  28. 'word_order_child_types',
  29. 'logistic',
  30. 'is_review',
  31. 'logistic_number',
  32. 'is_issue_order',
  33. 'order_issue_type',
  34. 'owner',
  35. 'client_code',
  36. 'is_end',
  37. 'status',
  38. 'process_progress'
  39. ];
  40. protected $array_filter;
  41. protected $params = [];
  42. protected $workOrderTypeQuery;
  43. protected $orderQuery;
  44. protected $orderPackageQuery;
  45. protected $issueTypeQuery;
  46. public function __construct(Request $request)
  47. {
  48. $this->request = $request;
  49. $this->params = $request->all();
  50. $this->array_filter = array_filter($this->request->only($this->filters));
  51. }
  52. public function apply($builder)
  53. {
  54. $this->queryBuilder = $builder;
  55. $this->afterApply();
  56. foreach ($this->array_filter as $filter => $value) {
  57. if (method_exists($this, $filter)) {
  58. $this->$filter($value, $this->queryBuilder);
  59. }
  60. }
  61. $this->beforeApply();
  62. return $this->queryBuilder;
  63. }
  64. public function afterApply()
  65. {
  66. if (isset($this->params['data']))
  67. $this->id(explode(',', $this->params['data']));
  68. if (Gate::allows('订单管理-工单处理-货主编辑') || Gate::allows('订单管理-工单处理-客服编辑')) {
  69. $this->queryBuilder->whereIn('owner_id', app('UserService')->getPermittingOwnerIds(Auth::user()) ?? []);
  70. }
  71. $this->afterFilter();
  72. }
  73. private function afterFilter()
  74. {
  75. $user = Auth::user();
  76. $logistic_ids = App('UserService')->getPermittingLogisticIds($user);
  77. $owner_ids = app('UserService')->getPermittingOwnerIds($user);
  78. $this->afterFilterLogistic($logistic_ids);
  79. $this->afterFilterOwner($owner_ids);
  80. $this->afterFileIssueType();
  81. $this->filterStatus();
  82. }
  83. private function filterStatus()
  84. {
  85. $status = [];
  86. if (Gate::allows('订单管理-工单处理-宝时编辑')){
  87. array_push($status,1,4);
  88. }
  89. if (Gate::allows('订单管理-工单处理-货主编辑')){
  90. array_push($status,1,2,3,4,6);
  91. }
  92. if (Gate::allows('订单管理-工单处理-承运商编辑')){
  93. array_push($status,1,2,3,4,6);
  94. }
  95. if (!isset($this->params['is_end'])) {
  96. $this->queryBuilder->where('status', '!=', 5); // 过滤已完成
  97. } else {
  98. array_push($status,5);
  99. }
  100. $this->queryBuilder->whereIn('status',$status);
  101. }
  102. private function afterFilterOwner($owner_ids)
  103. {
  104. if (Gate::allows('订单管理-工单处理-货主编辑') || Gate::allows('订单管理-工单处理-客服编辑')) {
  105. $this->queryBuilder->whereIn('owner_id', $owner_ids);
  106. }
  107. }
  108. private function afterFilterLogistic($logistic_ids)
  109. {
  110. if (Gate::allows('订单管理-工单处理-承运商编辑') && !Gate::allows('订单管理-工单处理-宝时编辑')) {
  111. $this->queryBuilder->whereIn('logistic_id', array_values($logistic_ids));
  112. }
  113. }
  114. public function afterFileIssueType()
  115. {
  116. if (Gate::allows('订单管理-工单处理-客服编辑') || Gate::allows('订单管理-工单处理-货主编辑')) {
  117. $this->getOrderIssueQuery()->whereIn('name', ['拦截','取消拦截', '信息更改', '其他', '快递异常', '错漏发', '破损', '快递丢件']);
  118. } else if (Gate::allows('订单管理-工单处理-承运商编辑')) {
  119. $this->getOrderIssueQuery()->whereIn('name', ['拦截','取消拦截', '信息更改', '破损', '快递丢件', '快递异常']);
  120. }
  121. }
  122. public function beforeApply()
  123. {
  124. if ($this->orderPackageQuery) {
  125. $this->queryBuilder->whereIn('order_id', $this->orderPackageQuery);
  126. }
  127. if ($this->orderQuery) {
  128. $this->queryBuilder->whereIn('order_id', $this->orderQuery);
  129. }
  130. if ($this->issueTypeQuery) {
  131. $this->queryBuilder->whereIn('order_issue_type_id', $this->issueTypeQuery);
  132. }
  133. $this->orderByTag();
  134. }
  135. public function orderByTag()
  136. {
  137. $this->queryBuilder->orderByDesc('work_order_status');
  138. if (Gate::allows('订单管理-工单处理-客服编辑')){
  139. $this->queryBuilder->orderByDesc("bao_shi_tag");
  140. } else if (Gate::allows('订单管理-工单处理-货主编辑')){
  141. $this->queryBuilder->orderByDesc("owner_tag");
  142. } else if (Gate::allows('订单管理-工单处理-承运商编辑')){
  143. $this->queryBuilder->orderByDesc("logistic_tag");
  144. }
  145. $this->queryBuilder->orderBy('created_at');
  146. }
  147. public function getOrderQuery(): Builder
  148. {
  149. if (!$this->orderQuery) {
  150. $this->orderQuery = Order::query()->select('id');
  151. }
  152. return $this->orderQuery;
  153. }
  154. public function getOrderPackageQuery(): Builder
  155. {
  156. if (!$this->orderPackageQuery) {
  157. $this->orderPackageQuery = OrderPackage::query()->select('order_id');
  158. }
  159. return $this->orderPackageQuery;
  160. }
  161. public function getOrderIssueQuery(): Builder
  162. {
  163. if (!$this->issueTypeQuery) {
  164. $this->issueTypeQuery = OrderIssueType::query()->select('id');
  165. }
  166. return $this->issueTypeQuery;
  167. }
  168. public function id($id)
  169. {
  170. if (is_array($id)) $this->queryBuilder->whereIn('work_orders.id', $id);
  171. else $this->queryBuilder->where('work_orders.id', $id);
  172. }
  173. // 创建开始时间
  174. public function created_at_start($create_at_start)
  175. {
  176. $this->queryBuilder->where('work_orders.created_at', '>=', $create_at_start);
  177. }
  178. // 创建结束时间
  179. public function created_at_end($created_at_end)
  180. {
  181. $this->queryBuilder->where('work_orders.created_at', '<=', $created_at_end);
  182. }
  183. // 终审开始时间
  184. public function review_at_start($review_at_start)
  185. {
  186. $this->queryBuilder->where('work_orders.review_at', '>=', $review_at_start);
  187. }
  188. // 终审结束时间
  189. public function review_at_end($review_at_end)
  190. {
  191. $this->queryBuilder->where('work_orders.review_at', '<=', $review_at_end);
  192. }
  193. // 创建人
  194. public function creator($creator)
  195. {
  196. $userQuery = User::query()->select('id')->where('name', 'like', "%{$creator}%");;
  197. $this->queryBuilder->whereIn('creator_id', $userQuery);
  198. }
  199. // 终审人
  200. public function reviewer($id)
  201. {
  202. if (is_array($id))
  203. $this->queryBuilder->whereIn('work_orders.reviewer_id', $id);
  204. else
  205. $this->queryBuilder->where('work_orders.reviewer_id', $id);
  206. }
  207. // 类型
  208. public function word_order_types($word_order_types)
  209. {
  210. if (!$this->workOrderTypeQuery)
  211. $this->workOrderTypeQuery = WorkOrder::query()->select('id');
  212. if (is_array($word_order_types))
  213. $this->workOrderTypeQuery->whereIn('id', $word_order_types);
  214. else $this->workOrderTypeQuery->where('id', $word_order_types);
  215. }
  216. public function order_issue_type($order_issue_type)
  217. {
  218. $this->queryBuilder->where('order_issue_type_id', $order_issue_type);
  219. }
  220. // 快递单号
  221. public function logistic_number($logistic_number)
  222. {
  223. $this->searchWay($this->getOrderPackageQuery(), $logistic_number, 'order_packages.logistic_number');
  224. }
  225. // 对应问题件
  226. public function is_issue_order($is_issue_order)
  227. {
  228. $orderIssueQuery = OrderIssue::query()->select('order_id')->whereIn('order_id', WorkOrder::query()->select('order_id'));
  229. if ($is_issue_order == 'true') {
  230. $this->queryBuilder->whereIn('order_id', $orderIssueQuery);
  231. } else {
  232. $this->queryBuilder->whereNotIn('order_id', $orderIssueQuery);
  233. }
  234. }
  235. // 承运商筛选
  236. public function logistic($logistic)
  237. {
  238. $this->searchWay($this->queryBuilder, $logistic, 'logistic_id');
  239. }
  240. // 货主
  241. public function owner($owner)
  242. {
  243. $this->searchWay($this->queryBuilder, $owner, 'work_orders.owner_id');
  244. }
  245. public function client_code($client_code)
  246. {
  247. $this->searchWay($this->getOrderQuery(), $client_code, 'orders.client_code');
  248. }
  249. public function process_progress($process_progress)
  250. {
  251. $this->searchWay($this->queryBuilder,$process_progress,'work_orders.process_progress');
  252. }
  253. public function status($status){
  254. $status_list = [];
  255. if ($status == '承运商处理'){
  256. array_push($status_list,3);
  257. } elseif ($status == '宝时处理') {
  258. array_push($status_list,4,1);
  259. } elseif ($status == '货主处理'){
  260. array_push($status_list,2,6);
  261. }
  262. $this->queryBuilder->whereIn('status',$status_list);
  263. }
  264. }