feat(property): 优化水电气表树结构和状态获取功能

- 在 queryMeterInfoTree 方法中添加 isMeter 参数,用于控制是否返回仪表信息
- 修改 getMeterStatus 方法,增加 meterType 参数,用于指定水电气类型
- 优化 TbMeterInfoController 中的路由设计,使用 query 参数替代路径参数- 在获取仪表状态时增加异常处理,确保数据获取失败时能够返回所有数据并设置通信状态为 0
- 在 HttpUtil.post 方法中增加超时设置,提高请求稳定性
This commit is contained in:
2025-08-31 12:44:04 +08:00
parent b1f0ce3d3c
commit f9544ad4e1
4 changed files with 52 additions and 31 deletions

View File

@@ -28,7 +28,7 @@ import org.dromara.common.mybatis.core.page.TableDataInfo;
* 前端访问路由地址为:/property/meterInfo
*
* @author lsm
* @date 2025-07-19
* @since 2025-07-19
*/
@Validated
@RequiredArgsConstructor
@@ -66,7 +66,7 @@ public class TbMeterInfoController extends BaseController {
@SaCheckPermission("property:meterInfo:query")
@GetMapping("/{id}")
public R<TbMeterInfoVo> getInfo(@NotNull(message = "主键不能为空")
@PathVariable("id") Long id) {
@PathVariable("id") Long id) {
return R.ok(tbMeterInfoService.queryById(id));
}
@@ -109,20 +109,23 @@ public class TbMeterInfoController extends BaseController {
* 生成 社区/建组/单元/楼栋/(水电气表)树结构
*
* @param meterType 水电气类型
*
* @param isMeter 是否返回仪表
* @return (水电气表)树结构
*/
@GetMapping("/tree/{meterType}")
public R<List<TreeNode<Long>>> queryMeterInfoTree(@PathVariable("meterType") Long meterType) {
return R.ok(tbMeterInfoService.queryMeterInfoTree(meterType));
@GetMapping("/tree")
public R<List<TreeNode<Long>>> queryMeterInfoTree(@RequestParam Long meterType, @RequestParam Boolean isMeter) {
return R.ok(tbMeterInfoService.queryMeterInfoTree(meterType, isMeter));
}
/**
* 获取水/电/气表当前读数
* 获取水/电/气表当前读数/状态
*
* @param meterType 水电气类型
* @param floorId 楼栋id
*/
@GetMapping("/currentReading/{floorId}")
public R<List<TbMeterInfoVo>> currentReading(@PathVariable("floorId") Long floorId) {
return R.ok(tbMeterInfoService.getMeterStatus(floorId));
@GetMapping("/currentReading")
public R<List<TbMeterInfoVo>> currentReading(@RequestParam Long meterType, @RequestParam Long floorId) {
return R.ok(tbMeterInfoService.getMeterStatus(meterType, floorId));
}
}

View File

@@ -179,10 +179,11 @@ public class TbMeterInfoServiceImpl implements ITbMeterInfoService {
* 查询水电气树结构
*
* @param meterType 水电气类型
* @param isMeter 是否返回仪表
* @return 水电气树结构
*/
@Override
public List<TreeNode<Long>> queryMeterInfoTree(Long meterType) {
public List<TreeNode<Long>> queryMeterInfoTree(Long meterType, Boolean isMeter) {
// 默认加载社区树
List<TreeNode<Long>> treeList = new ArrayList<>();
List<TbCommunityVo> tbCommunityVos = communityService.queryAll();
@@ -225,30 +226,36 @@ public class TbMeterInfoServiceImpl implements ITbMeterInfoService {
}).toList();
treeList.addAll(l3);
TbMeterInfoBo bo = new TbMeterInfoBo();
bo.setMeterType(meterType);
List<TbMeterInfoVo> meterInfoVos = this.queryList(bo);
if (meterInfoVos != null && !meterInfoVos.isEmpty()) {
List<TreeNode<Long>> l4 = meterInfoVos.stream().map(item -> {
TreeNode<Long> node = new TreeNode<>();
node.setLevel(4);
node.setCode(item.getId());
node.setParentCode(item.getFloorId());
node.setLabel(item.getMeterName());
return node;
}).toList();
treeList.addAll(l4);
if (isMeter){
TbMeterInfoBo bo = new TbMeterInfoBo();
bo.setMeterType(meterType);
List<TbMeterInfoVo> meterInfoVos = this.queryList(bo);
if (meterInfoVos != null && !meterInfoVos.isEmpty()) {
List<TreeNode<Long>> l4 = meterInfoVos.stream().map(item -> {
TreeNode<Long> node = new TreeNode<>();
node.setLevel(4);
node.setCode(item.getId());
node.setParentCode(item.getFloorId());
node.setLabel(item.getMeterName());
return node;
}).toList();
treeList.addAll(l4);
}
}
return TreeUtils.build(treeList, 0L);
}
/**
* 获取水/电/气表当前读数/状态
*
* @param meterType 水电气类型
* @param floorId 楼栋id
*/
@Override
public List<TbMeterInfoVo> getMeterStatus(Long floorId) {
public List<TbMeterInfoVo> getMeterStatus(Long meterType, Long floorId) {
TbMeterInfoBo meterInfoBo = new TbMeterInfoBo();
meterInfoBo.setFloorId(floorId);
meterInfoBo.setMeterType(meterType);
List<TbMeterInfoVo> meterInfoVoList = this.queryList(meterInfoBo);
if (meterInfoVoList.isEmpty()) return null;
@@ -257,8 +264,16 @@ public class TbMeterInfoServiceImpl implements ITbMeterInfoService {
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);
List<MeterResult> meterResults;
try{
meterResults = meterRecordUtil.getMeterStatus(ipCountMap, hostIpArr);
} catch (Exception e) {
// 获取数据失败返回所有数据设置通信状态为0
for (TbMeterInfoVo item : meterInfoVoList) {
item.setCommunicationState(0L);
}
return meterInfoVoList;
}
for (TbMeterInfoVo item : meterInfoVoList) {
MeterResult meterResult = meterResults.stream().filter(o -> o.getIp().equals(item.getHostIp())).findFirst().orElse(null);

View File

@@ -71,13 +71,16 @@ public interface ITbMeterInfoService {
* 查询水电气树结构
*
* @param meterType 水电气类型
*
* @param isMeter 是否返回仪表
* @return 水电气树结构
*/
List<TreeNode<Long>> queryMeterInfoTree(Long meterType);
List<TreeNode<Long>> queryMeterInfoTree(Long meterType, Boolean isMeter);
/**
* 获取水/电/气表当前读数/状态
*
* @param meterType 水电气类型
* @param floorId 楼栋id
*/
List<TbMeterInfoVo> getMeterStatus(Long floorId);
List<TbMeterInfoVo> getMeterStatus(Long meterType, Long floorId);
}

View File

@@ -40,7 +40,7 @@ public class MeterRecordUtil {
JSONObject jsonObject = new JSONObject();
jsonObject.putOnce("ipMap", ipMap);
jsonObject.putOnce("ipArr", ipArr);
String post = HttpUtil.post(meterRecordUrl + READING_URL, jsonObject.toString());
String post = HttpUtil.post(meterRecordUrl + READING_URL, jsonObject.toString(), 3000);
Result<List<MeterResult>> result = JSONUtil.toBean(post, new TypeReference<Result<List<MeterResult>>>() {}, true);
return result.getData();
}