From 3f4c3086f3396bcb9bea449dc9a757c2709cd5dd Mon Sep 17 00:00:00 2001 From: liangzai <2440983361@qq.com> Date: Mon, 4 Aug 2025 22:02:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=83=A8=E5=88=86=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E5=90=88=E5=90=8C=E6=8E=88=E6=9D=83=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/api_application_service.go | 24 +++++++++++++++---- .../api/services/api_request_service.go | 6 ++--- .../api/services/processors/dependencies.go | 13 ++++++++++ .../processors/flxg/flxg0v4b_processor.go | 7 ++++-- .../processors/qygl/qygl8271_processor.go | 6 ++++- .../domains/user/entities/contract_info.go | 22 ++++++++--------- 6 files changed, 57 insertions(+), 21 deletions(-) diff --git a/internal/application/api/api_application_service.go b/internal/application/api/api_application_service.go index 458cf31..7c3f301 100644 --- a/internal/application/api/api_application_service.go +++ b/internal/application/api/api_application_service.go @@ -34,7 +34,7 @@ type ApiApplicationService interface { // 获取用户API调用记录 GetUserApiCalls(ctx context.Context, userID string, filters map[string]interface{}, options interfaces.ListOptions) (*dto.ApiCallListResponse, error) - + // 管理端API调用记录 GetAdminApiCalls(ctx context.Context, filters map[string]interface{}, options interfaces.ListOptions) (*dto.ApiCallListResponse, error) } @@ -45,6 +45,7 @@ type ApiApplicationServiceImpl struct { apiRequestService *services.ApiRequestService apiCallRepository repositories.ApiCallRepository walletService finance_services.WalletAggregateService + contractInfoService user_repositories.ContractInfoRepository productManagementService *product_services.ProductManagementService productSubscriptionService *product_services.ProductSubscriptionService userRepo user_repositories.UserRepository @@ -53,8 +54,8 @@ type ApiApplicationServiceImpl struct { logger *zap.Logger } -func NewApiApplicationService(apiCallService services.ApiCallAggregateService, apiUserService services.ApiUserAggregateService, apiRequestService *services.ApiRequestService, apiCallRepository repositories.ApiCallRepository, walletService finance_services.WalletAggregateService, productManagementService *product_services.ProductManagementService, productSubscriptionService *product_services.ProductSubscriptionService, userRepo user_repositories.UserRepository, txManager *database.TransactionManager, config *config.Config, logger *zap.Logger) ApiApplicationService { - return &ApiApplicationServiceImpl{apiCallService: apiCallService, apiUserService: apiUserService, apiRequestService: apiRequestService, apiCallRepository: apiCallRepository, walletService: walletService, productManagementService: productManagementService, productSubscriptionService: productSubscriptionService, userRepo: userRepo, txManager: txManager, config: config, logger: logger} +func NewApiApplicationService(apiCallService services.ApiCallAggregateService, apiUserService services.ApiUserAggregateService, apiRequestService *services.ApiRequestService, apiCallRepository repositories.ApiCallRepository, walletService finance_services.WalletAggregateService, productManagementService *product_services.ProductManagementService, productSubscriptionService *product_services.ProductSubscriptionService, userRepo user_repositories.UserRepository, txManager *database.TransactionManager, config *config.Config, logger *zap.Logger, contractInfoService user_repositories.ContractInfoRepository) ApiApplicationService { + return &ApiApplicationServiceImpl{apiCallService: apiCallService, apiUserService: apiUserService, apiRequestService: apiRequestService, apiCallRepository: apiCallRepository, walletService: walletService, productManagementService: productManagementService, productSubscriptionService: productSubscriptionService, userRepo: userRepo, txManager: txManager, config: config, logger: logger, contractInfoService: contractInfoService} } // CallApi 应用服务层统一入口 @@ -149,7 +150,22 @@ func (s *ApiApplicationServiceImpl) CallApi(ctx context.Context, cmd *commands.A } // 6. 调用API - response, err := s.apiRequestService.PreprocessRequestApi(txCtx, cmd.ApiName, requestParams, &cmd.Options) + // 查询用户的合同信息获取合同编号 + contractCode := "" // 默认空字符串 + contractInfo, err := s.contractInfoService.FindByUserID(txCtx, apiUser.UserId) + if err == nil && len(contractInfo) > 0 { + contractCode = contractInfo[0].ContractCode + } else { + s.logger.Error("查合同信息失败", zap.Error(err)) + businessError = ErrSystem + return ErrSystem + } + + // 创建CallContext,传递合同编号 + callContext := &processors.CallContext{ + ContractCode: contractCode, + } + response, err := s.apiRequestService.PreprocessRequestApi(txCtx, cmd.ApiName, requestParams, &cmd.Options, callContext) if err != nil { if errors.Is(err, processors.ErrDatasource) { s.logger.Error("调用API失败", zap.Error(err)) diff --git a/internal/domains/api/services/api_request_service.go b/internal/domains/api/services/api_request_service.go index 0489480..ff882a2 100644 --- a/internal/domains/api/services/api_request_service.go +++ b/internal/domains/api/services/api_request_service.go @@ -147,10 +147,10 @@ func registerAllProcessors(combService *comb.CombService) { var RequestProcessors map[string]processors.ProcessorFunc // PreprocessRequestApi 调用指定的请求处理函数 -func (a *ApiRequestService) PreprocessRequestApi(ctx context.Context, apiCode string, params []byte, options *commands.ApiCallOptions) ([]byte, error) { +func (a *ApiRequestService) PreprocessRequestApi(ctx context.Context, apiCode string, params []byte, options *commands.ApiCallOptions, callContext *processors.CallContext) ([]byte, error) { if processor, exists := RequestProcessors[apiCode]; exists { - // 设置Options到依赖容器 - deps := a.processorDeps.WithOptions(options) + // 设置Options和CallContext到依赖容器 + deps := a.processorDeps.WithOptions(options).WithCallContext(callContext) return processor(ctx, params, deps) } return nil, fmt.Errorf("%s: 未找到处理器: %s", ErrSystem, apiCode) diff --git a/internal/domains/api/services/processors/dependencies.go b/internal/domains/api/services/processors/dependencies.go index c2c8a39..2be9d08 100644 --- a/internal/domains/api/services/processors/dependencies.go +++ b/internal/domains/api/services/processors/dependencies.go @@ -15,6 +15,11 @@ type CombServiceInterface interface { ProcessCombRequest(ctx context.Context, params []byte, deps *ProcessorDependencies, packageCode string) (*CombinedResult, error) } +// CallContext CallApi调用上下文,包含调用相关的数据 +type CallContext struct { + ContractCode string // 合同编号 +} + // ProcessorDependencies 处理器依赖容器 type ProcessorDependencies struct { WestDexService *westdex.WestDexService @@ -24,6 +29,7 @@ type ProcessorDependencies struct { Validator interfaces.RequestValidator CombService CombServiceInterface // Changed to interface to break import cycle Options *commands.ApiCallOptions // 添加Options支持 + CallContext *CallContext // 添加CallApi调用上下文 } // NewProcessorDependencies 创建处理器依赖容器 @@ -43,6 +49,7 @@ func NewProcessorDependencies( Validator: validator, CombService: combService, Options: nil, // 初始化为nil,在调用时设置 + CallContext: nil, // 初始化为nil,在调用时设置 } } @@ -52,6 +59,12 @@ func (deps *ProcessorDependencies) WithOptions(options *commands.ApiCallOptions) return deps } +// WithCallContext 设置CallContext的便捷方法 +func (deps *ProcessorDependencies) WithCallContext(callContext *CallContext) *ProcessorDependencies { + deps.CallContext = callContext + return deps +} + // ProcessorFunc 处理器函数类型定义 type ProcessorFunc func(ctx context.Context, params []byte, deps *ProcessorDependencies) ([]byte, error) diff --git a/internal/domains/api/services/processors/flxg/flxg0v4b_processor.go b/internal/domains/api/services/processors/flxg/flxg0v4b_processor.go index 2b6f4d8..6917d7b 100644 --- a/internal/domains/api/services/processors/flxg/flxg0v4b_processor.go +++ b/internal/domains/api/services/processors/flxg/flxg0v4b_processor.go @@ -33,13 +33,16 @@ func ProcessFLXG0V4BRequest(ctx context.Context, params []byte, deps *processors if err != nil { return nil, fmt.Errorf("%s: %w", processors.ErrSystem, err) } - + encryptedAuthAuthorizeFileCode, err := deps.WestDexService.Encrypt(deps.CallContext.ContractCode) + if err != nil { + return nil, fmt.Errorf("%s: %w", processors.ErrSystem, err) + } reqData := map[string]interface{}{ "data": map[string]interface{}{ "name": encryptedName, "idcard": encryptedIDCard, + "auth_authorizeFileCode": encryptedAuthAuthorizeFileCode, "inquired_auth": paramsDto.AuthDate, - // "auth_authorizeFileCode": paramsDto.AuthAuthorizeFileCode, }, } respBytes, err := deps.WestDexService.CallAPI("G22SC01", reqData) diff --git a/internal/domains/api/services/processors/qygl/qygl8271_processor.go b/internal/domains/api/services/processors/qygl/qygl8271_processor.go index 1b8b61e..5d03548 100644 --- a/internal/domains/api/services/processors/qygl/qygl8271_processor.go +++ b/internal/domains/api/services/processors/qygl/qygl8271_processor.go @@ -31,11 +31,15 @@ func ProcessQYGL8271Request(ctx context.Context, params []byte, deps *processors if err != nil { return nil, fmt.Errorf("%s: %w", processors.ErrSystem, err) } - + encryptedAuthAuthorizeFileCode, err := deps.WestDexService.Encrypt(deps.CallContext.ContractCode) + if err != nil { + return nil, fmt.Errorf("%s: %w", processors.ErrSystem, err) + } reqData := map[string]interface{}{ "data": map[string]interface{}{ "org_name": encryptedEntName, "uscc": encryptedEntCode, + "auth_authorizeFileCode": encryptedAuthAuthorizeFileCode, "inquired_auth": paramsDto.AuthDate, // AuthDate 有 encrypt:"false" 标签,不加密 }, } diff --git a/internal/domains/user/entities/contract_info.go b/internal/domains/user/entities/contract_info.go index 75cf69a..5bcb751 100644 --- a/internal/domains/user/entities/contract_info.go +++ b/internal/domains/user/entities/contract_info.go @@ -31,7 +31,7 @@ type ContractInfo struct { UserID string `gorm:"type:varchar(36);not null;index" json:"user_id" comment:"关联用户ID"` // 合同基本信息 - // ContractCode string `gorm:"type:varchar(255);not null" json:"contract_code" comment:"合同编号"` + ContractCode string `gorm:"type:varchar(255);not null;uniqueIndex" json:"contract_code" comment:"合同编号"` ContractName string `gorm:"type:varchar(255);not null" json:"contract_name" comment:"合同名称"` ContractType ContractType `gorm:"type:varchar(50);not null;index" json:"contract_type" comment:"合同类型"` ContractFileID string `gorm:"type:varchar(100);not null" json:"contract_file_id" comment:"合同文件ID"` @@ -91,13 +91,13 @@ func NewContractInfo(enterpriseInfoID, userID, contractName string, contractType } // 生成合同编码 - // contractCode := GenerateContractCode(contractType) + contractCode := GenerateContractCode(contractType) contractInfo := &ContractInfo{ ID: uuid.New().String(), EnterpriseInfoID: enterpriseInfoID, UserID: userID, - // ContractCode: contractCode, + ContractCode: contractCode, ContractName: contractName, ContractType: contractType, ContractFileID: contractFileID, @@ -110,7 +110,7 @@ func NewContractInfo(enterpriseInfoID, userID, contractName string, contractType ContractInfoID: contractInfo.ID, EnterpriseInfoID: enterpriseInfoID, UserID: userID, - // ContractCode: contractCode, + ContractCode: contractCode, ContractName: contractName, ContractType: string(contractType), CreatedAt: time.Now(), @@ -198,9 +198,9 @@ func (c *ContractInfo) validateBasicFields() error { if c.UserID == "" { return fmt.Errorf("用户ID不能为空") } - // if c.ContractCode == "" { - // return fmt.Errorf("合同编码不能为空") - // } + if c.ContractCode == "" { + return fmt.Errorf("合同编码不能为空") + } if c.ContractName == "" { return fmt.Errorf("合同名称不能为空") } @@ -309,7 +309,7 @@ type ContractInfoCreatedEvent struct { ContractInfoID string `json:"contract_info_id"` EnterpriseInfoID string `json:"enterprise_info_id"` UserID string `json:"user_id"` - // ContractCode string `json:"contract_code"` + ContractCode string `json:"contract_code"` ContractName string `json:"contract_name"` ContractType string `json:"contract_type"` CreatedAt time.Time `json:"created_at"` @@ -347,13 +347,13 @@ func GenerateContractCode(contractType ContractType) string { prefix += "02" } - // 获取当前日期,格式为YYMMDD + // 获取当前日期,格式为YYYYMMDD now := time.Now() - dateStr := now.Format("060102") // YYMMDD格式 + dateStr := now.Format("20060102") // YYYYMMDD格式 // 生成一个随机的6位数字 randNum := fmt.Sprintf("%06d", rand.Intn(1000000)) - // 格式:CON + 类型标识 + YYMMDD + 6位随机数 + // 格式:CON + 类型标识 + YYYYMMDD + 6位随机数 return fmt.Sprintf("%s%s%s", prefix, dateStr, randNum) }