f
This commit is contained in:
291
internal/infrastructure/events/certification_event_publisher.go
Normal file
291
internal/infrastructure/events/certification_event_publisher.go
Normal file
@@ -0,0 +1,291 @@
|
||||
package events
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"hyapi-server/internal/shared/interfaces"
|
||||
)
|
||||
|
||||
// ================ 常量定义 ================
|
||||
|
||||
const (
|
||||
// 事件类型
|
||||
EventTypeCertificationCreated = "certification.created"
|
||||
EventTypeEnterpriseInfoSubmitted = "certification.enterprise_info_submitted"
|
||||
EventTypeEnterpriseVerificationCompleted = "certification.enterprise_verification_completed"
|
||||
EventTypeContractGenerated = "certification.contract_generated"
|
||||
EventTypeContractSigned = "certification.contract_signed"
|
||||
EventTypeCertificationCompleted = "certification.completed"
|
||||
EventTypeCertificationFailed = "certification.failed"
|
||||
EventTypeStatusTransitioned = "certification.status_transitioned"
|
||||
|
||||
// 重试配置
|
||||
MaxRetries = 3
|
||||
RetryDelay = 5 * time.Second
|
||||
)
|
||||
|
||||
// ================ 事件结构 ================
|
||||
|
||||
// CertificationEventData 认证事件数据结构
|
||||
type CertificationEventData struct {
|
||||
EventType string `json:"event_type"`
|
||||
CertificationID string `json:"certification_id"`
|
||||
UserID string `json:"user_id"`
|
||||
Data map[string]interface{} `json:"data"`
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
Version string `json:"version"`
|
||||
}
|
||||
|
||||
// ================ 事件发布器实现 ================
|
||||
|
||||
// CertificationEventPublisher 认证事件发布器实现
|
||||
//
|
||||
// 职责:
|
||||
// - 发布认证域相关的事件
|
||||
// - 支持异步发布和重试机制
|
||||
// - 提供事件持久化能力
|
||||
// - 集成监控和日志
|
||||
type CertificationEventPublisher struct {
|
||||
eventBus interfaces.EventBus
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
// NewCertificationEventPublisher 创建认证事件发布器
|
||||
func NewCertificationEventPublisher(
|
||||
eventBus interfaces.EventBus,
|
||||
logger *zap.Logger,
|
||||
) *CertificationEventPublisher {
|
||||
return &CertificationEventPublisher{
|
||||
eventBus: eventBus,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// ================ 事件发布方法 ================
|
||||
|
||||
// PublishCertificationCreated 发布认证创建事件
|
||||
func (p *CertificationEventPublisher) PublishCertificationCreated(
|
||||
ctx context.Context,
|
||||
certificationID, userID string,
|
||||
data map[string]interface{},
|
||||
) error {
|
||||
eventData := &CertificationEventData{
|
||||
EventType: EventTypeCertificationCreated,
|
||||
CertificationID: certificationID,
|
||||
UserID: userID,
|
||||
Data: data,
|
||||
Timestamp: time.Now(),
|
||||
Version: "1.0",
|
||||
}
|
||||
|
||||
return p.publishEventData(ctx, eventData)
|
||||
}
|
||||
|
||||
// PublishEnterpriseInfoSubmitted 发布企业信息提交事件
|
||||
func (p *CertificationEventPublisher) PublishEnterpriseInfoSubmitted(
|
||||
ctx context.Context,
|
||||
certificationID, userID string,
|
||||
data map[string]interface{},
|
||||
) error {
|
||||
eventData := &CertificationEventData{
|
||||
EventType: EventTypeEnterpriseInfoSubmitted,
|
||||
CertificationID: certificationID,
|
||||
UserID: userID,
|
||||
Data: data,
|
||||
Timestamp: time.Now(),
|
||||
Version: "1.0",
|
||||
}
|
||||
|
||||
return p.publishEventData(ctx, eventData)
|
||||
}
|
||||
|
||||
// PublishEnterpriseVerificationCompleted 发布企业认证完成事件
|
||||
func (p *CertificationEventPublisher) PublishEnterpriseVerificationCompleted(
|
||||
ctx context.Context,
|
||||
certificationID, userID string,
|
||||
data map[string]interface{},
|
||||
) error {
|
||||
eventData := &CertificationEventData{
|
||||
EventType: EventTypeEnterpriseVerificationCompleted,
|
||||
CertificationID: certificationID,
|
||||
UserID: userID,
|
||||
Data: data,
|
||||
Timestamp: time.Now(),
|
||||
Version: "1.0",
|
||||
}
|
||||
|
||||
return p.publishEventData(ctx, eventData)
|
||||
}
|
||||
|
||||
// PublishContractGenerated 发布合同生成事件
|
||||
func (p *CertificationEventPublisher) PublishContractGenerated(
|
||||
ctx context.Context,
|
||||
certificationID, userID string,
|
||||
data map[string]interface{},
|
||||
) error {
|
||||
eventData := &CertificationEventData{
|
||||
EventType: EventTypeContractGenerated,
|
||||
CertificationID: certificationID,
|
||||
UserID: userID,
|
||||
Data: data,
|
||||
Timestamp: time.Now(),
|
||||
Version: "1.0",
|
||||
}
|
||||
|
||||
return p.publishEventData(ctx, eventData)
|
||||
}
|
||||
|
||||
// PublishContractSigned 发布合同签署事件
|
||||
func (p *CertificationEventPublisher) PublishContractSigned(
|
||||
ctx context.Context,
|
||||
certificationID, userID string,
|
||||
data map[string]interface{},
|
||||
) error {
|
||||
eventData := &CertificationEventData{
|
||||
EventType: EventTypeContractSigned,
|
||||
CertificationID: certificationID,
|
||||
UserID: userID,
|
||||
Data: data,
|
||||
Timestamp: time.Now(),
|
||||
Version: "1.0",
|
||||
}
|
||||
|
||||
return p.publishEventData(ctx, eventData)
|
||||
}
|
||||
|
||||
// PublishCertificationCompleted 发布认证完成事件
|
||||
func (p *CertificationEventPublisher) PublishCertificationCompleted(
|
||||
ctx context.Context,
|
||||
certificationID, userID string,
|
||||
data map[string]interface{},
|
||||
) error {
|
||||
eventData := &CertificationEventData{
|
||||
EventType: EventTypeCertificationCompleted,
|
||||
CertificationID: certificationID,
|
||||
UserID: userID,
|
||||
Data: data,
|
||||
Timestamp: time.Now(),
|
||||
Version: "1.0",
|
||||
}
|
||||
|
||||
return p.publishEventData(ctx, eventData)
|
||||
}
|
||||
|
||||
// PublishCertificationFailed 发布认证失败事件
|
||||
func (p *CertificationEventPublisher) PublishCertificationFailed(
|
||||
ctx context.Context,
|
||||
certificationID, userID string,
|
||||
data map[string]interface{},
|
||||
) error {
|
||||
eventData := &CertificationEventData{
|
||||
EventType: EventTypeCertificationFailed,
|
||||
CertificationID: certificationID,
|
||||
UserID: userID,
|
||||
Data: data,
|
||||
Timestamp: time.Now(),
|
||||
Version: "1.0",
|
||||
}
|
||||
|
||||
return p.publishEventData(ctx, eventData)
|
||||
}
|
||||
|
||||
// PublishStatusTransitioned 发布状态转换事件
|
||||
func (p *CertificationEventPublisher) PublishStatusTransitioned(
|
||||
ctx context.Context,
|
||||
certificationID, userID string,
|
||||
data map[string]interface{},
|
||||
) error {
|
||||
eventData := &CertificationEventData{
|
||||
EventType: EventTypeStatusTransitioned,
|
||||
CertificationID: certificationID,
|
||||
UserID: userID,
|
||||
Data: data,
|
||||
Timestamp: time.Now(),
|
||||
Version: "1.0",
|
||||
}
|
||||
|
||||
return p.publishEventData(ctx, eventData)
|
||||
}
|
||||
|
||||
// ================ 内部实现 ================
|
||||
|
||||
// publishEventData 发布事件数据(带重试机制)
|
||||
func (p *CertificationEventPublisher) publishEventData(ctx context.Context, eventData *CertificationEventData) error {
|
||||
p.logger.Info("发布认证事件",
|
||||
zap.String("event_type", eventData.EventType),
|
||||
zap.String("certification_id", eventData.CertificationID),
|
||||
zap.Time("timestamp", eventData.Timestamp))
|
||||
|
||||
// 尝试发布事件,带重试机制
|
||||
var lastErr error
|
||||
for attempt := 0; attempt <= MaxRetries; attempt++ {
|
||||
if attempt > 0 {
|
||||
// 指数退避重试
|
||||
delay := time.Duration(attempt) * RetryDelay
|
||||
p.logger.Warn("事件发布重试",
|
||||
zap.String("event_type", eventData.EventType),
|
||||
zap.Int("attempt", attempt),
|
||||
zap.Duration("delay", delay))
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case <-time.After(delay):
|
||||
// 继续重试
|
||||
}
|
||||
}
|
||||
|
||||
// 简化的事件发布:直接记录日志
|
||||
p.logger.Info("模拟事件发布",
|
||||
zap.String("event_type", eventData.EventType),
|
||||
zap.String("certification_id", eventData.CertificationID),
|
||||
zap.Any("data", eventData.Data))
|
||||
|
||||
// TODO: 这里可以集成真正的事件总线
|
||||
// if err := p.eventBus.Publish(ctx, eventData); err != nil {
|
||||
// lastErr = err
|
||||
// continue
|
||||
// }
|
||||
|
||||
// 发布成功
|
||||
p.logger.Info("事件发布成功",
|
||||
zap.String("event_type", eventData.EventType),
|
||||
zap.String("certification_id", eventData.CertificationID))
|
||||
return nil
|
||||
}
|
||||
|
||||
// 理论上不会到达这里,因为简化实现总是成功
|
||||
return lastErr
|
||||
}
|
||||
|
||||
// ================ 事件处理器注册 ================
|
||||
|
||||
// RegisterEventHandlers 注册事件处理器
|
||||
func (p *CertificationEventPublisher) RegisterEventHandlers() error {
|
||||
// TODO: 注册具体的事件处理器
|
||||
// 例如:发送通知、更新统计数据、触发后续流程等
|
||||
|
||||
p.logger.Info("认证事件处理器已注册")
|
||||
return nil
|
||||
}
|
||||
|
||||
// ================ 工具方法 ================
|
||||
|
||||
// CreateEventData 创建事件数据
|
||||
func CreateEventData(eventType, certificationID, userID string, data map[string]interface{}) map[string]interface{} {
|
||||
if data == nil {
|
||||
data = make(map[string]interface{})
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
"event_type": eventType,
|
||||
"certification_id": certificationID,
|
||||
"user_id": userID,
|
||||
"data": data,
|
||||
"timestamp": time.Now(),
|
||||
"version": "1.0",
|
||||
}
|
||||
}
|
||||
228
internal/infrastructure/events/invoice_event_handler.go
Normal file
228
internal/infrastructure/events/invoice_event_handler.go
Normal file
@@ -0,0 +1,228 @@
|
||||
package events
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"hyapi-server/internal/domains/finance/events"
|
||||
"hyapi-server/internal/infrastructure/external/email"
|
||||
"hyapi-server/internal/shared/interfaces"
|
||||
)
|
||||
|
||||
// InvoiceEventHandler 发票事件处理器
|
||||
type InvoiceEventHandler struct {
|
||||
logger *zap.Logger
|
||||
emailService *email.QQEmailService
|
||||
name string
|
||||
eventTypes []string
|
||||
isAsync bool
|
||||
}
|
||||
|
||||
// NewInvoiceEventHandler 创建发票事件处理器
|
||||
func NewInvoiceEventHandler(logger *zap.Logger, emailService *email.QQEmailService) *InvoiceEventHandler {
|
||||
return &InvoiceEventHandler{
|
||||
logger: logger,
|
||||
emailService: emailService,
|
||||
name: "invoice-event-handler",
|
||||
eventTypes: []string{
|
||||
"InvoiceApplicationCreated",
|
||||
"InvoiceApplicationApproved",
|
||||
"InvoiceApplicationRejected",
|
||||
"InvoiceFileUploaded",
|
||||
},
|
||||
isAsync: true,
|
||||
}
|
||||
}
|
||||
|
||||
// GetName 获取处理器名称
|
||||
func (h *InvoiceEventHandler) GetName() string {
|
||||
return h.name
|
||||
}
|
||||
|
||||
// GetEventTypes 获取支持的事件类型
|
||||
func (h *InvoiceEventHandler) GetEventTypes() []string {
|
||||
return h.eventTypes
|
||||
}
|
||||
|
||||
// IsAsync 是否为异步处理器
|
||||
func (h *InvoiceEventHandler) IsAsync() bool {
|
||||
return h.isAsync
|
||||
}
|
||||
|
||||
// GetRetryConfig 获取重试配置
|
||||
func (h *InvoiceEventHandler) GetRetryConfig() interfaces.RetryConfig {
|
||||
return interfaces.RetryConfig{
|
||||
MaxRetries: 3,
|
||||
RetryDelay: 5 * time.Second,
|
||||
BackoffFactor: 2.0,
|
||||
MaxDelay: 30 * time.Second,
|
||||
}
|
||||
}
|
||||
|
||||
// Handle 处理事件
|
||||
func (h *InvoiceEventHandler) Handle(ctx context.Context, event interfaces.Event) error {
|
||||
h.logger.Info("🔄 开始处理发票事件",
|
||||
zap.String("event_type", event.GetType()),
|
||||
zap.String("event_id", event.GetID()),
|
||||
zap.String("aggregate_id", event.GetAggregateID()),
|
||||
zap.String("handler_name", h.GetName()),
|
||||
zap.Time("event_timestamp", event.GetTimestamp()),
|
||||
)
|
||||
|
||||
switch event.GetType() {
|
||||
case "InvoiceApplicationCreated":
|
||||
h.logger.Info("📝 处理发票申请创建事件")
|
||||
return h.handleInvoiceApplicationCreated(ctx, event)
|
||||
case "InvoiceApplicationApproved":
|
||||
h.logger.Info("✅ 处理发票申请通过事件")
|
||||
return h.handleInvoiceApplicationApproved(ctx, event)
|
||||
case "InvoiceApplicationRejected":
|
||||
h.logger.Info("❌ 处理发票申请拒绝事件")
|
||||
return h.handleInvoiceApplicationRejected(ctx, event)
|
||||
case "InvoiceFileUploaded":
|
||||
h.logger.Info("📎 处理发票文件上传事件")
|
||||
return h.handleInvoiceFileUploaded(ctx, event)
|
||||
default:
|
||||
h.logger.Warn("⚠️ 未知的发票事件类型", zap.String("event_type", event.GetType()))
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// handleInvoiceApplicationCreated 处理发票申请创建事件
|
||||
func (h *InvoiceEventHandler) handleInvoiceApplicationCreated(ctx context.Context, event interfaces.Event) error {
|
||||
h.logger.Info("发票申请已创建",
|
||||
zap.String("application_id", event.GetAggregateID()),
|
||||
)
|
||||
|
||||
// 这里可以发送通知给管理员,告知有新的发票申请
|
||||
// 暂时只记录日志
|
||||
return nil
|
||||
}
|
||||
|
||||
// handleInvoiceApplicationApproved 处理发票申请通过事件
|
||||
func (h *InvoiceEventHandler) handleInvoiceApplicationApproved(ctx context.Context, event interfaces.Event) error {
|
||||
h.logger.Info("发票申请已通过",
|
||||
zap.String("application_id", event.GetAggregateID()),
|
||||
)
|
||||
|
||||
// 这里可以发送通知给用户,告知发票申请已通过
|
||||
// 暂时只记录日志
|
||||
return nil
|
||||
}
|
||||
|
||||
// handleInvoiceApplicationRejected 处理发票申请拒绝事件
|
||||
func (h *InvoiceEventHandler) handleInvoiceApplicationRejected(ctx context.Context, event interfaces.Event) error {
|
||||
h.logger.Info("发票申请被拒绝",
|
||||
zap.String("application_id", event.GetAggregateID()),
|
||||
)
|
||||
|
||||
// 这里可以发送邮件通知用户,告知发票申请被拒绝
|
||||
// 暂时只记录日志
|
||||
return nil
|
||||
}
|
||||
|
||||
// handleInvoiceFileUploaded 处理发票文件上传事件
|
||||
func (h *InvoiceEventHandler) handleInvoiceFileUploaded(ctx context.Context, event interfaces.Event) error {
|
||||
h.logger.Info("📎 发票文件已上传事件开始处理",
|
||||
zap.String("invoice_id", event.GetAggregateID()),
|
||||
zap.String("event_id", event.GetID()),
|
||||
)
|
||||
|
||||
// 解析事件数据
|
||||
payload := event.GetPayload()
|
||||
if payload == nil {
|
||||
h.logger.Error("❌ 事件数据为空")
|
||||
return fmt.Errorf("事件数据为空")
|
||||
}
|
||||
|
||||
h.logger.Info("📋 事件数据解析开始",
|
||||
zap.Any("payload_type", fmt.Sprintf("%T", payload)),
|
||||
)
|
||||
|
||||
// 将payload转换为JSON,然后解析为InvoiceFileUploadedEvent
|
||||
payloadBytes, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
h.logger.Error("❌ 序列化事件数据失败", zap.Error(err))
|
||||
return fmt.Errorf("序列化事件数据失败: %w", err)
|
||||
}
|
||||
|
||||
h.logger.Info("📄 事件数据序列化成功",
|
||||
zap.String("payload_json", string(payloadBytes)),
|
||||
)
|
||||
|
||||
var fileUploadedEvent events.InvoiceFileUploadedEvent
|
||||
err = json.Unmarshal(payloadBytes, &fileUploadedEvent)
|
||||
if err != nil {
|
||||
h.logger.Error("❌ 解析发票文件上传事件失败", zap.Error(err))
|
||||
return fmt.Errorf("解析发票文件上传事件失败: %w", err)
|
||||
}
|
||||
|
||||
h.logger.Info("✅ 事件数据解析成功",
|
||||
zap.String("invoice_id", fileUploadedEvent.InvoiceID),
|
||||
zap.String("user_id", fileUploadedEvent.UserID),
|
||||
zap.String("receiving_email", fileUploadedEvent.ReceivingEmail),
|
||||
zap.String("file_name", fileUploadedEvent.FileName),
|
||||
zap.String("file_url", fileUploadedEvent.FileURL),
|
||||
zap.String("company_name", fileUploadedEvent.CompanyName),
|
||||
zap.String("amount", fileUploadedEvent.Amount.String()),
|
||||
zap.String("invoice_type", string(fileUploadedEvent.InvoiceType)),
|
||||
)
|
||||
|
||||
// 发送发票邮件给用户
|
||||
return h.sendInvoiceEmail(ctx, &fileUploadedEvent)
|
||||
}
|
||||
|
||||
// sendInvoiceEmail 发送发票邮件
|
||||
func (h *InvoiceEventHandler) sendInvoiceEmail(ctx context.Context, event *events.InvoiceFileUploadedEvent) error {
|
||||
h.logger.Info("📧 开始发送发票邮件",
|
||||
zap.String("invoice_id", event.InvoiceID),
|
||||
zap.String("user_id", event.UserID),
|
||||
zap.String("receiving_email", event.ReceivingEmail),
|
||||
zap.String("file_name", event.FileName),
|
||||
zap.String("file_url", event.FileURL),
|
||||
)
|
||||
|
||||
// 构建邮件数据
|
||||
emailData := &email.InvoiceEmailData{
|
||||
CompanyName: event.CompanyName,
|
||||
Amount: event.Amount.String(),
|
||||
InvoiceType: event.InvoiceType.GetDisplayName(),
|
||||
FileURL: event.FileURL,
|
||||
FileName: event.FileName,
|
||||
ReceivingEmail: event.ReceivingEmail,
|
||||
ApprovedAt: event.UploadedAt.Format("2006-01-02 15:04:05"),
|
||||
}
|
||||
|
||||
h.logger.Info("📋 邮件数据构建完成",
|
||||
zap.String("company_name", emailData.CompanyName),
|
||||
zap.String("amount", emailData.Amount),
|
||||
zap.String("invoice_type", emailData.InvoiceType),
|
||||
zap.String("file_url", emailData.FileURL),
|
||||
zap.String("file_name", emailData.FileName),
|
||||
zap.String("receiving_email", emailData.ReceivingEmail),
|
||||
zap.String("approved_at", emailData.ApprovedAt),
|
||||
)
|
||||
|
||||
// 发送邮件
|
||||
h.logger.Info("🚀 开始调用邮件服务发送邮件")
|
||||
err := h.emailService.SendInvoiceEmail(ctx, emailData)
|
||||
if err != nil {
|
||||
h.logger.Error("❌ 发送发票邮件失败",
|
||||
zap.String("invoice_id", event.InvoiceID),
|
||||
zap.String("receiving_email", event.ReceivingEmail),
|
||||
zap.Error(err),
|
||||
)
|
||||
return fmt.Errorf("发送发票邮件失败: %w", err)
|
||||
}
|
||||
|
||||
h.logger.Info("✅ 发票邮件发送成功",
|
||||
zap.String("invoice_id", event.InvoiceID),
|
||||
zap.String("receiving_email", event.ReceivingEmail),
|
||||
)
|
||||
|
||||
return nil
|
||||
}
|
||||
115
internal/infrastructure/events/invoice_event_publisher.go
Normal file
115
internal/infrastructure/events/invoice_event_publisher.go
Normal file
@@ -0,0 +1,115 @@
|
||||
package events
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"hyapi-server/internal/domains/finance/events"
|
||||
"hyapi-server/internal/shared/interfaces"
|
||||
)
|
||||
|
||||
// InvoiceEventPublisher 发票事件发布器实现
|
||||
type InvoiceEventPublisher struct {
|
||||
logger *zap.Logger
|
||||
eventBus interfaces.EventBus
|
||||
}
|
||||
|
||||
// NewInvoiceEventPublisher 创建发票事件发布器
|
||||
func NewInvoiceEventPublisher(logger *zap.Logger, eventBus interfaces.EventBus) *InvoiceEventPublisher {
|
||||
return &InvoiceEventPublisher{
|
||||
logger: logger,
|
||||
eventBus: eventBus,
|
||||
}
|
||||
}
|
||||
|
||||
// PublishInvoiceApplicationCreated 发布发票申请创建事件
|
||||
func (p *InvoiceEventPublisher) PublishInvoiceApplicationCreated(ctx context.Context, event *events.InvoiceApplicationCreatedEvent) error {
|
||||
p.logger.Info("发布发票申请创建事件",
|
||||
zap.String("application_id", event.ApplicationID),
|
||||
zap.String("user_id", event.UserID),
|
||||
zap.String("invoice_type", string(event.InvoiceType)),
|
||||
zap.String("amount", event.Amount.String()),
|
||||
zap.String("company_name", event.CompanyName),
|
||||
zap.String("receiving_email", event.ReceivingEmail),
|
||||
)
|
||||
|
||||
// TODO: 实现实际的事件发布逻辑
|
||||
// 例如:发送到消息队列、调用外部服务等
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// PublishInvoiceApplicationApproved 发布发票申请通过事件
|
||||
func (p *InvoiceEventPublisher) PublishInvoiceApplicationApproved(ctx context.Context, event *events.InvoiceApplicationApprovedEvent) error {
|
||||
p.logger.Info("发布发票申请通过事件",
|
||||
zap.String("application_id", event.ApplicationID),
|
||||
zap.String("user_id", event.UserID),
|
||||
zap.String("amount", event.Amount.String()),
|
||||
zap.String("receiving_email", event.ReceivingEmail),
|
||||
zap.Time("approved_at", event.ApprovedAt),
|
||||
)
|
||||
|
||||
// TODO: 实现实际的事件发布逻辑
|
||||
// 例如:发送邮件通知用户、更新统计数据等
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// PublishInvoiceApplicationRejected 发布发票申请拒绝事件
|
||||
func (p *InvoiceEventPublisher) PublishInvoiceApplicationRejected(ctx context.Context, event *events.InvoiceApplicationRejectedEvent) error {
|
||||
p.logger.Info("发布发票申请拒绝事件",
|
||||
zap.String("application_id", event.ApplicationID),
|
||||
zap.String("user_id", event.UserID),
|
||||
zap.String("reason", event.Reason),
|
||||
zap.String("receiving_email", event.ReceivingEmail),
|
||||
zap.Time("rejected_at", event.RejectedAt),
|
||||
)
|
||||
|
||||
// TODO: 实现实际的事件发布逻辑
|
||||
// 例如:发送邮件通知用户、记录拒绝原因等
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// PublishInvoiceFileUploaded 发布发票文件上传事件
|
||||
func (p *InvoiceEventPublisher) PublishInvoiceFileUploaded(ctx context.Context, event *events.InvoiceFileUploadedEvent) error {
|
||||
p.logger.Info("📤 开始发布发票文件上传事件",
|
||||
zap.String("invoice_id", event.InvoiceID),
|
||||
zap.String("user_id", event.UserID),
|
||||
zap.String("file_id", event.FileID),
|
||||
zap.String("file_name", event.FileName),
|
||||
zap.String("file_url", event.FileURL),
|
||||
zap.String("receiving_email", event.ReceivingEmail),
|
||||
zap.Time("uploaded_at", event.UploadedAt),
|
||||
)
|
||||
|
||||
// 发布到事件总线
|
||||
if p.eventBus != nil {
|
||||
p.logger.Info("🚀 准备发布事件到事件总线",
|
||||
zap.String("event_type", event.GetType()),
|
||||
zap.String("event_id", event.GetID()),
|
||||
)
|
||||
|
||||
if err := p.eventBus.Publish(ctx, event); err != nil {
|
||||
p.logger.Error("❌ 发布发票文件上传事件到事件总线失败",
|
||||
zap.String("invoice_id", event.InvoiceID),
|
||||
zap.String("event_type", event.GetType()),
|
||||
zap.String("event_id", event.GetID()),
|
||||
zap.Error(err),
|
||||
)
|
||||
return err
|
||||
}
|
||||
p.logger.Info("✅ 发票文件上传事件已发布到事件总线",
|
||||
zap.String("invoice_id", event.InvoiceID),
|
||||
zap.String("event_type", event.GetType()),
|
||||
zap.String("event_id", event.GetID()),
|
||||
)
|
||||
} else {
|
||||
p.logger.Warn("⚠️ 事件总线未初始化,无法发布事件",
|
||||
zap.String("invoice_id", event.InvoiceID),
|
||||
)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user