xuhy
2026-05-08 d6c0f2ed698d6cdf86a0a3a8893b4d9717f3b7b1
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package com.xiaopeng.pushMessage.webSocket.controller;
 
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xiaopeng.api.dto.pushMessage.SendMessageDTO;
import com.xiaopeng.api.vo.XpTaskVO;
import com.xiaopeng.common.core.domain.R;
import com.xiaopeng.common.core.web.controller.BaseController;
import com.xiaopeng.pushMessage.webSocket.service.WebSocketUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
/**
 * WebSocket控制器
 * 提供REST接口用于触发服务端主动发送消息
 * 支持基于AppUser的消息发送
 */
@Slf4j
@Api(tags = "socket消息推送")
@RestController
public class WebSocketController extends BaseController {
 
 
    @Autowired
    private WebSocketUserService webSocketUserService;
 
    @Autowired ObjectMapper om;
 
    // ==================== 基于XpAppUser的WebSocket接口 ====================
 
    // 消息格式  "{\"type\":\"mission_push\",\"content\":\"{\"missionId\":\"1983875114637811713\"}\",\"timestamp\":\"2025-10-27T09:22:45.681\"}";
    /**
     * 发送订单状态
     * 状态 0=已取消,1=待接单,2=待出发,3=接驾中,4=待客上车,5=乘客已上车,6=服务中,7=待客下车,8=待付款,9=待评价,10=已完成
     * {"type":"ORDER_STATUS","content":"{\"orderId\":\"1983875114637811713\",\"status\":1}","timestamp":"176569945845456"}
     *
     * @param dto 消息实体
     * @return 操作结果
     */
    @ApiOperation(value = "发送订单状态")
    @PostMapping("/logan/inner/websocket/order/sendStatus")
    public R<String> sendStatus(@RequestBody SendMessageDTO dto) {
        try {
            String message = dto.getMessage();
            if (message.trim().isEmpty()) {
                return R.fail("消息内容不能为空");
            }
            String appUserId = dto.getAppUserId();
            if (appUserId.trim().isEmpty()) {
                return R.fail("用户id不能为空");
            }
            int deviceCount = webSocketUserService.sendMessageToUser(appUserId, message);
            if (deviceCount > 0) {
                return R.ok("消息发送成功,发送到 " + deviceCount + " 个设备");
            } else {
                return R.fail("用户不在线或发送失败");
            }
        } catch (Exception e) {
            log.error("向用户发送消息失败", e);
            return R.fail("发送消息失败: " + e.getMessage());
        }
    }
 
    @PostMapping("/base/websocket/order/sendStatusTest")
    public R<String> sendStatusTest(@RequestBody SendMessageDTO dto) {
        try {
            String message = dto.getMessage();
            if (message.trim().isEmpty()) {
                return R.fail("消息内容不能为空");
            }
            String appUserId = dto.getAppUserId();
            if (appUserId.trim().isEmpty()) {
                return R.fail("用户id不能为空");
            }
            int deviceCount = webSocketUserService.sendMessageToUser(appUserId, message);
            if (deviceCount > 0) {
                return R.ok("消息发送成功,发送到 " + deviceCount + " 个设备");
            } else {
                return R.fail("用户不在线或发送失败");
            }
        } catch (Exception e) {
            log.error("向用户发送消息失败", e);
            return R.fail("发送消息失败: " + e.getMessage());
        }
    }
 
