实现 RecordSickPigRecovery
This commit is contained in:
@@ -86,7 +86,70 @@ func (s *pigBatchService) RecordSickPigs(operatorID uint, batchID uint, penID ui
|
||||
|
||||
// RecordSickPigRecovery 记录病猪康复事件。
|
||||
func (s *pigBatchService) RecordSickPigRecovery(operatorID uint, batchID uint, penID uint, quantity int, treatmentLocation models.PigBatchSickPigTreatmentLocation, happenedAt time.Time, remarks string) error {
|
||||
panic("implement me")
|
||||
if quantity <= 0 {
|
||||
return errors.New("康复猪只数量必须大于0")
|
||||
}
|
||||
|
||||
var err error
|
||||
err = 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 fmt.Errorf("获取批次 %d 失败: %w", batchID, err)
|
||||
}
|
||||
if !batch.IsActive() {
|
||||
return fmt.Errorf("批次 %d 不活跃,无法记录病猪康复事件", batchID)
|
||||
}
|
||||
|
||||
// 2. 检查猪栏是否关联
|
||||
pen, err := s.transferSvc.GetPenByID(tx, penID)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrPenNotFound
|
||||
}
|
||||
return fmt.Errorf("获取猪栏 %d 失败: %w", penID, err)
|
||||
}
|
||||
if pen.PigBatchID == nil || *pen.PigBatchID != batchID {
|
||||
return fmt.Errorf("猪栏 %d 未与批次 %d 关联", penID, batchID)
|
||||
}
|
||||
|
||||
// 3. 检查当前病猪数量是否足够康复
|
||||
currentSickPigs, err := s.sickSvc.GetCurrentSickPigCount(tx, batchID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("获取批次 %d 当前病猪数量失败: %w", batchID, err)
|
||||
}
|
||||
|
||||
if currentSickPigs < quantity {
|
||||
return fmt.Errorf("当前病猪数量不足,当前病猪 %d 头,尝试康复 %d 头", currentSickPigs, quantity)
|
||||
}
|
||||
|
||||
// 4. 创建病猪日志
|
||||
sickLog := &models.PigSickLog{
|
||||
PigBatchID: batchID,
|
||||
PenID: penID,
|
||||
ChangeCount: -quantity, // 康复病猪,ChangeCount 为负数
|
||||
Reason: models.SickPigReasonTypeRecovery,
|
||||
TreatmentLocation: treatmentLocation,
|
||||
Remarks: remarks,
|
||||
OperatorID: operatorID,
|
||||
HappenedAt: happenedAt,
|
||||
}
|
||||
|
||||
if err := s.sickSvc.ProcessSickPigLog(tx, sickLog); err != nil {
|
||||
return fmt.Errorf("处理病猪康复日志失败: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("记录病猪康复事件失败: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// RecordSickPigDeath 记录病猪死亡事件。
|
||||
|
||||
Reference in New Issue
Block a user