2025-12-30 16:14:15 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2026-02-24 15:42:36 +08:00
|
|
|
"gitea.com/red-future/common/beans"
|
|
|
|
|
"gitea.com/red-future/common/log/dao"
|
|
|
|
|
"gitea.com/red-future/common/log/model/dto"
|
|
|
|
|
logEntity "gitea.com/red-future/common/log/model/entity"
|
|
|
|
|
"gitea.com/red-future/common/utils"
|
2025-12-30 16:14:15 +08:00
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type operationLog struct{}
|
|
|
|
|
|
|
|
|
|
// OperationLog 操作日志服务
|
|
|
|
|
var OperationLog = &operationLog{}
|
|
|
|
|
|
2026-03-02 14:22:08 +08:00
|
|
|
func (s *operationLog) AddOperationLog(ctx context.Context, msgData any) error {
|
|
|
|
|
msg := gconv.MapStrStr(msgData)
|
2026-01-16 16:55:32 +08:00
|
|
|
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)
|
2025-12-30 16:14:15 +08:00
|
|
|
|
|
|
|
|
log := &logEntity.OperationLog{
|
2026-01-16 16:55:32 +08:00
|
|
|
MongoBaseDO: beans.MongoBaseDO{
|
|
|
|
|
Creator: creator,
|
|
|
|
|
CreatedAt: &createdAt,
|
|
|
|
|
Updater: updater,
|
|
|
|
|
UpdatedAt: &updatedAt,
|
|
|
|
|
TenantId: tenantId,
|
|
|
|
|
},
|
2025-12-30 18:18:56 +08:00
|
|
|
ServiceName: serviceName,
|
|
|
|
|
Collection: collection,
|
2026-01-16 16:55:32 +08:00
|
|
|
CollectionID: collectionId,
|
2025-12-30 18:18:56 +08:00
|
|
|
Operation: operation,
|
|
|
|
|
IPAddress: ipAddress,
|
|
|
|
|
Data: data,
|
2025-12-30 16:14:15 +08:00
|
|
|
}
|
|
|
|
|
return dao.Log.Create(ctx, log)
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-16 16:55:32 +08:00
|
|
|
// 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
|
2025-12-30 16:14:15 +08:00
|
|
|
}
|
2026-01-16 16:55:32 +08:00
|
|
|
return
|
2025-12-30 16:14:15 +08:00
|
|
|
}
|