44323
2023-10-10 794546783a941be444319d98b1947dac23d65c2b
cloud-server-management/src/main/java/com/dsh/guns/modular/system/controller/code/DataStatisticsController.java
@@ -128,7 +128,6 @@
            competitionQuery.setInsertType(roleType);
                 incomeQuery.setInsertType(roleType);
                 incomeQuery.setInsertType(roleType);
        IncomeVO incomeVO = new IncomeVO();
        List<IncomeVO> incomeVOS = new ArrayList<>();
                // 会员支付记录
@@ -145,7 +144,6 @@
                }
                // 充值记录
                List<RechargeRecordsVO> rechargeRecordsVOS = financeClient.rechargeList(rechargeRecordsQuery);
        List<RechargeRecords> rechargeRecords = financeClient.rechargeList1(rechargeRecordsQuery);
                for (RechargeRecordsVO rechargeRecordsVO : rechargeRecordsVOS) {
                    IncomeVO temp = new IncomeVO();
                    temp.setInsertTime(rechargeRecordsVO.getInsertTime());
@@ -211,7 +209,6 @@
                incomeVOS.add(temp);
            }
        }
        // 最终返回的数据
        Map<String, List<Object>> monthlyData = new HashMap<>();
        // 创建一个 Map 用于存储每年的累计 amount
