waflog.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package admin
  2. import (
  3. "context"
  4. v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
  5. adminApi "github.com/go-nunu/nunu-layout-advanced/api/v1/admin"
  6. "github.com/go-nunu/nunu-layout-advanced/internal/model"
  7. "github.com/go-nunu/nunu-layout-advanced/internal/repository"
  8. "math"
  9. "strings"
  10. )
  11. type WafLogRepository interface {
  12. GetWafLog(ctx context.Context, id int64) (*model.WafLog, error)
  13. GetWafLogList(ctx context.Context, req adminApi.SearchWafLogParams) (*v1.PaginatedResponse[model.WafLog], error)
  14. AddWafLog(ctx context.Context, log *model.WafLog) error
  15. BatchAddWafLog(ctx context.Context, logs []*model.WafLog) error
  16. }
  17. func NewWafLogRepository(
  18. repository *repository.Repository,
  19. ) WafLogRepository {
  20. return &wafLogRepository{
  21. Repository: repository,
  22. }
  23. }
  24. type wafLogRepository struct {
  25. *repository.Repository
  26. }
  27. func (r *wafLogRepository) GetWafLog(ctx context.Context, id int64) (*model.WafLog, error) {
  28. var res model.WafLog
  29. return &res, r.DBWithName(ctx,"admin").Where("id = ?", id).First(&res).Error
  30. }
  31. func (r *wafLogRepository) GetWafLogList(ctx context.Context, req adminApi.SearchWafLogParams) (*v1.PaginatedResponse[model.WafLog], error) {
  32. var res []model.WafLog
  33. var total int64
  34. query := r.DBWithName(ctx,"admin").Model(&model.WafLog{})
  35. if req.RequestIp != "" {
  36. trimmedName := strings.TrimSpace(req.RequestIp)
  37. // 使用 LIKE 进行模糊匹配
  38. query = query.Where("request_ip LIKE CONCAT('%', ?, '%')", trimmedName)
  39. }
  40. if req.Uid != 0 {
  41. query = query.Where("uid = ?", req.Uid)
  42. }
  43. if req.Api != "" {
  44. trimmedName := strings.TrimSpace(req.Api)
  45. // 使用 LIKE 进行模糊匹配
  46. query = query.Where("api LIKE CONCAT('%', ?, '%')", trimmedName)
  47. }
  48. if req.Name != "" {
  49. trimmedName := strings.TrimSpace(req.Name)
  50. // 使用 LIKE 进行模糊匹配
  51. query = query.Where("name LIKE CONCAT('%', ?, '%')", trimmedName)
  52. }
  53. if req.RuleId != 0 {
  54. query = query.Where("rule_id = ?", req.RuleId)
  55. }
  56. if req.HostId != 0 {
  57. query = query.Where("host_id = ?", req.HostId)
  58. }
  59. if req.Api != "" {
  60. trimmedName := strings.TrimSpace(req.Api)
  61. // 使用 LIKE 进行模糊匹配
  62. query = query.Where("api LIKE CONCAT('%', ?, '%')", trimmedName)
  63. }
  64. if req.UserAgent != "" {
  65. trimmedName := strings.TrimSpace(req.UserAgent)
  66. // 使用 LIKE 进行模糊匹配
  67. query = query.Where("user_agent LIKE CONCAT('%', ?, '%')", trimmedName)
  68. }
  69. if req.ApiName != "" {
  70. trimmedName := strings.TrimSpace(req.ApiName)
  71. // 使用 LIKE 进行模糊匹配
  72. query = query.Where("api_name LIKE CONCAT('%', ?, '%')", trimmedName)
  73. }
  74. if req.ApiType != "" {
  75. query = query.Where("api_type = ?", req.ApiType)
  76. }
  77. if req.Column != "" {
  78. query = query.Order(req.Column + " " + req.Order)
  79. }
  80. if err := query.Count(&total).Error; err != nil {
  81. // 如果连计数都失败了,直接返回错误
  82. return nil, err
  83. }
  84. page := req.Current
  85. pageSize := req.PageSize
  86. if page <= 0 {
  87. page = 1
  88. }
  89. if pageSize <= 0 {
  90. pageSize = 10 // 默认每页 10 条
  91. } else if pageSize > 100 {
  92. pageSize = 100 // 每页最多 100 条
  93. }
  94. // 计算 offset (偏移量)
  95. // 例如,第 1 页,offset = (1-1)*10 = 0 (从第0条开始)
  96. // 第 2 页,offset = (2-1)*10 = 10 (从第10条开始)
  97. offset := (page - 1) * pageSize
  98. // 3. 执行最终的查询
  99. // 在所有条件都添加完毕后,再执行 .Find()
  100. result := query.Offset(offset).Limit(pageSize).Find(&res)
  101. if result.Error != nil {
  102. // 这里的错误可能是数据库连接问题等,而不是“未找到记录”
  103. return nil, result.Error
  104. }
  105. return &v1.PaginatedResponse[model.WafLog]{
  106. Records: res,
  107. Page: page,
  108. PageSize: pageSize,
  109. Total: total,
  110. TotalPages: int(math.Ceil(float64(total) / float64(pageSize))),
  111. }, nil
  112. }
  113. func (r *wafLogRepository) AddWafLog(ctx context.Context, log *model.WafLog) error {
  114. return r.DBWithName(ctx,"admin").Create(log).Error
  115. }
  116. func (r *wafLogRepository) BatchAddWafLog(ctx context.Context, logs []*model.WafLog) error {
  117. if len(logs) == 0 {
  118. return nil
  119. }
  120. return r.DBWithName(ctx, "admin").CreateInBatches(logs, len(logs)).Error
  121. }