From c97a9cd2fc453fcf3b7e7bc1f24e45fa318040a1 Mon Sep 17 00:00:00 2001
From: baoshiwei <baoshiwei@shlanbao.cn>
Date: 星期五, 08 十一月 2024 11:27:02 +0800
Subject: [PATCH] 解决mqtt会话无租户信息时数据查询问题

---
 jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mqtt/MqttSampleCallback.java |   58 +++++++++++++++++++++++++++++++++-------------------------
 1 files changed, 33 insertions(+), 25 deletions(-)

diff --git a/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mqtt/MqttSampleCallback.java b/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mqtt/MqttSampleCallback.java
index ab6e2a1..4b0354b 100644
--- a/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mqtt/MqttSampleCallback.java
+++ b/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mqtt/MqttSampleCallback.java
@@ -102,9 +102,14 @@
 
         }
         if (topic.startsWith("$SYS/brokers/") && topic.endsWith("disconnected")) {
-          String clientid = messageJson.getString("clientid");
-          redisUtil.del(MqttConstant.MQTT_ONLINE_CLIENT + clientid);
-          System.err.println(String.format("璁惧: %s涓嬬嚎", clientid));
+          try {
+            String clientid = messageJson.getString("clientid");
+            redisUtil.del(MqttConstant.MQTT_ONLINE_CLIENT + clientid);
+            System.err.println(String.format("璁惧: %s涓嬬嚎", clientid));
+          } catch (Exception e) {
+            e.printStackTrace();
+          }
+
         }
         parseAdminCommand(topic, mqttMessage);
 
@@ -188,34 +193,36 @@
 
         // 鎺ユ敹璁惧瀹炴椂鏁版嵁
       case MqttConstant.TENANT_UP_PREFIX_REALTIME_DATA:
-        try {
-          RealTimeDataVo vo = JSON.parseObject(message, RealTimeDataVo.class);
-          realTimeDataService.realTimeDataHandle(vo);
-        } catch (Exception e) {
-          e.printStackTrace();
-        }
+        ThreadUtil.execute(() -> {
+          try {
+            RealTimeDataVo vo = JSON.parseObject(message, RealTimeDataVo.class);
+            realTimeDataService.realTimeDataHandle(vo);
+          } catch (Exception e) {
+            e.printStackTrace();
+          }
+        });
 
         break;
       case MqttConstant.TENANT_UP_PREFIX_EQU:
-        try {
-          DryEquipment equipment = (DryEquipment) messageJson.get("equipment");
+        ThreadUtil.execute(() -> {
+          try {
+            DryEquipment equipment = (DryEquipment) messageJson.get("equipment");
 
-          DryEquipment dryEquipment = equipmentService.selectByTenantIdEquipmentId(equipment.getTenantId() + "", equipment.getCode());
+            DryEquipment dryEquipment = equipmentService.selectByTenantIdEquipmentId(equipment.getTenantId() + "", equipment.getCode());
+            if (dryEquipment == null) {
+              equipmentService.save(equipment);
+            }
 
-          if (dryEquipment == null) {
-            equipmentService.save(equipment);
+            // 鑾峰彇璁惧鎵�灞炶溅闂�
+            DryShop shop = (DryShop) messageJson.get("shop");
+            shop.setTenantId(equipment.getTenantId());
+            dryShopService.save(shop);
+
+          } catch (Exception e) {
+            e.printStackTrace();
           }
+        });
 
-
-
-          // 鑾峰彇璁惧鎵�灞炶溅闂�
-          DryShop shop = (DryShop) messageJson.get("shop");
-          shop.setTenantId(equipment.getTenantId());
-          dryShopService.save(shop);
-
-        } catch (Exception e) {
-          e.printStackTrace();
-        }
     }
 
   }
@@ -266,6 +273,7 @@
           String clientId = mqttUtil.getMqttClient().getClientId();
           String tenant = clientId.substring(clientId.lastIndexOf("_")+1);
           if (tenantId!=null && tenantId.equals(tenant)) {
+            TenantContext.setTenant(tenantId);
             // 鏍规嵁璁惧缂栫爜鏌ヨ璁惧淇℃伅
             String code = messageJson.getString("code");
             DryEquipment equipmentVo = equipmentService.selectByTenantIdEquipmentId(tenantId,code);
@@ -276,7 +284,7 @@
 
             JSONObject res = new JSONObject();
 
-            res.put("tenant", clientId);
+            res.put("tenant", tenantId);
             res.put("equipment", equipmentVo);
             res.put("shop", shop);
             res.put("eqpType", eqpType);

--
Gitblit v1.9.3