2025-12-30 16:14:15 +08:00
|
|
|
|
package dao
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
2026-02-24 15:42:36 +08:00
|
|
|
|
"gitea.com/red-future/common/beans"
|
2026-01-16 16:55:32 +08:00
|
|
|
|
"strings"
|
2025-12-30 16:14:15 +08:00
|
|
|
|
"time"
|
|
|
|
|
|
|
2026-02-24 15:42:36 +08:00
|
|
|
|
"gitea.com/red-future/common/log/consts"
|
|
|
|
|
|
"gitea.com/red-future/common/log/model/dto"
|
|
|
|
|
|
"gitea.com/red-future/common/log/model/entity"
|
|
|
|
|
|
"gitea.com/red-future/common/mongo"
|
2025-12-30 16:14:15 +08:00
|
|
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type log struct{}
|
|
|
|
|
|
|
|
|
|
|
|
// Log 日志数据访问对象
|
|
|
|
|
|
var Log = &log{}
|
|
|
|
|
|
|
|
|
|
|
|
// Create 创建日志记录
|
|
|
|
|
|
func (d *log) Create(ctx context.Context, log *entity.OperationLog) error {
|
|
|
|
|
|
_, err := mongo.DB().Insert(ctx, []interface{}{log}, consts.OperationLogCollection)
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// List 查询日志列表(通用方法,通过filter动态拼接查询条件)
|
2026-01-16 16:55:32 +08:00
|
|
|
|
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},
|
2025-12-30 16:14:15 +08:00
|
|
|
|
}
|
2026-01-16 16:55:32 +08:00
|
|
|
|
total, err = mongo.DB().Find(ctx, filter, &res, consts.OperationLogCollection, req.Page, req.OrderBy)
|
|
|
|
|
|
return
|
2025-12-30 16:14:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// buildFilter 构建MongoDB查询过滤器
|
|
|
|
|
|
func buildFilter(filter interface{}) bson.M {
|
|
|
|
|
|
bsonFilter := make(bson.M)
|
|
|
|
|
|
|
|
|
|
|
|
// 从ListLogsReq结构体中提取字段值
|
|
|
|
|
|
if req, ok := filter.(*dto.ListLogsReq); ok {
|
2025-12-30 18:18:56 +08:00
|
|
|
|
if req.ServiceName != "" {
|
|
|
|
|
|
bsonFilter["service_name"] = req.ServiceName
|
2025-12-30 16:14:15 +08:00
|
|
|
|
}
|
2025-12-30 18:18:56 +08:00
|
|
|
|
if req.Collection != "" {
|
|
|
|
|
|
bsonFilter["collection"] = req.Collection
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.CollectionID != "" {
|
2026-01-16 16:55:32 +08:00
|
|
|
|
bsonFilter["collection_id"] = bson.M{"$in": strings.Split(req.CollectionID, ",")}
|
2025-12-30 16:14:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
if req.Operation != "" {
|
|
|
|
|
|
bsonFilter["operation"] = req.Operation
|
|
|
|
|
|
}
|
|
|
|
|
|
// 处理时间范围字段
|
|
|
|
|
|
if req.StartTime != "" || req.EndTime != "" {
|
|
|
|
|
|
timeFilter := bson.M{}
|
|
|
|
|
|
if req.StartTime != "" {
|
|
|
|
|
|
if startTime, err := time.Parse("2006-01-02 15:04:05", req.StartTime); err == nil {
|
|
|
|
|
|
timeFilter["$gte"] = startTime
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.EndTime != "" {
|
|
|
|
|
|
if endTime, err := time.Parse("2006-01-02 15:04:05", req.EndTime); err == nil {
|
|
|
|
|
|
timeFilter["$lte"] = endTime
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(timeFilter) > 0 {
|
|
|
|
|
|
bsonFilter["createdAt"] = timeFilter
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return bsonFilter
|
|
|
|
|
|
}
|