feat(ems): (第三版)优化物联网数据查询功能

- 添加新的分页查询方法,提高查询效率
- 增加 monitorIds 参数的处理,支持多设备查询
- 优化查询条件,提高查询灵活性
- 调整排序方式,按 recodeTime 降序排列
boardTest
zch 2 months ago
parent bc6b241dcb
commit 5e3dfbdee1

@ -151,26 +151,22 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
AND recodeTime BETWEEN #{recordIotenvInstant.params.beginRecordTime} AND #{recordIotenvInstant.params.endRecordTime} AND recodeTime BETWEEN #{recordIotenvInstant.params.beginRecordTime} AND #{recordIotenvInstant.params.endRecordTime}
</if> </if>
<!-- <if test="recordIotenvInstant.monitorIds != null and recordIotenvInstant.monitorIds.length > 0"> <if test="recordIotenvInstant.monitorIds != null and recordIotenvInstant.monitorIds.length > 0">
AND monitorId IN AND monitorId IN
<foreach collection="recordIotenvInstant.monitorIds" item="monitorId" open="(" separator="," close=")"> <foreach collection="recordIotenvInstant.monitorIds" item="monitorId" open="(" separator="," close=")">
#{monitorId} #{monitorId}
</foreach> </foreach>
</if>--> </if>
</where> </where>
</foreach> </foreach>
ORDER BY recodeTime ASC ORDER BY recodeTime desc
</select> </select>
<!-- 检查表是否存在 -->
<select id="checkTableExists" parameterType="java.util.Map" resultType="java.lang.Integer">
SELECT COUNT(1) FROM information_schema.tables
WHERE table_schema = DATABASE() AND table_name = #{tableName}
</select>
<!-- 使用 UNION ALL 从多个表查询物联网数据列表 ,具有分页功能--> <!-- 待测试limit测试一 使用 UNION ALL 从多个表查询物联网数据列表 ,具有分页功能(可能会在查询完所有数据后才limit需测试)-->
<select id="selectFromTablesWithPage" resultMap="RecordIotenvInstantResult"> <!-- <select id="selectFromTablesWithPage" resultMap="RecordIotenvInstantResult">
select * from (
<foreach collection="tableNames" item="tableName" separator=" UNION ALL "> <foreach collection="tableNames" item="tableName" separator=" UNION ALL ">
SELECT objid, monitorId, temperature, humidity, illuminance, noise, concentration, SELECT objid, monitorId, temperature, humidity, illuminance, noise, concentration,
vibration_speed, vibration_displacement, vibration_acceleration, vibration_temp, vibration_speed, vibration_displacement, vibration_acceleration, vibration_temp,
@ -203,11 +199,62 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</where> </where>
</foreach> </foreach>
ORDER BY recodeTime ASC )AS combined_data
ORDER BY recodeTime desc
LIMIT #{offset}, #{pageSize} LIMIT #{offset}, #{pageSize}
</select> </select> -->
<!-- 测试二: 使用 UNION ALL 从多个表查询物联网数据列表 ,具有分页功能-->
<select id="selectFromTablesWithPage" resultMap="RecordIotenvInstantResult">
SELECT * FROM (
SELECT *, ROW_NUMBER() OVER (ORDER BY recodeTime DESC) as row_num
FROM (
<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>
ORDER BY objid DESC)
</foreach>
) AS all_data
) AS paged_data
WHERE row_num &gt; #{offset} AND row_num &lt;= (#{offset} + #{pageSize})
</select>
<!-- 检查表是否存在 -->
<select id="checkTableExists" parameterType="java.util.Map" resultType="java.lang.Integer">
SELECT COUNT(1) FROM information_schema.tables
WHERE table_schema = DATABASE() AND table_name = #{tableName}
</select>
<!-- 统计多表查询的总记录数 --> <!-- 统计多表查询的总记录数 -->
<select id="countFromTables" resultType="java.lang.Integer"> <select id="countFromTables" resultType="java.lang.Integer">
SELECT COUNT(*) FROM ( SELECT COUNT(*) FROM (
@ -231,6 +278,14 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="recordIotenvInstant.params.beginRecordTime!= null and recordIotenvInstant.params.endRecordTime != null"> <if test="recordIotenvInstant.params.beginRecordTime!= null and recordIotenvInstant.params.endRecordTime != null">
AND recodeTime BETWEEN #{recordIotenvInstant.params.beginRecordTime} AND #{recordIotenvInstant.params.endRecordTime} AND recodeTime BETWEEN #{recordIotenvInstant.params.beginRecordTime} AND #{recordIotenvInstant.params.endRecordTime}
</if> </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> </where>
</foreach> </foreach>
) AS total_count ) AS total_count

Loading…
Cancel
Save