527 lines
18 KiB
Go
527 lines
18 KiB
Go
package events
|
|
|
|
import (
|
|
"encoding/json"
|
|
"time"
|
|
|
|
"tyapi-server/internal/domains/certification/entities"
|
|
)
|
|
|
|
// 认证事件类型常量
|
|
const (
|
|
EventTypeCertificationCreated = "certification.created"
|
|
EventTypeCertificationSubmitted = "certification.submitted"
|
|
EventTypeLicenseUploaded = "certification.license.uploaded"
|
|
EventTypeOCRCompleted = "certification.ocr.completed"
|
|
EventTypeEnterpriseInfoConfirmed = "certification.enterprise.confirmed"
|
|
EventTypeFaceVerifyInitiated = "certification.face_verify.initiated"
|
|
EventTypeFaceVerifyCompleted = "certification.face_verify.completed"
|
|
EventTypeContractRequested = "certification.contract.requested"
|
|
EventTypeContractGenerated = "certification.contract.generated"
|
|
EventTypeContractSigned = "certification.contract.signed"
|
|
EventTypeCertificationApproved = "certification.approved"
|
|
EventTypeCertificationRejected = "certification.rejected"
|
|
EventTypeWalletCreated = "certification.wallet.created"
|
|
EventTypeCertificationCompleted = "certification.completed"
|
|
EventTypeCertificationFailed = "certification.failed"
|
|
)
|
|
|
|
// 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"`
|
|
}
|
|
|
|
// 实现 Event 接口
|
|
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-domain",
|
|
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
|
|
}
|
|
|
|
// CertificationSubmittedEvent 认证提交事件
|
|
type CertificationSubmittedEvent struct {
|
|
*BaseCertificationEvent
|
|
Data struct {
|
|
CertificationID string `json:"certification_id"`
|
|
UserID string `json:"user_id"`
|
|
Status string `json:"status"`
|
|
} `json:"data"`
|
|
}
|
|
|
|
// NewCertificationSubmittedEvent 创建认证提交事件
|
|
func NewCertificationSubmittedEvent(certification *entities.Certification) *CertificationSubmittedEvent {
|
|
event := &CertificationSubmittedEvent{
|
|
BaseCertificationEvent: NewBaseCertificationEvent(
|
|
EventTypeCertificationSubmitted,
|
|
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
|
|
}
|
|
|
|
// LicenseUploadedEvent 营业执照上传事件
|
|
type LicenseUploadedEvent struct {
|
|
*BaseCertificationEvent
|
|
Data struct {
|
|
CertificationID string `json:"certification_id"`
|
|
UserID string `json:"user_id"`
|
|
FileURL string `json:"file_url"`
|
|
FileName string `json:"file_name"`
|
|
FileSize int64 `json:"file_size"`
|
|
Status string `json:"status"`
|
|
} `json:"data"`
|
|
}
|
|
|
|
// NewLicenseUploadedEvent 创建营业执照上传事件
|
|
func NewLicenseUploadedEvent(certification *entities.Certification, record *entities.LicenseUploadRecord) *LicenseUploadedEvent {
|
|
event := &LicenseUploadedEvent{
|
|
BaseCertificationEvent: NewBaseCertificationEvent(
|
|
EventTypeLicenseUploaded,
|
|
certification.ID,
|
|
nil,
|
|
),
|
|
}
|
|
event.Data.CertificationID = certification.ID
|
|
event.Data.UserID = certification.UserID
|
|
event.Data.FileURL = record.FileURL
|
|
event.Data.FileName = record.OriginalFileName
|
|
event.Data.FileSize = record.FileSize
|
|
event.Data.Status = string(certification.Status)
|
|
event.Payload = event.Data
|
|
return event
|
|
}
|
|
|
|
// OCRCompletedEvent OCR识别完成事件
|
|
type OCRCompletedEvent struct {
|
|
*BaseCertificationEvent
|
|
Data struct {
|
|
CertificationID string `json:"certification_id"`
|
|
UserID string `json:"user_id"`
|
|
OCRResult map[string]interface{} `json:"ocr_result"`
|
|
Confidence float64 `json:"confidence"`
|
|
Status string `json:"status"`
|
|
} `json:"data"`
|
|
}
|
|
|
|
// NewOCRCompletedEvent 创建OCR识别完成事件
|
|
func NewOCRCompletedEvent(certification *entities.Certification, ocrResult map[string]interface{}, confidence float64) *OCRCompletedEvent {
|
|
event := &OCRCompletedEvent{
|
|
BaseCertificationEvent: NewBaseCertificationEvent(
|
|
EventTypeOCRCompleted,
|
|
certification.ID,
|
|
nil,
|
|
),
|
|
}
|
|
event.Data.CertificationID = certification.ID
|
|
event.Data.UserID = certification.UserID
|
|
event.Data.OCRResult = ocrResult
|
|
event.Data.Confidence = confidence
|
|
event.Data.Status = string(certification.Status)
|
|
event.Payload = event.Data
|
|
return event
|
|
}
|
|
|
|
// EnterpriseInfoConfirmedEvent 企业信息确认事件
|
|
type EnterpriseInfoConfirmedEvent struct {
|
|
*BaseCertificationEvent
|
|
Data struct {
|
|
CertificationID string `json:"certification_id"`
|
|
UserID string `json:"user_id"`
|
|
EnterpriseInfo map[string]interface{} `json:"enterprise_info"`
|
|
Status string `json:"status"`
|
|
} `json:"data"`
|
|
}
|
|
|
|
// NewEnterpriseInfoConfirmedEvent 创建企业信息确认事件
|
|
func NewEnterpriseInfoConfirmedEvent(certification *entities.Certification, enterpriseInfo map[string]interface{}) *EnterpriseInfoConfirmedEvent {
|
|
event := &EnterpriseInfoConfirmedEvent{
|
|
BaseCertificationEvent: NewBaseCertificationEvent(
|
|
EventTypeEnterpriseInfoConfirmed,
|
|
certification.ID,
|
|
nil,
|
|
),
|
|
}
|
|
event.Data.CertificationID = certification.ID
|
|
event.Data.UserID = certification.UserID
|
|
event.Data.EnterpriseInfo = enterpriseInfo
|
|
event.Data.Status = string(certification.Status)
|
|
event.Payload = event.Data
|
|
return event
|
|
}
|
|
|
|
// FaceVerifyInitiatedEvent 人脸识别初始化事件
|
|
type FaceVerifyInitiatedEvent struct {
|
|
*BaseCertificationEvent
|
|
Data struct {
|
|
CertificationID string `json:"certification_id"`
|
|
UserID string `json:"user_id"`
|
|
VerifyToken string `json:"verify_token"`
|
|
Status string `json:"status"`
|
|
} `json:"data"`
|
|
}
|
|
|
|
// NewFaceVerifyInitiatedEvent 创建人脸识别初始化事件
|
|
func NewFaceVerifyInitiatedEvent(certification *entities.Certification, verifyToken string) *FaceVerifyInitiatedEvent {
|
|
event := &FaceVerifyInitiatedEvent{
|
|
BaseCertificationEvent: NewBaseCertificationEvent(
|
|
EventTypeFaceVerifyInitiated,
|
|
certification.ID,
|
|
nil,
|
|
),
|
|
}
|
|
event.Data.CertificationID = certification.ID
|
|
event.Data.UserID = certification.UserID
|
|
event.Data.VerifyToken = verifyToken
|
|
event.Data.Status = string(certification.Status)
|
|
event.Payload = event.Data
|
|
return event
|
|
}
|
|
|
|
// FaceVerifyCompletedEvent 人脸识别完成事件
|
|
type FaceVerifyCompletedEvent struct {
|
|
*BaseCertificationEvent
|
|
Data struct {
|
|
CertificationID string `json:"certification_id"`
|
|
UserID string `json:"user_id"`
|
|
VerifyToken string `json:"verify_token"`
|
|
Success bool `json:"success"`
|
|
Score float64 `json:"score"`
|
|
Status string `json:"status"`
|
|
} `json:"data"`
|
|
}
|
|
|
|
// NewFaceVerifyCompletedEvent 创建人脸识别完成事件
|
|
func NewFaceVerifyCompletedEvent(certification *entities.Certification, record *entities.FaceVerifyRecord) *FaceVerifyCompletedEvent {
|
|
event := &FaceVerifyCompletedEvent{
|
|
BaseCertificationEvent: NewBaseCertificationEvent(
|
|
EventTypeFaceVerifyCompleted,
|
|
certification.ID,
|
|
nil,
|
|
),
|
|
}
|
|
event.Data.CertificationID = certification.ID
|
|
event.Data.UserID = certification.UserID
|
|
event.Data.VerifyToken = record.CertifyID
|
|
event.Data.Success = record.IsSuccess()
|
|
event.Data.Score = record.VerifyScore
|
|
event.Data.Status = string(certification.Status)
|
|
event.Payload = event.Data
|
|
return event
|
|
}
|
|
|
|
// ContractRequestedEvent 合同申请事件
|
|
type ContractRequestedEvent struct {
|
|
*BaseCertificationEvent
|
|
Data struct {
|
|
CertificationID string `json:"certification_id"`
|
|
UserID string `json:"user_id"`
|
|
Status string `json:"status"`
|
|
} `json:"data"`
|
|
}
|
|
|
|
// NewContractRequestedEvent 创建合同申请事件
|
|
func NewContractRequestedEvent(certification *entities.Certification) *ContractRequestedEvent {
|
|
event := &ContractRequestedEvent{
|
|
BaseCertificationEvent: NewBaseCertificationEvent(
|
|
EventTypeContractRequested,
|
|
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
|
|
}
|
|
|
|
// ContractGeneratedEvent 合同生成事件
|
|
type ContractGeneratedEvent struct {
|
|
*BaseCertificationEvent
|
|
Data struct {
|
|
CertificationID string `json:"certification_id"`
|
|
UserID string `json:"user_id"`
|
|
ContractURL string `json:"contract_url"`
|
|
ContractID string `json:"contract_id"`
|
|
Status string `json:"status"`
|
|
} `json:"data"`
|
|
}
|
|
|
|
// NewContractGeneratedEvent 创建合同生成事件
|
|
func NewContractGeneratedEvent(certification *entities.Certification, record *entities.ContractRecord) *ContractGeneratedEvent {
|
|
event := &ContractGeneratedEvent{
|
|
BaseCertificationEvent: NewBaseCertificationEvent(
|
|
EventTypeContractGenerated,
|
|
certification.ID,
|
|
nil,
|
|
),
|
|
}
|
|
event.Data.CertificationID = certification.ID
|
|
event.Data.UserID = certification.UserID
|
|
event.Data.ContractURL = record.ContractURL
|
|
event.Data.ContractID = record.ID
|
|
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"`
|
|
ContractID string `json:"contract_id"`
|
|
SignedAt string `json:"signed_at"`
|
|
Status string `json:"status"`
|
|
} `json:"data"`
|
|
}
|
|
|
|
// NewContractSignedEvent 创建合同签署事件
|
|
func NewContractSignedEvent(certification *entities.Certification, record *entities.ContractRecord) *ContractSignedEvent {
|
|
event := &ContractSignedEvent{
|
|
BaseCertificationEvent: NewBaseCertificationEvent(
|
|
EventTypeContractSigned,
|
|
certification.ID,
|
|
nil,
|
|
),
|
|
}
|
|
event.Data.CertificationID = certification.ID
|
|
event.Data.UserID = certification.UserID
|
|
event.Data.ContractID = record.ID
|
|
event.Data.SignedAt = record.SignedAt.Format(time.RFC3339)
|
|
event.Data.Status = string(certification.Status)
|
|
event.Payload = event.Data
|
|
return event
|
|
}
|
|
|
|
// CertificationApprovedEvent 认证审核通过事件
|
|
type CertificationApprovedEvent struct {
|
|
*BaseCertificationEvent
|
|
Data struct {
|
|
CertificationID string `json:"certification_id"`
|
|
UserID string `json:"user_id"`
|
|
AdminID string `json:"admin_id"`
|
|
ApprovedAt string `json:"approved_at"`
|
|
Status string `json:"status"`
|
|
} `json:"data"`
|
|
}
|
|
|
|
// NewCertificationApprovedEvent 创建认证审核通过事件
|
|
func NewCertificationApprovedEvent(certification *entities.Certification, adminID string) *CertificationApprovedEvent {
|
|
event := &CertificationApprovedEvent{
|
|
BaseCertificationEvent: NewBaseCertificationEvent(
|
|
EventTypeCertificationApproved,
|
|
certification.ID,
|
|
nil,
|
|
),
|
|
}
|
|
event.Data.CertificationID = certification.ID
|
|
event.Data.UserID = certification.UserID
|
|
event.Data.AdminID = adminID
|
|
event.Data.ApprovedAt = time.Now().Format(time.RFC3339)
|
|
event.Data.Status = string(certification.Status)
|
|
event.Payload = event.Data
|
|
return event
|
|
}
|
|
|
|
// CertificationRejectedEvent 认证审核拒绝事件
|
|
type CertificationRejectedEvent struct {
|
|
*BaseCertificationEvent
|
|
Data struct {
|
|
CertificationID string `json:"certification_id"`
|
|
UserID string `json:"user_id"`
|
|
AdminID string `json:"admin_id"`
|
|
RejectReason string `json:"reject_reason"`
|
|
RejectedAt string `json:"rejected_at"`
|
|
Status string `json:"status"`
|
|
} `json:"data"`
|
|
}
|
|
|
|
// NewCertificationRejectedEvent 创建认证审核拒绝事件
|
|
func NewCertificationRejectedEvent(certification *entities.Certification, adminID, rejectReason string) *CertificationRejectedEvent {
|
|
event := &CertificationRejectedEvent{
|
|
BaseCertificationEvent: NewBaseCertificationEvent(
|
|
EventTypeCertificationRejected,
|
|
certification.ID,
|
|
nil,
|
|
),
|
|
}
|
|
event.Data.CertificationID = certification.ID
|
|
event.Data.UserID = certification.UserID
|
|
event.Data.AdminID = adminID
|
|
event.Data.RejectReason = rejectReason
|
|
event.Data.RejectedAt = time.Now().Format(time.RFC3339)
|
|
event.Data.Status = string(certification.Status)
|
|
event.Payload = event.Data
|
|
return event
|
|
}
|
|
|
|
// WalletCreatedEvent 钱包创建事件
|
|
type WalletCreatedEvent struct {
|
|
*BaseCertificationEvent
|
|
Data struct {
|
|
CertificationID string `json:"certification_id"`
|
|
UserID string `json:"user_id"`
|
|
WalletID string `json:"wallet_id"`
|
|
AccessID string `json:"access_id"`
|
|
Status string `json:"status"`
|
|
} `json:"data"`
|
|
}
|
|
|
|
// NewWalletCreatedEvent 创建钱包创建事件
|
|
func NewWalletCreatedEvent(certification *entities.Certification, walletID, accessID string) *WalletCreatedEvent {
|
|
event := &WalletCreatedEvent{
|
|
BaseCertificationEvent: NewBaseCertificationEvent(
|
|
EventTypeWalletCreated,
|
|
certification.ID,
|
|
nil,
|
|
),
|
|
}
|
|
event.Data.CertificationID = certification.ID
|
|
event.Data.UserID = certification.UserID
|
|
event.Data.WalletID = walletID
|
|
event.Data.AccessID = accessID
|
|
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"`
|
|
WalletID string `json:"wallet_id"`
|
|
CompletedAt string `json:"completed_at"`
|
|
Status string `json:"status"`
|
|
} `json:"data"`
|
|
}
|
|
|
|
// NewCertificationCompletedEvent 创建认证完成事件
|
|
func NewCertificationCompletedEvent(certification *entities.Certification, walletID string) *CertificationCompletedEvent {
|
|
event := &CertificationCompletedEvent{
|
|
BaseCertificationEvent: NewBaseCertificationEvent(
|
|
EventTypeCertificationCompleted,
|
|
certification.ID,
|
|
nil,
|
|
),
|
|
}
|
|
event.Data.CertificationID = certification.ID
|
|
event.Data.UserID = certification.UserID
|
|
event.Data.WalletID = walletID
|
|
event.Data.CompletedAt = time.Now().Format(time.RFC3339)
|
|
event.Data.Status = string(certification.Status)
|
|
event.Payload = event.Data
|
|
return event
|
|
}
|
|
|
|
// CertificationFailedEvent 认证失败事件
|
|
type CertificationFailedEvent struct {
|
|
*BaseCertificationEvent
|
|
Data struct {
|
|
CertificationID string `json:"certification_id"`
|
|
UserID string `json:"user_id"`
|
|
FailedAt string `json:"failed_at"`
|
|
FailureReason string `json:"failure_reason"`
|
|
Status string `json:"status"`
|
|
} `json:"data"`
|
|
}
|
|
|
|
// NewCertificationFailedEvent 创建认证失败事件
|
|
func NewCertificationFailedEvent(certification *entities.Certification, failureReason string) *CertificationFailedEvent {
|
|
event := &CertificationFailedEvent{
|
|
BaseCertificationEvent: NewBaseCertificationEvent(
|
|
EventTypeCertificationFailed,
|
|
certification.ID,
|
|
nil,
|
|
),
|
|
}
|
|
event.Data.CertificationID = certification.ID
|
|
event.Data.UserID = certification.UserID
|
|
event.Data.FailedAt = time.Now().Format(time.RFC3339)
|
|
event.Data.FailureReason = failureReason
|
|
event.Data.Status = string(certification.Status)
|
|
event.Payload = event.Data
|
|
return event
|
|
}
|
|
|
|
// generateEventID 生成事件ID
|
|
func generateEventID() string {
|
|
return time.Now().Format("20060102150405") + "-" + generateRandomString(8)
|
|
}
|
|
|
|
// generateRandomString 生成随机字符串
|
|
func generateRandomString(length int) string {
|
|
const charset = "abcdefghijklmnopqrstuvwxyz0123456789"
|
|
b := make([]byte, length)
|
|
for i := range b {
|
|
b[i] = charset[time.Now().UnixNano()%int64(len(charset))]
|
|
}
|
|
return string(b)
|
|
}
|