// Package exchange 提供换货业务对公共基础设施的 Adapter。 package exchange import ( "context" "fmt" "strconv" "gorm.io/gorm" exchangeapp "github.com/break/junhong_cmp_fiber/internal/application/exchange" notificationapp "github.com/break/junhong_cmp_fiber/internal/application/notification" "github.com/break/junhong_cmp_fiber/internal/infrastructure/messaging/outbox" "github.com/break/junhong_cmp_fiber/pkg/constants" "github.com/break/junhong_cmp_fiber/pkg/errors" ) // ShippingNotificationWriter 将物流换货创建事实转换为个人客户通知 Outbox。 type ShippingNotificationWriter struct { outbox *outbox.Repository } // NewShippingNotificationWriter 创建物流换货通知 Outbox Adapter。 func NewShippingNotificationWriter(repository *outbox.Repository) *ShippingNotificationWriter { return &ShippingNotificationWriter{outbox: repository} } // Append 在换货业务事务中追加稳定且可重复提交的个人客户通知事件。 func (w *ShippingNotificationWriter) Append(ctx context.Context, tx *gorm.DB, event exchangeapp.ShippingCreatedEvent) error { if w == nil || w.outbox == nil { return errors.New(errors.CodeInternalError, "物流换货通知 Outbox Writer 未配置") } exchangeID := strconv.FormatUint(uint64(event.ExchangeID), 10) assetID := strconv.FormatUint(uint64(event.AssetID), 10) eventID := fmt.Sprintf("exchange-created:%d:%d", event.ExchangeID, event.CustomerID) _, err := w.outbox.AppendIdempotent(ctx, tx, outbox.Envelope{ EventID: eventID, EventType: constants.OutboxEventTypePersonalCustomerDirectNotification, PayloadVersion: constants.NotificationPayloadVersionV1, AggregateType: "exchange", AggregateID: exchangeID, ResourceType: event.AssetType, ResourceID: assetID, BusinessKey: eventID, RequestID: event.RequestID, CorrelationID: event.CorrelationID, Payload: notificationapp.PersonalCustomerDirectPayload{ RecipientID: event.CustomerID, NotificationType: constants.NotificationTypeExchangeShippingCreated, TemplateData: map[string]string{}, RefType: constants.NotificationRefTypeAsset, RefID: assetID, RefKey: event.AssetIdentifier, }, }) return err }