WorkOrderFilters.php 7.2 KB

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