54 lines
1.7 KiB
Go
54 lines
1.7 KiB
Go
|
|
package model
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"time"
|
|||
|
|
|
|||
|
|
"github.com/google/uuid"
|
|||
|
|
"gorm.io/gorm"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// NotificationType 通知类型
|
|||
|
|
type NotificationType string
|
|||
|
|
|
|||
|
|
const (
|
|||
|
|
NotificationTypeLikePost NotificationType = "like_post"
|
|||
|
|
NotificationTypeLikeComment NotificationType = "like_comment"
|
|||
|
|
NotificationTypeComment NotificationType = "comment"
|
|||
|
|
NotificationTypeReply NotificationType = "reply"
|
|||
|
|
NotificationTypeFollow NotificationType = "follow"
|
|||
|
|
NotificationTypeMention NotificationType = "mention"
|
|||
|
|
NotificationTypeSystem NotificationType = "system"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// Notification 通知实体
|
|||
|
|
type Notification struct {
|
|||
|
|
ID string `json:"id" gorm:"type:varchar(36);primaryKey"`
|
|||
|
|
UserID string `json:"user_id" gorm:"type:varchar(36);not null;index:idx_notifications_user_read_created,priority:1"` // 接收者
|
|||
|
|
Type NotificationType `json:"type" gorm:"type:varchar(30);not null"`
|
|||
|
|
Title string `json:"title" gorm:"type:varchar(200);not null"`
|
|||
|
|
Content string `json:"content" gorm:"type:text"`
|
|||
|
|
Data string `json:"data" gorm:"type:jsonb"` // 相关数据(JSON)
|
|||
|
|
|
|||
|
|
// 已读状态
|
|||
|
|
IsRead bool `json:"is_read" gorm:"default:false;index:idx_notifications_user_read_created,priority:2"`
|
|||
|
|
ReadAt *time.Time `json:"read_at" gorm:"type:timestamp"`
|
|||
|
|
|
|||
|
|
// 软删除
|
|||
|
|
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
|
|||
|
|
|
|||
|
|
// 时间戳
|
|||
|
|
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime;index:idx_notifications_user_read_created,priority:3,sort:desc"`
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// BeforeCreate 创建前生成UUID
|
|||
|
|
func (n *Notification) BeforeCreate(tx *gorm.DB) error {
|
|||
|
|
if n.ID == "" {
|
|||
|
|
n.ID = uuid.New().String()
|
|||
|
|
}
|
|||
|
|
return nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (Notification) TableName() string {
|
|||
|
|
return "notifications"
|
|||
|
|
}
|