优化日志模块:重构操作日志记录与查询功能,支持批量操作日志记录,完善日志查询接口,增加软删除操作类型

This commit is contained in:
2026-01-16 16:55:32 +08:00
committed by 张斌
parent cc940c27b7
commit f0e6bdd37c
8 changed files with 260 additions and 300 deletions

View File

@@ -2,6 +2,8 @@ package dao
import (
"context"
"gitee.com/red-future---jilin-g/common/beans"
"strings"
"time"
"gitee.com/red-future---jilin-g/common/log/consts"
@@ -9,7 +11,6 @@ import (
"gitee.com/red-future---jilin-g/common/log/model/entity"
"gitee.com/red-future---jilin-g/common/mongo"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo/options"
)
type log struct{}
@@ -23,75 +24,14 @@ func (d *log) Create(ctx context.Context, log *entity.OperationLog) error {
return err
}
// CreateBatch 批量创建日志记录
func (d *log) CreateBatch(ctx context.Context, logs []*entity.OperationLog) error {
if len(logs) == 0 {
return nil
}
documents := make([]interface{}, len(logs))
for i, log := range logs {
documents[i] = log
}
_, err := mongo.DB().Insert(ctx, documents, consts.OperationLogCollection)
return err
}
// GetByID 根据ID获取日志
func (d *log) GetByID(ctx context.Context, id string) (*entity.OperationLog, error) {
objectID, err := bson.ObjectIDFromHex(id)
if err != nil {
return nil, err
}
filter := bson.M{"_id": objectID}
var log entity.OperationLog
err = mongo.DB().FindOne(ctx, filter, &log, consts.OperationLogCollection)
if err != nil {
return nil, err
}
return &log, nil
}
// List 查询日志列表通用方法通过filter动态拼接查询条件
func (d *log) List(ctx context.Context, filter *dto.ListLogsReq, sortFields ...string) ([]*entity.OperationLog, int64, error) {
bsonFilter := buildFilter(filter)
total, err := mongo.DB().Count(ctx, bsonFilter, consts.OperationLogCollection)
if err != nil {
return nil, 0, err
func (d *log) List(ctx context.Context, req *dto.ListLogsReq) (res []*entity.OperationLog, total int64, err error) {
filter := buildFilter(req)
req.OrderBy = []beans.OrderBy{
{Field: "createdAt", Order: beans.Desc},
}
var findOptions []options.Lister[options.FindOptions]
if filter.PageNum > 0 && filter.PageSize > 0 {
findOptions = append(findOptions, options.Find().SetSkip(int64((filter.PageNum-1)*filter.PageSize)).SetLimit(int64(filter.PageSize)))
}
if len(sortFields) > 0 {
sort := bson.D{}
for _, field := range sortFields {
var order int
if len(field) > 0 && field[0] == '-' {
order = -1
field = field[1:]
} else {
order = 1
}
sort = append(sort, bson.E{Key: field, Value: order})
}
findOptions = append(findOptions, options.Find().SetSort(sort))
} else {
findOptions = append(findOptions, options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}}))
}
var logs []*entity.OperationLog
_, err = mongo.DB().Find(ctx, bsonFilter, &logs, consts.OperationLogCollection, nil, nil)
if err != nil {
return nil, 0, err
}
return logs, total, nil
total, err = mongo.DB().Find(ctx, filter, &res, consts.OperationLogCollection, req.Page, req.OrderBy)
return
}
// buildFilter 构建MongoDB查询过滤器
@@ -107,12 +47,11 @@ func buildFilter(filter interface{}) bson.M {
bsonFilter["collection"] = req.Collection
}
if req.CollectionID != "" {
bsonFilter["collection_id"] = req.CollectionID
bsonFilter["collection_id"] = bson.M{"$in": strings.Split(req.CollectionID, ",")}
}
if req.Operation != "" {
bsonFilter["operation"] = req.Operation
}
// 处理时间范围字段
if req.StartTime != "" || req.EndTime != "" {
timeFilter := bson.M{}