package services import ( "context" "fmt" "tyapi-server/internal/domains/certification/entities" "tyapi-server/internal/domains/certification/enums" "tyapi-server/internal/domains/certification/repositories" esign_service "tyapi-server/internal/shared/esign" "go.uber.org/zap" ) // CertificationManagementService 认证管理领域服务 // 负责认证申请的生命周期管理,包括创建、状态转换、进度查询等 type CertificationManagementService struct { certRepo repositories.CertificationRepository esignService *esign_service.Client stateMachine *CertificationStateMachine logger *zap.Logger } // NewCertificationManagementService 创建认证管理领域服务 func NewCertificationManagementService( certRepo repositories.CertificationRepository, esignService *esign_service.Client, stateMachine *CertificationStateMachine, logger *zap.Logger, ) *CertificationManagementService { return &CertificationManagementService{ certRepo: certRepo, esignService: esignService, stateMachine: stateMachine, logger: logger, } } // CreateCertification 创建认证申请 func (s *CertificationManagementService) CreateCertification(ctx context.Context, userID string) (*entities.Certification, error) { // 检查用户是否已有认证申请 existingCert, err := s.certRepo.GetByUserID(ctx, userID) if err == nil && existingCert != nil { return nil, fmt.Errorf("用户已有认证申请") } certification := &entities.Certification{ UserID: userID, Status: enums.StatusPending, } createdCert, err := s.certRepo.Create(ctx, *certification) if err != nil { s.logger.Error("创建认证申请失败", zap.Error(err)) return nil, fmt.Errorf("创建认证申请失败: %w", err) } certification = &createdCert s.logger.Info("认证申请创建成功", zap.String("certification_id", certification.ID), zap.String("user_id", userID), ) return certification, nil } // GetCertificationByUserID 根据用户ID获取认证申请 func (s *CertificationManagementService) GetCertificationByUserID(ctx context.Context, userID string) (*entities.Certification, error) { return s.certRepo.GetByUserID(ctx, userID) } // GetCertificationByID 根据ID获取认证申请 func (s *CertificationManagementService) GetCertificationByID(ctx context.Context, certificationID string) (*entities.Certification, error) { cert, err := s.certRepo.GetByID(ctx, certificationID) if err != nil { return nil, err } return &cert, nil } // GetCertificationByAuthFlowID 根据认证流程ID获取认证申请 func (s *CertificationManagementService) GetCertificationByAuthFlowID(ctx context.Context, authFlowID string) (*entities.Certification, error) { cert, err := s.certRepo.GetByAuthFlowID(ctx, authFlowID) if err != nil { return nil, err } return &cert, nil } // 根据签署流程ID获取认证申请 func (s *CertificationManagementService) GetCertificationByEsignFlowID(ctx context.Context, esignFlowID string) (*entities.Certification, error) { cert, err := s.certRepo.GetByEsignFlowID(ctx, esignFlowID) if err != nil { return nil, err } return &cert, nil } // GetCertificationProgress 获取认证进度信息 func (s *CertificationManagementService) GetCertificationProgress(ctx context.Context, certificationID string) (map[string]interface{}, error) { cert, err := s.certRepo.GetByID(ctx, certificationID) if err != nil { return nil, fmt.Errorf("认证申请不存在: %w", err) } progress := map[string]interface{}{ "certification_id": cert.ID, "user_id": cert.UserID, "current_status": cert.Status, "progress_percentage": cert.GetProgressPercentage(), "is_user_action_required": cert.IsUserActionRequired(), "next_valid_statuses": cert.GetNextValidStatuses(), "created_at": cert.CreatedAt, "updated_at": cert.UpdatedAt, } // 添加时间节点信息 if cert.InfoSubmittedAt != nil { progress["info_submitted_at"] = cert.InfoSubmittedAt } if cert.EnterpriseVerifiedAt != nil { progress["enterprise_verified_at"] = cert.EnterpriseVerifiedAt } if cert.ContractAppliedAt != nil { progress["contract_applied_at"] = cert.ContractAppliedAt } if cert.ContractSignedAt != nil { progress["contract_signed_at"] = cert.ContractSignedAt } if cert.CompletedAt != nil { progress["completed_at"] = cert.CompletedAt } return progress, nil } // 通过e签宝检查是否有过认证 func (s *CertificationManagementService) CheckCertification(ctx context.Context, companyName string, unifiedSocialCode string) (bool, error) { // 查询企业是否已经过认证 queryOrgIdentityInfo := &esign_service.QueryOrgIdentityRequest{ OrgName: companyName, OrgIDCardNum: unifiedSocialCode, OrgIDCardType: esign_service.OrgIDCardTypeUSCC, } queryOrgIdentityResponse, err := s.esignService.QueryOrgIdentityInfo(queryOrgIdentityInfo) if err != nil { return false, fmt.Errorf("查询机构认证信息失败: %w", err) } if queryOrgIdentityResponse.Data.RealnameStatus == 1 { s.logger.Info("该企业已进行过认证", zap.String("company_name", companyName), zap.String("unified_social_code", unifiedSocialCode)) return true, nil } return false, nil }