fix: 修复需求趋势图完成数量统计错误

- 完成数量改为按完成时间(complete_time)统计,而非创建时间
- 需求数量保持按创建时间(create_time)统计
This commit is contained in:
Your Name 2026-01-29 20:31:12 +08:00
parent 7392fd2908
commit 11b94b8438

View File

@ -263,45 +263,53 @@ public class AnalyticsService {
* 构建趋势数据
*/
private List<DemandStatisticsVO.TrendData> buildTrendData(List<Task> tasks, String dateRange) {
Map<String, List<Task>> tasksByDate = new LinkedHashMap<>();
// 根据dateRange生成日期范围
int days = "week".equals(dateRange) ? 7 : "month".equals(dateRange) ? 30 : 90;
LocalDate endDate = LocalDate.now();
// 初始化日期范围
Map<String, Integer> demandCountByDate = new LinkedHashMap<>();
Map<String, Integer> completedCountByDate = new LinkedHashMap<>();
Map<String, BigDecimal> 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<DemandStatisticsVO.TrendData> trendData = new ArrayList<>();
for (Map.Entry<String, List<Task>> entry : tasksByDate.entrySet()) {
List<Task> 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());
}