OwnerMaterialFilters.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Filters;
  3. use App\Material;
  4. use App\User;
  5. use Illuminate\Database\Eloquent\Builder;
  6. use Illuminate\Http\Request;
  7. class OwnerMaterialFilters
  8. {
  9. protected $request;
  10. protected $queryBuilder;
  11. protected $materialQuery;
  12. protected $userQuery;
  13. protected $filters = [];
  14. protected $array_filter;
  15. protected $params = [
  16. 'owner_id','material_code','user_name','created_at_start','created_at_end','special'
  17. ];
  18. public function __construct(Request $request)
  19. {
  20. $this->request = $request;
  21. }
  22. public function apply($builder)
  23. {
  24. $this->queryBuilder = $builder;
  25. $filters = array_filter($this->request->only($this->filters));
  26. foreach ($filters as $filter => $value) {
  27. if (method_exists($this, $filter)) {
  28. $this->$filter($value, $this->queryBuilder);
  29. }
  30. }
  31. $this->afterApply();
  32. return $this->queryBuilder;
  33. }
  34. private function isSearchLike($str):bool
  35. {
  36. if (substr($str, 0, 1) == "%" || substr($str, strlen($str) - 1, 1) == "%") {
  37. return true;
  38. }
  39. return false;
  40. }
  41. private function searchWay($query, $param, $column)
  42. {
  43. if ($this->isSearchLike($param)) {
  44. $query->where($column, 'like', $param);
  45. } else {
  46. $query->whereIn($column, array_filter(preg_split('/[,, ]+/is', $param)));
  47. }
  48. return $query;
  49. }
  50. private function getMaterialQuery():Builder
  51. {
  52. if(!$this->materialQuery)
  53. $this->materialQuery = Material::query()->selectRaw('id');
  54. return $this->materialQuery;
  55. }
  56. private function getUserQuery():Builder
  57. {
  58. if(!$this->userQuery)
  59. $this->userQuery = User::query()->selectRaw('id');
  60. return $this->userQuery;
  61. }
  62. public function owner_Id($owner_id)
  63. {
  64. $this->searchWay($this->queryBuilder,$owner_id,'owner_id');
  65. }
  66. public function material_code($material_code)
  67. {
  68. $this->getMaterialQuery()->where('code','like',"{$material_code}%");
  69. }
  70. public function user_name($user_name)
  71. {
  72. $this->getUserQuery()->where('name','like',"{$user_name}%");
  73. }
  74. public function created_at_start($created_at)
  75. {
  76. $this->queryBuilder->where('created_at','>=',"{$created_at} 00:00:00");
  77. }
  78. public function created_at_end($created_at)
  79. {
  80. $this->queryBuilder->where('created_at','<=',"{$created_at} 23:59:59");
  81. }
  82. public function special($special)
  83. {
  84. $this->queryBuilder->where('special','like',"{$special}%");
  85. }
  86. public function afterApply()
  87. {
  88. if($this->userQuery)
  89. $this->queryBuilder->whereIn('initiator',$this->userQuery);
  90. if($this->materialQuery)
  91. $this->queryBuilder->whereIn('material_id',$this->userQuery);
  92. }
  93. }