From d8de5a68ebd695f9364f16384829baf0742efa5f Mon Sep 17 00:00:00 2001 From: huang <1724659546@qq.com> Date: Sat, 25 Oct 2025 13:28:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9A=E4=B9=89=E9=80=9A=E7=9F=A5model?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/infra/database/postgres.go | 2 ++ internal/infra/models/models.go | 3 +++ internal/infra/models/notify.go | 38 +++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 internal/infra/models/notify.go diff --git a/internal/infra/database/postgres.go b/internal/infra/database/postgres.go index 4069a5d..fb58a30 100644 --- a/internal/infra/database/postgres.go +++ b/internal/infra/database/postgres.go @@ -171,6 +171,7 @@ func (ps *PostgresStorage) creatingHyperTable() error { {models.PigSickLog{}, "happened_at"}, {models.PigPurchase{}, "purchase_date"}, {models.PigSale{}, "sale_date"}, + {models.Notification{}, "alarm_timestamp"}, } for _, table := range tablesToConvert { @@ -211,6 +212,7 @@ func (ps *PostgresStorage) applyCompressionPolicies() error { {models.PigSickLog{}, "pig_batch_id"}, {models.PigPurchase{}, "pig_batch_id"}, {models.PigSale{}, "pig_batch_id"}, + {models.Notification{}, "user_id"}, } for _, policy := range policies { diff --git a/internal/infra/models/models.go b/internal/infra/models/models.go index 7344a8a..43b0edb 100644 --- a/internal/infra/models/models.go +++ b/internal/infra/models/models.go @@ -59,6 +59,9 @@ func GetAllModels() []interface{} { // Medication Models &Medication{}, &MedicationLog{}, + + // Notification Models + &Notification{}, } } diff --git a/internal/infra/models/notify.go b/internal/infra/models/notify.go new file mode 100644 index 0000000..a26db9f --- /dev/null +++ b/internal/infra/models/notify.go @@ -0,0 +1,38 @@ +package models + +import ( + "time" + + "git.huangwc.com/pig/pig-farm-controller/internal/infra/notify" + "go.uber.org/zap/zapcore" + "gorm.io/gorm" +) + +// Notification 表示已发送或尝试发送的通知记录。 +type Notification struct { + gorm.Model + + // NotifierType 通知器类型 (例如:"邮件", "企业微信", "飞书", "日志") + NotifierType notify.NotifierType `gorm:"type:varchar(20);not null;index" json:"notifier_type"` + // UserID 接收通知的用户ID,用于追溯通知记录到特定用户 + UserID uint `gorm:"index" json:"user_id"` // 增加 UserID 字段,并添加索引 + // Title 通知标题 + Title string `gorm:"type:varchar(255);not null" json:"title"` + // Message 通知内容 + Message string `gorm:"type:text;not null" json:"message"` + // Level 通知级别 (例如:INFO, WARN, ERROR) + Level zapcore.Level `gorm:"type:varchar(10);not null" json:"level"` + // AlarmTimestamp 通知内容生成时的时间戳,与 ID 构成复合主键 + AlarmTimestamp time.Time `gorm:"primaryKey;not null" json:"alarm_timestamp"` + // ToAddress 接收地址 (例如:邮箱地址, 企业微信ID, 日志标识符) + ToAddress string `gorm:"type:varchar(255);not null" json:"to_address"` + // Status 通知发送尝试的状态 (例如:"success", "failed", "pending") + Status string `gorm:"type:varchar(20);not null;default:'pending'" json:"status"` + // ErrorMessage 如果通知发送失败,此字段存储错误信息 + ErrorMessage string `gorm:"type:text" json:"error_message"` +} + +// TableName 指定 Notification 模型的表名。 +func (Notification) TableName() string { + return "notifications" +}