package service import ( "context" "encoding/json" "errors" "io" "time" "gorm.io/gorm" "ws_usernode/internal/model" ) // AuditService 审计服务:append-only 记录(业务代码仅允许 INSERT), // 保留策略与 CSV 导出归档在 M4 完成,这里给出接口与最小实现。 type AuditService struct { db *gorm.DB } // NewAuditService 创建审计服务。 func NewAuditService(db *gorm.DB) *AuditService { return &AuditService{db: db} } // Record 记录一条管理操作审计。detail 为任意结构体,入库前 JSON 序列化。 func (s *AuditService) Record(ctx context.Context, actorID uint, actorName, action, resourceType, resourceID string, detail any, ip, result string) error { b, err := json.Marshal(detail) if err != nil { return err } entry := model.AuditLog{ ActorID: actorID, ActorName: actorName, Action: action, ResourceType: resourceType, ResourceID: resourceID, Detail: string(b), IP: ip, Result: result, } return s.db.WithContext(ctx).Create(&entry).Error } // ExportCSV 导出审计为 CSV。M0 骨架:返回占位错误,M4 实现手动导出 + 每日归档。 func (s *AuditService) ExportCSV(ctx context.Context, w io.Writer, since, until *time.Time) error { _ = w _ = since _ = until return errors.New("service: 审计 CSV 导出将在 M4 实现") }