diff --git a/internal/app/api/router.go b/internal/app/api/router.go index 4189c06..b741782 100644 --- a/internal/app/api/router.go +++ b/internal/app/api/router.go @@ -260,7 +260,7 @@ func (a *API) setupRoutes() { feedGroup.GET("/recipes/:id", a.recipeController.GetRecipe) feedGroup.GET("/recipes", a.recipeController.ListRecipes) feedGroup.POST("/recipes/generate-from-all-materials/:pig_type_id", a.recipeController.GenerateFromAllMaterials) - feedGroup.POST("/recipes/generate-prioritized-stock/:pig_type_id", a.recipeController.GenerateFromAllMaterials) + feedGroup.POST("/recipes/generate-prioritized-stock/:pig_type_id", a.recipeController.GenerateRecipeWithPrioritizedStockRawMaterials) } logger.Debug("饲料管理相关接口注册成功 (需要认证和审计)") diff --git a/internal/domain/recipe/recipe_service.go b/internal/domain/recipe/recipe_service.go index 190f278..86e82a5 100644 --- a/internal/domain/recipe/recipe_service.go +++ b/internal/domain/recipe/recipe_service.go @@ -86,6 +86,7 @@ func (r *recipeServiceImpl) GenerateRecipeWithAllRawMaterials(ctx context.Contex } // 4. 丰富配方描述:计算并添加参考价格信息 + recipe.Name = fmt.Sprintf("%s - 使用所有已知原料", recipe.Name) // 填充 RecipeIngredients 中的 RawMaterial 字段,以便后续计算成本 rawMaterialMap := make(map[uint32]models.RawMaterial) @@ -120,7 +121,7 @@ func (r *recipeServiceImpl) GenerateRecipeWithAllRawMaterials(ctx context.Contex if recipe, err = r.CreateRecipe(serviceCtx, recipe); err != nil { return nil, fmt.Errorf("保存生成的配方失败: %w", err) } - logger.Infof("成功生成配方: %+v", recipe) + logger.Infof("成功生成配方: 配方名称: %v | 配方简介: %v", recipe.Name, recipe.Description) // 6. 返回创建的配方 (现在它应该已经有了ID) return recipe, nil @@ -184,6 +185,8 @@ func (r *recipeServiceImpl) GenerateRecipeWithPrioritizedStockRawMaterials(ctx c } // 5. 丰富配方描述:计算并添加参考价格信息 + recipe.Name = fmt.Sprintf("%s - 优先使用库存已有原料", recipe.Name) + // 注意:这里需要使用原始的、未调整价格的原料信息来计算最终的参考价格 // rawMaterialMap 从 allOriginalMaterials 构建,确保使用原始价格 rawMaterialMap := make(map[uint32]models.RawMaterial) @@ -201,7 +204,7 @@ func (r *recipeServiceImpl) GenerateRecipeWithPrioritizedStockRawMaterials(ctx c } referencePrice := recipe.CalculateReferencePricePerKilogram() / 100 - recipe.Description = fmt.Sprintf("%s 计算时预估成本: %.2f元/kg。", recipe.Description, referencePrice) + recipe.Description = fmt.Sprintf("使用有库存的 %v 种原料和 %v 种无库存原料计算的库存原料优先使用的配方。 计算时预估成本: %.2f元/kg。", len(stockMaterials), len(noStockMaterials), referencePrice) // 如果 totalPercentage 小于 100%,说明填充料被使用,这是符合预期的。 // 此时需要在描述中说明需要添加的廉价填充料的百分比。 @@ -216,7 +219,7 @@ func (r *recipeServiceImpl) GenerateRecipeWithPrioritizedStockRawMaterials(ctx c return nil, fmt.Errorf("保存生成的配方失败: %w", err) } - logger.Infof("成功生成优先使用库存原料的配方: %+v", recipe) + logger.Infof("成功生成优先使用库存原料的配方: 配方名称: %v | 配方简介: %v", recipe.Name, recipe.Description) // 7. 返回创建的配方 return recipe, nil