WorkOrderFilters.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. // $user = Auth::user();
  68. // $logistic_ids = App('LogisticService')->getPermittingLogisticIds($user);
  69. // $owner_id = app('UserService')->getPermittingOwnerIds($user);
  70. //
  71. // $this->getOrderQuery()->where(function($query)use ($owner_id,$logistic_ids){
  72. // $query->whereIn('owner_id',$owner_id)->orWhereIn('logistic_id',$logistic_ids);
  73. // });
  74. $this->afterFilter();
  75. }
  76. private function afterFilter(){
  77. $orderQuery = $this->getOrderQuery();
  78. $user = Auth::user();
  79. $logistic_ids = App('UserService')->getPermittingLogisticIds($user);
  80. $owner_ids = app('UserService')->getPermittingOwnerIds($user);
  81. $this->afterFilterLogistic($orderQuery,$owner_ids,$logistic_ids);
  82. $this->afterFilterOwner($orderQuery,$owner_ids);
  83. }
  84. // 可见货主过滤
  85. private function afterFilterOwner($orderQuery,$owner_ids)
  86. {
  87. $orderQuery->whereIn('owner_id',$owner_ids);
  88. // if(Gate::allows('订单管理-工单处理-客服编辑') || Gate::allows('订单管理-工单处理-承运商编辑')){
  89. //
  90. // }else if(Gate::allows('订单管理-工单处理-货主编辑')){
  91. // $orderQuery->whereIn('owner_id',$owner_ids);
  92. // }
  93. }
  94. // 可见承运商过滤
  95. private function afterFilterLogistic($orderQuery,$owner_ids,$logistic_ids)
  96. {
  97. if(Gate::allows('订单管理-工单处理-客服编辑') || Gate::allows('订单管理-工单处理-货主编辑')){
  98. if (!isset($this->params['owner'])){
  99. $this->getOrderQuery()->whereIn('owner_id', $owner_ids);
  100. }
  101. } else if (Gate::allows('订单管理-工单处理-承运商编辑')){
  102. $orderQuery->whereIn('logistic_id',array_values($logistic_ids));
  103. }
  104. }
  105. public function beforeApply()
  106. {
  107. if ($this->orderPackageQuery){
  108. $this->getOrderQuery()->whereIn('id',$this->orderPackageQuery);
  109. }
  110. if ($this->orderQuery) {
  111. $this->queryBuilder->whereIn('order_id',$this->getOrderQuery());
  112. }
  113. // 审核 默认为 待审核
  114. if (isset($this->params['is_review'])){
  115. $this->queryBuilder->where('status','2');
  116. $this->queryBuilder->whereHas('orderIssue')->orderBydesc('work_orders.created_at');
  117. } else {
  118. $work_order_query = WorkOrder::query()->select('id')->where('status','2')->whereHas('orderIssue');
  119. $this->queryBuilder->whereNotIn('id',$work_order_query);
  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 id($id)
  137. {
  138. if(is_array($id))$this->queryBuilder->whereIn('work_orders.id',$id);
  139. else $this->queryBuilder->where('work_orders.id',$id);
  140. }
  141. // 创建开始时间
  142. public function created_at_start($create_at_start)
  143. {
  144. $this->queryBuilder->where('work_orders.created_at','>=',$create_at_start);
  145. }
  146. // 创建结束时间
  147. public function created_at_end($created_at_end)
  148. {
  149. $this->queryBuilder->where('work_orders.created_at','<=',$created_at_end);
  150. }
  151. // 审核开始时间
  152. public function review_at_start($review_at_start)
  153. {
  154. $this->queryBuilder->where('work_orders.review_at','>=',$review_at_start);
  155. }
  156. // 审核结束时间
  157. public function review_at_end($review_at_end)
  158. {
  159. $this->queryBuilder->where('work_orders.review_at','<=',$review_at_end);
  160. }
  161. // 创建人
  162. public function creator($creator)
  163. {
  164. $userQuery = User::query()->select('id')->where('name','like',"%{$creator}%");;
  165. $this->queryBuilder->whereIn('creator_id',$userQuery);
  166. }
  167. // 审核人
  168. public function reviewer($id)
  169. {
  170. if (is_array($id))
  171. $this->queryBuilder->whereIn('work_orders.reviewer_id',$id);
  172. else
  173. $this->queryBuilder->where('work_orders.reviewer_id',$id);
  174. }
  175. // 类型
  176. public function word_order_types($word_order_types)
  177. {
  178. if(!$this->workOrderTypeQuery)
  179. $this->workOrderTypeQuery = WorkOrder::query()->select('id');
  180. if (is_array($word_order_types))
  181. $this->workOrderTypeQuery->whereIn('id',$word_order_types);
  182. else $this->workOrderTypeQuery->where('id',$word_order_types);
  183. }
  184. public function order_issue_type($order_issue_type)
  185. {
  186. $this->queryBuilder->where('order_issue_type_id',$order_issue_type);
  187. }
  188. // 快递单号
  189. public function logistic_number($logistic_number)
  190. {
  191. $this->searchWay($this->getOrderPackageQuery(),$logistic_number,'order_packages.logistic_number');
  192. }
  193. // 对应问题件
  194. public function is_issue_order($is_issue_order)
  195. {
  196. $orderIssueQuery = OrderIssue::query()->select('order_id')->whereIn('order_id',WorkOrder::query()->select('order_id'));
  197. if($is_issue_order == 'true'){
  198. $this->queryBuilder->whereIn('order_id',$orderIssueQuery);
  199. } else {
  200. $this->queryBuilder->whereNotIn('order_id',$orderIssueQuery);
  201. }
  202. }
  203. // 承运商筛选
  204. public function logistic($logistic)
  205. {
  206. $orderQuery = $this->getOrderQuery()->whereIn('id',WorkOrder::query()->select('order_id'));
  207. $this->searchWay($orderQuery,$logistic,'logistic_id');
  208. }
  209. public function grad($grad)
  210. {
  211. $this->queryBuilder->where('grad',$grad);
  212. }
  213. // 货主
  214. public function owner($owner)
  215. {
  216. $this->searchWay($this->queryBuilder,$owner,'work_orders.owner_id');
  217. }
  218. public function client_code($client_code)
  219. {
  220. $this->searchWay($this->getOrderQuery(),$client_code,'orders.client_code');
  221. }
  222. }