WorkOrderFilters.php 8.6 KB

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