feat(record): 添加获取所有设备最新数据的功能

- 新增接口和方法获取所有设备的最新数据
- 实现了批量查询多个设备在指定表中的最新记录
- 优化了查询效率,减少了数据库访问次数
- 增加了对分表不存在情况的处理
boardTest
zch 1 month ago
parent 1f7dc3452b
commit 7184485ba2

@ -103,4 +103,16 @@ public class EmsRecordAlarmRuleController extends BaseController
{ {
return toAjax(emsRecordAlarmRuleService.deleteEmsRecordAlarmRuleByObjIds(objIds)); return toAjax(emsRecordAlarmRuleService.deleteEmsRecordAlarmRuleByObjIds(objIds));
} }
/**
*
*/
@PreAuthorize("@ss.hasPermi('ems/record:recordAlarmRule:list')")
@GetMapping("/getEmsRecordAlarmRuleList")
public AjaxResult getEmsRecordAlarmRuleList(EmsRecordAlarmRule emsRecordAlarmRule)
{
List<EmsRecordAlarmRule> list = emsRecordAlarmRuleService.selectEmsRecordAlarmRuleList(emsRecordAlarmRule);
return success(list);
}
} }

@ -113,4 +113,19 @@ public class RecordIotenvInstantController extends BaseController
List<RecordIotenvInstant> recordIotenvInstants = recordIotenvInstantService.selectRecordIotenvInstantList(recordIotenvInstant); List<RecordIotenvInstant> recordIotenvInstants = recordIotenvInstantService.selectRecordIotenvInstantList(recordIotenvInstant);
return success(recordIotenvInstants); return success(recordIotenvInstants);
} }
/**
*
*/
@GetMapping("/getLatestRecords")
public AjaxResult getLatestRecords()
{
try {
List<RecordIotenvInstant> latestRecords = recordIotenvInstantService.selectRecordList(new RecordIotenvInstant());
return success(latestRecords);
} catch (Exception e) {
logger.error("获取设备最新数据失败", e);
return error("获取设备最新数据失败:" + e.getMessage());
}
}
} }

@ -134,4 +134,24 @@ public interface RecordIotenvInstantMapper
*/ */
public List<RecordIotenvInstant> selectRecentRecordsFromTable(@Param("tableName") String tableName, public List<RecordIotenvInstant> selectRecentRecordsFromTable(@Param("tableName") String tableName,
@Param("startTime") java.util.Date startTime); @Param("startTime") java.util.Date startTime);
/**
*
*
* @param tableName
* @param monitorId
* @return
*/
public RecordIotenvInstant selectLatestRecordByMonitorIdFromTable(@Param("tableName") String tableName,
@Param("monitorId") String monitorId);
/**
*
*
* @param tableName
* @param monitorIds
* @return
*/
public List<RecordIotenvInstant> selectLatestRecordsByMonitorIdsFromTable(@Param("tableName") String tableName,
@Param("monitorIds") List<String> monitorIds);
} }

@ -5,6 +5,7 @@ import java.util.Date;
import java.util.List; import java.util.List;
import com.os.common.core.page.TableDataInfo; import com.os.common.core.page.TableDataInfo;
import com.os.ems.base.domain.EmsBaseMonitorInfo;
import com.os.ems.record.domain.RecordIotenvInstant; import com.os.ems.record.domain.RecordIotenvInstant;
/** /**
@ -73,4 +74,6 @@ public interface IRecordIotenvInstantService
*/ */
public TableDataInfo selectRecordIotenvInstantListWithPage(RecordIotenvInstant recordIotenvInstant, int pageNum, int pageSize) throws ParseException; public TableDataInfo selectRecordIotenvInstantListWithPage(RecordIotenvInstant recordIotenvInstant, int pageNum, int pageSize) throws ParseException;
public List<RecordIotenvInstant> selectRecordList(RecordIotenvInstant recordIotenvInstant);
} }

