记录调猪人

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

@@ -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 {

View File

@@ -41,6 +41,7 @@ func GetAllModels() []interface{} {
&PigBatchLog{},
&WeighingBatch{},
&WeighingRecord{},
&PigTransferLog{},
// Feed Models
&RawMaterial{},

View File

@@ -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"
}