This commit is contained in:
2025-10-25 15:41:49 +08:00
parent 43c1839345
commit 86c9073da8
3 changed files with 34 additions and 3 deletions

View File

@@ -2,6 +2,7 @@ package dto
import ( import (
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models" "git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
"go.uber.org/zap/zapcore"
) )
// NewListNotificationResponse 从模型数据创建通知列表响应 DTO // NewListNotificationResponse 从模型数据创建通知列表响应 DTO
@@ -16,7 +17,7 @@ func NewListNotificationResponse(data []models.Notification, total int64, page,
UserID: item.UserID, UserID: item.UserID,
Title: item.Title, Title: item.Title,
Message: item.Message, Message: item.Message,
Level: item.Level, Level: zapcore.Level(item.Level),
AlarmTimestamp: item.AlarmTimestamp, AlarmTimestamp: item.AlarmTimestamp,
ToAddress: item.ToAddress, ToAddress: item.ToAddress,
Status: item.Status, Status: item.Status,

View File

@@ -273,7 +273,7 @@ func (s *failoverService) recordNotificationAttempt(
UserID: userID, UserID: userID,
Title: content.Title, Title: content.Title,
Message: content.Message, Message: content.Message,
Level: content.Level, Level: models.LogLevel(content.Level),
AlarmTimestamp: content.Timestamp, AlarmTimestamp: content.Timestamp,
ToAddress: toAddress, ToAddress: toAddress,
Status: status, Status: status,

View File

@@ -1,6 +1,8 @@
package models package models
import ( import (
"database/sql/driver"
"errors"
"time" "time"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/notify" "git.huangwc.com/pig/pig-farm-controller/internal/infra/notify"
@@ -17,6 +19,34 @@ const (
NotificationStatusSkipped NotificationStatus = "已跳过" // 通知因某些原因被跳过(例如:用户未配置联系方式) NotificationStatusSkipped NotificationStatus = "已跳过" // 通知因某些原因被跳过(例如:用户未配置联系方式)
) )
// LogLevel is a custom type for zapcore.Level to handle database scanning and valuing.
type LogLevel zapcore.Level
// Scan implements the sql.Scanner interface.
func (l *LogLevel) Scan(value interface{}) error {
var s string
switch v := value.(type) {
case []byte:
s = string(v)
case string:
s = v
default:
return errors.New("LogLevel的类型无效")
}
var zl zapcore.Level
if err := zl.UnmarshalText([]byte(s)); err != nil {
return err
}
*l = LogLevel(zl)
return nil
}
// Value implements the driver.Valuer interface.
func (l LogLevel) Value() (driver.Value, error) {
return (zapcore.Level)(l).String(), nil
}
// Notification 表示已发送或尝试发送的通知记录。 // Notification 表示已发送或尝试发送的通知记录。
type Notification struct { type Notification struct {
gorm.Model gorm.Model
@@ -30,7 +60,7 @@ type Notification struct {
// Message 通知内容 // Message 通知内容
Message string `gorm:"type:text;not null" json:"message"` Message string `gorm:"type:text;not null" json:"message"`
// Level 通知级别 (例如INFO, WARN, ERROR) // Level 通知级别 (例如INFO, WARN, ERROR)
Level zapcore.Level `gorm:"type:varchar(10);not null" json:"level"` Level LogLevel `gorm:"type:varchar(10);not null" json:"level"`
// AlarmTimestamp 通知内容生成时的时间戳,与 ID 构成复合主键 // AlarmTimestamp 通知内容生成时的时间戳,与 ID 构成复合主键
AlarmTimestamp time.Time `gorm:"primaryKey;not null" json:"alarm_timestamp"` AlarmTimestamp time.Time `gorm:"primaryKey;not null" json:"alarm_timestamp"`
// ToAddress 接收地址 (例如:邮箱地址, 企业微信ID, 日志标识符) // ToAddress 接收地址 (例如:邮箱地址, 企业微信ID, 日志标识符)