Просмотр исходного кода

feat(equipmentParam):参数配置页相关数据处理接口

HMY 8 месяцев назад
Родитель
Сommit
6d4f34f5f6

+ 2 - 1
admin/src/main/java/com/dcs/equipment/task/ModbusTcpTask.java

@@ -82,12 +82,13 @@ public class ModbusTcpTask {
             if (registerCode == null) continue; // 跳过没有功能码的设备
             for (DeviceResponse deviceResponse : deviceResponseMap.get(registerCode)) {
                 // 跳过不匹配的数据类型
+                if (!equipmentVo.getUnitId().equals(deviceResponse.getUnitId())) continue;
                 if (!equipmentVo.getDataType().equals(deviceResponse.getDataType())) continue;
                 int startAddress = deviceResponse.getOffsetOrIndex();
                 int quantity = deviceResponse.getLengthOrQos();
                 int dataLength = DATA_ADDRESS_LENGTH_MAP.get(deviceResponse.getDataType());
                 // 跳过不在当前地址范围内的响应数据
-                if (address < startAddress || address > startAddress + (quantity * dataLength)) continue;
+                if (address < startAddress || address >= startAddress + (quantity * dataLength)) continue;
                 int index = (address - startAddress) / dataLength;
 //                List<Object> data = modbusResponse.getData();
                 Object o = deviceResponse.getData().get(index);

+ 21 - 0
admin/src/main/java/com/dcs/hnyz/controller/EquipmentParamController.java

@@ -150,4 +150,25 @@ public class EquipmentParamController extends BaseController
     public AjaxResult getAllSetParam(@PathVariable String code) {
         return success(equipmentParamService.getAllSetParam(code));
     }
+
+    /**
+     * 根据设备标识List获取对应所有寄存器的参数配置项数据(用于参数配置页数据初始化)
+     */
+    @PostMapping("/getAllParamConfigDataByCodeList")
+    @ApiOperation(value = "根据设备codeList获取对应所有寄存器的参数配置项数据")
+    public AjaxResult getAllParamConfigDataByCodeList(@RequestBody List<String> codeList) {
+        return success(equipmentParamService.getAllParamConfigDataByCodeList(codeList));
+    }
+
+    /**
+     * 根据设备标识获取对应所有参数配置项寄存器
+     * @param code
+     * @return
+     */
+    @GetMapping("/getAllParamConfigByCode/{code}")
+    @ApiOperation(value = "根据设备code获取对应所有设置项寄存器数据")
+    public AjaxResult getAllParamConfigByCode(@PathVariable String code) {
+        return success(equipmentParamService.getAllParamConfigByCode(code));
+    }
+
 }

+ 3 - 0
admin/src/main/java/com/dcs/hnyz/domain/Equipment.java

@@ -94,4 +94,7 @@ public class Equipment extends BaseEntity
     @ApiModelProperty(value = "删除标志 0-存在 2-删除")
     private String delFlag;
 
+    /** 罐体排序,仅用于流程页面按罐子顺序排 */
+    private Integer tankSort;
+
 }

+ 8 - 1
admin/src/main/java/com/dcs/hnyz/enums/RegisterTypeEnum.java

@@ -14,7 +14,14 @@ public enum RegisterTypeEnum {
     SLAVE_DATA("2", "从数据项"),
     MASTER_SET("3", "主设置项"),
     SLAVE_SET("4", "从设置项"),
-    OTHER("5", "其他");
+    OTHER("5", "其他"),
+    SLAVE_SET2("6", "从设置项2"),
+    
+    PARAM_CONFIG1("11", "参数配置项1"),
+    PARAM_CONFIG2("12", "参数配置项2"),
+    PARAM_CONFIG3("13", "参数配置项3"),
+    PARAM_CONFIG4("14", "参数配置项4"),
+    PARAM_CONFIG5("15", "参数配置项5");
 
     private final String code;
     private final String label;

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

@@ -1,5 +1,6 @@
 package com.dcs.hnyz.service;
 
+import com.dcs.hnyz.domain.Equipment;
 import com.dcs.hnyz.domain.EquipmentParam;
 import com.dcs.hnyz.domain.vo.EquipmentParamFormVO;
 
@@ -123,4 +124,25 @@ public interface IEquipmentParamService
      * @return
      */
     List<EquipmentParam> getAllSetParam(String code);
+
+    /**
+     * 根据设备名的变化 更新寄存器名称
+     * @param equipment
+     * @return
+     */
+    int updateRegisterNameByEquipment(Equipment equipment);
+
+    /**
+     * 根据设备的codeList获取寄存器对应的所有参数配置项参数的数据
+     * @param codeList
+     * @return
+     */
+    Map<String,List<Object>> getAllParamConfigDataByCodeList(List<String> codeList);
+
+    /**
+     * 根据设备的code获取寄存器对应的所有参数配置项
+     * @param code
+     * @return
+     */
+    List<EquipmentParam> getAllParamConfigByCode(String code);
 }

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

