Files
pig-farm-controller/internal/infra/models/recipe.go

52 lines
1.6 KiB
Go
Raw Normal View History

2025-11-22 20:52:15 +08:00
package models
// Recipe 配方模型
type Recipe struct {
Model
Name string `gorm:"size:100;not null;comment:配方名称"`
Description string `gorm:"size:255;comment:配方描述"`
// RecipeIngredients 关联此配方的所有原料组成
RecipeIngredients []RecipeIngredient `gorm:"foreignKey:RecipeID"`
}
func (Recipe) TableName() string {
return "recipes"
}
2025-11-27 20:03:14 +08:00
// 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
}
2025-11-22 20:52:15 +08:00
// RecipeIngredient 配方原料组成模型
type RecipeIngredient struct {
Model
RecipeID uint32 `gorm:"not null;comment:关联的配方ID"`
Recipe Recipe `gorm:"foreignKey:RecipeID"`
RawMaterialID uint32 `gorm:"not null;comment:关联的原料ID"`
RawMaterial RawMaterial `gorm:"foreignKey:RawMaterialID"`
// 重量百分比
Percentage float32 `gorm:"not null;comment:原料在配方中的百分比 (0-1之间的小数, 例如0.15代表15%)"`
}
func (RecipeIngredient) TableName() string {
return "recipe_ingredients"
}