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:批次开始日期 (如转入日或购买日)"` InitialCount int `gorm:"not null;comment:初始数量"` CurrentCount int `gorm:"not null;comment:当前存栏数量"` CurrentSickCount int `gorm:"not null;default:0;comment:当前病猪数量"` AverageWeight float64 `gorm:"comment:平均体重 (kg)"` Status PigBatchStatus `gorm:"size:20;not null;index;comment:批次状态"` Pens []Pen `gorm:"foreignKey:PigBatchID;comment:所在圈舍ID"` } func (PigBatch) TableName() string { return "pig_batches" } // LogChangeType 定义了猪批次数量变更的类型 type LogChangeType string const ( ChangeTypeDeath LogChangeType = "死亡" ChangeTypeCull LogChangeType = "淘汰" ChangeTypeSale LogChangeType = "销售" ChangeTypeTransferIn LogChangeType = "转入" ChangeTypeTransferOut LogChangeType = "转出" ChangeTypeTreatment LogChangeType = "治疗" // 仅改变健康状态,不改变总数 ChangeTypeRecovering LogChangeType = "康复" // 仅改变健康状态,不改变总数 ChangeTypeCorrection LogChangeType = "盘点校正" ) // PigBatchLog 记录了猪批次数量或状态的每一次变更 type PigBatchLog struct { ID uint `gorm:"primaryKey"` 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:变更后总数"` BeforeSickCount int `gorm:"not null;comment:变更前病猪数"` AfterSickCount int `gorm:"not null;comment:变更后病猪数"` Operator string `gorm:"size:50;comment:操作员"` HappenedAt time.Time `gorm:"primaryKey;comment:事件发生时间"` CreatedAt time.Time UpdatedAt time.Time DeletedAt gorm.DeletedAt `gorm:"index"` } func (PigBatchLog) TableName() string { return "pig_batch_logs" }