业务逻辑移动到服务层
This commit is contained in:
@@ -1,12 +1,21 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"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"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrHouseContainsPens = errors.New("无法删除包含猪栏的猪舍")
|
||||
ErrHouseNotFound = errors.New("指定的猪舍不存在")
|
||||
ErrPenInUse = errors.New("猪栏正在被活跃批次使用,无法删除")
|
||||
)
|
||||
|
||||
// PigFarmService 提供了猪场资产管理的业务逻辑
|
||||
type PigFarmService interface {
|
||||
// PigHouse methods
|
||||
@@ -71,19 +80,42 @@ func (s *pigFarmService) UpdatePigHouse(id uint, name, description string) (*mod
|
||||
}
|
||||
|
||||
func (s *pigFarmService) DeletePigHouse(id uint) error {
|
||||
return s.repo.DeletePigHouse(id)
|
||||
// 业务逻辑:检查猪舍是否包含猪栏
|
||||
penCount, err := s.repo.CountPensInHouse(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if penCount > 0 {
|
||||
return ErrHouseContainsPens
|
||||
}
|
||||
|
||||
// 调用仓库层进行删除
|
||||
err = s.repo.DeletePigHouse(id)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrHouseNotFound // 或者直接返回 gorm.ErrRecordNotFound,取决于业务需求
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// --- Pen Implementation ---
|
||||
|
||||
func (s *pigFarmService) CreatePen(penNumber string, houseID uint, capacity int, status models.PenStatus) (*models.Pen, error) {
|
||||
// 业务逻辑:验证所属猪舍是否存在
|
||||
_, err := s.repo.GetPigHouseByID(houseID)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, ErrHouseNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pen := &models.Pen{
|
||||
PenNumber: penNumber,
|
||||
HouseID: houseID,
|
||||
Capacity: capacity,
|
||||
Status: status,
|
||||
}
|
||||
err := s.repo.CreatePen(pen)
|
||||
err = s.repo.CreatePen(pen)
|
||||
return pen, err
|
||||
}
|
||||
|
||||
@@ -96,6 +128,15 @@ func (s *pigFarmService) ListPens() ([]models.Pen, error) {
|
||||
}
|
||||
|
||||
func (s *pigFarmService) UpdatePen(id uint, penNumber string, houseID uint, capacity int, status models.PenStatus) (*models.Pen, error) {
|
||||
// 业务逻辑:验证所属猪舍是否存在
|
||||
_, err := s.repo.GetPigHouseByID(houseID)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, ErrHouseNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pen := &models.Pen{
|
||||
Model: gorm.Model{ID: id},
|
||||
PenNumber: penNumber,
|
||||
@@ -103,7 +144,7 @@ func (s *pigFarmService) UpdatePen(id uint, penNumber string, houseID uint, capa
|
||||
Capacity: capacity,
|
||||
Status: status,
|
||||
}
|
||||
err := s.repo.UpdatePen(pen)
|
||||
err = s.repo.UpdatePen(pen)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -112,5 +153,31 @@ func (s *pigFarmService) UpdatePen(id uint, penNumber string, houseID uint, capa
|
||||
}
|
||||
|
||||
func (s *pigFarmService) DeletePen(id uint) error {
|
||||
return s.repo.DeletePen(id)
|
||||
// 业务逻辑:检查猪栏是否被活跃批次使用
|
||||
pen, err := s.repo.GetPenByID(id)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return gorm.ErrRecordNotFound // 猪栏不存在
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// 检查猪栏是否关联了活跃批次
|
||||
if pen.PigBatchID != 0 {
|
||||
pigBatch, err := s.repo.GetPigBatchByID(pen.PigBatchID)
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return err
|
||||
}
|
||||
// 如果批次活跃,则不能删除猪栏
|
||||
if pigBatch.IsActive() {
|
||||
return ErrPenInUse
|
||||
}
|
||||
}
|
||||
|
||||
// 调用仓库层进行删除
|
||||
err = s.repo.DeletePen(id)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return gorm.ErrRecordNotFound // 猪栏不存在
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user