优化日志模块:重构操作日志记录与查询功能,支持批量操作日志记录,完善日志查询接口,增加软删除操作类型
This commit is contained in:
280
mongo/mongo.go
280
mongo/mongo.go
@@ -9,6 +9,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"gitee.com/red-future---jilin-g/common/log/consts"
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
@@ -62,9 +63,15 @@ var (
|
||||
manager = GetManager()
|
||||
logPool *grpool.Pool
|
||||
serverName string
|
||||
logRedisKey string
|
||||
LogRedisKey string
|
||||
)
|
||||
|
||||
// FieldInfo 定义字段信息结构体
|
||||
type FieldInfo struct {
|
||||
FieldName string
|
||||
FieldValue interface{}
|
||||
}
|
||||
|
||||
const PageSize = 20
|
||||
|
||||
// GetDB 获取默认数据源的数据库实例(向后兼容)
|
||||
@@ -88,6 +95,43 @@ func (m *MongoDB) getDataSource() (DataSource, error) {
|
||||
return manager.GetDataSource(m.dataSource)
|
||||
}
|
||||
|
||||
// Count 查询总数
|
||||
func (m *MongoDB) Count(ctx context.Context, filter bson.M, collection string) (count int64, err error) {
|
||||
source, err := m.getDataSource()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
db := source.Database()
|
||||
|
||||
user, err := utils.GetUserInfo(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
filter["isDeleted"] = false
|
||||
delete(filter, "tenantId")
|
||||
filterKey := fmt.Sprintf("%+v", filter)
|
||||
redisKey := fmt.Sprintf(redis.Count, user.TenantId, collection, filterKey)
|
||||
if m.Cache {
|
||||
var resultStr *gvar.Var
|
||||
resultStr, err = redis.RedisClient.Get(ctx, redisKey)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if !g.IsEmpty(resultStr) {
|
||||
count = gconv.Int64(resultStr)
|
||||
return
|
||||
}
|
||||
}
|
||||
count, err = db.Collection(collection).CountDocuments(ctx, filter)
|
||||
if m.Cache {
|
||||
err = redis.RedisClient.SetEX(ctx, redisKey, count, int64(time.Hour))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Find 查询多条记录
|
||||
func (m *MongoDB) Find(ctx context.Context, filter bson.M, result interface{}, collection string, page *beans.Page, orderBy []beans.OrderBy) (total int64, err error) {
|
||||
source, err := m.getDataSource()
|
||||
@@ -103,7 +147,9 @@ func (m *MongoDB) Find(ctx context.Context, filter bson.M, result interface{}, c
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
filter["isDeleted"] = false
|
||||
if g.IsEmpty(filter["isDeleted"]) {
|
||||
filter["isDeleted"] = false
|
||||
}
|
||||
filterKey := fmt.Sprintf("%+v", filter)
|
||||
optionsKey := fmt.Sprintf("%+v%+v", page, orderBy)
|
||||
redisKey := fmt.Sprintf(redis.List, user.TenantId, collection, filterKey, optionsKey)
|
||||
@@ -125,7 +171,7 @@ func (m *MongoDB) Find(ctx context.Context, filter bson.M, result interface{}, c
|
||||
|
||||
limit := int64(PageSize)
|
||||
skip := int64(0)
|
||||
if page != nil {
|
||||
if page != nil && !g.IsEmpty(page.PageNum) && !g.IsEmpty(page.PageSize) {
|
||||
limit = page.PageSize
|
||||
if limit == -1 {
|
||||
skip = 0
|
||||
@@ -229,6 +275,17 @@ func (m *MongoDB) FindOne(ctx context.Context, filter bson.M, result interface{}
|
||||
return
|
||||
}
|
||||
|
||||
// getDeletedData 获取要删除的数据
|
||||
func (m *MongoDB) getDeletedData(ctx context.Context, filter bson.M, collection string) (deletedIDs []bson.ObjectID, deletedData []bson.M, err error) {
|
||||
// 查询要删除的数据
|
||||
_, err = m.Find(ctx, filter, &deletedData, collection, nil, nil)
|
||||
// 从查询结果中获取 _id
|
||||
for _, doc := range deletedData {
|
||||
deletedIDs = append(deletedIDs, doc["_id"].(bson.ObjectID))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (m *MongoDB) CleanRedis(ctx context.Context, filter bson.M, tenantId interface{}, collection string) (err error) {
|
||||
listKeys := fmt.Sprintf(redis.CleanList, tenantId, collection)
|
||||
keys, err := redis.RedisClient.Keys(ctx, listKeys)
|
||||
@@ -263,27 +320,92 @@ func (m *MongoDB) CleanRedis(ctx context.Context, filter bson.M, tenantId interf
|
||||
return
|
||||
}
|
||||
|
||||
func (m *MongoDB) log(ctx context.Context, filter bson.M, collection string, data interface{}, userName, tenantId interface{}, operationType string) {
|
||||
_ = logPool.AddWithRecover(ctx, func(ctx context.Context) {
|
||||
log := &entity.OperationLog{
|
||||
ServiceName: serverName,
|
||||
Collection: collection,
|
||||
CollectionID: filter["_id"].(string),
|
||||
Operation: operationType,
|
||||
IPAddress: g.RequestFromCtx(ctx).GetClientIp(),
|
||||
Data: data,
|
||||
func (m *MongoDB) log(ctx context.Context, ids []bson.ObjectID, filter bson.M, collection string, data interface{}, userName, tenantId interface{}, operationType consts.OperationType) {
|
||||
// 提前获取 IP 地址,避免异步任务执行时请求已结束
|
||||
var ipAddress string
|
||||
if request := g.RequestFromCtx(ctx); request != nil {
|
||||
ipAddress = request.GetClientIp()
|
||||
}
|
||||
if operationType != consts.OperationInsert && operationType != consts.OperationDelete {
|
||||
if !g.IsEmpty(filter["_id"]) {
|
||||
objectID := filter["_id"].(*bson.ObjectID)
|
||||
ids = append(ids, *objectID)
|
||||
} else {
|
||||
var err error
|
||||
if ids, _, err = m.getDeletedData(ctx, filter, collection); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
log.Creator = userName
|
||||
now := >ime.Now().Time
|
||||
log.CreatedAt = now
|
||||
log.UpdatedAt = now
|
||||
log.TenantId = tenantId
|
||||
if _, err := redis.AddToStream(ctx, logRedisKey, log); err != nil {
|
||||
glog.Error(ctx, "mongoLog-AddToStream err: %v", err)
|
||||
}
|
||||
log := &entity.OperationLog{
|
||||
ServiceName: serverName,
|
||||
Collection: collection,
|
||||
CollectionID: ids,
|
||||
Operation: string(operationType),
|
||||
IPAddress: ipAddress,
|
||||
Data: data,
|
||||
}
|
||||
log.Creator = userName
|
||||
log.Updater = userName
|
||||
now := >ime.Now().Time
|
||||
log.CreatedAt = now
|
||||
log.UpdatedAt = now
|
||||
log.TenantId = tenantId
|
||||
// 使用新的 context 进行 Redis 操作
|
||||
if _, err := redis.AddToStream(ctx, LogRedisKey, log); err != nil {
|
||||
glog.Error(ctx, "mongoLog-AddToStream err: %v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Insert 插入多条记录
|
||||
func (m *MongoDB) Insert(ctx context.Context, documents []interface{}, collection string, opts ...options.Lister[options.InsertManyOptions]) (ids []interface{}, err error) {
|
||||
source, err := m.getDataSource()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
db := source.Database()
|
||||
|
||||
user, err := utils.GetUserInfo(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
docs := make([]interface{}, 0, len(documents))
|
||||
for _, document := range documents {
|
||||
doc := gconv.Map(document)
|
||||
delete(doc, "id")
|
||||
if !g.IsEmpty(user.UserName) && g.IsEmpty(doc["creator"]) {
|
||||
doc["creator"] = user.UserName
|
||||
}
|
||||
}, func(ctx context.Context, exception error) {
|
||||
glog.Error(ctx, "mongoLog-AddWithRecover err: %v", exception)
|
||||
})
|
||||
if !g.IsEmpty(user.UserName) && g.IsEmpty(doc["updater"]) {
|
||||
doc["updater"] = user.UserName
|
||||
}
|
||||
if !g.IsEmpty(user.TenantId) && g.IsEmpty(doc["tenantId"]) {
|
||||
doc["tenantId"] = user.TenantId
|
||||
}
|
||||
if g.IsEmpty(doc["createdAt"]) {
|
||||
doc["createdAt"] = gtime.Now().Time
|
||||
}
|
||||
if g.IsEmpty(doc["updatedAt"]) {
|
||||
doc["updatedAt"] = gtime.Now().Time
|
||||
}
|
||||
doc["isDeleted"] = false
|
||||
docs = append(docs, doc)
|
||||
}
|
||||
r, err := db.Collection(collection).InsertMany(ctx, docs, opts...)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
ids = r.InsertedIDs
|
||||
err = m.CleanRedis(ctx, bson.M{}, user.TenantId, collection)
|
||||
//写日志
|
||||
if collection != consts.OperationLogCollection {
|
||||
objectIds := make([]bson.ObjectID, 0)
|
||||
for _, id := range ids {
|
||||
objectIds = append(objectIds, id.(bson.ObjectID))
|
||||
}
|
||||
m.log(ctx, objectIds, nil, collection, nil, user.UserName, user.TenantId, consts.OperationInsert)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -304,15 +426,30 @@ func (m *MongoDB) Delete(ctx context.Context, filter bson.M, collection string,
|
||||
return
|
||||
}
|
||||
filter["tenantId"] = user.TenantId
|
||||
// 获取要删除的数据
|
||||
ds, ms, err := m.getDeletedData(ctx, filter, collection)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
// 执行删除操作
|
||||
r, err := db.Collection(collection).DeleteMany(ctx, filter, opts...)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
count = r.DeletedCount
|
||||
// 清理redis
|
||||
err = m.CleanRedis(ctx, filter, user.TenantId, collection)
|
||||
// 写日志
|
||||
m.log(ctx, ds, nil, collection, ms, user.UserName, user.TenantId, consts.OperationDelete)
|
||||
return
|
||||
}
|
||||
|
||||
// DeleteSoft 假删除记录
|
||||
func (m *MongoDB) DeleteSoft(ctx context.Context, filter bson.M, collection string, opts ...options.Lister[options.UpdateManyOptions]) (result *mongo.UpdateResult, err error) {
|
||||
update := bson.M{"$set": bson.M{"isDeleted": true}}
|
||||
return m.Update(ctx, filter, update, collection, opts...)
|
||||
}
|
||||
|
||||
// Update 修改记录
|
||||
func (m *MongoDB) Update(ctx context.Context, filter bson.M, update bson.M, collection string, opts ...options.Lister[options.UpdateManyOptions]) (result *mongo.UpdateResult, err error) {
|
||||
source, err := m.getDataSource()
|
||||
@@ -333,17 +470,38 @@ func (m *MongoDB) Update(ctx context.Context, filter bson.M, update bson.M, coll
|
||||
if !g.IsEmpty(user.TenantId) {
|
||||
filter["tenantId"] = user.TenantId
|
||||
}
|
||||
// 遍历 update 中的所有操作符和字段,存放到 list 中
|
||||
fieldList := make([]FieldInfo, 0)
|
||||
for _, doc := range update {
|
||||
if m, ok := doc.(bson.M); ok {
|
||||
for fieldName, fieldValue := range m {
|
||||
// 获取到字段名和字段值
|
||||
fieldList = append(fieldList, FieldInfo{
|
||||
FieldName: fieldName,
|
||||
FieldValue: fieldValue,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
setDoc := update["$set"].(bson.M)
|
||||
if !g.IsEmpty(user.UserName) {
|
||||
setDoc["updater"] = user.UserName
|
||||
}
|
||||
setDoc["updatedAt"] = gtime.Now().Time
|
||||
update = bson.M{"$set": setDoc}
|
||||
update["$set"] = setDoc
|
||||
result, err = db.Collection(collection).UpdateMany(ctx, filter, update, opts...)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
// 清理redis
|
||||
err = m.CleanRedis(ctx, filter, user.TenantId, collection)
|
||||
// 写日志
|
||||
if !g.IsEmpty(setDoc["isDeleted"]) && gconv.Bool(setDoc["isDeleted"]) {
|
||||
filter["isDeleted"] = true
|
||||
m.log(ctx, nil, filter, collection, nil, user.UserName, user.TenantId, consts.OperationDeleteSoft)
|
||||
} else {
|
||||
m.log(ctx, nil, filter, collection, fieldList, user.UserName, user.TenantId, consts.OperationUpdate)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -468,82 +626,6 @@ func (m *MongoDB) SaveOrUpdate(ctx context.Context, filter []bson.M, update []bs
|
||||
return bulkResult, nil
|
||||
}
|
||||
|
||||
// Insert 插入多条记录
|
||||
func (m *MongoDB) Insert(ctx context.Context, documents []interface{}, collection string, opts ...options.Lister[options.InsertManyOptions]) (ids []interface{}, err error) {
|
||||
source, err := m.getDataSource()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
db := source.Database()
|
||||
|
||||
user, err := utils.GetUserInfo(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
docs := make([]interface{}, 0, len(documents))
|
||||
for _, document := range documents {
|
||||
doc := gconv.Map(document)
|
||||
delete(doc, "id")
|
||||
if !g.IsEmpty(user.UserName) {
|
||||
doc["creator"] = user.UserName
|
||||
}
|
||||
if !g.IsEmpty(user.UserName) {
|
||||
doc["updater"] = user.UserName
|
||||
}
|
||||
if !g.IsEmpty(user.TenantId) {
|
||||
doc["tenantId"] = user.TenantId
|
||||
}
|
||||
doc["createdAt"] = gtime.Now().Time
|
||||
doc["updatedAt"] = gtime.Now().Time
|
||||
doc["isDeleted"] = false
|
||||
docs = append(docs, doc)
|
||||
}
|
||||
r, err := db.Collection(collection).InsertMany(ctx, docs, opts...)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
ids = r.InsertedIDs
|
||||
err = m.CleanRedis(ctx, bson.M{}, user.TenantId, collection)
|
||||
return
|
||||
}
|
||||
|
||||
// Count 查询总数
|
||||
func (m *MongoDB) Count(ctx context.Context, filter bson.M, collection string) (count int64, err error) {
|
||||
source, err := m.getDataSource()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
db := source.Database()
|
||||
|
||||
user, err := utils.GetUserInfo(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
filter["isDeleted"] = false
|
||||
delete(filter, "tenantId")
|
||||
filterKey := fmt.Sprintf("%+v", filter)
|
||||
redisKey := fmt.Sprintf(redis.Count, user.TenantId, collection, filterKey)
|
||||
if m.Cache {
|
||||
var resultStr *gvar.Var
|
||||
resultStr, err = redis.RedisClient.Get(ctx, redisKey)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if !g.IsEmpty(resultStr) {
|
||||
count = gconv.Int64(resultStr)
|
||||
return
|
||||
}
|
||||
}
|
||||
count, err = db.Collection(collection).CountDocuments(ctx, filter)
|
||||
if m.Cache {
|
||||
err = redis.RedisClient.SetEX(ctx, redisKey, count, int64(time.Hour))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func BuildUpdateFilter(ctx context.Context, req interface{}) (filter bson.M, err error) {
|
||||
_ = ctx
|
||||
filter = bson.M{}
|
||||
|
||||
Reference in New Issue
Block a user