package recipe import ( "context" "fmt" "git.huangwc.com/pig/pig-farm-controller/internal/infra/logs" "git.huangwc.com/pig/pig-farm-controller/internal/infra/models" "git.huangwc.com/pig/pig-farm-controller/internal/infra/repository" ) // Service 定义了配方与原料领域的核心业务服务接口 // 该接口聚合了所有子领域的服务接口 type Service interface { NutrientService RawMaterialService PigBreedService PigAgeStageService PigTypeService RecipeCoreService RecipeGenerateManager GenerateRecipeWithAllRawMaterials(ctx context.Context, pigTypeID uint32) (*models.Recipe, error) // GenerateRecipeWithPrioritizedStockRawMaterials 生成新配方,优先使用有库存的原料 GenerateRecipeWithPrioritizedStockRawMaterials(ctx context.Context, pigTypeID uint32) (*models.Recipe, error) } // recipeServiceImpl 是 Service 的实现,通过组合各个子服务来实现 type recipeServiceImpl struct { ctx context.Context NutrientService RawMaterialService PigBreedService PigAgeStageService PigTypeService RecipeCoreService RecipeGenerateManager } // NewRecipeService 创建一个新的 Service 实例 func NewRecipeService( ctx context.Context, nutrientService NutrientService, rawMaterialService RawMaterialService, pigBreedService PigBreedService, pigAgeStageService PigAgeStageService, pigTypeService PigTypeService, recipeCoreService RecipeCoreService, recipeGenerateManager RecipeGenerateManager, ) Service { return &recipeServiceImpl{ ctx: ctx, NutrientService: nutrientService, RawMaterialService: rawMaterialService, PigBreedService: pigBreedService, PigAgeStageService: pigAgeStageService, PigTypeService: pigTypeService, RecipeCoreService: recipeCoreService, RecipeGenerateManager: recipeGenerateManager, } } // GenerateRecipeWithAllRawMaterials 使用所有已知原料为特定猪类型生成一个新配方。 // pigTypeID: 目标猪类型的ID。 // 返回: 生成的配方对象指针和可能的错误。 func (r *recipeServiceImpl) GenerateRecipeWithAllRawMaterials(ctx context.Context, pigTypeID uint32) (*models.Recipe, error) { serviceCtx, logger := logs.Trace(ctx, r.ctx, "GenerateRecipeWithAllRawMaterials") // 1. 获取猪只类型信息,确保包含了营养需求 pigType, err := r.GetPigTypeByID(serviceCtx, pigTypeID) if err != nil { return nil, fmt.Errorf("获取猪类型信息失败: %w", err) } // 2. 获取所有原料 // 我们通过传递一个非常大的 pageSize 来获取所有原料,这在大多数情况下是可行的。 // 对于超大规模系统,可能需要考虑分页迭代,但目前这是一个简单有效的策略。 materials, _, err := r.ListRawMaterials(serviceCtx, repository.RawMaterialListOptions{}, 1, 9999) if err != nil { return nil, fmt.Errorf("获取所有原料列表失败: %w", err) } // 3. 调用生成器生成配方 recipe, err := r.GenerateRecipe(serviceCtx, *pigType, materials) if err != nil { return nil, fmt.Errorf("生成配方失败: %w", err) } // 4. 丰富配方描述:计算并添加参考价格信息 recipe.Name = fmt.Sprintf("%s - 使用所有已知原料", recipe.Name) // 填充 RecipeIngredients 中的 RawMaterial 字段,以便后续计算成本 rawMaterialMap := make(map[uint32]models.RawMaterial) for _, mat := range materials { rawMaterialMap[mat.ID] = mat } for i := range recipe.RecipeIngredients { if rawMat, ok := rawMaterialMap[recipe.RecipeIngredients[i].RawMaterialID]; ok { recipe.RecipeIngredients[i].RawMaterial = rawMat } else { // 理论上 GenerateRecipe 应该只使用传入的 materials 中的 RawMaterialID // 如果出现此情况,说明 GenerateRecipe 生成了不在当前 materials 列表中的 RawMaterialID // 这可能是一个数据不一致或逻辑错误,记录警告以便排查 logger.Warnf("未找到 RecipeIngredient (RawMaterialID: %d) 对应的 RawMaterial,成本计算可能不准确", recipe.RecipeIngredients[i].RawMaterialID) } } referencePrice := recipe.CalculateReferencePricePerKilogram() / 100 recipe.Description = fmt.Sprintf("%s 计算时预估成本: %.2f元/kg。", recipe.Description, referencePrice) // 如果 totalPercentage 小于 100%,说明填充料被使用,这是符合预期的。 // 此时需要在描述中说明需要添加的廉价填充料的百分比。 totalPercentage := recipe.CalculateTotalRawMaterialProportion() if totalPercentage < 99.99 { // 允许微小的浮点误差 fillerPercentage := 100 - totalPercentage recipe.Description = fmt.Sprintf("%s 注意:配方中实际原料占比 %.2f%%,需额外补充 %.2f%% 廉价填充料", recipe.Description, totalPercentage, fillerPercentage) } // 5. 保存新生成的配方到数据库 // CreateRecipe 会处理配方及其成分的保存 if recipe, err = r.CreateRecipe(serviceCtx, recipe); err != nil { return nil, fmt.Errorf("保存生成的配方失败: %w", err) } logger.Infof("成功生成配方: 配方名称: %v | 配方简介: %v", recipe.Name, recipe.Description) // 6. 返回创建的配方 (现在它应该已经有了ID) return recipe, nil } // GenerateRecipeWithPrioritizedStockRawMaterials 使用优先有库存原料的策略为特定猪类型生成一个新配方。 // 通过大幅调低有库存原料的参考价格,诱导生成器优先使用。 // pigTypeID: 目标猪类型的ID。 // 返回: 生成的配方对象指针和可能的错误。 func (r *recipeServiceImpl) GenerateRecipeWithPrioritizedStockRawMaterials(ctx context.Context, pigTypeID uint32) (*models.Recipe, error) { serviceCtx, logger := logs.Trace(ctx, r.ctx, "GenerateRecipeWithPrioritizedStockRawMaterials") // 1. 获取猪只类型信息,确保包含了营养需求 pigType, err := r.GetPigTypeByID(serviceCtx, pigTypeID) if err != nil { return nil, fmt.Errorf("获取猪类型信息失败: %w", err) } // 2. 获取所有原料,并区分有库存和无库存的原料 // 获取有库存的原料 hasStock := true stockOpts := repository.RawMaterialListOptions{HasStock: &hasStock} stockMaterials, _, err := r.ListRawMaterials(serviceCtx, stockOpts, 1, 9999) if err != nil { return nil, fmt.Errorf("获取有库存原料列表失败: %w", err) } // 获取无库存的原料 hasStock = false noStockOpts := repository.RawMaterialListOptions{HasStock: &hasStock} noStockMaterials, _, err := r.ListRawMaterials(serviceCtx, noStockOpts, 1, 9999) if err != nil { return nil, fmt.Errorf("获取无库存原料列表失败: %w", err) } // 合并有库存和无库存的原料,作为所有原始原料的列表,用于后续计算最终参考价格 allOriginalMaterials := make([]models.RawMaterial, 0, len(stockMaterials)+len(noStockMaterials)) allOriginalMaterials = append(allOriginalMaterials, stockMaterials...) allOriginalMaterials = append(allOriginalMaterials, noStockMaterials...) // 3. 创建一个用于配方生成的原料列表,并调整有库存原料的价格 var materialsForGeneration []models.RawMaterial // 先添加有库存的原料,并调整价格 for _, mat := range stockMaterials { adjustedMat := mat // 复制一份 // 大幅调低有库存原料的参考价格,诱导生成器优先使用 adjustedMat.ReferencePrice = 0.01 // 设置一个非常小的价格 materialsForGeneration = append(materialsForGeneration, adjustedMat) logger.Debugf("原料 '%s' (ID: %d) 有库存,生成配方时参考价格调整为 %.2f", mat.Name, mat.ID, adjustedMat.ReferencePrice) } // 再添加无库存的原料,保持原价 for _, mat := range noStockMaterials { materialsForGeneration = append(materialsForGeneration, mat) } // 4. 调用生成器生成配方 recipe, err := r.GenerateRecipe(serviceCtx, *pigType, materialsForGeneration) if err != nil { return nil, fmt.Errorf("生成配方失败: %w", err) } // 5. 丰富配方描述:计算并添加参考价格信息 recipe.Name = fmt.Sprintf("%s - 优先使用库存已有原料", recipe.Name) // 注意:这里需要使用原始的、未调整价格的原料信息来计算最终的参考价格 // rawMaterialMap 从 allOriginalMaterials 构建,确保使用原始价格 rawMaterialMap := make(map[uint32]models.RawMaterial) for _, mat := range allOriginalMaterials { rawMaterialMap[mat.ID] = mat } // 填充 RecipeIngredients 中的 RawMaterial 字段,以便后续计算成本 for i := range recipe.RecipeIngredients { if rawMat, ok := rawMaterialMap[recipe.RecipeIngredients[i].RawMaterialID]; ok { recipe.RecipeIngredients[i].RawMaterial = rawMat } else { logger.Warnf("未找到 RecipeIngredient (RawMaterialID: %d) 对应的原始 RawMaterial,成本计算可能不准确", recipe.RecipeIngredients[i].RawMaterialID) } } referencePrice := recipe.CalculateReferencePricePerKilogram() / 100 recipe.Description = fmt.Sprintf("使用 %v 种有库存原料和 %v 种无库存原料计算的库存原料优先使用的配方。 计算时预估成本: %.2f元/kg。", len(stockMaterials), len(noStockMaterials), referencePrice) // 如果 totalPercentage 小于 100%,说明填充料被使用,这是符合预期的。 // 此时需要在描述中说明需要添加的廉价填充料的百分比。 totalPercentage := recipe.CalculateTotalRawMaterialProportion() if totalPercentage < 99.99 { // 允许微小的浮点误差 fillerPercentage := 100 - totalPercentage recipe.Description = fmt.Sprintf("%s 注意:配方中实际原料占比 %.2f%%,需额外补充 %.2f%% 廉价填充料", recipe.Description, totalPercentage, fillerPercentage) } // 6. 保存新生成的配方到数据库 if recipe, err = r.CreateRecipe(serviceCtx, recipe); err != nil { return nil, fmt.Errorf("保存生成的配方失败: %w", err) } logger.Infof("成功生成优先使用库存原料的配方: 配方名称: %v | 配方简介: %v", recipe.Name, recipe.Description) // 7. 返回创建的配方 return recipe, nil }