|
|
|
@ -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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|