xuhy
1 天以前 a960c432d78dfe5f0ef07295d0210ddb09340e12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package com.ruoyi.web.controller.webSocket;
 
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.*;
 
/**
 * 基于AppUser的WebSocket服务类
 * 提供按用户发送消息的接口
 */
@Service
@Slf4j
public class WebSocketUserService {
 
    @Autowired
    private WebSocketUserConnectionManager userConnectionManager;
 
    /**
     * 向指定用户的所有设备发送消息
     * @param appUserId 用户ID
     * @param message 消息内容
     * @return 成功发送的设备数
     */
    public int sendMessageToUser(String appUserId, String message) {
        log.info("准备向用户 {} 发送消息: {}", appUserId, message);
        return userConnectionManager.sendMessageToUser(appUserId, message);
    }
 
    /**
     * 向指定用户发送JSON格式消息
     * @param appUserId 用户ID
     * @param messageType 消息类型
     * @param data 消息数据
     * @return 成功发送的设备数
     */
    public int sendJsonMessageToUser(String appUserId, String messageType, Object data) {
        String jsonMessage = buildJsonMessage(messageType, data, appUserId);
        return sendMessageToUser(appUserId, jsonMessage);
    }
 
    /**
     * 向多个用户发送消息
     * @param appUserIds 用户ID列表
     * @param message 消息内容
     * @return 成功发送的用户数
     */
    public int sendMessageToUsers(List<String> appUserIds, String message) {
        log.info("准备向 {} 个用户发送消息: {}", appUserIds.size(), message);
        return userConnectionManager.sendMessageToUsers(appUserIds, message);
    }
 
    /**
     * 向多个用户发送JSON格式消息
     * @param appUserIds 用户ID列表
     * @param messageType 消息类型
     * @param data 消息数据
     * @return 成功发送的用户数
     */
    public int sendJsonMessageToUsers(List<String> appUserIds, String messageType, Object data) {
        String jsonMessage = buildJsonMessage(messageType, data, null);
        return sendMessageToUsers(appUserIds, jsonMessage);
    }
 
    /**
     * 向指定连接发送消息
     * @param channelId 连接ID
     * @param message 消息内容
     * @return 是否发送成功
     */
    public boolean sendMessageToChannel(String channelId, String message) {
        log.info("准备向连接 {} 发送消息: {}", channelId, message);
        return userConnectionManager.sendMessageToChannel(channelId, message);
    }
 
    /**
     * 广播消息到所有连接
     * @param message 消息内容
     * @return 成功发送的连接数
     */
    public int broadcastMessage(String message) {
        log.info("准备广播消息: {}", message);
        return userConnectionManager.broadcastMessage(message);
    }
 
    /**
     * 广播JSON格式消息到所有连接
     * @param messageType 消息类型
     * @param data 消息数据
     * @return 成功发送的连接数
     */
    public int broadcastJsonMessage(String messageType, Object data) {
        String jsonMessage = buildJsonMessage(messageType, data, null);
        return broadcastMessage(jsonMessage);
    }
 
    /**
     * 发送系统通知消息
     * @param message 通知内容
     * @return 成功发送的连接数
     */
    public int sendSystemNotification(String message) {
        Map<String, Object> data = new HashMap<>();
        data.put("message", message);
        return sendJsonMessageToAllUsers("system_notification", data);
    }
 
    /**
     * 向所有在线用户发送系统通知
     * @param messageType 消息类型
     * @param data 消息数据
     * @return 成功发送的连接数
     */
    public int sendJsonMessageToAllUsers(String messageType, Object data) {
        Set<String> onlineUserIds = userConnectionManager.getOnlineUserIds();
        if (onlineUserIds.isEmpty()) {
            log.warn("没有在线用户,无法发送消息");
            return 0;
        }
        return sendJsonMessageToUsers(new ArrayList<>(onlineUserIds), messageType, data);
    }
 
    /**
     * 发送任务分配消息
     * @param taskId 任务ID
     * @param taskName 任务名称
     * @param assignee 分配人
     * @param assigneeUserId 分配人用户ID
     * @return 成功发送的连接数
     */
    public int sendTaskAssignment(String taskId, String taskName, String assignee, String assigneeUserId) {
        Map<String, Object> taskData = new HashMap<>();
        taskData.put("taskId", taskId);
        taskData.put("taskName", taskName);
        taskData.put("assignee", assignee);
        taskData.put("assigneeUserId", assigneeUserId);
        taskData.put("timestamp", System.currentTimeMillis());
        
        if (assigneeUserId != null) {
            // 发送给指定用户
            return sendJsonMessageToUser(assigneeUserId, "task_assignment", taskData);
        } else {
            // 广播给所有用户
            return broadcastJsonMessage("task_assignment", taskData);
        }
    }
 
