v0.1
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
||||
"tyapi-server/internal/domains/certification/entities"
|
||||
"tyapi-server/internal/domains/certification/enums"
|
||||
"tyapi-server/internal/domains/certification/repositories"
|
||||
"tyapi-server/internal/domains/certification/services/state_machine"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
@@ -19,40 +18,34 @@ type CertificationAggregateService interface {
|
||||
CreateCertification(ctx context.Context, userID string) (*entities.Certification, error)
|
||||
LoadCertification(ctx context.Context, certificationID string) (*entities.Certification, error)
|
||||
SaveCertification(ctx context.Context, cert *entities.Certification) error
|
||||
|
||||
// 状态转换管理
|
||||
TransitionState(ctx context.Context, certificationID string, targetStatus enums.CertificationStatus, actor enums.ActorType, actorID string, reason string, metadata map[string]interface{}) (*state_machine.StateTransitionResult, error)
|
||||
ValidateStateTransition(ctx context.Context, certificationID string, targetStatus enums.CertificationStatus, actor enums.ActorType) error
|
||||
|
||||
LoadCertificationByUserID(ctx context.Context, userID string) (*entities.Certification, error)
|
||||
LoadCertificationByAuthFlowId(ctx context.Context, authFlowId string) (*entities.Certification, error)
|
||||
LoadCertificationByEsignFlowId(ctx context.Context, esignFlowId string) (*entities.Certification, error)
|
||||
// 业务规则验证
|
||||
ValidateBusinessRules(ctx context.Context, cert *entities.Certification) error
|
||||
CheckInvariance(ctx context.Context, cert *entities.Certification) error
|
||||
|
||||
// 查询方法
|
||||
GetStateInfo(status enums.CertificationStatus) *state_machine.StateConfig
|
||||
GetValidTransitions(ctx context.Context, certificationID string, actor enums.ActorType) ([]*state_machine.StateTransitionRule, error)
|
||||
ExistsByUserID(ctx context.Context, userID string) (bool, error)
|
||||
}
|
||||
|
||||
// CertificationAggregateServiceImpl 认证聚合服务实现
|
||||
type CertificationAggregateServiceImpl struct {
|
||||
commandRepo repositories.CertificationCommandRepository
|
||||
queryRepo repositories.CertificationQueryRepository
|
||||
stateMachine *state_machine.CertificationStateMachine
|
||||
logger *zap.Logger
|
||||
commandRepo repositories.CertificationCommandRepository
|
||||
queryRepo repositories.CertificationQueryRepository
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
// NewCertificationAggregateService 创建认证聚合服务
|
||||
func NewCertificationAggregateService(
|
||||
commandRepo repositories.CertificationCommandRepository,
|
||||
queryRepo repositories.CertificationQueryRepository,
|
||||
stateMachine *state_machine.CertificationStateMachine,
|
||||
logger *zap.Logger,
|
||||
) CertificationAggregateService {
|
||||
return &CertificationAggregateServiceImpl{
|
||||
commandRepo: commandRepo,
|
||||
queryRepo: queryRepo,
|
||||
stateMachine: stateMachine,
|
||||
logger: logger,
|
||||
commandRepo: commandRepo,
|
||||
queryRepo: queryRepo,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,17 +56,15 @@ func (s *CertificationAggregateServiceImpl) CreateCertification(ctx context.Cont
|
||||
s.logger.Info("创建认证申请", zap.String("user_id", userID))
|
||||
|
||||
// 1. 检查用户是否已有认证申请
|
||||
existingCert, err := s.queryRepo.GetByUserID(ctx, userID)
|
||||
if err == nil && existingCert != nil {
|
||||
// 检查现有认证的状态
|
||||
if !existingCert.IsFinalStatus() {
|
||||
return nil, fmt.Errorf("用户已有进行中的认证申请,请先完成或取消现有申请")
|
||||
}
|
||||
|
||||
s.logger.Info("用户已有完成的认证申请,允许创建新申请",
|
||||
zap.String("user_id", userID),
|
||||
zap.String("existing_cert_id", existingCert.ID),
|
||||
zap.String("existing_status", string(existingCert.Status)))
|
||||
exists, err := s.ExistsByUserID(ctx, userID)
|
||||
if err != nil {
|
||||
s.logger.Error("检查用户认证是否存在失败", zap.Error(err), zap.String("user_id", userID))
|
||||
return nil, fmt.Errorf("检查用户认证是否存在失败: %w", err)
|
||||
}
|
||||
if exists {
|
||||
s.logger.Info("用户已有认证申请,不允许创建新申请",
|
||||
zap.String("user_id", userID))
|
||||
return nil, fmt.Errorf("用户已有认证申请")
|
||||
}
|
||||
|
||||
// 2. 创建新的认证聚合根
|
||||
@@ -122,6 +113,48 @@ func (s *CertificationAggregateServiceImpl) LoadCertification(ctx context.Contex
|
||||
return cert, nil
|
||||
}
|
||||
|
||||
// LoadCertificationByUserID 加载用户认证聚合根
|
||||
func (s *CertificationAggregateServiceImpl) LoadCertificationByUserID(ctx context.Context, userID string) (*entities.Certification, error) {
|
||||
s.logger.Debug("加载用户认证聚合根", zap.String("user_id", userID))
|
||||
|
||||
// 从查询仓储加载
|
||||
cert, err := s.queryRepo.GetByUserID(ctx, userID)
|
||||
if err != nil {
|
||||
s.logger.Error("加载用户认证聚合根失败", zap.Error(err), zap.String("user_id", userID))
|
||||
return nil, fmt.Errorf("认证申请不存在: %w", err)
|
||||
}
|
||||
|
||||
return cert, nil
|
||||
}
|
||||
|
||||
// LoadCertificationByAuthFlowId 加载认证聚合根
|
||||
func (s *CertificationAggregateServiceImpl) LoadCertificationByAuthFlowId(ctx context.Context, authFlowId string) (*entities.Certification, error) {
|
||||
s.logger.Debug("加载认证聚合根", zap.String("auth_flow_id", authFlowId))
|
||||
|
||||
// 从查询仓储加载
|
||||
cert, err := s.queryRepo.FindByAuthFlowID(ctx, authFlowId)
|
||||
if err != nil {
|
||||
s.logger.Error("加载认证聚合根失败", zap.Error(err), zap.String("auth_flow_id", authFlowId))
|
||||
return nil, fmt.Errorf("认证申请不存在: %w", err)
|
||||
}
|
||||
|
||||
return cert, nil
|
||||
}
|
||||
|
||||
// LoadCertificationByEsignFlowId 加载认证聚合根
|
||||
func (s *CertificationAggregateServiceImpl) LoadCertificationByEsignFlowId(ctx context.Context, esignFlowId string) (*entities.Certification, error) {
|
||||
s.logger.Debug("加载认证聚合根", zap.String("esign_flow_id", esignFlowId))
|
||||
|
||||
// 从查询仓储加载
|
||||
cert, err := s.queryRepo.FindByEsignFlowID(ctx, esignFlowId)
|
||||
if err != nil {
|
||||
s.logger.Error("加载认证聚合根失败", zap.Error(err), zap.String("esign_flow_id", esignFlowId))
|
||||
return nil, fmt.Errorf("认证申请不存在: %w", err)
|
||||
}
|
||||
|
||||
return cert, nil
|
||||
}
|
||||
|
||||
// SaveCertification 保存认证聚合根
|
||||
func (s *CertificationAggregateServiceImpl) SaveCertification(ctx context.Context, cert *entities.Certification) error {
|
||||
s.logger.Debug("保存认证聚合根", zap.String("certification_id", cert.ID))
|
||||
@@ -156,74 +189,6 @@ func (s *CertificationAggregateServiceImpl) SaveCertification(ctx context.Contex
|
||||
return nil
|
||||
}
|
||||
|
||||
// ================ 状态转换管理 ================
|
||||
|
||||
// TransitionState 执行状态转换
|
||||
func (s *CertificationAggregateServiceImpl) TransitionState(
|
||||
ctx context.Context,
|
||||
certificationID string,
|
||||
targetStatus enums.CertificationStatus,
|
||||
actor enums.ActorType,
|
||||
actorID string,
|
||||
reason string,
|
||||
metadata map[string]interface{},
|
||||
) (*state_machine.StateTransitionResult, error) {
|
||||
s.logger.Info("执行状态转换",
|
||||
zap.String("certification_id", certificationID),
|
||||
zap.String("target_status", string(targetStatus)),
|
||||
zap.String("actor", string(actor)),
|
||||
zap.String("actor_id", actorID))
|
||||
|
||||
// 构建状态转换请求
|
||||
req := &state_machine.StateTransitionRequest{
|
||||
CertificationID: certificationID,
|
||||
TargetStatus: targetStatus,
|
||||
Actor: actor,
|
||||
ActorID: actorID,
|
||||
Reason: reason,
|
||||
Context: metadata,
|
||||
AllowRollback: true,
|
||||
}
|
||||
|
||||
// 执行状态转换
|
||||
result, err := s.stateMachine.ExecuteTransition(ctx, req)
|
||||
if err != nil {
|
||||
s.logger.Error("状态转换执行失败",
|
||||
zap.String("certification_id", certificationID),
|
||||
zap.Error(err))
|
||||
return result, err
|
||||
}
|
||||
|
||||
s.logger.Info("状态转换执行成功",
|
||||
zap.String("certification_id", certificationID),
|
||||
zap.String("from_status", string(result.OldStatus)),
|
||||
zap.String("to_status", string(result.NewStatus)))
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// ValidateStateTransition 验证状态转换
|
||||
func (s *CertificationAggregateServiceImpl) ValidateStateTransition(
|
||||
ctx context.Context,
|
||||
certificationID string,
|
||||
targetStatus enums.CertificationStatus,
|
||||
actor enums.ActorType,
|
||||
) error {
|
||||
// 加载认证聚合根
|
||||
cert, err := s.LoadCertification(ctx, certificationID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 检查是否可以转换
|
||||
canTransition, message := s.stateMachine.CanTransition(cert, targetStatus, actor)
|
||||
if !canTransition {
|
||||
return fmt.Errorf("状态转换验证失败: %s", message)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ================ 业务规则验证 ================
|
||||
|
||||
// ValidateBusinessRules 验证业务规则
|
||||
@@ -280,26 +245,9 @@ func (s *CertificationAggregateServiceImpl) CheckInvariance(ctx context.Context,
|
||||
|
||||
// ================ 查询方法 ================
|
||||
|
||||
// GetStateInfo 获取状态信息
|
||||
func (s *CertificationAggregateServiceImpl) GetStateInfo(status enums.CertificationStatus) *state_machine.StateConfig {
|
||||
return s.stateMachine.GetStateInfo(status)
|
||||
}
|
||||
|
||||
// GetValidTransitions 获取有效的状态转换
|
||||
func (s *CertificationAggregateServiceImpl) GetValidTransitions(
|
||||
ctx context.Context,
|
||||
certificationID string,
|
||||
actor enums.ActorType,
|
||||
) ([]*state_machine.StateTransitionRule, error) {
|
||||
// 加载认证聚合根
|
||||
cert, err := s.LoadCertification(ctx, certificationID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 获取有效转换
|
||||
transitions := s.stateMachine.GetValidTransitions(cert, actor)
|
||||
return transitions, nil
|
||||
// Exists 判断认证是否存在
|
||||
func (s *CertificationAggregateServiceImpl) ExistsByUserID(ctx context.Context, userID string) (bool, error) {
|
||||
return s.queryRepo.ExistsByUserID(ctx, userID)
|
||||
}
|
||||
|
||||
// ================ 私有方法 ================
|
||||
@@ -344,6 +292,17 @@ func (s *CertificationAggregateServiceImpl) validateStatusInvariance(cert *entit
|
||||
if cert.ContractSignedAt == nil {
|
||||
return fmt.Errorf("合同签署状态下必须有签署完成时间")
|
||||
}
|
||||
|
||||
case enums.StatusCompleted:
|
||||
if cert.ContractFileID == "" || cert.EsignFlowID == "" || cert.ContractURL == "" {
|
||||
return fmt.Errorf("认证完成状态下必须有完整的合同信息")
|
||||
}
|
||||
if cert.ContractSignedAt == nil {
|
||||
return fmt.Errorf("认证完成状态下必须有合同签署时间")
|
||||
}
|
||||
if cert.CompletedAt == nil {
|
||||
return fmt.Errorf("认证完成状态下必须有完成时间")
|
||||
}
|
||||
}
|
||||
|
||||
// 失败状态检查
|
||||
@@ -380,5 +339,11 @@ func (s *CertificationAggregateServiceImpl) validateTimestampInvariance(cert *en
|
||||
}
|
||||
}
|
||||
|
||||
if cert.ContractSignedAt != nil && cert.CompletedAt != nil {
|
||||
if cert.ContractSignedAt.After(*cert.CompletedAt) {
|
||||
return fmt.Errorf("合同签署时间不能晚于认证完成时间")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user