|
@@ -1,9 +1,9 @@
|
|
|
package cn.iocoder.yudao.module.statistics.service.trade;
|
|
|
|
|
|
+import cn.hutool.core.date.DatePattern;
|
|
|
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.common.vo.DataComparisonRespVO;
|
|
|
import cn.iocoder.yudao.module.statistics.controller.admin.trade.vo.TradeTrendSummaryRespVO;
|
|
|
import cn.iocoder.yudao.module.statistics.convert.trade.TradeStatisticsConvert;
|
|
|
import cn.iocoder.yudao.module.statistics.dal.dataobject.trade.TradeStatisticsDO;
|
|
@@ -21,6 +21,8 @@ import javax.annotation.Resource;
|
|
|
import java.time.Duration;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+import java.util.stream.IntStream;
|
|
|
|
|
|
/**
|
|
|
* 交易统计 Service 实现类
|
|
@@ -44,51 +46,27 @@ public class TradeStatisticsServiceImpl implements TradeStatisticsService {
|
|
|
private PayWalletStatisticsService payWalletStatisticsService;
|
|
|
|
|
|
@Override
|
|
|
- public TradeStatisticsComparisonRespVO<TradeSummaryRespVO> getTradeSummaryComparison() {
|
|
|
- // 1.1 昨天的数据
|
|
|
- TradeSummaryRespBO yesterdayData = getTradeSummaryByDays(-1);
|
|
|
- // 1.2 前天的数据(用于对照昨天的数据)
|
|
|
- TradeSummaryRespBO beforeYesterdayData = getTradeSummaryByDays(-2);
|
|
|
- // 2.1 本月数据
|
|
|
- TradeSummaryRespBO monthData = getTradeSummaryByMonths(0);
|
|
|
- // 2.2 上月数据(用于对照本月的数据)
|
|
|
- TradeSummaryRespBO lastMonthData = getTradeSummaryByMonths(-1);
|
|
|
- // 转换返回
|
|
|
- return TradeStatisticsConvert.INSTANCE.convert(yesterdayData, beforeYesterdayData, monthData, lastMonthData);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 统计指定日期的交易数据
|
|
|
- *
|
|
|
- * @param days 增加的天数
|
|
|
- * @return 交易数据
|
|
|
- */
|
|
|
- private TradeSummaryRespBO getTradeSummaryByDays(int days) {
|
|
|
+ public TradeSummaryRespBO getTradeSummaryByDays(int days) {
|
|
|
LocalDateTime date = LocalDateTime.now().plusDays(days);
|
|
|
return tradeStatisticsMapper.selectOrderCreateCountSumAndOrderPayPriceSumByTimeBetween(
|
|
|
LocalDateTimeUtil.beginOfDay(date), LocalDateTimeUtil.endOfDay(date));
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 统计指定月份的交易数据
|
|
|
- *
|
|
|
- * @param months 增加的月数
|
|
|
- * @return 交易数据
|
|
|
- */
|
|
|
- private TradeSummaryRespBO getTradeSummaryByMonths(int months) {
|
|
|
+ @Override
|
|
|
+ public TradeSummaryRespBO getTradeSummaryByMonths(int months) {
|
|
|
LocalDateTime monthDate = LocalDateTime.now().plusMonths(months);
|
|
|
return tradeStatisticsMapper.selectOrderCreateCountSumAndOrderPayPriceSumByTimeBetween(
|
|
|
LocalDateTimeUtils.beginOfMonth(monthDate), LocalDateTimeUtils.endOfMonth(monthDate));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public TradeStatisticsComparisonRespVO<TradeTrendSummaryRespVO> getTradeTrendSummaryComparison(LocalDateTime beginTime,
|
|
|
- LocalDateTime endTime) {
|
|
|
+ public DataComparisonRespVO<TradeTrendSummaryRespVO> getTradeTrendSummaryComparison(LocalDateTime beginTime,
|
|
|
+ LocalDateTime endTime) {
|
|
|
// 统计数据
|
|
|
- TradeTrendSummaryRespVO value = tradeStatisticsMapper.selectByTimeBetween(beginTime, endTime);
|
|
|
+ TradeTrendSummaryRespVO value = tradeStatisticsMapper.selectVoByTimeBetween(beginTime, endTime);
|
|
|
// 对照数据
|
|
|
LocalDateTime referenceBeginTime = beginTime.minus(Duration.between(beginTime, endTime));
|
|
|
- TradeTrendSummaryRespVO reference = tradeStatisticsMapper.selectByTimeBetween(referenceBeginTime, beginTime);
|
|
|
+ TradeTrendSummaryRespVO reference = tradeStatisticsMapper.selectVoByTimeBetween(referenceBeginTime, beginTime);
|
|
|
return TradeStatisticsConvert.INSTANCE.convert(value, reference);
|
|
|
}
|
|
|
|
|
@@ -97,41 +75,60 @@ public class TradeStatisticsServiceImpl implements TradeStatisticsService {
|
|
|
return tradeStatisticsMapper.selectExpensePriceByTimeBetween(beginTime, endTime);
|
|
|
}
|
|
|
|
|
|
- // TODO @疯狂:是不是直接返回 TradeStatisticsDO;上层在去聚合?
|
|
|
@Override
|
|
|
- public List<TradeTrendSummaryRespVO> getTradeStatisticsList(LocalDateTime beginTime, LocalDateTime endTime) {
|
|
|
+ public List<TradeStatisticsDO> getTradeStatisticsList(LocalDateTime beginTime, LocalDateTime endTime) {
|
|
|
return tradeStatisticsMapper.selectListByTimeBetween(beginTime, endTime);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public String statisticsYesterdayTrade() {
|
|
|
- // TODO @疯狂:如果已经统计,则跳过;
|
|
|
- // TODO @疯狂:改成 statisticsTrade,然后传入 days,统计多少天;days 通过 job 传参;方便把历史给统计出来;或者大家有的时候要修复数据(会 fix 业务数据,然后清理统计表),重新统计的时候;
|
|
|
- // 1. 从各个数据表,统计对应数据
|
|
|
- LocalDateTime yesterday = LocalDateTime.now().minusDays(1);
|
|
|
- LocalDateTime beginTime = LocalDateTimeUtil.beginOfDay(yesterday);
|
|
|
- LocalDateTime endTime = LocalDateTimeUtil.endOfDay(yesterday);
|
|
|
- // 1.1 统计订单
|
|
|
- StopWatch stopWatch = new StopWatch("交易统计");
|
|
|
+ public String statisticsTrade(Integer days) {
|
|
|
+ LocalDateTime today = LocalDateTime.now();
|
|
|
+ return IntStream.rangeClosed(1, days)
|
|
|
+ .parallel()
|
|
|
+ .mapToObj(day -> statisticsTrade(today.minusDays(day)))
|
|
|
+ .sorted()
|
|
|
+ .collect(Collectors.joining("\n"));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 统计交易数据
|
|
|
+ *
|
|
|
+ * @param date 需要统计的日期
|
|
|
+ * @return 统计结果
|
|
|
+ */
|
|
|
+ private String statisticsTrade(LocalDateTime date) {
|
|
|
+ // 1. 处理统计时间范围
|
|
|
+ LocalDateTime beginTime = LocalDateTimeUtil.beginOfDay(date);
|
|
|
+ LocalDateTime endTime = LocalDateTimeUtil.endOfDay(date);
|
|
|
+ String dateStr = DatePattern.NORM_DATE_FORMAT.format(date);
|
|
|
+ // 2. 检查该日是否已经统计过
|
|
|
+ TradeStatisticsDO entity = tradeStatisticsMapper.selectByTimeBetween(beginTime, endTime);
|
|
|
+ if (entity != null) {
|
|
|
+ return dateStr + " 数据已存在,如果需要重新统计,请先删除对应的数据";
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 从各个数据表,统计对应数据
|
|
|
+ StopWatch stopWatch = new StopWatch(dateStr);
|
|
|
+ // 3.1 统计订单
|
|
|
stopWatch.start("统计订单");
|
|
|
TradeOrderSummaryRespBO orderSummary = tradeOrderStatisticsService.getOrderSummary(beginTime, endTime);
|
|
|
stopWatch.stop();
|
|
|
- // 1.2 统计售后
|
|
|
+ // 3.2 统计售后
|
|
|
stopWatch.start("统计售后");
|
|
|
AfterSaleSummaryRespBO afterSaleSummary = afterSaleStatisticsService.getAfterSaleSummary(beginTime, endTime);
|
|
|
stopWatch.stop();
|
|
|
- // 1.3 统计佣金
|
|
|
+ // 3.3 统计佣金
|
|
|
stopWatch.start("统计佣金");
|
|
|
Integer brokerageSettlementPrice = brokerageStatisticsService.getBrokerageSettlementPriceSummary(beginTime, endTime);
|
|
|
stopWatch.stop();
|
|
|
- // 1.4 统计充值
|
|
|
+ // 3.4 统计充值
|
|
|
stopWatch.start("统计充值");
|
|
|
WalletSummaryRespBO walletSummary = payWalletStatisticsService.getWalletSummary(beginTime, endTime);
|
|
|
stopWatch.stop();
|
|
|
|
|
|
- // 2. 插入数据
|
|
|
- TradeStatisticsDO entity = TradeStatisticsConvert.INSTANCE.convert(yesterday, orderSummary, afterSaleSummary,
|
|
|
- brokerageSettlementPrice, walletSummary);
|
|
|
+ // 4. 插入数据
|
|
|
+ entity = TradeStatisticsConvert.INSTANCE.convert(date, orderSummary, afterSaleSummary, brokerageSettlementPrice,
|
|
|
+ walletSummary);
|
|
|
tradeStatisticsMapper.insert(entity);
|
|
|
return stopWatch.prettyPrint();
|
|
|
}
|