    /**
     * 发送车辆路线、时间
     * routeType : 1=接单线路  2=行程线路
     * {
     *     "appUserId":"-1",
     *     "message":"{\"type\":\"ORDER_ROUTE\",\"content\":\"{\\\"orderId\\\":\\\"1983875114637811713\\\",\\\"routeType\\\":1,\\\"route\\\":\\\"{\\\\\\\"distance\\\\\\\":200,\\\\\\\"duration\\\\\\\":300,\\\\\\\"routeID\\\\\\\":\\\\\\\"\\\\\\\",\\\\\\\"polyline\\\\\\\":[{\\\\\\\"latitude\\\\\\\":39.932109,\\\\\\\"longitude\\\\\\\":116.362743},{\\\\\\\"latitude\\\\\\\":39.93224,\\\\\\\"longitude\\\\\\\":116.362743},{\\\\\\\"latitude\\\\\\\":39.932322,\\\\\\\"longitude\\\\\\\":116.362743},{\\\\\\\"latitude\\\\\\\":39.932322,\\\\\\\"longitude\\\\\\\":116.36474},{\\\\\\\"latitude\\\\\\\":39.932331,\\\\\\\"longitude\\\\\\\":116.367188},{\\\\\\\"latitude\\\\\\\":39.932335,\\\\\\\"longitude\\\\\\\":116.367947},{\\\\\\\"latitude\\\\\\\":39.932339,\\\\\\\"longitude\\\\\\\":116.368064},{\\\\\\\"latitude\\\\\\\":39.9324,\\\\\\\"longitude\\\\\\\":116.369214},{\\\\\\\"latitude\\\\\\\":39.932413,\\\\\\\"longitude\\\\\\\":116.369614},{\\\\\\\"latitude\\\\\\\":39.932474,\\\\\\\"longitude\\\\\\\":116.370556},{\\\\\\\"latitude\\\\\\\":39.93253,\\\\\\\"longitude\\\\\\\":116.371489},{\\\\\\\"latitude\\\\\\\":39.932609,\\\\\\\"longitude\\\\\\\":116.372956},{\\\\\\\"latitude\\\\\\\":39.932609,\\\\\\\"longitude\\\\\\\":116.373056},{\\\\\\\"latitude\\\\\\\":39.932708,\\\\\\\"longitude\\\\\\\":116.375833},{\\\\\\\"latitude\\\\\\\":39.932747,\\\\\\\"longitude\\\\\\\":116.376858},{\\\\\\\"latitude\\\\\\\":39.93286,\\\\\\\"longitude\\\\\\\":116.378902},{\\\\\\\"latitude\\\\\\\":39.932904,\\\\\\\"longitude\\\\\\\":116.379544},{\\\\\\\"latitude\\\\\\\":39.93293,\\\\\\\"longitude\\\\\\\":116.380013},{\\\\\\\"latitude\\\\\\\":39.932938,\\\\\\\"longitude\\\\\\\":116.380221},{\\\\\\\"latitude\\\\\\\":39.932969,\\\\\\\"longitude\\\\\\\":116.381224},{\\\\\\\"latitude\\\\\\\":39.932995,\\\\\\\"longitude\\\\\\\":116.381949},{\\\\\\\"latitude\\\\\\\":39.933038,\\\\\\\"longitude\\\\\\\":116.382908},{\\\\\\\"latitude\\\\\\\":39.933082,\\\\\\\"longitude\\\\\\\":116.384141},{\\\\\\\"latitude\\\\\\\":39.933095,\\\\\\\"longitude\\\\\\\":116.384631},{\\\\\\\"latitude\\\\\\\":39.933155,\\\\\\\"longitude\\\\\\\":116.386315},{\\\\\\\"latitude\\\\\\\":39.933181,\\\\\\\"longitude\\\\\\\":116.387023},{\\\\\\\"latitude\\\\\\\":39.933186,\\\\\\\"longitude\\\\\\\":116.387118},{\\\\\\\"latitude\\\\\\\":39.933234,\\\\\\\"longitude\\\\\\\":116.388294},{\\\\\\\"latitude\\\\\\\":39.933277,\\\\\\\"longitude\\\\\\\":116.389158},{\\\\\\\"latitude\\\\\\\":39.933299,\\\\\\\"longitude\\\\\\\":116.389653},{\\\\\\\"latitude\\\\\\\":39.933303,\\\\\\\"longitude\\\\\\\":116.389731},{\\\\\\\"latitude\\\\\\\":39.93332,\\\\\\\"longitude\\\\\\\":116.390043},{\\\\\\\"latitude\\\\\\\":39.933333,\\\\\\\"longitude\\\\\\\":116.390174},{\\\\\\\"latitude\\\\\\\":39.933381,\\\\\\\"longitude\\\\\\\":116.391428},{\\\\\\\"latitude\\\\\\\":39.933394,\\\\\\\"longitude\\\\\\\":116.391845},{\\\\\\\"latitude\\\\\\\":39.933429,\\\\\\\"longitude\\\\\\\":116.392813},{\\\\\\\"latitude\\\\\\\":39.933429,\\\\\\\"longitude\\\\\\\":116.392895},{\\\\\\\"latitude\\\\\\\":39.933416,\\\\\\\"longitude\\\\\\\":116.393303},{\\\\\\\"latitude\\\\\\\":39.933411,\\\\\\\"longitude\\\\\\\":116.39372},{\\\\\\\"latitude\\\\\\\":39.933394,\\\\\\\"longitude\\\\\\\":116.395091},{\\\\\\\"latitude\\\\\\\":39.933385,\\\\\\\"longitude\\\\\\\":116.396254},{\\\\\\\"latitude\\\\\\\":39.933381,\\\\\\\"longitude\\\\\\\":116.396324},{\\\\\\\"latitude\\\\\\\":39.933381,\\\\\\\"longitude\\\\\\\":116.396471},{\\\\\\\"latitude\\\\\\\":39.933385,\\\\\\\"longitude\\\\\\\":116.397726},{\\\\\\\"latitude\\\\\\\":39.933394,\\\\\\\"longitude\\\\\\\":116.399635},{\\\\\\\"latitude\\\\\\\":39.933403,\\\\\\\"longitude\\\\\\\":116.400477},{\\\\\\\"latitude\\\\\\\":39.933416,\\\\\\\"longitude\\\\\\\":116.402747},{\\\\\\\"latitude\\\\\\\":39.933416,\\\\\\\"longitude\\\\\\\":116.403099},{\\\\\\\"latitude\\\\\\\":39.933424,\\\\\\\"longitude\\\\\\\":116.40428},{\\\\\\\"latitude\\\\\\\":39.933416,\\\\\\\"longitude\\\\\\\":116.405069},{\\\\\\\"latitude\\\\\\\":39.933407,\\\\\\\"longitude\\\\\\\":116.405768},{\\\\\\\"latitude\\\\\\\":39.933407,\\\\\\\"longitude\\\\\\\":116.405846},{\\\\\\\"latitude\\\\\\\":39.933403,\\\\\\\"longitude\\\\\\\":116.406328},{\\\\\\\"latitude\\\\\\\":39.933368,\\\\\\\"longitude\\\\\\\":116.407548},{\\\\\\\"latitude\\\\\\\":39.933338,\\\\\\\"longitude\\\\\\\":116.408793},{\\\\\\\"latitude\\\\\\\":39.933338,\\\\\\\"longitude\\\\\\\":116.408889},{\\\\\\\"latitude\\\\\\\":39.933342,\\\\\\\"longitude\\\\\\\":116.410056},{\\\\\\\"latitude\\\\\\\":39.933359,\\\\\\\"longitude\\\\\\\":116.411133},{\\\\\\\"latitude\\\\\\\":39.933368,\\\\\\\"longitude\\\\\\\":116.411918},{\\\\\\\"latitude\\\\\\\":39.93339,\\\\\\\"longitude\\\\\\\":116.413433},{\\\\\\\"latitude\\\\\\\":39.933411,\\\\\\\"longitude\\\\\\\":116.414796},{\\\\\\\"latitude\\\\\\\":39.933451,\\\\\\\"longitude\\\\\\\":116.415547},{\\\\\\\"latitude\\\\\\\":39.933494,\\\\\\\"longitude\\\\\\\":116.415933},{\\\\\\\"latitude\\\\\\\":39.933594,\\\\\\\"longitude\\\\\\\":116.417101},{\\\\\\\"latitude\\\\\\\":39.933594,\\\\\\\"longitude\\\\\\\":116.417192},{\\\\\\\"latitude\\\\\\\":39.93362,\\\\\\\"longitude\\\\\\\":116.41888},{\\\\\\\"latitude\\\\\\\":39.933628,\\\\\\\"longitude\\\\\\\":116.419392},{\\\\\\\"latitude\\\\\\\":39.933628,\\\\\\\"longitude\\\\\\\":116.4198},{\\\\\\\"latitude\\\\\\\":39.933633,\\\\\\\"longitude\\\\\\\":116.421033},{\\\\\\\"latitude\\\\\\\":39.933628,\\\\\\\"longitude\\\\\\\":116.421823},{\\\\\\\"latitude\\\\\\\":39.933615,\\\\\\\"longitude\\\\\\\":116.423646},{\\\\\\\"latitude\\\\\\\":39.93362,\\\\\\\"longitude\\\\\\\":116.42431},{\\\\\\\"latitude\\\\\\\":39.933637,\\\\\\\"longitude\\\\\\\":116.42579},{\\\\\\\"latitude\\\\\\\":39.933633,\\\\\\\"longitude\\\\\\\":116.425946},{\\\\\\\"latitude\\\\\\\":39.93362,\\\\\\\"longitude\\\\\\\":116.427031},{\\\\\\\"latitude\\\\\\\":39.93362,\\\\\\\"longitude\\\\\\\":116.428012},{\\\\\\\"latitude\\\\\\\":39.933628,\\\\\\\"longitude\\\\\\\":116.42855},{\\\\\\\"latitude\\\\\\\":39.933628,\\\\\\\"longitude\\\\\\\":116.428672},{\\\\\\\"latitude\\\\\\\":39.933628,\\\\\\\"longitude\\\\\\\":116.428711},{\\\\\\\"latitude\\\\\\\":39.933628,\\\\\\\"longitude\\\\\\\":116.428876},{\\\\\\\"latitude\\\\\\\":39.933633,\\\\\\\"longitude\\\\\\\":116.429609},{\\\\\\\"latitude\\\\\\\":39.933637,\\\\\\\"longitude\\\\\\\":116.430282},{\\\\\\\"latitude\\\\\\\":39.933633,\\\\\\\"longitude\\\\\\\":116.430464},{\\\\\\\"latitude\\\\\\\":39.933637,\\\\\\\"longitude\\\\\\\":116.431458},{\\\\\\\"latitude\\\\\\\":39.933637,\\\\\\\"longitude\\\\\\\":116.431641},{\\\\\\\"latitude\\\\\\\":39.933637,\\\\\\\"longitude\\\\\\\":116.432305},{\\\\\\\"latitude\\\\\\\":39.933615,\\\\\\\"longitude\\\\\\\":116.433103},{\\\\\\\"latitude\\\\\\\":39.933611,\\\\\\\"longitude\\\\\\\":116.433168},{\\\\\\\"latitude\\\\\\\":39.933594,\\\\\\\"longitude\\\\\\\":116.433255},{\\\\\\\"latitude\\\\\\\":39.933529,\\\\\\\"longitude\\\\\\\":116.43342},{\\\\\\\"latitude\\\\\\\":39.933529,\\\\\\\"longitude\\\\\\\":116.43342},{\\\\\\\"latitude\\\\\\\":39.933451,\\\\\\\"longitude\\\\\\\":116.433494},{\\\\\\\"latitude\\\\\\\":39.933333,\\\\\\\"longitude\\\\\\\":116.43365},{\\\\\\\"latitude\\\\\\\":39.933294,\\\\\\\"longitude\\\\\\\":116.433724},{\\\\\\\"latitude\\\\\\\":39.933268,\\\\\\\"longitude\\\\\\\":116.433811},{\\\\\\\"latitude\\\\\\\":39.933255,\\\\\\\"longitude\\\\\\\":116.433906},{\\\\\\\"latitude\\\\\\\":39.933264,\\\\\\\"longitude\\\\\\\":116.434371},{\\\\\\\"latitude\\\\\\\":39.933277,\\\\\\\"longitude\\\\\\\":116.434466},{\\\\\\\"latitude\\\\\\\":39.93329,\\\\\\\"longitude\\\\\\\":116.434497},{\\\\\\\"latitude\\\\\\\":39.933312,\\\\\\\"longitude\\\\\\\":116.434536},{\\\\\\\"latitude\\\\\\\":39.933381,\\\\\\\"longitude\\\\\\\":116.434644},{\\\\\\\"latitude\\\\\\\":39.933516,\\\\\\\"longitude\\\\\\\":116.434783},{\\\\\\\"latitude\\\\\\\":39.933594,\\\\\\\"longitude\\\\\\\":116.434857},{\\\\\\\"latitude\\\\\\\":39.933594,\\\\\\\"longitude\\\\\\\":116.434857},{\\\\\\\"latitude\\\\\\\":39.933628,\\\\\\\"longitude\\\\\\\":116.434948},{\\\\\\\"latitude\\\\\\\":39.933641,\\\\\\\"longitude\\\\\\\":116.435056},{\\\\\\\"latitude\\\\\\\":39.93365,\\\\\\\"longitude\\\\\\\":116.435269},{\\\\\\\"latitude\\\\\\\":39.933641,\\\\\\\"longitude\\\\\\\":116.436619},{\\\\\\\"latitude\\\\\\\":39.933641,\\\\\\\"longitude\\\\\\\":116.436619},{\\\\\\\"latitude\\\\\\\":39.93286,\\\\\\\"longitude\\\\\\\":116.436606},{\\\\\\\"latitude\\\\\\\":39.932405,\\\\\\\"longitude\\\\\\\":116.43661},{\\\\\\\"latitude\\\\\\\":39.932075,\\\\\\\"longitude\\\\\\\":116.43661},{\\\\\\\"latitude\\\\\\\":39.931866,\\\\\\\"longitude\\\\\\\":116.43661},{\\\\\\\"latitude\\\\\\\":39.931628,\\\\\\\"longitude\\\\\\\":116.436602},{\\\\\\\"latitude\\\\\\\":39.930977,\\\\\\\"longitude\\\\\\\":116.436623},{\\\\\\\"latitude\\\\\\\":39.93049,\\\\\\\"longitude\\\\\\\":116.436701},{\\\\\\\"latitude\\\\\\\":39.930278,\\\\\\\"longitude\\\\\\\":116.43678},{\\\\\\\"latitude\\\\\\\":39.930278,\\\\\\\"longitude\\\\\\\":116.43678},{\\\\\\\"latitude\\\\\\\":39.930239,\\\\\\\"longitude\\\\\\\":116.435699},{\\\\\\\"latitude\\\\\\\":39.930239,\\\\\\\"longitude\\\\\\\":116.434965},{\\\\\\\"latitude\\\\\\\":39.93023,\\\\\\\"longitude\\\\\\\":116.434497},{\\\\\\\"latitude\\\\\\\":39.93023,\\\\\\\"longitude\\\\\\\":116.434497},{\\\\\\\"latitude\\\\\\\":39.930755,\\\\\\\"longitude\\\\\\\":116.434475},{\\\\\\\"latitude\\\\\\\":39.930946,\\\\\\\"longitude\\\\\\\":116.434466},{\\\\\\\"latitude\\\\\\\":39.931124,\\\\\\\"longitude\\\\\\\":116.434457}]}\\\"}\",\"timestamp\":\"176569945845456\"}"
     * }
     *
     * @param dto 消息实体
     * @return 操作结果
     */
    @ApiOperation(value = "发送车辆路线、时间")
    @PostMapping("/logan/inner/websocket/order/sendRoute")
    public R<String> sendRoute(@RequestBody SendMessageDTO dto) {
        try {
            String message = dto.getMessage();
            if (message.trim().isEmpty()) {
                return R.fail("消息内容不能为空");
            }
            String appUserId = dto.getAppUserId();
            if (appUserId.trim().isEmpty()) {
                return R.fail("用户id不能为空");
            }
            int deviceCount = webSocketUserService.sendMessageToUser(appUserId, message);
            if (deviceCount > 0) {
                return R.ok("消息发送成功,发送到 " + deviceCount + " 个设备");
            } else {
                return R.fail("用户不在线或发送失败");
            }
        } catch (Exception e) {
            log.error("向用户发送消息失败", e);
            return R.fail("发送消息失败: " + e.getMessage());
        }
    }
 
