refactor(Property): 修改仪表信息获取和处理逻辑

This commit is contained in:
2025-08-30 22:42:12 +08:00
parent 28f44a1c5f
commit b1f0ce3d3c
2 changed files with 30 additions and 65 deletions

View File

@@ -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<TbMeterInfo> 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<TbMeterInfoVo> meterInfoVoList = this.queryList(meterInfoBo);
if (meterInfoVoList.isEmpty()) return null;
String[] hostIpArr = meterInfoVoList.stream().map(TbMeterInfoVo::getHostIp).toArray(String[]::new);
Map<String, Long> ipCountMap = meterInfoVoList.stream().collect(Collectors.groupingBy(TbMeterInfoVo::getHostIp, Collectors.counting()));
List<MeterResult> meterResults = meterRecordUtil.getMeterStatus(ipCountMap, hostIpArr);
List<TbMeterInfoVo> 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<String, Long> ipCountMap = new HashMap<>();
for (String ip : hostIpArr) {
ipCountMap.put(ip, baseMapper.selectCount(new LambdaQueryWrapper<TbMeterInfo>().eq(TbMeterInfo::getHostIp, ip)));
}
List<MeterResult> 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;
}
}

View File

@@ -33,46 +33,6 @@ public class MeterRecordUtil {
private T data;
}
/**
* 发起请求并解析响应结果
*
* @param uri 请求路径
* @param params 请求参数
* @param <T> 泛型类型
* @return 解析后的数据对象
*/
public <T> 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<T> 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<List<MeterResult>> result = JSONUtil.toBean(post, new TypeReference<Result<List<MeterResult>>>() {}, true);
return result.getData();
}
}