@@ -123,4 +123,9 @@ public interface IEquipmentService
      * @return
      */
     Map<String,List<Equipment>> getPageEquipmentGroupByTankByFlowCode(String flowCode);
+
+    /**
+     * 获取设备code->设备id的映射
+     */
+    Map<String, Long> getEquipmentIdCodeMap();
 }

+ 169 - 2
admin/src/main/java/com/dcs/hnyz/service/impl/EquipmentParamServiceImpl.java

@@ -4,6 +4,8 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.dcs.common.enums.GeneralStatus;
 import com.dcs.common.exception.CustomException;
+import com.dcs.common.utils.StringUtils;
+import com.dcs.common.utils.bean.BeanUtils;
 import com.dcs.equipment.task.ModbusTcpTask;
 import com.dcs.hnyz.domain.Equipment;
 import com.dcs.hnyz.domain.EquipmentParam;
@@ -19,6 +21,8 @@ import javax.annotation.Resource;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.util.*;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 import java.util.stream.Collectors;
 
 import static com.dcs.framework.datasource.DynamicDataSourceContextHolder.log;
@@ -332,7 +336,7 @@ public class EquipmentParamServiceImpl implements IEquipmentParamService {
         LambdaQueryWrapper<EquipmentParam> wrapper = new LambdaQueryWrapper<>();
         wrapper.eq(EquipmentParam::getRelationId, e.getEquipmentId());
         wrapper.eq(EquipmentParam::getParamType, RegisterTypeEnum.MASTER_SET.getCode());
-        EquipmentParam ep=equipmentParamMapper.selectOne(wrapper);
+        EquipmentParam ep = equipmentParamMapper.selectOne(wrapper);
 
         ep.setIpAddress(e.getIpAddress());
         ep.setPort(e.getPort());
@@ -352,7 +356,7 @@ public class EquipmentParamServiceImpl implements IEquipmentParamService {
         wrapper.eq(EquipmentParam::getStatus, GeneralStatus.ENABLE.getCode());
         //按ParamType升序(主设置,从设置)
         wrapper.orderByAsc(EquipmentParam::getParamType);
-        List<EquipmentParam> epList=equipmentParamMapper.selectList(wrapper);
+        List<EquipmentParam> epList = equipmentParamMapper.selectList(wrapper);
         //填充通信参数
         for (EquipmentParam ep : epList) {
             ep.setIpAddress(e.getIpAddress());
@@ -362,4 +366,167 @@ public class EquipmentParamServiceImpl implements IEquipmentParamService {
         return epList;
     }
 
+    @Override
+    public int updateRegisterNameByEquipment(Equipment equipment) {
+        if (equipment == null || equipment.getEquipmentId() == null ||
+                StringUtils.isBlank(equipment.getEquipmentName())) {
+            // 校验入参,避免空指针或无效数据
+            return 0;
+        }
+
+        String eqName = equipment.getEquipmentName();
+        LambdaQueryWrapper<EquipmentParam> wrapper = new LambdaQueryWrapper<>();
+        wrapper.eq(EquipmentParam::getRelationId, equipment.getEquipmentId())
+                .eq(EquipmentParam::getStatus, GeneralStatus.ENABLE.getCode());
+
+        List<EquipmentParam> epList = equipmentParamMapper.selectList(wrapper);
+        if (CollectionUtils.isEmpty(epList)) {
+            // 没有需要更新的参数,直接返回
+            return 0;
+        }
+
+        // 预编译正则表达式,避免循环中重复编译
+        Pattern pattern = Pattern.compile("[\\u4e00-\\u9fa5\\uF900-\\uFA2D]+$");
+        int updateCount = 0; // 记录成功更新的数量
+
+        for (EquipmentParam ep : epList) {
+            String registerName = ep.getRegisterName();
+            if (StringUtils.isBlank(registerName)) {
+                continue; // 跳过空注册名的参数
+            }
+
+            Matcher matcher = pattern.matcher(registerName);
+            String newRegisterName;
+
+            if (matcher.find()) {
+                // 保留原有的中文后缀,前面拼接设备名称
+                newRegisterName = eqName + matcher.group();
+            } else {
+                // 如果没有中文后缀,直接使用设备名称作为注册名
+                newRegisterName = eqName;
+            }
+
+            // 只有当注册名发生变化时才执行更新
+            if (!newRegisterName.equals(registerName)) {
+                ep.setRegisterName(newRegisterName);
+                updateCount += equipmentParamMapper.updateById(ep);
+            }
+        }
+
+        return updateCount; // 返回实际更新成功的数量
+    }
+
+    @Override
+//    public Map<String, List<Object>> getAllParamConfigDataByCodeList(List<String> codeList) {
+//        Map<String, Long> equipmentIdCodeMap = equipmentService.getEquipmentIdCodeMap();//设备code与id的映射
+//        List<EquipmentParamFormVO> broadCastEquipmentParamFormVOList = ModbusTcpTask.getBroadCastEquipmentParamFormVOList();
+//        Map<String, List<Object>> result = new HashMap<>();
+//        codeList.forEach(code -> {
+//            List<Object> dataList = new ArrayList<>();
+//            if (equipmentIdCodeMap.containsKey(code)) {
+//                long equipmentId = equipmentIdCodeMap.get(code);
+//                LambdaQueryWrapper<EquipmentParam> wrapper = new LambdaQueryWrapper<>();
+//                wrapper.eq(EquipmentParam::getRelationId, equipmentId);
+//                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)
+//                List<EquipmentParam> epList = equipmentParamMapper.selectList(wrapper);
+//                epList.forEach(equipmentParam -> {
+//                    //把对应的value加入dataList
+//                    dataList.add(broadCastEquipmentParamFormVOList.stream().filter(broadCastEquipmentParamFormVO -> broadCastEquipmentParamFormVO.getCode().equals(equipmentParam.getCode())).findFirst().orElse(null).getValue());
+//                });
+//            }
+//            result.put(code, dataList);
+//        });
+//        return result;
+//    }
+
+    public Map<String, List<Object>> getAllParamConfigDataByCodeList(List<String> codeList) {
+        // 1. 获取设备code与id的映射(原逻辑保留)
+        Map<String, Long> equipmentIdCodeMap = equipmentService.getEquipmentIdCodeMap();
+        if (equipmentIdCodeMap.isEmpty()) {
+            return Collections.emptyMap();
+        }
+
+        // 2. 预构建broadCastVO的code->VO映射(O(m)构建,后续查询O(1))
+        List<EquipmentParamFormVO> broadCastList = ModbusTcpTask.getBroadCastEquipmentParamFormVOList();
+        Map<String, EquipmentParamFormVO> broadCastCodeMap = broadCastList.stream()
+                .collect(Collectors.toMap(
+                        EquipmentParamFormVO::getCode,  // key为VO的code
+                        vo -> vo,                      // value为VO本身
+                        (existing, replacement) -> existing  // 处理code重复(保留第一个)
+                ));
+
+        // 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();
+        }
+
+        // 4. 批量查询所有有效的EquipmentParam(一次数据库查询)
+        LambdaQueryWrapper<EquipmentParam> wrapper = new LambdaQueryWrapper<>();
+        wrapper.in(EquipmentParam::getRelationId, validEquipmentIds)  // 批量匹配equipmentId
+                .eq(EquipmentParam::getStatus, GeneralStatus.ENABLE.getCode())
+                .in(EquipmentParam::getParamType,
+                        RegisterTypeEnum.PARAM_CONFIG1.getCode(),
+                        RegisterTypeEnum.PARAM_CONFIG2.getCode(),
+                        RegisterTypeEnum.PARAM_CONFIG3.getCode(),
+                        RegisterTypeEnum.PARAM_CONFIG4.getCode(),
+                        RegisterTypeEnum.PARAM_CONFIG5.getCode()
+                )
+                .orderByAsc(EquipmentParam::getParamType);
+
+        List<EquipmentParam> allEpList = equipmentParamMapper.selectList(wrapper);
+
+        // 5. 按relationId(即equipmentId)分组,便于后续快速获取
+        Map<Long, List<EquipmentParam>> epGroupByEquipmentId = allEpList.stream()
+                .collect(Collectors.groupingBy(EquipmentParam::getRelationId));
+
+        // 6. 构建结果(遍历codeList,填充数据)
+        Map<String, List<Object>> result = new HashMap<>(codeList.size());  // 预设容量
+        for (String code : codeList) {
+            List<Object> dataList = new ArrayList<>();
+            Long equipmentId = equipmentIdCodeMap.get(code);
+            if (equipmentId == null) {
+                result.put(code, dataList);  // 无效code,返回空列表
+                continue;
+            }
+
+            // 从分组中获取当前equipmentId对应的参数列表
+            List<EquipmentParam> epList = epGroupByEquipmentId.getOrDefault(equipmentId, Collections.emptyList());
+            for (EquipmentParam ep : epList) {
+                // 从预构建的map中快速获取VO,避免遍历
+                EquipmentParamFormVO vo = broadCastCodeMap.get(ep.getCode());
+                dataList.add(vo != null ? vo.getValue() : null);  // 避免空指针
+            }
+            result.put(code, dataList);
+        }
+
+        return result;
+    }
+
+    @Override
+    public List<EquipmentParam> getAllParamConfigByCode(String code) {
+        Equipment e = equipmentService.getEquipmentByCode(code);
+        if (e == null) {
+            throw new CustomException("未找到设备:" + code);
+        }
+        LambdaQueryWrapper<EquipmentParam> wrapper = new LambdaQueryWrapper<>();
+        wrapper.eq(EquipmentParam::getRelationId, e.getEquipmentId());
+        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)
+        List<EquipmentParam> epList = equipmentParamMapper.selectList(wrapper);
+        epList.forEach(ep -> {
+            ep.setIpAddress(e.getIpAddress());
+            ep.setPort(e.getPort());
+            ep.setProtocolId(e.getProtocolId());
+        });
+        return epList;
+    }
+
 }

