This commit is contained in:
2025-10-04 01:31:35 +08:00
parent 8d9e4286b0
commit 740e14e6cc
9 changed files with 264 additions and 38 deletions

View File

@@ -16,7 +16,7 @@ var (
ErrPigBatchNotFound = errors.New("指定的猪批次不存在")
ErrPigBatchActive = errors.New("活跃的猪批次不能被删除")
ErrPigBatchNotActive = errors.New("猪批次不处于活跃状态,无法修改关联猪栏")
ErrPenOccupiedByOtherBatch = errors.New("猪栏已被其他批次用")
ErrPenOccupiedByOtherBatch = errors.New("猪栏已被其他批次使用")
ErrPenStatusInvalidForAllocation = errors.New("猪栏状态不允许分配")
ErrPenNotAssociatedWithBatch = errors.New("猪栏未与该批次关联")
)
@@ -257,7 +257,7 @@ func (s *pigBatchService) UpdatePigBatchPens(batchID uint, desiredPenIDs []uint)
updates := make(map[string]interface{})
updates["pig_batch_id"] = nil // 总是将 PigBatchID 设为 nil
// 只有当猪栏当前状态是“占用”时,才将其状态改回“空闲”
// 只有当猪栏当前状态是“使用中”时,才将其状态改回“空闲”
if currentPen.Status == models.PenStatusOccupied {
updates["status"] = models.PenStatusEmpty
}
@@ -279,17 +279,17 @@ func (s *pigBatchService) UpdatePigBatchPens(batchID uint, desiredPenIDs []uint)
return fmt.Errorf("获取猪栏 %d 信息失败: %w", penID, err)
}
// 验证:猪栏必须是“空闲”状态且未被任何批次用,才能被分配
// 验证:猪栏必须是“空闲”状态且未被任何批次使用,才能被分配
if actualPen.Status != models.PenStatusEmpty {
return fmt.Errorf("猪栏 %s 状态为 %s无法分配: %w", actualPen.PenNumber, actualPen.Status, ErrPenStatusInvalidForAllocation)
}
if actualPen.PigBatchID != nil {
return fmt.Errorf("猪栏 %s 已被其他批次 %d 用,无法分配: %w", actualPen.PenNumber, *actualPen.PigBatchID, ErrPenOccupiedByOtherBatch)
return fmt.Errorf("猪栏 %s 已被其他批次 %d 使用,无法分配: %w", actualPen.PenNumber, *actualPen.PigBatchID, ErrPenOccupiedByOtherBatch)
}
updates := map[string]interface{}{
"pig_batch_id": &batchID, // 将 PigBatchID 设为当前批次ID的指针
"status": models.PenStatusOccupied, // 分配后,状态变为“占用
"status": models.PenStatusOccupied, // 分配后,状态变为“使用中
}
if err := s.pigFarmRepo.UpdatePenFields(tx, penID, updates); err != nil {
s.logger.Errorf("更新猪批次猪栏失败: 添加猪栏 %d 失败: %v", penID, err)

View File

@@ -16,8 +16,8 @@ var (
ErrHouseNotFound = errors.New("指定的猪舍不存在")
ErrPenInUse = errors.New("猪栏正在被活跃批次使用,无法删除")
ErrPenNotFound = errors.New("指定的猪栏不存在")
ErrPenStatusInvalidForOccupiedPen = errors.New("猪栏已被批次用,无法设置为非占用状态")
ErrPenStatusInvalidForUnoccupiedPen = errors.New("猪栏未被批次用,无法设置为占用状态")
ErrPenStatusInvalidForOccupiedPen = errors.New("猪栏已被批次使用,无法设置为非使用中状态")
ErrPenStatusInvalidForUnoccupiedPen = errors.New("猪栏未被批次使用,无法设置为使用中状态")
)
// PigFarmService 提供了猪场资产管理的业务逻辑
@@ -217,12 +217,12 @@ func (s *pigFarmService) UpdatePenStatus(id uint, newStatus models.PenStatus) (*
}
// 业务逻辑:根据猪栏的 PigBatchID 和当前状态,判断是否允许设置为 newStatus
if pen.PigBatchID != nil && *pen.PigBatchID != 0 { // 猪栏已被批次
if newStatus == models.PenStatusEmpty { // 猪栏已被批次用,不能直接设置为空闲
if pen.PigBatchID != nil && *pen.PigBatchID != 0 { // 猪栏已被批次使
if newStatus == models.PenStatusEmpty { // 猪栏已被批次使用,不能直接设置为空闲
return ErrPenStatusInvalidForOccupiedPen
}
} else { // 猪栏未被批次用 (PigBatchID == nil)
if newStatus == models.PenStatusOccupied { // 猪栏未被批次用,不能设置为占用
} else { // 猪栏未被批次使用 (PigBatchID == nil)
if newStatus == models.PenStatusOccupied { // 猪栏未被批次使用,不能设置为使用中
return ErrPenStatusInvalidForUnoccupiedPen
}
}