uint/uint64全部改为uint32
This commit is contained in:
@@ -2,8 +2,6 @@ package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// AlarmSourceType 定义了告警的来源类型
|
||||
@@ -46,13 +44,13 @@ const (
|
||||
)
|
||||
|
||||
// ActiveAlarm 活跃告警
|
||||
// 活跃告警会被更新(如忽略状态),因此保留 gorm.Model 以包含所有标准字段。
|
||||
// 活跃告警会被更新(如忽略状态),因此保留 Model 以包含所有标准字段。
|
||||
type ActiveAlarm struct {
|
||||
gorm.Model
|
||||
Model
|
||||
|
||||
SourceType AlarmSourceType `gorm:"type:varchar(50);not null;index:idx_alarm_uniqueness;comment:告警来源类型" json:"source_type"`
|
||||
// SourceID 告警来源ID,其具体含义取决于 SourceType 字段 (例如:设备ID, 区域主控ID, 猪栏ID)。
|
||||
SourceID uint `gorm:"not null;index:idx_alarm_uniqueness;comment:告警来源ID" json:"source_id"`
|
||||
SourceID uint32 `gorm:"not null;index:idx_alarm_uniqueness;comment:告警来源ID" json:"source_id"`
|
||||
|
||||
// AlarmCode 是一个机器可读的、标准化的告警类型标识。
|
||||
// 它与 SourceType 和 SourceID 共同构成一个活跃告警的唯一标识。
|
||||
@@ -79,15 +77,15 @@ func (ActiveAlarm) TableName() string {
|
||||
}
|
||||
|
||||
// HistoricalAlarm 历史告警
|
||||
// 历史告警是不可变归档数据,我们移除 gorm.Model,并手动定义字段。
|
||||
// 历史告警是不可变归档数据,我们移除 Model,并手动定义字段。
|
||||
// ID 和 TriggerTime 共同构成联合主键,以满足 TimescaleDB 超表的要求。
|
||||
type HistoricalAlarm struct {
|
||||
// 手动定义主键,ID 仍然自增
|
||||
ID uint `gorm:"primaryKey;autoIncrement;comment:主键ID" json:"id"`
|
||||
ID uint32 `gorm:"primaryKey;autoIncrement;comment:主键ID" json:"id"`
|
||||
|
||||
SourceType AlarmSourceType `gorm:"type:varchar(50);not null;index;comment:告警来源类型" json:"source_type"`
|
||||
// SourceID 告警来源ID,其具体含义取决于 SourceType 字段 (例如:设备ID, 区域主控ID, 猪栏ID)。
|
||||
SourceID uint `gorm:"not null;index;comment:告警来源ID" json:"source_id"`
|
||||
SourceID uint32 `gorm:"not null;index;comment:告警来源ID" json:"source_id"`
|
||||
|
||||
// AlarmCode 是一个机器可读的、标准化的告警类型标识。
|
||||
AlarmCode AlarmCode `gorm:"type:varchar(100);not null;index;comment:告警代码" json:"alarm_code"`
|
||||
@@ -99,8 +97,8 @@ type HistoricalAlarm struct {
|
||||
ResolveTime time.Time `gorm:"not null;comment:告警解决时间" json:"resolve_time"`
|
||||
ResolveMethod string `gorm:"comment:告警解决方式" json:"resolve_method"`
|
||||
|
||||
// ResolvedBy 使用指针类型 *uint 来表示可为空解决人, 当字段为空时表示系统自动解决的
|
||||
ResolvedBy *uint `gorm:"comment:告警解决人" json:"resolved_by"`
|
||||
// ResolvedBy 使用指针类型 *uint32 来表示可为空解决人, 当字段为空时表示系统自动解决的
|
||||
ResolvedBy *uint32 `gorm:"comment:告警解决人" json:"resolved_by"`
|
||||
}
|
||||
|
||||
// TableName 指定 HistoricalAlarm 结构体对应的数据库表名
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"strings"
|
||||
|
||||
"gorm.io/datatypes"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// --- Properties 结构体定义 ---
|
||||
@@ -19,7 +18,7 @@ type Bus485Properties struct {
|
||||
|
||||
// AreaController 是一个LoRa转总线(如485)的通信网关
|
||||
type AreaController struct {
|
||||
gorm.Model
|
||||
Model
|
||||
|
||||
// Name 是主控的业务名称,例如 "1号猪舍主控"
|
||||
Name string `gorm:"not null;unique" json:"name"`
|
||||
@@ -53,20 +52,20 @@ func (AreaController) TableName() string {
|
||||
|
||||
// Device 代表系统中的所有普通设备
|
||||
type Device struct {
|
||||
// gorm.Model 内嵌了标准模型字段 (ID, CreatedAt, UpdatedAt, DeletedAt)
|
||||
gorm.Model
|
||||
// Model 内嵌了标准模型字段 (ID, CreatedAt, UpdatedAt, DeletedAt)
|
||||
Model
|
||||
|
||||
// Name 是设备的业务名称,应清晰可读,例如 "1号猪舍温度传感器"
|
||||
Name string `gorm:"not null" json:"name"`
|
||||
|
||||
// DeviceTemplateID 是设备模板的外键
|
||||
DeviceTemplateID uint `gorm:"not null;index" json:"device_template_id"`
|
||||
DeviceTemplateID uint32 `gorm:"not null;index" json:"device_template_id"`
|
||||
|
||||
// DeviceTemplate 是设备的模板,包含了设备的通用信息
|
||||
DeviceTemplate DeviceTemplate `json:"device_template"`
|
||||
|
||||
// AreaControllerID 是区域主控的外键
|
||||
AreaControllerID uint `gorm:"not null;index" json:"area_controller_id"`
|
||||
AreaControllerID uint32 `gorm:"not null;index" json:"area_controller_id"`
|
||||
|
||||
// AreaController 是设备所属的区域主控
|
||||
AreaController AreaController `json:"area_controller"`
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"fmt"
|
||||
|
||||
"gorm.io/datatypes"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// ModbusFunctionCode 定义Modbus功能码的枚举类型
|
||||
@@ -106,7 +105,7 @@ func (sc *SensorCommands) SelfCheck() error {
|
||||
|
||||
// DeviceTemplate 代表一种物理设备的类型。
|
||||
type DeviceTemplate struct {
|
||||
gorm.Model
|
||||
Model
|
||||
|
||||
// Name 是此模板的唯一名称, 例如 "FanModel-XYZ-2000" 或 "TempSensor-T1"
|
||||
Name string `gorm:"not null;unique" json:"name"`
|
||||
|
||||
@@ -27,12 +27,12 @@ const (
|
||||
// PlanExecutionLog 记录整个计划的一次执行历史
|
||||
|
||||
type PlanExecutionLog struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
ID uint32 `gorm:"primaryKey"`
|
||||
CreatedAt time.Time `gorm:"primaryKey"` // 作为联合主键方便只查询热点数据
|
||||
UpdatedAt time.Time
|
||||
DeletedAt gorm.DeletedAt `gorm:"index"`
|
||||
|
||||
PlanID uint `gorm:"index"`
|
||||
PlanID uint32 `gorm:"index"`
|
||||
Status ExecutionStatus
|
||||
StartedAt time.Time
|
||||
EndedAt time.Time
|
||||
@@ -46,12 +46,12 @@ func (PlanExecutionLog) TableName() string {
|
||||
|
||||
// TaskExecutionLog 记录单个任务的一次执行历史
|
||||
type TaskExecutionLog struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
ID uint32 `gorm:"primaryKey"`
|
||||
CreatedAt time.Time `gorm:"primaryKey"` // 作为联合主键方便只查询热点数据
|
||||
UpdatedAt time.Time
|
||||
DeletedAt gorm.DeletedAt `gorm:"index"`
|
||||
|
||||
PlanExecutionLogID uint `gorm:"index"` // 关联到某次计划执行
|
||||
PlanExecutionLogID uint32 `gorm:"index"` // 关联到某次计划执行
|
||||
|
||||
// TaskID 使用 int 类型以容纳特殊的负数ID,代表系统任务
|
||||
TaskID int `gorm:"index"`
|
||||
@@ -106,7 +106,7 @@ type DeviceCommandLog struct {
|
||||
|
||||
// DeviceID 是接收此下行任务的设备的ID。
|
||||
// 对于 LoRaWAN,这通常是区域主控设备的ID。
|
||||
DeviceID uint `gorm:"not null;index" json:"device_id"`
|
||||
DeviceID uint32 `gorm:"not null;index" json:"device_id"`
|
||||
|
||||
// SentAt 记录下行任务最初发送的时间。
|
||||
SentAt time.Time `gorm:"primaryKey" json:"sent_at"`
|
||||
@@ -133,7 +133,7 @@ type PendingCollection struct {
|
||||
|
||||
// DeviceID 是接收此任务的设备ID
|
||||
// 对于 LoRaWAN,这通常是区域主控设备的ID。
|
||||
DeviceID uint `gorm:"index"`
|
||||
DeviceID uint32 `gorm:"index"`
|
||||
|
||||
// CommandMetadata 存储了此次采集任务对应的设备ID列表,顺序与设备响应值的顺序一致。
|
||||
CommandMetadata UintArray `gorm:"type:bigint[]"`
|
||||
@@ -183,13 +183,13 @@ func (a AuditContextKey) String() string {
|
||||
// UserActionLog 记录用户的操作历史,用于审计
|
||||
type UserActionLog struct {
|
||||
// 用 ID 和 Time 组成复合主键, 防止高并发时时间重复
|
||||
ID uint `gorm:"primaryKey"`
|
||||
ID uint32 `gorm:"primaryKey"`
|
||||
|
||||
// Time 是操作发生的时间,作为主键和超表的时间分区键
|
||||
Time time.Time `gorm:"primaryKey" json:"time"`
|
||||
|
||||
// --- Who (谁) ---
|
||||
UserID uint `gorm:"not null" json:"user_id,omitempty"`
|
||||
UserID uint32 `gorm:"not null" json:"user_id,omitempty"`
|
||||
Username string `json:"username,omitempty"` // 操作发生时用户名的快照
|
||||
|
||||
// --- Where (何地) ---
|
||||
|
||||
@@ -1,16 +1,12 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
/*
|
||||
猪场固定资产相关模型
|
||||
*/
|
||||
|
||||
// PigHouse 定义了猪舍,是猪栏的集合
|
||||
type PigHouse struct {
|
||||
gorm.Model
|
||||
Model
|
||||
Name string `gorm:"size:100;not null;unique;comment:猪舍名称, 如 '育肥舍A栋'"`
|
||||
Description string `gorm:"size:255;comment:描述信息"`
|
||||
Pens []Pen `gorm:"foreignKey:HouseID"` // 一个猪舍包含多个猪栏
|
||||
@@ -30,10 +26,10 @@ const (
|
||||
|
||||
// Pen 是猪栏的物理实体模型, 是所有空间相关数据的“锚点”
|
||||
type Pen struct {
|
||||
gorm.Model
|
||||
Model
|
||||
PenNumber string `gorm:"not null;comment:猪栏的唯一编号, 如 A-01"`
|
||||
HouseID uint `gorm:"index;comment:所属猪舍ID"`
|
||||
PigBatchID *uint `gorm:"index;comment:关联的猪批次ID"`
|
||||
HouseID uint32 `gorm:"index;comment:所属猪舍ID"`
|
||||
PigBatchID *uint32 `gorm:"index;comment:关联的猪批次ID"`
|
||||
Capacity int `gorm:"not null;comment:设计容量 (头)"`
|
||||
Status PenStatus `gorm:"not null;index;comment:猪栏当前状态"`
|
||||
}
|
||||
|
||||
@@ -2,8 +2,6 @@ package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
/*
|
||||
@@ -13,7 +11,7 @@ import (
|
||||
// RawMaterial 代表饲料的原料。
|
||||
// 建议:所有重量单位统一存储 (例如, 全部使用 'g'),便于计算和避免转换错误。
|
||||
type RawMaterial struct {
|
||||
gorm.Model
|
||||
Model
|
||||
Name string `gorm:"size:100;unique;not null;comment:原料名称"`
|
||||
Description string `gorm:"size:255;comment:描述"`
|
||||
Quantity float32 `gorm:"not null;comment:库存总量, 单位: g"`
|
||||
@@ -25,8 +23,8 @@ func (RawMaterial) TableName() string {
|
||||
|
||||
// RawMaterialPurchase 记录了原料的每一次采购。
|
||||
type RawMaterialPurchase struct {
|
||||
gorm.Model
|
||||
RawMaterialID uint `gorm:"not null;index;comment:关联的原料ID"`
|
||||
Model
|
||||
RawMaterialID uint32 `gorm:"not null;index;comment:关联的原料ID"`
|
||||
RawMaterial RawMaterial `gorm:"foreignKey:RawMaterialID"`
|
||||
Supplier string `gorm:"size:100;comment:供应商"`
|
||||
Amount float32 `gorm:"not null;comment:采购数量, 单位: g"`
|
||||
@@ -54,11 +52,11 @@ const (
|
||||
|
||||
// RawMaterialStockLog 记录了原料库存的所有变动,提供了完整的追溯链。
|
||||
type RawMaterialStockLog struct {
|
||||
gorm.Model
|
||||
RawMaterialID uint `gorm:"not null;index;comment:关联的原料ID"`
|
||||
Model
|
||||
RawMaterialID uint32 `gorm:"not null;index;comment:关联的原料ID"`
|
||||
ChangeAmount float32 `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)"`
|
||||
SourceID uint32 `gorm:"not null;index;comment:来源记录的ID (如 RawMaterialPurchase.ID 或 FeedUsageRecord.ID)"`
|
||||
HappenedAt time.Time `gorm:"primaryKey;comment:业务发生时间"`
|
||||
Remarks string `gorm:"comment:备注, 如主动领取的理由等"`
|
||||
}
|
||||
@@ -70,7 +68,7 @@ func (RawMaterialStockLog) TableName() string {
|
||||
// FeedFormula 代表饲料配方。
|
||||
// 对于没有配方的外购饲料,可以将其视为一种特殊的 RawMaterial, 并为其创建一个仅包含它自己的 FeedFormula。
|
||||
type FeedFormula struct {
|
||||
gorm.Model
|
||||
Model
|
||||
Name string `gorm:"size:100;unique;not null;comment:配方名称"`
|
||||
Description string `gorm:"size:255;comment:描述"`
|
||||
Components []FeedFormulaComponent `gorm:"foreignKey:FeedFormulaID"`
|
||||
@@ -82,9 +80,9 @@ func (FeedFormula) TableName() string {
|
||||
|
||||
// FeedFormulaComponent 代表配方中的一种原料及其占比。
|
||||
type FeedFormulaComponent struct {
|
||||
gorm.Model
|
||||
FeedFormulaID uint `gorm:"not null;index;comment:外键到 FeedFormula"`
|
||||
RawMaterialID uint `gorm:"not null;index;comment:外键到 RawMaterial"`
|
||||
Model
|
||||
FeedFormulaID uint32 `gorm:"not null;index;comment:外键到 FeedFormula"`
|
||||
RawMaterialID uint32 `gorm:"not null;index;comment:外键到 RawMaterial"`
|
||||
RawMaterial RawMaterial `gorm:"foreignKey:RawMaterialID"`
|
||||
Percentage float32 `gorm:"not null;comment:该原料在配方中的百分比 (0-1.0)"`
|
||||
}
|
||||
@@ -97,14 +95,14 @@ func (FeedFormulaComponent) TableName() string {
|
||||
// 应用层逻辑:当一条使用记录被创建时,应根据其使用的 FeedFormula,
|
||||
// 计算出每种 RawMaterial 的消耗量,并在 RawMaterialStockLog 中创建对应的出库记录。
|
||||
type FeedUsageRecord struct {
|
||||
gorm.Model
|
||||
PenID uint `gorm:"not null;index;comment:关联的猪栏ID"`
|
||||
Model
|
||||
PenID uint32 `gorm:"not null;index;comment:关联的猪栏ID"`
|
||||
Pen Pen `gorm:"foreignKey:PenID"`
|
||||
FeedFormulaID uint `gorm:"not null;index;comment:使用的饲料配方ID"`
|
||||
FeedFormulaID uint32 `gorm:"not null;index;comment:使用的饲料配方ID"`
|
||||
FeedFormula FeedFormula `gorm:"foreignKey:FeedFormulaID"`
|
||||
Amount float32 `gorm:"not null;comment:使用数量, 单位: g"`
|
||||
RecordedAt time.Time `gorm:"primaryKey;comment:记录时间"`
|
||||
OperatorID uint `gorm:"not null;comment:操作员"`
|
||||
OperatorID uint32 `gorm:"not null;comment:操作员"`
|
||||
Remarks string `gorm:"comment:备注, 如 '例行喂料, 弱猪补料' 等"`
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"time"
|
||||
|
||||
"gorm.io/datatypes"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
/*
|
||||
@@ -57,7 +56,7 @@ type PowderInstructions struct {
|
||||
|
||||
// Medication 定义了兽药/疫苗的基本信息模型
|
||||
type Medication struct {
|
||||
gorm.Model
|
||||
Model
|
||||
Name string `gorm:"size:100;not null;comment:药品名称" json:"name"`
|
||||
Type MedicationType `gorm:"size:20;not null;comment:兽药类型 (粉剂, 针剂, 疫苗)" json:"type"`
|
||||
Category MedicationCategory `gorm:"size:30;not null;comment:兽药种类 (四环素类, 磺胺类等)" json:"category"`
|
||||
@@ -82,15 +81,15 @@ const (
|
||||
|
||||
// MedicationLog 记录了对整个猪批次的用药情况
|
||||
type MedicationLog struct {
|
||||
gorm.Model
|
||||
PigBatchID uint `gorm:"not null;index;comment:关联的猪批次ID"`
|
||||
MedicationID uint `gorm:"not null;index;comment:关联的药品ID"`
|
||||
Model
|
||||
PigBatchID uint32 `gorm:"not null;index;comment:关联的猪批次ID"`
|
||||
MedicationID uint32 `gorm:"not null;index;comment:关联的药品ID"`
|
||||
Medication Medication `gorm:"foreignKey:MedicationID"` // 预加载药品信息
|
||||
DosageUsed float32 `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:具体描述,如'治疗呼吸道病'"`
|
||||
OperatorID uint `gorm:"comment:操作员ID"`
|
||||
OperatorID uint32 `gorm:"comment:操作员ID"`
|
||||
HappenedAt time.Time `gorm:"primaryKey;comment:用药时间"`
|
||||
}
|
||||
|
||||
|
||||
@@ -6,10 +6,20 @@ import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap/zapcore"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Model 用于代替gorm.Model, 使用uint32以节约空间
|
||||
type Model struct {
|
||||
ID uint32 `gorm:"primarykey"`
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
DeletedAt gorm.DeletedAt `gorm:"index"`
|
||||
}
|
||||
|
||||
// GetAllModels 返回一个包含所有数据库模型实例的切片。
|
||||
// 这个函数用于在数据库初始化时自动迁移所有的表结构。
|
||||
func GetAllModels() []interface{} {
|
||||
@@ -72,10 +82,10 @@ func GetAllModels() []interface{} {
|
||||
}
|
||||
}
|
||||
|
||||
// UintArray 是一个自定义类型,代表 uint 的切片。
|
||||
// UintArray 是一个自定义类型,代表 uint32 的切片。
|
||||
// 它实现了 gorm.Scanner 和 driver.Valuer 接口,
|
||||
// 以便能与数据库的 bigint[] 类型进行原生映射。
|
||||
type UintArray []uint
|
||||
type UintArray []uint32
|
||||
|
||||
// Value 实现了 driver.Valuer 接口。
|
||||
// 它告诉 GORM 如何将 UintArray ([]) 转换为数据库能够理解的格式。
|
||||
@@ -117,19 +127,19 @@ func (a *UintArray) Scan(src interface{}) error {
|
||||
// 去掉花括号
|
||||
srcStr = strings.Trim(srcStr, "{}")
|
||||
if srcStr == "" {
|
||||
*a = []uint{}
|
||||
*a = []uint32{}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 按逗号分割
|
||||
parts := strings.Split(srcStr, ",")
|
||||
arr := make([]uint, len(parts))
|
||||
arr := make([]uint32, len(parts))
|
||||
for i, p := range parts {
|
||||
val, err := strconv.ParseUint(p, 10, 64)
|
||||
if err != nil {
|
||||
return fmt.Errorf("解析 UintArray 元素失败: %w", err)
|
||||
}
|
||||
arr[i] = uint(val)
|
||||
arr[i] = uint32(val)
|
||||
}
|
||||
|
||||
*a = arr
|
||||
|
||||
@@ -2,8 +2,6 @@ package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// NotifierType 定义了通知器的类型。
|
||||
@@ -31,12 +29,12 @@ const (
|
||||
|
||||
// Notification 表示已发送或尝试发送的通知记录。
|
||||
type Notification struct {
|
||||
gorm.Model
|
||||
Model
|
||||
|
||||
// NotifierType 通知器类型 (例如:"邮件", "企业微信", "飞书", "日志")
|
||||
NotifierType NotifierType `gorm:"type:varchar(20);not null;index" json:"notifier_type"`
|
||||
// UserID 接收通知的用户ID,用于追溯通知记录到特定用户
|
||||
UserID uint `gorm:"index" json:"user_id"` // 增加 UserID 字段,并添加索引
|
||||
UserID uint32 `gorm:"index" json:"user_id"` // 增加 UserID 字段,并添加索引
|
||||
// Title 通知标题
|
||||
Title string `gorm:"type:varchar(255);not null" json:"title"`
|
||||
// Message 通知内容
|
||||
|
||||
@@ -2,8 +2,6 @@ package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
/*
|
||||
@@ -32,7 +30,7 @@ const (
|
||||
|
||||
// PigBatch 是猪批次的核心模型,代表了一群被共同管理的猪
|
||||
type PigBatch struct {
|
||||
gorm.Model
|
||||
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:批次开始日期 (如转入日或购买日)"`
|
||||
@@ -65,14 +63,14 @@ const (
|
||||
|
||||
// PigBatchLog 记录了猪批次数量或状态的每一次变更
|
||||
type PigBatchLog struct {
|
||||
gorm.Model
|
||||
PigBatchID uint `gorm:"not null;index;comment:关联的猪批次ID"`
|
||||
Model
|
||||
PigBatchID uint32 `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"`
|
||||
OperatorID uint32 `gorm:"comment:操作员ID"`
|
||||
HappenedAt time.Time `gorm:"primaryKey;comment:事件发生时间"`
|
||||
}
|
||||
|
||||
@@ -82,10 +80,10 @@ func (PigBatchLog) TableName() string {
|
||||
|
||||
// WeighingBatch 记录了一次批次称重的信息
|
||||
type WeighingBatch struct {
|
||||
gorm.Model
|
||||
Model
|
||||
WeighingTime time.Time `gorm:"primaryKey;comment:称重时间"`
|
||||
Description string `gorm:"size:255;comment:批次称重描述"`
|
||||
PigBatchID uint `gorm:"not null;index;comment:关联的猪批次ID"`
|
||||
PigBatchID uint32 `gorm:"not null;index;comment:关联的猪批次ID"`
|
||||
}
|
||||
|
||||
func (WeighingBatch) TableName() string {
|
||||
@@ -94,11 +92,11 @@ func (WeighingBatch) TableName() string {
|
||||
|
||||
// WeighingRecord 记录了单次称重信息
|
||||
type WeighingRecord struct {
|
||||
gorm.Model
|
||||
Model
|
||||
Weight float32 `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"`
|
||||
WeighingBatchID uint32 `gorm:"not null;index;comment:关联的批次称重ID"`
|
||||
PenID uint32 `gorm:"not null;index;comment:所在猪圈ID"`
|
||||
OperatorID uint32 `gorm:"not null;comment:操作员ID"`
|
||||
Remark string `gorm:"size:255;comment:备注"`
|
||||
WeighingTime time.Time `gorm:"primaryKey;comment:称重时间"`
|
||||
}
|
||||
|
||||
@@ -2,8 +2,6 @@ package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// PigBatchSickPigTreatmentLocation 定义了病猪治疗地点
|
||||
@@ -29,16 +27,16 @@ const (
|
||||
|
||||
// PigSickLog 记录了猪批次中病猪数量的变化日志
|
||||
type PigSickLog struct {
|
||||
gorm.Model
|
||||
PigBatchID uint `gorm:"primaryKey;comment:关联的猪批次ID"`
|
||||
PenID uint `gorm:"not null;index;comment:所在猪圈ID"`
|
||||
Model
|
||||
PigBatchID uint32 `gorm:"primaryKey;comment:关联的猪批次ID"`
|
||||
PenID uint32 `gorm:"not null;index;comment:所在猪圈ID"`
|
||||
ChangeCount int `gorm:"not null;comment:变化数量, 正数表示新增, 负数表示移除"`
|
||||
Reason PigBatchSickPigReasonType `gorm:"size:20;not null;comment:变化原因 (如: 患病, 康复, 死亡, 转入, 转出, 其他)"`
|
||||
BeforeCount int `gorm:"comment:变化前的数量"`
|
||||
AfterCount int `gorm:"comment:变化后的数量"`
|
||||
Remarks string `gorm:"size:255;comment:备注"`
|
||||
TreatmentLocation PigBatchSickPigTreatmentLocation `gorm:"size:50;comment:治疗地点"`
|
||||
OperatorID uint `gorm:"comment:操作员ID"`
|
||||
OperatorID uint32 `gorm:"comment:操作员ID"`
|
||||
HappenedAt time.Time `gorm:"primaryKey;comment:事件发生时间"`
|
||||
}
|
||||
|
||||
|
||||
@@ -2,21 +2,19 @@ package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// PigPurchase 记录了猪只采购信息
|
||||
type PigPurchase struct {
|
||||
gorm.Model
|
||||
PigBatchID uint `gorm:"not null;index;comment:关联的猪批次ID"`
|
||||
Model
|
||||
PigBatchID uint32 `gorm:"not null;index;comment:关联的猪批次ID"`
|
||||
PurchaseDate time.Time `gorm:"primaryKey;comment:采购日期"`
|
||||
Supplier string `gorm:"comment:供应商"`
|
||||
Quantity int `gorm:"not null;comment:采购数量"`
|
||||
UnitPrice float32 `gorm:"not null;comment:单价"`
|
||||
TotalPrice float32 `gorm:"not null;comment:总价"`
|
||||
Remarks string `gorm:"size:255;comment:备注"`
|
||||
OperatorID uint `gorm:"comment:操作员ID"`
|
||||
OperatorID uint32 `gorm:"comment:操作员ID"`
|
||||
}
|
||||
|
||||
func (PigPurchase) TableName() string {
|
||||
@@ -25,15 +23,15 @@ func (PigPurchase) TableName() string {
|
||||
|
||||
// PigSale 记录了猪只销售信息
|
||||
type PigSale struct {
|
||||
gorm.Model
|
||||
PigBatchID uint `gorm:"not null;index;comment:关联的猪批次ID"`
|
||||
Model
|
||||
PigBatchID uint32 `gorm:"not null;index;comment:关联的猪批次ID"`
|
||||
SaleDate time.Time `gorm:"primaryKey;comment:销售日期"`
|
||||
Buyer string `gorm:"comment:购买方"`
|
||||
Quantity int `gorm:"not null;comment:销售数量"`
|
||||
UnitPrice float32 `gorm:"not null;comment:单价"`
|
||||
TotalPrice float32 `gorm:"not null;comment:总价"`
|
||||
Remarks string `gorm:"size:255;comment:备注"`
|
||||
OperatorID uint `gorm:"comment:操作员ID"`
|
||||
OperatorID uint32 `gorm:"comment:操作员ID"`
|
||||
}
|
||||
|
||||
func (PigSale) TableName() string {
|
||||
|
||||
@@ -2,8 +2,6 @@ package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// PigTransferType 定义了猪只迁移的类型
|
||||
@@ -23,14 +21,14 @@ const (
|
||||
// PigTransferLog 记录了每一次猪只数量在猪栏间的变动事件。
|
||||
// 它作为事件溯源的基础,用于推算任意时间点猪栏的猪只数量。
|
||||
type PigTransferLog struct {
|
||||
gorm.Model
|
||||
Model
|
||||
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,作为联合主键
|
||||
PigBatchID uint32 `gorm:"primaryKey;comment:关联的猪群ID" json:"pig_batch_id"` // 关联的猪群ID,作为联合主键
|
||||
PenID uint32 `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
|
||||
OperatorID uint32 `gorm:"not null;comment:操作员ID" json:"operator_id"` // 操作员ID
|
||||
Remarks string `gorm:"comment:备注" json:"remarks"`
|
||||
}
|
||||
|
||||
|
||||
@@ -74,15 +74,15 @@ const (
|
||||
|
||||
// Plan 代表系统中的一个计划,可以包含子计划或任务
|
||||
type Plan struct {
|
||||
gorm.Model
|
||||
Model
|
||||
|
||||
Name PlanName `gorm:"not null" json:"name"`
|
||||
Description string `json:"description"`
|
||||
PlanType PlanType `gorm:"not null;index" json:"plan_type"` // 任务类型, 包括系统任务和用户自定义任务
|
||||
ExecutionType PlanExecutionType `gorm:"not null;index" json:"execution_type"`
|
||||
Status PlanStatus `gorm:"default:'已禁用';index" json:"status"` // 计划是否被启动
|
||||
ExecuteNum uint `gorm:"default:0" json:"execute_num"` // 计划预期执行次数
|
||||
ExecuteCount uint `gorm:"default:0" json:"execute_count"` // 执行计数器
|
||||
ExecuteNum uint32 `gorm:"default:0" json:"execute_num"` // 计划预期执行次数
|
||||
ExecuteCount uint32 `gorm:"default:0" json:"execute_count"` // 执行计数器
|
||||
|
||||
// 针对 PlanExecutionTypeAutomatic,使用 Cron 表达式定义调度规则
|
||||
CronExpression string `json:"cron_expression"`
|
||||
@@ -163,12 +163,12 @@ func (p *Plan) ReorderSteps() {
|
||||
|
||||
// SubPlan 代表作为另一个计划一部分的子计划,具有执行顺序
|
||||
type SubPlan struct {
|
||||
gorm.Model
|
||||
Model
|
||||
|
||||
ParentPlanID uint `gorm:"not null;index" json:"parent_plan_id"` // 父计划的ID
|
||||
ChildPlanID uint `gorm:"not null;index" json:"child_plan_id"` // 子计划的ID (它本身也是一个 Plan)
|
||||
ExecutionOrder int `gorm:"not null" json:"execution_order"` // 在父计划中的执行顺序
|
||||
ChildPlan *Plan `gorm:"-" json:"child_plan"` // 完整子计划数据,仅内存中
|
||||
ParentPlanID uint32 `gorm:"not null;index" json:"parent_plan_id"` // 父计划的ID
|
||||
ChildPlanID uint32 `gorm:"not null;index" json:"child_plan_id"` // 子计划的ID (它本身也是一个 Plan)
|
||||
ExecutionOrder int `gorm:"not null" json:"execution_order"` // 在父计划中的执行顺序
|
||||
ChildPlan *Plan `gorm:"-" json:"child_plan"` // 完整子计划数据,仅内存中
|
||||
}
|
||||
|
||||
// TableName 自定义 GORM 使用的数据库表名
|
||||
@@ -184,7 +184,7 @@ type Task struct {
|
||||
UpdatedAt time.Time
|
||||
DeletedAt gorm.DeletedAt `gorm:"index"` // 保持软删除功能
|
||||
|
||||
PlanID uint `gorm:"not null;index" json:"plan_id"` // 此任务所属计划的ID
|
||||
PlanID uint32 `gorm:"not null;index" json:"plan_id"` // 此任务所属计划的ID
|
||||
Name string `gorm:"not null" json:"name"`
|
||||
Description string `json:"description"`
|
||||
ExecutionOrder int `gorm:"not null" json:"execution_order"` // 在计划中的执行顺序
|
||||
@@ -229,9 +229,9 @@ func (t *Task) SaveParameters(v interface{}) error {
|
||||
|
||||
// DeviceTask 是设备和任务之间的关联模型,表示一个设备可以执行多个任务,一个任务可以被多个设备执行。
|
||||
type DeviceTask struct {
|
||||
gorm.Model
|
||||
DeviceID uint `gorm:"not null;index"` // 设备ID
|
||||
TaskID uint `gorm:"not null;index"` // 任务ID
|
||||
Model
|
||||
DeviceID uint32 `gorm:"not null;index"` // 设备ID
|
||||
TaskID uint32 `gorm:"not null;index"` // 任务ID
|
||||
|
||||
// 可选:如果需要存储关联的额外信息,可以在这里添加字段,例如:
|
||||
// Configuration datatypes.JSON `json:"configuration"` // 任务在特定设备上的配置
|
||||
|
||||
@@ -7,8 +7,8 @@ import (
|
||||
// PendingTask 是一个待执行任务队列, 里面会储存待执行的Task以及这个Task什么时候执行
|
||||
// 它是一个纯粹的工作队列,任务被认领后即被删除。
|
||||
type PendingTask struct {
|
||||
// 手动填充必须字段以实现硬删除,不内嵌 gorm.Model
|
||||
ID uint `gorm:"primarykey"`
|
||||
// 手动填充必须字段以实现硬删除,不内嵌 Model
|
||||
ID uint32 `gorm:"primarykey"`
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
|
||||
@@ -21,7 +21,7 @@ type PendingTask struct {
|
||||
Task *Task `gorm:"foreignKey:TaskID"`
|
||||
|
||||
ExecuteAt time.Time `gorm:"index"` // 任务执行时间
|
||||
TaskExecutionLogID uint `gorm:"unique;not null;index"` // 对应的执行历史记录ID
|
||||
TaskExecutionLogID uint32 `gorm:"unique;not null;index"` // 对应的执行历史记录ID
|
||||
|
||||
// 通过 TaskExecutionLogID 关联到唯一的 TaskExecutionLog 记录
|
||||
// ON DELETE CASCADE 确保如果日志被删除,这个待办任务也会被自动清理
|
||||
|
||||
@@ -55,10 +55,10 @@ type SensorData struct {
|
||||
Time time.Time `gorm:"primaryKey" json:"time"`
|
||||
|
||||
// DeviceID 是传感器的唯一标识符,作为复合主键的另一部分。
|
||||
DeviceID uint `gorm:"primaryKey" json:"device_id"`
|
||||
DeviceID uint32 `gorm:"primaryKey" json:"device_id"`
|
||||
|
||||
// AreaControllerID 是上报此数据的区域主控的ID。
|
||||
AreaControllerID uint `json:"area_controller_id"`
|
||||
AreaControllerID uint32 `json:"area_controller_id"`
|
||||
|
||||
// SensorType 是传感数据的类型
|
||||
SensorType SensorType `gorm:"not null;index" json:"sensor_type"`
|
||||
|
||||
@@ -38,9 +38,9 @@ func (ci ContactInfo) Value() (driver.Value, error) {
|
||||
|
||||
// User 代表系统中的用户模型
|
||||
type User struct {
|
||||
// gorm.Model 内嵌了 ID, CreatedAt, UpdatedAt, 和 DeletedAt
|
||||
// Model 内嵌了 ID, CreatedAt, UpdatedAt, 和 DeletedAt
|
||||
// DeletedAt 字段的存在自动为 GORM 开启了软删除模式
|
||||
gorm.Model
|
||||
Model
|
||||
|
||||
// Username 是用户的登录名,应该是唯一的
|
||||
// 修正了 gorm 标签的拼写错误 (移除了 gorm 后面的冒号)
|
||||
|
||||
Reference in New Issue
Block a user