WorkOrderFilters.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. namespace App\Filters;
  3. use App\Order;
  4. use App\OrderIssue;
  5. use App\OrderPackage;
  6. use App\Services\OwnerService;
  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. protected $request;
  18. protected $queryBuilder;
  19. protected $filters = [
  20. 'ids',
  21. 'creator',
  22. 'remake',
  23. 'created_at_start','created_at_end',
  24. 'review_at_start','review_at_end',
  25. 'reviewer',
  26. 'word_order_types',
  27. 'word_order_child_types',
  28. 'logistic',
  29. 'is_review',
  30. 'logistic_number',
  31. 'is_issue_order',
  32. 'order_issue_type',
  33. 'grad',
  34. 'owner',
  35. 'client_code',
  36. ];
  37. protected $array_filter;
  38. protected $params = [];
  39. protected $workOrderTypeQuery;
  40. protected $orderQuery;
  41. protected $orderPackageQuery;
  42. public function __construct(Request $request)
  43. {
  44. $this->request = $request;
  45. $this->params = $request->all();
  46. $this->array_filter = array_filter($this->request->only($this->filters));
  47. }
  48. public function apply($builder)
  49. {
  50. $this->queryBuilder = $builder;
  51. $this->afterApply();
  52. foreach ($this->array_filter as $filter => $value) {
  53. if (method_exists($this, $filter)) {
  54. $this->$filter($value, $this->queryBuilder);
  55. }
  56. }
  57. $this->beforeApply();
  58. return $this->queryBuilder;
  59. }
  60. public function afterApply()
  61. {
  62. if(isset($this->params['data']))
  63. $this->id(explode(',',$this->params['data']));
  64. if (!isset($this->params['owner'])){
  65. $this->getOrderQuery()->whereIn('owner_id', app('UserService')->getPermittingOwnerIds(Auth::user())??[]);
  66. }
  67. $this->afterFilter();
  68. }
  69. private function afterFilter()
  70. {
  71. $user = Auth::user();
  72. $logistic_ids = App('UserService')->getPermittingLogisticIds($user);
  73. $owner_ids = app('UserService')->getPermittingOwnerIds($user);
  74. $this->afterFilterLogistic($owner_ids,$logistic_ids);
  75. $this->afterFilterOwner($owner_ids);
  76. }
  77. // 可见货主过滤
  78. private function afterFilterOwner($owner_ids)
  79. {
  80. if(Gate::allows('订单管理-工单处理-客服编辑') || Gate::allows('订单管理-工单处理-承运商编辑')){
  81. }else if(Gate::allows('订单管理-工单处理-货主编辑')){
  82. $this->queryBuilder->whereIn('owner_id',$owner_ids);
  83. }
  84. }
  85. // 可见承运商过滤
  86. private function afterFilterLogistic($owner_ids,$logistic_ids)
  87. {
  88. if(Gate::allows('订单管理-工单处理-客服编辑') || Gate::allows('订单管理-工单处理-货主编辑')){
  89. if (!isset($this->params['owner'])){
  90. $this->queryBuilder->whereIn('owner_id', $owner_ids);
  91. }
  92. } else if (Gate::allows('订单管理-工单处理-承运商编辑')){
  93. $this->queryBuilder->whereIn('logistic_id',array_values($logistic_ids));
  94. }
  95. }
  96. public function beforeApply()
  97. {
  98. if ($this->orderPackageQuery){
  99. $this->getOrderQuery()->whereIn('id',$this->orderPackageQuery);
  100. }
  101. if ($this->orderQuery) {
  102. $this->queryBuilder->whereIn('order_id',$this->orderQuery);
  103. }
  104. // 审核 默认为 待审核
  105. // if (isset($this->params['is_review'])){
  106. // $this->queryBuilder->where('status','2');
  107. // $this->queryBuilder->whereHas('orderIssue')->orderBydesc('work_orders.created_at');
  108. // } else {
  109. // $work_order_query = WorkOrder::query()->select('id')->where('status','2')->whereHas('orderIssue');
  110. // $this->queryBuilder->whereNotIn('id',$work_order_query);
  111. // }
  112. }
  113. public function getOrderQuery(): Builder
  114. {
  115. if (!$this->orderQuery){
  116. $this->orderQuery = Order::query()->select('id');
  117. }
  118. return $this->orderQuery;
  119. }
  120. public function getOrderPackageQuery(): Builder
  121. {
  122. if (!$this->orderPackageQuery){
  123. $this->orderPackageQuery = OrderPackage::query()->select('order_id');
  124. }
  125. return $this->orderPackageQuery;
  126. }
  127. public function id($id)
  128. {
  129. if(is_array($id))$this->queryBuilder->whereIn('work_orders.id',$id);
  130. else $this->queryBuilder->where('work_orders.id',$id);
  131. }
  132. // 创建开始时间
  133. public function created_at_start($create_at_start)
  134. {
  135. $this->queryBuilder->where('work_orders.created_at','>=',$create_at_start);
  136. }
  137. // 创建结束时间
  138. public function created_at_end($created_at_end)
  139. {
  140. $this->queryBuilder->where('work_orders.created_at','<=',$created_at_end);
  141. }
  142. // 审核开始时间
  143. public function review_at_start($review_at_start)
  144. {
  145. $this->queryBuilder->where('work_orders.review_at','>=',$review_at_start);
  146. }
  147. // 审核结束时间
  148. public function review_at_end($review_at_end)
  149. {
  150. $this->queryBuilder->where('work_orders.review_at','<=',$review_at_end);
  151. }
  152. // 创建人
  153. public function creator($creator)
  154. {
  155. $userQuery = User::query()->select('id')->where('name','like',"%{$creator}%");;
  156. $this->queryBuilder->whereIn('creator_id',$userQuery);
  157. }
  158. // 审核人
  159. public function reviewer($id)
  160. {
  161. if (is_array($id))
  162. $this->queryBuilder->whereIn('work_orders.reviewer_id',$id);
  163. else
  164. $this->queryBuilder->where('work_orders.reviewer_id',$id);
  165. }
  166. // 类型
  167. public function word_order_types($word_order_types)
  168. {
  169. if(!$this->workOrderTypeQuery)
  170. $this->workOrderTypeQuery = WorkOrder::query()->select('id');
  171. if (is_array($word_order_types))
  172. $this->workOrderTypeQuery->whereIn('id',$word_order_types);
  173. else $this->workOrderTypeQuery->where('id',$word_order_types);
  174. }
  175. public function order_issue_type($order_issue_type)
  176. {
  177. $this->queryBuilder->where('order_issue_type_id',$order_issue_type);
  178. }
  179. // 快递单号
  180. public function logistic_number($logistic_number)
  181. {
  182. $this->searchWay($this->getOrderPackageQuery(),$logistic_number,'order_packages.logistic_number');
  183. }
  184. // 对应问题件
  185. public function is_issue_order($is_issue_order)
  186. {
  187. $orderIssueQuery = OrderIssue::query()->select('order_id')->whereIn('order_id',WorkOrder::query()->select('order_id'));
  188. if($is_issue_order == 'true'){
  189. $this->queryBuilder->whereIn('order_id',$orderIssueQuery);
  190. } else {
  191. $this->queryBuilder->whereNotIn('order_id',$orderIssueQuery);
  192. }
  193. }
  194. // 承运商筛选
  195. public function logistic($logistic)
  196. {
  197. $orderQuery = $this->getOrderQuery()->whereIn('id',WorkOrder::query()->select('order_id'));
  198. $this->searchWay($orderQuery,$logistic,'logistic_id');
  199. }
  200. public function grad($grad)
  201. {
  202. $this->queryBuilder->where('grad',$grad);
  203. }
  204. // 货主
  205. public function owner($owner)
  206. {
  207. $this->searchWay($this->queryBuilder,$owner,'work_orders.owner_id');
  208. }
  209. public function client_code($client_code)
  210. {
  211. $this->searchWay($this->getOrderQuery(),$client_code,'orders.client_code');
  212. }
  213. }