123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- 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"
- "time"
- )
- type WafLogRepository interface {
- GetWafLog(ctx context.Context, id int64) (*model.WafLog, error)
- GetWafLogList(ctx context.Context, req adminApi.SearchWafLogParams) (*v1.PaginatedResponse[model.WafLog], error)
- AddWafLog(ctx context.Context, log *model.WafLog) error
- BatchAddWafLog(ctx context.Context, logs []*model.WafLog) error
- // 导出日志
- ExportWafLog(ctx context.Context, req adminApi.ExportWafLog) ([]model.WafLog, error)
- // 获取网关组记录
- GetWafLogGateWayIp(ctx context.Context, hostId int64, Uid int64,createdAt time.Time) (model.WafLog, error)
- }
- func NewWafLogRepository(
- repository *repository.Repository,
- ) WafLogRepository {
- return &wafLogRepository{
- Repository: repository,
- }
- }
- type wafLogRepository struct {
- *repository.Repository
- }
- func (r *wafLogRepository) GetWafLog(ctx context.Context, id int64) (*model.WafLog, error) {
- var res model.WafLog
- return &res, r.DBWithName(ctx,"admin").Where("id = ?", id).First(&res).Error
- }
- func (r *wafLogRepository) GetWafLogList(ctx context.Context, req adminApi.SearchWafLogParams) (*v1.PaginatedResponse[model.WafLog], error) {
- var res []model.WafLog
- var total int64
- query := r.DBWithName(ctx,"admin").Model(&model.WafLog{})
- if req.RequestIp != "" {
- trimmedName := strings.TrimSpace(req.RequestIp)
- // 使用 LIKE 进行模糊匹配
- query = query.Where("request_ip LIKE CONCAT('%', ?, '%')", trimmedName)
- }
- if req.Uid != 0 {
- query = query.Where("uid = ?", req.Uid)
- }
- if req.Api != "" {
- trimmedName := strings.TrimSpace(req.Api)
- // 使用 LIKE 进行模糊匹配
- query = query.Where("api LIKE CONCAT('%', ?, '%')", trimmedName)
- }
- if req.Name != "" {
- trimmedName := strings.TrimSpace(req.Name)
- // 使用 LIKE 进行模糊匹配
- query = query.Where("name LIKE CONCAT('%', ?, '%')", trimmedName)
- }
- if req.RuleId != 0 {
- query = query.Where("rule_id = ?", req.RuleId)
- }
- if req.HostId != 0 {
- query = query.Where("host_id = ?", req.HostId)
- }
- if req.Api != "" {
- trimmedName := strings.TrimSpace(req.Api)
- // 使用 LIKE 进行模糊匹配
- query = query.Where("api LIKE CONCAT('%', ?, '%')", trimmedName)
- }
- if req.UserAgent != "" {
- trimmedName := strings.TrimSpace(req.UserAgent)
- // 使用 LIKE 进行模糊匹配
- query = query.Where("user_agent LIKE CONCAT('%', ?, '%')", trimmedName)
- }
- if req.ApiName != "" {
- trimmedName := strings.TrimSpace(req.ApiName)
- // 使用 LIKE 进行模糊匹配
- query = query.Where("api_name LIKE CONCAT('%', ?, '%')", trimmedName)
- }
- if req.ApiType != "" {
- query = query.Where("api_type = ?", req.ApiType)
- }
- if req.Column != "" {
- 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 // 默认每页 10 条
- } else if pageSize > 100 {
- pageSize = 100 // 每页最多 100 条
- }
- // 计算 offset (偏移量)
- // 例如,第 1 页,offset = (1-1)*10 = 0 (从第0条开始)
- // 第 2 页,offset = (2-1)*10 = 10 (从第10条开始)
- offset := (page - 1) * pageSize
- // 3. 执行最终的查询
- // 在所有条件都添加完毕后,再执行 .Find()
- result := query.Offset(offset).Limit(pageSize).Find(&res)
- if result.Error != nil {
- // 这里的错误可能是数据库连接问题等,而不是“未找到记录”
- return nil, result.Error
- }
- return &v1.PaginatedResponse[model.WafLog]{
- Records: res,
- Page: page,
- PageSize: pageSize,
- Total: total,
- TotalPages: int(math.Ceil(float64(total) / float64(pageSize))),
- }, nil
- }
- func (r *wafLogRepository) AddWafLog(ctx context.Context, log *model.WafLog) error {
- return r.DBWithName(ctx,"admin").Create(log).Error
- }
- func (r *wafLogRepository) BatchAddWafLog(ctx context.Context, logs []*model.WafLog) error {
- if len(logs) == 0 {
- return nil
- }
- return r.DBWithName(ctx, "admin").CreateInBatches(logs, len(logs)).Error
- }
- func (r *wafLogRepository) ExportWafLog(ctx context.Context, req adminApi.ExportWafLog) ([]model.WafLog, error) {
- var res []model.WafLog
- query := r.DBWithName(ctx,"admin").Model(&model.WafLog{})
- if req.RequestIp != "" {
- trimmedName := strings.TrimSpace(req.RequestIp)
- // 使用 LIKE 进行模糊匹配
- query = query.Where("request_ip = ?", trimmedName)
- }
- if req.Uid != 0 {
- query = query.Where("uid = ?", req.Uid)
- }
- if req.Api != "" {
- trimmedName := strings.TrimSpace(req.Api)
- // 使用 LIKE 进行模糊匹配
- query = query.Where("api = ?", trimmedName)
- }
- if req.Name != "" {
- trimmedName := strings.TrimSpace(req.Name)
- // 使用 LIKE 进行模糊匹配
- query = query.Where("name = ?", trimmedName)
- }
- if req.RuleId != 0 {
- query = query.Where("rule_id = ?", req.RuleId)
- }
- if len(req.HostIds) > 0 {
- query = query.Where("host_id IN ?", req.HostIds)
- }
- if req.Api != "" {
- trimmedName := strings.TrimSpace(req.Api)
- // 使用 LIKE 进行模糊匹配
- query = query.Where("api = ?", trimmedName)
- }
- if req.UserAgent != "" {
- trimmedName := strings.TrimSpace(req.UserAgent)
- // 使用 LIKE 进行模糊匹配
- query = query.Where("user_agent = ?", trimmedName)
- }
- if len(req.ApiNames) > 0 {
- trimmedNames := make([]string, len(req.ApiNames))
- for _, apiName := range req.ApiNames {
- trimmedNames = append(trimmedNames, strings.TrimSpace(apiName))
- }
- // 使用 LIKE 进行模糊匹配
- query = query.Where("api_name IN ?", trimmedNames)
- }
- if len(req.ApiTypes) > 0 {
- query = query.Where("api_type IN ?", req.ApiTypes)
- }
- if req.StartTime != "" {
- trimmedName := strings.TrimSpace(req.StartTime)
- // 使用 LIKE 进行模糊匹配
- query = query.Where("create_at > ?", trimmedName)
- }
- if req.EndTime != "" {
- trimmedName := strings.TrimSpace(req.EndTime)
- // 使用 LIKE 进行模糊匹配
- query = query.Where("create_at < ?", trimmedName)
- }
- result := query.Find(&res)
- if result.Error != nil {
- return nil, result.Error
- }
- return res, nil
- }
- func (r *wafLogRepository) GetWafLogGateWayIp(ctx context.Context, hostId int64, Uid int64,createdAt time.Time) (model.WafLog, error) {
- var res model.WafLog
- return res, r.DBWithName(ctx,"admin").Where("host_id = ? and uid = ? and api_name = ? and created_at > ? ", hostId, Uid, "分配网关组", createdAt).First(&res).Error
- }
|