Administrator
2025-07-08 e34b6d59b2938e29c384c4b887c3bdea12d598a6
refactor(mall): 重构管理员数据统计逻辑

- 移除使用 MallMoneyFlow 进行统计的方式
- 新增使用 MallOrderInfo 和 HappyActivityOrder 进行统计
- 优化统计逻辑,提高数据准确性和性能
1 files modified
59 ■■■■ changed files
src/main/java/cc/mrbird/febs/mall/service/impl/AdminMallMemberServiceImpl.java 59 ●●●● patch | view | raw | blame | history
src/main/java/cc/mrbird/febs/mall/service/impl/AdminMallMemberServiceImpl.java
@@ -81,6 +81,8 @@
    private final MallGoodsCouponMapper mallGoodsCouponMapper;
    private final HappySaleLevelMapper  happySaleLevelMapper;
    private final HappyMemberLevelMapper happyMemberLevelMapper;
    private final MallOrderInfoMapper mallOrderInfoMapper;
    private final HappyActivityOrderMapper happyActivityOrderMapper;
    @Override
@@ -1015,25 +1017,50 @@
                    Math.toIntExact(mallMembers.stream().filter(mallMember -> DateUtil.compare(mallMember.getCreatedTime(), DateUtil.beginOfMonth(new Date())) >= 0).count()));
        }
        List<Integer> typeList = Arrays.asList(ScoreFlowTypeEnum.WECHAT_PAY.getValue(), ScoreFlowTypeEnum.PAY_BALANCE.getValue());
        List<MallMoneyFlow> mallMoneyFlows = mallMoneyFlowMapper.selectList(
                Wrappers.lambdaQuery(MallMoneyFlow.class)
                        .in(MallMoneyFlow::getType, typeList)
        );
        if(CollUtil.isNotEmpty(mallMoneyFlows)){
            adminDataInfoVo.setTotalAmount(mallMoneyFlows.stream().map(MallMoneyFlow::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add).negate());
            adminDataInfoVo.setTotalDayAmount(
                    mallMoneyFlows.stream().filter(mallMoneyFlow -> DateUtil.compare(mallMoneyFlow.getCreatedTime(), DateUtil.beginOfDay(new Date())) >= 0)
                            .map(MallMoneyFlow::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add).negate());
            adminDataInfoVo.setTotalWeekAmount(
                    mallMoneyFlows.stream().filter(mallMoneyFlow -> DateUtil.compare(mallMoneyFlow.getCreatedTime(), DateUtil.beginOfWeek(new Date())) >= 0)
                            .map(MallMoneyFlow::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add).negate());
            adminDataInfoVo.setTotalMonthAmount(
                    mallMoneyFlows.stream().filter(mallMoneyFlow -> DateUtil.compare(mallMoneyFlow.getCreatedTime(), DateUtil.beginOfMonth(new Date())) >= 0)
                            .map(MallMoneyFlow::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add).negate());
        BigDecimal totalAmount = BigDecimal.ZERO;//总数
        BigDecimal totalDayAmount = BigDecimal.ZERO;//新增
        BigDecimal totalWeekAmount = BigDecimal.ZERO;//本周新增
        BigDecimal totalMonthAmount = BigDecimal.ZERO;//本月新增
        List<MallOrderInfo> mallOrderInfos = mallOrderInfoMapper.selectList(
                Wrappers.lambdaQuery(MallOrderInfo.class)
                        .eq(MallOrderInfo::getPayResult, "1")
        );
        if(CollUtil.isNotEmpty(mallOrderInfos)){
            totalAmount = totalAmount.add(mallOrderInfos.stream().map(MallOrderInfo::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
            totalDayAmount = totalDayAmount.add(
                    mallOrderInfos.stream().filter(entity -> DateUtil.compare(entity.getPayTime(), DateUtil.beginOfDay(new Date())) >= 0)
                    .map(MallOrderInfo::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
            totalWeekAmount = totalWeekAmount.add(
                    mallOrderInfos.stream().filter(entity -> DateUtil.compare(entity.getPayTime(), DateUtil.beginOfWeek(new Date())) >= 0)
                    .map(MallOrderInfo::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
            totalMonthAmount = totalMonthAmount.add(
                    mallOrderInfos.stream().filter(entity -> DateUtil.compare(entity.getPayTime(), DateUtil.beginOfMonth(new Date())) >= 0)
                    .map(MallOrderInfo::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        }
        List<HappyActivityOrder> happyActivityOrders = happyActivityOrderMapper.selectList(
                Wrappers.lambdaQuery(HappyActivityOrder.class)
                        .eq(HappyActivityOrder::getPayState, StateUpDownEnum.PAY_STATE_PAY_SUCCESS.getCode())
        );
        if(CollUtil.isNotEmpty(happyActivityOrders)){
            totalAmount = totalAmount.add(happyActivityOrders.stream().map(HappyActivityOrder::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
            totalDayAmount = totalDayAmount.add(
                    happyActivityOrders.stream().filter(entity -> DateUtil.compare(entity.getPayTime(), DateUtil.beginOfDay(new Date())) >= 0)
                            .map(HappyActivityOrder::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
            totalWeekAmount = totalWeekAmount.add(
                    happyActivityOrders.stream().filter(entity -> DateUtil.compare(entity.getPayTime(), DateUtil.beginOfWeek(new Date())) >= 0)
                            .map(HappyActivityOrder::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
            totalMonthAmount = totalMonthAmount.add(
                    happyActivityOrders.stream().filter(entity -> DateUtil.compare(entity.getPayTime(), DateUtil.beginOfMonth(new Date())) >= 0)
                            .map(HappyActivityOrder::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        }
        adminDataInfoVo.setTotalAmount(totalAmount);
        adminDataInfoVo.setTotalDayAmount(totalDayAmount);
        adminDataInfoVo.setTotalWeekAmount(totalWeekAmount);
        adminDataInfoVo.setTotalMonthAmount(totalMonthAmount);
        return new FebsResponse().success().data(adminDataInfoVo);
    }
}