实现修改猪营养需求

This commit is contained in:
2025-11-22 17:55:52 +08:00
parent 4405d1f3f1
commit 0637f5fb6c
9 changed files with 427 additions and 20 deletions

View File

@@ -62,6 +62,7 @@ type FeedManagementService interface {
DeletePigType(ctx context.Context, id uint32) error
GetPigType(ctx context.Context, id uint32) (*dto.PigTypeResponse, error)
ListPigTypes(ctx context.Context, req *dto.ListPigTypeRequest) (*dto.ListPigTypeResponse, error)
UpdatePigTypeNutrientRequirements(ctx context.Context, id uint32, req *dto.UpdatePigTypeNutrientRequirementsRequest) (*dto.PigTypeResponse, error) // 新增
}
// feedManagementServiceImpl 是 FeedManagementService 接口的实现
@@ -576,3 +577,42 @@ func (s *feedManagementServiceImpl) ListPigTypes(ctx context.Context, req *dto.L
return dto.ConvertPigTypeListToDTO(pigTypes, total, req.Page, req.PageSize), nil
}
// UpdatePigTypeNutrientRequirements 全量更新猪类型的营养需求
func (s *feedManagementServiceImpl) UpdatePigTypeNutrientRequirements(ctx context.Context, id uint32, req *dto.UpdatePigTypeNutrientRequirementsRequest) (*dto.PigTypeResponse, error) {
serviceCtx := logs.AddFuncName(ctx, s.ctx, "UpdatePigTypeNutrientRequirements")
// 1. 将 DTO 转换为领域模型
requirements := make([]models.PigNutrientRequirement, len(req.NutrientRequirements))
for i, item := range req.NutrientRequirements {
requirements[i] = models.PigNutrientRequirement{
PigTypeID: id, // 设置所属的 PigTypeID
NutrientID: item.NutrientID,
MinRequirement: item.MinRequirement,
MaxRequirement: item.MaxRequirement,
}
}
// 2. 调用领域服务执行更新命令
err := s.recipeSvc.UpdatePigTypeNutrientRequirements(serviceCtx, id, requirements)
if err != nil {
if errors.Is(err, recipe.ErrPigTypeNotFound) {
return nil, ErrPigTypeNotFound
}
// 此处可以根据领域层可能返回的其他特定错误进行转换
return nil, fmt.Errorf("更新猪类型营养需求失败: %w", err)
}
// 3. 更新成功后,调用查询服务获取最新的猪类型信息
updatedPigType, err := s.recipeSvc.GetPigTypeByID(serviceCtx, id)
if err != nil {
if errors.Is(err, recipe.ErrPigTypeNotFound) {
// 理论上不应该发生,因为刚更新成功
return nil, ErrPigTypeNotFound
}
return nil, fmt.Errorf("更新后获取猪类型信息失败: %w", err)
}
// 4. 将领域模型转换为 DTO 并返回
return dto.ConvertPigTypeToDTO(updatedPigType), nil
}