WorkOrderFilters.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. }
  134. public function getOrderQuery(): Builder
  135. {
  136. if (!$this->orderQuery) {
  137. $this->orderQuery = Order::query()->select('id');
  138. }
  139. return $this->orderQuery;
  140. }
  141. public function getOrderPackageQuery(): Builder
  142. {
  143. if (!$this->orderPackageQuery) {
  144. $this->orderPackageQuery = OrderPackage::query()->select('order_id');
  145. }
  146. return $this->orderPackageQuery;
  147. }
  148. public function getOrderIssueQuery(): Builder
  149. {
  150. if (!$this->issueTypeQuery) {
  151. $this->issueTypeQuery = OrderIssueType::query()->select('id');
  152. }
  153. return $this->issueTypeQuery;
  154. }
  155. public function id($id)
  156. {
  157. if (is_array($id)) $this->queryBuilder->whereIn('work_orders.id', $id);
  158. else $this->queryBuilder->where('work_orders.id', $id);
  159. }
  160. // 创建开始时间
  161. public function created_at_start($create_at_start)
  162. {
  163. $this->queryBuilder->where('work_orders.created_at', '>=', $create_at_start);
  164. }
  165. // 创建结束时间
  166. public function created_at_end($created_at_end)
  167. {
  168. $this->queryBuilder->where('work_orders.created_at', '<=', $created_at_end);
  169. }
  170. // 终审开始时间
  171. public function review_at_start($review_at_start)
  172. {
  173. $this->queryBuilder->where('work_orders.review_at', '>=', $review_at_start);
  174. }
  175. // 终审结束时间
  176. public function review_at_end($review_at_end)
  177. {
  178. $this->queryBuilder->where('work_orders.review_at', '<=', $review_at_end);
  179. }
  180. // 创建人
  181. public function creator($creator)
  182. {
  183. $userQuery = User::query()->select('id')->where('name', 'like', "%{$creator}%");;
  184. $this->queryBuilder->whereIn('creator_id', $userQuery);
  185. }
  186. // 终审人
  187. public function reviewer($id)
  188. {
  189. if (is_array($id))
  190. $this->queryBuilder->whereIn('work_orders.reviewer_id', $id);
  191. else
  192. $this->queryBuilder->where('work_orders.reviewer_id', $id);
  193. }
  194. // 类型
  195. public function word_order_types($word_order_types)
  196. {
  197. if (!$this->workOrderTypeQuery)
  198. $this->workOrderTypeQuery = WorkOrder::query()->select('id');
  199. if (is_array($word_order_types))
  200. $this->workOrderTypeQuery->whereIn('id', $word_order_types);
  201. else $this->workOrderTypeQuery->where('id', $word_order_types);
  202. }
  203. public function order_issue_type($order_issue_type)
  204. {
  205. $this->queryBuilder->where('order_issue_type_id', $order_issue_type);
  206. }
  207. // 快递单号
  208. public function logistic_number($logistic_number)
  209. {
  210. $this->searchWay($this->getOrderPackageQuery(), $logistic_number, 'order_packages.logistic_number');
  211. }
  212. // 对应问题件
  213. public function is_issue_order($is_issue_order)
  214. {
  215. $orderIssueQuery = OrderIssue::query()->select('order_id')->whereIn('order_id', WorkOrder::query()->select('order_id'));
  216. if ($is_issue_order == 'true') {
  217. $this->queryBuilder->whereIn('order_id', $orderIssueQuery);
  218. } else {
  219. $this->queryBuilder->whereNotIn('order_id', $orderIssueQuery);
  220. }
  221. }
  222. // 承运商筛选
  223. public function logistic($logistic)
  224. {
  225. $this->searchWay($this->queryBuilder, $logistic, 'logistic_id');
  226. }
  227. // 货主
  228. public function owner($owner)
  229. {
  230. $this->searchWay($this->queryBuilder, $owner, 'work_orders.owner_id');
  231. }
  232. public function client_code($client_code)
  233. {
  234. $this->searchWay($this->getOrderQuery(), $client_code, 'orders.client_code');
  235. }
  236. public function process_progress($process_progress)
  237. {
  238. $this->searchWay($this->queryBuilder,$process_progress,'work_order.process_progress');
  239. }
  240. public function status($status){
  241. $status_list = [];
  242. if ($status == '承运商处理'){
  243. array_push($status_list,3);
  244. } elseif ($status == '宝时处理') {
  245. array_push($status_list,4,1);
  246. } elseif ($status == '货主处理'){
  247. array_push($status_list,2,6);
  248. }
  249. $this->queryBuilder->whereIn('status',$status_list);
  250. }
  251. }