WorkOrderFilters.php 10 KB

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