@@ -252,216 +249,231 @@
        for (int i = 0; i < 10; i++) {
            integers1.add(years-i);
        }
        for (int year = integers1.get(0); year >= integers1.get(integers1.size()-1); year++) {
            BigDecimal totalAmount = yearToTotalAmountMap.getOrDefault(year, BigDecimal.ZERO);
            // 创建一个新的 IncomeVO 对象,并设置年份和累计金额
            IncomeVO resultIncomeVO = new IncomeVO();
            resultIncomeVO.setAmount(totalAmount);
            resultIncomeVOS.add(resultIncomeVO);
        }
        monthlyData.put("yearIncome",resultIncomeVOS);
        // 创建一个 Map 用于存储今年每个月的累计 amount
        Map<Integer, BigDecimal> monthToTotalAmountMap = new HashMap<>();
        Calendar calendar = Calendar.getInstance();
        int currentYear = calendar.get(Calendar.YEAR);
        List<BigDecimal> monthlyAmountList = new ArrayList<>(Collections.nCopies(12, BigDecimal.ZERO));
        for (IncomeVO incomeVO1 : incomeVOS) {
            Calendar calendar1 = Calendar.getInstance();
            calendar1.setTime(incomeVO1.getInsertTime());
            int year = calendar1.get(Calendar.YEAR);
            int month = calendar1.get(Calendar.MONTH) + 1; // 月份从0开始,所以需要加1
            if (year == currentYear) {
                BigDecimal totalAmount = monthlyAmountList.get(month - 1);
                totalAmount = totalAmount.add(incomeVO1.getAmount());
                monthlyAmountList.set(month - 1, totalAmount);
            }
        }
        monthlyData.put("monthIncome", Collections.singletonList(monthlyAmountList));
        // 获取当前月份
        LocalDate currentDate = LocalDate.now();
        int currentMonth = currentDate.getMonthValue();
        // 初始化一个长度为当前月份的天数的列表,并将每一天的金额初始化为零
        int daysInCurrentMonth = currentDate.lengthOfMonth();
        List<BigDecimal> weekAmount = new ArrayList<>();
        // 获取最近四周
        LocalDateTime minNow = LocalDateTime.now().with(LocalTime.MIN);
        LocalDateTime maxNow = LocalDateTime.now().with(LocalTime.MAX);
        // 周度数据
        LocalDateTime week1Start = minNow.minusDays(6);
        LocalDateTime week1End = maxNow;
        LocalDateTime week2Start = minNow.minusDays(13);
        LocalDateTime week2End = minNow.minusDays(7);
        LocalDateTime week3Start = minNow.minusDays(20);
        LocalDateTime week3End = minNow.minusDays(14);
        LocalDateTime week4Start = minNow.minusDays(27);
        LocalDateTime week4End = minNow.minusDays(21);
        // 用于存放四个周的数据
        List<Object> collect = integers1.stream().sorted().collect(Collectors.toList());
        List<Object> objects1 = new ArrayList<>();
        // 计算每个周的amount总和
        BigDecimal tAmount1 = BigDecimal.ZERO;
        BigDecimal tAmount2 = BigDecimal.ZERO;
        BigDecimal tAmount3 = BigDecimal.ZERO;
        BigDecimal tAmount4 = BigDecimal.ZERO;
        for (IncomeVO incomeVO1 : incomeVOS) {
            Date insertTime = incomeVO1.getInsertTime();
            // 将Date转换为LocalDateTime
            Instant instant = insertTime.toInstant();
            ZoneId zoneId = ZoneId.systemDefault();
            LocalDateTime dateTime = instant.atZone(zoneId).toLocalDateTime();
            // 判断该数据属于哪个周
            if (dateTime.isAfter(week1Start) && dateTime.isBefore(week1End)) {
                BigDecimal amount1 = incomeVO1.getAmount();
                tAmount1=tAmount1.add(amount1);
            } else if (dateTime.isAfter(week2Start) && dateTime.isBefore(week2End)) {
                BigDecimal amount1 = incomeVO1.getAmount();
                tAmount2=tAmount2.add(amount1);
            } else if (dateTime.isAfter(week3Start) && dateTime.isBefore(week3End)) {
                BigDecimal amount1 = incomeVO1.getAmount();
                tAmount4=tAmount3.add(amount1);
            } else if (dateTime.isAfter(week4Start) && dateTime.isBefore(week4End)) {
                BigDecimal amount1 = incomeVO1.getAmount();
                tAmount4=tAmount4.add(amount1);
        for (Object o : collect) {
            BigDecimal orDefault = yearToTotalAmountMap.getOrDefault(Integer.valueOf(o.toString()), BigDecimal.ZERO);
            objects1.add(orDefault);
            }
        }
        weekAmount.add(tAmount1);
        weekAmount.add(tAmount2);
        weekAmount.add(tAmount3);
        weekAmount.add(tAmount4);
        monthlyData.put("weekIncome", Collections.singletonList(weekAmount));
        LocalDateTime currentDateTime = LocalDateTime.now();
        List<BigDecimal> amountByDay = new ArrayList<>(Collections.nCopies(7, BigDecimal.ZERO));
        for (IncomeVO incomeVO3 : incomeVOS) {
            Date insertTime = incomeVO3.getInsertTime();
            // 将 Date 转换为 LocalDateTime
            LocalDateTime insertDateTime = LocalDateTime.ofInstant(insertTime.toInstant(), ZoneId.systemDefault());
            // 计算日期与当前日期的差距
            long daysDifference = ChronoUnit.DAYS.between(insertDateTime.toLocalDate(), currentDateTime.toLocalDate());
            // 如果日期在七天内,累加金额
            if (daysDifference >= 0 && daysDifference < 7) {
                int index = (int) (6 - daysDifference); // 计算对应的索引位置
                BigDecimal amount3 = incomeVO3.getAmount();
                BigDecimal updatedAmount = amountByDay.get(index).add(amount3);
                amountByDay.set(index, updatedAmount);
            }
        }
        monthlyData.put("dayIncome", Collections.singletonList(amountByDay));
        // 获取当前日期和时间
        LocalDateTime now = LocalDateTime.now();
        // 今年的起始日期和结束日期
        LocalDateTime startOfYear = now.with(TemporalAdjusters.firstDayOfYear()).with(LocalTime.MIN);
        LocalDateTime endOfYear = now.with(TemporalAdjusters.lastDayOfYear()).with(LocalTime.MAX);
        // 本月的起始日期和结束日期
        LocalDateTime startOfMonth = now.with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN);
        LocalDateTime endOfMonth = now.with(TemporalAdjusters.lastDayOfMonth()).with(LocalTime.MAX);
        // 本周的起始日期和结束日期
        LocalDateTime startOfWeek = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)).with(LocalTime.MIN);
        LocalDateTime endOfWeek = now.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)).with(LocalTime.MAX);
        // 今天的起始日期和结束日期
        LocalDateTime startOfDay = now.with(LocalTime.MIN);
        LocalDateTime endOfDay = now.with(LocalTime.MAX);
        // 创建日期格式化器
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 筛选符合条件的IncomeVO对象
        List<IncomeVO> thisYearIncome = incomeVOS.stream()
                .filter(income -> {
                    Date insertTime = income.getInsertTime();
                    try {
                        LocalDateTime insertDateTime = LocalDateTime.ofInstant(insertTime.toInstant(), ZoneId.systemDefault());
                        return insertDateTime.isAfter(startOfYear) && insertDateTime.isBefore(endOfYear);
                    } catch (Exception e) {
                        e.printStackTrace();
                        return false;
                    }
                })
                .collect(Collectors.toList());
        // 使用 map 方法将 IncomeVO 转换为 BigDecimal,提取出 amount 字段
        List<BigDecimal> amounts = thisYearIncome.stream()
                .map(IncomeVO::getAmount)
                .collect(Collectors.toList());
        //本年总收入
        BigDecimal totalAmount = amounts.stream()
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        // 用于展示总营收 本年度营收 本月营收 本周营收 本日营收
        objects.add(totalAmount);
        List<IncomeVO> thisMonthIncome = incomeVOS.stream()
                .filter(income -> {
                    Date insertTime = income.getInsertTime();
                    try {
                        LocalDateTime insertDateTime = LocalDateTime.ofInstant(insertTime.toInstant(), ZoneId.systemDefault());
                        return insertDateTime.isAfter(startOfMonth) && insertDateTime.isBefore(endOfMonth);
                    } catch (Exception e) {
                        e.printStackTrace();
                        return false;
                    }
                })
                .collect(Collectors.toList());
        List<BigDecimal> amountMonth = thisMonthIncome.stream()
                .map(IncomeVO::getAmount)
                .collect(Collectors.toList());
        // 使用 本月总收入
        BigDecimal totalMonth = amountMonth.stream()
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        objects.add(totalMonth);
        List<IncomeVO> thisWeekIncome = incomeVOS.stream()
                .filter(income -> {
                    Date insertTime = income.getInsertTime();
                    try {
                        LocalDateTime insertDateTime = LocalDateTime.ofInstant(insertTime.toInstant(), ZoneId.systemDefault());
                        return insertDateTime.isAfter(startOfWeek) && insertDateTime.isBefore(endOfWeek);
                    } catch (Exception e) {
                        e.printStackTrace();
                        return false;
                    }
                })
                .collect(Collectors.toList());
        List<BigDecimal> amountWeek = thisWeekIncome.stream()
                .map(IncomeVO::getAmount)
                .collect(Collectors.toList());
        // 使用 本月总收入
        BigDecimal totalWeek = amountWeek.stream()
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        objects.add(totalWeek);
        List<IncomeVO> todayIncome = incomeVOS.stream()
                .filter(income -> {
                    Date insertTime = income.getInsertTime();
                    try {
                        LocalDateTime insertDateTime = LocalDateTime.ofInstant(insertTime.toInstant(), ZoneId.systemDefault());
                        return insertDateTime.isAfter(startOfDay) && insertDateTime.isBefore(endOfDay);
                    } catch (Exception e) {
                        e.printStackTrace();
                        return false;
                    }
                })
                .collect(Collectors.toList());
        List<BigDecimal> amountToday = todayIncome.stream()
                .map(IncomeVO::getAmount)
                .collect(Collectors.toList());
        // 使用 本月总收入
        BigDecimal totalToday = amountToday.stream()
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        objects.add(totalToday);
        // 最后put
        monthlyData.put("yearMount",objects);
        ArrayList<Object> integers = new ArrayList<>();
        int year = DateUtil.year(new Date());
        for (int i = 0; i < 10; i++) {
            integers.add(year-i);
        }
        List<Object> collect = integers.stream().sorted().collect(Collectors.toList());
        monthlyData.put("year",collect);
