refactor(record): 优化物联网数据查询功能

- 修改了 IRecordIotenvInstantService 接口,增加 ParseException 异常
- 更新了 RecordIotenvInstantController 中的 list 和 export 方法,添加异常处理
- 修改了 RecordIotenvInstantMapper 接口中的方法参数名
- 重构了 RecordIotenvInstantServiceImpl 中的 selectRecordIotenvInstantList 方法:
  - 移除了对时间范围参数的冗余判断
  - 优化了分表查询逻辑
  - 改进了异常处理方式
- 更新了 getTableNamesByDateRange 和 isTableExists 方法,添加异常处理
boardTest
zch 4 months ago
parent 3095d43854
commit 2c9a2377f1

@ -1,5 +1,6 @@
package com.os.ems.record.controller;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
@ -35,10 +36,9 @@ public class RecordIotenvInstantController extends BaseController
*/
@PreAuthorize("@ss.hasPermi('ems/record:recordIotenvInstant:list')")
@GetMapping("/list")
public TableDataInfo list(RecordIotenvInstant recordIotenvInstant)
public TableDataInfo list (RecordIotenvInstant recordIotenvInstant)throws ParseException
{
startPage();
List<RecordIotenvInstant> list = recordIotenvInstantService.selectRecordIotenvInstantList(recordIotenvInstant);
return getDataTable(list);
}
@ -50,7 +50,7 @@ public class RecordIotenvInstantController extends BaseController
@PreAuthorize("@ss.hasPermi('ems/record:recordIotenvInstant:export')")
@Log(title = "物联网数据", businessType = BusinessType.EXPORT)
@PostMapping("/export")
public void export(HttpServletResponse response, RecordIotenvInstant recordIotenvInstant)
public void export(HttpServletResponse response, RecordIotenvInstant recordIotenvInstant)throws ParseException
{
List<RecordIotenvInstant> list = recordIotenvInstantService.selectRecordIotenvInstantList(recordIotenvInstant);
ExcelUtil<RecordIotenvInstant> util = new ExcelUtil<RecordIotenvInstant>(RecordIotenvInstant.class);

@ -81,7 +81,7 @@ public interface RecordIotenvInstantMapper
* @return
*/
List<RecordIotenvInstant> selectRecordIotenvInstantListFromTables(@Param("tableNames") List<String> tableNames,
@Param("record") RecordIotenvInstant recordIotenvInstant);
@Param("recordIotenvInstant") RecordIotenvInstant recordIotenvInstant);
Integer checkTableExists(Map<String, Object> params);

@ -1,5 +1,6 @@
package com.os.ems.record.service;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import com.os.ems.record.domain.RecordIotenvInstant;
@ -26,7 +27,7 @@ public interface IRecordIotenvInstantService
* @param recordIotenvInstant
* @return
*/
public List<RecordIotenvInstant> selectRecordIotenvInstantList(RecordIotenvInstant recordIotenvInstant);
public List<RecordIotenvInstant> selectRecordIotenvInstantList(RecordIotenvInstant recordIotenvInstant) throws ParseException;
/**

@ -1,5 +1,6 @@
package com.os.ems.record.service.impl;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
@ -47,12 +48,10 @@ public class RecordIotenvInstantServiceImpl implements IRecordIotenvInstantServi
// return recordIotenvInstantMapper.selectRecordIotenvInstantList(recordIotenvInstant);
// }
@Override
public List<RecordIotenvInstant> selectRecordIotenvInstantList(RecordIotenvInstant recordIotenvInstant) {
public List<RecordIotenvInstant> selectRecordIotenvInstantList(RecordIotenvInstant recordIotenvInstant) throws ParseException {
Map<String, Object> params = recordIotenvInstant.getParams();
// 检查是否有时间范围参数
if (params != null && params.containsKey("beginRecordTime") && params.containsKey("endRecordTime")) {
try {
// 获取开始和结束时间
String beginTimeStr = params.get("beginRecordTime").toString();
String endTimeStr = params.get("endRecordTime").toString();
@ -67,16 +66,8 @@ public class RecordIotenvInstantServiceImpl implements IRecordIotenvInstantServi
if (tableNames.isEmpty()) {
return new ArrayList<>();
}
// 执行分表查询
return recordIotenvInstantMapper.selectRecordIotenvInstantListFromTables(tableNames, recordIotenvInstant);
} catch (Exception e) {
e.printStackTrace();
}
}
// 如果没有时间范围或发生异常,则使用原始查询方法
return recordIotenvInstantMapper.selectRecordIotenvInstantList(recordIotenvInstant);
}
/**
@ -135,7 +126,7 @@ public class RecordIotenvInstantServiceImpl implements IRecordIotenvInstantServi
* @param endDate
* @return
*/
private List<String> getTableNamesByDateRange(Date beginDate, Date endDate) {
private List<String> getTableNamesByDateRange(Date beginDate, Date endDate) throws ParseException {
List<String> tableNames = new ArrayList<>();
// 如果开始日期晚于结束日期,则返回空列表
if (beginDate.after(endDate)) {
@ -144,7 +135,7 @@ public class RecordIotenvInstantServiceImpl implements IRecordIotenvInstantServi
// 设置日期格式
SimpleDateFormat tableFormat = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
// 获取开始日期和结束日期(不含时间部分)
String beginDateStr = dateFormat.format(beginDate);
String endDateStr = dateFormat.format(endDate);
@ -164,12 +155,10 @@ public class RecordIotenvInstantServiceImpl implements IRecordIotenvInstantServi
}
for (String tableName : tableNames){
if (!isTableExists(tableName)){
throw new ServiceException(tableName + "分表不存在");
throw new ServiceException(tableName + "分表不存在,未记录数据");
}
}
} catch (Exception e) {
e.printStackTrace();
}
return tableNames;
}
@ -181,14 +170,11 @@ public class RecordIotenvInstantServiceImpl implements IRecordIotenvInstantServi
* @return
*/
private boolean isTableExists(String tableName) {
try {
// 查询数据库中是否存在该表
Map<String, Object> params = new HashMap<>();
params.put("tableName", tableName);
Integer count = recordIotenvInstantMapper.checkTableExists(params);
return count != null && count > 0;
} catch (Exception e) {
return false;
}
}
}

Loading…
Cancel
Save