From cafa938ff4d4e619a911b3276af4bf713456ef85 Mon Sep 17 00:00:00 2001 From: owen Date: Sat, 30 Sep 2023 22:21:47 +0800 Subject: [PATCH] =?UTF-8?q?common=EF=BC=9ALocalDateTimeUtils=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E6=96=B9=E6=B3=95=EF=BC=9A=E6=9C=88=E4=BB=BD=E5=BC=80?= =?UTF-8?q?=E5=A7=8B=E6=97=B6=E9=97=B4=E3=80=81=E6=9C=88=E4=BB=BD=E7=BB=93?= =?UTF-8?q?=E6=9D=9F=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/util/date/LocalDateTimeUtils.java | 26 +++++++++++++++++++ .../trade/TradeStatisticsServiceImpl.java | 18 +++---------- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/date/LocalDateTimeUtils.java b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/date/LocalDateTimeUtils.java index 6bbdcc7cf..527478941 100644 --- a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/date/LocalDateTimeUtils.java +++ b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/date/LocalDateTimeUtils.java @@ -6,6 +6,7 @@ import java.time.Duration; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; +import java.time.temporal.TemporalAdjusters; /** * 时间工具类,用于 {@link java.time.LocalDateTime} @@ -97,4 +98,29 @@ public class LocalDateTimeUtils { LocalDateTime.of(nowDate, startTime2), LocalDateTime.of(nowDate, endTime2)); } + /** + * 获取指定日期所在的月份的开始时间 + * 例如:2023-09-30 00:00:00,000 + * + * @param date 日期 + * @return 月份的开始时间 + */ + public static LocalDateTime beginOfMonth(LocalDateTime date) { + return date + .with(TemporalAdjusters.firstDayOfMonth()) + .with(LocalTime.MIN); + } + + /** + * 获取指定日期所在的月份的最后时间 + * 例如:2023-09-30 23:59:59,999 + * + * @param date 日期 + * @return 月份的结束时间 + */ + public static LocalDateTime endOfMonth(LocalDateTime date) { + return date + .with(TemporalAdjusters.lastDayOfMonth()) + .with(LocalTime.MAX); + } } diff --git a/yudao-module-mall/yudao-module-statistics-biz/src/main/java/cn/iocoder/yudao/module/statistics/service/trade/TradeStatisticsServiceImpl.java b/yudao-module-mall/yudao-module-statistics-biz/src/main/java/cn/iocoder/yudao/module/statistics/service/trade/TradeStatisticsServiceImpl.java index 51db092ab..541120eb1 100644 --- a/yudao-module-mall/yudao-module-statistics-biz/src/main/java/cn/iocoder/yudao/module/statistics/service/trade/TradeStatisticsServiceImpl.java +++ b/yudao-module-mall/yudao-module-statistics-biz/src/main/java/cn/iocoder/yudao/module/statistics/service/trade/TradeStatisticsServiceImpl.java @@ -1,6 +1,7 @@ package cn.iocoder.yudao.module.statistics.service.trade; import cn.hutool.core.date.LocalDateTimeUtil; +import cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils; import cn.iocoder.yudao.module.statistics.controller.admin.trade.vo.TradeStatisticsComparisonRespVO; import cn.iocoder.yudao.module.statistics.controller.admin.trade.vo.TradeSummaryRespVO; import cn.iocoder.yudao.module.statistics.controller.admin.trade.vo.TradeTrendSummaryRespVO; @@ -13,8 +14,6 @@ import org.springframework.validation.annotation.Validated; import javax.annotation.Resource; import java.time.Duration; import java.time.LocalDateTime; -import java.time.LocalTime; -import java.time.temporal.TemporalAdjusters; import java.util.List; /** @@ -79,18 +78,9 @@ public class TradeStatisticsServiceImpl implements TradeStatisticsService { * @return 交易数据 */ private TradeSummaryRespBO getTradeSummaryByMonths(int months) { - // TODO @疯狂:可以在 LocalDateUtils 封装方法;获得月份的开始;以及结束两个方法;然后这里就可以直接调用了 - // 月份开始时间 - LocalDateTime beginOfMonth = LocalDateTime.now() - .plusMonths(months) - .with(TemporalAdjusters.firstDayOfMonth()) - .with(LocalTime.MIN); - // 月份截止时间 - LocalDateTime endOfToday = LocalDateTime.now() - .plusMonths(months) - .with(TemporalAdjusters.lastDayOfMonth()) - .with(LocalTime.MAX); - return tradeStatisticsMapper.selectOrderCreateCountSumAndOrderPayPriceSumByTimeBetween(beginOfMonth, endOfToday); + LocalDateTime monthDate = LocalDateTime.now().plusMonths(months); + return tradeStatisticsMapper.selectOrderCreateCountSumAndOrderPayPriceSumByTimeBetween( + LocalDateTimeUtils.beginOfMonth(monthDate), LocalDateTimeUtils.endOfMonth(monthDate)); } }