修bug
This commit is contained in:
@@ -204,7 +204,7 @@ func (r *recipeServiceImpl) GenerateRecipeWithPrioritizedStockRawMaterials(ctx c
|
|||||||
}
|
}
|
||||||
|
|
||||||
referencePrice := recipe.CalculateReferencePricePerKilogram() / 100
|
referencePrice := recipe.CalculateReferencePricePerKilogram() / 100
|
||||||
recipe.Description = fmt.Sprintf("使用有库存的 %v 种原料和 %v 种无库存原料计算的库存原料优先使用的配方。 计算时预估成本: %.2f元/kg。", len(stockMaterials), len(noStockMaterials), referencePrice)
|
recipe.Description = fmt.Sprintf("使用 %v 种有库存原料和 %v 种无库存原料计算的库存原料优先使用的配方。 计算时预估成本: %.2f元/kg。", len(stockMaterials), len(noStockMaterials), referencePrice)
|
||||||
|
|
||||||
// 如果 totalPercentage 小于 100%,说明填充料被使用,这是符合预期的。
|
// 如果 totalPercentage 小于 100%,说明填充料被使用,这是符合预期的。
|
||||||
// 此时需要在描述中说明需要添加的廉价填充料的百分比。
|
// 此时需要在描述中说明需要添加的廉价填充料的百分比。
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ type RawMaterialListOptions struct {
|
|||||||
NutrientName *string
|
NutrientName *string
|
||||||
MinReferencePrice *float32 // 参考价格最小值
|
MinReferencePrice *float32 // 参考价格最小值
|
||||||
MaxReferencePrice *float32 // 参考价格最大值
|
MaxReferencePrice *float32 // 参考价格最大值
|
||||||
HasStock *bool // 是否只查询有库存的原料
|
HasStock *bool
|
||||||
OrderBy string
|
OrderBy string
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,17 +123,24 @@ func (r *gormRawMaterialRepository) ListRawMaterials(ctx context.Context, opts R
|
|||||||
db = db.Where("reference_price <= ?", *opts.MaxReferencePrice)
|
db = db.Where("reference_price <= ?", *opts.MaxReferencePrice)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 筛选有库存的原料
|
// 筛选有/无库存的原料
|
||||||
if opts.HasStock != nil && *opts.HasStock {
|
if opts.HasStock != nil {
|
||||||
// 内部子查询:生成带有 rn 的结果集,GORM 会自动为 models.RawMaterialStockLog 添加 deleted_at IS NULL
|
// 内部子查询:生成带有 rn 的结果集,GORM 会自动为 models.RawMaterialStockLog 添加 deleted_at IS NULL
|
||||||
rankedLogsQuery := r.db.Model(&models.RawMaterialStockLog{}).
|
rankedLogsQuery := r.db.Model(&models.RawMaterialStockLog{}).
|
||||||
Select("raw_material_id, after_quantity, ROW_NUMBER() OVER(PARTITION BY raw_material_id ORDER BY happened_at DESC, id DESC) as rn")
|
Select("raw_material_id, after_quantity, ROW_NUMBER() OVER(PARTITION BY raw_material_id ORDER BY happened_at DESC, id DESC) as rn")
|
||||||
|
|
||||||
// 外部子查询:从 ranked_logs 中筛选 rn=1 且 after_quantity > 0 的 raw_material_id
|
// 外部子查询:从 ranked_logs 中筛选 rn=1 的 raw_material_id
|
||||||
// GORM 会将 rankedLogsQuery 作为一个子查询嵌入到 FROM 子句中
|
|
||||||
latestStockLogSubQuery := r.db.Table("(?) as ranked_logs", rankedLogsQuery).
|
latestStockLogSubQuery := r.db.Table("(?) as ranked_logs", rankedLogsQuery).
|
||||||
Select("raw_material_id").
|
Select("raw_material_id").
|
||||||
Where("rn = 1 AND after_quantity > 0")
|
Where("rn = 1")
|
||||||
|
|
||||||
|
if *opts.HasStock {
|
||||||
|
// 筛选有库存的原料 (after_quantity > 0)
|
||||||
|
latestStockLogSubQuery = latestStockLogSubQuery.Where("after_quantity > 0")
|
||||||
|
} else {
|
||||||
|
// 筛选没有库存的原料 (after_quantity = 0)
|
||||||
|
latestStockLogSubQuery = latestStockLogSubQuery.Where("after_quantity = 0")
|
||||||
|
}
|
||||||
|
|
||||||
// 将这个子查询直接应用到主查询的 WHERE id IN (?) 条件中
|
// 将这个子查询直接应用到主查询的 WHERE id IN (?) 条件中
|
||||||
db = db.Where("id IN (?)", latestStockLogSubQuery)
|
db = db.Where("id IN (?)", latestStockLogSubQuery)
|
||||||
|
|||||||
Reference in New Issue
Block a user