WorkOrderFilters.php 8.0 KB

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