| 
									
										
										
										
											2025-07-11 21:05:58 +08:00
										 |  |  | package events | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"encoding/json" | 
					
						
							|  |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"tyapi-server/internal/domains/certification/entities" | 
					
						
							| 
									
										
										
										
											2025-07-20 20:53:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/google/uuid" | 
					
						
							| 
									
										
										
										
											2025-07-11 21:05:58 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-20 20:53:26 +08:00
										 |  |  | // 事件类型常量 | 
					
						
							| 
									
										
										
										
											2025-07-11 21:05:58 +08:00
										 |  |  | const ( | 
					
						
							| 
									
										
										
										
											2025-07-20 20:53:26 +08:00
										 |  |  | 	EventTypeCertificationCreated        = "certification.created" | 
					
						
							|  |  |  | 	EventTypeEnterpriseInfoSubmitted     = "enterprise.info.submitted" | 
					
						
							|  |  |  | 	EventTypeEnterpriseVerified          = "enterprise.verified" | 
					
						
							|  |  |  | 	EventTypeContractApplied             = "contract.applied" | 
					
						
							|  |  |  | 	EventTypeContractSigned              = "contract.signed" | 
					
						
							|  |  |  | 	EventTypeCertificationCompleted      = "certification.completed" | 
					
						
							| 
									
										
										
										
											2025-07-11 21:05:58 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 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"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-20 20:53:26 +08:00
										 |  |  | // 实现 DomainEvent 接口 | 
					
						
							| 
									
										
										
										
											2025-07-11 21:05:58 +08:00
										 |  |  | 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(), | 
					
						
							| 
									
										
										
										
											2025-07-20 20:53:26 +08:00
										 |  |  | 		Source:        "certification-service", | 
					
						
							| 
									
										
										
										
											2025-07-11 21:05:58 +08:00
										 |  |  | 		AggregateID:   aggregateID, | 
					
						
							| 
									
										
										
										
											2025-07-20 20:53:26 +08:00
										 |  |  | 		AggregateType: "Certification", | 
					
						
							| 
									
										
										
										
											2025-07-11 21:05:58 +08:00
										 |  |  | 		Metadata:      make(map[string]interface{}), | 
					
						
							|  |  |  | 		Payload:       payload, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-20 20:53:26 +08:00
										 |  |  | // CertificationCreatedEvent 认证申请创建事件 | 
					
						
							| 
									
										
										
										
											2025-07-11 21:05:58 +08:00
										 |  |  | type CertificationCreatedEvent struct { | 
					
						
							|  |  |  | 	*BaseCertificationEvent | 
					
						
							|  |  |  | 	Data struct { | 
					
						
							|  |  |  | 		CertificationID string `json:"certification_id"` | 
					
						
							|  |  |  | 		UserID          string `json:"user_id"` | 
					
						
							|  |  |  | 		Status          string `json:"status"` | 
					
						
							|  |  |  | 	} `json:"data"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-20 20:53:26 +08:00
										 |  |  | // NewCertificationCreatedEvent 创建认证申请创建事件 | 
					
						
							| 
									
										
										
										
											2025-07-11 21:05:58 +08:00
										 |  |  | 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 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-20 20:53:26 +08:00
										 |  |  | // EnterpriseInfoSubmittedEvent 企业信息提交事件 | 
					
						
							|  |  |  | type EnterpriseInfoSubmittedEvent struct { | 
					
						
							| 
									
										
										
										
											2025-07-11 21:05:58 +08:00
										 |  |  | 	*BaseCertificationEvent | 
					
						
							|  |  |  | 	Data struct { | 
					
						
							|  |  |  | 		CertificationID string `json:"certification_id"` | 
					
						
							|  |  |  | 		UserID          string `json:"user_id"` | 
					
						
							|  |  |  | 		Status          string `json:"status"` | 
					
						
							|  |  |  | 	} `json:"data"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-20 20:53:26 +08:00
										 |  |  | // NewEnterpriseInfoSubmittedEvent 创建企业信息提交事件 | 
					
						
							|  |  |  | func NewEnterpriseInfoSubmittedEvent(certification *entities.Certification) *EnterpriseInfoSubmittedEvent { | 
					
						
							|  |  |  | 	event := &EnterpriseInfoSubmittedEvent{ | 
					
						
							| 
									
										
										
										
											2025-07-11 21:05:58 +08:00
										 |  |  | 		BaseCertificationEvent: NewBaseCertificationEvent( | 
					
						
							| 
									
										
										
										
											2025-07-20 20:53:26 +08:00
										 |  |  | 			EventTypeEnterpriseInfoSubmitted, | 
					
						
							| 
									
										
										
										
											2025-07-11 21:05:58 +08:00
										 |  |  | 			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 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-20 20:53:26 +08:00
										 |  |  | // EnterpriseVerifiedEvent 企业认证完成事件 | 
					
						
							|  |  |  | type EnterpriseVerifiedEvent struct { | 
					
						
							| 
									
										
										
										
											2025-07-11 21:05:58 +08:00
										 |  |  | 	*BaseCertificationEvent | 
					
						
							|  |  |  | 	Data struct { | 
					
						
							|  |  |  | 		CertificationID string `json:"certification_id"` | 
					
						
							|  |  |  | 		UserID          string `json:"user_id"` | 
					
						
							|  |  |  | 		Status          string `json:"status"` | 
					
						
							|  |  |  | 	} `json:"data"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-20 20:53:26 +08:00
										 |  |  | // NewEnterpriseVerifiedEvent 创建企业认证完成事件 | 
					
						
							|  |  |  | func NewEnterpriseVerifiedEvent(certification *entities.Certification) *EnterpriseVerifiedEvent { | 
					
						
							|  |  |  | 	event := &EnterpriseVerifiedEvent{ | 
					
						
							| 
									
										
										
										
											2025-07-11 21:05:58 +08:00
										 |  |  | 		BaseCertificationEvent: NewBaseCertificationEvent( | 
					
						
							| 
									
										
										
										
											2025-07-20 20:53:26 +08:00
										 |  |  | 			EventTypeEnterpriseVerified, | 
					
						
							| 
									
										
										
										
											2025-07-11 21:05:58 +08:00
										 |  |  | 			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 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-20 20:53:26 +08:00
										 |  |  | // ContractAppliedEvent 合同申请事件 | 
					
						
							|  |  |  | type ContractAppliedEvent struct { | 
					
						
							| 
									
										
										
										
											2025-07-11 21:05:58 +08:00
										 |  |  | 	*BaseCertificationEvent | 
					
						
							|  |  |  | 	Data struct { | 
					
						
							|  |  |  | 		CertificationID string `json:"certification_id"` | 
					
						
							|  |  |  | 		UserID          string `json:"user_id"` | 
					
						
							|  |  |  | 		Status          string `json:"status"` | 
					
						
							|  |  |  | 	} `json:"data"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-20 20:53:26 +08:00
										 |  |  | // NewContractAppliedEvent 创建合同申请事件 | 
					
						
							|  |  |  | func NewContractAppliedEvent(certification *entities.Certification) *ContractAppliedEvent { | 
					
						
							|  |  |  | 	event := &ContractAppliedEvent{ | 
					
						
							| 
									
										
										
										
											2025-07-11 21:05:58 +08:00
										 |  |  | 		BaseCertificationEvent: NewBaseCertificationEvent( | 
					
						
							| 
									
										
										
										
											2025-07-20 20:53:26 +08:00
										 |  |  | 			EventTypeContractApplied, | 
					
						
							| 
									
										
										
										
											2025-07-11 21:05:58 +08:00
										 |  |  | 			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"` | 
					
						
							| 
									
										
										
										
											2025-07-20 20:53:26 +08:00
										 |  |  | 		ContractURL     string `json:"contract_url"` | 
					
						
							| 
									
										
										
										
											2025-07-11 21:05:58 +08:00
										 |  |  | 		Status          string `json:"status"` | 
					
						
							|  |  |  | 	} `json:"data"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // NewContractSignedEvent 创建合同签署事件 | 
					
						
							| 
									
										
										
										
											2025-07-20 20:53:26 +08:00
										 |  |  | func NewContractSignedEvent(certification *entities.Certification, contractURL string) *ContractSignedEvent { | 
					
						
							| 
									
										
										
										
											2025-07-11 21:05:58 +08:00
										 |  |  | 	event := &ContractSignedEvent{ | 
					
						
							|  |  |  | 		BaseCertificationEvent: NewBaseCertificationEvent( | 
					
						
							|  |  |  | 			EventTypeContractSigned, | 
					
						
							|  |  |  | 			certification.ID, | 
					
						
							|  |  |  | 			nil, | 
					
						
							|  |  |  | 		), | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	event.Data.CertificationID = certification.ID | 
					
						
							|  |  |  | 	event.Data.UserID = certification.UserID | 
					
						
							| 
									
										
										
										
											2025-07-20 20:53:26 +08:00
										 |  |  | 	event.Data.ContractURL = contractURL | 
					
						
							| 
									
										
										
										
											2025-07-11 21:05:58 +08:00
										 |  |  | 	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 创建认证完成事件 | 
					
						
							| 
									
										
										
										
											2025-07-20 20:53:26 +08:00
										 |  |  | func NewCertificationCompletedEvent(certification *entities.Certification) *CertificationCompletedEvent { | 
					
						
							| 
									
										
										
										
											2025-07-11 21:05:58 +08:00
										 |  |  | 	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 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-20 20:53:26 +08:00
										 |  |  | // 工具函数 | 
					
						
							| 
									
										
										
										
											2025-07-11 21:05:58 +08:00
										 |  |  | func generateEventID() string { | 
					
						
							| 
									
										
										
										
											2025-07-20 20:53:26 +08:00
										 |  |  | 	return uuid.New().String() | 
					
						
							| 
									
										
										
										
											2025-07-11 21:05:58 +08:00
										 |  |  | } |