Compare commits
	
		
			2 Commits
		
	
	
		
			cb20732205
			...
			47c72dff3e
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 47c72dff3e | |||
| 811c6a09c5 | 
@@ -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)
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
		}
 | 
			
		||||
 
 | 
			
		||||
@@ -167,6 +167,7 @@ func (ps *PostgresStorage) creatingHyperTable() error {
 | 
			
		||||
		{models.PigBatchLog{}, "happened_at"},
 | 
			
		||||
		{models.WeighingBatch{}, "weighing_time"},
 | 
			
		||||
		{models.WeighingRecord{}, "weighing_time"},
 | 
			
		||||
		{models.PigTransferLog{}, "transfer_time"},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for _, table := range tablesToConvert {
 | 
			
		||||
@@ -203,6 +204,7 @@ func (ps *PostgresStorage) applyCompressionPolicies() error {
 | 
			
		||||
		{models.PigBatchLog{}, "pig_batch_id"},
 | 
			
		||||
		{models.WeighingBatch{}, "pig_batch_id"},
 | 
			
		||||
		{models.WeighingRecord{}, "weighing_batch_id"},
 | 
			
		||||
		{models.PigTransferLog{}, "pig_batch_id"},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for _, policy := range policies {
 | 
			
		||||
 
 | 
			
		||||
@@ -41,6 +41,7 @@ func GetAllModels() []interface{} {
 | 
			
		||||
		&PigBatchLog{},
 | 
			
		||||
		&WeighingBatch{},
 | 
			
		||||
		&WeighingRecord{},
 | 
			
		||||
		&PigTransferLog{},
 | 
			
		||||
 | 
			
		||||
		// Feed Models
 | 
			
		||||
		&RawMaterial{},
 | 
			
		||||
 
 | 
			
		||||
@@ -59,8 +59,6 @@ const (
 | 
			
		||||
	ChangeTypeSale        LogChangeType = "销售"
 | 
			
		||||
	ChangeTypeTransferIn  LogChangeType = "转入"
 | 
			
		||||
	ChangeTypeTransferOut LogChangeType = "转出"
 | 
			
		||||
	ChangeTypeTreatment   LogChangeType = "治疗" // 仅改变健康状态,不改变总数
 | 
			
		||||
	ChangeTypeRecovering  LogChangeType = "康复" // 仅改变健康状态,不改变总数
 | 
			
		||||
	ChangeTypeCorrection  LogChangeType = "盘点校正"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -6,14 +6,32 @@ import (
 | 
			
		||||
	"gorm.io/gorm"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// PigTransferType 定义了猪只迁移的类型
 | 
			
		||||
type PigTransferType string
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
	PigTransferTypeInternal   PigTransferType = "群内调栏" // 同一猪群内猪栏间的调动
 | 
			
		||||
	PigTransferTypeCrossBatch PigTransferType = "跨群调栏" // 不同猪群间的调动
 | 
			
		||||
	PigTransferTypeSale       PigTransferType = "销售"   // 猪只售出
 | 
			
		||||
	PigTransferTypeDeath      PigTransferType = "死亡"   // 猪只死亡
 | 
			
		||||
	PigTransferTypePurchase   PigTransferType = "新购入"  // 新购入猪只
 | 
			
		||||
	// 可以根据业务需求添加更多类型,例如:淘汰、转出到其他农场等
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// PigTransferLog 记录了每一次猪只数量在猪栏间的变动事件。
 | 
			
		||||
// 它作为事件溯源的基础,用于推算任意时间点猪栏的猪只数量。
 | 
			
		||||
type PigTransferLog struct {
 | 
			
		||||
	gorm.Model
 | 
			
		||||
	TransferTime  time.Time `json:"transfer_time"`  // 迁移发生时间
 | 
			
		||||
	PigBatchID    uint      `json:"pig_batch_id"`   // 关联的猪群ID
 | 
			
		||||
	PenID         uint      `json:"pen_id"`         // 发生变动的猪栏ID
 | 
			
		||||
	Quantity      int       `json:"quantity"`       // 变动数量(正数表示增加,负数表示减少)
 | 
			
		||||
	Type          string    `json:"type"`           // 变动类型 (e.g., "群内调栏", "跨群调栏", "销售", "死亡", "新购入")
 | 
			
		||||
	CorrelationID string    `json:"correlation_id"` // 用于关联一次完整操作(如一次调栏会产生两条日志)
 | 
			
		||||
	TransferTime  time.Time       `gorm:"primaryKey;comment:迁移发生时间" json:"transfer_time"`         // 迁移发生时间,作为联合主键
 | 
			
		||||
	PigBatchID    uint            `gorm:"primaryKey;comment:关联的猪群ID" json:"pig_batch_id"`         // 关联的猪群ID,作为联合主键
 | 
			
		||||
	PenID         uint            `gorm:"primaryKey;comment:发生变动的猪栏ID" json:"pen_id"`             // 发生变动的猪栏ID,作为联合主键
 | 
			
		||||
	Quantity      int             `gorm:"not null;comment:变动数量(正数表示增加,负数表示减少)" json:"quantity"`   // 变动数量(正数表示增加,负数减少)
 | 
			
		||||
	Type          PigTransferType `gorm:"not null;comment:变动类型" json:"type"`                      // 变动类型,使用枚举类型
 | 
			
		||||
	CorrelationID string          `gorm:"comment:用于关联一次完整操作(如一次调栏会产生两条日志)" json:"correlation_id"` // 用于关联一次完整操作
 | 
			
		||||
	OperatorID    uint            `gorm:"not null;comment:操作员ID" json:"operator_id"`              // 操作员ID
 | 
			
		||||
	Remarks       string          `gorm:"comment:备注" json:"remarks"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (p PigTransferLog) TableName() string {
 | 
			
		||||
	return "pig_transfer_logs"
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user