优化mongo,封装count逻辑,处理objectId

This commit is contained in:
2026-01-07 18:27:43 +08:00
parent 07b4240226
commit b5697d72ff
4 changed files with 38 additions and 80 deletions

View File

@@ -48,12 +48,3 @@ type ListLogsResp struct {
Logs []OperationLogInfo `json:"logs" dc:"日志列表"`
Total int64 `json:"total" dc:"总数"`
}
// ========== 记录操作日志DTO ==========
// RecordCreateLogReq 记录创建操作日志请求
type RecordCreateLogReq struct {
ServiceName string `json:"service_name" v:"required" dc:"服务名"`
Collection string `json:"collection" v:"required" dc:"数据所在集合名称"`
Data []interface{} `json:"data" dc:"当前数据"`
}

View File

@@ -8,11 +8,10 @@ import (
type OperationLog struct {
beans.MongoBaseDO `bson:",inline"`
ServiceName string `bson:"service_name" json:"service_name"` // 服务名:具体的微服务名称
Collection string `bson:"collection" json:"collection"` // 集合名:数据所在的集合名称
CollectionID string `bson:"collection_id" json:"collection_id"` // 数据ID具体操作的数据ID如订单号、钱包ID等
Operation string `bson:"operation" json:"operation"` // 操作类型create, update, delete
UserName string `bson:"user_name" json:"user_name"` // 操作人名称
IPAddress string `bson:"ip_address" json:"ip_address"` // 操作IP地址
Data map[string]interface{} `bson:"data,omitempty" json:"data"` // 当前数据:操作时的数据状态
ServiceName string `bson:"service_name" json:"service_name"` // 服务名:具体的微服务名称
Collection string `bson:"collection" json:"collection"` // 集合名:数据所在的集合名称
CollectionID string `bson:"collection_id" json:"collection_id"` // 数据ID具体操作的数据ID如订单号、钱包ID等
Operation string `bson:"operation" json:"operation"` // 操作类型create, update, delete
IPAddress string `bson:"ip_address" json:"ip_address"` // 操作IP地址
Data interface{} `bson:"data,omitempty" json:"data"` // 当前数据:操作时的数据状态
}

View File

@@ -7,7 +7,6 @@ import (
"gitee.com/red-future---jilin-g/common/log/dao"
"gitee.com/red-future---jilin-g/common/log/model/dto"
logEntity "gitee.com/red-future---jilin-g/common/log/model/entity"
"gitee.com/red-future---jilin-g/common/utils"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gconv"
)
@@ -75,26 +74,14 @@ func (s *operationLog) List(ctx context.Context, req *dto.ListLogsReq) ([]dto.Op
// record 记录操作日志的通用方法
func (s *operationLog) record(ctx context.Context, serviceName, collection, collectionID, operation string, data map[string]interface{}) error {
// 获取用户信息
user, err := utils.GetUserInfo(ctx)
if err != nil {
return err
}
// 获取请求信息
ipAddress := getHTTPRequestInfo(ctx)
var userName string
if user.UserName != nil {
userName = gconv.String(user.UserName)
}
log := &logEntity.OperationLog{
ServiceName: serviceName,
Collection: collection,
CollectionID: collectionID,
Operation: operation,
UserName: userName,
IPAddress: ipAddress,
Data: data,
}