单独修改猪圈状态
This commit is contained in:
@@ -16,7 +16,6 @@ var (
|
||||
ErrPigBatchNotFound = errors.New("指定的猪批次不存在")
|
||||
ErrPigBatchActive = errors.New("活跃的猪批次不能被删除")
|
||||
ErrPigBatchNotActive = errors.New("猪批次不处于活跃状态,无法修改关联猪栏")
|
||||
ErrPenNotFound = errors.New("指定的猪栏不存在")
|
||||
ErrPenOccupiedByOtherBatch = errors.New("猪栏已被其他批次占用")
|
||||
ErrPenStatusInvalidForAllocation = errors.New("猪栏状态不允许分配")
|
||||
ErrPenNotAssociatedWithBatch = errors.New("猪栏未与该批次关联")
|
||||
|
||||
@@ -2,6 +2,7 @@ package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
@@ -11,9 +12,12 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
ErrHouseContainsPens = errors.New("无法删除包含猪栏的猪舍")
|
||||
ErrHouseNotFound = errors.New("指定的猪舍不存在")
|
||||
ErrPenInUse = errors.New("猪栏正在被活跃批次使用,无法删除")
|
||||
ErrHouseContainsPens = errors.New("无法删除包含猪栏的猪舍")
|
||||
ErrHouseNotFound = errors.New("指定的猪舍不存在")
|
||||
ErrPenInUse = errors.New("猪栏正在被活跃批次使用,无法删除")
|
||||
ErrPenNotFound = errors.New("指定的猪栏不存在")
|
||||
ErrPenStatusInvalidForOccupiedPen = errors.New("猪栏已被批次占用,无法设置为非占用状态")
|
||||
ErrPenStatusInvalidForUnoccupiedPen = errors.New("猪栏未被批次占用,无法设置为占用状态")
|
||||
)
|
||||
|
||||
// PigFarmService 提供了猪场资产管理的业务逻辑
|
||||
@@ -31,18 +35,22 @@ type PigFarmService interface {
|
||||
ListPens() ([]models.Pen, error)
|
||||
UpdatePen(id uint, penNumber string, houseID uint, capacity int, status models.PenStatus) (*models.Pen, error)
|
||||
DeletePen(id uint) error
|
||||
// UpdatePenStatus 更新猪栏状态
|
||||
UpdatePenStatus(id uint, newStatus models.PenStatus) (*models.Pen, error)
|
||||
}
|
||||
|
||||
type pigFarmService struct {
|
||||
logger *logs.Logger
|
||||
repo repository.PigFarmRepository
|
||||
uow repository.UnitOfWork // 工作单元,用于事务管理
|
||||
}
|
||||
|
||||
// NewPigFarmService 创建一个新的 PigFarmService 实例
|
||||
func NewPigFarmService(repo repository.PigFarmRepository, logger *logs.Logger) PigFarmService {
|
||||
func NewPigFarmService(repo repository.PigFarmRepository, uow repository.UnitOfWork, logger *logs.Logger) PigFarmService {
|
||||
return &pigFarmService{
|
||||
logger: logger,
|
||||
repo: repo,
|
||||
uow: uow,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -194,3 +202,57 @@ func (s *pigFarmService) DeletePen(id uint) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdatePenStatus 更新猪栏状态
|
||||
func (s *pigFarmService) UpdatePenStatus(id uint, newStatus models.PenStatus) (*models.Pen, error) {
|
||||
var updatedPen *models.Pen
|
||||
err := s.uow.ExecuteInTransaction(func(tx *gorm.DB) error {
|
||||
pen, err := s.repo.GetPenByIDTx(tx, id)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrPenNotFound
|
||||
}
|
||||
s.logger.Errorf("更新猪栏状态失败: 获取猪栏 %d 信息错误: %v", id, err)
|
||||
return fmt.Errorf("获取猪栏 %d 信息失败: %w", id, err)
|
||||
}
|
||||
|
||||
// 业务逻辑:根据猪栏的 PigBatchID 和当前状态,判断是否允许设置为 newStatus
|
||||
if pen.PigBatchID != nil && *pen.PigBatchID != 0 { // 猪栏已被批次占用
|
||||
if newStatus == models.PenStatusEmpty { // 猪栏已被批次占用,不能直接设置为空闲
|
||||
return ErrPenStatusInvalidForOccupiedPen
|
||||
}
|
||||
} else { // 猪栏未被批次占用 (PigBatchID == nil)
|
||||
if newStatus == models.PenStatusOccupied { // 猪栏未被批次占用,不能设置为占用
|
||||
return ErrPenStatusInvalidForUnoccupiedPen
|
||||
}
|
||||
}
|
||||
|
||||
// 如果新状态与旧状态相同,则无需更新
|
||||
if pen.Status == newStatus {
|
||||
updatedPen = pen // 返回原始猪栏,因为没有实际更新
|
||||
return nil
|
||||
}
|
||||
|
||||
updates := map[string]interface{}{
|
||||
"status": newStatus,
|
||||
}
|
||||
|
||||
if err := s.repo.UpdatePenFields(tx, id, updates); err != nil {
|
||||
s.logger.Errorf("更新猪栏 %d 状态失败: %v", id, err)
|
||||
return fmt.Errorf("更新猪栏 %d 状态失败: %w", id, err)
|
||||
}
|
||||
|
||||
// 获取更新后的猪栏信息
|
||||
updatedPen, err = s.repo.GetPenByIDTx(tx, id)
|
||||
if err != nil {
|
||||
s.logger.Errorf("更新猪栏状态后获取猪栏 %d 信息失败: %v", id, err)
|
||||
return fmt.Errorf("更新猪栏状态后获取猪栏 %d 信息失败: %w", id, err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return updatedPen, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user