    /**
     * 发送车辆实时定位
     * {
     *     "appUserId":"-1",
     *     "message":"{\"type\":\"VEHICLE_LOCATION\",\"content\":\"{\\\"orderId\\\":\\\"1983875114637811713\\\",\\\"lon\\\":116.362743,\\\"lat\\\":39.93224}\",\"timestamp\":\"176569945845456\"}"
     * }
     *
     * @param dto 消息实体
     * @return 操作结果
     */
    @ApiOperation(value = "发送车辆实时定位")
    @PostMapping("/inner/websocket/order/sendVehicleLocation")
    public R<String> sendVehicleLocation(@RequestBody SendMessageDTO dto) {
        try {
            String message = dto.getMessage();
            if (message.trim().isEmpty()) {
                return R.fail("消息内容不能为空");
            }
            String appUserId = dto.getAppUserId();
            if (appUserId.trim().isEmpty()) {
                return R.fail("用户id不能为空");
            }
            int deviceCount = webSocketUserService.sendMessageToUser(appUserId, message);
            if (deviceCount > 0) {
                return R.ok("消息发送成功,发送到 " + deviceCount + " 个设备");
            } else {
                return R.fail("用户不在线或发送失败");
            }
        } catch (Exception e) {
            log.error("向用户发送消息失败", e);
            return R.fail("发送消息失败: " + e.getMessage());
        }
    }
 
