From 86c9073da87a571f4d271d8e98046f09186bc1bc Mon Sep 17 00:00:00 2001 From: huang <1724659546@qq.com> Date: Sat, 25 Oct 2025 15:41:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AEbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/app/dto/notification_converter.go | 3 +- internal/domain/notify/notify.go | 2 +- internal/infra/models/notify.go | 32 +++++++++++++++++++++- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/internal/app/dto/notification_converter.go b/internal/app/dto/notification_converter.go index dddc5d2..5df25a8 100644 --- a/internal/app/dto/notification_converter.go +++ b/internal/app/dto/notification_converter.go @@ -2,6 +2,7 @@ package dto import ( "git.huangwc.com/pig/pig-farm-controller/internal/infra/models" + "go.uber.org/zap/zapcore" ) // NewListNotificationResponse 从模型数据创建通知列表响应 DTO @@ -16,7 +17,7 @@ func NewListNotificationResponse(data []models.Notification, total int64, page, UserID: item.UserID, Title: item.Title, Message: item.Message, - Level: item.Level, + Level: zapcore.Level(item.Level), AlarmTimestamp: item.AlarmTimestamp, ToAddress: item.ToAddress, Status: item.Status, diff --git a/internal/domain/notify/notify.go b/internal/domain/notify/notify.go index df13c1d..2cfde52 100644 --- a/internal/domain/notify/notify.go +++ b/internal/domain/notify/notify.go @@ -273,7 +273,7 @@ func (s *failoverService) recordNotificationAttempt( UserID: userID, Title: content.Title, Message: content.Message, - Level: content.Level, + Level: models.LogLevel(content.Level), AlarmTimestamp: content.Timestamp, ToAddress: toAddress, Status: status, diff --git a/internal/infra/models/notify.go b/internal/infra/models/notify.go index a286a12..291ee9b 100644 --- a/internal/infra/models/notify.go +++ b/internal/infra/models/notify.go @@ -1,6 +1,8 @@ package models import ( + "database/sql/driver" + "errors" "time" "git.huangwc.com/pig/pig-farm-controller/internal/infra/notify" @@ -17,6 +19,34 @@ const ( 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 表示已发送或尝试发送的通知记录。 type Notification struct { gorm.Model @@ -30,7 +60,7 @@ type Notification struct { // 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"` + Level LogLevel `gorm:"type:varchar(10);not null" json:"level"` // AlarmTimestamp 通知内容生成时的时间戳,与 ID 构成复合主键 AlarmTimestamp time.Time `gorm:"primaryKey;not null" json:"alarm_timestamp"` // ToAddress 接收地址 (例如:邮箱地址, 企业微信ID, 日志标识符)