feat(record): 根据父节点 ID 获取子设备最新数据

- 在 IRecordIotenvInstantService 接口中添加 selectRecordListByParentId 方法
- 在 RecordIotenvInstantController 中添加 getLatestRecordsByParentId 控制器- 在 RecordIotenvInstantServiceImpl 中实现 selectRecordListByParentId 方法
- 优化查询逻辑,支持分表查询和批量获取数据
boardTest
zch 3 months ago
parent f100f1771b
commit e1f45203a0

@ -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<RecordIotenvInstant> latestRecords = recordIotenvInstantService.selectRecordListByParentId(parentId);
return success(latestRecords);
} catch (Exception e) {
logger.error("根据父节点ID获取设备最新数据失败parentId: " + parentId, e);
return error("获取设备最新数据失败:" + e.getMessage());
}
}
}

@ -76,4 +76,12 @@ public interface IRecordIotenvInstantService
public List<RecordIotenvInstant> selectRecordList(RecordIotenvInstant recordIotenvInstant);
/**
* ID
*
* @param parentId ID
* @return
*/
public List<RecordIotenvInstant> selectRecordListByParentId(Long parentId);
}

@ -364,6 +364,88 @@ public class RecordIotenvInstantServiceImpl implements IRecordIotenvInstantServi
return result;
}
/**
* ID
*
* @param parentId ID
* @return
*/
@Override
public List<RecordIotenvInstant> selectRecordListByParentId(Long parentId)
{
List<RecordIotenvInstant> result = new ArrayList<>();
// 获取当天日期对应的分表名
String todayTableName = getTodayTableName();
// 检查当天分表是否存在
boolean tableExists = isTableExists(todayTableName);
// 根据父节点ID查询子设备信息
EmsBaseMonitorInfo queryParam = new EmsBaseMonitorInfo();
queryParam.setParentId(parentId);
List<EmsBaseMonitorInfo> childDevices = emsBaseMonitorInfoMapper.selectEmsBaseMonitorInfoList(queryParam);
if (childDevices.isEmpty()) {
return result;
}
if (tableExists) {
// 如果分表存在,批量查询子设备的最新数据
List<String> monitorCodes = new ArrayList<>();
for (EmsBaseMonitorInfo device : childDevices) {
monitorCodes.add(device.getMonitorCode());
}
// 批量查询最新记录
List<RecordIotenvInstant> latestRecords = recordIotenvInstantMapper
.selectLatestRecordsByMonitorIdsFromTable(todayTableName, monitorCodes);
// 创建设备编号到最新记录的映射
Map<String, RecordIotenvInstant> 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;
}
/**
*
*

@ -261,40 +261,40 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</select>
<!-- 统计多表查询的总记录数 -->
<select id="countFromTables" resultType="java.lang.Integer">
SELECT COUNT(*) FROM (
<foreach collection="tableNames" item="tableName" separator=" UNION ALL ">
SELECT objid
FROM ${tableName}
<where>
<if test="recordIotenvInstant.monitorId != null and recordIotenvInstant.monitorId != ''"> and monitorId = #{recordIotenvInstant.monitorId}</if>
<if test="recordIotenvInstant.temperature != null "> and temperature = #{recordIotenvInstant.temperature}</if>
<if test="recordIotenvInstant.humidity != null "> and humidity = #{recordIotenvInstant.humidity}</if>
<if test="recordIotenvInstant.illuminance != null "> and illuminance = #{recordIotenvInstant.illuminance}</if>
<if test="recordIotenvInstant.noise != null "> and noise = #{recordIotenvInstant.noise}</if>
<if test="recordIotenvInstant.concentration != null "> and concentration = #{recordIotenvInstant.concentration}</if>
<if test="recordIotenvInstant.vibrationSpeed != null "> and vibration_speed = #{recordIotenvInstant.vibrationSpeed}</if>
<if test="recordIotenvInstant.vibrationDisplacement != null "> and vibration_displacement = #{recordIotenvInstant.vibrationDisplacement}</if>
<if test="recordIotenvInstant.vibrationAcceleration != null "> and vibration_acceleration = #{recordIotenvInstant.vibrationAcceleration}</if>
<if test="recordIotenvInstant.vibrationTemp != null "> and vibration_temp = #{recordIotenvInstant.vibrationTemp}</if>
<if test="recordIotenvInstant.collectTime != null "> and collectTime = #{recordIotenvInstant.collectTime}</if>
<if test="recordIotenvInstant.recodeTime != null "> and recodeTime = #{recordIotenvInstant.recodeTime}</if>
<select id="countFromTables" resultType="java.lang.Integer">
SELECT COUNT(*) FROM (
<foreach collection="tableNames" item="tableName" separator=" UNION ALL ">
SELECT objid
FROM ${tableName}
<where>
<if test="recordIotenvInstant.monitorId != null and recordIotenvInstant.monitorId != ''"> and monitorId = #{recordIotenvInstant.monitorId}</if>
<if test="recordIotenvInstant.temperature != null "> and temperature = #{recordIotenvInstant.temperature}</if>
<if test="recordIotenvInstant.humidity != null "> and humidity = #{recordIotenvInstant.humidity}</if>
<if test="recordIotenvInstant.illuminance != null "> and illuminance = #{recordIotenvInstant.illuminance}</if>
<if test="recordIotenvInstant.noise != null "> and noise = #{recordIotenvInstant.noise}</if>
<if test="recordIotenvInstant.concentration != null "> and concentration = #{recordIotenvInstant.concentration}</if>
<if test="recordIotenvInstant.vibrationSpeed != null "> and vibration_speed = #{recordIotenvInstant.vibrationSpeed}</if>
<if test="recordIotenvInstant.vibrationDisplacement != null "> and vibration_displacement = #{recordIotenvInstant.vibrationDisplacement}</if>
<if test="recordIotenvInstant.vibrationAcceleration != null "> and vibration_acceleration = #{recordIotenvInstant.vibrationAcceleration}</if>
<if test="recordIotenvInstant.vibrationTemp != null "> and vibration_temp = #{recordIotenvInstant.vibrationTemp}</if>
<if test="recordIotenvInstant.collectTime != null "> and collectTime = #{recordIotenvInstant.collectTime}</if>
<if test="recordIotenvInstant.recodeTime != null "> and recodeTime = #{recordIotenvInstant.recodeTime}</if>
<if test="recordIotenvInstant.params.beginRecordTime!= null and recordIotenvInstant.params.endRecordTime != null">
AND recodeTime BETWEEN #{recordIotenvInstant.params.beginRecordTime} AND #{recordIotenvInstant.params.endRecordTime}
</if>
<if test="recordIotenvInstant.params.beginRecordTime!= null and recordIotenvInstant.params.endRecordTime != null">
AND recodeTime BETWEEN #{recordIotenvInstant.params.beginRecordTime} AND #{recordIotenvInstant.params.endRecordTime}
</if>
<if test="recordIotenvInstant.monitorIds != null and recordIotenvInstant.monitorIds.length > 0">
AND monitorId IN
<foreach collection="recordIotenvInstant.monitorIds" item="monitorId" open="(" separator="," close=")">
#{monitorId}
</foreach>
</if>
<if test="recordIotenvInstant.monitorIds != null and recordIotenvInstant.monitorIds.length > 0">
AND monitorId IN
<foreach collection="recordIotenvInstant.monitorIds" item="monitorId" open="(" separator="," close=")">
#{monitorId}
</foreach>
</if>
</where>
</foreach>
) AS total_count
</select>
</where>
</foreach>
) AS total_count
</select>
<!-- 从指定表查询最新的N条记录 -->
<select id="selectLatestRecordsFromTable" resultMap="RecordIotenvInstantResult">

Loading…
Cancel
Save