实现修改原料营养信息
This commit is contained in:
@@ -26,6 +26,8 @@ type RawMaterialRepository interface {
|
||||
ListRawMaterials(ctx context.Context, opts RawMaterialListOptions, page, pageSize int) ([]models.RawMaterial, int64, error)
|
||||
UpdateRawMaterial(ctx context.Context, rawMaterial *models.RawMaterial) error
|
||||
DeleteRawMaterial(ctx context.Context, id uint32) error
|
||||
DeleteNutrientsByRawMaterialIDTx(ctx context.Context, db *gorm.DB, rawMaterialID uint32) error
|
||||
CreateBatchRawMaterialNutrientsTx(ctx context.Context, db *gorm.DB, nutrients []models.RawMaterialNutrient) error
|
||||
|
||||
// 库存日志相关方法
|
||||
CreateRawMaterialStockLog(ctx context.Context, log *models.RawMaterialStockLog) error
|
||||
@@ -162,6 +164,30 @@ func (r *gormRawMaterialRepository) DeleteRawMaterial(ctx context.Context, id ui
|
||||
})
|
||||
}
|
||||
|
||||
// DeleteNutrientsByRawMaterialIDTx 在事务中软删除指定原料的所有营养成分
|
||||
func (r *gormRawMaterialRepository) DeleteNutrientsByRawMaterialIDTx(ctx context.Context, db *gorm.DB, rawMaterialID uint32) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "DeleteNutrientsByRawMaterialIDTx")
|
||||
tx := db.WithContext(repoCtx)
|
||||
if err := tx.Where("raw_material_id = ?", rawMaterialID).Delete(&models.RawMaterialNutrient{}).Error; err != nil {
|
||||
return fmt.Errorf("软删除原料营养成分失败: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateBatchRawMaterialNutrientsTx 在事务中批量创建原料营养成分
|
||||
func (r *gormRawMaterialRepository) CreateBatchRawMaterialNutrientsTx(ctx context.Context, db *gorm.DB, nutrients []models.RawMaterialNutrient) error {
|
||||
// 如果没有要创建的记录,直接返回成功,避免执行空的Create语句
|
||||
if len(nutrients) == 0 {
|
||||
return nil
|
||||
}
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "CreateBatchRawMaterialNutrientsTx")
|
||||
tx := db.WithContext(repoCtx)
|
||||
if err := tx.Create(&nutrients).Error; err != nil {
|
||||
return fmt.Errorf("批量创建原料营养成分失败: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateRawMaterialStockLog 创建一条新的原料库存日志
|
||||
func (r *gormRawMaterialRepository) CreateRawMaterialStockLog(ctx context.Context, log *models.RawMaterialStockLog) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "CreateRawMaterialStockLog")
|
||||
|
||||
Reference in New Issue
Block a user