返回对象实体类
@Getter
@Setter
public class CostDTO {
private String yearMonth;
private Double cost;
private Double costRate;
}
实现类业务代码
DateTimeFormatter timeFormatter = DateTimeFormatter.ISO_LOCAL_DATE;
if (StringUtils.isBlank(qqlVehicleMaintenanceStatisticsVo.getStartTime()) || StringUtils.isBlank(qqlVehicleMaintenanceStatisticsVo.getEndTime())) {
LocalDate localDate = LocalDate.now();
qqlVehicleMaintenanceStatisticsVo.setStartTime(localDate.minusYears(2).plusMonths(1).format(timeFormatter));
qqlVehicleMaintenanceStatisticsVo.setEndTime(localDate.format(timeFormatter));
}else{
qqlVehicleMaintenanceStatisticsVo.setStartTime(LocalDate.parse(qqlVehicleMaintenanceStatisticsVo.getStartTime(),timeFormatter).minusYears(1).format(timeFormatter));
}
List<CostDTO> maintenanceCostDTOList = this.baseMapper.listMaintenanceCost(qqlVehicleMaintenanceStatisticsVo);
List<CostDTO> oilCostDTOList = this.baseMapper.listOilCost(qqlVehicleMaintenanceStatisticsVo);
List<CostDTO> otherCostDTOList = this.baseMapper.listOtherCost(qqlVehicleMaintenanceStatisticsVo);
List<String> formatTime = generateFormatTime(qqlVehicleMaintenanceStatisticsVo.getStartTime(), qqlVehicleMaintenanceStatisticsVo.getEndTime());
addLackDataCost(maintenanceCostDTOList, formatTime);
addLackDataCost(oilCostDTOList, formatTime);
addLackDataCost(otherCostDTOList, formatTime);
Map<String, DoubleSummaryStatistics> computeCostByYearMonth = computeCostByYearMonth(maintenanceCostDTOList, oilCostDTOList, otherCostDTOList);
Iterator<Map.Entry<String, DoubleSummaryStatistics>> it = computeCostByYearMonth.entrySet().iterator();
List<CostDTO> costDTOS = new ArrayList<CostDTO>();
CostDTO costDTO = null;
while (it.hasNext()) {
Map.Entry<String, DoubleSummaryStatistics> entry = it.next();
String key = entry.getKey();
double cost = entry.getValue().getSum();
costDTO = new CostDTO();
costDTO.setYearMonth(key);
costDTO.setCost(cost);
costDTOS.add(costDTO);
}
List<CostDTO> dtoList = costDTOS.stream().sorted(Comparator.comparing(CostDTO::getYearMonth)).collect(Collectors.toList());
List<CostDTO> previousYearCostDTOS = new ArrayList<CostDTO>();
for (int i = 0, j = 11; i <= j; i++) {
previousYearCostDTOS.add(dtoList.get(i));
}
List<CostDTO> currentYearCostDTOS = dtoList.stream().filter(item -> !previousYearCostDTOS.contains(item)).collect(Collectors.toList());
for (int i = 0, j = currentYearCostDTOS.size(); i < j; i++) {
CostDTO dto = currentYearCostDTOS.get(i);
Double cost = previousYearCostDTOS.get(i).getCost();
if (cost != 0) {
dto.setCostRate(DoubleUtil.div(dto.getCost() - cost, cost, 4) * 100);
} else {
dto.setCostRate(0D);
}
}
CostRateVO costRateVO = new CostRateVO();
costRateVO.setCurrentYearCostDTOS(currentYearCostDTOS);
costRateVO.setMaintenanceCostDTOList(maintenanceCostDTOList.stream().sorted(Comparator.comparing(CostDTO::getYearMonth)).skip(12).collect(Collectors.toList()));
costRateVO.setOilCostDTOList(oilCostDTOList.stream().sorted(Comparator.comparing(CostDTO::getYearMonth)).skip(12).collect(Collectors.toList()));
costRateVO.setOtherCostDTOList(otherCostDTOList.stream().sorted(Comparator.comparing(CostDTO::getYearMonth)).skip(12).collect(Collectors.toList()));
实现类private 方法
private Map<String, DoubleSummaryStatistics> computeCostByYearMonth(List<CostDTO> maintenanceCostDTOList, List<CostDTO> costDTOList, List<CostDTO> otherCostDTOList) {
List<CostDTO> costDTOS = new ArrayList<>();
costDTOS.addAll(maintenanceCostDTOList);
costDTOS.addAll(costDTOList);
costDTOS.addAll(otherCostDTOList);
Map<String, DoubleSummaryStatistics> doubleSummaryStatisticsMap = costDTOS.stream()
.collect(Collectors.groupingBy(CostDTO::getYearMonth, Collectors.summarizingDouble(CostDTO::getCost)));
return doubleSummaryStatisticsMap;
}
private void addLackDataCost(List<CostDTO> costList, List<String> formatTime) {
if (CollectionUtils.isNotEmpty(costList)) {
List<String> mDate = costList.stream().map(CostDTO::getYearMonth).collect(Collectors.toList());
List<String> lackDate = formatTime.stream().filter(item -> !mDate.contains(item)).collect(Collectors.toList());
CostDTO costDTO = null;
for (String lack : lackDate) {
costDTO = new CostDTO();
costDTO.setYearMonth(lack);
costDTO.setCost(0D);
costList.add(costDTO);
}
}
}
private List<String> generateFormatTime(String startTime, String endTime) {
LocalDate startDate = LocalDate.parse(startTime, DateTimeFormatter.ISO_LOCAL_DATE);
LocalDate endDate = LocalDate.parse(endTime, DateTimeFormatter.ISO_LOCAL_DATE);
int startDateYear = startDate.getYear();
int midYear = startDateYear + 1;
int endDateYear = endDate.getYear();
int startDateMonthValue = startDate.getMonthValue();
int endDateMonthValue = endDate.getMonthValue();
List<String> strDateList = new ArrayList<>();
int ten = 10;
int twelve = 12;
if (startDateMonthValue < ten) {
for (int i = startDateMonthValue, j = ten; i < j; i++) {
strDateList.add(startDateYear + "-" + "0" + i);
}
for (int i = ten, j = twelve; i <= j; i++) {
strDateList.add(startDateYear + "-" + i);
}
} else {
for (int i = startDateMonthValue, j = twelve; i <= j; i++) {
strDateList.add(startDateYear + "-" + i);
}
}
for (int i = 1, j = twelve; i <= j; i++) {
if (i < 10) {
strDateList.add(midYear + "-" + "0" + i);
} else {
strDateList.add(midYear + "-" + i);
}
}
for (int i = 1, j = endDateMonthValue; i <= j; i++) {
if (i < 10) {
strDateList.add(endDateYear + "-" + "0" + i);
} else {
strDateList.add(endDateYear + "-" + i);
}
}
return strDateList;
}
mysql查询语句(date_day_time为yyyy-MM-dd格式)
SELECT
DATE_FORMAT( date_day_time, '%Y-%m' ) yearMonth,
ROUND( SUM( maintenance_cost ), 2 ) AS cost
FROM
qql_vehicle_maintenance_maintenance
WHERE
org_id =
AND date_day_time BETWEEN
AND
GROUP BY
yearMonth
SELECT
DATE_FORMAT( date_day_time, '%Y-%m' ) yearMonth,
ROUND( SUM( oil_cost ), 2 ) AS cost
FROM
qql_vehicle_maintenance_oil
WHERE
org_id =
AND date_day_time BETWEEN
AND
GROUP BY
yearMonth
SELECT
DATE_FORMAT( date_day_time, '%Y-%m' ) yearMonth,
ROUND( SUM( other_cost ), 2 ) AS cost
FROM
qql_vehicle_maintenance_other
WHERE
org_id =
AND date_day_time BETWEEN
AND
GROUP BY
yearMonth