记录调猪人

This commit is contained in:
2025-10-05 21:20:22 +08:00
parent 811c6a09c5
commit 47c72dff3e
5 changed files with 38 additions and 13 deletions

View File

@@ -31,10 +31,10 @@ var (
// 它抽象了所有与猪批次相关的操作,使得应用层可以依赖于此接口,而不是具体的实现。
type PigBatchService interface {
// TransferPigsWithinBatch 处理同一个猪群内部的调栏业务。
TransferPigsWithinBatch(batchID uint, fromPenID uint, toPenID uint, quantity uint) error
TransferPigsWithinBatch(batchID uint, fromPenID uint, toPenID uint, quantity uint, operatorID uint, remarks string) error
// TransferPigsAcrossBatches 处理跨猪群的调栏业务。
TransferPigsAcrossBatches(sourceBatchID uint, destBatchID uint, fromPenID uint, toPenID uint, quantity uint) error
TransferPigsAcrossBatches(sourceBatchID uint, destBatchID uint, fromPenID uint, toPenID uint, quantity uint, operatorID uint, remarks string) error
// CreatePigBatch 创建一个新的猪批次。
CreatePigBatch(operatorID uint, batch *models.PigBatch) (*models.PigBatch, error)

View File

@@ -237,7 +237,7 @@ func (s *pigBatchService) UpdatePigBatchPens(batchID uint, desiredPenIDs []uint)
// --- 新增的调栏业务实现 ---
// executeTransferAndLog 是一个私有辅助方法,用于封装创建和记录迁移日志的通用逻辑。
func (s *pigBatchService) executeTransferAndLog(tx *gorm.DB, fromBatchID, toBatchID, fromPenID, toPenID uint, quantity int, transferType string) error {
func (s *pigBatchService) executeTransferAndLog(tx *gorm.DB, fromBatchID, toBatchID, fromPenID, toPenID uint, quantity int, transferType models.PigTransferType, operatorID uint, remarks string) error {
// 1. 生成关联ID
correlationID := uuid.New().String()
@@ -249,6 +249,8 @@ func (s *pigBatchService) executeTransferAndLog(tx *gorm.DB, fromBatchID, toBatc
Quantity: -quantity, // 调出为负数
Type: transferType,
CorrelationID: correlationID,
OperatorID: operatorID,
Remarks: remarks,
}
// 3. 创建调入日志
@@ -259,6 +261,8 @@ func (s *pigBatchService) executeTransferAndLog(tx *gorm.DB, fromBatchID, toBatc
Quantity: quantity, // 调入为正数
Type: transferType,
CorrelationID: correlationID,
OperatorID: operatorID,
Remarks: remarks,
}
// 4. 调用子服务记录日志
@@ -273,7 +277,7 @@ func (s *pigBatchService) executeTransferAndLog(tx *gorm.DB, fromBatchID, toBatc
}
// TransferPigsWithinBatch 实现了同一个猪群内部的调栏业务。
func (s *pigBatchService) TransferPigsWithinBatch(batchID uint, fromPenID uint, toPenID uint, quantity uint) error {
func (s *pigBatchService) TransferPigsWithinBatch(batchID uint, fromPenID uint, toPenID uint, quantity uint, operatorID uint, remarks string) error {
if fromPenID == toPenID {
return errors.New("源猪栏和目标猪栏不能相同")
}
@@ -300,7 +304,7 @@ func (s *pigBatchService) TransferPigsWithinBatch(batchID uint, fromPenID uint,
}
// 2. 调用通用辅助方法执行日志记录
err = s.executeTransferAndLog(tx, batchID, batchID, fromPenID, toPenID, int(quantity), "群内调栏")
err = s.executeTransferAndLog(tx, batchID, batchID, fromPenID, toPenID, int(quantity), "群内调栏", operatorID, remarks)
if err != nil {
return err
}
@@ -311,7 +315,7 @@ func (s *pigBatchService) TransferPigsWithinBatch(batchID uint, fromPenID uint,
}
// TransferPigsAcrossBatches 实现了跨猪群的调栏业务。
func (s *pigBatchService) TransferPigsAcrossBatches(sourceBatchID uint, destBatchID uint, fromPenID uint, toPenID uint, quantity uint) error {
func (s *pigBatchService) TransferPigsAcrossBatches(sourceBatchID uint, destBatchID uint, fromPenID uint, toPenID uint, quantity uint, operatorID uint, remarks string) error {
if sourceBatchID == destBatchID {
return errors.New("源猪群和目标猪群不能相同")
}
@@ -339,7 +343,7 @@ func (s *pigBatchService) TransferPigsAcrossBatches(sourceBatchID uint, destBatc
}
// 2. 调用通用辅助方法执行日志记录
err = s.executeTransferAndLog(tx, sourceBatchID, destBatchID, fromPenID, toPenID, int(quantity), "跨群调栏")
err = s.executeTransferAndLog(tx, sourceBatchID, destBatchID, fromPenID, toPenID, int(quantity), "跨群调栏", operatorID, remarks)
if err != nil {
return err
}