Files
pig-farm-controller/internal/infra/models/pig_batch.go
2025-10-05 21:58:06 +08:00

156 lines
6.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package models
import (
"time"
"gorm.io/gorm"
)
/*
和猪只、猪群本身相关的模型
*/
// PigBatchStatus 定义了猪批次所处的不同阶段或状态
type PigBatchStatus string
const (
BatchStatusWeaning PigBatchStatus = "保育" // 从断奶到保育结束
BatchStatusGrowing PigBatchStatus = "生长" // 生长育肥阶段
BatchStatusFinishing PigBatchStatus = "育肥" // 最后的育肥阶段
BatchStatusForSale PigBatchStatus = "待售" // 达到出栏标准
BatchStatusSold PigBatchStatus = "已出售"
BatchStatusArchived PigBatchStatus = "已归档" // 批次结束(如全群淘汰等)
)
// PigBatchOriginType 定义了猪批次的来源
type PigBatchOriginType string
const (
OriginTypeSelfFarrowed PigBatchOriginType = "自繁"
OriginTypePurchased PigBatchOriginType = "外购"
)
// PigBatch 是猪批次的核心模型,代表了一群被共同管理的猪
type PigBatch struct {
gorm.Model
BatchNumber string `gorm:"size:50;not null;uniqueIndex;comment:批次编号,如 2024-W25-A01"`
OriginType PigBatchOriginType `gorm:"size:20;not null;comment:批次来源 (自繁, 外购)"`
StartDate time.Time `gorm:"not null;comment:批次开始日期 (如转入日或购买日)"`
EndDate time.Time `gorm:"not null;comment:批次结束日期 (全部淘汰或售出)"`
InitialCount int `gorm:"not null;comment:初始数量"`
Status PigBatchStatus `gorm:"size:20;not null;index;comment:批次状态"`
}
func (PigBatch) TableName() string {
return "pig_batches"
}
// IsActive 判断猪批次是否处于活跃状态
func (pb PigBatch) IsActive() bool {
return pb.Status != BatchStatusSold && pb.Status != BatchStatusArchived
}
// LogChangeType 定义了猪批次数量变更的类型
type LogChangeType string
const (
ChangeTypeDeath LogChangeType = "死亡"
ChangeTypeCull LogChangeType = "淘汰"
ChangeTypeSale LogChangeType = "销售"
ChangeTypeTransferIn LogChangeType = "转入"
ChangeTypeTransferOut LogChangeType = "转出"
ChangeTypeCorrection LogChangeType = "盘点校正"
)
// PigBatchLog 记录了猪批次数量或状态的每一次变更
type PigBatchLog struct {
gorm.Model
PigBatchID uint `gorm:"not null;index;comment:关联的猪批次ID"`
ChangeType LogChangeType `gorm:"size:20;not null;comment:变更类型"`
ChangeCount int `gorm:"not null;comment:数量变化,负数表示减少"`
Reason string `gorm:"size:255;comment:变更原因描述"`
BeforeCount int `gorm:"not null;comment:变更前总数"`
AfterCount int `gorm:"not null;comment:变更后总数"`
OperatorID uint `gorm:"comment:操作员ID"`
HappenedAt time.Time `gorm:"primaryKey;comment:事件发生时间"`
}
func (PigBatchLog) TableName() string {
return "pig_batch_logs"
}
// WeighingBatch 记录了一次批次称重的信息
type WeighingBatch struct {
gorm.Model
WeighingTime time.Time `gorm:"primaryKey;comment:称重时间"`
Description string `gorm:"size:255;comment:批次称重描述"`
PigBatchID uint `gorm:"not null;index;comment:关联的猪批次ID"`
}
func (WeighingBatch) TableName() string {
return "weighing_batches"
}
// WeighingRecord 记录了单次称重信息
type WeighingRecord struct {
gorm.Model
Weight float64 `gorm:"not null;comment:单只猪重量 (kg)"`
WeighingBatchID uint `gorm:"not null;index;comment:关联的批次称重ID"`
PenID uint `gorm:"not null;index;comment:所在猪圈ID"`
OperatorID uint `gorm:"not null;comment:操作员ID"`
Remark string `gorm:"size:255;comment:备注"`
WeighingTime time.Time `gorm:"primaryKey;comment:称重时间"`
}
func (WeighingRecord) TableName() string {
return "weighing_records"
}
// PigBatchSickPigLogChangeType 定义了病猪变化日志的类型
type PigBatchSickPigLogChangeType string
const (
SickPigChangeTypeAdd PigBatchSickPigLogChangeType = "新增" // 新增病猪
SickPigChangeTypeRemove PigBatchSickPigLogChangeType = "移除" // 移除病猪 (康复, 死亡, 转出等)
)
// PigBatchSickPigTreatmentLocation 定义了病猪治疗地点
type PigBatchSickPigTreatmentLocation string
const (
TreatmentLocationOnSite PigBatchSickPigTreatmentLocation = "原地治疗"
TreatmentLocationSickBay PigBatchSickPigTreatmentLocation = "病猪栏治疗"
)
// PigBatchSickPigReasonType 定义了病猪变化的原因类型
type PigBatchSickPigReasonType string
const (
SickPigReasonTypeIllness PigBatchSickPigReasonType = "患病" // 猪只患病
SickPigReasonTypeRecovery PigBatchSickPigReasonType = "康复" // 猪只康复
SickPigReasonTypeDeath PigBatchSickPigReasonType = "死亡" // 猪只死亡
SickPigReasonTypeEliminate PigBatchSickPigReasonType = "淘汰" // 猪只淘汰
SickPigReasonTypeTransferIn PigBatchSickPigReasonType = "转入" // 病猪转入当前批次
SickPigReasonTypeTransferOut PigBatchSickPigReasonType = "转出" // 病猪转出当前批次 (例如转到其他批次或出售)
SickPigReasonTypeOther PigBatchSickPigReasonType = "其他" // 其他原因
)
// PigBatchSickPigLog 记录了猪批次中病猪数量的变化日志
type PigBatchSickPigLog struct {
gorm.Model
PigBatchID uint `gorm:"primaryKey;comment:关联的猪批次ID"`
PenID uint `gorm:"not null;index;comment:所在猪圈ID"`
PigIDs string `gorm:"size:500;comment:涉及的猪只ID列表逗号分隔"`
ChangeType PigBatchSickPigLogChangeType `gorm:"size:20;not null;comment:变化类型 (新增, 移除)"`
ChangeCount int `gorm:"not null;comment:变化数量, 正数表示新增, 负数表示移除"`
Reason PigBatchSickPigReasonType `gorm:"size:20;not null;comment:变化原因 (如: 患病, 康复, 死亡, 转入, 转出, 其他)"`
Remarks string `gorm:"size:255;comment:备注"`
TreatmentLocation PigBatchSickPigTreatmentLocation `gorm:"size:50;comment:治疗地点"`
OperatorID uint `gorm:"comment:操作员ID"`
HappenedAt time.Time `gorm:"primaryKey;comment:事件发生时间"`
}
func (PigBatchSickPigLog) TableName() string {
return "pig_batch_sick_pig_logs"
}