|
|
@@ -10,15 +10,6 @@ import org.springframework.web.bind.annotation.*;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
-/**
|
|
|
- * 状态查询控制器
|
|
|
- * 供小程序轮询查询设备状态、识别结果和订单信息
|
|
|
- *
|
|
|
- * 数据流程:
|
|
|
- * 1. 哈哈平台通过回调推送状态到 CallbackController
|
|
|
- * 2. CallbackController 将状态保存到 Redis
|
|
|
- * 3. 小程序通过此控制器轮询 Redis 获取最新状态
|
|
|
- */
|
|
|
@Slf4j
|
|
|
@RestController
|
|
|
@RequestMapping("/status")
|
|
|
@@ -32,23 +23,21 @@ public class StatusQueryController {
|
|
|
private static final String RECOGNIZE_RESULT_KEY = "haha:recognize:result:";
|
|
|
private static final String ORDER_INFO_KEY = "haha:order:info:";
|
|
|
|
|
|
- /**
|
|
|
- * 查询设备状态
|
|
|
- *
|
|
|
- * @param deviceId 设备ID
|
|
|
- * @return 设备状态信息
|
|
|
- */
|
|
|
@PostMapping("/device")
|
|
|
public Result<Map<String, String>> queryDeviceStatus(@RequestBody Map<String, String> params) {
|
|
|
String deviceId = params.get("deviceId");
|
|
|
if (deviceId == null || deviceId.isEmpty()) {
|
|
|
+ log.warn("[状态查询] 查询设备状态失败 - 设备ID为空");
|
|
|
return Result.error(400, "设备ID不能为空");
|
|
|
}
|
|
|
|
|
|
+ log.debug("[状态查询] 查询设备状态 - deviceId: {}", deviceId);
|
|
|
+
|
|
|
String statusKey = DEVICE_STATUS_KEY + deviceId;
|
|
|
Map<Object, Object> statusData = redisTemplate.opsForHash().entries(statusKey);
|
|
|
|
|
|
if (statusData.isEmpty()) {
|
|
|
+ log.debug("[状态查询] 设备状态暂无数据 - deviceId: {}", deviceId);
|
|
|
Map<String, String> emptyResult = new HashMap<>();
|
|
|
emptyResult.put("deviceId", deviceId);
|
|
|
emptyResult.put("doorStatus", "unknown");
|
|
|
@@ -56,7 +45,6 @@ public class StatusQueryController {
|
|
|
}
|
|
|
|
|
|
Map<String, String> result = new HashMap<>();
|
|
|
- // 只返回必要的字段,确保门状态字段名为doorStatus
|
|
|
result.put("deviceId", statusData.getOrDefault("deviceId", "").toString());
|
|
|
result.put("activityId", statusData.getOrDefault("activityId", "").toString());
|
|
|
result.put("doorStatus", statusData.getOrDefault("doorStatus", "unknown").toString());
|
|
|
@@ -64,81 +52,78 @@ public class StatusQueryController {
|
|
|
result.put("userId", statusData.getOrDefault("userId", "").toString());
|
|
|
result.put("timestamp", statusData.getOrDefault("timestamp", "").toString());
|
|
|
|
|
|
+ log.debug("[状态查询] 设备状态查询成功 - deviceId: {}, doorStatus: {}", deviceId, result.get("doorStatus"));
|
|
|
+
|
|
|
return Result.success("查询成功", result);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 查询识别结果
|
|
|
- *
|
|
|
- * @param activityId 活动ID
|
|
|
- * @return 识别结果
|
|
|
- */
|
|
|
@PostMapping("/recognize")
|
|
|
public Result<Map<String, String>> queryRecognizeResult(@RequestBody Map<String, String> params) {
|
|
|
String activityId = params.get("activityId");
|
|
|
if (activityId == null || activityId.isEmpty()) {
|
|
|
+ log.warn("[状态查询] 查询识别结果失败 - 活动ID为空");
|
|
|
return Result.error(400, "活动ID不能为空");
|
|
|
}
|
|
|
|
|
|
+ log.info("[状态查询] 查询识别结果 - activityId: {}", activityId);
|
|
|
+
|
|
|
String resultKey = RECOGNIZE_RESULT_KEY + activityId;
|
|
|
Map<Object, Object> resultData = redisTemplate.opsForHash().entries(resultKey);
|
|
|
|
|
|
if (resultData.isEmpty()) {
|
|
|
+ log.debug("[状态查询] 识别结果尚未生成 - activityId: {}", activityId);
|
|
|
return Result.error(404, "识别结果尚未生成,请稍后重试");
|
|
|
}
|
|
|
|
|
|
Map<String, String> result = new HashMap<>();
|
|
|
resultData.forEach((k, v) -> result.put(k.toString(), v != null ? v.toString() : ""));
|
|
|
|
|
|
+ log.info("[状态查询] 识别结果查询成功 - activityId: {}, nobuy: {}", activityId, result.get("nobuy"));
|
|
|
+
|
|
|
return Result.success("查询成功", result);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 查询订单信息
|
|
|
- *
|
|
|
- * @param orderId 订单ID
|
|
|
- * @param activityId 活动ID
|
|
|
- * @return 订单信息
|
|
|
- */
|
|
|
@PostMapping("/order")
|
|
|
public Result<Map<String, String>> queryOrderInfo(@RequestBody Map<String, String> params) {
|
|
|
String orderId = params.get("orderId");
|
|
|
String activityId = params.get("activityId");
|
|
|
|
|
|
if ((orderId == null || orderId.isEmpty()) && (activityId == null || activityId.isEmpty())) {
|
|
|
+ log.warn("[状态查询] 查询订单信息失败 - 订单ID和活动ID同时为空");
|
|
|
return Result.error(400, "订单ID或活动ID不能同时为空");
|
|
|
}
|
|
|
|
|
|
- String orderKey = ORDER_INFO_KEY + (orderId != null && !orderId.isEmpty() ? orderId : activityId);
|
|
|
+ String queryKey = orderId != null && !orderId.isEmpty() ? orderId : activityId;
|
|
|
+ log.debug("[状态查询] 查询订单信息 - orderId: {}, activityId: {}", orderId, activityId);
|
|
|
+
|
|
|
+ String orderKey = ORDER_INFO_KEY + queryKey;
|
|
|
Map<Object, Object> orderData = redisTemplate.opsForHash().entries(orderKey);
|
|
|
|
|
|
if (orderData.isEmpty()) {
|
|
|
+ log.debug("[状态查询] 订单信息尚未生成 - queryKey: {}", queryKey);
|
|
|
return Result.error(404, "订单信息尚未生成,请稍后重试");
|
|
|
}
|
|
|
|
|
|
Map<String, String> result = new HashMap<>();
|
|
|
orderData.forEach((k, v) -> result.put(k.toString(), v != null ? v.toString() : ""));
|
|
|
|
|
|
+ log.debug("[状态查询] 订单信息查询成功 - queryKey: {}, totalAmount: {}", queryKey, result.get("totalAmount"));
|
|
|
+
|
|
|
return Result.success("查询成功", result);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 综合状态查询
|
|
|
- * 根据设备ID查询所有相关状态(设备状态 + 最新的识别结果和订单)
|
|
|
- *
|
|
|
- * @param deviceId 设备ID
|
|
|
- * @return 综合状态信息
|
|
|
- */
|
|
|
@PostMapping("/all")
|
|
|
public Result<Map<String, Object>> queryAllStatus(@RequestBody Map<String, String> params) {
|
|
|
String deviceId = params.get("deviceId");
|
|
|
if (deviceId == null || deviceId.isEmpty()) {
|
|
|
+ log.warn("[状态查询] 综合状态查询失败 - 设备ID为空");
|
|
|
return Result.error(400, "设备ID不能为空");
|
|
|
}
|
|
|
|
|
|
+ log.debug("[状态查询] 综合状态查询 - deviceId: {}", deviceId);
|
|
|
+
|
|
|
Map<String, Object> result = new HashMap<>();
|
|
|
|
|
|
- // 查询设备状态
|
|
|
String statusKey = DEVICE_STATUS_KEY + deviceId;
|
|
|
Map<Object, Object> statusData = redisTemplate.opsForHash().entries(statusKey);
|
|
|
if (!statusData.isEmpty()) {
|
|
|
@@ -146,10 +131,8 @@ public class StatusQueryController {
|
|
|
statusData.forEach((k, v) -> deviceStatus.put(k.toString(), v != null ? v.toString() : ""));
|
|
|
result.put("deviceStatus", deviceStatus);
|
|
|
|
|
|
- // 如果有活动ID,查询识别结果和订单
|
|
|
String activityId = deviceStatus.get("activityId");
|
|
|
if (activityId != null && !activityId.isEmpty()) {
|
|
|
- // 查询识别结果
|
|
|
String recognizeKey = RECOGNIZE_RESULT_KEY + activityId;
|
|
|
Map<Object, Object> recognizeData = redisTemplate.opsForHash().entries(recognizeKey);
|
|
|
if (!recognizeData.isEmpty()) {
|
|
|
@@ -158,7 +141,6 @@ public class StatusQueryController {
|
|
|
result.put("recognizeResult", recognizeResult);
|
|
|
}
|
|
|
|
|
|
- // 查询订单信息
|
|
|
String orderKey = ORDER_INFO_KEY + activityId;
|
|
|
Map<Object, Object> orderData = redisTemplate.opsForHash().entries(orderKey);
|
|
|
if (!orderData.isEmpty()) {
|
|
|
@@ -174,6 +156,8 @@ public class StatusQueryController {
|
|
|
result.put("deviceStatus", emptyStatus);
|
|
|
}
|
|
|
|
|
|
+ log.debug("[状态查询] 综合状态查询成功 - deviceId: {}, 包含数据: {}", deviceId, result.keySet());
|
|
|
+
|
|
|
return Result.success("查询成功", result);
|
|
|
}
|
|
|
}
|