原料模型增加参考价
This commit is contained in:
@@ -21,8 +21,9 @@ const (
|
||||
// RawMaterial 代表一种原料的静态定义,是系统中的原料字典。
|
||||
type RawMaterial struct {
|
||||
Model
|
||||
Name string `gorm:"size:100;not null;comment:原料名称"`
|
||||
Description string `gorm:"size:255;comment:描述"`
|
||||
Name string `gorm:"size:100;not null;comment:原料名称"`
|
||||
Description string `gorm:"size:255;comment:描述"`
|
||||
ReferencePrice float32 `gorm:"comment:参考价格(kg/元)"`
|
||||
// RawMaterialNutrients 关联此原料的所有营养素含量信息
|
||||
RawMaterialNutrients []RawMaterialNutrient `gorm:"foreignKey:RawMaterialID"`
|
||||
}
|
||||
|
||||
@@ -14,9 +14,11 @@ import (
|
||||
|
||||
// RawMaterialListOptions 定义了查询原料列表时的筛选条件
|
||||
type RawMaterialListOptions struct {
|
||||
Name *string
|
||||
NutrientName *string
|
||||
OrderBy string
|
||||
Name *string
|
||||
NutrientName *string
|
||||
MinReferencePrice *float32 // 参考价格最小值
|
||||
MaxReferencePrice *float32 // 参考价格最大值
|
||||
OrderBy string
|
||||
}
|
||||
|
||||
// StockLogListOptions 定义了查询库存日志列表时的筛选条件
|
||||
@@ -111,6 +113,14 @@ func (r *gormRawMaterialRepository) ListRawMaterials(ctx context.Context, opts R
|
||||
db = db.Where("id IN (?)", subQuery)
|
||||
}
|
||||
|
||||
// 筛选参考价格
|
||||
if opts.MinReferencePrice != nil {
|
||||
db = db.Where("reference_price >= ?", *opts.MinReferencePrice)
|
||||
}
|
||||
if opts.MaxReferencePrice != nil {
|
||||
db = db.Where("reference_price <= ?", *opts.MaxReferencePrice)
|
||||
}
|
||||
|
||||
// 首先计算总数
|
||||
if err := db.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
@@ -133,8 +143,9 @@ func (r *gormRawMaterialRepository) UpdateRawMaterial(ctx context.Context, rawMa
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "UpdateRawMaterial")
|
||||
// 使用 map 更新以避免 GORM 的零值问题,并确保只更新指定字段
|
||||
updateData := map[string]interface{}{
|
||||
"name": rawMaterial.Name,
|
||||
"description": rawMaterial.Description,
|
||||
"name": rawMaterial.Name,
|
||||
"description": rawMaterial.Description,
|
||||
"reference_price": rawMaterial.ReferencePrice,
|
||||
}
|
||||
result := r.db.WithContext(repoCtx).Model(&models.RawMaterial{}).Where("id = ?", rawMaterial.ID).Updates(updateData)
|
||||
if result.Error != nil {
|
||||
|
||||
Reference in New Issue
Block a user