230 lines
		
	
	
		
			7.8 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			230 lines
		
	
	
		
			7.8 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package events
 | |
| 
 | |
| import (
 | |
| 	"encoding/json"
 | |
| 	"time"
 | |
| 
 | |
| 	"tyapi-server/internal/domains/certification/entities"
 | |
| 
 | |
| 	"github.com/google/uuid"
 | |
| )
 | |
| 
 | |
| // 事件类型常量
 | |
| const (
 | |
| 	EventTypeCertificationCreated        = "certification.created"
 | |
| 	EventTypeEnterpriseInfoSubmitted     = "enterprise.info.submitted"
 | |
| 	EventTypeEnterpriseVerified          = "enterprise.verified"
 | |
| 	EventTypeContractApplied             = "contract.applied"
 | |
| 	EventTypeContractSigned              = "contract.signed"
 | |
| 	EventTypeCertificationCompleted      = "certification.completed"
 | |
| )
 | |
| 
 | |
| // BaseCertificationEvent 认证事件基础结构
 | |
| type BaseCertificationEvent 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"`
 | |
| 	Payload       interface{}            `json:"payload"`
 | |
| }
 | |
| 
 | |
| // 实现 DomainEvent 接口
 | |
| func (e *BaseCertificationEvent) GetID() string                       { return e.ID }
 | |
| func (e *BaseCertificationEvent) GetType() string                     { return e.Type }
 | |
| func (e *BaseCertificationEvent) GetVersion() string                  { return e.Version }
 | |
| func (e *BaseCertificationEvent) GetTimestamp() time.Time             { return e.Timestamp }
 | |
| func (e *BaseCertificationEvent) GetSource() string                   { return e.Source }
 | |
| func (e *BaseCertificationEvent) GetAggregateID() string              { return e.AggregateID }
 | |
| func (e *BaseCertificationEvent) GetAggregateType() string            { return e.AggregateType }
 | |
| func (e *BaseCertificationEvent) GetPayload() interface{}             { return e.Payload }
 | |
| func (e *BaseCertificationEvent) GetMetadata() map[string]interface{} { return e.Metadata }
 | |
| func (e *BaseCertificationEvent) Marshal() ([]byte, error)            { return json.Marshal(e) }
 | |
| func (e *BaseCertificationEvent) Unmarshal(data []byte) error         { return json.Unmarshal(data, e) }
 | |
| func (e *BaseCertificationEvent) GetDomainVersion() string            { return e.Version }
 | |
| func (e *BaseCertificationEvent) GetCausationID() string              { return e.ID }
 | |
| func (e *BaseCertificationEvent) GetCorrelationID() string            { return e.ID }
 | |
| 
 | |
| // NewBaseCertificationEvent 创建基础认证事件
 | |
| func NewBaseCertificationEvent(eventType, aggregateID string, payload interface{}) *BaseCertificationEvent {
 | |
| 	return &BaseCertificationEvent{
 | |
| 		ID:            generateEventID(),
 | |
| 		Type:          eventType,
 | |
| 		Version:       "1.0",
 | |
| 		Timestamp:     time.Now(),
 | |
| 		Source:        "certification-service",
 | |
| 		AggregateID:   aggregateID,
 | |
| 		AggregateType: "Certification",
 | |
| 		Metadata:      make(map[string]interface{}),
 | |
| 		Payload:       payload,
 | |
| 	}
 | |
| }
 | |
| 
 | |
| // CertificationCreatedEvent 认证申请创建事件
 | |
| type CertificationCreatedEvent struct {
 | |
| 	*BaseCertificationEvent
 | |
| 	Data struct {
 | |
| 		CertificationID string `json:"certification_id"`
 | |
| 		UserID          string `json:"user_id"`
 | |
| 		Status          string `json:"status"`
 | |
| 	} `json:"data"`
 | |
| }
 | |
| 
 | |
| // NewCertificationCreatedEvent 创建认证申请创建事件
 | |
| func NewCertificationCreatedEvent(certification *entities.Certification) *CertificationCreatedEvent {
 | |
| 	event := &CertificationCreatedEvent{
 | |
| 		BaseCertificationEvent: NewBaseCertificationEvent(
 | |
| 			EventTypeCertificationCreated,
 | |
| 			certification.ID,
 | |
| 			nil,
 | |
| 		),
 | |
| 	}
 | |
| 	event.Data.CertificationID = certification.ID
 | |
| 	event.Data.UserID = certification.UserID
 | |
| 	event.Data.Status = string(certification.Status)
 | |
| 	event.Payload = event.Data
 | |
| 	return event
 | |
| }
 | |
| 
 | |
| // EnterpriseInfoSubmittedEvent 企业信息提交事件
 | |
| type EnterpriseInfoSubmittedEvent struct {
 | |
| 	*BaseCertificationEvent
 | |
| 	Data struct {
 | |
| 		CertificationID string `json:"certification_id"`
 | |
| 		UserID          string `json:"user_id"`
 | |
| 		Status          string `json:"status"`
 | |
| 	} `json:"data"`
 | |
| }
 | |
| 
 | |
| // NewEnterpriseInfoSubmittedEvent 创建企业信息提交事件
 | |
| func NewEnterpriseInfoSubmittedEvent(certification *entities.Certification) *EnterpriseInfoSubmittedEvent {
 | |
| 	event := &EnterpriseInfoSubmittedEvent{
 | |
| 		BaseCertificationEvent: NewBaseCertificationEvent(
 | |
| 			EventTypeEnterpriseInfoSubmitted,
 | |
| 			certification.ID,
 | |
| 			nil,
 | |
| 		),
 | |
| 	}
 | |
| 	event.Data.CertificationID = certification.ID
 | |
| 	event.Data.UserID = certification.UserID
 | |
| 	event.Data.Status = string(certification.Status)
 | |
| 	event.Payload = event.Data
 | |
| 	return event
 | |
| }
 | |
