This commit is contained in:
Mrx
2026-05-10 13:36:47 +08:00
parent 609a35fad6
commit 07394a4ffa
12 changed files with 2010 additions and 82 deletions

View File

@@ -119,6 +119,58 @@ func NewContractInfo(enterpriseInfoID, userID, contractName string, contractType
return contractInfo, nil
}
// NewContractInfoWithContractCode 使用指定合同编号创建合同信息(与认证阶段生成的编号一致)
func NewContractInfoWithContractCode(enterpriseInfoID, userID, contractName string, contractType ContractType, contractFileID, contractFileURL, contractCode string) (*ContractInfo, error) {
if enterpriseInfoID == "" {
return nil, fmt.Errorf("企业信息ID不能为空")
}
if userID == "" {
return nil, fmt.Errorf("用户ID不能为空")
}
if contractName == "" {
return nil, fmt.Errorf("合同名称不能为空")
}
if contractType == "" {
return nil, fmt.Errorf("合同类型不能为空")
}
if contractFileID == "" {
return nil, fmt.Errorf("合同文件ID不能为空")
}
if contractFileURL == "" {
return nil, fmt.Errorf("合同文件URL不能为空")
}
if contractCode == "" {
return nil, fmt.Errorf("合同编号不能为空")
}
if !isValidContractType(contractType) {
return nil, fmt.Errorf("无效的合同类型: %s", contractType)
}
contractInfo := &ContractInfo{
ID: uuid.New().String(),
EnterpriseInfoID: enterpriseInfoID,
UserID: userID,
ContractCode: contractCode,
ContractName: contractName,
ContractType: contractType,
ContractFileID: contractFileID,
ContractFileURL: contractFileURL,
domainEvents: make([]interface{}, 0),
}
contractInfo.addDomainEvent(&ContractInfoCreatedEvent{
ContractInfoID: contractInfo.ID,
EnterpriseInfoID: enterpriseInfoID,
UserID: userID,
ContractCode: contractCode,
ContractName: contractName,
ContractType: string(contractType),
CreatedAt: time.Now(),
})
return contractInfo, nil
}
// ================ 聚合根核心方法 ================
// UpdateContractInfo 更新合同信息

View File

@@ -14,6 +14,7 @@ import (
type ContractAggregateService interface {
// 聚合根生命周期管理
CreateContract(ctx context.Context, enterpriseInfoID, userID, contractName string, contractType entities.ContractType, contractFileID, contractFileURL string) (*entities.ContractInfo, error)
CreateContractWithCode(ctx context.Context, enterpriseInfoID, userID, contractName string, contractType entities.ContractType, contractFileID, contractFileURL, contractCode string) (*entities.ContractInfo, error)
LoadContract(ctx context.Context, contractID string) (*entities.ContractInfo, error)
SaveContract(ctx context.Context, contract *entities.ContractInfo) error
DeleteContract(ctx context.Context, contractID string) error
@@ -94,6 +95,51 @@ func (s *ContractAggregateServiceImpl) CreateContract(
return contract, nil
}
// CreateContractWithCode 使用指定合同编号创建合同信息(与认证合同模板上的编号一致)
func (s *ContractAggregateServiceImpl) CreateContractWithCode(
ctx context.Context,
enterpriseInfoID, userID, contractName string,
contractType entities.ContractType,
contractFileID, contractFileURL, contractCode string,
) (*entities.ContractInfo, error) {
s.logger.Debug("创建合同信息(指定编号)",
zap.String("enterprise_info_id", enterpriseInfoID),
zap.String("user_id", userID),
zap.String("contract_name", contractName),
zap.String("contract_code", contractCode),
zap.String("contract_type", string(contractType)))
exists, err := s.ExistsByContractFileID(ctx, contractFileID)
if err != nil {
return nil, fmt.Errorf("检查合同文件ID失败: %w", err)
}
if exists {
return nil, fmt.Errorf("合同文件ID已存在")
}
contract, err := entities.NewContractInfoWithContractCode(enterpriseInfoID, userID, contractName, contractType, contractFileID, contractFileURL, contractCode)
if err != nil {
return nil, fmt.Errorf("创建合同信息失败: %w", err)
}
if err := s.ValidateBusinessRules(ctx, contract); err != nil {
return nil, fmt.Errorf("业务规则验证失败: %w", err)
}
err = s.SaveContract(ctx, contract)
if err != nil {
return nil, fmt.Errorf("保存合同信息失败: %w", err)
}
s.logger.Info("合同信息创建成功",
zap.String("contract_id", contract.ID),
zap.String("enterprise_info_id", enterpriseInfoID),
zap.String("contract_code", contractCode),
zap.String("contract_name", contractName))
return contract, nil
}
// LoadContract 加载合同信息
func (s *ContractAggregateServiceImpl) LoadContract(ctx context.Context, contractID string) (*entities.ContractInfo, error) {
s.logger.Debug("加载合同信息", zap.String("contract_id", contractID))