    /**
     * 发送状态更新消息
     * @param status 状态
     * @param details 详细信息
     * @param targetUserId 目标用户ID,为null时广播
     * @return 成功发送的连接数
     */
    public int sendStatusUpdate(String status, String details, String targetUserId) {
        Map<String, Object> statusData = new HashMap<>();
        statusData.put("status", status);
        statusData.put("details", details);
        statusData.put("timestamp", System.currentTimeMillis());
        
        if (targetUserId != null) {
            // 发送给指定用户
            return sendJsonMessageToUser(targetUserId, "status_update", statusData);
        } else {
            // 广播给所有用户
            return broadcastJsonMessage("status_update", statusData);
        }
    }
 
    /**
     * 发送任务状态更新消息
     * @param taskId 任务ID
     * @param taskStatus 任务状态
     * @param message 消息内容
     * @param targetUserId 目标用户ID,为null时广播
     * @return 成功发送的连接数
     */
    public int sendTaskStatusUpdate(String taskId, String taskStatus, String message, String targetUserId) {
        Map<String, Object> taskStatusData = new HashMap<>();
        taskStatusData.put("taskId", taskId);
        taskStatusData.put("taskStatus", taskStatus);
        taskStatusData.put("message", message);
        taskStatusData.put("timestamp", System.currentTimeMillis());
        
        if (targetUserId != null) {
            // 发送给指定用户
            return sendJsonMessageToUser(targetUserId, "task_status_update", taskStatusData);
        } else {
            // 广播给所有用户
            return broadcastJsonMessage("task_status_update", taskStatusData);
        }
    }
 
    /**
     * 发送位置更新消息
     * @param userId 用户ID
     * @param longitude 经度
     * @param latitude 纬度
     * @param address 地址
     * @return 成功发送的连接数
     */
    public int sendLocationUpdate(String userId, String longitude, String latitude, String address) {
        Map<String, Object> locationData = new HashMap<>();
        locationData.put("longitude", longitude);
        locationData.put("latitude", latitude);
        locationData.put("address", address != null ? address : "");
        locationData.put("timestamp", System.currentTimeMillis());
        
        return sendJsonMessageToUser(userId, "location_update", locationData);
    }
 
    /**
     * 获取用户连接信息
     * @param appUserId 用户ID
     * @return 连接信息
     */
    public Map<String, Object> getUserConnectionInfo(String appUserId) {
        Map<String, Object> info = new HashMap<>();
        info.put("appUserId", appUserId);
        info.put("isOnline", userConnectionManager.isUserOnline(appUserId));
        info.put("connectionCount", userConnectionManager.getUserConnectionCount(appUserId));
        return info;
    }
 
    /**
     * 获取所有连接统计信息
     * @return 统计信息
     */
    public Map<String, Object> getConnectionStats() {
        return userConnectionManager.getConnectionStats();
    }
 
    /**
     * 检查用户是否在线
     * @param appUserId 用户ID
     * @return 是否在线
     */
    public boolean isUserOnline(String appUserId) {
        return userConnectionManager.isUserOnline(appUserId);
    }
 
    /**
     * 获取在线用户列表
     * @return 在线用户ID列表
     */
    public Set<String> getOnlineUserIds() {
        return userConnectionManager.getOnlineUserIds();
    }
 
    /**
     * 获取总连接数
     * @return 总连接数
     */
    public int getTotalConnectionCount() {
        return userConnectionManager.getTotalConnectionCount();
    }
 
    /**
     * 获取在线用户数
     * @return 在线用户数
     */
    public int getOnlineUserCount() {
        return userConnectionManager.getOnlineUserCount();
    }
 
    /**
     * 构建JSON格式消息
     * @param messageType 消息类型
     * @param data 消息数据
     * @param targetUserId 目标用户ID
     * @return JSON字符串
     */
    private String buildJsonMessage(String messageType, Object data, String targetUserId) {
        Map<String, Object> message = new HashMap<>();
        message.put("type", messageType);
        message.put("data", data);
        message.put("timestamp", System.currentTimeMillis());
        
        if (targetUserId != null) {
            message.put("targetUserId", targetUserId);
        }
        
        return com.alibaba.fastjson.JSON.toJSONString(message);
    }
}