package service import ( "context" "gitea.redpowerfuture.com/red-future/common/beans" "gitea.redpowerfuture.com/red-future/common/log/dao" "gitea.redpowerfuture.com/red-future/common/log/model/dto" logEntity "gitea.redpowerfuture.com/red-future/common/log/model/entity" "gitea.redpowerfuture.com/red-future/common/utils" "github.com/gogf/gf/v2/util/gconv" ) type operationLog struct{} // OperationLog 操作日志服务 var OperationLog = &operationLog{} func (s *operationLog) AddOperationLog(ctx context.Context, msgData any) error { msg := gconv.MapStrStr(msgData) 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, Operation: operation, IPAddress: ipAddress, Data: data, } return dao.Log.Create(ctx, log) } // 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 } res = &dto.ListLogsResp{ Total: total, } err = utils.Struct(logs, &res.Logs) if err != nil { return } return }