LogFilters.php 3.8 KB

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