fix(ems): 修复查询时间范围内的数据表

- 更新 RecordIotenvInstantMapper.xml 中的时间范围查询条件
- 优化 RecordIotenvInstantServiceImpl 中的表名获取逻辑
- 将自定义异常改为 ServiceException
boardTest
zch 2 months ago
parent 41672fa3f5
commit 3095d43854

@ -3,6 +3,7 @@ package com.os.ems.record.service.impl;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
import com.os.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.os.ems.record.mapper.RecordIotenvInstantMapper; import com.os.ems.record.mapper.RecordIotenvInstantMapper;
@ -136,16 +137,13 @@ public class RecordIotenvInstantServiceImpl implements IRecordIotenvInstantServi
*/ */
private List<String> getTableNamesByDateRange(Date beginDate, Date endDate) { private List<String> getTableNamesByDateRange(Date beginDate, Date endDate) {
List<String> tableNames = new ArrayList<>(); List<String> tableNames = new ArrayList<>();
// 如果开始日期晚于结束日期,则返回空列表 // 如果开始日期晚于结束日期,则返回空列表
if (beginDate.after(endDate)) { if (beginDate.after(endDate)) {
return tableNames; return tableNames;
} }
// 设置日期格式 // 设置日期格式
SimpleDateFormat tableFormat = new SimpleDateFormat("yyyyMMdd"); SimpleDateFormat tableFormat = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
try { try {
// 获取开始日期和结束日期(不含时间部分) // 获取开始日期和结束日期(不含时间部分)
String beginDateStr = dateFormat.format(beginDate); String beginDateStr = dateFormat.format(beginDate);
@ -153,14 +151,12 @@ public class RecordIotenvInstantServiceImpl implements IRecordIotenvInstantServi
Date currentDate = dateFormat.parse(beginDateStr); Date currentDate = dateFormat.parse(beginDateStr);
Date endDateOnly = dateFormat.parse(endDateStr); Date endDateOnly = dateFormat.parse(endDateStr);
// 循环添加每一天对应的表名 // 循环添加每一天对应的表名
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
while (!currentDate.after(endDateOnly)) { while (!currentDate.after(endDateOnly)) {
String tableSuffix = tableFormat.format(currentDate); String tableSuffix = tableFormat.format(currentDate);
String tableName = "record_iotenv_instant_" + tableSuffix; String tableName = "record_iotenv_instant_" + tableSuffix;
tableNames.add(tableName); tableNames.add(tableName);
// 日期加1天 // 日期加1天
calendar.setTime(currentDate); calendar.setTime(currentDate);
calendar.add(Calendar.DAY_OF_MONTH, 1); calendar.add(Calendar.DAY_OF_MONTH, 1);
@ -168,13 +164,12 @@ public class RecordIotenvInstantServiceImpl implements IRecordIotenvInstantServi
} }
for (String tableName : tableNames){ for (String tableName : tableNames){
if (!isTableExists(tableName)){ if (!isTableExists(tableName)){
throw new SecurityException(tableName + "分表不存在"); throw new ServiceException(tableName + "分表不存在");
} }
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return tableNames; return tableNames;
} }

@ -52,8 +52,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="vibrationTemp != null "> and vibration_temp = #{vibrationTemp}</if> <if test="vibrationTemp != null "> and vibration_temp = #{vibrationTemp}</if>
<if test="collectTime != null "> and collectTime = #{collectTime}</if> <if test="collectTime != null "> and collectTime = #{collectTime}</if>
<if test="recodeTime != null "> and recodeTime = #{recodeTime}</if> <if test="recodeTime != null "> and recodeTime = #{recodeTime}</if>
<if test="beginRecordTime!= null and endRecordTime != null"> <if test="params.beginRecordTime!= null and params.endRecordTime != null">
AND recodeTime BETWEEN #{beginRecordTime} AND #{endRecordTime} AND recodeTime BETWEEN #{params.beginRecordTime} AND #{params.endRecordTime}
</if> </if>
</where> </where>
</select> </select>

Loading…
Cancel
Save