feat(record): (第二版)增加物联网数据分页查询功能并优化多设备查询

- 在 IRecordIotenvInstantService 接口中添加了分页查询方法
- 在 RecordIotenvInstantController 中实现了分页查询和不分页查询接口
- 在 RecordIotenvInstantMapper 中添加了分页查询和计数方法
- 在 RecordIotenvInstantMapper.xml 中添加了对应的 SQL 语句
- 在 RecordIotenvInstantServiceImpl 中实现了分页查询逻辑,并优化了多设备查询性能
boardTest
zch 2 months ago
parent 2c9a2377f1
commit 0c7f7b6ff6

@ -5,6 +5,9 @@ import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.os.common.core.page.PageDomain;
import com.os.common.core.page.TableSupport;
import org.checkerframework.checker.units.qual.A;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
@ -38,9 +41,10 @@ public class RecordIotenvInstantController extends BaseController
@GetMapping("/list")
public TableDataInfo list (RecordIotenvInstant recordIotenvInstant)throws ParseException
{
startPage();
List<RecordIotenvInstant> list = recordIotenvInstantService.selectRecordIotenvInstantList(recordIotenvInstant);
return getDataTable(list);
PageDomain pageDomain = TableSupport.buildPageRequest();
Integer pageNum = pageDomain.getPageNum();
Integer pageSize = pageDomain.getPageSize();
return recordIotenvInstantService.selectRecordIotenvInstantListWithPage(recordIotenvInstant,pageNum,pageSize);
}
@ -101,4 +105,12 @@ public class RecordIotenvInstantController extends BaseController
{
return toAjax(recordIotenvInstantService.deleteRecordIotenvInstantByObjids(objids));
}
@PreAuthorize("@ss.hasPermi('ems/record:recordIotenvInstant:list')")
@GetMapping("/getList")
public AjaxResult getList (RecordIotenvInstant recordIotenvInstant)throws ParseException
{
List<RecordIotenvInstant> recordIotenvInstants = recordIotenvInstantService.selectRecordIotenvInstantList(recordIotenvInstant);
return success(recordIotenvInstants);
}
}

@ -2,7 +2,11 @@ package com.os.ems.record.domain;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.os.common.annotation.DataSource;
import lombok.Data;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.os.common.annotation.Excel;
@ -14,6 +18,7 @@ import com.os.common.core.domain.BaseEntity;
* @author zch
* @date 2025-04-28
*/
@Data
public class RecordIotenvInstant extends BaseEntity
{
private static final long serialVersionUID = 1L;
@ -71,6 +76,9 @@ public class RecordIotenvInstant extends BaseEntity
@Excel(name = "记录时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss")
private Date recodeTime;
private String[] monitorIds;
public void setObjid(Long objid)
{
this.objid = objid;

@ -1,5 +1,6 @@
package com.os.ems.record.mapper;
import java.text.ParseException;
import java.util.List;
import java.util.Map;
@ -83,6 +84,35 @@ public interface RecordIotenvInstantMapper
List<RecordIotenvInstant> selectRecordIotenvInstantListFromTables(@Param("tableNames") List<String> tableNames,
@Param("recordIotenvInstant") RecordIotenvInstant recordIotenvInstant);
/**
*
*
* @param recordIotenvInstant
* @return
*/
List<RecordIotenvInstant> selectFromTablesWithPage(@Param("tableNames") List<String> tableNames,
@Param("recordIotenvInstant") RecordIotenvInstant recordIotenvInstant,
@Param("offset") int offset,
@Param("pageSize") int pageSize);
/**
*
*
* @param params
* @return
*/
Integer checkTableExists(Map<String, Object> params);
/**
*
*
* @param tableNames
* @param recordIotenvInstant
* @return
*/
int countFromTables(@Param("tableNames") List<String> tableNames, @Param("recordIotenvInstant") RecordIotenvInstant recordIotenvInstant);
}

@ -3,6 +3,8 @@ package com.os.ems.record.service;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import com.os.common.core.page.TableDataInfo;
import com.os.ems.record.domain.RecordIotenvInstant;
/**
@ -61,4 +63,14 @@ public interface IRecordIotenvInstantService
* @return
*/
public int deleteRecordIotenvInstantByObjid(Long objid);
/**
*
*
* @param recordIotenvInstant
* @return
*/
public TableDataInfo selectRecordIotenvInstantListWithPage(RecordIotenvInstant recordIotenvInstant, int pageNum, int pageSize) throws ParseException;
}

