This commit is contained in:
2025-11-27 22:01:37 +08:00
parent 968d996a9b
commit bc4355cad5

View File

@@ -132,18 +132,18 @@ func (r *gormRawMaterialRepository) ListRawMaterials(ctx context.Context, opts R
// 外部子查询:从 ranked_logs 中筛选 rn=1 的 raw_material_id // 外部子查询:从 ranked_logs 中筛选 rn=1 的 raw_material_id
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") Where("rn = 1").
Where("after_quantity > 0")
if *opts.HasStock { if *opts.HasStock {
// 筛选有库存的原料 (after_quantity > 0) // 筛选有库存的原料 (ID 在有正库存的集合中)
latestStockLogSubQuery = latestStockLogSubQuery.Where("after_quantity > 0") db = db.Where("id IN (?)", latestStockLogSubQuery)
} else { } else {
// 筛选没有库存的原料 (after_quantity = 0) // 筛选库存的原料 (ID 不在有正库存的集合中)
latestStockLogSubQuery = latestStockLogSubQuery.Where("after_quantity = 0") // 包含了最新库存为0 和 没有库存日志的原料。
db = db.Where("id NOT IN (?)", latestStockLogSubQuery)
} }
// 将这个子查询直接应用到主查询的 WHERE id IN (?) 条件中
db = db.Where("id IN (?)", latestStockLogSubQuery)
} }
// 首先计算总数 // 首先计算总数