实现 RecordCull 和 RecordDeath
This commit is contained in:
@@ -77,6 +77,12 @@ type PigBatchService interface {
|
|||||||
RecordSickPigDeath(operatorID uint, batchID uint, penID uint, quantity int, treatmentLocation models.PigBatchSickPigTreatmentLocation, happenedAt time.Time, remarks string) error
|
RecordSickPigDeath(operatorID uint, batchID uint, penID uint, quantity int, treatmentLocation models.PigBatchSickPigTreatmentLocation, happenedAt time.Time, remarks string) error
|
||||||
// RecordSickPigCull 记录病猪淘汰事件。
|
// RecordSickPigCull 记录病猪淘汰事件。
|
||||||
RecordSickPigCull(operatorID uint, batchID uint, penID uint, quantity int, treatmentLocation models.PigBatchSickPigTreatmentLocation, happenedAt time.Time, remarks string) error
|
RecordSickPigCull(operatorID uint, batchID uint, penID uint, quantity int, treatmentLocation models.PigBatchSickPigTreatmentLocation, happenedAt time.Time, remarks string) error
|
||||||
|
|
||||||
|
// --- 正常猪只管理相关方法 ---
|
||||||
|
// RecordDeath 记录正常猪只死亡事件。
|
||||||
|
RecordDeath(operatorID uint, batchID uint, penID uint, quantity int, happenedAt time.Time, remarks string) error
|
||||||
|
// RecordCull 记录正常猪只淘汰事件。
|
||||||
|
RecordCull(operatorID uint, batchID uint, penID uint, quantity int, happenedAt time.Time, remarks string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// pigBatchService 是 PigBatchService 接口的具体实现。
|
// pigBatchService 是 PigBatchService 接口的具体实现。
|
||||||
|
|||||||
@@ -323,8 +323,8 @@ func (s *pigBatchService) RecordSickPigCull(operatorID uint, batchID uint, penID
|
|||||||
TransferTime: happenedAt,
|
TransferTime: happenedAt,
|
||||||
PigBatchID: batchID,
|
PigBatchID: batchID,
|
||||||
PenID: penID,
|
PenID: penID,
|
||||||
Quantity: -quantity, // 减少猪只数量
|
Quantity: -quantity, // 减少猪只数量
|
||||||
Type: models.PigTransferTypeEliminate, // 淘汰类型
|
Type: models.PigTransferTypeCull, // 淘汰类型
|
||||||
OperatorID: operatorID,
|
OperatorID: operatorID,
|
||||||
Remarks: remarks,
|
Remarks: remarks,
|
||||||
}
|
}
|
||||||
@@ -341,3 +341,143 @@ func (s *pigBatchService) RecordSickPigCull(operatorID uint, batchID uint, penID
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RecordDeath 记录正常猪只死亡事件。
|
||||||
|
func (s *pigBatchService) RecordDeath(operatorID uint, batchID uint, penID uint, quantity int, happenedAt time.Time, remarks string) error {
|
||||||
|
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. 检查猪栏内猪只数量是否足够死亡
|
||||||
|
currentPigsInPen, err := s.transferSvc.GetCurrentPigsInPen(tx, penID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("获取猪栏 %d 当前猪只数量失败: %w", penID, err)
|
||||||
|
}
|
||||||
|
if currentPigsInPen < quantity {
|
||||||
|
return fmt.Errorf("猪栏 %d 内猪只数量不足,当前 %d 头,尝试记录死亡 %d 头", penID, currentPigsInPen, quantity)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. 更新批次总猪只数量 (减少批次总数)
|
||||||
|
if err := s.UpdatePigBatchQuantity(operatorID, batchID, models.ChangeTypeDeath, -quantity, remarks, happenedAt); err != nil {
|
||||||
|
return fmt.Errorf("更新批次 %d 总猪只数量失败: %w", batchID, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5. 记录猪只转移日志 (减少猪栏内猪只数量)
|
||||||
|
transferLog := &models.PigTransferLog{
|
||||||
|
TransferTime: happenedAt,
|
||||||
|
PigBatchID: batchID,
|
||||||
|
PenID: penID,
|
||||||
|
Quantity: -quantity, // 减少猪只数量
|
||||||
|
Type: models.PigTransferTypeDeath,
|
||||||
|
OperatorID: operatorID,
|
||||||
|
Remarks: remarks,
|
||||||
|
}
|
||||||
|
if err := s.transferSvc.LogTransfer(tx, transferLog); err != nil {
|
||||||
|
return fmt.Errorf("记录猪只死亡转移日志失败: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("记录正常猪只死亡事件失败: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// RecordCull 记录正常猪只淘汰事件。
|
||||||
|
func (s *pigBatchService) RecordCull(operatorID uint, batchID uint, penID uint, quantity int, happenedAt time.Time, remarks string) error {
|
||||||
|
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. 检查猪栏内猪只数量是否足够淘汰
|
||||||
|
currentPigsInPen, err := s.transferSvc.GetCurrentPigsInPen(tx, penID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("获取猪栏 %d 当前猪只数量失败: %w", penID, err)
|
||||||
|
}
|
||||||
|
if currentPigsInPen < quantity {
|
||||||
|
return fmt.Errorf("猪栏 %d 内猪只数量不足,当前 %d 头,尝试记录淘汰 %d 头", penID, currentPigsInPen, quantity)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. 更新批次总猪只数量 (减少批次总数)
|
||||||
|
if err := s.UpdatePigBatchQuantity(operatorID, batchID, models.ChangeTypeCull, -quantity, remarks, happenedAt); err != nil {
|
||||||
|
return fmt.Errorf("更新批次 %d 总猪只数量失败: %w", batchID, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5. 记录猪只转移日志 (减少猪栏内猪只数量)
|
||||||
|
transferLog := &models.PigTransferLog{
|
||||||
|
TransferTime: happenedAt,
|
||||||
|
PigBatchID: batchID,
|
||||||
|
PenID: penID,
|
||||||
|
Quantity: -quantity, // 减少猪只数量
|
||||||
|
Type: models.PigTransferTypeCull,
|
||||||
|
OperatorID: operatorID,
|
||||||
|
Remarks: remarks,
|
||||||
|
}
|
||||||
|
if err := s.transferSvc.LogTransfer(tx, transferLog); err != nil {
|
||||||
|
return fmt.Errorf("记录猪只淘汰转移日志失败: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("记录正常猪只淘汰事件失败: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ const (
|
|||||||
PigTransferTypeCrossBatch PigTransferType = "跨群调栏" // 不同猪群间的调动
|
PigTransferTypeCrossBatch PigTransferType = "跨群调栏" // 不同猪群间的调动
|
||||||
PigTransferTypeSale PigTransferType = "销售" // 猪只售出
|
PigTransferTypeSale PigTransferType = "销售" // 猪只售出
|
||||||
PigTransferTypeDeath PigTransferType = "死亡" // 猪只死亡
|
PigTransferTypeDeath PigTransferType = "死亡" // 猪只死亡
|
||||||
PigTransferTypeEliminate PigTransferType = "淘汰" // 猪只淘汰
|
PigTransferTypeCull PigTransferType = "淘汰" // 猪只淘汰
|
||||||
PigTransferTypePurchase PigTransferType = "新购入" // 新购入猪只
|
PigTransferTypePurchase PigTransferType = "新购入" // 新购入猪只
|
||||||
PigTransferTypeDeliveryRoomTransfor PigTransferType = "产房转入" // 产房转入
|
PigTransferTypeDeliveryRoomTransfor PigTransferType = "产房转入" // 产房转入
|
||||||
// 可以根据业务需求添加更多类型,例如:转出到其他农场等
|
// 可以根据业务需求添加更多类型,例如:转出到其他农场等
|
||||||
|
|||||||
Reference in New Issue
Block a user