115 lines
3.8 KiB
Go
115 lines
3.8 KiB
Go
|
|
package events
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
|
||
|
|
"go.uber.org/zap"
|
||
|
|
|
||
|
|
"tyapi-server/internal/domains/finance/events"
|
||
|
|
"tyapi-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
|
||
|
|
}
|