From 10033dd646a365feff7f2ed0d39d34d71105b7c4 Mon Sep 17 00:00:00 2001 From: zcxlsm Date: Sat, 30 Aug 2025 21:03:26 +0800 Subject: [PATCH] =?UTF-8?q?feat(Property):=20=E6=B7=BB=E5=8A=A0=E6=B0=B4/?= =?UTF-8?q?=E7=94=B5/=E6=B0=94=E8=A1=A8=E5=BD=93=E5=89=8D=E8=AF=BB?= =?UTF-8?q?=E6=95=B0=E5=92=8C=E7=8A=B6=E6=80=81=E8=8E=B7=E5=8F=96=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../TbMeterInfoController.java | 8 ++ .../TbMeterInfoServiceImpl.java | 37 +++++++- .../TbMeterRecordServiceImpl.java | 12 +-- .../ITbMeterInfoService.java | 7 +- .../property/utils/MeterRecordUtil.java | 86 +++++++++++++++++++ 5 files changed, 138 insertions(+), 12 deletions(-) create mode 100644 ruoyi-modules/Property/src/main/java/org/dromara/property/utils/MeterRecordUtil.java diff --git a/ruoyi-modules/Property/src/main/java/org/dromara/property/controller/smartDevicesController/TbMeterInfoController.java b/ruoyi-modules/Property/src/main/java/org/dromara/property/controller/smartDevicesController/TbMeterInfoController.java index df9b6d17..e3f4de13 100644 --- a/ruoyi-modules/Property/src/main/java/org/dromara/property/controller/smartDevicesController/TbMeterInfoController.java +++ b/ruoyi-modules/Property/src/main/java/org/dromara/property/controller/smartDevicesController/TbMeterInfoController.java @@ -117,4 +117,12 @@ public class TbMeterInfoController extends BaseController { return R.ok(tbMeterInfoService.queryMeterInfoTree(meterType)); } + /** + * 获取水/电/气表当前读数 + */ + @GetMapping("/currentReading/{floorId}") + public R currentReading(@PathVariable("floorId") Long floorId) { + return R.ok(); + } + } 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 a5a71bec..cefb768b 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 @@ -12,15 +12,15 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.dromara.common.satoken.utils.LoginHelper; import org.dromara.property.domain.bo.TbFloorBo; import org.dromara.property.domain.vo.TbBuildingVo; import org.dromara.property.domain.vo.TbCommunityVo; import org.dromara.property.domain.vo.TbFloorVo; +import org.dromara.property.rocketmq.domain.MeterResult; import org.dromara.property.service.ITbBuildingService; import org.dromara.property.service.ITbCommunityService; import org.dromara.property.service.ITbFloorService; -import org.dromara.system.api.model.LoginUser; +import org.dromara.property.utils.MeterRecordUtil; import org.springframework.stereotype.Service; import org.dromara.property.domain.bo.smartDevicesBo.TbMeterInfoBo; import org.dromara.property.domain.vo.smartDevicesVo.TbMeterInfoVo; @@ -29,6 +29,7 @@ import org.dromara.property.mapper.smartDevicesMapper.TbMeterInfoMapper; 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; @@ -51,6 +52,8 @@ public class TbMeterInfoServiceImpl implements ITbMeterInfoService { private final ITbBuildingService buildingService; private final ITbCommunityService communityService; + private final MeterRecordUtil meterRecordUtil; + /** * 查询水电气 * @@ -239,4 +242,34 @@ public class TbMeterInfoServiceImpl implements ITbMeterInfoService { } return TreeUtils.build(treeList, 0L); } + + /** + * 获取水/电/气表当前读数/状态 + */ + @Override + public List getMeterStatus(Long floorId) { + TbMeterInfoBo meterInfoBo = new TbMeterInfoBo(); + meterInfoBo.setFloorId(floorId); + 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); + } + + return resultList; + } } diff --git a/ruoyi-modules/Property/src/main/java/org/dromara/property/service/impl/smartDevicesImpl/TbMeterRecordServiceImpl.java b/ruoyi-modules/Property/src/main/java/org/dromara/property/service/impl/smartDevicesImpl/TbMeterRecordServiceImpl.java index de1e099e..bf6a03b9 100644 --- a/ruoyi-modules/Property/src/main/java/org/dromara/property/service/impl/smartDevicesImpl/TbMeterRecordServiceImpl.java +++ b/ruoyi-modules/Property/src/main/java/org/dromara/property/service/impl/smartDevicesImpl/TbMeterRecordServiceImpl.java @@ -280,9 +280,7 @@ public class TbMeterRecordServiceImpl implements ITbMeterRecordService { Map hourMap = new HashMap<>(); List> hourList = baseMapper.getHourTrend(StrUtil.isBlank(floorId) ? null : Long.parseLong(floorId), StrUtil.isBlank(meterId) ? null : Long.parseLong(meterId), meterType, day); List hourData = new ArrayList<>(); - hourList.forEach(item -> { - hourData.add(new String[]{item.get("hour").toString(), item.get("total_consumption").toString()}); - }); + hourList.forEach(item -> hourData.add(new String[]{item.get("hour").toString(), item.get("total_consumption").toString()})); Float total = hourList.stream().map(map -> new BigDecimal(map.get("total_consumption").toString())).reduce(BigDecimal::add).orElse(BigDecimal.ZERO).floatValue(); hourMap.put("total", total); hourMap.put("data", hourData); @@ -293,9 +291,7 @@ public class TbMeterRecordServiceImpl implements ITbMeterRecordService { Map dayMap = new HashMap<>(); List> dayList = baseMapper.getDayTrend(StrUtil.isBlank(floorId) ? null : Long.parseLong(floorId), StrUtil.isBlank(meterId) ? null : Long.parseLong(meterId), meterType, year, month); List dayData = new ArrayList<>(); - dayList.forEach(item -> { - dayData.add(new String[]{item.get("day").toString(), item.get("total_consumption").toString()}); - }); + dayList.forEach(item -> dayData.add(new String[]{item.get("day").toString(), item.get("total_consumption").toString()})); Float total = dayList.stream().map(map -> new BigDecimal(map.get("total_consumption").toString())).reduce(BigDecimal::add).orElse(BigDecimal.ZERO).floatValue(); dayMap.put("total", total); dayMap.put("data", dayData); @@ -306,9 +302,7 @@ public class TbMeterRecordServiceImpl implements ITbMeterRecordService { Map resultMap = new HashMap<>(); List> monthList = baseMapper.getMonthTrend(StrUtil.isBlank(floorId) ? null : Long.parseLong(floorId), StrUtil.isBlank(meterId) ? null : Long.parseLong(meterId), meterType, year); List monthData = new ArrayList<>(); - monthList.forEach(item -> { - monthData.add(new String[]{item.get("month").toString(), item.get("total_consumption").toString()}); - }); + monthList.forEach(item -> monthData.add(new String[]{item.get("month").toString(), item.get("total_consumption").toString()})); Float total = monthList.stream().map(map -> new BigDecimal(map.get("total_consumption").toString())).reduce(BigDecimal::add).orElse(BigDecimal.ZERO).floatValue(); resultMap.put("total", total); resultMap.put("data", monthData); diff --git a/ruoyi-modules/Property/src/main/java/org/dromara/property/service/smartDevicesService/ITbMeterInfoService.java b/ruoyi-modules/Property/src/main/java/org/dromara/property/service/smartDevicesService/ITbMeterInfoService.java index 7695b8f2..f3ca6e17 100644 --- a/ruoyi-modules/Property/src/main/java/org/dromara/property/service/smartDevicesService/ITbMeterInfoService.java +++ b/ruoyi-modules/Property/src/main/java/org/dromara/property/service/smartDevicesService/ITbMeterInfoService.java @@ -13,7 +13,7 @@ import java.util.List; * 水电气Service接口 * * @author lsm - * @date 2025-07-19 + * @since 2025-07-19 */ public interface ITbMeterInfoService { @@ -75,4 +75,9 @@ public interface ITbMeterInfoService { * @return 水电气树结构 */ List> queryMeterInfoTree(Long meterType); + + /** + * 获取水/电/气表当前读数/状态 + */ + List getMeterStatus(Long floorId); } 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 new file mode 100644 index 00000000..82e7f2f3 --- /dev/null +++ b/ruoyi-modules/Property/src/main/java/org/dromara/property/utils/MeterRecordUtil.java @@ -0,0 +1,86 @@ +package org.dromara.property.utils; + +import cn.hutool.core.lang.TypeReference; +import cn.hutool.http.HttpUtil; +import cn.hutool.json.JSONObject; +import cn.hutool.json.JSONUtil; +import lombok.Data; +import lombok.extern.slf4j.Slf4j; +import org.dromara.property.rocketmq.domain.MeterResult; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.Map; + +/** + * @author lsm + * @apiNote MeterRecordUtil + * @since 2025/8/30 + */ +@Slf4j +@Component +public class MeterRecordUtil { + + @Value("${eqp.config.meter.host}") + private String meterRecordUrl; + + private static final String READING_URL = "reading"; + + @Data + public static class Result { + private Integer code; + 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; + } + + /** + * 获取水/电/气表当前读数 + */ + public List getMeterStatus(Map ipMap, String[] ipArr) { + JSONObject jsonObject = new JSONObject(); + jsonObject.putOnce("ipMap", ipMap); + jsonObject.putOnce("ipArr", ipArr); + return request(READING_URL, jsonObject.toString()); + } + +}