实现配方领域关于猪模型和营养需求的增删改查
This commit is contained in:
@@ -14,12 +14,15 @@ import (
|
||||
|
||||
// 定义领域特定的错误
|
||||
var (
|
||||
ErrNutrientNameConflict = fmt.Errorf("营养种类名称已存在")
|
||||
ErrNutrientNotFound = fmt.Errorf("营养种类不存在")
|
||||
ErrNutrientInUse = fmt.Errorf("营养种类正在被原料使用,无法删除")
|
||||
|
||||
ErrNutrientNameConflict = fmt.Errorf("营养种类名称已存在")
|
||||
ErrNutrientNotFound = fmt.Errorf("营养种类不存在")
|
||||
ErrRawMaterialNameConflict = fmt.Errorf("原料名称已存在")
|
||||
ErrRawMaterialNotFound = fmt.Errorf("原料不存在")
|
||||
ErrPigBreedInUse = fmt.Errorf("猪品种正在被猪类型使用,无法删除")
|
||||
ErrPigBreedNotFound = fmt.Errorf("猪品种不存在")
|
||||
ErrPigAgeStageInUse = fmt.Errorf("猪年龄阶段正在被猪类型使用,无法删除")
|
||||
ErrPigAgeStageNotFound = fmt.Errorf("猪年龄阶段不存在")
|
||||
ErrPigTypeNotFound = fmt.Errorf("猪类型不存在")
|
||||
)
|
||||
|
||||
// Service 定义了配方与原料领域的核心业务服务接口
|
||||
@@ -37,6 +40,27 @@ type Service interface {
|
||||
DeleteRawMaterial(ctx context.Context, id uint32) error
|
||||
GetRawMaterial(ctx context.Context, id uint32) (*models.RawMaterial, error)
|
||||
ListRawMaterials(ctx context.Context, page, pageSize int) ([]models.RawMaterial, int64, error)
|
||||
|
||||
// 猪品种相关接口
|
||||
CreatePigBreed(ctx context.Context, breed *models.PigBreed) error
|
||||
GetPigBreedByID(ctx context.Context, id uint32) (*models.PigBreed, error)
|
||||
UpdatePigBreed(ctx context.Context, breed *models.PigBreed) error
|
||||
DeletePigBreed(ctx context.Context, id uint32) error
|
||||
ListPigBreeds(ctx context.Context, opts repository.PigBreedListOptions, page, pageSize int) ([]models.PigBreed, int64, error)
|
||||
|
||||
// 猪年龄阶段相关接口
|
||||
CreatePigAgeStage(ctx context.Context, ageStage *models.PigAgeStage) error
|
||||
GetPigAgeStageByID(ctx context.Context, id uint32) (*models.PigAgeStage, error)
|
||||
UpdatePigAgeStage(ctx context.Context, ageStage *models.PigAgeStage) error
|
||||
DeletePigAgeStage(ctx context.Context, id uint32) error
|
||||
ListPigAgeStages(ctx context.Context, opts repository.PigAgeStageListOptions, page, pageSize int) ([]models.PigAgeStage, int64, error)
|
||||
|
||||
// 猪类型相关接口
|
||||
CreatePigType(ctx context.Context, pigType *models.PigType) error
|
||||
GetPigTypeByID(ctx context.Context, id uint32) (*models.PigType, error)
|
||||
UpdatePigType(ctx context.Context, pigType *models.PigType) error
|
||||
DeletePigType(ctx context.Context, id uint32) error
|
||||
ListPigTypes(ctx context.Context, opts repository.PigTypeListOptions, page, pageSize int) ([]models.PigType, int64, error)
|
||||
}
|
||||
|
||||
// recipeServiceImpl 是 RecipeService 的实现
|
||||
@@ -44,14 +68,16 @@ type recipeServiceImpl struct {
|
||||
ctx context.Context
|
||||
nutrientRepo repository.NutrientRepository
|
||||
rawMaterialRepo repository.RawMaterialRepository
|
||||
pigTypeRepo repository.PigTypeRepository
|
||||
}
|
||||
|
||||
// NewRecipeService 创建一个新的 RecipeService 实例
|
||||
func NewRecipeService(ctx context.Context, nutrientRepo repository.NutrientRepository, rawMaterialRepo repository.RawMaterialRepository) Service {
|
||||
func NewRecipeService(ctx context.Context, nutrientRepo repository.NutrientRepository, rawMaterialRepo repository.RawMaterialRepository, pigTypeRepo repository.PigTypeRepository) Service {
|
||||
return &recipeServiceImpl{
|
||||
ctx: ctx,
|
||||
nutrientRepo: nutrientRepo,
|
||||
rawMaterialRepo: rawMaterialRepo,
|
||||
pigTypeRepo: pigTypeRepo,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -262,3 +288,203 @@ func (s *recipeServiceImpl) ListRawMaterials(ctx context.Context, page, pageSize
|
||||
}
|
||||
return rawMaterials, total, nil
|
||||
}
|
||||
|
||||
// CreatePigBreed 实现了创建猪品种的核心业务逻辑
|
||||
func (s *recipeServiceImpl) CreatePigBreed(ctx context.Context, breed *models.PigBreed) error {
|
||||
serviceCtx := logs.AddFuncName(ctx, s.ctx, "CreatePigBreed")
|
||||
if err := s.pigTypeRepo.CreatePigBreed(serviceCtx, breed); err != nil {
|
||||
return fmt.Errorf("创建猪品种失败: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetPigBreedByID 实现了获取单个猪品种的逻辑
|
||||
func (s *recipeServiceImpl) GetPigBreedByID(ctx context.Context, id uint32) (*models.PigBreed, error) {
|
||||
serviceCtx := logs.AddFuncName(ctx, s.ctx, "GetPigBreedByID")
|
||||
breed, err := s.pigTypeRepo.GetPigBreedByID(serviceCtx, id)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, ErrPigBreedNotFound
|
||||
}
|
||||
return nil, fmt.Errorf("获取猪品种失败: %w", err)
|
||||
}
|
||||
return breed, nil
|
||||
}
|
||||
|
||||
// UpdatePigBreed 实现了更新猪品种的核心业务逻辑
|
||||
func (s *recipeServiceImpl) UpdatePigBreed(ctx context.Context, breed *models.PigBreed) error {
|
||||
serviceCtx := logs.AddFuncName(ctx, s.ctx, "UpdatePigBreed")
|
||||
if err := s.pigTypeRepo.UpdatePigBreed(serviceCtx, breed); err != nil {
|
||||
return fmt.Errorf("更新猪品种失败: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeletePigBreed 实现了删除猪品种的核心业务逻辑
|
||||
func (s *recipeServiceImpl) DeletePigBreed(ctx context.Context, id uint32) error {
|
||||
serviceCtx := logs.AddFuncName(ctx, s.ctx, "DeletePigBreed")
|
||||
|
||||
// 检查是否有猪类型关联到该品种
|
||||
opts := repository.PigTypeListOptions{BreedID: &id}
|
||||
pigTypes, _, err := s.pigTypeRepo.ListPigTypes(serviceCtx, opts, 1, 1) // 只需检查是否存在,所以取1条
|
||||
if err != nil {
|
||||
return fmt.Errorf("检查猪品种关联失败: %w", err)
|
||||
}
|
||||
if len(pigTypes) > 0 {
|
||||
return ErrPigBreedInUse
|
||||
}
|
||||
|
||||
// 检查实体是否存在
|
||||
_, err = s.pigTypeRepo.GetPigBreedByID(serviceCtx, id)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrPigBreedNotFound
|
||||
}
|
||||
return fmt.Errorf("获取待删除的猪品种失败: %w", err)
|
||||
}
|
||||
|
||||
if err := s.pigTypeRepo.DeletePigBreed(serviceCtx, id); err != nil {
|
||||
return fmt.Errorf("删除猪品种失败: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListPigBreeds 实现了列出猪品种的逻辑
|
||||
func (s *recipeServiceImpl) ListPigBreeds(ctx context.Context, opts repository.PigBreedListOptions, page, pageSize int) ([]models.PigBreed, int64, error) {
|
||||
serviceCtx := logs.AddFuncName(ctx, s.ctx, "ListPigBreeds")
|
||||
breeds, total, err := s.pigTypeRepo.ListPigBreeds(serviceCtx, opts, page, pageSize)
|
||||
if err != nil {
|
||||
return nil, 0, fmt.Errorf("获取猪品种列表失败: %w", err)
|
||||
}
|
||||
return breeds, total, nil
|
||||
}
|
||||
|
||||
// CreatePigAgeStage 实现了创建猪年龄阶段的核心业务逻辑
|
||||
func (s *recipeServiceImpl) CreatePigAgeStage(ctx context.Context, ageStage *models.PigAgeStage) error {
|
||||
serviceCtx := logs.AddFuncName(ctx, s.ctx, "CreatePigAgeStage")
|
||||
if err := s.pigTypeRepo.CreatePigAgeStage(serviceCtx, ageStage); err != nil {
|
||||
return fmt.Errorf("创建猪年龄阶段失败: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetPigAgeStageByID 实现了获取单个猪年龄阶段的逻辑
|
||||
func (s *recipeServiceImpl) GetPigAgeStageByID(ctx context.Context, id uint32) (*models.PigAgeStage, error) {
|
||||
serviceCtx := logs.AddFuncName(ctx, s.ctx, "GetPigAgeStageByID")
|
||||
ageStage, err := s.pigTypeRepo.GetPigAgeStageByID(serviceCtx, id)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, ErrPigAgeStageNotFound
|
||||
}
|
||||
return nil, fmt.Errorf("获取猪年龄阶段失败: %w", err)
|
||||
}
|
||||
return ageStage, nil
|
||||
}
|
||||
|
||||
// UpdatePigAgeStage 实现了更新猪年龄阶段的核心业务逻辑
|
||||
func (s *recipeServiceImpl) UpdatePigAgeStage(ctx context.Context, ageStage *models.PigAgeStage) error {
|
||||
serviceCtx := logs.AddFuncName(ctx, s.ctx, "UpdatePigAgeStage")
|
||||
if err := s.pigTypeRepo.UpdatePigAgeStage(serviceCtx, ageStage); err != nil {
|
||||
return fmt.Errorf("更新猪年龄阶段失败: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeletePigAgeStage 实现了删除猪年龄阶段的核心业务逻辑
|
||||
func (s *recipeServiceImpl) DeletePigAgeStage(ctx context.Context, id uint32) error {
|
||||
serviceCtx := logs.AddFuncName(ctx, s.ctx, "DeletePigAgeStage")
|
||||
|
||||
// 检查是否有猪类型关联到该年龄阶段
|
||||
opts := repository.PigTypeListOptions{AgeStageID: &id}
|
||||
pigTypes, _, err := s.pigTypeRepo.ListPigTypes(serviceCtx, opts, 1, 1) // 只需检查是否存在,所以取1条
|
||||
if err != nil {
|
||||
return fmt.Errorf("检查猪年龄阶段关联失败: %w", err)
|
||||
}
|
||||
if len(pigTypes) > 0 {
|
||||
return ErrPigAgeStageInUse
|
||||
}
|
||||
|
||||
// 检查实体是否存在
|
||||
_, err = s.pigTypeRepo.GetPigAgeStageByID(serviceCtx, id)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrPigAgeStageNotFound
|
||||
}
|
||||
return fmt.Errorf("获取待删除的猪年龄阶段失败: %w", err)
|
||||
}
|
||||
|
||||
if err := s.pigTypeRepo.DeletePigAgeStage(serviceCtx, id); err != nil {
|
||||
return fmt.Errorf("删除猪年龄阶段失败: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListPigAgeStages 实现了列出猪年龄阶段的逻辑
|
||||
func (s *recipeServiceImpl) ListPigAgeStages(ctx context.Context, opts repository.PigAgeStageListOptions, page, pageSize int) ([]models.PigAgeStage, int64, error) {
|
||||
serviceCtx := logs.AddFuncName(ctx, s.ctx, "ListPigAgeStages")
|
||||
ageStages, total, err := s.pigTypeRepo.ListPigAgeStages(serviceCtx, opts, page, pageSize)
|
||||
if err != nil {
|
||||
return nil, 0, fmt.Errorf("获取猪年龄阶段列表失败: %w", err)
|
||||
}
|
||||
return ageStages, total, nil
|
||||
}
|
||||
|
||||
// CreatePigType 实现了创建猪类型的核心业务逻辑
|
||||
func (s *recipeServiceImpl) CreatePigType(ctx context.Context, pigType *models.PigType) error {
|
||||
serviceCtx := logs.AddFuncName(ctx, s.ctx, "CreatePigType")
|
||||
if err := s.pigTypeRepo.CreatePigType(serviceCtx, pigType); err != nil {
|
||||
return fmt.Errorf("创建猪类型失败: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetPigTypeByID 实现了获取单个猪类型的逻辑
|
||||
func (s *recipeServiceImpl) GetPigTypeByID(ctx context.Context, id uint32) (*models.PigType, error) {
|
||||
serviceCtx := logs.AddFuncName(ctx, s.ctx, "GetPigTypeByID")
|
||||
pigType, err := s.pigTypeRepo.GetPigTypeByID(serviceCtx, id)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, ErrPigTypeNotFound
|
||||
}
|
||||
return nil, fmt.Errorf("获取猪类型失败: %w", err)
|
||||
}
|
||||
return pigType, nil
|
||||
}
|
||||
|
||||
// UpdatePigType 实现了更新猪类型的核心业务逻辑
|
||||
func (s *recipeServiceImpl) UpdatePigType(ctx context.Context, pigType *models.PigType) error {
|
||||
serviceCtx := logs.AddFuncName(ctx, s.ctx, "UpdatePigType")
|
||||
if err := s.pigTypeRepo.UpdatePigType(serviceCtx, pigType); err != nil {
|
||||
return fmt.Errorf("更新猪类型失败: %m", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeletePigType 实现了删除猪类型的核心业务逻辑
|
||||
func (s *recipeServiceImpl) DeletePigType(ctx context.Context, id uint32) error {
|
||||
serviceCtx := logs.AddFuncName(ctx, s.ctx, "DeletePigType")
|
||||
|
||||
// 检查实体是否存在
|
||||
_, err := s.pigTypeRepo.GetPigTypeByID(serviceCtx, id)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrPigTypeNotFound
|
||||
}
|
||||
return fmt.Errorf("获取待删除的猪类型失败: %w", err)
|
||||
}
|
||||
|
||||
if err := s.pigTypeRepo.DeletePigType(serviceCtx, id); err != nil {
|
||||
return fmt.Errorf("删除猪类型失败: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListPigTypes 实现了列出猪类型的逻辑
|
||||
func (s *recipeServiceImpl) ListPigTypes(ctx context.Context, opts repository.PigTypeListOptions, page, pageSize int) ([]models.PigType, int64, error) {
|
||||
serviceCtx := logs.AddFuncName(ctx, s.ctx, "ListPigTypes")
|
||||
pigTypes, total, err := s.pigTypeRepo.ListPigTypes(serviceCtx, opts, page, pageSize)
|
||||
if err != nil {
|
||||
return nil, 0, fmt.Errorf("获取猪类型列表失败: %w", err)
|
||||
}
|
||||
return pigTypes, total, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user