@ -2,6 +2,8 @@ package com.os.ems.record.service.impl;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*; import java.util.*;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@ -53,12 +55,6 @@ public class RecordIotenvInstantServiceImpl implements IRecordIotenvInstantServi
* @param recordIotenvInstant * @param recordIotenvInstant
* @return * @return
*/ */
// @Override
// public List<RecordIotenvInstant> selectRecordIotenvInstantList(RecordIotenvInstant recordIotenvInstant)
// {
// Map<String, Object> params = recordIotenvInstant.getParams();
// return recordIotenvInstantMapper.selectRecordIotenvInstantList(recordIotenvInstant);
// }
@Override @Override
public List<RecordIotenvInstant> selectRecordIotenvInstantList(RecordIotenvInstant recordIotenvInstant) throws ParseException { public List<RecordIotenvInstant> selectRecordIotenvInstantList(RecordIotenvInstant recordIotenvInstant) throws ParseException {
Map<String, Object> params = recordIotenvInstant.getParams(); Map<String, Object> params = recordIotenvInstant.getParams();
@ -297,6 +293,82 @@ public class RecordIotenvInstantServiceImpl implements IRecordIotenvInstantServi
return result; return result;
} }
@Override
public List<RecordIotenvInstant> selectRecordList(RecordIotenvInstant recordIotenvInstant)
{
List<RecordIotenvInstant> result = new ArrayList<>();
// 获取当天日期对应的分表名
String todayTableName = getTodayTableName();
// 检查当天分表是否存在
boolean tableExists = isTableExists(todayTableName);
// 获取所有设备信息
List<EmsBaseMonitorInfo> baseMonitorInfos = emsBaseMonitorInfoMapper.selectEmsBaseMonitorInfoList(new EmsBaseMonitorInfo());
//获取所有能源类型
if (tableExists) {
// 如果分表存在,批量查询所有设备的最新数据
List<String> monitorCodes = new ArrayList<>();
for (EmsBaseMonitorInfo baseMonitorInfo : baseMonitorInfos) {
monitorCodes.add(baseMonitorInfo.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 baseMonitorInfo : baseMonitorInfos) {
String monitorCode = baseMonitorInfo.getMonitorCode();
String monitorName = baseMonitorInfo.getMonitorName();
Long monitorType = baseMonitorInfo.getMonitorType();
RecordIotenvInstant iotenvInstant = recordMap.get(monitorCode);
if (iotenvInstant == null) {
// 如果没有找到记录,创建一个空记录
iotenvInstant = new RecordIotenvInstant();
iotenvInstant.setMonitorId(monitorCode);
}
// 设置设备基本信息
iotenvInstant.setMonitorName(monitorName);
iotenvInstant.setMonitorCode(monitorCode);
result.add(iotenvInstant);
}
} else {
// 如果分表不存在,返回所有设备的空记录
for (EmsBaseMonitorInfo baseMonitorInfo : baseMonitorInfos) {
RecordIotenvInstant iotenvInstant = new RecordIotenvInstant();
iotenvInstant.setMonitorId(baseMonitorInfo.getMonitorCode());
iotenvInstant.setMonitorName(baseMonitorInfo.getMonitorName());
iotenvInstant.setMonitorCode(baseMonitorInfo.getMonitorCode());
// 数据值字段保持null前端会显示"无最新数据"
result.add(iotenvInstant);
}
}
return result;
}
/**
*
*
* @return
*/
private String getTodayTableName() {
LocalDate today = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
String dateSuffix = today.format(formatter);
return "record_iotenv_instant_" + dateSuffix;
}
} }

@ -174,7 +174,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
t.collectTime, t.recodeTime, t.collectTime, t.recodeTime,
ebmi.monitor_name ebmi.monitor_name
FROM ${tableName} t FROM ${tableName} t
left join ems_base_monitor_info ebmi on t.monitorId = ebmi.obj_id left join ems_base_monitor_info ebmi on t.monitorId = ebmi.monitor_code
<where> <where>
<if test="recordIotenvInstant.monitorId != null and recordIotenvInstant.monitorId != ''"> and monitorId = #{recordIotenvInstant.monitorId}</if> <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.temperature != null "> and temperature = #{recordIotenvInstant.temperature}</if>
@ -316,4 +316,36 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
ORDER BY recodeTime DESC ORDER BY recodeTime DESC
</select> </select>
<!-- 根据设备编号从指定表查询最新一条记录 -->
<select id="selectLatestRecordByMonitorIdFromTable" resultMap="RecordIotenvInstantResult">
SELECT t.objid, t.monitorId, t.temperature, t.humidity, t.illuminance, t.noise, t.concentration,
t.vibration_speed, t.vibration_displacement, t.vibration_acceleration, t.vibration_temp,
t.collectTime, t.recodeTime,
ebmi.monitor_name
FROM ${tableName} t
LEFT JOIN ems_base_monitor_info ebmi ON t.monitorId = ebmi.monitor_code
WHERE t.monitorId = #{monitorId}
ORDER BY t.objid DESC
LIMIT 1
</select>
<!-- 批量查询多个设备在指定表中的最新记录 -->
<select id="selectLatestRecordsByMonitorIdsFromTable" resultMap="RecordIotenvInstantResult">
SELECT t1.objid, t1.monitorId, t1.temperature, t1.humidity, t1.illuminance, t1.noise, t1.concentration,
t1.vibration_speed, t1.vibration_displacement, t1.vibration_acceleration, t1.vibration_temp,
t1.collectTime, t1.recodeTime,
ebmi.monitor_name
FROM ${tableName} t1
LEFT JOIN ems_base_monitor_info ebmi ON t1.monitorId = ebmi.monitor_code
INNER JOIN (
SELECT monitorId, MAX(objid) as max_objid
FROM ${tableName}
WHERE monitorId IN
<foreach collection="monitorIds" item="monitorId" open="(" separator="," close=")">
#{monitorId}
</foreach>
GROUP BY monitorId
) t2 ON t1.monitorId = t2.monitorId AND t1.objid = t2.max_objid
</select>
</mapper> </mapper>
Loading…
Cancel
Save