| 
 | |
| // EnterpriseVerifiedEvent 企业认证完成事件
 | |
| type EnterpriseVerifiedEvent struct {
 | |
| 	*BaseCertificationEvent
 | |
| 	Data struct {
 | |
| 		CertificationID string `json:"certification_id"`
 | |
| 		UserID          string `json:"user_id"`
 | |
| 		Status          string `json:"status"`
 | |
| 	} `json:"data"`
 | |
| }
 | |
| 
 | |
| // NewEnterpriseVerifiedEvent 创建企业认证完成事件
 | |
| func NewEnterpriseVerifiedEvent(certification *entities.Certification) *EnterpriseVerifiedEvent {
 | |
| 	event := &EnterpriseVerifiedEvent{
 | |
| 		BaseCertificationEvent: NewBaseCertificationEvent(
 | |
| 			EventTypeEnterpriseVerified,
 | |
| 			certification.ID,
 | |
| 			nil,
 | |
| 		),
 | |
| 	}
 | |
| 	event.Data.CertificationID = certification.ID
 | |
| 	event.Data.UserID = certification.UserID
 | |
| 	event.Data.Status = string(certification.Status)
 | |
| 	event.Payload = event.Data
 | |
| 	return event
 | |
| }
 | |
| 
 | |
| // ContractAppliedEvent 合同申请事件
 | |
| type ContractAppliedEvent struct {
 | |
| 	*BaseCertificationEvent
 | |
| 	Data struct {
 | |
| 		CertificationID string `json:"certification_id"`
 | |
| 		UserID          string `json:"user_id"`
 | |
| 		Status          string `json:"status"`
 | |
| 	} `json:"data"`
 | |
| }
 | |
| 
 | |
| // NewContractAppliedEvent 创建合同申请事件
 | |
| func NewContractAppliedEvent(certification *entities.Certification) *ContractAppliedEvent {
 | |
| 	event := &ContractAppliedEvent{
 | |
| 		BaseCertificationEvent: NewBaseCertificationEvent(
 | |
| 			EventTypeContractApplied,
 | |
| 			certification.ID,
 | |
| 			nil,
 | |
| 		),
 | |
| 	}
 | |
| 	event.Data.CertificationID = certification.ID
 | |
| 	event.Data.UserID = certification.UserID
 | |
| 	event.Data.Status = string(certification.Status)
 | |
| 	event.Payload = event.Data
 | |
| 	return event
 | |
| }
 | |
| 
 | |
| // ContractSignedEvent 合同签署事件
 | |
| type ContractSignedEvent struct {
 | |
| 	*BaseCertificationEvent
 | |
| 	Data struct {
 | |
| 		CertificationID string `json:"certification_id"`
 | |
| 		UserID          string `json:"user_id"`
 | |
| 		ContractURL     string `json:"contract_url"`
 | |
| 		Status          string `json:"status"`
 | |
| 	} `json:"data"`
 | |
| }
 | |
| 
 | |
| // NewContractSignedEvent 创建合同签署事件
 | |
| func NewContractSignedEvent(certification *entities.Certification, contractURL string) *ContractSignedEvent {
 | |
| 	event := &ContractSignedEvent{
 | |
| 		BaseCertificationEvent: NewBaseCertificationEvent(
 | |
| 			EventTypeContractSigned,
 | |
| 			certification.ID,
 | |
| 			nil,
 | |
| 		),
 | |
| 	}
 | |
| 	event.Data.CertificationID = certification.ID
 | |
| 	event.Data.UserID = certification.UserID
 | |
| 	event.Data.ContractURL = contractURL
 | |
| 	event.Data.Status = string(certification.Status)
 | |
| 	event.Payload = event.Data
 | |
| 	return event
 | |
| }
 | |
| 
 | |
| // CertificationCompletedEvent 认证完成事件
 | |
| type CertificationCompletedEvent struct {
 | |
| 	*BaseCertificationEvent
 | |
| 	Data struct {
 | |
| 		CertificationID string `json:"certification_id"`
 | |
| 		UserID          string `json:"user_id"`
 | |
| 		CompletedAt     string `json:"completed_at"`
 | |
| 		Status          string `json:"status"`
 | |
| 	} `json:"data"`
 | |
| }
 | |
| 
 | |
| // NewCertificationCompletedEvent 创建认证完成事件
 | |
| func NewCertificationCompletedEvent(certification *entities.Certification) *CertificationCompletedEvent {
 | |
| 	event := &CertificationCompletedEvent{
 | |
| 		BaseCertificationEvent: NewBaseCertificationEvent(
 | |
| 			EventTypeCertificationCompleted,
 | |
| 			certification.ID,
 | |
| 			nil,
 | |
| 		),
 | |
| 	}
 | |
| 	event.Data.CertificationID = certification.ID
 | |
| 	event.Data.UserID = certification.UserID
 | |
| 	event.Data.CompletedAt = time.Now().Format(time.RFC3339)
 | |
| 	event.Data.Status = string(certification.Status)
 | |
| 	event.Payload = event.Data
 | |
| 	return event
 | |
| }
 | |
| 
 | |
| // 工具函数
 | |
| func generateEventID() string {
 | |
| 	return uuid.New().String()
 | |
| }
 |