Files
tyapi-server/internal/domains/finance/events/invoice_events.go
2025-08-02 02:54:21 +08:00

213 lines
6.7 KiB
Go

package events
import (
"encoding/json"
"time"
"tyapi-server/internal/domains/finance/value_objects"
"github.com/google/uuid"
"github.com/shopspring/decimal"
)
// BaseEvent 基础事件结构
type BaseEvent struct {
ID string `json:"id"`
Type string `json:"type"`
Version string `json:"version"`
Timestamp time.Time `json:"timestamp"`
Source string `json:"source"`
AggregateID string `json:"aggregate_id"`
AggregateType string `json:"aggregate_type"`
Metadata map[string]interface{} `json:"metadata"`
}
// NewBaseEvent 创建基础事件
func NewBaseEvent(eventType, aggregateID, aggregateType string) BaseEvent {
return BaseEvent{
ID: uuid.New().String(),
Type: eventType,
Version: "1.0",
Timestamp: time.Now(),
Source: "finance-domain",
AggregateID: aggregateID,
AggregateType: aggregateType,
Metadata: make(map[string]interface{}),
}
}
// GetID 获取事件ID
func (e BaseEvent) GetID() string {
return e.ID
}
// GetType 获取事件类型
func (e BaseEvent) GetType() string {
return e.Type
}
// GetVersion 获取事件版本
func (e BaseEvent) GetVersion() string {
return e.Version
}
// GetTimestamp 获取事件时间戳
func (e BaseEvent) GetTimestamp() time.Time {
return e.Timestamp
}
// GetSource 获取事件来源
func (e BaseEvent) GetSource() string {
return e.Source
}
// GetAggregateID 获取聚合根ID
func (e BaseEvent) GetAggregateID() string {
return e.AggregateID
}
// GetAggregateType 获取聚合根类型
func (e BaseEvent) GetAggregateType() string {
return e.AggregateType
}
// GetMetadata 获取事件元数据
func (e BaseEvent) GetMetadata() map[string]interface{} {
return e.Metadata
}
// Marshal 序列化事件
func (e BaseEvent) Marshal() ([]byte, error) {
return json.Marshal(e)
}
// Unmarshal 反序列化事件
func (e BaseEvent) Unmarshal(data []byte) error {
return json.Unmarshal(data, e)
}
// InvoiceApplicationCreatedEvent 发票申请创建事件
type InvoiceApplicationCreatedEvent struct {
BaseEvent
ApplicationID string `json:"application_id"`
UserID string `json:"user_id"`
InvoiceType value_objects.InvoiceType `json:"invoice_type"`
Amount decimal.Decimal `json:"amount"`
CompanyName string `json:"company_name"`
ReceivingEmail string `json:"receiving_email"`
CreatedAt time.Time `json:"created_at"`
}
// NewInvoiceApplicationCreatedEvent 创建发票申请创建事件
func NewInvoiceApplicationCreatedEvent(applicationID, userID string, invoiceType value_objects.InvoiceType, amount decimal.Decimal, companyName, receivingEmail string) *InvoiceApplicationCreatedEvent {
event := &InvoiceApplicationCreatedEvent{
BaseEvent: NewBaseEvent("InvoiceApplicationCreated", applicationID, "InvoiceApplication"),
ApplicationID: applicationID,
UserID: userID,
InvoiceType: invoiceType,
Amount: amount,
CompanyName: companyName,
ReceivingEmail: receivingEmail,
CreatedAt: time.Now(),
}
return event
}
// GetPayload 获取事件载荷
func (e *InvoiceApplicationCreatedEvent) GetPayload() interface{} {
return e
}
// InvoiceApplicationApprovedEvent 发票申请通过事件
type InvoiceApplicationApprovedEvent struct {
BaseEvent
ApplicationID string `json:"application_id"`
UserID string `json:"user_id"`
Amount decimal.Decimal `json:"amount"`
ReceivingEmail string `json:"receiving_email"`
ApprovedAt time.Time `json:"approved_at"`
}
// NewInvoiceApplicationApprovedEvent 创建发票申请通过事件
func NewInvoiceApplicationApprovedEvent(applicationID, userID string, amount decimal.Decimal, receivingEmail string) *InvoiceApplicationApprovedEvent {
event := &InvoiceApplicationApprovedEvent{
BaseEvent: NewBaseEvent("InvoiceApplicationApproved", applicationID, "InvoiceApplication"),
ApplicationID: applicationID,
UserID: userID,
Amount: amount,
ReceivingEmail: receivingEmail,
ApprovedAt: time.Now(),
}
return event
}
// GetPayload 获取事件载荷
func (e *InvoiceApplicationApprovedEvent) GetPayload() interface{} {
return e
}
// InvoiceApplicationRejectedEvent 发票申请拒绝事件
type InvoiceApplicationRejectedEvent struct {
BaseEvent
ApplicationID string `json:"application_id"`
UserID string `json:"user_id"`
Reason string `json:"reason"`
ReceivingEmail string `json:"receiving_email"`
RejectedAt time.Time `json:"rejected_at"`
}
// NewInvoiceApplicationRejectedEvent 创建发票申请拒绝事件
func NewInvoiceApplicationRejectedEvent(applicationID, userID, reason, receivingEmail string) *InvoiceApplicationRejectedEvent {
event := &InvoiceApplicationRejectedEvent{
BaseEvent: NewBaseEvent("InvoiceApplicationRejected", applicationID, "InvoiceApplication"),
ApplicationID: applicationID,
UserID: userID,
Reason: reason,
ReceivingEmail: receivingEmail,
RejectedAt: time.Now(),
}
return event
}
// GetPayload 获取事件载荷
func (e *InvoiceApplicationRejectedEvent) GetPayload() interface{} {
return e
}
// InvoiceFileUploadedEvent 发票文件上传事件
type InvoiceFileUploadedEvent struct {
BaseEvent
InvoiceID string `json:"invoice_id"`
UserID string `json:"user_id"`
FileID string `json:"file_id"`
FileName string `json:"file_name"`
FileURL string `json:"file_url"`
ReceivingEmail string `json:"receiving_email"`
CompanyName string `json:"company_name"`
Amount decimal.Decimal `json:"amount"`
InvoiceType value_objects.InvoiceType `json:"invoice_type"`
UploadedAt time.Time `json:"uploaded_at"`
}
// NewInvoiceFileUploadedEvent 创建发票文件上传事件
func NewInvoiceFileUploadedEvent(invoiceID, userID, fileID, fileName, fileURL, receivingEmail, companyName string, amount decimal.Decimal, invoiceType value_objects.InvoiceType) *InvoiceFileUploadedEvent {
event := &InvoiceFileUploadedEvent{
BaseEvent: NewBaseEvent("InvoiceFileUploaded", invoiceID, "InvoiceApplication"),
InvoiceID: invoiceID,
UserID: userID,
FileID: fileID,
FileName: fileName,
FileURL: fileURL,
ReceivingEmail: receivingEmail,
CompanyName: companyName,
Amount: amount,
InvoiceType: invoiceType,
UploadedAt: time.Now(),
}
return event
}
// GetPayload 获取事件载荷
func (e *InvoiceFileUploadedEvent) GetPayload() interface{} {
return e
}