From e914de39532d09184a60fc043f41f5a7e47a38be Mon Sep 17 00:00:00 2001 From: Lemon Date: Fri, 2 Aug 2024 14:08:50 +0800 Subject: [PATCH] =?UTF-8?q?update=20=20=E8=8E=B7=E5=8F=96=E7=AB=99?= =?UTF-8?q?=E7=82=B9=E8=BF=90=E8=90=A5=E5=88=86=E6=9E=90=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=EF=BC=8812=E4=B8=AA=E6=9C=88=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/jsowell/common/util/DateUtils.java | 38 ++++++++++ .../impl/PileStationInfoServiceImpl.java | 71 +++++++++++++++---- .../pile/vo/StationBusinessAnalyzeInfoVO.java | 3 + 3 files changed, 98 insertions(+), 14 deletions(-) diff --git a/jsowell-common/src/main/java/com/jsowell/common/util/DateUtils.java b/jsowell-common/src/main/java/com/jsowell/common/util/DateUtils.java index 6677e0752..ed71e9712 100644 --- a/jsowell-common/src/main/java/com/jsowell/common/util/DateUtils.java +++ b/jsowell-common/src/main/java/com/jsowell/common/util/DateUtils.java @@ -1073,6 +1073,44 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils { return new LocalDateTime[]{startDateTime, endDateTime}; } + /** + * 获取去年这个月第一天的日期 + * @return 去年这个月第一天的日期,格式为 "yyyy-MM-dd" + */ + public static String getFirstDayOfLastYearMonth() { + // 获取当前日期 + LocalDate currentDate = LocalDate.now(); + + // 获取去年这个月的年份 + int lastYear = currentDate.getYear() - 1; + + // 获取这个月的月份 + int currentMonth = currentDate.getMonthValue(); + + // 获取去年这个月第一天的日期 + LocalDate firstDayOfLastYearMonth = LocalDate.of(lastYear, currentMonth, 1); + + // 格式化日期为 "yyyy-MM-dd" + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + return firstDayOfLastYearMonth.format(formatter); + } + + /** + * 获取当前年月最后一天的日期 + * @return 当前年月最后一天的日期,格式为 "yyyy-MM-dd" + */ + public static String getLastDayOfCurrentMonth() { + // 获取当前的年份和月份 + YearMonth currentYearMonth = YearMonth.now(); + + // 获取该年月的最后一天 + LocalDate lastDayOfMonth = currentYearMonth.atEndOfMonth(); + + // 格式化日期为 "yyyy-MM-dd" + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + return lastDayOfMonth.format(formatter); + } + public static void main(String[] args) { String startTime = "22:00:00"; String endTime1 = "05:30:00"; diff --git a/jsowell-pile/src/main/java/com/jsowell/pile/service/impl/PileStationInfoServiceImpl.java b/jsowell-pile/src/main/java/com/jsowell/pile/service/impl/PileStationInfoServiceImpl.java index e149ef420..4010341b2 100644 --- a/jsowell-pile/src/main/java/com/jsowell/pile/service/impl/PileStationInfoServiceImpl.java +++ b/jsowell-pile/src/main/java/com/jsowell/pile/service/impl/PileStationInfoServiceImpl.java @@ -47,6 +47,8 @@ import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.text.DecimalFormat; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; import java.util.*; import java.util.concurrent.TimeUnit; import java.util.function.Function; @@ -956,16 +958,22 @@ public class PileStationInfoServiceImpl implements PileStationInfoService { */ @Override public StationBusinessAnalyzeInfoVO getStationMonthlyBusinessAnalyzeInfo(StationBusinessAnalyzeInfoDTO dto) { + // 获取目标日期(年-月) + String dateTime = dto.getDateTime(); + // 设置开始时间、结束时间 String stationId = dto.getStationId(); - Calendar instance = Calendar.getInstance(); - instance.add(Calendar.YEAR, -1); - String startTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, instance.getTime()); - String endTime = DateUtils.dateTimeNow(DateUtils.YYYY_MM_DD); + String startTime = DateUtils.getLastDayOfCurrentMonth(); // 去年月份第一天 + String endTime = DateUtils.getFirstDayOfLastYearMonth(); // 当前月份最后一天 + // 查询订单日报表过去一年的数据 List list = settleOrderReportService.queryOrderReport(Lists.newArrayList(stationId), startTime, endTime); - // 按照日期汇总数据 Map collect = list.stream() + .peek(report -> { + LocalDate date = LocalDate.parse(report.getTradeDate(), DateTimeFormatter.ofPattern("yyyy-MM-dd")); + String formattedDate = date.format(DateTimeFormatter.ofPattern("yyyy-MM")); + report.setTradeDate(formattedDate); + }) .sorted(Comparator.comparing(SettleOrderReport::getTradeDate)) .collect(Collectors.toMap(SettleOrderReport::getTradeDate, Function.identity(), (a, b) -> { @@ -979,17 +987,52 @@ public class PileStationInfoServiceImpl implements PileStationInfoService { TreeMap map = new TreeMap<>(collect); List settleOrderReports = new ArrayList<>(map.values()); - // StationBusinessAnalyzeInfoVO vo = StationBusinessAnalyzeInfoVO.builder() - // .electricityGrowthRate() - // .orderAmountGrowthRate() - // .serviceAmountGrowthRate() - // - // .businessOrderDetailInfoVOList() - // .build(); + SettleOrderReport nowReportInfo = new SettleOrderReport(); + SettleOrderReport lastMonthReportInfo = new SettleOrderReport(); + if(dateTime == null) { + // 为空默认对比最后一个月和前一个月的数据 + nowReportInfo = settleOrderReports.get(settleOrderReports.size() - 1); + lastMonthReportInfo = settleOrderReports.get(settleOrderReports.size() - 2); + }else { + // 对比目标日期和目标日期前一个月数据 + nowReportInfo = map.get(dateTime); + Date lastMonth = DateUtils.addMonths(DateUtils.parseDate(dateTime), -1); + String lastMonthStr = DateUtils.parseDateToStr(DateUtils.YYYY_MM, lastMonth); + lastMonthReportInfo = map.get(lastMonthStr); + } + String electricityGrowthRate = "-"; + String orderAmountGrowthRate = ""; + String serviceAmountGrowthRate = ""; + // 充电量增长率 + BigDecimal useElectricity = nowReportInfo.getUseElectricity(); + BigDecimal lastMonthUseElectricity = lastMonthReportInfo.getUseElectricity(); + if (lastMonthUseElectricity.compareTo(BigDecimal.ZERO) != 0) { + BigDecimal usedElectricityRate = useElectricity.subtract(lastMonthUseElectricity).divide(lastMonthUseElectricity, 2, BigDecimal.ROUND_HALF_UP); + electricityGrowthRate = usedElectricityRate.multiply(new BigDecimal("100")) + "%"; + } + // 订单金额增长率 + BigDecimal totalAmount = nowReportInfo.getTotalAmount(); + BigDecimal lastTotalAmount = lastMonthReportInfo.getTotalAmount(); + if (lastTotalAmount.compareTo(BigDecimal.ZERO) != 0) { + BigDecimal totalAmountRate = totalAmount.subtract(lastTotalAmount).divide(lastTotalAmount, 2, BigDecimal.ROUND_HALF_UP); + orderAmountGrowthRate = totalAmountRate.multiply(new BigDecimal("100")) + "%"; + } + // 服务费增长率 + BigDecimal serviceAmount = nowReportInfo.getServiceAmount(); + BigDecimal lastMonthServiceAmount = lastMonthReportInfo.getServiceAmount(); + if (lastMonthServiceAmount.compareTo(BigDecimal.ZERO) != 0) { + BigDecimal serviceAmountRate = serviceAmount.subtract(lastMonthServiceAmount).divide(lastMonthServiceAmount, 2, BigDecimal.ROUND_HALF_UP); + serviceAmountGrowthRate = serviceAmountRate.multiply(new BigDecimal("100")) + "%"; + } + StationBusinessAnalyzeInfoVO vo = StationBusinessAnalyzeInfoVO.builder() + .electricityGrowthRate(electricityGrowthRate) + .orderAmountGrowthRate(orderAmountGrowthRate) + .serviceAmountGrowthRate(serviceAmountGrowthRate) + .settleOrderReportList(settleOrderReports) + .build(); - - return null; + return vo; } } diff --git a/jsowell-pile/src/main/java/com/jsowell/pile/vo/StationBusinessAnalyzeInfoVO.java b/jsowell-pile/src/main/java/com/jsowell/pile/vo/StationBusinessAnalyzeInfoVO.java index a38c22c6f..069aa2737 100644 --- a/jsowell-pile/src/main/java/com/jsowell/pile/vo/StationBusinessAnalyzeInfoVO.java +++ b/jsowell-pile/src/main/java/com/jsowell/pile/vo/StationBusinessAnalyzeInfoVO.java @@ -1,5 +1,6 @@ package com.jsowell.pile.vo; +import com.jsowell.pile.domain.SettleOrderReport; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -35,4 +36,6 @@ public class StationBusinessAnalyzeInfoVO { private String serviceAmountGrowthRate; private List businessOrderDetailInfoVOList; + + private List settleOrderReportList; }