    /**
     * 发送是否开启倒计时
     * {
     *     "appUserId":"-1",
     *     {"type":"OPEN_TRUNK","content":"{\"orderId\":\"1983875114637811713\",\"isOpen\":1}","timestamp":"176569945845456"}
     * }
     *
     * @param dto 消息实体
     * @return 操作结果
     */
    @ApiOperation(value = "发送是否开启倒计时")
    @PostMapping("/inner/websocket/order/sendOpenTrunk")
    public R<String> sendOpenTrunk(@RequestBody SendMessageDTO dto) {
        try {
            String message = dto.getMessage();
            if (message.trim().isEmpty()) {
                return R.fail("消息内容不能为空");
            }
            String appUserId = dto.getAppUserId();
            if (appUserId.trim().isEmpty()) {
                return R.fail("用户id不能为空");
            }
            int deviceCount = webSocketUserService.sendMessageToUser(appUserId, message);
            if (deviceCount > 0) {
                return R.ok("消息发送成功,发送到 " + deviceCount + " 个设备");
            } else {
                return R.fail("用户不在线或发送失败");
            }
        } catch (Exception e) {
            log.error("向用户发送消息失败", e);
            return R.fail("发送消息失败: " + e.getMessage());
        }
    }
 
    /**
     * 向指定用户发送JSON格式消息
     * @param appUserId 用户ID
     * @param request 请求参数
     * @return 操作结果
     */
    @PostMapping("/user/send-json/{appUserId}")
    public R<String> sendJsonMessageToUser(@PathVariable String appUserId, @RequestBody Map<String, Object> request) {
        try {
            String messageType = (String) request.get("messageType");
            Object data = request.get("data");
 
            if (messageType == null || messageType.trim().isEmpty()) {
                return R.fail("消息类型不能为空");
            }
 
            int deviceCount = webSocketUserService.sendJsonMessageToUser(appUserId, messageType, data);
            if (deviceCount > 0) {
                return R.ok("JSON消息发送成功,发送到 " + deviceCount + " 个设备");
            } else {
                return R.fail("用户不在线或发送失败");
            }
        } catch (Exception e) {
            log.error("向用户发送JSON消息失败", e);
            return R.fail("发送消息失败: " + e.getMessage());
        }
    }
 
