定义配方领域, 实现营养元素的增删改查
This commit is contained in:
144
internal/domain/recipe/recipe_service.go
Normal file
144
internal/domain/recipe/recipe_service.go
Normal file
@@ -0,0 +1,144 @@
|
||||
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
|
||||
}
|
||||
134
internal/infra/repository/nutrient_repository.go
Normal file
134
internal/infra/repository/nutrient_repository.go
Normal file
@@ -0,0 +1,134 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// NutrientRepository 定义了与营养种类相关的数据库操作接口
|
||||
type NutrientRepository interface {
|
||||
CreateNutrient(ctx context.Context, nutrient *models.Nutrient) error
|
||||
GetNutrientByID(ctx context.Context, id uint32) (*models.Nutrient, error)
|
||||
GetNutrientByName(ctx context.Context, name string) (*models.Nutrient, error)
|
||||
ListNutrients(ctx context.Context, page, pageSize int) ([]models.Nutrient, int64, error)
|
||||
UpdateNutrient(ctx context.Context, nutrient *models.Nutrient) error
|
||||
DeleteNutrient(ctx context.Context, id uint32) error
|
||||
}
|
||||
|
||||
// gormNutrientRepository 是 NutrientRepository 的 GORM 实现
|
||||
type gormNutrientRepository struct {
|
||||
ctx context.Context
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewGormNutrientRepository 创建一个新的 NutrientRepository GORM 实现实例
|
||||
func NewGormNutrientRepository(ctx context.Context, db *gorm.DB) NutrientRepository {
|
||||
return &gormNutrientRepository{ctx: ctx, db: db}
|
||||
}
|
||||
|
||||
// CreateNutrient 创建一个新的营养种类
|
||||
func (r *gormNutrientRepository) CreateNutrient(ctx context.Context, nutrient *models.Nutrient) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "CreateNutrient")
|
||||
return r.db.WithContext(repoCtx).Create(nutrient).Error
|
||||
}
|
||||
|
||||
// GetNutrientByID 根据ID获取单个营养种类
|
||||
func (r *gormNutrientRepository) GetNutrientByID(ctx context.Context, id uint32) (*models.Nutrient, error) {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "GetNutrientByID")
|
||||
var nutrient models.Nutrient
|
||||
if err := r.db.WithContext(repoCtx).First(&nutrient, id).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil // 记录未找到不应视为错误
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &nutrient, nil
|
||||
}
|
||||
|
||||
// GetNutrientByName 根据名称获取单个营养种类
|
||||
func (r *gormNutrientRepository) GetNutrientByName(ctx context.Context, name string) (*models.Nutrient, error) {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "GetNutrientByName")
|
||||
var nutrient models.Nutrient
|
||||
if err := r.db.WithContext(repoCtx).Where("name = ?", name).First(&nutrient).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil // 记录未找到不应视为错误
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &nutrient, nil
|
||||
}
|
||||
|
||||
// ListNutrients 列出所有营养种类(分页)
|
||||
func (r *gormNutrientRepository) ListNutrients(ctx context.Context, page, pageSize int) ([]models.Nutrient, int64, error) {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "ListNutrients")
|
||||
var nutrients []models.Nutrient
|
||||
var total int64
|
||||
|
||||
db := r.db.WithContext(repoCtx).Model(&models.Nutrient{})
|
||||
|
||||
// 首先计算总数
|
||||
if err := db.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
// 然后应用分页并获取数据
|
||||
offset := (page - 1) * pageSize
|
||||
if err := db.Offset(offset).Limit(pageSize).Find(&nutrients).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return nutrients, total, nil
|
||||
}
|
||||
|
||||
// UpdateNutrient 更新一个营养种类
|
||||
func (r *gormNutrientRepository) UpdateNutrient(ctx context.Context, nutrient *models.Nutrient) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "UpdateNutrient")
|
||||
// 使用 map 更新以避免 GORM 的零值问题,并确保只更新指定字段
|
||||
updateData := map[string]interface{}{
|
||||
"name": nutrient.Name,
|
||||
"description": nutrient.Description,
|
||||
}
|
||||
result := r.db.WithContext(repoCtx).Model(&models.Nutrient{}).Where("id = ?", nutrient.ID).Updates(updateData)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
return fmt.Errorf("未找到要更新的营养种类,ID: %d", nutrient.ID)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteNutrient 根据ID删除一个营养种类,并级联软删除关联的 RawMaterialNutrient 记录
|
||||
func (r *gormNutrientRepository) DeleteNutrient(ctx context.Context, id uint32) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "DeleteNutrient")
|
||||
|
||||
return r.db.WithContext(repoCtx).Transaction(func(tx *gorm.DB) error {
|
||||
// 1. 查找 Nutrient 记录,确保其存在
|
||||
var nutrient models.Nutrient
|
||||
if err := tx.First(&nutrient, id).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fmt.Errorf("未找到要删除的营养种类,ID: %d", id)
|
||||
}
|
||||
return fmt.Errorf("查询营养种类失败: %w", err)
|
||||
}
|
||||
|
||||
// 2. 软删除所有关联的 RawMaterialNutrient 记录
|
||||
if err := tx.Where("nutrient_id = ?", id).Delete(&models.RawMaterialNutrient{}).Error; err != nil {
|
||||
return fmt.Errorf("软删除关联的原料营养素记录失败: %w", err)
|
||||
}
|
||||
|
||||
// 3. 软删除 Nutrient 记录本身
|
||||
if err := tx.Delete(&nutrient).Error; err != nil {
|
||||
return fmt.Errorf("软删除营养种类失败: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
}
|
||||
22
internal/infra/repository/raw_material_repository.go
Normal file
22
internal/infra/repository/raw_material_repository.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// RawMaterialRepository 定义了与原料相关的数据库操作接口
|
||||
type RawMaterialRepository interface {
|
||||
}
|
||||
|
||||
// gormRawMaterialRepository 是 RawMaterialRepository 的 GORM 实现
|
||||
type gormRawMaterialRepository struct {
|
||||
ctx context.Context
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewGormRawMaterialRepository 创建一个新的 RawMaterialRepository GORM 实现实例
|
||||
func NewGormRawMaterialRepository(ctx context.Context, db *gorm.DB) RawMaterialRepository {
|
||||
return &gormRawMaterialRepository{ctx: ctx, db: db}
|
||||
}
|
||||
Reference in New Issue
Block a user