优化日志模块:重构操作日志记录与查询功能,支持批量操作日志记录,完善日志查询接口,增加软删除操作类型
This commit is contained in:
@@ -2,12 +2,11 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitee.com/red-future---jilin-g/common/log/consts"
|
||||
"gitee.com/red-future---jilin-g/common/beans"
|
||||
"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"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"gitee.com/red-future---jilin-g/common/utils"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
@@ -16,84 +15,52 @@ type operationLog struct{}
|
||||
// OperationLog 操作日志服务
|
||||
var OperationLog = &operationLog{}
|
||||
|
||||
// RecordCreate 记录创建操作
|
||||
func (s *operationLog) RecordCreate(ctx context.Context, serviceName, collection, collectionID string, data map[string]interface{}) error {
|
||||
return s.record(ctx, serviceName, collection, collectionID, string(consts.OperationCreate), data)
|
||||
}
|
||||
|
||||
// RecordUpdate 记录更新操作
|
||||
func (s *operationLog) RecordUpdate(ctx context.Context, serviceName, collection, collectionID string, data map[string]interface{}) error {
|
||||
return s.record(ctx, serviceName, collection, collectionID, string(consts.OperationUpdate), data)
|
||||
}
|
||||
|
||||
// RecordDelete 记录删除操作
|
||||
func (s *operationLog) RecordDelete(ctx context.Context, serviceName, collection, collectionID string, data map[string]interface{}) error {
|
||||
return s.record(ctx, serviceName, collection, collectionID, string(consts.OperationDelete), data)
|
||||
}
|
||||
|
||||
// BatchRecordCreate 批量记录创建操作
|
||||
func (s *operationLog) BatchRecordCreate(ctx context.Context, logs []*logEntity.OperationLog) error {
|
||||
return dao.Log.CreateBatch(ctx, logs)
|
||||
}
|
||||
|
||||
// GetByID 根据ID获取操作日志
|
||||
func (s *operationLog) GetByID(ctx context.Context, id string) (*dto.OperationLogInfo, error) {
|
||||
log, err := dao.Log.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var logInfo dto.OperationLogInfo
|
||||
if err := gconv.Struct(log, &logInfo); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
logInfo.ID = log.Id.Hex()
|
||||
return &logInfo, nil
|
||||
}
|
||||
|
||||
// List 查询操作日志列表
|
||||
func (s *operationLog) List(ctx context.Context, req *dto.ListLogsReq) ([]dto.OperationLogInfo, int64, error) {
|
||||
logs, total, err := dao.Log.List(ctx, req)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
var logInfos []dto.OperationLogInfo
|
||||
err = gconv.Structs(logs, &logInfos)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
// 处理特殊字段
|
||||
for i, log := range logs {
|
||||
logInfos[i].ID = log.Id.Hex()
|
||||
}
|
||||
|
||||
return logInfos, total, nil
|
||||
}
|
||||
|
||||
// record 记录操作日志的通用方法
|
||||
func (s *operationLog) record(ctx context.Context, serviceName, collection, collectionID, operation string, data map[string]interface{}) error {
|
||||
// 获取请求信息
|
||||
ipAddress := getHTTPRequestInfo(ctx)
|
||||
func (s *operationLog) AddOperationLog(ctx context.Context, msg map[string]interface{}) error {
|
||||
serviceName := gconv.String(msg["service_name"])
|
||||
collection := gconv.String(msg["collection"])
|
||||
collectionId := gconv.Strings(msg["collection_id"])
|
||||
operation := gconv.String(msg["operation"])
|
||||
ipAddress := gconv.String(msg["ip_address"])
|
||||
data := gconv.Maps(msg["data"])
|
||||
creator := gconv.String(msg["creator"])
|
||||
createdAt := gconv.Time(msg["createdAt"])
|
||||
updater := gconv.String(msg["updater"])
|
||||
updatedAt := gconv.Time(msg["updatedAt"])
|
||||
tenantId := gconv.Float64(msg["tenantId"])
|
||||
// 设置 userId 和 tenantId 到 ctx
|
||||
ctx = context.WithValue(ctx, "userName", creator)
|
||||
ctx = context.WithValue(ctx, "tenantId", tenantId)
|
||||
|
||||
log := &logEntity.OperationLog{
|
||||
MongoBaseDO: beans.MongoBaseDO{
|
||||
Creator: creator,
|
||||
CreatedAt: &createdAt,
|
||||
Updater: updater,
|
||||
UpdatedAt: &updatedAt,
|
||||
TenantId: tenantId,
|
||||
},
|
||||
ServiceName: serviceName,
|
||||
Collection: collection,
|
||||
CollectionID: collectionID,
|
||||
CollectionID: collectionId,
|
||||
Operation: operation,
|
||||
IPAddress: ipAddress,
|
||||
Data: data,
|
||||
}
|
||||
|
||||
return dao.Log.Create(ctx, log)
|
||||
}
|
||||
|
||||
// getHTTPRequestInfo 从上下文中获取HTTP请求信息
|
||||
func getHTTPRequestInfo(ctx context.Context) string {
|
||||
request := g.RequestFromCtx(ctx)
|
||||
if request != nil {
|
||||
return request.GetClientIp()
|
||||
// GetByCollectionId 根据集合ID获取操作日志
|
||||
func (s *operationLog) GetByCollectionId(ctx context.Context, req *dto.ListLogsReq) (res *dto.ListLogsResp, err error) {
|
||||
logs, total, err := dao.Log.List(ctx, req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return ""
|
||||
res = &dto.ListLogsResp{
|
||||
Total: total,
|
||||
}
|
||||
err = utils.Struct(logs, &res.Logs)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user