|
|
@@ -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;
|
|
|
+ }
|
|
|
+
|
|
|
}
|