实现修改原料营养信息
This commit is contained in:
@@ -40,6 +40,7 @@ type Service interface {
|
||||
DeleteRawMaterial(ctx context.Context, id uint32) error
|
||||
GetRawMaterial(ctx context.Context, id uint32) (*models.RawMaterial, error)
|
||||
ListRawMaterials(ctx context.Context, opts repository.RawMaterialListOptions, page, pageSize int) ([]models.RawMaterial, int64, error)
|
||||
UpdateRawMaterialNutrients(ctx context.Context, rawMaterialID uint32, nutrients []models.RawMaterialNutrient) error
|
||||
|
||||
// 猪品种相关接口
|
||||
CreatePigBreed(ctx context.Context, breed *models.PigBreed) error
|
||||
@@ -66,15 +67,17 @@ type Service interface {
|
||||
// recipeServiceImpl 是 RecipeService 的实现
|
||||
type recipeServiceImpl struct {
|
||||
ctx context.Context
|
||||
uow repository.UnitOfWork
|
||||
nutrientRepo repository.NutrientRepository
|
||||
rawMaterialRepo repository.RawMaterialRepository
|
||||
pigTypeRepo repository.PigTypeRepository
|
||||
}
|
||||
|
||||
// NewRecipeService 创建一个新的 RecipeService 实例
|
||||
func NewRecipeService(ctx context.Context, nutrientRepo repository.NutrientRepository, rawMaterialRepo repository.RawMaterialRepository, pigTypeRepo repository.PigTypeRepository) Service {
|
||||
func NewRecipeService(ctx context.Context, uow repository.UnitOfWork, nutrientRepo repository.NutrientRepository, rawMaterialRepo repository.RawMaterialRepository, pigTypeRepo repository.PigTypeRepository) Service {
|
||||
return &recipeServiceImpl{
|
||||
ctx: ctx,
|
||||
uow: uow,
|
||||
nutrientRepo: nutrientRepo,
|
||||
rawMaterialRepo: rawMaterialRepo,
|
||||
pigTypeRepo: pigTypeRepo,
|
||||
@@ -289,6 +292,41 @@ func (s *recipeServiceImpl) ListRawMaterials(ctx context.Context, opts repositor
|
||||
return rawMaterials, total, nil
|
||||
}
|
||||
|
||||
// UpdateRawMaterialNutrients 实现了全量更新原料营养成分的业务逻辑
|
||||
func (s *recipeServiceImpl) UpdateRawMaterialNutrients(ctx context.Context, rawMaterialID uint32, nutrients []models.RawMaterialNutrient) error {
|
||||
serviceCtx := logs.AddFuncName(ctx, s.ctx, "UpdateRawMaterialNutrients")
|
||||
|
||||
// 1. 检查原料是否存在
|
||||
if _, err := s.rawMaterialRepo.GetRawMaterialByID(serviceCtx, rawMaterialID); err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrRawMaterialNotFound
|
||||
}
|
||||
return fmt.Errorf("获取待更新的原料失败: %w", err)
|
||||
}
|
||||
|
||||
// 2. 在事务中执行替换操作
|
||||
err := s.uow.ExecuteInTransaction(serviceCtx, func(tx *gorm.DB) error {
|
||||
// 2.1. 删除旧的关联记录
|
||||
if err := s.rawMaterialRepo.DeleteNutrientsByRawMaterialIDTx(serviceCtx, tx, rawMaterialID); err != nil {
|
||||
return err // 错误已在仓库层封装,直接返回
|
||||
}
|
||||
|
||||
// 2.2. 创建新的关联记录
|
||||
if err := s.rawMaterialRepo.CreateBatchRawMaterialNutrientsTx(serviceCtx, tx, nutrients); err != nil {
|
||||
return err // 错误已在仓库层封装,直接返回
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("更新原料营养成分事务执行失败: %w", err)
|
||||
}
|
||||
|
||||
// 3. 操作成功,直接返回 nil
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreatePigBreed 实现了创建猪品种的核心业务逻辑
|
||||
func (s *recipeServiceImpl) CreatePigBreed(ctx context.Context, breed *models.PigBreed) error {
|
||||
serviceCtx := logs.AddFuncName(ctx, s.ctx, "CreatePigBreed")
|
||||
|
||||
Reference in New Issue
Block a user