    /**
     * 向多个用户发送消息
     * @param request 请求参数
     * @return 操作结果
     */
    @PostMapping("/user/send-multiple")
    public R<String> sendMessageToUsers(@RequestBody Map<String, Object> request) {
        try {
            @SuppressWarnings("unchecked")
            List<String> appUserIds = (List<String>) request.get("appUserIds");
            String message = (String) request.get("message");
 
            if (appUserIds == null || appUserIds.isEmpty()) {
                return R.fail("用户ID列表不能为空");
            }
            if (message == null || message.trim().isEmpty()) {
                return R.fail("消息内容不能为空");
            }
 
            int successUserCount = webSocketUserService.sendMessageToUsers(appUserIds, message);
            return R.ok("消息发送完成,成功发送到 " + successUserCount + " 个用户");
        } catch (Exception e) {
            log.error("向多个用户发送消息失败", e);
            return R.fail("发送消息失败: " + e.getMessage());
        }
    }
 
 
    /**
     * 发送状态更新消息
     * @param request 请求参数
     * @return 操作结果
     */
    @PostMapping("/user/status-update")
    public R<String> sendStatusUpdate(@RequestBody Map<String, String> request) {
        try {
            String status = request.get("status");
            String details = request.get("details");
            String targetUserId = request.get("targetUserId");
 
            if (status == null || status.trim().isEmpty()) {
                return R.fail("状态不能为空");
            }
 
            int successCount = webSocketUserService.sendStatusUpdate(status, details, targetUserId);
            return R.ok("状态更新消息发送完成,成功发送到 " + successCount + " 个设备");
        } catch (Exception e) {
            log.error("发送状态更新消息失败", e);
            return R.fail("发送状态更新消息失败: " + e.getMessage());
        }
    }
 
