配方领域层方法+重构配方领域
This commit is contained in:
@@ -176,13 +176,21 @@ func (r *gormRawMaterialRepository) DeleteNutrientsByRawMaterialIDTx(ctx context
|
||||
|
||||
// CreateBatchRawMaterialNutrientsTx 在事务中批量创建原料营养成分
|
||||
func (r *gormRawMaterialRepository) CreateBatchRawMaterialNutrientsTx(ctx context.Context, db *gorm.DB, nutrients []models.RawMaterialNutrient) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "CreateBatchRawMaterialNutrientsTx")
|
||||
|
||||
// 如果没有要创建的记录,直接返回成功,避免执行空的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 {
|
||||
|
||||
// 确保每个营养都关联到正确的原料ID
|
||||
// 注意:这里假设传入的 nutrients 已经设置了正确的 RawMaterialID
|
||||
for i := range nutrients {
|
||||
if nutrients[i].RawMaterialID == 0 {
|
||||
return fmt.Errorf("创建原料营养时 RecipeID 不能为空")
|
||||
}
|
||||
}
|
||||
if err := db.WithContext(repoCtx).Create(&nutrients).Error; err != nil {
|
||||
return fmt.Errorf("批量创建原料营养成分失败: %w", err)
|
||||
}
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user