优化算法

This commit is contained in:
2025-11-27 20:03:14 +08:00
parent 33cdf7278e
commit da8e1d0191
3 changed files with 67 additions and 17 deletions

View File

@@ -13,6 +13,28 @@ func (Recipe) TableName() string {
return "recipes"
}
// CalculateTotalRawMaterialProportion 计算配方中所有原料的总比例
func (r Recipe) CalculateTotalRawMaterialProportion() float32 {
var totalPercentage float32
for _, ingredient := range r.RecipeIngredients {
totalPercentage += ingredient.Percentage
}
return totalPercentage
}
// CalculateReferencePricePerKilogram 根据原料参考价计算配方每公斤的成本
func (r Recipe) CalculateReferencePricePerKilogram() float32 {
var totalCost float32
for _, ingredient := range r.RecipeIngredients {
// 确保 RawMaterial 已经被加载
if ingredient.RawMaterial.ID == 0 {
return 0.0
}
totalCost += ingredient.RawMaterial.ReferencePrice * ingredient.Percentage
}
return totalCost
}
// RecipeIngredient 配方原料组成模型
type RecipeIngredient struct {
Model