    /**
     * 发送位置更新消息
     * @param request 请求参数
     * @return 操作结果
     */
    @PostMapping("/user/location-update")
    public R<String> sendLocationUpdate(@RequestBody Map<String, String> request) {
        try {
            String userId = request.get("userId");
            String longitude = request.get("longitude");
            String latitude = request.get("latitude");
            String address = request.get("address");
 
            if (userId == null || longitude == null || latitude == null) {
                return R.fail("用户ID、经度和纬度不能为空");
            }
 
            int successCount = webSocketUserService.sendLocationUpdate(userId, longitude, latitude, address);
            return R.ok("位置更新消息发送完成,成功发送到 " + successCount + " 个设备");
        } catch (Exception e) {
            log.error("发送位置更新消息失败", e);
            return R.fail("发送位置更新消息失败: " + e.getMessage());
        }
    }
 
    /**
     * 获取用户连接信息
     * @param appUserId 用户ID
     * @return 连接信息
     */
    @GetMapping("/user/info/{appUserId}")
    public R getUserConnectionInfo(@PathVariable String appUserId) {
        try {
            Map<String, Object> info = webSocketUserService.getUserConnectionInfo(appUserId);
            return R.ok(info, "获取用户连接信息成功");
        } catch (Exception e) {
            log.error("获取用户连接信息失败", e);
            return R.fail("获取用户连接信息失败: " + e.getMessage());
        }
    }
 
