Files
common/log/service/log_service.go

67 lines
1.9 KiB
Go

package service
import (
"context"
"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"
"gitee.com/red-future---jilin-g/common/utils"
"github.com/gogf/gf/v2/util/gconv"
)
type operationLog struct{}
// OperationLog 操作日志服务
var OperationLog = &operationLog{}
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,
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
}