2025-10-03 18:27:53 +08:00
|
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2025-10-03 22:33:43 +08:00
|
|
|
|
"errors"
|
|
|
|
|
|
|
2025-10-03 18:27:53 +08:00
|
|
|
|
"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"
|
2025-10-03 22:33:43 +08:00
|
|
|
|
|
2025-10-03 18:27:53 +08:00
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2025-10-03 22:33:43 +08:00
|
|
|
|
var (
|
|
|
|
|
|
ErrHouseContainsPens = errors.New("无法删除包含猪栏的猪舍")
|
|
|
|
|
|
ErrHouseNotFound = errors.New("指定的猪舍不存在")
|
|
|
|
|
|
ErrPenInUse = errors.New("猪栏正在被活跃批次使用,无法删除")
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2025-10-03 18:27:53 +08:00
|
|
|
|
// PigFarmService 提供了猪场资产管理的业务逻辑
|
|
|
|
|
|
type PigFarmService interface {
|
|
|
|
|
|
// PigHouse methods
|
|
|
|
|
|
CreatePigHouse(name, description string) (*models.PigHouse, error)
|
|
|
|
|
|
GetPigHouseByID(id uint) (*models.PigHouse, error)
|
|
|
|
|
|
ListPigHouses() ([]models.PigHouse, error)
|
|
|
|
|
|
UpdatePigHouse(id uint, name, description string) (*models.PigHouse, error)
|
|
|
|
|
|
DeletePigHouse(id uint) error
|
|
|
|
|
|
|
|
|
|
|
|
// Pen methods
|
2025-10-03 23:58:37 +08:00
|
|
|
|
CreatePen(penNumber string, houseID uint, capacity int) (*models.Pen, error)
|
2025-10-03 18:27:53 +08:00
|
|
|
|
GetPenByID(id uint) (*models.Pen, error)
|
|
|
|
|
|
ListPens() ([]models.Pen, error)
|
|
|
|
|
|
UpdatePen(id uint, penNumber string, houseID uint, capacity int, status models.PenStatus) (*models.Pen, error)
|
|
|
|
|
|
DeletePen(id uint) error
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type pigFarmService struct {
|
|
|
|
|
|
logger *logs.Logger
|
|
|
|
|
|
repo repository.PigFarmRepository
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// NewPigFarmService 创建一个新的 PigFarmService 实例
|
|
|
|
|
|
func NewPigFarmService(repo repository.PigFarmRepository, logger *logs.Logger) PigFarmService {
|
|
|
|
|
|
return &pigFarmService{
|
|
|
|
|
|
logger: logger,
|
|
|
|
|
|
repo: repo,
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// --- PigHouse Implementation ---
|
|
|
|
|
|
|
|
|
|
|
|
func (s *pigFarmService) CreatePigHouse(name, description string) (*models.PigHouse, error) {
|
|
|
|
|
|
house := &models.PigHouse{
|
|
|
|
|
|
Name: name,
|
|
|
|
|
|
Description: description,
|
|
|
|
|
|
}
|
|
|
|
|
|
err := s.repo.CreatePigHouse(house)
|
|
|
|
|
|
return house, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *pigFarmService) GetPigHouseByID(id uint) (*models.PigHouse, error) {
|
|
|
|
|
|
return s.repo.GetPigHouseByID(id)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *pigFarmService) ListPigHouses() ([]models.PigHouse, error) {
|
|
|
|
|
|
return s.repo.ListPigHouses()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *pigFarmService) UpdatePigHouse(id uint, name, description string) (*models.PigHouse, error) {
|
|
|
|
|
|
house := &models.PigHouse{
|
|
|
|
|
|
Model: gorm.Model{ID: id},
|
|
|
|
|
|
Name: name,
|
|
|
|
|
|
Description: description,
|
|
|
|
|
|
}
|
|
|
|
|
|
err := s.repo.UpdatePigHouse(house)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
// 返回更新后的完整信息
|
|
|
|
|
|
return s.repo.GetPigHouseByID(id)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *pigFarmService) DeletePigHouse(id uint) error {
|
2025-10-03 22:33:43 +08:00
|
|
|
|
// 业务逻辑:检查猪舍是否包含猪栏
|
|
|
|
|
|
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
|
2025-10-03 18:27:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// --- Pen Implementation ---
|
|
|
|
|
|
|
2025-10-03 23:58:37 +08:00
|
|
|
|
func (s *pigFarmService) CreatePen(penNumber string, houseID uint, capacity int) (*models.Pen, error) {
|
2025-10-03 22:33:43 +08:00
|
|
|
|
// 业务逻辑:验证所属猪舍是否存在
|
|
|
|
|
|
_, err := s.repo.GetPigHouseByID(houseID)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
|
|
return nil, ErrHouseNotFound
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-03 18:27:53 +08:00
|
|
|
|
pen := &models.Pen{
|
|
|
|
|
|
PenNumber: penNumber,
|
|
|
|
|
|
HouseID: houseID,
|
|
|
|
|
|
Capacity: capacity,
|
2025-10-03 23:58:37 +08:00
|
|
|
|
Status: models.PenStatusEmpty,
|
2025-10-03 18:27:53 +08:00
|
|
|
|
}
|
2025-10-03 22:33:43 +08:00
|
|
|
|
err = s.repo.CreatePen(pen)
|
2025-10-03 18:27:53 +08:00
|
|
|
|
return pen, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *pigFarmService) GetPenByID(id uint) (*models.Pen, error) {
|
|
|
|
|
|
return s.repo.GetPenByID(id)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *pigFarmService) ListPens() ([]models.Pen, error) {
|
|
|
|
|
|
return s.repo.ListPens()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *pigFarmService) UpdatePen(id uint, penNumber string, houseID uint, capacity int, status models.PenStatus) (*models.Pen, error) {
|
2025-10-03 22:33:43 +08:00
|
|
|
|
// 业务逻辑:验证所属猪舍是否存在
|
|
|
|
|
|
_, err := s.repo.GetPigHouseByID(houseID)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
|
|
return nil, ErrHouseNotFound
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-03 18:27:53 +08:00
|
|
|
|
pen := &models.Pen{
|
|
|
|
|
|
Model: gorm.Model{ID: id},
|
|
|
|
|
|
PenNumber: penNumber,
|
|
|
|
|
|
HouseID: houseID,
|
|
|
|
|
|
Capacity: capacity,
|
|
|
|
|
|
Status: status,
|
|
|
|
|
|
}
|
2025-10-03 22:33:43 +08:00
|
|
|
|
err = s.repo.UpdatePen(pen)
|
2025-10-03 18:27:53 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
// 返回更新后的完整信息
|
|
|
|
|
|
return s.repo.GetPenByID(id)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *pigFarmService) DeletePen(id uint) error {
|
2025-10-03 22:33:43 +08:00
|
|
|
|
// 业务逻辑:检查猪栏是否被活跃批次使用
|
|
|
|
|
|
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
|
2025-10-03 18:27:53 +08:00
|
|
|
|
}
|