package audit import ( "context" "gorm.io/gorm" pkgerrors "github.com/break/junhong_cmp_fiber/pkg/errors" ) // BatchInput 描述一条批次根事件及每个已识别资源的子事件。 type BatchInput struct { Root AppendInput Children []AppendInput } // AppendBatch 在同一事务内追加批次根事件和资源子事件。 func (w *Writer) AppendBatch(ctx context.Context, tx *gorm.DB, input BatchInput) error { if input.Root.EventID == "" { return pkgerrors.New(pkgerrors.CodeInvalidParam, "批次根事件缺少稳定事件ID") } if input.Root.BatchTotal < 0 || input.Root.SuccessCount < 0 || input.Root.FailCount < 0 || input.Root.SuccessCount+input.Root.FailCount > input.Root.BatchTotal || len(input.Children) < input.Root.SuccessCount || len(input.Children) > input.Root.SuccessCount+input.Root.FailCount { return pkgerrors.New(pkgerrors.CodeInvalidParam, "批次根子事件计数不一致") } if err := w.Append(ctx, tx, input.Root); err != nil { return err } for index := range input.Children { child := input.Children[index] if child.EventID == "" { return pkgerrors.New(pkgerrors.CodeInvalidParam, "批次子事件缺少稳定事件ID") } if child.ParentEventID == "" { child.ParentEventID = input.Root.EventID } if child.CorrelationID == "" { child.CorrelationID = input.Root.CorrelationID } if err := w.Append(ctx, tx, child); err != nil { return err } } return nil }