Compare commits
	
		
			2 Commits
		
	
	
		
			829f0a6253
			...
			5754a1d94c
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 5754a1d94c | |||
| 609aee2513 | 
							
								
								
									
										39
									
								
								internal/infra/models/farm_asset.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								internal/infra/models/farm_asset.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,39 @@
 | 
			
		||||
package models
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"gorm.io/gorm"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
	猪场固定资产相关模型
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
// PigHouse 定义了猪舍,是猪栏的集合
 | 
			
		||||
type PigHouse struct {
 | 
			
		||||
	gorm.Model
 | 
			
		||||
	Name        string `gorm:"size:100;not null;unique;comment:猪舍名称, 如 '育肥舍A栋'"`
 | 
			
		||||
	Description string `gorm:"size:255;comment:描述信息"`
 | 
			
		||||
	Pens        []Pen  `gorm:"foreignKey:HouseID"` // 一个猪舍包含多个猪栏
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// PenStatus 定义了猪栏的当前状态
 | 
			
		||||
type PenStatus string
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
	PenStatusEmpty      PenStatus = "空闲"
 | 
			
		||||
	PenStatusOccupied   PenStatus = "占用"
 | 
			
		||||
	PenStatusSickPen    PenStatus = "病猪栏"
 | 
			
		||||
	PenStatusRecovering PenStatus = "康复栏"
 | 
			
		||||
	PenStatusCleaning   PenStatus = "清洗消毒"
 | 
			
		||||
	PenStatusUnderMaint PenStatus = "维修中"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Pen 是猪栏的物理实体模型, 是所有空间相关数据的“锚点”
 | 
			
		||||
type Pen struct {
 | 
			
		||||
	gorm.Model
 | 
			
		||||
	PenNumber  string    `gorm:"not null;comment:猪栏的唯一编号, 如 A-01"`
 | 
			
		||||
	HouseID    uint      `gorm:"index;comment:所属猪舍ID"`
 | 
			
		||||
	PigBatchID uint      `gorm:"index;comment:关联的猪批次ID"`
 | 
			
		||||
	Capacity   int       `gorm:"not null;comment:设计容量 (头)"`
 | 
			
		||||
	Status     PenStatus `gorm:"not null;index;comment:猪栏当前状态"`
 | 
			
		||||
}
 | 
			
		||||
@@ -1,5 +1,88 @@
 | 
			
		||||
package models
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"gorm.io/gorm"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
	饲料和饲喂相关的模型
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
// RawMaterial 代表饲料的原料。
 | 
			
		||||
// 建议:所有重量单位统一存储 (例如, 全部使用 'g'),便于计算和避免转换错误。
 | 
			
		||||
type RawMaterial struct {
 | 
			
		||||
	gorm.Model
 | 
			
		||||
	Name        string  `gorm:"size:100;unique;not null;comment:原料名称"`
 | 
			
		||||
	Description string  `gorm:"size:255;comment:描述"`
 | 
			
		||||
	Quantity    float64 `gorm:"not null;comment:库存总量, 单位: g"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// RawMaterialPurchase 记录了原料的每一次采购。
 | 
			
		||||
type RawMaterialPurchase struct {
 | 
			
		||||
	gorm.Model
 | 
			
		||||
	RawMaterialID uint        `gorm:"not null;index;comment:关联的原料ID"`
 | 
			
		||||
	RawMaterial   RawMaterial `gorm:"foreignKey:RawMaterialID"`
 | 
			
		||||
	Supplier      string      `gorm:"size:100;comment:供应商"`
 | 
			
		||||
	Amount        float64     `gorm:"not null;comment:采购数量, 单位: g"`
 | 
			
		||||
	UnitPrice     float64     `gorm:"comment:单价"`
 | 
			
		||||
	TotalPrice    float64     `gorm:"comment:总价"`
 | 
			
		||||
	PurchaseDate  time.Time   `gorm:"not null;comment:采购日期"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// StockLogSourceType 定义了库存日志来源的类型
 | 
			
		||||
type StockLogSourceType string
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
	StockLogSourcePurchase      StockLogSourceType = "采购入库"
 | 
			
		||||
	StockLogSourceFeeding       StockLogSourceType = "饲喂出库"
 | 
			
		||||
	StockLogSourceDeteriorate   StockLogSourceType = "变质出库"
 | 
			
		||||
	StockLogSourceSale          StockLogSourceType = "售卖出库"
 | 
			
		||||
	StockLogSourceMiscellaneous StockLogSourceType = "杂用领取"
 | 
			
		||||
	StockLogSourceManual        StockLogSourceType = "手动盘点"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// RawMaterialStockLog 记录了原料库存的所有变动,提供了完整的追溯链。
 | 
			
		||||
type RawMaterialStockLog struct {
 | 
			
		||||
	gorm.Model
 | 
			
		||||
	RawMaterialID uint               `gorm:"not null;index;comment:关联的原料ID"`
 | 
			
		||||
	ChangeAmount  float64            `gorm:"not null;comment:变动数量, 正数为入库, 负数为出库"`
 | 
			
		||||
	SourceType    StockLogSourceType `gorm:"size:50;not null;index;comment:库存变动来源类型"`
 | 
			
		||||
	SourceID      uint               `gorm:"not null;index;comment:来源记录的ID (如 RawMaterialPurchase.ID 或 FeedUsageRecord.ID)"`
 | 
			
		||||
	HappenedAt    time.Time          `gorm:"not null;comment:业务发生时间"`
 | 
			
		||||
	Remarks       string             `gorm:"comment:备注, 如主动领取的理由等"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FeedFormula 代表饲料配方。
 | 
			
		||||
// 对于没有配方的外购饲料,可以将其视为一种特殊的 RawMaterial, 并为其创建一个仅包含它自己的 FeedFormula。
 | 
			
		||||
type FeedFormula struct {
 | 
			
		||||
	gorm.Model
 | 
			
		||||
	Name        string                 `gorm:"size:100;unique;not null;comment:配方名称"`
 | 
			
		||||
	Description string                 `gorm:"size:255;comment:描述"`
 | 
			
		||||
	Components  []FeedFormulaComponent `gorm:"foreignKey:FeedFormulaID"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FeedFormulaComponent 代表配方中的一种原料及其占比。
 | 
			
		||||
type FeedFormulaComponent struct {
 | 
			
		||||
	gorm.Model
 | 
			
		||||
	FeedFormulaID uint        `gorm:"not null;index;comment:外键到 FeedFormula"`
 | 
			
		||||
	RawMaterialID uint        `gorm:"not null;index;comment:外键到 RawMaterial"`
 | 
			
		||||
	RawMaterial   RawMaterial `gorm:"foreignKey:RawMaterialID"`
 | 
			
		||||
	Percentage    float64     `gorm:"not null;comment:该原料在配方中的百分比 (0-1.0)"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FeedUsageRecord 代表饲料使用记录。
 | 
			
		||||
// 应用层逻辑:当一条使用记录被创建时,应根据其使用的 FeedFormula,
 | 
			
		||||
// 计算出每种 RawMaterial 的消耗量,并在 RawMaterialStockLog 中创建对应的出库记录。
 | 
			
		||||
type FeedUsageRecord struct {
 | 
			
		||||
	gorm.Model
 | 
			
		||||
	PenID         uint        `gorm:"not null;index;comment:关联的猪栏ID"`
 | 
			
		||||
	Pen           Pen         `gorm:"foreignKey:PenID"`
 | 
			
		||||
	FeedFormulaID uint        `gorm:"not null;index;comment:使用的饲料配方ID"`
 | 
			
		||||
	FeedFormula   FeedFormula `gorm:"foreignKey:FeedFormulaID"`
 | 
			
		||||
	Amount        float64     `gorm:"not null;comment:使用数量, 单位: g"`
 | 
			
		||||
	RecordedAt    time.Time   `gorm:"not null;comment:记录时间"`
 | 
			
		||||
	OperatorID    uint        `gorm:"not null;comment:操作员"`
 | 
			
		||||
	Remarks       string      `gorm:"comment:备注, 如 '例行喂料, 弱猪补料' 等"`
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -66,3 +66,26 @@ type Medication struct {
 | 
			
		||||
	Manufacturer                  string             `gorm:"size:100;comment:生产厂家" json:"manufacturer"`
 | 
			
		||||
	Instructions                  datatypes.JSON     `gorm:"type:jsonb;comment:使用说明" json:"instructions"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// MedicationReasonType 定义了用药原因
 | 
			
		||||
type MedicationReasonType string
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
	ReasonTypePreventive MedicationReasonType = "预防"
 | 
			
		||||
	ReasonTypeTreatment  MedicationReasonType = "治疗"
 | 
			
		||||
	ReasonTypeHealthCare MedicationReasonType = "保健"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// GroupMedicationLog 记录了对整个猪批次的用药情况
 | 
			
		||||
type GroupMedicationLog struct {
 | 
			
		||||
	gorm.Model
 | 
			
		||||
	PigBatchID   uint                 `gorm:"not null;index;comment:关联的猪批次ID"`
 | 
			
		||||
	MedicationID uint                 `gorm:"not null;index;comment:关联的药品ID"`
 | 
			
		||||
	Medication   Medication           `gorm:"foreignKey:MedicationID"` // 预加载药品信息
 | 
			
		||||
	DosageUsed   float64              `gorm:"not null;comment:使用的总剂量 (单位由药品决定,如g或ml)"`
 | 
			
		||||
	TargetCount  int                  `gorm:"not null;comment:用药对象数量"`
 | 
			
		||||
	Reason       MedicationReasonType `gorm:"size:20;not null;comment:用药原因"`
 | 
			
		||||
	Description  string               `gorm:"size:255;comment:具体描述,如'治疗呼吸道病'"`
 | 
			
		||||
	Operator     string               `gorm:"size:50;comment:操作员"`
 | 
			
		||||
	HappenedAt   time.Time            `gorm:"not null;default:CURRENT_TIMESTAMP;comment:用药时间"`
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,74 @@
 | 
			
		||||
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"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 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 {
 | 
			
		||||
	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:变更后总数"`
 | 
			
		||||
	BeforeSickCount int           `gorm:"not null;comment:变更前病猪数"`
 | 
			
		||||
	AfterSickCount  int           `gorm:"not null;comment:变更后病猪数"`
 | 
			
		||||
	Operator        string        `gorm:"size:50;comment:操作员"`
 | 
			
		||||
	HappenedAt      time.Time     `gorm:"not null;default:CURRENT_TIMESTAMP;comment:事件发生时间"`
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user