|
|
|
@ -5,6 +5,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper; |
|
|
|
|
import com.mh.common.core.domain.dto.HouseGroupDTO; |
|
|
|
|
import com.mh.common.core.domain.dto.IndoorTempDTO; |
|
|
|
|
import com.mh.common.core.domain.dto.TempHisRequest; |
|
|
|
|
import com.mh.common.core.domain.dto.TempHumidityDTO; |
|
|
|
|
import com.mh.common.core.domain.entity.ChillersEntity; |
|
|
|
|
import com.mh.common.core.domain.vo.IndoorTempVO; |
|
|
|
@ -45,8 +46,12 @@ public class IndoorTempMonitorServiceImpl implements IIndoorTempMonitorService {
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public List<HouseGroupDTO> getIndoorTempByFloor(String systemType, String floorId) throws JsonProcessingException { |
|
|
|
|
// 查询采集类型是温度的参数,采集参数是99
|
|
|
|
|
// 查询采集类型是温度的参数,采集参数是12
|
|
|
|
|
List<TempHumidityDTO> collectionParamsManages = collectionParamsManageMapper.selectByParamType(systemType, floorId, "12"); |
|
|
|
|
// 湿度采集参数:32
|
|
|
|
|
List<TempHumidityDTO> humidityDTOS = collectionParamsManageMapper.selectByParamType(systemType, floorId, "32"); |
|
|
|
|
// 合并collectionParamsManages和humidityDTOS
|
|
|
|
|
collectionParamsManages.addAll(humidityDTOS); |
|
|
|
|
return convert(collectionParamsManages); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -72,8 +77,10 @@ public class IndoorTempMonitorServiceImpl implements IIndoorTempMonitorService {
|
|
|
|
|
if (list.size() > 1) { |
|
|
|
|
log.warn("Duplicate entries found for key: {}|{}", list.get(0).getHouseId(), list.get(0).getHouseName()); |
|
|
|
|
} |
|
|
|
|
HouseGroupDTO group = new HouseGroupDTO(list.get(0)); |
|
|
|
|
list.forEach(group::addDevice); |
|
|
|
|
HouseGroupDTO group = new HouseGroupDTO(list.getFirst()); |
|
|
|
|
for (TempHumidityDTO data : list) { |
|
|
|
|
group.addDevice(data); |
|
|
|
|
} |
|
|
|
|
return group; |
|
|
|
|
} |
|
|
|
|
) |
|
|
|
@ -89,74 +96,132 @@ public class IndoorTempMonitorServiceImpl implements IIndoorTempMonitorService {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public List<IndoorTempDTO> getIndoorTempHisByHouseId(String systemType, List<String> houseIds) { |
|
|
|
|
// 1. 使用Java 8时间API替代字符串截取
|
|
|
|
|
LocalDateTime now = LocalDateTime.now(); |
|
|
|
|
String tableName = "chillers_data_min" + now.getYear(); // 直接获取年份更安全
|
|
|
|
|
|
|
|
|
|
// 2. 增强SQL查询参数校验
|
|
|
|
|
public List<IndoorTempDTO> getIndoorTempHisByHouseId(TempHisRequest tempHisRequest) { |
|
|
|
|
List<String> houseIds = tempHisRequest.getHouseIds(); |
|
|
|
|
String systemType = tempHisRequest.getSystemType(); |
|
|
|
|
String startTime = tempHisRequest.getStartTime(); |
|
|
|
|
String endTime = tempHisRequest.getEndTime(); |
|
|
|
|
// 1. 参数校验
|
|
|
|
|
if (houseIds.isEmpty()) { |
|
|
|
|
log.warn("无效房间ID: {}", houseIds); |
|
|
|
|
return Collections.emptyList(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 3. 使用更明确的集合类型声明
|
|
|
|
|
List<Map<String, String>> cpmIdList = collectionParamsManageMapper.selectByHouseIdAndMtType( |
|
|
|
|
houseIds, |
|
|
|
|
"7", // 使用枚举常量
|
|
|
|
|
"12" |
|
|
|
|
); |
|
|
|
|
// 2. 准备基础数据
|
|
|
|
|
String tableName = "chillers_data_min" + LocalDateTime.now().getYear(); |
|
|
|
|
|
|
|
|
|
// cpmIdList用stream houseId分组遍历得出houseId,houseName, orderNum先赋值给result;
|
|
|
|
|
// 使用Stream分组映射
|
|
|
|
|
Map<String, Object> houseMapping = cpmIdList.stream() |
|
|
|
|
.filter(map -> map.containsKey("houseId") && map.containsKey("houseName")) |
|
|
|
|
.collect(Collectors.toMap( |
|
|
|
|
map -> map.get("houseId"), // 键:houseId
|
|
|
|
|
map -> map.get("houseName"), // 值:houseName
|
|
|
|
|
(oldVal, newVal) -> oldVal // 重复键处理:保留首次出现的值
|
|
|
|
|
)); |
|
|
|
|
// 3. 获取温度数据映射
|
|
|
|
|
Map<String, HouseData> tempData = getHouseDataMapping(houseIds, systemType, "8", "12"); |
|
|
|
|
Map<String, HouseData> humidityData = getHouseDataMapping(houseIds, systemType, "8", "32"); |
|
|
|
|
|
|
|
|
|
// 4. 构建结果
|
|
|
|
|
return houseIds.stream() |
|
|
|
|
.map(houseId -> buildIndoorTempDTO( |
|
|
|
|
houseId, |
|
|
|
|
tableName, |
|
|
|
|
tempData.getOrDefault(houseId, HouseData.EMPTY), |
|
|
|
|
humidityData.getOrDefault(houseId, HouseData.EMPTY), |
|
|
|
|
startTime, |
|
|
|
|
endTime |
|
|
|
|
)) |
|
|
|
|
.collect(Collectors.toList()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Map<String, Object> houseOrderMapping = cpmIdList.stream() |
|
|
|
|
.filter(map -> map.containsKey("houseId") && map.containsKey("houseName")) |
|
|
|
|
// 辅助类封装房屋数据
|
|
|
|
|
private static class HouseData { |
|
|
|
|
static final HouseData EMPTY = new HouseData("未知房间", "0", Collections.emptyList()); |
|
|
|
|
|
|
|
|
|
final String houseName; |
|
|
|
|
final String orderNum; |
|
|
|
|
final List<String> cpmIds; |
|
|
|
|
|
|
|
|
|
HouseData(String houseName, String orderNum, List<String> cpmIds) { |
|
|
|
|
this.houseName = houseName; |
|
|
|
|
this.orderNum = orderNum; |
|
|
|
|
this.cpmIds = cpmIds; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 获取房屋数据映射
|
|
|
|
|
private Map<String, HouseData> getHouseDataMapping(List<String> houseIds, String systemType, String mtType, String paramType) { |
|
|
|
|
List<Map<String, String>> rawData = collectionParamsManageMapper.selectByHouseIdAndMtType(houseIds, systemType, mtType, paramType); |
|
|
|
|
|
|
|
|
|
return rawData.stream() |
|
|
|
|
.filter(map -> map.containsKey("houseId") && map.containsKey("houseName") && map.containsKey("orderNum")) |
|
|
|
|
.collect(Collectors.toMap( |
|
|
|
|
map -> map.get("houseId"), // 键:houseId
|
|
|
|
|
map -> map.get("orderNum"), // 值:houseName
|
|
|
|
|
(oldVal, newVal) -> oldVal // 重复键处理:保留首次出现的值
|
|
|
|
|
// 确保 houseId 转换为 String
|
|
|
|
|
map -> String.valueOf(map.get("houseId")), |
|
|
|
|
map -> new HouseData( |
|
|
|
|
// 确保 houseName 转换为 String
|
|
|
|
|
String.valueOf(map.get("houseName")), |
|
|
|
|
// 确保 orderNum 转换为 String
|
|
|
|
|
String.valueOf(map.get("orderNum")), |
|
|
|
|
rawData.stream() |
|
|
|
|
.filter(m -> String.valueOf(m.get("houseId")).equals(String.valueOf(map.get("houseId")))) |
|
|
|
|
.map(m -> String.valueOf(m.get("cpmId"))) |
|
|
|
|
.collect(Collectors.toList()) |
|
|
|
|
), |
|
|
|
|
(oldVal, newVal) -> oldVal |
|
|
|
|
)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 构建结果集合
|
|
|
|
|
return houseIds.stream() |
|
|
|
|
.map(houseId -> { |
|
|
|
|
// 根据houseId查询对应的cmpList
|
|
|
|
|
List<String> cpmData = cpmIdList.stream() |
|
|
|
|
.filter(map -> houseId.equals(map.get("houseId"))) |
|
|
|
|
.map(map -> map.get("cpmId")) |
|
|
|
|
.collect(Collectors.toList()); |
|
|
|
|
List<IndoorTempVO> rawData = dataProcessMapper.selectHisDataByCodeAndRegisterAddr(tableName, cpmData); |
|
|
|
|
// 4. 优化聚合计算(单次Stream完成所有操作)
|
|
|
|
|
List<IndoorTempVO> collect = rawData.stream() |
|
|
|
|
// .filter(vo -> isValidTimeFormat(vo.getCurTime())) // 格式校验
|
|
|
|
|
.collect(Collectors.groupingBy( |
|
|
|
|
IndoorTempVO::getCurTime, |
|
|
|
|
() -> new TreeMap<>(this::compareTimeString), // 自定义排序
|
|
|
|
|
Collectors.collectingAndThen( |
|
|
|
|
Collectors.toList(), |
|
|
|
|
this::calculateAverage |
|
|
|
|
) |
|
|
|
|
)) |
|
|
|
|
.entrySet().stream() |
|
|
|
|
.map(entry -> buildResultVO(entry.getKey(), entry.getValue())) |
|
|
|
|
.toList(); |
|
|
|
|
return new IndoorTempDTO( |
|
|
|
|
Integer.parseInt(String.valueOf(houseOrderMapping.getOrDefault(houseId, 0))), // 默认值兜底
|
|
|
|
|
houseId, |
|
|
|
|
String.valueOf(houseMapping.getOrDefault(houseId, "未知房间")), // 默认值兜底
|
|
|
|
|
collect); // 初始化数据列表
|
|
|
|
|
// 构建单个房屋的DTO
|
|
|
|
|
private IndoorTempDTO buildIndoorTempDTO(String houseId, String tableName, HouseData tempData, HouseData humidityData, String startTime, String endTime) { |
|
|
|
|
List<IndoorTempVO> tempRecords = getProcessedData(tableName, tempData.cpmIds, startTime, endTime); |
|
|
|
|
List<IndoorTempVO> humidityRecords = getProcessedData(tableName, humidityData.cpmIds, startTime, endTime); |
|
|
|
|
|
|
|
|
|
List<IndoorTempVO> mergedData = mergeTempAndHumidity(tempRecords, humidityRecords); |
|
|
|
|
|
|
|
|
|
return new IndoorTempDTO( |
|
|
|
|
Integer.parseInt(tempData.orderNum), |
|
|
|
|
houseId, |
|
|
|
|
tempData.houseName, |
|
|
|
|
mergedData |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 获取处理后的数据
|
|
|
|
|
private List<IndoorTempVO> getProcessedData(String tableName, List<String> cpmIds, String startTime, String endTime) { |
|
|
|
|
if (cpmIds.isEmpty()) { |
|
|
|
|
return Collections.emptyList(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
List<IndoorTempVO> rawData = dataProcessMapper.selectHisDataByCodeAndRegisterAddr(tableName, cpmIds, startTime, endTime); |
|
|
|
|
|
|
|
|
|
return rawData.stream() |
|
|
|
|
.collect(Collectors.groupingBy( |
|
|
|
|
IndoorTempVO::getCurTime, |
|
|
|
|
() -> new TreeMap<>(this::compareTimeString), |
|
|
|
|
Collectors.collectingAndThen( |
|
|
|
|
Collectors.toList(), |
|
|
|
|
this::calculateAverage |
|
|
|
|
) |
|
|
|
|
)) |
|
|
|
|
.entrySet().stream() |
|
|
|
|
.map(entry -> buildResultVO(entry.getKey(), entry.getValue())) |
|
|
|
|
.collect(Collectors.toList()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 合并温度和湿度数据
|
|
|
|
|
private List<IndoorTempVO> mergeTempAndHumidity(List<IndoorTempVO> tempData, List<IndoorTempVO> humidityData) { |
|
|
|
|
return humidityData.stream() |
|
|
|
|
.filter(humidity -> tempData.stream().anyMatch(temp -> temp.getCurTime().equals(humidity.getCurTime()))) |
|
|
|
|
.map(humidity -> { |
|
|
|
|
IndoorTempVO temp = tempData.stream() |
|
|
|
|
.filter(t -> t.getCurTime().equals(humidity.getCurTime())) |
|
|
|
|
.findFirst() |
|
|
|
|
.orElseThrow(); |
|
|
|
|
|
|
|
|
|
return new IndoorTempVO( |
|
|
|
|
humidity.getCurTime(), |
|
|
|
|
temp.getIndoorTemp().setScale(1, RoundingMode.HALF_UP), |
|
|
|
|
humidity.getIndoorTemp().setScale(1, RoundingMode.HALF_UP) |
|
|
|
|
); |
|
|
|
|
}) |
|
|
|
|
.collect(Collectors.toList()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 辅助方法:时间格式校验
|
|
|
|
|
private boolean isValidTimeFormat(String time) { |
|
|
|
|
return time != null && time.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}"); |
|
|
|
|