wafmanage.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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/repository"
  7. "math"
  8. "strings"
  9. )
  10. type WafManageRepository interface {
  11. GetWafManageList(ctx context.Context,req adminApi.WafManageList) (*v1.PaginatedResponse[adminApi.WafManageListRes], error)
  12. }
  13. func NewWafManageRepository(
  14. repository *repository.Repository,
  15. ) WafManageRepository {
  16. return &wafManageRepository{
  17. Repository: repository,
  18. }
  19. }
  20. type wafManageRepository struct {
  21. *repository.Repository
  22. }
  23. func (r *wafManageRepository) GetWafManageList(ctx context.Context,req adminApi.WafManageList) (*v1.PaginatedResponse[adminApi.WafManageListRes], error) {
  24. var res []adminApi.WafManageListRes
  25. var total int64
  26. query := r.DB(ctx).Table("shd_waf as waf")
  27. query = query.Joins("left join shd_clients as user on user.id = waf.uid")
  28. query = query.Joins("left join shd_host as host on host.id = waf.host_id")
  29. query = query.Where("waf.state = ?", 1)
  30. if req.Id > 0 {
  31. query = query.Where("waf.uid = ?", req.Id)
  32. }
  33. if req.Name != "" {
  34. trimmedName := strings.TrimSpace(req.Name)
  35. // 使用 LIKE 进行模糊匹配
  36. query = query.Where("waf.name LIKE CONCAT('%', ?, '%')", trimmedName)
  37. }
  38. if req.HostId > 0 {
  39. query = query.Where("waf.host_id = ?", req.HostId)
  40. }
  41. if req.Uid > 0 {
  42. query = query.Where("waf.uid = ?", req.Uid)
  43. }
  44. if req.Username != "" {
  45. trimmedName := strings.TrimSpace(req.Username)
  46. // 使用 LIKE 进行模糊匹配
  47. query = query.Where("user.username LIKE CONCAT('%', ?, '%')", trimmedName)
  48. }
  49. if !req.ExpiredAt.IsZero() {
  50. // 使用 LIKE 进行模糊匹配
  51. query = query.Where("waf.expired_at <= ?", req.ExpiredAt)
  52. }
  53. if req.Column != "" && req.Order != "" {
  54. query = query.Order(req.Column + " " + req.Order)
  55. }
  56. if err := query.Count(&total).Error; err != nil {
  57. return nil, err
  58. }
  59. page := req.Current
  60. pageSize := req.PageSize
  61. if page <= 0 {
  62. page = 1
  63. }
  64. if pageSize <= 0 {
  65. pageSize = 10
  66. } else if pageSize > 100 {
  67. pageSize = 100
  68. }
  69. offset := (page - 1) * pageSize
  70. result := query.Select("waf.id, waf.host_id, waf.uid, waf.name, waf.expired_at, host.nextduedate as nextduedate, user.username as username").Offset(offset).Limit(pageSize).Find(&res)
  71. if result.Error != nil {
  72. return nil, result.Error
  73. }
  74. return &v1.PaginatedResponse[adminApi.WafManageListRes]{
  75. Records: res,
  76. Page: page,
  77. PageSize: pageSize,
  78. Total: total,
  79. TotalPages: int(math.Ceil(float64(total) / float64(pageSize))),
  80. }, nil
  81. }