333 lines
10 KiB
Go
333 lines
10 KiB
Go
package notification
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/mark3labs/mcp-go/mcp"
|
|
"github.com/mark3labs/mcp-go/server"
|
|
)
|
|
|
|
// NotificationTools MCP通知工具集
|
|
var NotificationTools = []server.ServerTool{
|
|
// 发送职位变动通知
|
|
{
|
|
Tool: mcp.NewTool(
|
|
"notifyPositionChange",
|
|
mcp.WithDescription("发送职位变动通知"),
|
|
mcp.WithArray("staffGuids", mcp.Required(), mcp.Description("员工GUID列表")),
|
|
mcp.WithString("title", mcp.Required(), mcp.Description("通知标题")),
|
|
mcp.WithString("content", mcp.Required(), mcp.Description("通知内容")),
|
|
mcp.WithString("oldPosition", mcp.Description("旧职位")),
|
|
mcp.WithString("newPosition", mcp.Description("新职位")),
|
|
mcp.WithString("oldOrganization", mcp.Description("旧组织")),
|
|
mcp.WithString("newOrganization", mcp.Description("新组织")),
|
|
mcp.WithString("changeType", mcp.Description("变动类型:新增、移除、更新")),
|
|
mcp.WithString("effectiveDate", mcp.Description("生效日期")),
|
|
mcp.WithString("url", mcp.Description("相关链接")),
|
|
),
|
|
Handler: func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
// 验证必填参数
|
|
staffGuidsRaw := make([]string, 0)
|
|
|
|
// 使用单个GUID参数
|
|
staffGuid := request.GetString("staffGuid", "")
|
|
if staffGuid != "" {
|
|
staffGuidsRaw = append(staffGuidsRaw, staffGuid)
|
|
}
|
|
|
|
if len(staffGuidsRaw) == 0 {
|
|
return nil, fmt.Errorf("员工GUID列表不能为空")
|
|
}
|
|
|
|
title := request.GetString("title", "")
|
|
if title == "" {
|
|
return nil, fmt.Errorf("通知标题不能为空")
|
|
}
|
|
|
|
content := request.GetString("content", "")
|
|
if content == "" {
|
|
return nil, fmt.Errorf("通知内容不能为空")
|
|
}
|
|
|
|
// 直接使用已有的staffGuidsRaw
|
|
staffGuids := staffGuidsRaw
|
|
|
|
// 创建职位变动通知
|
|
payload := PositionChangePayload{
|
|
NotificationPayload: NotificationPayload{
|
|
Title: title,
|
|
Content: content,
|
|
URL: request.GetString("url", ""),
|
|
},
|
|
OldPosition: request.GetString("oldPosition", ""),
|
|
NewPosition: request.GetString("newPosition", ""),
|
|
OldOrganization: request.GetString("oldOrganization", ""),
|
|
NewOrganization: request.GetString("newOrganization", ""),
|
|
ChangeType: request.GetString("changeType", ""),
|
|
EffectiveDate: request.GetString("effectiveDate", time.Now().Format("2006-01-02")),
|
|
}
|
|
|
|
// 发送通知
|
|
NotifyPositionChange(staffGuids, payload)
|
|
|
|
// 返回结果
|
|
result := map[string]interface{}{
|
|
"success": true,
|
|
"message": fmt.Sprintf("已向%d位员工发送职位变动通知", len(staffGuids)),
|
|
"eventId": uuid.New().String(),
|
|
}
|
|
|
|
resultJSON, err := json.Marshal(result)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return mcp.NewToolResultText(string(resultJSON)), nil
|
|
},
|
|
},
|
|
// 发送审批通知
|
|
{
|
|
Tool: mcp.NewTool(
|
|
"notifyApproval",
|
|
mcp.WithDescription("发送审批通知"),
|
|
mcp.WithArray("staffGuids", mcp.Required(), mcp.Description("员工GUID列表")),
|
|
mcp.WithString("title", mcp.Required(), mcp.Description("通知标题")),
|
|
mcp.WithString("content", mcp.Required(), mcp.Description("通知内容")),
|
|
mcp.WithString("approvalId", mcp.Required(), mcp.Description("审批ID")),
|
|
mcp.WithString("approvalType", mcp.Description("审批类型")),
|
|
mcp.WithString("approvalState", mcp.Description("审批状态:待审批、已通过、已拒绝")),
|
|
mcp.WithString("approver", mcp.Description("审批人")),
|
|
mcp.WithString("url", mcp.Description("相关链接")),
|
|
),
|
|
Handler: func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
// 验证必填参数
|
|
staffGuidsRaw := make([]string, 0)
|
|
|
|
// 使用单个GUID参数
|
|
staffGuid := request.GetString("staffGuid", "")
|
|
if staffGuid != "" {
|
|
staffGuidsRaw = append(staffGuidsRaw, staffGuid)
|
|
}
|
|
|
|
if len(staffGuidsRaw) == 0 {
|
|
return nil, fmt.Errorf("员工GUID列表不能为空")
|
|
}
|
|
|
|
title := request.GetString("title", "")
|
|
if title == "" {
|
|
return nil, fmt.Errorf("通知标题不能为空")
|
|
}
|
|
|
|
content := request.GetString("content", "")
|
|
if content == "" {
|
|
return nil, fmt.Errorf("通知内容不能为空")
|
|
}
|
|
|
|
approvalId := request.GetString("approvalId", "")
|
|
if approvalId == "" {
|
|
return nil, fmt.Errorf("审批ID不能为空")
|
|
}
|
|
|
|
// 直接使用已有的staffGuidsRaw
|
|
staffGuids := staffGuidsRaw
|
|
|
|
// 创建审批通知
|
|
payload := ApprovalNoticePayload{
|
|
NotificationPayload: NotificationPayload{
|
|
Title: title,
|
|
Content: content,
|
|
URL: request.GetString("url", ""),
|
|
},
|
|
ApprovalID: approvalId,
|
|
ApprovalType: request.GetString("approvalType", ""),
|
|
ApprovalState: request.GetString("approvalState", "待审批"),
|
|
Approver: request.GetString("approver", ""),
|
|
ApprovalTime: time.Now().Format("2006-01-02 15:04:05"),
|
|
}
|
|
|
|
// 发送通知
|
|
NotifyApproval(staffGuids, payload)
|
|
|
|
// 返回结果
|
|
result := map[string]interface{}{
|
|
"success": true,
|
|
"message": fmt.Sprintf("已向%d位员工发送审批通知", len(staffGuids)),
|
|
"eventId": uuid.New().String(),
|
|
}
|
|
|
|
resultJSON, err := json.Marshal(result)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return mcp.NewToolResultText(string(resultJSON)), nil
|
|
},
|
|
},
|
|
// 发送消息提醒
|
|
{
|
|
Tool: mcp.NewTool(
|
|
"notifyMessage",
|
|
mcp.WithDescription("发送消息提醒"),
|
|
mcp.WithArray("staffGuids", mcp.Required(), mcp.Description("员工GUID列表")),
|
|
mcp.WithString("title", mcp.Required(), mcp.Description("通知标题")),
|
|
mcp.WithString("content", mcp.Required(), mcp.Description("通知内容")),
|
|
mcp.WithString("sender", mcp.Description("发送者")),
|
|
mcp.WithString("category", mcp.Description("消息分类")),
|
|
mcp.WithString("priority", mcp.Description("优先级:低、中、高")),
|
|
mcp.WithString("url", mcp.Description("相关链接")),
|
|
),
|
|
Handler: func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
// 验证必填参数
|
|
staffGuidsRaw := make([]string, 0)
|
|
|
|
// 使用单个GUID参数
|
|
staffGuid := request.GetString("staffGuid", "")
|
|
if staffGuid != "" {
|
|
staffGuidsRaw = append(staffGuidsRaw, staffGuid)
|
|
}
|
|
|
|
if len(staffGuidsRaw) == 0 {
|
|
return nil, fmt.Errorf("员工GUID列表不能为空")
|
|
}
|
|
|
|
title := request.GetString("title", "")
|
|
if title == "" {
|
|
return nil, fmt.Errorf("通知标题不能为空")
|
|
}
|
|
|
|
content := request.GetString("content", "")
|
|
if content == "" {
|
|
return nil, fmt.Errorf("通知内容不能为空")
|
|
}
|
|
|
|
// 直接使用已有的staffGuidsRaw
|
|
staffGuids := staffGuidsRaw
|
|
|
|
// 创建消息提醒
|
|
payload := MessagePayload{
|
|
NotificationPayload: NotificationPayload{
|
|
Title: title,
|
|
Content: content,
|
|
URL: request.GetString("url", ""),
|
|
},
|
|
Sender: request.GetString("sender", "系统"),
|
|
Category: request.GetString("category", "通知"),
|
|
Priority: request.GetString("priority", "中"),
|
|
ReadState: false,
|
|
}
|
|
|
|
// 发送通知
|
|
NotifyMessage(staffGuids, payload)
|
|
|
|
// 返回结果
|
|
result := map[string]interface{}{
|
|
"success": true,
|
|
"message": fmt.Sprintf("已向%d位员工发送消息提醒", len(staffGuids)),
|
|
"eventId": uuid.New().String(),
|
|
}
|
|
|
|
resultJSON, err := json.Marshal(result)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return mcp.NewToolResultText(string(resultJSON)), nil
|
|
},
|
|
},
|
|
// 广播系统通知
|
|
{
|
|
Tool: mcp.NewTool(
|
|
"broadcastSystemNotification",
|
|
mcp.WithDescription("广播系统通知给所有在线用户"),
|
|
mcp.WithString("title", mcp.Required(), mcp.Description("通知标题")),
|
|
mcp.WithString("content", mcp.Required(), mcp.Description("通知内容")),
|
|
mcp.WithString("category", mcp.Description("通知分类")),
|
|
mcp.WithString("severity", mcp.Description("严重程度:信息、警告、错误")),
|
|
mcp.WithString("url", mcp.Description("相关链接")),
|
|
),
|
|
Handler: func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
title := request.GetString("title", "")
|
|
if title == "" {
|
|
return nil, fmt.Errorf("通知标题不能为空")
|
|
}
|
|
|
|
content := request.GetString("content", "")
|
|
if content == "" {
|
|
return nil, fmt.Errorf("通知内容不能为空")
|
|
}
|
|
|
|
// 创建系统通知
|
|
payload := SystemNotificationPayload{
|
|
NotificationPayload: NotificationPayload{
|
|
Title: title,
|
|
Content: content,
|
|
URL: request.GetString("url", ""),
|
|
},
|
|
Category: request.GetString("category", "系统通知"),
|
|
Severity: request.GetString("severity", "信息"),
|
|
}
|
|
|
|
// 广播通知
|
|
BroadcastSystemNotification(payload)
|
|
|
|
// 获取当前在线用户数量
|
|
clients := GetSSEServer().GetClients()
|
|
|
|
// 返回结果
|
|
result := map[string]interface{}{
|
|
"success": true,
|
|
"message": fmt.Sprintf("已向%d位在线用户广播系统通知", len(clients)),
|
|
"eventId": uuid.New().String(),
|
|
}
|
|
|
|
resultJSON, err := json.Marshal(result)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return mcp.NewToolResultText(string(resultJSON)), nil
|
|
},
|
|
},
|
|
// 获取当前连接的用户信息
|
|
{
|
|
Tool: mcp.NewTool(
|
|
"getConnectedClients",
|
|
mcp.WithDescription("获取当前连接的用户信息"),
|
|
),
|
|
Handler: func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
// 获取所有客户端
|
|
clients := GetSSEServer().GetClients()
|
|
|
|
// 构建客户端信息列表
|
|
clientInfos := make([]map[string]interface{}, 0, len(clients))
|
|
for _, client := range clients {
|
|
if client.Connected {
|
|
clientInfo := map[string]interface{}{
|
|
"userGuid": client.UserGuid,
|
|
"staffGuid": client.StaffGuid,
|
|
"lastPing": client.LastPing.Format("2006-01-02 15:04:05"),
|
|
}
|
|
clientInfos = append(clientInfos, clientInfo)
|
|
}
|
|
}
|
|
|
|
// 返回结果
|
|
result := map[string]interface{}{
|
|
"count": len(clientInfos),
|
|
"clients": clientInfos,
|
|
}
|
|
|
|
resultJSON, err := json.Marshal(result)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return mcp.NewToolResultText(string(resultJSON)), nil
|
|
},
|
|
},
|
|
}
|