skyline 2 месяцев назад
Родитель
Сommit
279b24104e

+ 48 - 13
haha-common/src/main/java/com/haha/common/enums/DeviceDoorStatus.java

@@ -2,18 +2,16 @@ package com.haha.common.enums;
 
 public enum DeviceDoorStatus {
 
-    ERROR("1", "error", "开门失败"),
-    OPENED("2", "open", "门已开"),
-    CLOSED("3", "close", "门已关"),
-    BUSY("4", "busy", "设备繁忙");
+    ERROR("ERROR", "开门失败"),
+    OPENED("OPENED", "门已开"),
+    CLOSED("CLOSED", "门已关"),
+    BUSY("ANOTHER", "设备繁忙");
 
     private final String code;
-    private final String status;
     private final String description;
 
-    DeviceDoorStatus(String code, String status, String description) {
+    DeviceDoorStatus(String code, String description) {
         this.code = code;
-        this.status = status;
         this.description = description;
     }
 
@@ -21,10 +19,6 @@ public enum DeviceDoorStatus {
         return code;
     }
 
-    public String getStatus() {
-        return status;
-    }
-
     public String getDescription() {
         return description;
     }
@@ -41,8 +35,49 @@ public enum DeviceDoorStatus {
         return null;
     }
 
-    public static String convertToStatus(String code) {
+    /**
+     * 将原始状态码转换为数据库存储的字符串值
+     * @param code 原始状态码("1","2","3","4")
+     * @return 数据库存储的字符串值("ERROR","OPENED","CLOSED","ANOTHER")
+     */
+    public static String convertToDbValue(String code) {
         DeviceDoorStatus doorStatus = fromCode(code);
-        return doorStatus != null ? doorStatus.getStatus() : "unknown";
+        return doorStatus != null ? doorStatus.getCode() : "UNKNOWN";
+    }
+    
+    /**
+     * 从数据库存储的字符串值获取枚举
+     * @param dbValue 数据库中的字符串值
+     * @return 对应的枚举值
+     */
+    public static DeviceDoorStatus fromDbValue(String dbValue) {
+        if (dbValue == null) {
+            return null;
+        }
+        for (DeviceDoorStatus doorStatus : values()) {
+            if (doorStatus.getCode().equals(dbValue)) {
+                return doorStatus;
+            }
+        }
+        return null;
+    }
+    
+    /**
+     * 将数据库值转换为前端显示的状态
+     * 返回简化的显示状态
+     */
+    public static String convertToStatus(String dbValue) {
+        DeviceDoorStatus doorStatus = fromDbValue(dbValue);
+        if (doorStatus == null) {
+            return "unknown";
+        }
+        
+        // 根据枚举值返回对应的简化状态
+        return switch (doorStatus) {
+            case ERROR -> "error";
+            case OPENED -> "open";
+            case CLOSED -> "close";
+            case BUSY -> "busy";
+        };
     }
 }

+ 1 - 1
haha-entity/src/main/java/com/haha/entity/Device.java

@@ -29,7 +29,7 @@ public class Device implements Serializable {
     private String currentInventoryHash;
 
     /**
-     * 门状态:1-开门失败(ERROR),2-门已开(OPENED),3-门已关(CLOSED),4-设备繁忙(BUSY)
+     * 门状态:ERROR-开门失败,OPENED-门已开,CLOSED-门已关,ANOTHER-设备繁忙
      */
     private String doorStatus;
 

+ 5 - 3
haha-miniapp/src/main/java/com/haha/miniapp/controller/CallbackController.java

@@ -204,11 +204,13 @@ public class CallbackController {
 
             // 同步更新数据库中的门状态
             try {
-                boolean updated = deviceService.updateDeviceDoorStatus(deviceId, doorStatus);
+                // 将原始状态码转换为数据库存储的字符串格式
+                String dbDoorStatus = DeviceDoorStatus.convertToDbValue(doorStatus);
+                boolean updated = deviceService.updateDeviceDoorStatus(deviceId, dbDoorStatus);
                 if (updated) {
-                    log.info("数据库门状态更新成功 - 设备: {}, 状态: {}", deviceId, doorStatus);
+                    log.info("数据库门状态更新成功 - 设备: {}, 状态: {}", deviceId, dbDoorStatus);
                 } else {
-                    log.warn("数据库门状态更新失败 - 设备: {}, 状态: {}", deviceId, doorStatus);
+                    log.warn("数据库门状态更新失败 - 设备: {}, 状态: {}", deviceId, dbDoorStatus);
                 }
             } catch (Exception e) {
                 log.error("更新数据库门状态异常 - 设备: {}", deviceId, e);

+ 64 - 67
haha-service/src/main/java/com/haha/service/impl/HahaCallbackServiceImpl.java

@@ -27,16 +27,16 @@ public class HahaCallbackServiceImpl implements HahaCallbackService {
 
     @Autowired
     private OrderService orderService;
-    
+
     @Autowired
     private DeviceMapper deviceMapper;
-    
+
     @Autowired
     private StringRedisTemplate stringRedisTemplate;
 
     @Value("${haha.api.app-secret}")
     private String appSecret;
-    
+
     private static final String DEVICE_STATUS_KEY = "haha:device:status:";
     private static final String RECOGNIZE_RESULT_KEY = "haha:recognize:result:";
     private static final String ORDER_INFO_KEY = "haha:order:info:";
@@ -78,40 +78,40 @@ public class HahaCallbackServiceImpl implements HahaCallbackService {
     public void handleDeviceStatus(Map<String, Object> params) {
         try {
             String deviceId = (String) params.get("device_id");
-            String status = (String) params.get("status");
+            String openStatus = (String) params.get("status");
             String openType = (String) params.get("open_type");
             String activityId = (String) params.get("activity_id");
             String userId = (String) params.get("user_id");
-            
-            log.info("开关门状态通知 - 设备: {}, 状态: {}, 类型: {}, 活动: {}, 用户: {}", 
-                    deviceId, status, openType, activityId, userId);
-            
+
+            log.info("开关门状态通知 - 设备: {}, 状态: {}, 类型: {}, 活动: {}, 用户: {}",
+                    deviceId, openStatus, openType, activityId, userId);
+
             // 保存设备状态到Redis,供小程序轮询
             String statusKey = DEVICE_STATUS_KEY + deviceId;
             stringRedisTemplate.opsForValue().set(statusKey, JSON.toJSONString(params), 30, TimeUnit.MINUTES);
-            
+
             // 更新设备表中的门状态字段
-            updateDeviceDoorStatus(deviceId, status);
-            
-            switch (status) {
-                case "2": // OPENED
+            updateDeviceDoorStatus(deviceId, openStatus);
+
+            switch (openStatus) {
+                case "OPENED":
                     log.info("设备 {} 开门成功", deviceId);
                     handleDeviceOpened(deviceId, activityId, userId, openType);
                     break;
-                case "3": // CLOSED
+                case "CLOSED":
                     log.info("设备 {} 关门成功,等待AI识别", deviceId);
                     handleDeviceClosed(deviceId, activityId, userId);
                     break;
-                case "1": // ERROR
+                case "ERROR":
                     log.error("设备 {} 开门失败", deviceId);
                     handleDeviceError(deviceId, activityId, userId);
                     break;
-                case "4": // ANOTHER
+                case "ANOTHER":
                     log.warn("设备 {} 繁忙", deviceId);
                     handleDeviceBusy(deviceId, activityId, userId);
                     break;
                 default:
-                    log.warn("未知的设备状态: {}", status);
+                    log.warn("未知的设备状态: {}", openStatus);
             }
         } catch (Exception e) {
             log.error("处理开关门状态通知逻辑失败", e);
@@ -124,13 +124,13 @@ public class HahaCallbackServiceImpl implements HahaCallbackService {
             String deviceId = (String) params.get("device_id");
             Object onlineObj = params.get("is_online");
             Integer isOnline = onlineObj instanceof Integer ? (Integer) onlineObj : Integer.valueOf(onlineObj.toString());
-            
+
             log.info("设备在线状态通知 - 设备: {}, 在线状态: {}", deviceId, isOnline == 1 ? "在线" : "离线");
-            
+
             // 更新设备在线状态到缓存
             String onlineKey = "device:online:" + deviceId;
             stringRedisTemplate.opsForValue().set(onlineKey, isOnline.toString(), 10, TimeUnit.MINUTES);
-            
+
         } catch (Exception e) {
             log.error("处理设备在线状态通知逻辑失败", e);
         }
@@ -142,13 +142,13 @@ public class HahaCallbackServiceImpl implements HahaCallbackService {
             String deviceId = (String) params.get("device_id");
             Object voiceObj = params.get("voice");
             Integer voice = voiceObj instanceof Integer ? (Integer) voiceObj : Integer.valueOf(voiceObj.toString());
-            
+
             log.info("音量调节结果通知 - 设备: {}, 音量值: {}", deviceId, voice);
-            
+
             // 保存音量设置到缓存
             String voiceKey = "device:voice:" + deviceId;
             stringRedisTemplate.opsForValue().set(voiceKey, voice.toString(), 30, TimeUnit.DAYS);
-            
+
         } catch (Exception e) {
             log.error("处理音量调节结果通知逻辑失败", e);
         }
@@ -162,15 +162,15 @@ public class HahaCallbackServiceImpl implements HahaCallbackService {
             Integer status = statusObj instanceof Integer ? (Integer) statusObj : Integer.valueOf(statusObj.toString());
             String name = (String) params.get("name");
             String auditRemark = (String) params.get("audit_remark");
-            
+
             log.info("新品审核结果 - ID: {}, 商品名: {}, 状态: {}, 备注: {}", id, name, status, auditRemark);
-            
+
             // 保存审核结果到缓存
             String auditKey = "product:audit:" + id;
             stringRedisTemplate.opsForValue().set(auditKey, JSON.toJSONString(params), 7, TimeUnit.DAYS);
-            
+
             // TODO: 根据审核状态更新商品信息或通知相关人员
-            
+
         } catch (Exception e) {
             log.error("处理新品审核结果通知逻辑失败", e);
         }
@@ -183,13 +183,13 @@ public class HahaCallbackServiceImpl implements HahaCallbackService {
             if (listObj instanceof JSONArray) {
                 JSONArray list = (JSONArray) listObj;
                 log.info("商品合并结果通知 - 合并数量: {}", list.size());
-                
+
                 // 保存合并结果到缓存
                 String mergeKey = "product:merge:" + System.currentTimeMillis();
                 stringRedisTemplate.opsForValue().set(mergeKey, list.toJSONString(), 1, TimeUnit.DAYS);
-                
+
                 // TODO: 处理商品合并逻辑,如更新商品关联关系
-                
+
             }
         } catch (Exception e) {
             log.error("处理商品合并结果通知逻辑失败", e);
@@ -208,11 +208,11 @@ public class HahaCallbackServiceImpl implements HahaCallbackService {
             String resourceInfoStr = (String) params.get("resource_info");
             Object confidenceObj = params.get("confidence");
             BigDecimal confidence = confidenceObj != null ? new BigDecimal(confidenceObj.toString()) : null;
-            
+
             // 保存识别结果到Redis
             String recognizeKey = RECOGNIZE_RESULT_KEY + activityId;
             stringRedisTemplate.opsForValue().set(recognizeKey, JSON.toJSONString(params), 30, TimeUnit.MINUTES);
-            
+
             if (nobuy == 1) {
                 log.info("AI识别结果: 用户未消费 (activityId: {})", activityId);
                 handleNoPurchase(activityId, deviceId, userId);
@@ -224,24 +224,24 @@ public class HahaCallbackServiceImpl implements HahaCallbackService {
             String skuListStr = (String) params.get("skuList");
             JSONObject result = JSON.parseObject(resultStr);
             JSONArray excepts = result.getJSONArray("excepts");
-            
+
             if (excepts != null && !excepts.isEmpty()) {
                 log.warn("识别结果包含异常 (activityId: {}): {}", activityId, excepts.toJSONString());
             }
 
             log.info("AI识别结果通知处理完成 - 设备: {}, 活动: {}, 用户: {}", deviceId, activityId, userId);
-            
+
             // 创建订单
             Order order = orderService.createOrderFromRecognition(
                 activityId, deviceId, userId, skuListStr, resourceInfoStr, confidence);
-            
+
             if (order != null) {
                 log.info("AI识别订单创建成功 - 订单ID: {}, 金额: {}", order.getId(), order.getTotalAmount());
-                
+
                 // 保存订单信息到Redis供小程序查询
                 saveOrderInfoToRedis(order, activityId);
             }
-            
+
         } catch (Exception e) {
             log.error("处理AI识别结果逻辑失败", e);
         }
@@ -252,9 +252,9 @@ public class HahaCallbackServiceImpl implements HahaCallbackService {
      */
     private void handleDeviceOpened(String deviceId, String activityId, String userId, String openType) {
         try {
-            log.info("处理设备开门成功 - 设备: {}, 活动: {}, 用户: {}, 类型: {}", 
+            log.info("处理设备开门成功 - 设备: {}, 活动: {}, 用户: {}, 类型: {}",
                     deviceId, activityId, userId, openType);
-            
+
             // 更新相关订单状态为进行中
             if (activityId != null && userId != null) {
                 Order order = orderService.lambdaQuery()
@@ -264,7 +264,7 @@ public class HahaCallbackServiceImpl implements HahaCallbackService {
                     .orderByDesc(Order::getCreateTime)
                     .last("LIMIT 1")
                     .one();
-                
+
                 if (order != null) {
                     order.setStatus(OrderConstants.STATUS_PENDING_PAYMENT);
                     order.setPayStatus(OrderConstants.PAY_STATUS_UNPAID);
@@ -272,34 +272,34 @@ public class HahaCallbackServiceImpl implements HahaCallbackService {
                     log.info("更新订单状态为待支付: orderNo={}", order.getOrderNo());
                 }
             }
-            
+
         } catch (Exception e) {
             log.error("处理设备开门成功逻辑失败", e);
         }
     }
-    
+
     /**
      * 处理设备关门
      */
     private void handleDeviceClosed(String deviceId, String activityId, String userId) {
         try {
             log.info("处理设备关门 - 设备: {}, 活动: {}, 用户: {}", deviceId, activityId, userId);
-            
+
             // 触发AI识别流程
             // 实际的AI识别由哈哈平台处理,我们只需等待ORC_RESULT回调
-            
+
         } catch (Exception e) {
             log.error("处理设备关门逻辑失败", e);
         }
     }
-    
+
     /**
      * 处理设备错误
      */
     private void handleDeviceError(String deviceId, String activityId, String userId) {
         try {
             log.error("处理设备错误 - 设备: {}, 活动: {}, 用户: {}", deviceId, activityId, userId);
-            
+
             // 取消相关订单
             if (activityId != null && userId != null) {
                 Order order = orderService.lambdaQuery()
@@ -309,7 +309,7 @@ public class HahaCallbackServiceImpl implements HahaCallbackService {
                     .orderByDesc(Order::getCreateTime)
                     .last("LIMIT 1")
                     .one();
-                
+
                 if (order != null) {
                     order.setStatus(OrderConstants.STATUS_CANCELLED);
                     order.setPayStatus(OrderConstants.PAY_STATUS_UNPAID);
@@ -317,34 +317,34 @@ public class HahaCallbackServiceImpl implements HahaCallbackService {
                     log.info("取消订单due to设备错误: orderNo={}", order.getOrderNo());
                 }
             }
-            
+
         } catch (Exception e) {
             log.error("处理设备错误逻辑失败", e);
         }
     }
-    
+
     /**
      * 处理设备繁忙
      */
     private void handleDeviceBusy(String deviceId, String activityId, String userId) {
         try {
             log.warn("处理设备繁忙 - 设备: {}, 活动: {}, 用户: {}", deviceId, activityId, userId);
-            
+
             // 通知用户设备繁忙,建议重试
             // TODO: 发送消息通知给用户
-            
+
         } catch (Exception e) {
             log.error("处理设备繁忙逻辑失败", e);
         }
     }
-    
+
     /**
      * 处理用户未消费情况
      */
     private void handleNoPurchase(String activityId, String deviceId, String userId) {
         try {
             log.info("处理用户未消费 - 活动: {}, 设备: {}, 用户: {}", activityId, deviceId, userId);
-            
+
             // 取消相关订单
             if (activityId != null && userId != null) {
                 Order order = orderService.lambdaQuery()
@@ -354,7 +354,7 @@ public class HahaCallbackServiceImpl implements HahaCallbackService {
                     .orderByDesc(Order::getCreateTime)
                     .last("LIMIT 1")
                     .one();
-                
+
                 if (order != null) {
                     order.setStatus(OrderConstants.STATUS_CANCELLED);
                     order.setPayStatus(OrderConstants.PAY_STATUS_UNPAID);
@@ -362,12 +362,12 @@ public class HahaCallbackServiceImpl implements HahaCallbackService {
                     log.info("取消订单due to用户未消费: orderNo={}", order.getOrderNo());
                 }
             }
-            
+
         } catch (Exception e) {
             log.error("处理用户未消费逻辑失败", e);
         }
     }
-    
+
     /**
      * 保存订单信息到Redis供小程序查询
      */
@@ -386,35 +386,32 @@ public class HahaCallbackServiceImpl implements HahaCallbackService {
             orderJson.put("payStatus", order.getPayStatus());
             orderJson.put("createTime", order.getCreateTime());
             orderJson.put("timestamp", System.currentTimeMillis());
-            
+
             stringRedisTemplate.opsForValue().set(orderKey, orderJson.toJSONString(), 30, TimeUnit.MINUTES);
-            
+
         } catch (Exception e) {
             log.error("保存订单信息到Redis失败", e);
         }
     }
-    
+
     /**
      * 更新设备门状态
      */
-    private void updateDeviceDoorStatus(String deviceId, String status) {
+    private void updateDeviceDoorStatus(String deviceId, String doorStatus) {
         try {
-            // 将原始状态码转换为数据库存储格式
-            String doorStatus = DeviceDoorStatus.convertToStatus(status);
-            
             // 使用专门的更新方法直接更新数据库
             int result = deviceMapper.updateDoorStatus(deviceId, doorStatus);
-            
+
             if (result > 0) {
-                log.info("更新设备 {} 门状态为: {} (原始状态: {})" , deviceId, doorStatus, status);
+                log.info("更新设备 {} 门状态为: {} " , deviceId, doorStatus);
             } else {
                 log.warn("未找到设备或更新失败: {}", deviceId);
             }
         } catch (Exception e) {
-            log.error("更新设备门状态失败 - 设备: {}, 原始状态: {}", deviceId, status, e);
+            log.error("更新设备门状态失败 - 设备: {}, 状态: {}", deviceId, doorStatus, e);
         }
     }
-    
+
     @Override
     public boolean validateSign(Map<String, Object> params) {
         // TODO: 实现具体的签名验证逻辑