diff --git a/ruoyi-modules/Property/src/main/java/org/dromara/property/service/impl/smartDevicesImpl/TbMeterInfoServiceImpl.java b/ruoyi-modules/Property/src/main/java/org/dromara/property/service/impl/smartDevicesImpl/TbMeterInfoServiceImpl.java index cefb768b..0ec6ff16 100644 --- a/ruoyi-modules/Property/src/main/java/org/dromara/property/service/impl/smartDevicesImpl/TbMeterInfoServiceImpl.java +++ b/ruoyi-modules/Property/src/main/java/org/dromara/property/service/impl/smartDevicesImpl/TbMeterInfoServiceImpl.java @@ -30,10 +30,8 @@ import org.dromara.property.service.smartDevicesService.ITbMeterInfoService; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Collection; +import java.math.RoundingMode; +import java.util.*; import java.util.stream.Collectors; /** @@ -109,16 +107,17 @@ public class TbMeterInfoServiceImpl implements ITbMeterInfoService { LambdaQueryWrapper lqw = Wrappers.lambdaQuery(); lqw.orderByAsc(TbMeterInfo::getId); lqw.like(StringUtils.isNotBlank(bo.getHostIp()), TbMeterInfo::getHostIp, bo.getHostIp()); - lqw.like(StringUtils.isNotBlank(bo.getMeterName()), TbMeterInfo::getMeterName, bo.getMeterName()); lqw.eq(StringUtils.isNotBlank(bo.getMeterCode()), TbMeterInfo::getMeterCode, bo.getMeterCode()); lqw.eq(StringUtils.isNotBlank(bo.getFactoryNo()), TbMeterInfo::getFactoryNo, bo.getFactoryNo()); + lqw.like(StringUtils.isNotBlank(bo.getMeterName()), TbMeterInfo::getMeterName, bo.getMeterName()); + lqw.eq(StringUtils.isNotBlank(bo.getInstallLocation()), TbMeterInfo::getInstallLocation, bo.getInstallLocation()); + lqw.eq(bo.getFloorId() != null, TbMeterInfo::getFloorId, bo.getFloorId()); + lqw.eq(bo.getMaxRang() != null, TbMeterInfo::getMaxRang, bo.getMaxRang()); lqw.eq(bo.getMeterType() != null, TbMeterInfo::getMeterType, bo.getMeterType()); lqw.eq(bo.getMeterUnit() != null, TbMeterInfo::getMeterUnit, bo.getMeterUnit()); - lqw.eq(StringUtils.isNotBlank(bo.getInstallLocation()), TbMeterInfo::getInstallLocation, bo.getInstallLocation()); lqw.eq(bo.getInitReading() != null, TbMeterInfo::getInitReading, bo.getInitReading()); - lqw.eq(bo.getMaxRang() != null, TbMeterInfo::getMaxRang, bo.getMaxRang()); - lqw.eq(bo.getCommunicationState() != null, TbMeterInfo::getCommunicationState, bo.getCommunicationState()); lqw.eq(bo.getRunningState() != null, TbMeterInfo::getRunningState, bo.getRunningState()); + lqw.eq(bo.getCommunicationState() != null, TbMeterInfo::getCommunicationState, bo.getCommunicationState()); return lqw; } @@ -253,23 +252,27 @@ public class TbMeterInfoServiceImpl implements ITbMeterInfoService { List meterInfoVoList = this.queryList(meterInfoBo); if (meterInfoVoList.isEmpty()) return null; - String[] hostIpArr = meterInfoVoList.stream().map(TbMeterInfoVo::getHostIp).toArray(String[]::new); - Map ipCountMap = meterInfoVoList.stream().collect(Collectors.groupingBy(TbMeterInfoVo::getHostIp, Collectors.counting())); - List meterResults = meterRecordUtil.getMeterStatus(ipCountMap, hostIpArr); - - List resultList = new ArrayList<>(); - for (MeterResult item : meterResults){ - TbMeterInfoVo meterInfoVo = meterInfoVoList.stream().filter(o -> o.getHostIp().equals(item.getIp())).findFirst().orElse(null); - if (meterInfoVo == null) continue; - - BigDecimal initReading = BigDecimal.valueOf(item.getCollectionValue().get(Integer.parseInt(meterInfoVo.getMeterCode()))); - if (initReading.equals(BigDecimal.ZERO)){ - meterInfoVo.setCommunicationState(0L); - } - meterInfoVo.setInitReading(initReading); - resultList.add(meterInfoVo); + String[] hostIpArr = meterInfoVoList.stream().map(TbMeterInfoVo::getHostIp).distinct().toArray(String[]::new); + Map ipCountMap = new HashMap<>(); + for (String ip : hostIpArr) { + ipCountMap.put(ip, baseMapper.selectCount(new LambdaQueryWrapper().eq(TbMeterInfo::getHostIp, ip))); } + List meterResults = meterRecordUtil.getMeterStatus(ipCountMap, hostIpArr); + log.info("获取仪表状态结果={}", meterResults); - return resultList; + for (TbMeterInfoVo item : meterInfoVoList) { + MeterResult meterResult = meterResults.stream().filter(o -> o.getIp().equals(item.getHostIp())).findFirst().orElse(null); + if (meterResult == null) continue; + + BigDecimal initReading = BigDecimal.valueOf(meterResult.getCollectionValue().get(Integer.parseInt(item.getMeterCode()))) + .setScale(2, RoundingMode.HALF_UP); + if (initReading.equals(BigDecimal.ZERO)) { + item.setCommunicationState(0L); + } else { + item.setCommunicationState(1L); + } + item.setInitReading(initReading); + } + return meterInfoVoList; } } diff --git a/ruoyi-modules/Property/src/main/java/org/dromara/property/utils/MeterRecordUtil.java b/ruoyi-modules/Property/src/main/java/org/dromara/property/utils/MeterRecordUtil.java index 82e7f2f3..bfec4d8e 100644 --- a/ruoyi-modules/Property/src/main/java/org/dromara/property/utils/MeterRecordUtil.java +++ b/ruoyi-modules/Property/src/main/java/org/dromara/property/utils/MeterRecordUtil.java @@ -33,46 +33,6 @@ public class MeterRecordUtil { private T data; } - /** - * 发起请求并解析响应结果 - * - * @param uri 请求路径 - * @param params 请求参数 - * @param 泛型类型 - * @return 解析后的数据对象 - */ - public T request(String uri, String params) { - // 参数校验 - if (uri == null || uri.isEmpty()) { - log.warn("请求URI不能为空"); - return null; - } - - String url = meterRecordUrl + uri; - log.info("发起请求 - URL: {}, 参数: {}", url, params); - - try { - // 发起POST请求 - String post = HttpUtil.post(url, params); - log.debug("接收到响应: {}", post); - - // 解析JSON响应 - Result result = JSONUtil.toBean(post, new TypeReference<>() { - }, true); - - // 判断响应是否成功 - if (result != null && result.getCode() == 200) { - return result.getData(); - } else { - log.warn("请求失败,状态码: {}", result != null ? result.getCode() : "未知"); - } - } catch (Exception e) { - log.error("请求处理异常 - URL: {}, 参数: {}", url, params, e); - } - - return null; - } - /** * 获取水/电/气表当前读数 */ @@ -80,7 +40,9 @@ public class MeterRecordUtil { JSONObject jsonObject = new JSONObject(); jsonObject.putOnce("ipMap", ipMap); jsonObject.putOnce("ipArr", ipArr); - return request(READING_URL, jsonObject.toString()); + String post = HttpUtil.post(meterRecordUrl + READING_URL, jsonObject.toString()); + Result> result = JSONUtil.toBean(post, new TypeReference>>() {}, true); + return result.getData(); } }