代码优化
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.njcn.csdevice.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.date.DatePattern;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.njcn.csdevice.mapper.CsEquipmentDeliveryMapper;
|
||||
@@ -26,6 +27,7 @@ import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
|
||||
/**
|
||||
@@ -76,14 +78,154 @@ public class InfluxdbCsCommunicateServiceImpl implements ICsCommunicateService {
|
||||
public List<PqsCommunicateDto> getRawData(LineCountEvaluateParam lineParam) {
|
||||
List<PqsCommunicateDto> result = new ArrayList<>();
|
||||
List<PqsCommunicate> list = getPqsCommunicateData(lineParam);
|
||||
list.forEach(item -> {
|
||||
PqsCommunicateDto dto = new PqsCommunicateDto();
|
||||
BeanUtils.copyProperties(item, dto);
|
||||
dto.setTime(DATE_TIME_FORMATTER.format(item.getTime()));
|
||||
result.add(dto);
|
||||
});
|
||||
return result;
|
||||
|
||||
if (CollectionUtil.isNotEmpty(list)) {
|
||||
result = processWithData(list, lineParam);
|
||||
} else {
|
||||
result = processWithoutData(lineParam);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理有数据的情况
|
||||
*/
|
||||
private List<PqsCommunicateDto> processWithData(List<PqsCommunicate> list, LineCountEvaluateParam lineParam) {
|
||||
List<PqsCommunicateDto> result = new ArrayList<>();
|
||||
int lastIndex = list.size() - 1;
|
||||
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
PqsCommunicate item = list.get(i);
|
||||
PqsCommunicateDto dto = convertToDto(item);
|
||||
result.add(dto);
|
||||
|
||||
// 如果是最后一组数据,补充结束数据点
|
||||
if (i == lastIndex) {
|
||||
PqsCommunicateDto endData = createEndDataDto(item, lineParam);
|
||||
result.add(endData);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理无数据的情况
|
||||
*/
|
||||
private List<PqsCommunicateDto> processWithoutData(LineCountEvaluateParam lineParam) {
|
||||
LocalDateTime endTime = getEndDateTime(lineParam.getEndTime());
|
||||
LocalDateTime currentTime = LocalDateTime.now();
|
||||
|
||||
// 判断 endTime 是否早于最早入库时间
|
||||
List<PqsCommunicateDto> firstData = getRawDataFirst(lineParam);
|
||||
if (CollectionUtil.isNotEmpty(firstData)) {
|
||||
LocalDateTime firstTime = parseDateTime(firstData.get(0).getTime());
|
||||
if (endTime.isBefore(firstTime)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
// 判断 startTime 是否晚于当前时间
|
||||
LocalDateTime startTime = getStartDateTime(lineParam.getStartTime());
|
||||
if (startTime.isAfter(currentTime)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
// 获取最新数据并创建起始和结束点
|
||||
List<PqsCommunicateDto> latestData = getRawDataLatest(lineParam);
|
||||
if (CollectionUtil.isEmpty(latestData)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
return createStartAndEndDtos(latestData.get(0), lineParam);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建起始和结束数据点
|
||||
*/
|
||||
private List<PqsCommunicateDto> createStartAndEndDtos(PqsCommunicateDto templateDto, LineCountEvaluateParam lineParam) {
|
||||
List<PqsCommunicateDto> result = new ArrayList<>();
|
||||
|
||||
// 创建起始点
|
||||
PqsCommunicateDto startDto = new PqsCommunicateDto();
|
||||
copyDtoProperties(templateDto, startDto);
|
||||
startDto.setTime(lineParam.getStartTime() + " 00:00:00");
|
||||
result.add(startDto);
|
||||
|
||||
// 创建结束点
|
||||
PqsCommunicateDto endDto = new PqsCommunicateDto();
|
||||
copyDtoProperties(templateDto, endDto);
|
||||
endDto.setTime(getEndTimeString(lineParam));
|
||||
result.add(endDto);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建结束数据点
|
||||
*/
|
||||
private PqsCommunicateDto createEndDataDto(PqsCommunicate item, LineCountEvaluateParam lineParam) {
|
||||
PqsCommunicateDto endData = new PqsCommunicateDto();
|
||||
endData.setTime(getEndTimeString(lineParam));
|
||||
endData.setDevId(lineParam.getLineId().get(0));
|
||||
endData.setType(item.getType());
|
||||
endData.setDescription(item.getDescription());
|
||||
return endData;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换实体为DTO
|
||||
*/
|
||||
private PqsCommunicateDto convertToDto(PqsCommunicate item) {
|
||||
PqsCommunicateDto dto = new PqsCommunicateDto();
|
||||
BeanUtils.copyProperties(item, dto);
|
||||
dto.setTime(DATE_TIME_FORMATTER.format(item.getTime()));
|
||||
return dto;
|
||||
}
|
||||
|
||||
/**
|
||||
* 复制DTO属性
|
||||
*/
|
||||
private void copyDtoProperties(PqsCommunicateDto source, PqsCommunicateDto target) {
|
||||
target.setDevId(source.getDevId());
|
||||
target.setType(source.getType());
|
||||
target.setDescription(source.getDescription());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取结束时间字符串
|
||||
*/
|
||||
private String getEndTimeString(LineCountEvaluateParam lineParam) {
|
||||
LocalDateTime currentTime = LocalDateTime.now();
|
||||
LocalDateTime endTime = getEndDateTime(lineParam.getEndTime());
|
||||
|
||||
if (endTime.isBefore(currentTime)) {
|
||||
return lineParam.getEndTime() + " 23:59:59";
|
||||
} else {
|
||||
return currentTime.format(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析日期时间字符串
|
||||
*/
|
||||
private LocalDateTime parseDateTime(String dateTimeStr) {
|
||||
return LocalDateTime.parse(dateTimeStr, DATE_TIME_FORMATTER);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取开始时间
|
||||
*/
|
||||
private LocalDateTime getStartDateTime(String startDate) {
|
||||
return parseDateTime(startDate + " 00:00:00");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取结束时间
|
||||
*/
|
||||
private LocalDateTime getEndDateTime(String endDate) {
|
||||
return parseDateTime(endDate + " 23:59:59");
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -145,4 +287,24 @@ public class InfluxdbCsCommunicateServiceImpl implements ICsCommunicateService {
|
||||
.timeAsc();
|
||||
return pqsCommunicateMapper.selectByQueryWrapper(influxQueryWrapper);
|
||||
}
|
||||
|
||||
public List<PqsCommunicateDto> getRawDataFirst(LineCountEvaluateParam lineParam) {
|
||||
List<PqsCommunicateDto> result = new ArrayList<>();
|
||||
InfluxQueryWrapper influxQueryWrapper = new InfluxQueryWrapper(PqsCommunicate.class);
|
||||
influxQueryWrapper.regular(PqsCommunicate::getDevId, lineParam.getLineId())
|
||||
.select(PqsCommunicate::getTime)
|
||||
.select(PqsCommunicate::getDevId)
|
||||
.select(PqsCommunicate::getDescription)
|
||||
.select(PqsCommunicate::getType)
|
||||
.timeAsc()
|
||||
.limit(1);
|
||||
List<PqsCommunicate> list = pqsCommunicateMapper.selectByQueryWrapper(influxQueryWrapper);
|
||||
list.forEach(item -> {
|
||||
PqsCommunicateDto dto = new PqsCommunicateDto();
|
||||
BeanUtils.copyProperties(item, dto);
|
||||
dto.setTime(DATE_TIME_FORMATTER.format(item.getTime()));
|
||||
result.add(dto);
|
||||
});
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user