LogFilters.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace App\Filters;
  3. use App\User;
  4. use Carbon\Carbon;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Arr;
  7. class LogFilters
  8. {
  9. protected $request;
  10. protected $queryBuilder;
  11. protected $filters = ['username', 'type', 'description',
  12. 'created_at_start', 'created_at_end', 'is_exception'];
  13. protected $array_filter;
  14. public function __construct(Request $request)
  15. {
  16. $this->request = $request;
  17. $this->array_filter = array_filter($this->request->only($this->filters));
  18. }
  19. public function apply($builder)
  20. {
  21. $this->queryBuilder = $builder;
  22. foreach ($this->array_filter as $filter => $value) {
  23. if (method_exists($this, $filter)) {
  24. $this->$filter($value, $this->queryBuilder);
  25. }
  26. }
  27. return $this->afterApply($this->queryBuilder);
  28. }
  29. /**
  30. * 后置处理,解决查询条件之间的关联关系,方法名后缀’_after‘
  31. * @param $builder
  32. * @return mixed
  33. */
  34. public function afterApply($builder)
  35. {
  36. $this->queryBuilder = $builder;
  37. foreach ($this->array_filter as $filter => $value) {
  38. $filter .= '_after';
  39. if (method_exists($this, $filter)) {
  40. $this->$filter($value, $this->queryBuilder);
  41. }
  42. }
  43. if (!Arr::hasAny($this->array_filter, ['created_at_start', 'created_at_end'])) {
  44. $start = Carbon::now()->toDateString();
  45. $end = Carbon::tomorrow()->toDateString();
  46. $this->queryBuilder = $this->queryBuilder->where('created_at', '>=', $start)->where('created_at', '<', $end);
  47. }
  48. return $this->queryBuilder;
  49. }
  50. private function created_at_start($created_at_start)
  51. {
  52. $this->queryBuilder->where('created_at', '>=', $created_at_start);
  53. }
  54. private function created_at_end($created_at_end)
  55. {
  56. $this->queryBuilder->where('created_at', '<=', $created_at_end);
  57. }
  58. /**
  59. * 如果中传入开始时间,默认结束时间为开始时间的下一天
  60. * @param $created_at_start
  61. */
  62. private function created_at_start_after($created_at_start)
  63. {
  64. if (!Arr::has($this->array_filter, 'created_at_end')) {
  65. $endDay = Carbon::parse($created_at_start)->addDay()->toDateString();
  66. $this->queryBuilder->where('created_at', '<', $endDay);
  67. }
  68. }
  69. /**
  70. * 如果中传入截止时间,默认开始时间为开始时间的前一天
  71. * @param $created_at_end
  72. */
  73. private function created_at_end_after($created_at_end)
  74. {
  75. if (!Arr::has($this->array_filter, 'created_at_start')) {
  76. $startDay = Carbon::parse($created_at_end)->subDay()->toDateString();
  77. $this->queryBuilder->where('created_at', '>=', $startDay);
  78. }
  79. }
  80. private function description($description)
  81. {
  82. $this->queryBuilder->where("description", 'like', $description . '%');
  83. }
  84. private function type($type)
  85. {
  86. $types = array_filter(preg_split('/[,, ]+/is', $type));
  87. $this->queryBuilder->whereIn("type", $types);
  88. }
  89. private function username($username)
  90. {
  91. $this->queryBuilder->whereIn("id_user", function ($query) use ($username) {
  92. return $query->from("users")->select("id")->where("name", 'like', $username . '%');
  93. });
  94. }
  95. private function is_exception()
  96. {
  97. $this->queryBuilder->where(function ($query) {
  98. $query->where("method", "like", "ERROR%")->orWhere("method", "like", "EXCEPTION%");
  99. });
  100. }
  101. }