WorkOrderFilters.php 7.0 KB

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