@ -3,8 +3,15 @@ package com.os.ems.record.service.impl;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import com.github.pagehelper.PageInfo;
import com.os.common.constant.HttpStatus;
import com.os.common.core.page.TableDataInfo;
import com.os.common.exception.ServiceException;
import com.os.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.os.ems.record.mapper.RecordIotenvInstantMapper;
@ -49,13 +56,11 @@ public class RecordIotenvInstantServiceImpl implements IRecordIotenvInstantServi
// }
@Override
public List<RecordIotenvInstant> selectRecordIotenvInstantList(RecordIotenvInstant recordIotenvInstant) throws ParseException {
Map<String, Object> params = recordIotenvInstant.getParams();
// 检查是否有时间范围参数
// 获取开始和结束时间
String beginTimeStr = params.get("beginRecordTime").toString();
String endTimeStr = params.get("endRecordTime").toString();
// 解析日期
SimpleDateFormat fullFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date beginDate = fullFormat.parse(beginTimeStr);
@ -135,7 +140,6 @@ public class RecordIotenvInstantServiceImpl implements IRecordIotenvInstantServi
// 设置日期格式
SimpleDateFormat tableFormat = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
// 获取开始日期和结束日期(不含时间部分)
String beginDateStr = dateFormat.format(beginDate);
String endDateStr = dateFormat.format(endDate);
@ -163,6 +167,52 @@ public class RecordIotenvInstantServiceImpl implements IRecordIotenvInstantServi
}
/**
*
*
* @param recordIotenvInstant
* @return
*/
@Override
public TableDataInfo selectRecordIotenvInstantListWithPage(RecordIotenvInstant recordIotenvInstant, int pageNum, int pageSize) throws ParseException {
Map<String, Object> params = recordIotenvInstant.getParams();
// 检查是否有时间范围参数
// 获取开始和结束时间
String beginTimeStr = params.get("beginRecordTime").toString();
String endTimeStr = params.get("endRecordTime").toString();
// 解析日期
SimpleDateFormat fullFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date beginDate = fullFormat.parse(beginTimeStr);
Date endDate = fullFormat.parse(endTimeStr);
// 获取需要查询的表名列表
List<String> tableNames = getTableNamesByDateRange(beginDate, endDate);
// 如果没有需要查询的表,则返回空结果
if (tableNames.isEmpty()) {
TableDataInfo rspData = new TableDataInfo();
rspData.setCode(0);
rspData.setRows(new ArrayList<>());
rspData.setTotal(0);
return rspData;
}
// 先查询总记录数
int total = recordIotenvInstantMapper.countFromTables(tableNames, recordIotenvInstant);
// 计算分页偏移量
int offset = (pageNum - 1) * pageSize;
// 执行分页查询
List<RecordIotenvInstant> list = recordIotenvInstantMapper.selectFromTablesWithPage(tableNames, recordIotenvInstant, offset, pageSize);
TableDataInfo rspData = new TableDataInfo();
rspData.setCode(HttpStatus.SUCCESS);
rspData.setMsg("查询成功");
rspData.setRows(list);
rspData.setTotal(total);
return rspData;
}
/**
*
*
@ -177,4 +227,59 @@ public class RecordIotenvInstantServiceImpl implements IRecordIotenvInstantServi
return count != null && count > 0;
}
/**
* 线
* @param tableNames
* @param recordIotenvInstant
* @param monitorIds ID
* @return
*/
private List<RecordIotenvInstant> selectMultipleMonitorData(List<String> tableNames, RecordIotenvInstant recordIotenvInstant,String[] monitorIds) {
// 创建线程池
int threadCount = Math.min(monitorIds.length,20);
ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
//创建任务列表
List<Future<List<RecordIotenvInstant>>> futures = new ArrayList<>();
// 为每个设备创建查询任务
for(String monitorId : monitorIds){
Future<List<RecordIotenvInstant>> future
= executorService.submit(() -> {
// 创建新的查询对象,避免并发修改
RecordIotenvInstant query = new RecordIotenvInstant();
BeanUtils.copyProperties(recordIotenvInstant, query);
query.setMonitorId(monitorId);
query.setMonitorIds(null); // 清空数组使用单个ID查询
// 执行查询
return recordIotenvInstantMapper
.selectRecordIotenvInstantListFromTables(tableNames, query);
});
futures.add(future);
}
// 收集所有查询结果
List<RecordIotenvInstant> result = new ArrayList<>();
for (Future<List<RecordIotenvInstant>> future : futures) {
try {
List<RecordIotenvInstant> partialResult = future.get();
if (partialResult != null && !partialResult.isEmpty()) {
result.addAll(partialResult);
}
} catch (Exception e) {
// 记录异常但继续处理其他结果
e.printStackTrace();
}
}
// 关闭线程池
executorService.shutdown();
return result;
}
}

@ -150,6 +150,14 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<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>-->
</where>
</foreach>
ORDER BY recodeTime ASC
@ -161,4 +169,71 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
WHERE table_schema = DATABASE() AND table_name = #{tableName}
</select>
<!-- 使用 UNION ALL 从多个表查询物联网数据列表 ,具有分页功能-->
<select id="selectFromTablesWithPage" resultMap="RecordIotenvInstantResult">
<foreach collection="tableNames" item="tableName" separator=" UNION ALL ">
SELECT objid, monitorId, temperature, humidity, illuminance, noise, concentration,
vibration_speed, vibration_displacement, vibration_acceleration, vibration_temp,
collectTime, recodeTime
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.monitorIds != null and recordIotenvInstant.monitorIds.length > 0">
AND monitorId IN
<foreach collection="recordIotenvInstant.monitorIds" item="monitorId" open="(" separator="," close=")">
#{monitorId}
</foreach>
</if>
</where>
</foreach>
ORDER BY recodeTime ASC
LIMIT #{offset}, #{pageSize}
</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>
<if test="recordIotenvInstant.params.beginRecordTime!= null and recordIotenvInstant.params.endRecordTime != null">
AND recodeTime BETWEEN #{recordIotenvInstant.params.beginRecordTime} AND #{recordIotenvInstant.params.endRecordTime}
</if>
</where>
</foreach>
) AS total_count
</select>
</mapper>
Loading…
Cancel
Save