WiiCITMS/servers/notification/notification_utils.go
2025-11-07 14:14:34 +08:00

127 lines
4.0 KiB
Go

package notification
import (
"encoding/json"
"time"
"github.com/google/uuid"
)
// NotificationPayload 通知数据载荷
type NotificationPayload struct {
Title string `json:"title"` // 通知标题
Content string `json:"content"` // 通知内容
Data interface{} `json:"data"` // 附加数据
URL string `json:"url"` // 相关链接
}
// PositionChangePayload 职位变动通知
type PositionChangePayload struct {
NotificationPayload
OldPosition string `json:"oldPosition"` // 旧职位
NewPosition string `json:"newPosition"` // 新职位
OldOrganization string `json:"oldOrganization"` // 旧组织
NewOrganization string `json:"newOrganization"` // 新组织
ChangeType string `json:"changeType"` // 变动类型:新增、移除、更新
EffectiveDate string `json:"effectiveDate"` // 生效日期
}
// ApprovalNoticePayload 审批通知
type ApprovalNoticePayload struct {
NotificationPayload
ApprovalID string `json:"approvalId"` // 审批ID
ApprovalType string `json:"approvalType"` // 审批类型
ApprovalState string `json:"approvalState"` // 审批状态:待审批、已通过、已拒绝
Approver string `json:"approver"` // 审批人
ApprovalTime string `json:"approvalTime"` // 审批时间
}
// MessagePayload 消息提醒
type MessagePayload struct {
NotificationPayload
Sender string `json:"sender"` // 发送者
Category string `json:"category"` // 消息分类
Priority string `json:"priority"` // 优先级:低、中、高
ReadState bool `json:"readState"` // 阅读状态
}
// SystemNotificationPayload 系统通知
type SystemNotificationPayload struct {
NotificationPayload
Category string `json:"category"` // 通知分类
Severity string `json:"severity"` // 严重程度:信息、警告、错误
}
// ScheduleReminderPayload 日程提醒通知
type ScheduleReminderPayload struct {
NotificationPayload
ScheduleGuid string `json:"scheduleGuid"`
StartTime string `json:"startTime"`
EndTime string `json:"endTime"`
}
// NotifyPositionChange 发送职位变动通知
func NotifyPositionChange(staffGuids []string, payload PositionChangePayload) {
sseServer := GetSSEServer()
sseServer.SendEventToStaff(staffGuids, PositionChangeEvent, payload)
}
// NotifyApproval 发送审批通知
func NotifyApproval(staffGuids []string, payload ApprovalNoticePayload) {
sseServer := GetSSEServer()
sseServer.SendEventToStaff(staffGuids, ApprovalNoticeEvent, payload)
}
// NotifyMessage 发送消息提醒
func NotifyMessage(staffGuids []string, payload MessagePayload) {
sseServer := GetSSEServer()
sseServer.SendEventToStaff(staffGuids, MessageNotificationEvent, payload)
}
// NotifySystem 发送系统通知
func NotifySystem(staffGuids []string, payload SystemNotificationPayload) {
sseServer := GetSSEServer()
sseServer.SendEventToStaff(staffGuids, SystemNotificationEvent, payload)
}
// NotifyScheduleReminder 发送日程提醒
func NotifyScheduleReminder(staffGuids []string, payload ScheduleReminderPayload) {
sseServer := GetSSEServer()
sseServer.SendEventToStaff(staffGuids, ScheduleReminderEvent, payload)
}
// BroadcastSystemNotification 广播系统通知给所有在线用户
func BroadcastSystemNotification(payload SystemNotificationPayload) {
sseServer := GetSSEServer()
sseServer.BroadcastEvent(SystemNotificationEvent, payload)
}
// CreateGenericNotification 创建通用通知
func CreateGenericNotification(title, content string) NotificationPayload {
return NotificationPayload{
Title: title,
Content: content,
Data: nil,
URL: "",
}
}
// SerializeEvent 序列化事件为JSON
func SerializeEvent(event Event) (string, error) {
bytes, err := json.Marshal(event)
if err != nil {
return "", err
}
return string(bytes), nil
}
// CreateEvent 创建一个新事件
func CreateEvent(eventType EventType, data interface{}) Event {
return Event{
ID: uuid.New().String(),
Type: eventType,
Data: data,
Timestamp: time.Now().Unix(),
}
}