145 lines
4.6 KiB
Go
145 lines
4.6 KiB
Go
|
|
package recipe
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
|
||
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
||
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
|
||
|
|
)
|
||
|
|
|
||
|
|
// 定义领域特定的错误
|
||
|
|
var (
|
||
|
|
ErrNutrientNameConflict = fmt.Errorf("营养种类名称已存在")
|
||
|
|
ErrNutrientNotFound = fmt.Errorf("营养种类不存在")
|
||
|
|
ErrNutrientInUse = fmt.Errorf("营养种类正在被原料使用,无法删除")
|
||
|
|
)
|
||
|
|
|
||
|
|
// Service 定义了配方与原料领域的核心业务服务接口
|
||
|
|
type Service interface {
|
||
|
|
CreateNutrient(ctx context.Context, name, description string) (*models.Nutrient, error)
|
||
|
|
UpdateNutrient(ctx context.Context, id uint32, name, description string) (*models.Nutrient, error)
|
||
|
|
DeleteNutrient(ctx context.Context, id uint32) error
|
||
|
|
GetNutrient(ctx context.Context, id uint32) (*models.Nutrient, error)
|
||
|
|
ListNutrients(ctx context.Context, page, pageSize int) ([]models.Nutrient, int64, error)
|
||
|
|
}
|
||
|
|
|
||
|
|
// recipeServiceImpl 是 RecipeService 的实现
|
||
|
|
type recipeServiceImpl struct {
|
||
|
|
ctx context.Context
|
||
|
|
nutrientRepo repository.NutrientRepository
|
||
|
|
}
|
||
|
|
|
||
|
|
// NewRecipeService 创建一个新的 RecipeService 实例
|
||
|
|
func NewRecipeService(ctx context.Context, nutrientRepo repository.NutrientRepository) Service {
|
||
|
|
return &recipeServiceImpl{
|
||
|
|
ctx: ctx,
|
||
|
|
nutrientRepo: nutrientRepo,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// CreateNutrient 实现了创建营养种类的核心业务逻辑
|
||
|
|
func (s *recipeServiceImpl) CreateNutrient(ctx context.Context, name, description string) (*models.Nutrient, error) {
|
||
|
|
serviceCtx := logs.AddFuncName(ctx, s.ctx, "CreateNutrient")
|
||
|
|
|
||
|
|
// 检查名称是否已存在
|
||
|
|
existing, err := s.nutrientRepo.GetNutrientByName(serviceCtx, name)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("检查营养种类名称失败: %w", err)
|
||
|
|
}
|
||
|
|
if existing != nil {
|
||
|
|
return nil, ErrNutrientNameConflict
|
||
|
|
}
|
||
|
|
|
||
|
|
nutrient := &models.Nutrient{
|
||
|
|
Name: name,
|
||
|
|
Description: description,
|
||
|
|
}
|
||
|
|
|
||
|
|
if err := s.nutrientRepo.CreateNutrient(serviceCtx, nutrient); err != nil {
|
||
|
|
return nil, fmt.Errorf("创建营养种类失败: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
return nutrient, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// UpdateNutrient 实现了更新营养种类的核心业务逻辑
|
||
|
|
func (s *recipeServiceImpl) UpdateNutrient(ctx context.Context, id uint32, name, description string) (*models.Nutrient, error) {
|
||
|
|
serviceCtx := logs.AddFuncName(ctx, s.ctx, "UpdateNutrient")
|
||
|
|
|
||
|
|
// 检查要更新的实体是否存在
|
||
|
|
nutrient, err := s.nutrientRepo.GetNutrientByID(serviceCtx, id)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("获取待更新的营养种类失败: %w", err)
|
||
|
|
}
|
||
|
|
if nutrient == nil {
|
||
|
|
return nil, ErrNutrientNotFound
|
||
|
|
}
|
||
|
|
|
||
|
|
// 如果名称有变动,检查新名称是否与其它记录冲突
|
||
|
|
if nutrient.Name != name {
|
||
|
|
existing, err := s.nutrientRepo.GetNutrientByName(serviceCtx, name)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("检查新的营养种类名称失败: %w", err)
|
||
|
|
}
|
||
|
|
if existing != nil && existing.ID != id {
|
||
|
|
return nil, ErrNutrientNameConflict
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
nutrient.Name = name
|
||
|
|
nutrient.Description = description
|
||
|
|
|
||
|
|
if err := s.nutrientRepo.UpdateNutrient(serviceCtx, nutrient); err != nil {
|
||
|
|
return nil, fmt.Errorf("更新营养种类失败: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
return nutrient, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// DeleteNutrient 实现了删除营养种类的核心业务逻辑
|
||
|
|
func (s *recipeServiceImpl) DeleteNutrient(ctx context.Context, id uint32) error {
|
||
|
|
serviceCtx := logs.AddFuncName(ctx, s.ctx, "DeleteNutrient")
|
||
|
|
|
||
|
|
// 检查实体是否存在
|
||
|
|
nutrient, err := s.nutrientRepo.GetNutrientByID(serviceCtx, id)
|
||
|
|
if err != nil {
|
||
|
|
return fmt.Errorf("获取待删除的营养种类失败: %w", err)
|
||
|
|
}
|
||
|
|
if nutrient == nil {
|
||
|
|
return ErrNutrientNotFound
|
||
|
|
}
|
||
|
|
|
||
|
|
if err := s.nutrientRepo.DeleteNutrient(serviceCtx, id); err != nil {
|
||
|
|
return fmt.Errorf("删除营养种类失败: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetNutrient 实现了获取单个营养种类的逻辑
|
||
|
|
func (s *recipeServiceImpl) GetNutrient(ctx context.Context, id uint32) (*models.Nutrient, error) {
|
||
|
|
serviceCtx := logs.AddFuncName(ctx, s.ctx, "GetNutrient")
|
||
|
|
|
||
|
|
nutrient, err := s.nutrientRepo.GetNutrientByID(serviceCtx, id)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("获取营养种类失败: %w", err)
|
||
|
|
}
|
||
|
|
if nutrient == nil {
|
||
|
|
return nil, ErrNutrientNotFound
|
||
|
|
}
|
||
|
|
return nutrient, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// ListNutrients 实现了列出营养种类的逻辑
|
||
|
|
func (s *recipeServiceImpl) ListNutrients(ctx context.Context, page, pageSize int) ([]models.Nutrient, int64, error) {
|
||
|
|
serviceCtx := logs.AddFuncName(ctx, s.ctx, "ListNutrients")
|
||
|
|
|
||
|
|
nutrients, total, err := s.nutrientRepo.ListNutrients(serviceCtx, page, pageSize)
|
||
|
|
if err != nil {
|
||
|
|
return nil, 0, fmt.Errorf("获取营养种类列表失败: %w", err)
|
||
|
|
}
|
||
|
|
return nutrients, total, nil
|
||
|
|
}
|