package service import ( "context" "time" "github.com/google/uuid" "github.com/ocdp/cluster-service/internal/domain/entity" "github.com/ocdp/cluster-service/internal/domain/repository" ) // AuditService 审计日志领域服务 type AuditService struct { auditLogRepo repository.AuditLogRepository } // NewAuditService 创建审计服务 func NewAuditService(auditLogRepo repository.AuditLogRepository) *AuditService { return &AuditService{ auditLogRepo: auditLogRepo, } } // Log 创建审计日志 func (s *AuditService) Log(ctx context.Context, workspaceID, userID string, action entity.AuditAction, resourceType entity.AuditResourceType, resourceID, resourceName string, details map[string]interface{}, ipAddress, userAgent string) error { auditLog := &entity.AuditLog{ ID: uuid.New().String(), WorkspaceID: workspaceID, UserID: userID, Action: action, ResourceType: resourceType, ResourceID: resourceID, ResourceName: resourceName, Details: details, IPAddress: ipAddress, UserAgent: userAgent, CreatedAt: time.Now(), } return s.auditLogRepo.Create(ctx, auditLog) } // LogAction 简化版日志记录 func (s *AuditService) LogAction(ctx context.Context, workspaceID, userID string, action entity.AuditAction, resourceType entity.AuditResourceType, resourceName string) error { return s.Log(ctx, workspaceID, userID, action, resourceType, "", resourceName, nil, "", "") } // LogWithDetails 带详情的日志记录 func (s *AuditService) LogWithDetails(ctx context.Context, workspaceID, userID string, action entity.AuditAction, resourceType entity.AuditResourceType, resourceID, resourceName string, details map[string]interface{}) error { return s.Log(ctx, workspaceID, userID, action, resourceType, resourceID, resourceName, details, "", "") } // GetLogs 获取审计日志 func (s *AuditService) GetLogs(ctx context.Context, workspaceID string, limit int) ([]*entity.AuditLog, error) { return s.auditLogRepo.GetByWorkspace(ctx, workspaceID, limit) } // GetUserLogs 获取用户的审计日志 func (s *AuditService) GetUserLogs(ctx context.Context, userID string, limit int) ([]*entity.AuditLog, error) { return s.auditLogRepo.GetByUser(ctx, userID, limit) } // GetResourceLogs 获取资源的审计日志 func (s *AuditService) GetResourceLogs(ctx context.Context, resourceType entity.AuditResourceType, resourceID string, limit int) ([]*entity.AuditLog, error) { return s.auditLogRepo.GetByResource(ctx, resourceType, resourceID, limit) } // GetAllLogs 获取所有审计日志(Admin) func (s *AuditService) GetAllLogs(ctx context.Context, limit int, offset int) ([]*entity.AuditLog, error) { return s.auditLogRepo.List(ctx, limit, offset) }