实现 RemoveEmptyPenFromBatch
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// --- 业务错误定义 ---
|
||||
@@ -25,6 +26,8 @@ var (
|
||||
ErrPenNotFound = errors.New("指定的猪栏不存在")
|
||||
// ErrPenNotAssociatedWithBatch 表示猪栏未与该批次关联
|
||||
ErrPenNotAssociatedWithBatch = errors.New("猪栏未与该批次关联")
|
||||
// ErrPenNotEmpty 表示猪栏内仍有猪只,不允许执行当前操作。
|
||||
ErrPenNotEmpty = errors.New("猪栏内仍有猪只,无法执行此操作")
|
||||
// ErrInvalidOperation 非法操作
|
||||
ErrInvalidOperation = errors.New("非法操作")
|
||||
)
|
||||
@@ -52,6 +55,8 @@ type PigBatchService interface {
|
||||
MovePigsIntoPen(batchID uint, toPenID uint, quantity int, operatorID uint, remarks string) error
|
||||
// ReclassifyPenToNewBatch 连猪带栏,整体划拨到另一个猪群
|
||||
ReclassifyPenToNewBatch(fromBatchID uint, toBatchID uint, penID uint, operatorID uint, remarks string) error
|
||||
// RemoveEmptyPenFromBatch 将一个猪栏移除出猪群,此方法需要在猪栏为空的情况下执行。
|
||||
RemoveEmptyPenFromBatch(batchID uint, penID uint) error
|
||||
|
||||
// GetCurrentPigQuantity 获取指定猪批次的当前猪只数量。
|
||||
GetCurrentPigQuantity(batchID uint) (int, error)
|
||||
@@ -115,3 +120,48 @@ func NewPigBatchService(
|
||||
sickSvc: sickSvc,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *pigBatchService) RemoveEmptyPenFromBatch(batchID uint, penID uint) error {
|
||||
return s.uow.ExecuteInTransaction(func(tx *gorm.DB) error {
|
||||
// 1. 检查猪批次是否存在且活跃
|
||||
batch, err := s.pigBatchRepo.GetPigBatchByIDTx(tx, batchID)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrPigBatchNotFound
|
||||
}
|
||||
return err
|
||||
}
|
||||
if !batch.IsActive() {
|
||||
return ErrPigBatchNotActive
|
||||
}
|
||||
|
||||
// 2. 检查猪栏是否存在
|
||||
pen, err := s.transferSvc.GetPenByID(tx, penID)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrPenNotFound
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// 3. 检查猪栏是否与当前批次关联
|
||||
if pen.PigBatchID == nil || *pen.PigBatchID != batchID {
|
||||
return ErrPenNotAssociatedWithBatch
|
||||
}
|
||||
|
||||
// 4. 检查猪栏是否为空
|
||||
pigsInPen, err := s.transferSvc.GetCurrentPigsInPen(tx, penID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if pigsInPen > 0 {
|
||||
return ErrPenNotEmpty
|
||||
}
|
||||
|
||||
// 5. 释放猪栏 (将 pig_batch_id 设置为 nil,状态设置为空闲)
|
||||
if err := s.transferSvc.ReleasePen(tx, penID); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user