fix:修复实时数据点位类型展示问题

hhk
HuangHuiKang 1 month ago
parent 9c07497475
commit ebb6014659

@ -715,6 +715,9 @@ public class DeviceServiceImpl implements DeviceService {
DeviceAttributeTypeDO::getName DeviceAttributeTypeDO::getName
)); ));
Map<Long, String> attributeTypeCache = new HashMap<>();
// 3. 遍历并处理 // 3. 遍历并处理
for (Map<String, Object> deviceData : deviceDataList) { for (Map<String, Object> deviceData : deviceDataList) {
String queryDataJson = (String) deviceData.get("queryData"); String queryDataJson = (String) deviceData.get("queryData");
@ -729,12 +732,31 @@ public class DeviceServiceImpl implements DeviceService {
for (Map<String, Object> data : dataList) { for (Map<String, Object> data : dataList) {
// 获取属性类型名称 // 获取属性类型名称
String attributeTypeName = "其他"; String attributeTypeName = "其他";
String typeStr = (String) data.get("attributeType"); Object typeObj = data.get("attributeType");
if (typeStr != null) {
if (typeObj != null) {
String typeStr = typeObj.toString();
try { try {
// 尝试当作数字 ID
Long typeId = Long.parseLong(typeStr);
// 先从缓存查
if (attributeTypeCache.containsKey(typeId)) {
attributeTypeName = attributeTypeCache.get(typeId);
} else {
// 查数据库
String nameFromDb = idToNameMap.get(typeId);
if (nameFromDb != null) {
attributeTypeName = nameFromDb;
} else {
attributeTypeName = typeStr; // 查不到就用原值
}
attributeTypeCache.put(typeId, attributeTypeName);
}
} catch (NumberFormatException e) {
// 不是数字,直接用原字符串
attributeTypeName = typeStr; attributeTypeName = typeStr;
} catch (Exception e) {
attributeTypeName = "未知";
} }
} }
@ -781,7 +803,8 @@ public class DeviceServiceImpl implements DeviceService {
// 2. 获取属性类型映射 (ID -> Name) 并构建缓存,避免重复数据库查询 // 2. 获取属性类型映射 (ID -> Name) 并构建缓存,避免重复数据库查询
Map<Long, String> idToNameMap = deviceAttributeTypeMapper.selectList() Map<Long, String> idToNameMap = deviceAttributeTypeMapper.selectList()
.stream() .stream()
.collect(Collectors.toMap(DeviceAttributeTypeDO::getId, DeviceAttributeTypeDO::getName)); .collect(Collectors.toMap(DeviceAttributeTypeDO::getId, DeviceAttributeTypeDO::getName)
);
// 额外缓存数字ID -> 名称防止同一个ID多次查询数据库 // 额外缓存数字ID -> 名称防止同一个ID多次查询数据库
Map<Long, String> attributeTypeCache = new HashMap<>(); Map<Long, String> attributeTypeCache = new HashMap<>();

Loading…
Cancel
Save