diff --git a/internal/infra/models/alarm.go b/internal/infra/models/alarm.go new file mode 100644 index 0000000..2d7256f --- /dev/null +++ b/internal/infra/models/alarm.go @@ -0,0 +1,52 @@ +package models + +import ( + "time" + + "gorm.io/gorm" +) + +// ActiveAlarm 活跃告警 +// 活跃告警会被更新(如确认状态),因此保留 gorm.Model 以包含所有标准字段。 +type ActiveAlarm struct { + gorm.Model + FarmID uint `gorm:"not null;comment:猪场ID" json:"farm_id,omitempty"` + DeviceID uint `gorm:"not null;comment:设备ID" json:"device_id,omitempty"` + AlarmType string `gorm:"comment:告警类型" json:"alarm_type,omitempty"` + AlarmLevel string `gorm:"comment:告警级别" json:"alarm_level,omitempty"` + AlarmContent string `gorm:"comment:告警内容描述" json:"alarm_content,omitempty"` + TriggerTime time.Time `gorm:"not null;comment:告警触发时间" json:"trigger_time"` + IsAcknowledged bool `gorm:"default:false;comment:是否已确认" json:"is_acknowledged,omitempty"` + AcknowledgedBy string `gorm:"comment:确认人" json:"acknowledged_by,omitempty"` + // 使用指针类型 *time.Time 来表示可为空的确认时间 + AcknowledgedTime *time.Time `gorm:"comment:确认时间" json:"acknowledged_time,omitempty"` +} + +// TableName 指定 ActiveAlarm 结构体对应的数据库表名 +func (ActiveAlarm) TableName() string { + return "active_alarms" +} + +// HistoricalAlarm 历史告警 +// 历史告警是不可变归档数据,我们移除 gorm.Model,并手动定义字段。 +// ID 和 CreatedAt 共同构成联合主键,以满足 TimescaleDB 超表的要求。 +type HistoricalAlarm struct { + // 手动定义主键,ID 仍然自增 + ID uint `gorm:"primaryKey;autoIncrement;comment:主键ID" json:"id,omitempty"` + FarmID uint `gorm:"not null;comment:猪场ID" json:"farm_id,omitempty"` + DeviceID uint `gorm:"not null;comment:设备ID" json:"device_id,omitempty"` + AlarmType string `gorm:"comment:告警类型" json:"alarm_type,omitempty"` + AlarmLevel string `gorm:"comment:告警级别" json:"alarm_level,omitempty"` + AlarmContent string `gorm:"comment:告警内容描述" json:"alarm_content,omitempty"` + TriggerTime time.Time `gorm:"not null;comment:告警触发时间" json:"trigger_time"` + ResolveTime time.Time `gorm:"not null;comment:告警解决时间" json:"resolve_time"` + ResolveMethod string `gorm:"comment:告警解决方式" json:"resolve_method,omitempty"` + ResolvedBy string `gorm:"comment:告警解决人" json:"resolved_by,omitempty"` + // 将 CreatedAt 作为联合主键的一部分,用于 TimescaleDB 分区 + CreatedAt time.Time `gorm:"primaryKey;comment:创建时间" json:"created_at"` +} + +// TableName 指定 HistoricalAlarm 结构体对应的数据库表名 +func (HistoricalAlarm) TableName() string { + return "historical_alarms" +}