//        for (int year = integers1.get(0); year >= integers1.get(integers1.size()-1); year++) {
//            BigDecimal totalAmount = yearToTotalAmountMap.getOrDefault(year, BigDecimal.ZERO);
//            // 创建一个新的 IncomeVO 对象,并设置年份和累计金额
//            IncomeVO resultIncomeVO = new IncomeVO();
//            resultIncomeVO.setAmount(totalAmount);
//            resultIncomeVOS.add(resultIncomeVO);
//        }
        monthlyData.put("yearIncome",objects1);
        return monthlyData;
//
//        // 创建一个 Map 用于存储今年每个月的累计 amount
//        Map<Integer, BigDecimal> monthToTotalAmountMap = new HashMap<>();
//        Calendar calendar = Calendar.getInstance();
//        int currentYear = calendar.get(Calendar.YEAR);
//        List<BigDecimal> monthlyAmountList = new ArrayList<>(Collections.nCopies(12, BigDecimal.ZERO));
//
//        for (IncomeVO incomeVO1 : incomeVOS) {
//            Calendar calendar1 = Calendar.getInstance();
//            calendar1.setTime(incomeVO1.getInsertTime());
//            int year = calendar1.get(Calendar.YEAR);
//            int month = calendar1.get(Calendar.MONTH) + 1; // 月份从0开始,所以需要加1
//            if (year == currentYear) {
//                BigDecimal totalAmount = monthlyAmountList.get(month - 1);
//                totalAmount = totalAmount.add(incomeVO1.getAmount());
//                monthlyAmountList.set(month - 1, totalAmount);
//            }
//        }
//        monthlyData.put("monthIncome", Collections.singletonList(monthlyAmountList));
//
//        // 获取当前月份
//        LocalDate currentDate = LocalDate.now();
//        int currentMonth = currentDate.getMonthValue();
//        // 初始化一个长度为当前月份的天数的列表,并将每一天的金额初始化为零
//        int daysInCurrentMonth = currentDate.lengthOfMonth();
//        List<BigDecimal> weekAmount = new ArrayList<>();
//
//        // 获取最近四周
//        LocalDateTime minNow = LocalDateTime.now().with(LocalTime.MIN);
//        LocalDateTime maxNow = LocalDateTime.now().with(LocalTime.MAX);
//        // 周度数据
//        LocalDateTime week1Start = minNow.minusDays(6);
//        LocalDateTime week1End = maxNow;
//
//        LocalDateTime week2Start = minNow.minusDays(13);
//        LocalDateTime week2End = minNow.minusDays(7);
//
//        LocalDateTime week3Start = minNow.minusDays(20);
//        LocalDateTime week3End = minNow.minusDays(14);
//
//        LocalDateTime week4Start = minNow.minusDays(27);
//        LocalDateTime week4End = minNow.minusDays(21);
//        // 用于存放四个周的数据
//        List<Object> objects1 = new ArrayList<>();
//        // 计算每个周的amount总和
//        BigDecimal tAmount1 = BigDecimal.ZERO;
//        BigDecimal tAmount2 = BigDecimal.ZERO;
//        BigDecimal tAmount3 = BigDecimal.ZERO;
//        BigDecimal tAmount4 = BigDecimal.ZERO;
//        for (IncomeVO incomeVO1 : incomeVOS) {
//            Date insertTime = incomeVO1.getInsertTime();
//            // 将Date转换为LocalDateTime
//            Instant instant = insertTime.toInstant();
//            ZoneId zoneId = ZoneId.systemDefault();
//            LocalDateTime dateTime = instant.atZone(zoneId).toLocalDateTime();
//            // 判断该数据属于哪个周
//            if (dateTime.isAfter(week1Start) && dateTime.isBefore(week1End)) {
//                BigDecimal amount1 = incomeVO1.getAmount();
//                tAmount1=tAmount1.add(amount1);
//            } else if (dateTime.isAfter(week2Start) && dateTime.isBefore(week2End)) {
//                BigDecimal amount1 = incomeVO1.getAmount();
//                tAmount2=tAmount2.add(amount1);
//            } else if (dateTime.isAfter(week3Start) && dateTime.isBefore(week3End)) {
//                BigDecimal amount1 = incomeVO1.getAmount();
//                tAmount4=tAmount3.add(amount1);
//            } else if (dateTime.isAfter(week4Start) && dateTime.isBefore(week4End)) {
//                BigDecimal amount1 = incomeVO1.getAmount();
//                tAmount4=tAmount4.add(amount1);
//            }
//        }
//        weekAmount.add(tAmount1);
//        weekAmount.add(tAmount2);
//        weekAmount.add(tAmount3);
//        weekAmount.add(tAmount4);
//        monthlyData.put("weekIncome", Collections.singletonList(weekAmount));
//
//        LocalDateTime currentDateTime = LocalDateTime.now();
//        List<BigDecimal> amountByDay = new ArrayList<>(Collections.nCopies(7, BigDecimal.ZERO));
//        for (IncomeVO incomeVO3 : incomeVOS) {
//            Date insertTime = incomeVO3.getInsertTime();
//            // 将 Date 转换为 LocalDateTime
//            LocalDateTime insertDateTime = LocalDateTime.ofInstant(insertTime.toInstant(), ZoneId.systemDefault());
//            // 计算日期与当前日期的差距
//            long daysDifference = ChronoUnit.DAYS.between(insertDateTime.toLocalDate(), currentDateTime.toLocalDate());
//            // 如果日期在七天内,累加金额
//            if (daysDifference >= 0 && daysDifference < 7) {
//                int index = (int) (6 - daysDifference); // 计算对应的索引位置
//                BigDecimal amount3 = incomeVO3.getAmount();
//                BigDecimal updatedAmount = amountByDay.get(index).add(amount3);
//                amountByDay.set(index, updatedAmount);
//
//            }
//        }
//
//        monthlyData.put("dayIncome", Collections.singletonList(amountByDay));
//        // 获取当前日期和时间
//        LocalDateTime now = LocalDateTime.now();
//        // 今年的起始日期和结束日期
//        LocalDateTime startOfYear = now.with(TemporalAdjusters.firstDayOfYear()).with(LocalTime.MIN);
//        LocalDateTime endOfYear = now.with(TemporalAdjusters.lastDayOfYear()).with(LocalTime.MAX);
//        // 本月的起始日期和结束日期
//        LocalDateTime startOfMonth = now.with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN);
//        LocalDateTime endOfMonth = now.with(TemporalAdjusters.lastDayOfMonth()).with(LocalTime.MAX);
//        // 本周的起始日期和结束日期
//        LocalDateTime startOfWeek = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)).with(LocalTime.MIN);
//        LocalDateTime endOfWeek = now.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)).with(LocalTime.MAX);
//        // 今天的起始日期和结束日期
//        LocalDateTime startOfDay = now.with(LocalTime.MIN);
//        LocalDateTime endOfDay = now.with(LocalTime.MAX);
//        // 创建日期格式化器
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        // 筛选符合条件的IncomeVO对象
//        List<IncomeVO> thisYearIncome = incomeVOS.stream()
//                .filter(income -> {
//                    Date insertTime = income.getInsertTime();
//                    try {
//                        LocalDateTime insertDateTime = LocalDateTime.ofInstant(insertTime.toInstant(), ZoneId.systemDefault());
//                        return insertDateTime.isAfter(startOfYear) && insertDateTime.isBefore(endOfYear);
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                        return false;
//                    }
//                })
//                .collect(Collectors.toList());
//        // 使用 map 方法将 IncomeVO 转换为 BigDecimal,提取出 amount 字段
//        List<BigDecimal> amounts = thisYearIncome.stream()
//                .map(IncomeVO::getAmount)
//                .collect(Collectors.toList());
//        //本年总收入
//        BigDecimal totalAmount = amounts.stream()
//                .reduce(BigDecimal.ZERO, BigDecimal::add);
//        // 用于展示总营收 本年度营收 本月营收 本周营收 本日营收
//        objects.add(totalAmount);
//        List<IncomeVO> thisMonthIncome = incomeVOS.stream()
//                .filter(income -> {
//                    Date insertTime = income.getInsertTime();
//                    try {
//                        LocalDateTime insertDateTime = LocalDateTime.ofInstant(insertTime.toInstant(), ZoneId.systemDefault());
//                        return insertDateTime.isAfter(startOfMonth) && insertDateTime.isBefore(endOfMonth);
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                        return false;
//                    }
//                })
//                .collect(Collectors.toList());
//        List<BigDecimal> amountMonth = thisMonthIncome.stream()
//                .map(IncomeVO::getAmount)
//                .collect(Collectors.toList());
//        // 使用 本月总收入
//        BigDecimal totalMonth = amountMonth.stream()
//                .reduce(BigDecimal.ZERO, BigDecimal::add);
//
//        objects.add(totalMonth);
//        List<IncomeVO> thisWeekIncome = incomeVOS.stream()
//                .filter(income -> {
//                    Date insertTime = income.getInsertTime();
//                    try {
//                        LocalDateTime insertDateTime = LocalDateTime.ofInstant(insertTime.toInstant(), ZoneId.systemDefault());
//                        return insertDateTime.isAfter(startOfWeek) && insertDateTime.isBefore(endOfWeek);
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                        return false;
//                    }
//                })
//                .collect(Collectors.toList());
//        List<BigDecimal> amountWeek = thisWeekIncome.stream()
//                .map(IncomeVO::getAmount)
//                .collect(Collectors.toList());
//        // 使用 本月总收入
//        BigDecimal totalWeek = amountWeek.stream()
//                .reduce(BigDecimal.ZERO, BigDecimal::add);
//        objects.add(totalWeek);
//        List<IncomeVO> todayIncome = incomeVOS.stream()
//                .filter(income -> {
//                    Date insertTime = income.getInsertTime();
//                    try {
//                        LocalDateTime insertDateTime = LocalDateTime.ofInstant(insertTime.toInstant(), ZoneId.systemDefault());
//                        return insertDateTime.isAfter(startOfDay) && insertDateTime.isBefore(endOfDay);
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                        return false;
//                    }
//                })
//                .collect(Collectors.toList());
//        List<BigDecimal> amountToday = todayIncome.stream()
//                .map(IncomeVO::getAmount)
//                .collect(Collectors.toList());
//        // 使用 本月总收入
//        BigDecimal totalToday = amountToday.stream()
//                .reduce(BigDecimal.ZERO, BigDecimal::add);
//        objects.add(totalToday);
//        // 最后put
//        monthlyData.put("yearMount",objects);
//
//        ArrayList<Object> integers = new ArrayList<>();
//        int year = DateUtil.year(new Date());
//        for (int i = 0; i < 10; i++) {
//            integers.add(year-i);
//        }
//        List<Object> collect = integers.stream().sorted().collect(Collectors.toList());
//        monthlyData.put("year",collect);
//        return monthlyData;
    }