WorkOrderFilters.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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\Services\OwnerService;
  8. use App\Traits\ModelSearchWay;
  9. use App\User;
  10. use App\WorkOrder;
  11. use Illuminate\Database\Eloquent\Builder;
  12. use Illuminate\Http\Request;
  13. use Illuminate\Support\Facades\Auth;
  14. use Illuminate\Support\Facades\Gate;
  15. class WorkOrderFilters
  16. {
  17. use ModelSearchWay;
  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. 'grad',
  35. 'owner',
  36. 'client_code',
  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 (!isset($this->params['owner'])){
  67. if(Gate::allows('订单管理-工单处理-客服编辑') || Gate::allows('订单管理-工单处理-承运商编辑')){
  68. }else if(Gate::allows('订单管理-工单处理-货主编辑')){
  69. $this->queryBuilder->whereIn('owner_id', app('UserService')->getPermittingOwnerIds(Auth::user())??[]);
  70. }
  71. }
  72. $this->afterFilter();
  73. }
  74. private function afterFilter()
  75. {
  76. $user = Auth::user();
  77. $logistic_ids = App('UserService')->getPermittingLogisticIds($user);
  78. $owner_ids = app('UserService')->getPermittingOwnerIds($user);
  79. $this->afterFilterLogistic($owner_ids,$logistic_ids);
  80. $this->afterFilterOwner($owner_ids);
  81. $this->afterFileIssueType();
  82. }
  83. // 可见货主过滤
  84. private function afterFilterOwner($owner_ids)
  85. {
  86. if(Gate::allows('订单管理-工单处理-客服编辑') || Gate::allows('订单管理-工单处理-承运商编辑')){
  87. }else if(Gate::allows('订单管理-工单处理-货主编辑')){
  88. $this->queryBuilder->whereIn('owner_id',$owner_ids);
  89. }
  90. }
  91. // 可见承运商过滤
  92. private function afterFilterLogistic($owner_ids,$logistic_ids)
  93. {
  94. if(Gate::allows('订单管理-工单处理-客服编辑') || Gate::allows('订单管理-工单处理-货主编辑')){
  95. if (!isset($this->params['owner'])){
  96. $this->queryBuilder->whereIn('owner_id', $owner_ids);
  97. }
  98. } else if (Gate::allows('订单管理-工单处理-承运商编辑')){
  99. $this->queryBuilder->whereIn('logistic_id',array_values($logistic_ids));
  100. }
  101. }
  102. public function afterFileIssueType()
  103. {
  104. if(Gate::allows('订单管理-工单处理-客服编辑') || Gate::allows('订单管理-工单处理-货主编辑')){
  105. $this->getOrderIssueQuery()->whereIn('name',['拦截', '信息更改', '其他', '快递异常', '错漏发', '破损', '快递丢件']);
  106. } else if (Gate::allows('订单管理-工单处理-承运商编辑')){
  107. $this->getOrderIssueQuery()->whereIn('name',['拦截', '信息更改', '破损', '快递丢件','快递异常']);
  108. }
  109. }
  110. public function beforeApply()
  111. {
  112. if ($this->orderPackageQuery){
  113. $this->queryBuilder->whereIn('order_id',$this->orderPackageQuery);
  114. }
  115. if ($this->orderQuery) {
  116. $this->queryBuilder->whereIn('order_id',$this->orderQuery);
  117. }
  118. if ($this->issueTypeQuery){
  119. $this->queryBuilder->whereIn('order_issue_type_id',$this->issueTypeQuery);
  120. }
  121. }
  122. public function getOrderQuery(): Builder
  123. {
  124. if (!$this->orderQuery){
  125. $this->orderQuery = Order::query()->select('id');
  126. }
  127. return $this->orderQuery;
  128. }
  129. public function getOrderPackageQuery(): Builder
  130. {
  131. if (!$this->orderPackageQuery){
  132. $this->orderPackageQuery = OrderPackage::query()->select('order_id');
  133. }
  134. return $this->orderPackageQuery;
  135. }
  136. public function getOrderIssueQuery(): Builder
  137. {
  138. if (!$this->issueTypeQuery){
  139. $this->issueTypeQuery = OrderIssueType::query()->select('id');
  140. }
  141. return $this->issueTypeQuery;
  142. }
  143. public function id($id)
  144. {
  145. if(is_array($id))$this->queryBuilder->whereIn('work_orders.id',$id);
  146. else $this->queryBuilder->where('work_orders.id',$id);
  147. }
  148. // 创建开始时间
  149. public function created_at_start($create_at_start)
  150. {
  151. $this->queryBuilder->where('work_orders.created_at','>=',$create_at_start);
  152. }
  153. // 创建结束时间
  154. public function created_at_end($created_at_end)
  155. {
  156. $this->queryBuilder->where('work_orders.created_at','<=',$created_at_end);
  157. }
  158. // 审核开始时间
  159. public function review_at_start($review_at_start)
  160. {
  161. $this->queryBuilder->where('work_orders.review_at','>=',$review_at_start);
  162. }
  163. // 审核结束时间
  164. public function review_at_end($review_at_end)
  165. {
  166. $this->queryBuilder->where('work_orders.review_at','<=',$review_at_end);
  167. }
  168. // 创建人
  169. public function creator($creator)
  170. {
  171. $userQuery = User::query()->select('id')->where('name','like',"%{$creator}%");;
  172. $this->queryBuilder->whereIn('creator_id',$userQuery);
  173. }
  174. // 审核人
  175. public function reviewer($id)
  176. {
  177. if (is_array($id))
  178. $this->queryBuilder->whereIn('work_orders.reviewer_id',$id);
  179. else
  180. $this->queryBuilder->where('work_orders.reviewer_id',$id);
  181. }
  182. // 类型
  183. public function word_order_types($word_order_types)
  184. {
  185. if(!$this->workOrderTypeQuery)
  186. $this->workOrderTypeQuery = WorkOrder::query()->select('id');
  187. if (is_array($word_order_types))
  188. $this->workOrderTypeQuery->whereIn('id',$word_order_types);
  189. else $this->workOrderTypeQuery->where('id',$word_order_types);
  190. }
  191. public function order_issue_type($order_issue_type)
  192. {
  193. $this->queryBuilder->where('order_issue_type_id',$order_issue_type);
  194. }
  195. // 快递单号
  196. public function logistic_number($logistic_number)
  197. {
  198. $this->searchWay($this->getOrderPackageQuery(),$logistic_number,'order_packages.logistic_number');
  199. }
  200. // 对应问题件
  201. public function is_issue_order($is_issue_order)
  202. {
  203. $orderIssueQuery = OrderIssue::query()->select('order_id')->whereIn('order_id',WorkOrder::query()->select('order_id'));
  204. if($is_issue_order == 'true'){
  205. $this->queryBuilder->whereIn('order_id',$orderIssueQuery);
  206. } else {
  207. $this->queryBuilder->whereNotIn('order_id',$orderIssueQuery);
  208. }
  209. }
  210. // 承运商筛选
  211. public function logistic($logistic)
  212. {
  213. $this->searchWay($this->queryBuilder,$logistic,'logistic_id');
  214. }
  215. public function grad($grad)
  216. {
  217. $this->queryBuilder->where('grad',$grad);
  218. }
  219. // 货主
  220. public function owner($owner)
  221. {
  222. $this->searchWay($this->queryBuilder,$owner,'work_orders.owner_id');
  223. }
  224. public function client_code($client_code)
  225. {
  226. $this->searchWay($this->getOrderQuery(),$client_code,'orders.client_code');
  227. }
  228. }