123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- package admin
- import (
- "context"
- v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
- adminApi "github.com/go-nunu/nunu-layout-advanced/api/v1/admin"
- "github.com/go-nunu/nunu-layout-advanced/internal/model"
- "github.com/go-nunu/nunu-layout-advanced/internal/repository"
- "math"
- "strings"
- )
- type WafManageRepository interface {
- GetWafManageList(ctx context.Context,req adminApi.WafManageList) (*v1.PaginatedResponse[adminApi.WafManageListRes], error)
- }
- func NewWafManageRepository(
- repository *repository.Repository,
- ) WafManageRepository {
- return &wafManageRepository{
- Repository: repository,
- }
- }
- type wafManageRepository struct {
- *repository.Repository
- }
- func (r *wafManageRepository) GetWafManageList(ctx context.Context,req adminApi.WafManageList) (*v1.PaginatedResponse[adminApi.WafManageListRes], error) {
- var res []adminApi.WafManageListRes
- var total int64
- query := r.DB(ctx).Model(&model.GlobalLimit{}).Table("shd_waf as waf")
- query = query.Joins("left join shd_clients as user on user.id = waf.uid")
- query = query.Joins("left join shd_host as host on host.id = waf.host_id")
- query = query.Where("waf.state = ?", 1)
- if req.Id > 0 {
- query = query.Where("waf.uid = ?", req.Id)
- }
- if req.Name != "" {
- trimmedName := strings.TrimSpace(req.Name)
- // 使用 LIKE 进行模糊匹配
- query = query.Where("waf.name LIKE CONCAT('%', ?, '%')", trimmedName)
- }
- if req.HostId > 0 {
- query = query.Where("waf.host_id = ?", req.HostId)
- }
- if req.Username != "" {
- trimmedName := strings.TrimSpace(req.Username)
- // 使用 LIKE 进行模糊匹配
- query = query.Where("user.username LIKE CONCAT('%', ?, '%')", trimmedName)
- }
- if !req.ExpiredAt.IsZero() {
- // 使用 LIKE 进行模糊匹配
- query = query.Where("waf.expired_at <= ?", req.ExpiredAt)
- }
- if req.Column != "" && req.Order != "" {
- query = query.Order(req.Column + " " + req.Order)
- }
- if err := query.Count(&total).Error; err != nil {
- return nil, err
- }
- page := req.Current
- pageSize := req.PageSize
- if page <= 0 {
- page = 1
- }
- if pageSize <= 0 {
- pageSize = 10
- } else if pageSize > 100 {
- pageSize = 100
- }
- offset := (page - 1) * pageSize
- result := query.Offset(offset).Limit(pageSize).Find(&res)
- if result.Error != nil {
- return nil, result.Error
- }
- return &v1.PaginatedResponse[adminApi.WafManageListRes]{
- Records: res,
- Page: page,
- PageSize: pageSize,
- Total: total,
- TotalPages: int(math.Ceil(float64(total) / float64(pageSize))),
- }, nil
- }
|