定义model

This commit is contained in:
2025-10-03 20:32:34 +08:00
parent 8cbe313c89
commit 6cc6d719e1
6 changed files with 46 additions and 22 deletions

View File

@@ -25,7 +25,7 @@ func (RawMaterial) TableName() string {
// RawMaterialPurchase 记录了原料的每一次采购。
type RawMaterialPurchase struct {
ID uint `gorm:"primaryKey"`
gorm.Model
RawMaterialID uint `gorm:"not null;index;comment:关联的原料ID"`
RawMaterial RawMaterial `gorm:"foreignKey:RawMaterialID"`
Supplier string `gorm:"size:100;comment:供应商"`
@@ -34,8 +34,6 @@ type RawMaterialPurchase struct {
TotalPrice float64 `gorm:"comment:总价"`
PurchaseDate time.Time `gorm:"primaryKey;comment:采购日期"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
}
func (RawMaterialPurchase) TableName() string {
@@ -56,16 +54,13 @@ const (
// RawMaterialStockLog 记录了原料库存的所有变动,提供了完整的追溯链。
type RawMaterialStockLog struct {
ID uint `gorm:"primaryKey"`
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:"primaryKey;comment:业务发生时间"`
Remarks string `gorm:"comment:备注, 如主动领取的理由等"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
}
func (RawMaterialStockLog) TableName() string {
@@ -102,7 +97,7 @@ func (FeedFormulaComponent) TableName() string {
// 应用层逻辑:当一条使用记录被创建时,应根据其使用的 FeedFormula,
// 计算出每种 RawMaterial 的消耗量,并在 RawMaterialStockLog 中创建对应的出库记录。
type FeedUsageRecord struct {
ID uint `gorm:"primaryKey"`
gorm.Model
PenID uint `gorm:"not null;index;comment:关联的猪栏ID"`
Pen Pen `gorm:"foreignKey:PenID"`
FeedFormulaID uint `gorm:"not null;index;comment:使用的饲料配方ID"`
@@ -111,9 +106,6 @@ type FeedUsageRecord struct {
RecordedAt time.Time `gorm:"primaryKey;comment:记录时间"`
OperatorID uint `gorm:"not null;comment:操作员"`
Remarks string `gorm:"comment:备注, 如 '例行喂料, 弱猪补料' 等"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
}
func (FeedUsageRecord) TableName() string {