5.19

学习记录app 完结 撒花 ~~~

下面是今天一天的努力成果

统计月份 和 每个月的次数 和 各个分段等级的次数

  public Map<String, Map<String, Integer>> statistic(Integer userId) {
        List<Plan> plans = planMapper.allPlan(userId);
        Map<String, Map<String, Integer>> map = new HashMap<>();
        Map<String, Integer> monthlyStatistics = new HashMap<>();

        // Define multiple formatters for different millisecond lengths
        DateTimeFormatter[] formatters = {
                DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"),
                DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SS"),
                DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S"),
                DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
        };
        DateTimeFormatter monthFormatter = DateTimeFormatter.ofPattern("yyyy-MM");

        for (Plan plan : plans) {
            String dateString = plan.getDate().toString();
            LocalDateTime dateTime = null;

            // Try each formatter until one succeeds
            for (DateTimeFormatter formatter : formatters) {
                try {
                    dateTime = LocalDateTime.parse(dateString, formatter);
                    break;
                } catch (DateTimeParseException e) {
                    // Ignore and try the next formatter
                }
            }

            // If none of the formatters succeeded, throw an exception
            if (dateTime == null) {
                throw new IllegalArgumentException("Invalid date format: " + dateString);
            }

            // Format to year-month
            String month = dateTime.format(monthFormatter);

//            monthlyStatistics.put(month, monthlyStatistics.getOrDefault(month, 0) + 1);
//            map.getOrDefault(month,new HashMap<>()).put("count", map.getOrDefault(month,new HashMap<>()).getOrDefault("count",0) + 1);
//            if (plan.getAnalyse() != null) {
//                switch (plan.getAnalyse()) {
//                    case "很差" ->
//                            map.getOrDefault(month,new HashMap<>()).put("1", map.getOrDefault(month,new HashMap<>()).getOrDefault("1",0) + 1);
//                    case "及格" ->
//                            map.getOrDefault(month,new HashMap<>()).put("2", map.getOrDefault(month,new HashMap<>()).getOrDefault("2",0) + 1);
//                    case "良好" ->
//                            map.getOrDefault(month,new HashMap<>()).put("3", map.getOrDefault(month,new HashMap<>()).getOrDefault("3",0) + 1);
//                    case "优秀" ->
//                            map.getOrDefault(month,new HashMap<>()).put("4", map.getOrDefault(month,new HashMap<>()).getOrDefault("4",0) + 1);
//                }
//            }

            Map<String, Integer> stats = map.computeIfAbsent(month, k -> new HashMap<>());

            // Update the count for the month
            stats.put("count", stats.getOrDefault("count", 0) + 1);

            // Update the analysis count
            if (plan.getAnalyse() != null) {
                switch (plan.getAnalyse()) {
                    case "很差" -> stats.put("1", stats.getOrDefault("1", 0) + 1);
                    case "及格" -> stats.put("2", stats.getOrDefault("2", 0) + 1);
                    case "良好" -> stats.put("3", stats.getOrDefault("3", 0) + 1);
                    case "优秀" -> stats.put("4", stats.getOrDefault("4", 0) + 1);
                }
            }

        }
        return map;
    }
posted @ 2024-05-19 23:02  258333  阅读(5)  评论(0编辑  收藏  举报