Kaynağa Gözat

refactor(EquipmentParam):寄存器关联设备id和顶级父设备id改为code

HMY 7 ay önce
ebeveyn
işleme
ccbe6b6a4c

+ 5 - 5
admin/src/main/java/com/dcs/equipment/service/impl/DataTwinServiceImpl.java

@@ -160,7 +160,7 @@ public class DataTwinServiceImpl implements DataTwinService {
         M1Pressure.setRegisterName("M1压力");
         extracted(M1dataTwinEquipmentVoList, M1Pressure);
         EquipmentParamFormVO M1Temperature = new EquipmentParamFormVO();
-        M1Temperature.setEquipmentId(0L);
+//        M1Temperature.setEquipmentId(0L);
         M1Temperature.setRegisterName("M1温度");
         M1Temperature.setValue(Float.parseFloat(S1Temperature.getValue().toString()) * 1.2f);
         extracted(M1dataTwinEquipmentVoList, M1Temperature);
@@ -171,7 +171,7 @@ public class DataTwinServiceImpl implements DataTwinService {
         M2Pressure.setRegisterName("M2压力");
         extracted(M2dataTwinEquipmentVoList, M2Pressure);
         EquipmentParamFormVO M2Temperature = new EquipmentParamFormVO();
-        M2Temperature.setEquipmentId(0L);
+//        M2Temperature.setEquipmentId(0L);
         M2Temperature.setRegisterName("M2温度");
         M2Temperature.setValue(Float.parseFloat(S2Temperature.getValue().toString()) * 1.2f);
         extracted(M2dataTwinEquipmentVoList, M2Temperature);
@@ -196,19 +196,19 @@ public class DataTwinServiceImpl implements DataTwinService {
         extracted(dataTwinEquipmentVoList, S2Temperature);
         // S3
         EquipmentParamFormVO S3 = new EquipmentParamFormVO();
-        S3.setEquipmentId(0L);
+//        S3.setEquipmentId(0L);
         S3.setRegisterName("S3温度");
         S3.setValue(Float.parseFloat(S2Temperature.getValue().toString()) * 1.3f);
         extracted(dataTwinEquipmentVoList, S3);
         // M1
         EquipmentParamFormVO M1 = new EquipmentParamFormVO();
-        M1.setEquipmentId(0L);
+//        M1.setEquipmentId(0L);
         M1.setRegisterName("M1温度");
         M1.setValue(Float.parseFloat(S1Temperature.getValue().toString()) * 1.2f);
         extracted(dataTwinEquipmentVoList, M1);
         // M2
         EquipmentParamFormVO M2 = new EquipmentParamFormVO();
-        M2.setEquipmentId(0L);
+//        M2.setEquipmentId(0L);
         M2.setRegisterName("M2温度");
         M2.setValue(Float.parseFloat(S2Temperature.getValue().toString()) * 1.2f);
         extracted(dataTwinEquipmentVoList, M2);

+ 6 - 0
admin/src/main/java/com/dcs/hnyz/cache/CacheCenter.java

@@ -32,6 +32,12 @@ public class CacheCenter {
      */
     public static Map<String, EquipmentParamFormVO> registerNowDataMap = new ConcurrentHashMap<>();
 
+    /**
+     * key: 设备code
+     * value: 设备id
+     */
+    public static Map<String, Long> deviceCodeToIdMap = new ConcurrentHashMap<>();
+
     private CacheCenter() {}// 防止实例化
 
 }

+ 19 - 7
admin/src/main/java/com/dcs/hnyz/cache/builder/DeviceToRegistersMapBuilder.java

@@ -1,6 +1,7 @@
 package com.dcs.hnyz.cache.builder;
 
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
+import com.dcs.common.utils.StringUtils;
 import com.dcs.hnyz.cache.CacheCenter;
 import com.dcs.hnyz.domain.Equipment;
 import com.dcs.hnyz.domain.EquipmentParam;
@@ -12,6 +13,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
 import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.stream.Collectors;
 
 /**
@@ -53,22 +55,32 @@ public class DeviceToRegistersMapBuilder {
             return Collections.emptyMap();
         }
 
+        Map<String, Long> codeToIdMap = equipmentList.stream()
+                .filter(e -> StringUtils.isNotBlank(e.getCode())) // 过滤掉 code 为空的设备
+                .collect(Collectors.toMap(
+                        Equipment::getCode,
+                        Equipment::getEquipmentId,
+                        (oldValue, newValue) -> newValue, // 如果有重复 code,用后者覆盖
+                        ConcurrentHashMap::new // 线程安全
+                ));
+        CacheCenter.deviceCodeToIdMap=codeToIdMap;
+
         // 2. 过滤掉 TANK 类型,收集设备ID
         List<Equipment> filteredEquipments = equipmentList.stream()
                 .filter(e -> !DeviceTypeEnum.TANK.getCode().equals(e.getEquipmentType()))
                 .collect(Collectors.toList());
 
         //设备id集合
-        List<Long> equipmentIds = filteredEquipments.stream()
-                .map(Equipment::getEquipmentId)
+        List<String> equipmentCodes = filteredEquipments.stream()
+                .map(Equipment::getCode)
                 .collect(Collectors.toList());
 
         // 3. 查出这些设备的所有参数
-        List<EquipmentParam> allParams = equipmentParamService.getAllEquipmentParamByRelationIds(equipmentIds);
+        List<EquipmentParam> allParams = equipmentParamService.getAllEquipmentParamByRelationCodes(equipmentCodes);
 
-        // 4. 按 relationId 分组参数
-        Map<Long, List<EquipmentParam>> paramMap = allParams.stream()
-                .collect(Collectors.groupingBy(EquipmentParam::getRelationId));
+        // 4. 按 relationCode 分组参数
+        Map<String, List<EquipmentParam>> paramMap = allParams.stream()
+                .collect(Collectors.groupingBy(EquipmentParam::getRelationCode));
 
         // 5. 构建设备code → 参数codes映射
         Map<String, List<String>> deviceCodeToRegisterCodesMap = new HashMap<>();
@@ -101,7 +113,7 @@ public class DeviceToRegistersMapBuilder {
             }
 
             // 取出该设备的参数,并按 paramTypes 顺序排序
-            List<EquipmentParam> params = paramMap.getOrDefault(equipment.getEquipmentId(), Collections.emptyList());
+            List<EquipmentParam> params = paramMap.getOrDefault(equipment.getCode(), Collections.emptyList());
             List<String> codes = params.stream()
                     .filter(p -> paramTypes.contains(p.getParamType()))
                     .sorted(Comparator.comparingInt(p -> paramTypes.indexOf(p.getParamType())))

+ 4 - 4
admin/src/main/java/com/dcs/hnyz/cache/builder/RegisterCodeMapBuilder.java

@@ -40,15 +40,15 @@ public class RegisterCodeMapBuilder {
         List<Equipment> equipmentList = equipmentService.selectEquipmentList(null);
 
         // 只取非 TANK 类型设备 ID
-        List<Long> validEquipmentIds = equipmentList.stream()
+        List<String> validEquipmentCodes = equipmentList.stream()
                 .filter(e -> !DeviceTypeEnum.TANK.getCode().equals(e.getEquipmentType()))
-                .map(Equipment::getEquipmentId)
+                .map(Equipment::getCode)
                 .collect(Collectors.toList());
 
-        if (validEquipmentIds.isEmpty()) {
+        if (validEquipmentCodes.isEmpty()) {
             registerCodeMap = Collections.emptyMap();
         }else {//  批量查询所有设备id 对应的 code
-            registerCodeMap = equipmentParamService.getCodeMapByParentIds(validEquipmentIds);
+            registerCodeMap = equipmentParamService.getCodeMapByParentCodes(validEquipmentCodes);
         }
         return registerCodeMap;
     }

+ 3 - 1
admin/src/main/java/com/dcs/hnyz/domain/AlarmEvent.java

@@ -1,5 +1,6 @@
 package com.dcs.hnyz.domain;
 
+import com.dcs.hnyz.cache.CacheCenter;
 import com.dcs.hnyz.domain.vo.EquipmentParamFormVO;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
@@ -115,7 +116,8 @@ public class AlarmEvent extends BaseEntity {
 //    根据告警配置和valueStr生成告警事件
     public static AlarmEvent from(EquipmentParamFormVO param, AlarmConfig config) {
         AlarmEvent event = new AlarmEvent();
-        event.setEquipmentId(config.getEquipmentId() != null ? config.getEquipmentId() : param.getRelationId());
+        long equipmentId = CacheCenter.deviceCodeToIdMap.get(param.getRegisterCode());
+        event.setEquipmentId(config.getEquipmentId() != null ? config.getEquipmentId() : equipmentId);
         event.setAlarmCode(config.getAlarmCode());
         event.setAlarmLevel(config.getAlarmLevel());
         event.setAlarmType(config.getAlarmType());

+ 6 - 6
admin/src/main/java/com/dcs/hnyz/domain/EquipmentParam.java

@@ -31,16 +31,16 @@ public class EquipmentParam extends BaseEntity {
     /**
      * 归属设备id(最顶级父设备id)
      */
-    @Excel(name = "归属设备id(最顶级父设备id)")
-    @ApiModelProperty(value = "归属设备id(最顶级父设备id)")
-    private Long equipmentId;
+    @Excel(name = "归属设备code(最顶级父设备code)")
+    @ApiModelProperty(value = "归属设备code(最顶级父设备code)")
+    private String equipmentCode;
 
     /**
      * 关联id(上一级父设备id)
      */
-    @Excel(name = "关联id(上一级父设备id)")
-    @ApiModelProperty(value = "关联id(上一级父设备id)")
-    private Long relationId;
+    @Excel(name = "关联设备code(上一级父设备code)")
+    @ApiModelProperty(value = "关联设备code(上一级父设备code)")
+    private String relationCode;
 
     /**
      * 从机id

+ 1 - 1
admin/src/main/java/com/dcs/hnyz/mapper/EquipmentMapper.java

@@ -31,5 +31,5 @@ public interface EquipmentMapper extends BaseMapper<Equipment>
 
     int updateEquipmentByPlcId(Long plcId);
 
-    List<Equipment> getEquipmentsByParentId(Long equipmentId);
+    List<Equipment> getEquipmentsByParentCode(String code);
 }

+ 5 - 5
admin/src/main/java/com/dcs/hnyz/service/IEquipmentParamService.java

@@ -102,10 +102,10 @@ public interface IEquipmentParamService
 
     /**
      * 批量查询所有设备对应的 code
-     * @param validEquipmentIds
+     * @param validEquipmentCodes
      * @return
      */
-    Map<Long, String> getCodeMapByParentIds(List<Long> validEquipmentIds);
+    Map<Long, String> getCodeMapByParentCodes(List<String> validEquipmentCodes);
 
     List<EquipmentParam> selectEquipmentParamListByProtocolId(Integer protocolId);
 
@@ -149,9 +149,9 @@ public interface IEquipmentParamService
     List<EquipmentParam> getAllParamConfigByCode(String code);
 
     /**
-     * 根据设备ids获取对应所有寄存器code
-     * @param equipmentIds
+     * 根据设备codes获取对应所有寄存器code
+     * @param codes
      * @return
      */
-    List<EquipmentParam> getAllEquipmentParamByRelationIds(List<Long> equipmentIds);
+    List<EquipmentParam> getAllEquipmentParamByRelationCodes(List<String> codes);
 }

+ 2 - 0
admin/src/main/java/com/dcs/hnyz/service/IEquipmentService.java

@@ -137,4 +137,6 @@ public interface IEquipmentService
     List<Equipment> selectBatchIds(List<Long> equipmentIds);
 
     List<EquipmentFormVO> selectEquipmentExportList(Equipment equipment);
+
+    List<Equipment> selectByCodes(List<String> validEquipmentCodes);
 }

+ 33 - 32
admin/src/main/java/com/dcs/hnyz/service/impl/EquipmentParamServiceImpl.java

@@ -10,6 +10,7 @@ import com.dcs.common.utils.StringUtils;
 import com.dcs.common.utils.StringUtils;
 import com.dcs.common.utils.bean.BeanUtils;
 import com.dcs.equipment.task.ModbusTcpTask;
+import com.dcs.hnyz.cache.CacheCenter;
 import com.dcs.hnyz.domain.Equipment;
 import com.dcs.hnyz.domain.EquipmentParam;
 import com.dcs.hnyz.domain.vo.EquipmentParamFormVO;
@@ -156,7 +157,7 @@ public class EquipmentParamServiceImpl implements IEquipmentParamService {
         }
         DeviceTypeEnum type = DeviceTypeEnum.fromCode(e.getEquipmentType());
         LambdaQueryWrapper<EquipmentParam> wrapper = new LambdaQueryWrapper<>();
-        wrapper.eq(EquipmentParam::getRelationId, parentId);
+        wrapper.eq(EquipmentParam::getRelationCode, CacheCenter.deviceCodeToIdMap.get(parentId));
         switch (type) {
             case SENSOR:
             case VALVE_WITH_FEEDBACK:
@@ -196,7 +197,7 @@ public class EquipmentParamServiceImpl implements IEquipmentParamService {
         }
         DeviceTypeEnum type = DeviceTypeEnum.fromCode(e.getEquipmentType());
         LambdaQueryWrapper<EquipmentParam> wrapper = new LambdaQueryWrapper<>();
-        wrapper.eq(EquipmentParam::getRelationId, parentId);
+        wrapper.eq(EquipmentParam::getRelationCode, CacheCenter.deviceCodeToIdMap.get(parentId));
         switch (type) {
             case SENSOR:
             case VALVE_WITH_FEEDBACK:
@@ -240,7 +241,7 @@ public class EquipmentParamServiceImpl implements IEquipmentParamService {
 
         // 初始化查询 wrapper
         LambdaQueryWrapper<EquipmentParam> wrapper = new LambdaQueryWrapper<>();
-        wrapper.eq(EquipmentParam::getRelationId, parentId);
+        wrapper.eq(EquipmentParam::getRelationCode, CacheCenter.deviceCodeToIdMap.get(parentId));
 
         List<String> paramTypes = new ArrayList<>();
 
@@ -309,13 +310,13 @@ public class EquipmentParamServiceImpl implements IEquipmentParamService {
     }
 
     @Override
-    public Map<Long, String> getCodeMapByParentIds(List<Long> validEquipmentIds) {
-        if (CollectionUtils.isEmpty(validEquipmentIds)) {
+    public Map<Long, String> getCodeMapByParentCodes(List<String> validEquipmentCodes) {
+        if (CollectionUtils.isEmpty(validEquipmentCodes)) {
             return Collections.emptyMap();
         }
 
         // 1. 批量查询设备信息
-        List<Equipment> equipments = equipmentService.selectBatchIds(validEquipmentIds);
+        List<Equipment> equipments = equipmentService.selectByCodes(validEquipmentCodes);
         if (CollectionUtils.isEmpty(equipments)) {
             return Collections.emptyMap();
         }
@@ -323,20 +324,20 @@ public class EquipmentParamServiceImpl implements IEquipmentParamService {
         // 2. 批量查询设备参数
         List<EquipmentParam> params = equipmentParamMapper.selectList(
                 new LambdaQueryWrapper<EquipmentParam>()
-                        .in(EquipmentParam::getRelationId, validEquipmentIds)
+                        .in(EquipmentParam::getRelationCode, validEquipmentCodes)
         );
         if (CollectionUtils.isEmpty(params)) {
             return Collections.emptyMap();
         }
-        // relationId → 该设备的所有参数
-        Map<Long, List<EquipmentParam>> paramMap = params.stream()
-                .collect(Collectors.groupingBy(EquipmentParam::getRelationId));
+        // relationCode → 该设备的所有参数
+        Map<String, List<EquipmentParam>> paramMap = params.stream()
+                .collect(Collectors.groupingBy(EquipmentParam::getRelationCode));
 
         // 3. 按设备类型过滤参数,挑选对应的 code
         Map<Long, String> result = new HashMap<>();
         for (Equipment eq : equipments) {
             DeviceTypeEnum type = DeviceTypeEnum.fromCode(eq.getEquipmentType());
-            List<EquipmentParam> eps = paramMap.get(eq.getEquipmentId());
+            List<EquipmentParam> eps = paramMap.get(eq.getCode());
             if (CollectionUtils.isEmpty(eps)) {
                 continue;
             }
@@ -379,7 +380,7 @@ public class EquipmentParamServiceImpl implements IEquipmentParamService {
     public EquipmentParam getMainSetParam(String code) {
         Equipment e = equipmentService.getEquipmentByCode(code);
         LambdaQueryWrapper<EquipmentParam> wrapper = new LambdaQueryWrapper<>();
-        wrapper.eq(EquipmentParam::getRelationId, e.getEquipmentId());
+        wrapper.eq(EquipmentParam::getRelationCode, code);
         wrapper.eq(EquipmentParam::getParamType, RegisterTypeEnum.MASTER_SET.getCode());
         EquipmentParam ep = equipmentParamMapper.selectOne(wrapper);
 
@@ -396,7 +397,7 @@ public class EquipmentParamServiceImpl implements IEquipmentParamService {
             throw new CustomException("未找到设备:" + code);
         }
         LambdaQueryWrapper<EquipmentParam> wrapper = new LambdaQueryWrapper<>();
-        wrapper.eq(EquipmentParam::getRelationId, e.getEquipmentId());
+        wrapper.eq(EquipmentParam::getRelationCode, code);
         wrapper.in(EquipmentParam::getParamType, RegisterTypeEnum.MASTER_SET.getCode(), RegisterTypeEnum.SLAVE_SET.getCode());
         wrapper.eq(EquipmentParam::getStatus, GeneralStatus.ENABLE.getCode());
         //按ParamType升序(主设置,从设置)
@@ -467,7 +468,7 @@ public class EquipmentParamServiceImpl implements IEquipmentParamService {
 
         String eqName = equipment.getEquipmentName();
         LambdaQueryWrapper<EquipmentParam> wrapper = new LambdaQueryWrapper<>();
-        wrapper.eq(EquipmentParam::getRelationId, equipment.getEquipmentId())
+        wrapper.eq(EquipmentParam::getRelationCode, equipment.getCode())
                 .eq(EquipmentParam::getStatus, GeneralStatus.ENABLE.getCode());
 
         List<EquipmentParam> epList = equipmentParamMapper.selectList(wrapper);
@@ -534,7 +535,7 @@ public class EquipmentParamServiceImpl implements IEquipmentParamService {
 
     public Map<String, List<Object>> getAllParamConfigDataByCodeList(List<String> codeList) {
         // 1. 获取设备code与id的映射(原逻辑保留)
-        Map<String, Long> equipmentIdCodeMap = equipmentService.getEquipmentIdCodeMap();
+        Map<String, Long> equipmentIdCodeMap = CacheCenter.deviceCodeToIdMap;
         if (equipmentIdCodeMap.isEmpty()) {
             return Collections.emptyMap();
         }
@@ -549,18 +550,18 @@ public class EquipmentParamServiceImpl implements IEquipmentParamService {
                 ));
 
         // 3. 收集有效的equipmentId(过滤不存在的code,去重)
-        Set<Long> validEquipmentIds = codeList.stream()
-                .filter(equipmentIdCodeMap::containsKey)  // 只保留存在的code
-                .map(equipmentIdCodeMap::get)             // 转为equipmentId
-                .collect(Collectors.toSet());             // 去重,避免重复查询
-
-        if (validEquipmentIds.isEmpty()) {
-            return Collections.emptyMap();
-        }
+//        Set<Long> validEquipmentIds = codeList.stream()
+//                .filter(equipmentIdCodeMap::containsKey)  // 只保留存在的code
+//                .map(equipmentIdCodeMap::get)             // 转为equipmentId
+//                .collect(Collectors.toSet());             // 去重,避免重复查询
+//
+//        if (validEquipmentIds.isEmpty()) {
+//            return Collections.emptyMap();
+//        }
 
         // 4. 批量查询所有有效的EquipmentParam(一次数据库查询)
         LambdaQueryWrapper<EquipmentParam> wrapper = new LambdaQueryWrapper<>();
-        wrapper.in(EquipmentParam::getRelationId, validEquipmentIds)  // 批量匹配equipmentId
+        wrapper.in(EquipmentParam::getRelationCode, codeList)  // 批量匹配codeList
                 .eq(EquipmentParam::getStatus, GeneralStatus.ENABLE.getCode())
                 .in(EquipmentParam::getParamType,
                         RegisterTypeEnum.PARAM_CONFIG1.getCode(),
@@ -573,9 +574,9 @@ public class EquipmentParamServiceImpl implements IEquipmentParamService {
 
         List<EquipmentParam> allEpList = equipmentParamMapper.selectList(wrapper);
 
-        // 5. 按relationId(即equipmentId)分组,便于后续快速获取
-        Map<Long, List<EquipmentParam>> epGroupByEquipmentId = allEpList.stream()
-                .collect(Collectors.groupingBy(EquipmentParam::getRelationId));
+        // 5. 按relationCode分组,便于后续快速获取
+        Map<String, List<EquipmentParam>> epGroupByEquipmentCode = allEpList.stream()
+                .collect(Collectors.groupingBy(EquipmentParam::getRelationCode));
 
         // 6. 构建结果(遍历codeList,填充数据)
         Map<String, List<Object>> result = new HashMap<>(codeList.size());  // 预设容量
@@ -587,8 +588,8 @@ public class EquipmentParamServiceImpl implements IEquipmentParamService {
                 continue;
             }
 
-            // 从分组中获取当前equipmentId对应的参数列表
-            List<EquipmentParam> epList = epGroupByEquipmentId.getOrDefault(equipmentId, Collections.emptyList());
+            // 从分组中获取当前equipmentCode对应的参数列表
+            List<EquipmentParam> epList = epGroupByEquipmentCode.getOrDefault(code, Collections.emptyList());
             for (EquipmentParam ep : epList) {
                 // 从预构建的map中快速获取VO,避免遍历
                 EquipmentParamFormVO vo = broadCastCodeMap.get(ep.getCode());
@@ -607,7 +608,7 @@ public class EquipmentParamServiceImpl implements IEquipmentParamService {
             throw new CustomException("未找到设备:" + code);
         }
         LambdaQueryWrapper<EquipmentParam> wrapper = new LambdaQueryWrapper<>();
-        wrapper.eq(EquipmentParam::getRelationId, e.getEquipmentId());
+        wrapper.eq(EquipmentParam::getRelationCode, code);
         wrapper.eq(EquipmentParam::getStatus, GeneralStatus.ENABLE.getCode());
         wrapper.in(EquipmentParam::getParamType, RegisterTypeEnum.PARAM_CONFIG1.getCode(), RegisterTypeEnum.PARAM_CONFIG2.getCode(), RegisterTypeEnum.PARAM_CONFIG3.getCode(), RegisterTypeEnum.PARAM_CONFIG4.getCode(), RegisterTypeEnum.PARAM_CONFIG5.getCode());
         wrapper.orderByAsc(EquipmentParam::getParamType);//按ParamType升序(参数配置1,2,3,4,5)
@@ -621,9 +622,9 @@ public class EquipmentParamServiceImpl implements IEquipmentParamService {
     }
 
     @Override
-    public List<EquipmentParam> getAllEquipmentParamByRelationIds(List<Long> equipmentIds) {
+    public List<EquipmentParam> getAllEquipmentParamByRelationCodes(List<String> codes) {
         return equipmentParamMapper.selectList(new LambdaQueryWrapper<EquipmentParam>()
-                .in(EquipmentParam::getRelationId, equipmentIds));
+                .in(EquipmentParam::getRelationCode, codes));
     }
 
 }

+ 13 - 4
admin/src/main/java/com/dcs/hnyz/service/impl/EquipmentServiceImpl.java

@@ -8,6 +8,7 @@ import com.dcs.common.enums.GeneralStatus;
 import com.dcs.common.exception.CustomException;
 import com.dcs.common.exception.ServiceException;
 import com.dcs.common.utils.StringUtils;
+import com.dcs.hnyz.cache.CacheCenter;
 import com.dcs.hnyz.domain.*;
 import com.dcs.hnyz.domain.vo.EquipmentFormVO;
 import com.dcs.hnyz.enums.DeviceTypeEnum;
@@ -125,6 +126,14 @@ public class EquipmentServiceImpl implements IEquipmentService {
         return collect;
     }
 
+    @Override
+    public List<Equipment> selectByCodes(List<String> codes) {
+        return equipmentMapper.selectList(
+                new LambdaQueryWrapper<Equipment>()
+                        .in(Equipment::getCode, codes)
+        );
+    }
+
     @Override
     public List<EquipmentFormVO> selectEquipmentForm(Equipment equipment) {
         List<EquipmentFormVO> list = equipmentMapper.selectEquipmentForm(equipment);
@@ -368,14 +377,14 @@ public class EquipmentServiceImpl implements IEquipmentService {
 
         // 2. 创建每个寄存器节点
         List<Map<String, Object>> leafNodes = new ArrayList<>();
+        Map<String,Long> deviceCodeToIdMap = CacheCenter.deviceCodeToIdMap;
         for (EquipmentParam param : paramList) {
             Map<String, Object> leaf = new LinkedHashMap<>();
             leaf.put("label", param.getRegisterName());
             leaf.put("value", param.getCode());
 
-            Long equipmentId = param.getEquipmentId(); // 顶层设备
-            Long relationId = param.getRelationId();   // 上级设备
-
+            Long equipmentId = deviceCodeToIdMap.get(param.getEquipmentCode()); // 顶层设备
+            Long relationId = deviceCodeToIdMap.get(param.getRelationCode());   // 上级设备
             // 3. 构建一条从寄存器向上的链条
             Map<String, Object> child = leaf;
             if (relationId != null && !relationId.equals(equipmentId)) {
@@ -571,7 +580,7 @@ public class EquipmentServiceImpl implements IEquipmentService {
         //按罐体进行分类
         for (Equipment tank : tankList) {
             // 查找该罐体关联的设备列表
-            List<Equipment> relatedEquipments = equipmentMapper.getEquipmentsByParentId(tank.getEquipmentId());
+            List<Equipment> relatedEquipments = equipmentMapper.getEquipmentsByParentCode(tank.getCode());
             // 以罐体的code作为key,关联设备列表作为value
             map.put(tank.getTitle()+'-'+tank.getEquipmentName(), relatedEquipments);
         }

+ 3 - 3
admin/src/main/resources/mapper/hnyzdcs/EquipmentMapper.xml

@@ -65,10 +65,10 @@
             <if test="delFlag == null">and del_flag = '0'</if>
         </where>
     </select>
-    <select id="getEquipmentsByParentId" resultType="com.dcs.hnyz.domain.Equipment">
+    <select id="getEquipmentsByParentCode" resultType="com.dcs.hnyz.domain.Equipment">
         <include refid="selectEquipmentVo"/>
-        WHERE equipment_id IN (
-        SELECT relation_id FROM equipment_param WHERE equipment_id = #{equipmentId}
+        WHERE code IN (
+        SELECT relation_code FROM equipment_param WHERE equipment_code = #{code}
         )
     </select>
 

+ 22 - 22
admin/src/main/resources/mapper/hnyzdcs/EquipmentParamMapper.xml

@@ -6,8 +6,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
     
     <resultMap type="EquipmentParam" id="EquipmentParamResult">
         <result property="registerId"    column="register_id"    />
-        <result property="equipmentId"    column="equipment_id"    />
-        <result property="relationId"    column="relation_id"    />
+        <result property="equipmentCode"    column="equipment_code"    />
+        <result property="relationCode"    column="relation_code"    />
         <result property="unitId"    column="unit_id"    />
         <result property="address"    column="address"    />
         <result property="registerName"    column="register_name"    />
@@ -20,8 +20,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 
     <resultMap type="EquipmentParam" id="EquipmentParamResultWithProtocol">
         <result property="registerId"    column="register_id"    />
-        <result property="equipmentId"    column="equipment_id"    />
-        <result property="relationId"    column="relation_id"    />
+        <result property="equipmentCode"    column="equipment_code"    />
+        <result property="relationCode"    column="relation_code"    />
         <result property="unitId"    column="unit_id"    />
         <result property="address"    column="address"    />
         <result property="registerName"    column="register_name"    />
@@ -41,14 +41,14 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
     </resultMap>
 
     <sql id="selectEquipmentParamVo">
-        select register_id, unit_id, equipment_id, relation_id, address, register_name, data_type, code, register_code, unit_type, param_type from equipment_param
+        select register_id, unit_id, equipment_code, relation_code, address, register_name, data_type, code, register_code, unit_type, param_type from equipment_param
     </sql>
 
     <sql id="selectEquipmentParamWithProtocolVo">
         select ep.register_id,
                ep.unit_id,
-               ep.equipment_id,
-               ep.relation_id,
+               ep.equipment_code,
+               ep.relation_code,
                ep.address,
                ep.register_name,
                ep.data_type,
@@ -61,14 +61,14 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
                e.protocol_id
             FROM
             equipment_param ep
-        LEFT JOIN equipment e ON ep.relation_id = e.equipment_id
+        LEFT JOIN equipment e ON ep.relation_code = e.code
     </sql>
 
     <select id="selectEquipmentParamList" parameterType="EquipmentParam" resultMap="EquipmentParamResult">
         <include refid="selectEquipmentParamVo"/>
         <where>  
-            <if test="equipmentId != null "> and equipment_id = #{equipmentId}</if>
-            <if test="relationId != null "> and relation_id = #{relationId}</if>
+            <if test="equipmentCode != null "> and equipment_code = #{equipmentCode}</if>
+            <if test="relationCode != null "> and relation_code = #{relationCode}</if>
             <if test="address != null "> and address = #{address}</if>
             <if test="registerName != null  and registerName != ''"> and register_name like concat('%', #{registerName}, '%')</if>
             <if test="dataType != null  and dataType != ''"> and data_type = #{dataType}</if>
@@ -93,17 +93,17 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         c.equipment_name AS equipment_name
         FROM
         equipment_param AS a
-        INNER JOIN
+        LEFT JOIN
         equipment AS b
         ON
-        a.relation_id = b.equipment_id
-        INNER JOIN
+        a.relation_code = b.code
+        LEFT JOIN
         equipment AS c
         ON
-        a.equipment_id = c.equipment_id
+        a.equipment_code = c.code
         <where>
-            <if test="equipmentId != null "> and equipment_id = #{equipmentId}</if>
-            <if test="relationId != null "> and relation_id = #{relationId}</if>
+            <if test="equipmentCode != null and equipmentCode != ''"> and equipment_code = #{equipmentCode}</if>
+            <if test="relationCode != null and relationCode != ''"> and relation_code = #{relationCode}</if>
             <if test="address != null "> and address = #{address}</if>
             <if test="registerName != null  and registerName != ''"> and register_name like concat('%', #{registerName}, '%')</if>
             <if test="dataType != null  and dataType != ''"> and data_type = #{dataType}</if>
@@ -128,8 +128,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
     <insert id="insertEquipmentParam" parameterType="EquipmentParam" useGeneratedKeys="true" keyProperty="registerId">
         insert into equipment_param
         <trim prefix="(" suffix=")" suffixOverrides=",">
-            <if test="equipmentId != null">equipment_id,</if>
-            <if test="relationId != null">relation_id,</if>
+            <if test="equipmentCode != null">equipment_code,</if>
+            <if test="relationCode != null">relation_code,</if>
             <if test="address != null">address,</if>
             <if test="registerName != null and registerName != ''">register_name,</if>
             <if test="dataType != null and dataType != ''">data_type,</if>
@@ -139,8 +139,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             <if test="paramType != null">param_type,</if>
          </trim>
         <trim prefix="values (" suffix=")" suffixOverrides=",">
-            <if test="equipmentId != null">#{equipmentId},</if>
-            <if test="relationId != null">#{relationId},</if>
+            <if test="equipmentCode != null">#{equipmentCode},</if>
+            <if test="relationCode != null">#{relationCode},</if>
             <if test="address != null">#{address},</if>
             <if test="registerName != null and registerName != ''">#{registerName},</if>
             <if test="dataType != null and dataType != ''">#{dataType},</if>
@@ -154,8 +154,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
     <update id="updateEquipmentParam" parameterType="EquipmentParam">
         update equipment_param
         <trim prefix="SET" suffixOverrides=",">
-            <if test="equipmentId != null">equipment_id = #{equipmentId},</if>
-            <if test="relationId != null">relation_id = #{relationId},</if>
+            <if test="equipmentCode != null">equipment_code = #{equipmentCode},</if>
+            <if test="relationCode != null">relation_code = #{relationCode},</if>
             <if test="address != null">address = #{address},</if>
             <if test="registerName != null and registerName != ''">register_name = #{registerName},</if>
             <if test="dataType != null and dataType != ''">data_type = #{dataType},</if>