152 lines
4.4 KiB
Go
152 lines
4.4 KiB
Go
package certification
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
|
|
"hyapi-server/internal/domains/certification/entities"
|
|
"hyapi-server/internal/domains/certification/ports"
|
|
|
|
"go.uber.org/zap"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// resolveCorpAuthQueryForGet 构造 /corp/get 入参(三选一)。
|
|
// 优先级:库中 openCorpId > corpIdentNo(USCC) > 库中 clientCorpId
|
|
func (s *CertificationApplicationServiceImpl) resolveCorpAuthQueryForGet(
|
|
ctx context.Context,
|
|
userID, uscc, fallbackClientCorpID string,
|
|
) *ports.CorpAuthStatusQuery {
|
|
uscc = strings.TrimSpace(uscc)
|
|
fallbackClientCorpID = strings.TrimSpace(fallbackClientCorpID)
|
|
rec := s.loadFadadaCorpAuthRecord(ctx, userID, uscc, fallbackClientCorpID)
|
|
|
|
q := &ports.CorpAuthStatusQuery{}
|
|
if rec != nil && strings.TrimSpace(rec.OpenCorpID) != "" {
|
|
q.OpenCorpID = strings.TrimSpace(rec.OpenCorpID)
|
|
return q
|
|
}
|
|
if uscc != "" {
|
|
q.CorpIdentNo = uscc
|
|
return q
|
|
}
|
|
if rec != nil && strings.TrimSpace(rec.ClientCorpID) != "" {
|
|
q.ClientCorpID = strings.TrimSpace(rec.ClientCorpID)
|
|
return q
|
|
}
|
|
if fallbackClientCorpID != "" {
|
|
q.ClientCorpID = fallbackClientCorpID
|
|
}
|
|
return q
|
|
}
|
|
|
|
func (s *CertificationApplicationServiceImpl) loadFadadaCorpAuthRecord(
|
|
ctx context.Context,
|
|
userID, uscc, clientCorpID string,
|
|
) *entities.FadadaCorpAuthRecord {
|
|
if s.fadadaCorpAuthRecordRepo == nil {
|
|
return nil
|
|
}
|
|
if userID != "" {
|
|
if rec, err := s.fadadaCorpAuthRecordRepo.FindLatestByUserID(ctx, userID); err == nil && rec != nil {
|
|
return rec
|
|
} else if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
s.logger.Warn("按用户查询法大大授权记录失败", zap.String("user_id", userID), zap.Error(err))
|
|
}
|
|
}
|
|
if uscc != "" {
|
|
if rec, err := s.fadadaCorpAuthRecordRepo.FindByUnifiedSocialCode(ctx, uscc); err == nil && rec != nil {
|
|
return rec
|
|
} else if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
s.logger.Warn("按信用代码查询法大大授权记录失败", zap.String("uscc", uscc), zap.Error(err))
|
|
}
|
|
}
|
|
if clientCorpID != "" {
|
|
if rec, err := s.fadadaCorpAuthRecordRepo.FindByClientCorpID(ctx, clientCorpID); err == nil && rec != nil {
|
|
return rec
|
|
} else if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
s.logger.Warn("按clientCorpId查询法大大授权记录失败", zap.String("client_corp_id", clientCorpID), zap.Error(err))
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *CertificationApplicationServiceImpl) fadadaAppID() string {
|
|
if s.config != nil {
|
|
return strings.TrimSpace(s.config.Fadada.AppID)
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func (s *CertificationApplicationServiceImpl) ensureFadadaCorpAuthSeed(
|
|
ctx context.Context,
|
|
cert *entities.Certification,
|
|
record *entities.EnterpriseInfoSubmitRecord,
|
|
clientCorpID string,
|
|
) {
|
|
if s.fadadaCorpAuthRecordRepo == nil || cert == nil || record == nil {
|
|
return
|
|
}
|
|
seed := entities.NewFadadaCorpAuthRecord(
|
|
cert.UserID,
|
|
cert.ID,
|
|
s.fadadaAppID(),
|
|
record.CompanyName,
|
|
record.UnifiedSocialCode,
|
|
clientCorpID,
|
|
)
|
|
seed.Source = "select_sign_platform"
|
|
if err := s.fadadaCorpAuthRecordRepo.UpsertByBizKey(ctx, seed); err != nil {
|
|
s.logger.Warn("初始化法大大授权记录失败", zap.Error(err), zap.String("user_id", cert.UserID))
|
|
}
|
|
}
|
|
|
|
func (s *CertificationApplicationServiceImpl) persistFadadaCorpAuthStatus(
|
|
ctx context.Context,
|
|
cert *entities.Certification,
|
|
record *entities.EnterpriseInfoSubmitRecord,
|
|
status *ports.CorpAuthStatusResult,
|
|
source string,
|
|
) {
|
|
if s.fadadaCorpAuthRecordRepo == nil || status == nil || !status.Found {
|
|
return
|
|
}
|
|
userID, certID, company, uscc := "", "", "", ""
|
|
if cert != nil {
|
|
userID = cert.UserID
|
|
certID = cert.ID
|
|
}
|
|
if record != nil {
|
|
company = record.CompanyName
|
|
uscc = record.UnifiedSocialCode
|
|
if userID == "" {
|
|
userID = record.UserID
|
|
}
|
|
}
|
|
rec := entities.NewFadadaCorpAuthRecord(userID, certID, s.fadadaAppID(), company, uscc, status.ClientCorpID)
|
|
rec.ApplyAuthStatus(
|
|
status.OpenCorpID,
|
|
status.ClientCorpID,
|
|
status.BindingStatus,
|
|
status.IdentStatus,
|
|
"",
|
|
nil,
|
|
source,
|
|
)
|
|
if err := s.fadadaCorpAuthRecordRepo.UpsertByBizKey(ctx, rec); err != nil {
|
|
s.logger.Warn("保存法大大授权状态失败", zap.Error(err), zap.String("user_id", userID), zap.String("source", source))
|
|
}
|
|
}
|
|
|
|
func (s *CertificationApplicationServiceImpl) partyAOpenCorpIDFromStore(
|
|
ctx context.Context,
|
|
userID, uscc string,
|
|
) string {
|
|
rec := s.loadFadadaCorpAuthRecord(ctx, userID, uscc, uscc)
|
|
if rec == nil {
|
|
return ""
|
|
}
|
|
return strings.TrimSpace(rec.OpenCorpID)
|
|
}
|