From 375af57afe2850c13f4e6381da108f0dc18fd025 Mon Sep 17 00:00:00 2001 From: huang <1724659546@qq.com> Date: Fri, 7 Nov 2025 20:56:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9A=E4=B9=89=E5=91=8A=E8=AD=A6=E8=A1=A8?= =?UTF-8?q?=E5=92=8C=E5=91=8A=E8=AD=A6=E5=8E=86=E5=8F=B2=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/infra/models/alarm.go | 52 ++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 internal/infra/models/alarm.go 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" +}