    /**
     * 获取所有连接统计信息
     * @return 统计信息
     */
    @GetMapping("/user/stats")
    public R getConnectionStats() {
        try {
            Map<String, Object> stats = webSocketUserService.getConnectionStats();
            return R.ok(stats, "获取连接统计信息成功");
        } catch (Exception e) {
            log.error("获取连接统计信息失败", e);
            return R.fail("获取连接统计信息失败: " + e.getMessage());
        }
    }
 
    /**
     * 检查用户是否在线
     * @param appUserId 用户ID
     * @return 检查结果
     */
    @GetMapping("/user/online/{appUserId}")
    public R checkUserOnline(@PathVariable String appUserId) {
        try {
            boolean isOnline = webSocketUserService.isUserOnline(appUserId);
            return R.ok(isOnline, "用户在线状态检查完成");
        } catch (Exception e) {
            log.error("检查用户在线状态失败", e);
            return R.fail("检查用户在线状态失败: " + e.getMessage());
        }
    }
 
    /**
     * 获取在线用户列表
     * @return 在线用户列表
     */
    @GetMapping("/user/online-list")
    public R getOnlineUserIds() {
        try {
            Set<String> onlineUserIds = webSocketUserService.getOnlineUserIds();
            return R.ok(onlineUserIds, "获取在线用户列表成功");
        } catch (Exception e) {
            log.error("获取在线用户列表失败", e);
            return R.fail("获取在线用户列表失败: " + e.getMessage());
        }
    }
 
 
    /**
     * 运维工单app推送
     */
    @PostMapping("/inner/websocket/push/task")
    public R<?> pushTask(@RequestBody XpTaskVO vo) throws JsonProcessingException {
 
        String handleUserId = vo.getHandleUserId();
        Map<String, Object> msg = new HashMap<>();
        msg.put("type", "task");
        msg.put("data", vo);
        webSocketUserService.sendMessageToUser(handleUserId, om.writeValueAsString(msg));
        return R.ok();
 
    }
 
}