78 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			78 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package models
 | 
						||
 | 
						||
import (
 | 
						||
	"database/sql/driver"
 | 
						||
	"errors"
 | 
						||
	"time"
 | 
						||
 | 
						||
	"git.huangwc.com/pig/pig-farm-controller/internal/infra/notify"
 | 
						||
	"go.uber.org/zap/zapcore"
 | 
						||
	"gorm.io/gorm"
 | 
						||
)
 | 
						||
 | 
						||
// NotificationStatus 定义了通知发送尝试的状态枚举。
 | 
						||
type NotificationStatus string
 | 
						||
 | 
						||
const (
 | 
						||
	NotificationStatusSuccess NotificationStatus = "发送成功" // 通知已成功发送
 | 
						||
	NotificationStatusFailed  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 表示已发送或尝试发送的通知记录。
 | 
						||
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 LogLevel `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 通知发送尝试的状态 (例如:"待发送", "发送成功", "发送失败", "已跳过")
 | 
						||
	Status NotificationStatus `gorm:"type:varchar(20);not null;default:'待发送'" json:"status"`
 | 
						||
	// ErrorMessage 如果通知发送失败,此字段存储错误信息
 | 
						||
	ErrorMessage string `gorm:"type:text" json:"error_message"`
 | 
						||
}
 | 
						||
 | 
						||
// TableName 指定 Notification 模型的表名。
 | 
						||
func (Notification) TableName() string {
 | 
						||
	return "notifications"
 | 
						||
}
 |