From e1f45203a02205cd34515c824e96a1026f80cd9b Mon Sep 17 00:00:00 2001 From: zch Date: Tue, 27 May 2025 15:55:38 +0800 Subject: [PATCH] =?UTF-8?q?feat(record):=20=E6=A0=B9=E6=8D=AE=E7=88=B6?= =?UTF-8?q?=E8=8A=82=E7=82=B9=20ID=20=E8=8E=B7=E5=8F=96=E5=AD=90=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E6=9C=80=E6=96=B0=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 IRecordIotenvInstantService 接口中添加 selectRecordListByParentId 方法 - 在 RecordIotenvInstantController 中添加 getLatestRecordsByParentId 控制器- 在 RecordIotenvInstantServiceImpl 中实现 selectRecordListByParentId 方法 - 优化查询逻辑,支持分表查询和批量获取数据 --- .../RecordIotenvInstantController.java | 15 ++++ .../service/IRecordIotenvInstantService.java | 8 ++ .../impl/RecordIotenvInstantServiceImpl.java | 82 +++++++++++++++++++ .../ems/record/RecordIotenvInstantMapper.xml | 62 +++++++------- 4 files changed, 136 insertions(+), 31 deletions(-) diff --git a/os-ems/src/main/java/com/os/ems/record/controller/RecordIotenvInstantController.java b/os-ems/src/main/java/com/os/ems/record/controller/RecordIotenvInstantController.java index e6a464e..0513bfb 100644 --- a/os-ems/src/main/java/com/os/ems/record/controller/RecordIotenvInstantController.java +++ b/os-ems/src/main/java/com/os/ems/record/controller/RecordIotenvInstantController.java @@ -128,4 +128,19 @@ public class RecordIotenvInstantController extends BaseController return error("获取设备最新数据失败:" + e.getMessage()); } } + + /** + * 根据父节点ID获取子设备的最新数据 + */ + @GetMapping("/getLatestRecordsByParentId/{parentId}") + public AjaxResult getLatestRecordsByParentId(@PathVariable("parentId") Long parentId) + { + try { + List latestRecords = recordIotenvInstantService.selectRecordListByParentId(parentId); + return success(latestRecords); + } catch (Exception e) { + logger.error("根据父节点ID获取设备最新数据失败,parentId: " + parentId, e); + return error("获取设备最新数据失败:" + e.getMessage()); + } + } } diff --git a/os-ems/src/main/java/com/os/ems/record/service/IRecordIotenvInstantService.java b/os-ems/src/main/java/com/os/ems/record/service/IRecordIotenvInstantService.java index 18345f5..e64839d 100644 --- a/os-ems/src/main/java/com/os/ems/record/service/IRecordIotenvInstantService.java +++ b/os-ems/src/main/java/com/os/ems/record/service/IRecordIotenvInstantService.java @@ -76,4 +76,12 @@ public interface IRecordIotenvInstantService public List selectRecordList(RecordIotenvInstant recordIotenvInstant); + /** + * 根据父节点ID查询子设备的最新数据 + * + * @param parentId 父节点ID + * @return 子设备最新数据列表 + */ + public List selectRecordListByParentId(Long parentId); + } diff --git a/os-ems/src/main/java/com/os/ems/record/service/impl/RecordIotenvInstantServiceImpl.java b/os-ems/src/main/java/com/os/ems/record/service/impl/RecordIotenvInstantServiceImpl.java index e92212f..6c306ac 100644 --- a/os-ems/src/main/java/com/os/ems/record/service/impl/RecordIotenvInstantServiceImpl.java +++ b/os-ems/src/main/java/com/os/ems/record/service/impl/RecordIotenvInstantServiceImpl.java @@ -364,6 +364,88 @@ public class RecordIotenvInstantServiceImpl implements IRecordIotenvInstantServi return result; } + /** + * 根据父节点ID查询子设备的最新数据 + * + * @param parentId 父节点ID + * @return 子设备最新数据列表 + */ + @Override + public List selectRecordListByParentId(Long parentId) + { + List result = new ArrayList<>(); + + // 获取当天日期对应的分表名 + String todayTableName = getTodayTableName(); + + // 检查当天分表是否存在 + boolean tableExists = isTableExists(todayTableName); + + // 根据父节点ID查询子设备信息 + EmsBaseMonitorInfo queryParam = new EmsBaseMonitorInfo(); + queryParam.setParentId(parentId); + List childDevices = emsBaseMonitorInfoMapper.selectEmsBaseMonitorInfoList(queryParam); + + if (childDevices.isEmpty()) { + return result; + } + + if (tableExists) { + // 如果分表存在,批量查询子设备的最新数据 + List monitorCodes = new ArrayList<>(); + for (EmsBaseMonitorInfo device : childDevices) { + monitorCodes.add(device.getMonitorCode()); + } + + // 批量查询最新记录 + List latestRecords = recordIotenvInstantMapper + .selectLatestRecordsByMonitorIdsFromTable(todayTableName, monitorCodes); + + // 创建设备编号到最新记录的映射 + Map recordMap = new HashMap<>(); + for (RecordIotenvInstant record : latestRecords) { + recordMap.put(record.getMonitorId(), record); + } + + // 遍历子设备,构建返回结果 + for (EmsBaseMonitorInfo device : childDevices) { + String monitorCode = device.getMonitorCode(); + String monitorName = device.getMonitorName(); + Long monitorType = device.getMonitorType(); + String energyName = device.getEnergyName(); + + RecordIotenvInstant iotenvInstant = recordMap.get(monitorCode); + if (iotenvInstant == null) { + // 如果没有找到记录,创建一个空记录 + iotenvInstant = new RecordIotenvInstant(); + iotenvInstant.setMonitorId(monitorCode); + } + + // 设置设备基本信息 + iotenvInstant.setMonitorName(monitorName); + iotenvInstant.setMonitorCode(monitorCode); + iotenvInstant.setEnergyName(energyName); + iotenvInstant.setMonitorType(monitorType); + + result.add(iotenvInstant); + } + } else { + // 如果分表不存在,返回子设备的空记录 + for (EmsBaseMonitorInfo device : childDevices) { + RecordIotenvInstant iotenvInstant = new RecordIotenvInstant(); + iotenvInstant.setMonitorId(device.getMonitorCode()); + iotenvInstant.setMonitorName(device.getMonitorName()); + iotenvInstant.setMonitorCode(device.getMonitorCode()); + iotenvInstant.setEnergyName(device.getEnergyName()); + iotenvInstant.setMonitorType(device.getMonitorType()); + // 数据值字段保持null,前端会显示"无最新数据" + result.add(iotenvInstant); + } + } + + return result; + } + /** * 获取当天日期对应的分表名 * diff --git a/os-ems/src/main/resources/mapper/ems/record/RecordIotenvInstantMapper.xml b/os-ems/src/main/resources/mapper/ems/record/RecordIotenvInstantMapper.xml index 762e82e..0ef5cdd 100644 --- a/os-ems/src/main/resources/mapper/ems/record/RecordIotenvInstantMapper.xml +++ b/os-ems/src/main/resources/mapper/ems/record/RecordIotenvInstantMapper.xml @@ -261,40 +261,40 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" - + SELECT COUNT(*) FROM ( + + SELECT objid + FROM ${tableName} + + and monitorId = #{recordIotenvInstant.monitorId} + and temperature = #{recordIotenvInstant.temperature} + and humidity = #{recordIotenvInstant.humidity} + and illuminance = #{recordIotenvInstant.illuminance} + and noise = #{recordIotenvInstant.noise} + and concentration = #{recordIotenvInstant.concentration} + and vibration_speed = #{recordIotenvInstant.vibrationSpeed} + and vibration_displacement = #{recordIotenvInstant.vibrationDisplacement} + and vibration_acceleration = #{recordIotenvInstant.vibrationAcceleration} + and vibration_temp = #{recordIotenvInstant.vibrationTemp} + and collectTime = #{recordIotenvInstant.collectTime} + and recodeTime = #{recordIotenvInstant.recodeTime} - - AND recodeTime BETWEEN #{recordIotenvInstant.params.beginRecordTime} AND #{recordIotenvInstant.params.endRecordTime} - + + AND recodeTime BETWEEN #{recordIotenvInstant.params.beginRecordTime} AND #{recordIotenvInstant.params.endRecordTime} + - - AND monitorId IN - - #{monitorId} - - + + AND monitorId IN + + #{monitorId} + + - - - ) AS total_count - + + + ) AS total_count +