WorkOrderFilters.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. 'log_content',
  43. 'tags'
  44. ];
  45. protected $array_filter;
  46. protected $params = [];
  47. protected $workOrderTypeQuery;
  48. protected $orderQuery;
  49. protected $orderPackageQuery;
  50. protected $issueTypeQuery;
  51. protected $orderIssueLogQuery;
  52. protected $orderIssueQuery;
  53. public function __construct(Request $request)
  54. {
  55. $this->request = $request;
  56. $this->params = $request->all();
  57. $this->array_filter = array_filter($this->request->only($this->filters));
  58. }
  59. public function apply($builder)
  60. {
  61. $this->queryBuilder = $builder;
  62. $this->afterApply();
  63. foreach ($this->array_filter as $filter => $value) {
  64. if (method_exists($this, $filter)) {
  65. $this->$filter($value, $this->queryBuilder);
  66. }
  67. }
  68. $this->beforeApply();
  69. return $this->queryBuilder;
  70. }
  71. public function afterApply()
  72. {
  73. if (isset($this->params['data']))
  74. $this->id(explode(',', $this->params['data']));
  75. if (Gate::allows('订单管理-工单处理-货主编辑') || Gate::allows('订单管理-工单处理-客服编辑')) {
  76. $this->queryBuilder->whereIn('owner_id', app('UserService')->getPermittingOwnerIds(Auth::user()) ?? []);
  77. }
  78. $this->afterFilter();
  79. }
  80. private function afterFilter()
  81. {
  82. $user = Auth::user();
  83. $logistic_ids = App('UserService')->getPermittingLogisticIds($user);
  84. $owner_ids = app('UserService')->getPermittingOwnerIds($user);
  85. $this->afterFilterLogistic($logistic_ids);
  86. $this->afterFilterOwner($owner_ids);
  87. $this->afterFileIssueType();
  88. $this->filterStatus();
  89. }
  90. private function filterStatus()
  91. {
  92. $status = [];
  93. if (Gate::allows('订单管理-工单处理-宝时编辑')){
  94. array_push($status,1,4);
  95. }
  96. if (Gate::allows('订单管理-工单处理-货主编辑')){
  97. array_push($status,1,2,3,4,6);
  98. }
  99. if (Gate::allows('订单管理-工单处理-承运商编辑')){
  100. array_push($status,1,2,3,4,6);
  101. }
  102. if (!isset($this->params['is_end'])) {
  103. $this->queryBuilder->where('status', '!=', 5); // 过滤已完成
  104. } else {
  105. array_push($status,5);
  106. }
  107. $this->queryBuilder->whereIn('status',$status);
  108. }
  109. private function afterFilterOwner($owner_ids)
  110. {
  111. if (Gate::allows('订单管理-工单处理-货主编辑') || Gate::allows('订单管理-工单处理-客服编辑')) {
  112. $this->queryBuilder->whereIn('owner_id', $owner_ids);
  113. }
  114. }
  115. private function afterFilterLogistic($logistic_ids)
  116. {
  117. if (Gate::allows('订单管理-工单处理-承运商编辑') && !Gate::allows('订单管理-工单处理-宝时编辑')) {
  118. $this->queryBuilder->whereIn('logistic_id', array_values($logistic_ids));
  119. }
  120. }
  121. public function afterFileIssueType()
  122. {
  123. if (Gate::allows('订单管理-工单处理-客服编辑') || Gate::allows('订单管理-工单处理-货主编辑')) {
  124. $this->getOrderIssueTypeQuery()->whereIn('name', ['拦截','取消拦截', '信息更改', '其他', '快递异常', '错漏发', '破损', '快递丢件']);
  125. } else if (Gate::allows('订单管理-工单处理-承运商编辑')) {
  126. $this->getOrderIssueTypeQuery()->whereIn('name', ['拦截','取消拦截', '信息更改', '破损', '快递丢件', '快递异常']);
  127. }
  128. }
  129. public function beforeApply()
  130. {
  131. if ($this->orderPackageQuery) {
  132. $this->queryBuilder->whereIn('order_id', $this->orderPackageQuery);
  133. }
  134. if ($this->orderQuery) {
  135. $this->queryBuilder->whereIn('order_id', $this->orderQuery);
  136. }
  137. if ($this->issueTypeQuery) {
  138. $this->queryBuilder->whereIn('order_issue_type_id', $this->issueTypeQuery);
  139. }
  140. if ($this->orderIssueQuery){
  141. $this->queryBuilder->whereIn('work_orders.order_id', $this->orderIssueQuery);
  142. }
  143. $this->orderByTag();
  144. }
  145. public function orderByTag()
  146. {
  147. $this->queryBuilder->orderByDesc('work_order_status');
  148. if (Gate::allows('订单管理-工单处理-客服编辑')){
  149. $this->queryBuilder->orderByDesc("bao_shi_tag");
  150. } else if (Gate::allows('订单管理-工单处理-货主编辑')){
  151. $this->queryBuilder->orderByDesc("owner_tag");
  152. } else if (Gate::allows('订单管理-工单处理-承运商编辑')){
  153. $this->queryBuilder->orderByDesc("logistic_tag");
  154. }
  155. $this->queryBuilder->orderBy('created_at');
  156. }
  157. public function getOrderQuery(): Builder
  158. {
  159. if (!$this->orderQuery) {
  160. $this->orderQuery = Order::query()->select('id');
  161. }
  162. return $this->orderQuery;
  163. }
  164. public function getOrderPackageQuery(): Builder
  165. {
  166. if (!$this->orderPackageQuery) {
  167. $this->orderPackageQuery = OrderPackage::query()->select('order_id');
  168. }
  169. return $this->orderPackageQuery;
  170. }
  171. public function getOrderIssueTypeQuery(): Builder
  172. {
  173. if (!$this->issueTypeQuery) {
  174. $this->issueTypeQuery = OrderIssueType::query()->select('id');
  175. }
  176. return $this->issueTypeQuery;
  177. }
  178. public function getOrderIssueQuery(): Builder
  179. {
  180. if (!$this->orderIssueQuery) {
  181. $this->orderIssueQuery = OrderIssue::query()->select('order_issues.order_id');
  182. }
  183. return $this->orderIssueQuery;
  184. }
  185. public function getOrderIssueLogQuery(): Builder
  186. {
  187. if (!$this->orderIssueLogQuery) {
  188. $this->orderIssueLogQuery = OrderIssueProcessLog::query()->select('order_issue_id');
  189. }
  190. return $this->orderIssueLogQuery;
  191. }
  192. public function id($id)
  193. {
  194. if (is_array($id)) $this->queryBuilder->whereIn('work_orders.id', $id);
  195. else $this->queryBuilder->where('work_orders.id', $id);
  196. }
  197. // 创建开始时间
  198. public function created_at_start($create_at_start)
  199. {
  200. $this->queryBuilder->where('work_orders.created_at', '>=', $create_at_start);
  201. }
  202. // 创建结束时间
  203. public function created_at_end($created_at_end)
  204. {
  205. $this->queryBuilder->where('work_orders.created_at', '<=', $created_at_end);
  206. }
  207. // 终审开始时间
  208. public function review_at_start($review_at_start)
  209. {
  210. $this->queryBuilder->where('work_orders.review_at', '>=', $review_at_start);
  211. }
  212. // 终审结束时间
  213. public function review_at_end($review_at_end)
  214. {
  215. $this->queryBuilder->where('work_orders.review_at', '<=', $review_at_end);
  216. }
  217. // 创建人
  218. public function creator($creator)
  219. {
  220. $userQuery = User::query()->select('id')->where('name', 'like', "%{$creator}%");;
  221. $this->queryBuilder->whereIn('creator_id', $userQuery);
  222. }
  223. // 终审人
  224. public function reviewer($id)
  225. {
  226. if (is_array($id))
  227. $this->queryBuilder->whereIn('work_orders.reviewer_id', $id);
  228. else
  229. $this->queryBuilder->where('work_orders.reviewer_id', $id);
  230. }
  231. // 类型
  232. public function word_order_types($word_order_types)
  233. {
  234. if (!$this->workOrderTypeQuery)
  235. $this->workOrderTypeQuery = WorkOrder::query()->select('id');
  236. if (is_array($word_order_types))
  237. $this->workOrderTypeQuery->whereIn('id', $word_order_types);
  238. else $this->workOrderTypeQuery->where('id', $word_order_types);
  239. }
  240. public function order_issue_type($order_issue_type)
  241. {
  242. $this->queryBuilder->where('order_issue_type_id', $order_issue_type);
  243. }
  244. // 快递单号
  245. public function logistic_number($logistic_number)
  246. {
  247. $this->searchWay($this->getOrderPackageQuery(), $logistic_number, 'order_packages.logistic_number');
  248. }
  249. // 对应问题件
  250. public function is_issue_order($is_issue_order)
  251. {
  252. $orderIssueQuery = OrderIssue::query()->select('order_id')->whereIn('order_id', WorkOrder::query()->select('order_id'));
  253. if ($is_issue_order == 'true') {
  254. $this->queryBuilder->whereIn('order_id', $orderIssueQuery);
  255. } else {
  256. $this->queryBuilder->whereNotIn('order_id', $orderIssueQuery);
  257. }
  258. }
  259. // 承运商筛选
  260. public function logistic($logistic)
  261. {
  262. $this->searchWay($this->queryBuilder, $logistic, 'logistic_id');
  263. }
  264. // 货主
  265. public function owner($owner)
  266. {
  267. $this->searchWay($this->queryBuilder, $owner, 'work_orders.owner_id');
  268. }
  269. public function client_code($client_code)
  270. {
  271. $this->searchWay($this->getOrderQuery(), $client_code, 'orders.client_code');
  272. }
  273. public function process_progress($process_progress)
  274. {
  275. $this->searchWay($this->queryBuilder,$process_progress,'work_orders.process_progress');
  276. }
  277. public function order_issue_log($log_content){
  278. $order_issue_process_log_query = OrderIssueProcessLog::query()->select('order_issue_id')->where('content', 'like', $log_content);
  279. $order_issue_query = OrderIssue::query()->select('order_id')->whereIn('id',$order_issue_process_log_query);
  280. $this->queryBuilder->whereIn('order_id',$order_issue_query);
  281. }
  282. public function log_content($log_content)
  283. {
  284. $order_issue_process_log_query = OrderIssueProcessLog::query()->selectRaw('order_issue_id')->where('content', 'like', $log_content);
  285. if (!array_key_exists('addtime',$this->params) ) {
  286. $order_issue_process_log_query->where('created_at', '>=', Carbon::now()->subDays(31));
  287. } else {
  288. $order_issue_process_log_query->where('created_at', '>=', Carbon::now()->subDays($this->params['addtime']));
  289. }
  290. $this->getOrderIssueQuery()->whereIn('id',$order_issue_process_log_query);
  291. }
  292. public function status($status){
  293. $status_list = [];
  294. if ($status == '承运商处理'){
  295. array_push($status_list,3);
  296. } elseif ($status == '宝时处理') {
  297. array_push($status_list,4,1);
  298. } elseif ($status == '货主处理'){
  299. array_push($status_list,2,6);
  300. }
  301. $this->queryBuilder->whereIn('status',$status_list);
  302. }
  303. public function tags($tag){
  304. $status = $this->array_filter['status'] ?? null;
  305. if ($status){
  306. switch ($status){
  307. case '宝时处理':
  308. $this->queryBuilder->where('bao_shi_tag',$tag);
  309. break;
  310. case '货主处理':
  311. $this->queryBuilder->where('owner_tag',$tag);
  312. break;
  313. case '承运商处理':
  314. $this->queryBuilder->where('logistic_tag',$tag);
  315. break;
  316. }
  317. return ;
  318. }
  319. if (Gate::allows('订单管理-工单处理-宝时编辑')){
  320. $this->queryBuilder->where('bao_shi_tag',$tag);
  321. } else if (Gate::allows('订单管理-工单处理-货主编辑')){
  322. $this->queryBuilder->where('owner_tag',$tag);
  323. } else if (Gate::allows('订单管理-工单处理-承运商编辑')){
  324. $this->queryBuilder->where('logistic_tag',$tag);
  325. }
  326. }
  327. }