+ 15 - 2
admin/src/main/java/com/dcs/hnyz/service/impl/EquipmentServiceImpl.java

@@ -151,6 +151,10 @@ public class EquipmentServiceImpl implements IEquipmentService {
         if (d != null) {
             throw new CustomException("设备标识已存在");
         }
+        //如果是修改设备名,那要同步修改对应寄存器名称
+        if(!equipment.getEquipmentName().equals(selectEquipmentByEquipmentId(equipment.getEquipmentId()).getEquipmentName())){
+            equipmentParamService.updateRegisterNameByEquipment(equipment);
+        }
         return equipmentMapper.updateById(equipment);
     }
 
@@ -442,7 +446,7 @@ public class EquipmentServiceImpl implements IEquipmentService {
 
     @Override
     public Map<String, List<Equipment>> getPageEquipmentGroupByTankByFlowCode(String flowCode) {
-        Map<String, List<Equipment>> map = new HashMap<>();
+        Map<String, List<Equipment>> map = new LinkedHashMap<>();
         Flow flow = flowService.getFlowByCode(flowCode);
         if (flow == null) {
             throw new CustomException("该流程编码不存在");
@@ -450,7 +454,9 @@ public class EquipmentServiceImpl implements IEquipmentService {
         // 查询该流程下所有罐体设备(TANK)
         List<Equipment> tankList = equipmentMapper.selectList(new LambdaQueryWrapper<Equipment>()
                 .apply("FIND_IN_SET({0}, flow_ids)", flow.getFlowId())
-                .eq(Equipment::getEquipmentType, DeviceTypeEnum.TANK.getCode()));
+                .eq(Equipment::getEquipmentType, DeviceTypeEnum.TANK.getCode())
+                .eq(Equipment::getStatus, GeneralStatus.ENABLE.getCode())
+                .orderByAsc(Equipment::getTankSort));//按罐体排序
         //按罐体进行分类
         for (Equipment tank : tankList) {
             // 查找该罐体关联的设备列表
@@ -461,4 +467,11 @@ public class EquipmentServiceImpl implements IEquipmentService {
         return map;
     }
 
+    @Override
+    public Map<String , Long> getEquipmentIdCodeMap() {
+        List<Equipment> equipmentList = equipmentMapper.selectList(null);
+        Map<String , Long> map = equipmentList.stream().collect(Collectors.toMap(Equipment::getCode,Equipment::getEquipmentId));
+        return map;
+    }
+
 }