diff --git a/backend/src/main/java/com/designhub/service/AnalyticsService.java b/backend/src/main/java/com/designhub/service/AnalyticsService.java index 28cd61f..1b27453 100644 --- a/backend/src/main/java/com/designhub/service/AnalyticsService.java +++ b/backend/src/main/java/com/designhub/service/AnalyticsService.java @@ -263,45 +263,53 @@ public class AnalyticsService { * 构建趋势数据 */ private List buildTrendData(List tasks, String dateRange) { - Map> tasksByDate = new LinkedHashMap<>(); - // 根据dateRange生成日期范围 int days = "week".equals(dateRange) ? 7 : "month".equals(dateRange) ? 30 : 90; LocalDate endDate = LocalDate.now(); + // 初始化日期范围 + Map demandCountByDate = new LinkedHashMap<>(); + Map completedCountByDate = new LinkedHashMap<>(); + Map scoreUsedByDate = new LinkedHashMap<>(); + for (int i = days - 1; i >= 0; i--) { LocalDate date = endDate.minusDays(i); - tasksByDate.put(date.format(DateTimeFormatter.ISO_LOCAL_DATE), new ArrayList<>()); + String dateKey = date.format(DateTimeFormatter.ISO_LOCAL_DATE); + demandCountByDate.put(dateKey, 0); + completedCountByDate.put(dateKey, 0); + scoreUsedByDate.put(dateKey, BigDecimal.ZERO); } - // 将任务按日期分组 + // 统计需求数量(按创建时间) for (Task task : tasks) { - String dateKey = task.getCreateTime().toLocalDate().format(DateTimeFormatter.ISO_LOCAL_DATE); - if (tasksByDate.containsKey(dateKey)) { - tasksByDate.get(dateKey).add(task); + String createDateKey = task.getCreateTime().toLocalDate().format(DateTimeFormatter.ISO_LOCAL_DATE); + if (demandCountByDate.containsKey(createDateKey)) { + demandCountByDate.merge(createDateKey, 1, Integer::sum); + // 积分按创建日期统计 + if (task.getScore() != null) { + scoreUsedByDate.merge(createDateKey, task.getScore(), BigDecimal::add); + } + } + } + + // 统计完成数量(按完成时间) + for (Task task : tasks) { + if ("completed".equals(task.getStatus()) && task.getCompleteTime() != null) { + String completeDateKey = task.getCompleteTime().toLocalDate().format(DateTimeFormatter.ISO_LOCAL_DATE); + if (completedCountByDate.containsKey(completeDateKey)) { + completedCountByDate.merge(completeDateKey, 1, Integer::sum); + } } } + // 构建返回数据 List trendData = new ArrayList<>(); - for (Map.Entry> entry : tasksByDate.entrySet()) { - List dateTasks = entry.getValue(); - - int demandCount = dateTasks.size(); - int completedCount = (int) dateTasks.stream() - .filter(t -> "completed".equals(t.getStatus())) - .count(); - - // 使用 score 字段(预估积分)作为积分消耗,因为 scoreReward 字段未被使用 - BigDecimal scoreUsed = dateTasks.stream() - .map(Task::getScore) - .filter(Objects::nonNull) - .reduce(BigDecimal.ZERO, BigDecimal::add); - + for (String dateKey : demandCountByDate.keySet()) { trendData.add(DemandStatisticsVO.TrendData.builder() - .date(entry.getKey()) - .demandCount(demandCount) - .completedCount(completedCount) - .scoreUsed(scoreUsed) + .date(dateKey) + .demandCount(demandCountByDate.get(dateKey)) + .completedCount(completedCountByDate.get(dateKey)) + .scoreUsed(scoreUsedByDate.get(dateKey)) .build()); }