From 4bd856ea011df0b28c0873a9f882bf66e3d0a1f0 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 8 Jan 2026 14:21:09 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E5=88=86=E5=B8=83=E4=BD=BF=E7=94=A8=E9=94=99=E8=AF=AF=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E5=92=8C=E8=AE=BE=E8=AE=A1=E5=B8=88=E8=BF=9B=E8=A1=8C?= =?UTF-8?q?=E4=B8=AD=E4=BB=BB=E5=8A=A1=E6=95=B0=E6=9C=AA=E5=8C=85=E5=90=AB?= =?UTF-8?q?=E5=AD=90=E4=BB=BB=E5=8A=A1=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../designhub/service/AnalyticsService.java | 43 +++++++++++--- .../designhub/service/DesignerService.java | 58 +++++++++++++++---- 2 files changed, 83 insertions(+), 18 deletions(-) diff --git a/backend/src/main/java/com/designhub/service/AnalyticsService.java b/backend/src/main/java/com/designhub/service/AnalyticsService.java index f42d2f2..cde3159 100644 --- a/backend/src/main/java/com/designhub/service/AnalyticsService.java +++ b/backend/src/main/java/com/designhub/service/AnalyticsService.java @@ -37,6 +37,7 @@ public class AnalyticsService { private final TaskReviewMapper taskReviewMapper; private final RoleService roleService; private final DesignerTargetScoreMapper designerTargetScoreMapper; + private final DemandTypeMapper demandTypeMapper; /** * 获取需求统计数据(根据角色返回不同维度的数据) @@ -326,25 +327,53 @@ public class AnalyticsService { * 构建类型分布 */ private List buildTypeDistribution(List tasks) { - Map> tasksByType = tasks.stream() - .filter(t -> t.getDemandTypeId() != null) - .collect(Collectors.groupingBy(t -> String.valueOf(t.getDemandTypeId()))); + // 优先使用 taskTypeId,如果为空则使用 demandTypeId + Map> tasksByType = tasks.stream() + .filter(t -> t.getTaskTypeId() != null || t.getDemandTypeId() != null) + .collect(Collectors.groupingBy(t -> { + if (t.getTaskTypeId() != null) { + return t.getTaskTypeId(); + } + return t.getDemandTypeId(); + })); int totalTasks = tasks.size(); List distribution = new ArrayList<>(); - for (Map.Entry> entry : tasksByType.entrySet()) { + // 预加载所有任务类型名称 + Map typeNameMap = new HashMap<>(); + for (Long typeId : tasksByType.keySet()) { + try { + DemandType demandType = demandTypeMapper.selectById(typeId); + if (demandType != null) { + typeNameMap.put(typeId, demandType.getName()); + } else { + typeNameMap.put(typeId, "未知类型"); + } + } catch (Exception e) { + typeNameMap.put(typeId, "类型" + typeId); + } + } + + for (Map.Entry> entry : tasksByType.entrySet()) { List typeTasks = entry.getValue(); int count = typeTasks.size(); double percentage = totalTasks > 0 ? (double) count / totalTasks * 100 : 0.0; + // 优先使用 scoreReward,其次使用 score BigDecimal totalScore = typeTasks.stream() - .map(Task::getScoreReward) - .filter(Objects::nonNull) + .map(t -> { + if (t.getScoreReward() != null && t.getScoreReward().compareTo(BigDecimal.ZERO) > 0) { + return t.getScoreReward(); + } + return t.getScore() != null ? BigDecimal.valueOf(t.getScore()) : BigDecimal.ZERO; + }) .reduce(BigDecimal.ZERO, BigDecimal::add); + String typeName = typeNameMap.getOrDefault(entry.getKey(), "类型" + entry.getKey()); + distribution.add(DemandStatisticsVO.TypeDistribution.builder() - .typeName("类型" + entry.getKey()) + .typeName(typeName) .count(count) .percentage(percentage) .totalScore(totalScore) diff --git a/backend/src/main/java/com/designhub/service/DesignerService.java b/backend/src/main/java/com/designhub/service/DesignerService.java index 0e2a9e2..473bc67 100644 --- a/backend/src/main/java/com/designhub/service/DesignerService.java +++ b/backend/src/main/java/com/designhub/service/DesignerService.java @@ -3,9 +3,11 @@ package com.designhub.service; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.designhub.entity.Department; import com.designhub.entity.Task; +import com.designhub.entity.TaskItem; import com.designhub.entity.User; import com.designhub.mapper.DepartmentMapper; import com.designhub.mapper.TaskMapper; +import com.designhub.mapper.TaskItemMapper; import com.designhub.mapper.UserMapper; import com.designhub.vo.DesignerStatsVO; import lombok.RequiredArgsConstructor; @@ -24,6 +26,7 @@ public class DesignerService { private final UserMapper userMapper; private final TaskMapper taskMapper; + private final TaskItemMapper taskItemMapper; private final DepartmentMapper departmentMapper; /** @@ -150,21 +153,35 @@ public class DesignerService { // 查询任务统计 Long userId = user.getId(); - // 总接收任务数 - Long totalCountLong = taskMapper.selectCount( + // 总接收任务数(包括主任务和子任务) + Long mainTotalCount = taskMapper.selectCount( new LambdaQueryWrapper() .eq(Task::getReceiverId, userId) + .eq(Task::getDeleted, 0) ); - Integer totalCount = totalCountLong.intValue(); + Long subTotalCount = taskItemMapper.selectCount( + new LambdaQueryWrapper() + .eq(TaskItem::getReceiverId, userId) + ); + Integer totalCount = (mainTotalCount != null ? mainTotalCount.intValue() : 0) + + (subTotalCount != null ? subTotalCount.intValue() : 0); stats.setTotalTaskCount(totalCount); - // 进行中任务数 - Long inProgressCountLong = taskMapper.selectCount( + // 进行中任务数(包括主任务和子任务) + Long mainInProgressCount = taskMapper.selectCount( new LambdaQueryWrapper() .eq(Task::getReceiverId, userId) .eq(Task::getStatus, "in_progress") + .eq(Task::getDeleted, 0) ); - Integer inProgressCount = inProgressCountLong.intValue(); + // 同时查询子任务表中进行中的任务数 + Long subInProgressCount = taskItemMapper.selectCount( + new LambdaQueryWrapper() + .eq(TaskItem::getReceiverId, userId) + .eq(TaskItem::getStatus, "in_progress") + ); + Integer inProgressCount = (mainInProgressCount != null ? mainInProgressCount.intValue() : 0) + + (subInProgressCount != null ? subInProgressCount.intValue() : 0); stats.setInProgressCount(inProgressCount); // 待接单任务数(队列)- 这里应该是用户已接收但还未开始的任务,实际上应该是0 @@ -172,13 +189,20 @@ public class DesignerService { // 队列数可以理解为用户当前可以接的任务数(这个需要从任务大厅查询,这里暂时设为0) stats.setQueueCount(0); - // 已完成任务数 - Long completedCountLong = taskMapper.selectCount( + // 已完成任务数(包括主任务和子任务) + Long mainCompletedCount = taskMapper.selectCount( new LambdaQueryWrapper() .eq(Task::getReceiverId, userId) .eq(Task::getStatus, "completed") + .eq(Task::getDeleted, 0) ); - Integer completedCount = completedCountLong.intValue(); + Long subCompletedCount = taskItemMapper.selectCount( + new LambdaQueryWrapper() + .eq(TaskItem::getReceiverId, userId) + .eq(TaskItem::getStatus, "completed") + ); + Integer completedCount = (mainCompletedCount != null ? mainCompletedCount.intValue() : 0) + + (subCompletedCount != null ? subCompletedCount.intValue() : 0); stats.setCompletedCount(completedCount); // 延误任务数 @@ -210,12 +234,24 @@ public class DesignerService { new LambdaQueryWrapper() .eq(Task::getReceiverId, userId) .eq(Task::getStatus, "completed") + .eq(Task::getDeleted, 0) ); - // 累计获得积分 - Integer totalScore = completedTasks.stream() + // 查询已完成子任务(用于计算积分) + List completedSubTasks = taskItemMapper.selectList( + new LambdaQueryWrapper() + .eq(TaskItem::getReceiverId, userId) + .eq(TaskItem::getStatus, "completed") + ); + + // 累计获得积分(主任务 + 子任务) + Integer mainTaskScore = completedTasks.stream() .mapToInt(task -> task.getScore() != null ? task.getScore() : 0) .sum(); + Integer subTaskScore = completedSubTasks.stream() + .mapToInt(item -> item.getTotalScore() != null ? item.getTotalScore() : 0) + .sum(); + Integer totalScore = mainTaskScore + subTaskScore; stats.setTotalScore(totalScore); // 平均评分