WorkOrderFilters.php 13 KB

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