From 47c72dff3e1f1e2857ab3dc8007fd137143d3082 Mon Sep 17 00:00:00 2001 From: huang <1724659546@qq.com> Date: Sun, 5 Oct 2025 21:20:22 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=B0=E5=BD=95=E8=B0=83=E7=8C=AA=E4=BA=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/domain/pig/pig_batch.go | 4 ++-- internal/domain/pig/pig_batch_service.go | 14 +++++++---- internal/infra/database/postgres.go | 2 ++ internal/infra/models/models.go | 1 + internal/infra/models/pig_transfer.go | 30 +++++++++++++++++++----- 5 files changed, 38 insertions(+), 13 deletions(-) diff --git a/internal/domain/pig/pig_batch.go b/internal/domain/pig/pig_batch.go index 1983684..53ffa8f 100644 --- a/internal/domain/pig/pig_batch.go +++ b/internal/domain/pig/pig_batch.go @@ -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) diff --git a/internal/domain/pig/pig_batch_service.go b/internal/domain/pig/pig_batch_service.go index 12d2868..cdc03b7 100644 --- a/internal/domain/pig/pig_batch_service.go +++ b/internal/domain/pig/pig_batch_service.go @@ -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 } diff --git a/internal/infra/database/postgres.go b/internal/infra/database/postgres.go index 04058ff..0dfc003 100644 --- a/internal/infra/database/postgres.go +++ b/internal/infra/database/postgres.go @@ -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 { diff --git a/internal/infra/models/models.go b/internal/infra/models/models.go index 1e6c116..6b4296a 100644 --- a/internal/infra/models/models.go +++ b/internal/infra/models/models.go @@ -41,6 +41,7 @@ func GetAllModels() []interface{} { &PigBatchLog{}, &WeighingBatch{}, &WeighingRecord{}, + &PigTransferLog{}, // Feed Models &RawMaterial{}, diff --git a/internal/infra/models/pig_transfer.go b/internal/infra/models/pig_transfer.go index 811076d..2c2a7b0 100644 --- a/internal/infra/models/pig_transfer.go +++ b/internal/infra/models/pig_transfer.go @@ -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" }