fix: 修复类型分布使用错误字段和设计师进行中任务数未包含子任务的问题

This commit is contained in:
Your Name 2026-01-08 14:21:09 +08:00
parent 4d582170aa
commit 4bd856ea01
2 changed files with 83 additions and 18 deletions

View File

@ -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<DemandStatisticsVO.TypeDistribution> buildTypeDistribution(List<Task> tasks) {
Map<String, List<Task>> tasksByType = tasks.stream()
.filter(t -> t.getDemandTypeId() != null)
.collect(Collectors.groupingBy(t -> String.valueOf(t.getDemandTypeId())));
// 优先使用 taskTypeId,如果为空则使用 demandTypeId
Map<Long, List<Task>> 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<DemandStatisticsVO.TypeDistribution> distribution = new ArrayList<>();
for (Map.Entry<String, List<Task>> entry : tasksByType.entrySet()) {
// 预加载所有任务类型名称
Map<Long, String> 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<Long, List<Task>> entry : tasksByType.entrySet()) {
List<Task> 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)

View File

@ -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<Task>()
.eq(Task::getReceiverId, userId)
.eq(Task::getDeleted, 0)
);
Integer totalCount = totalCountLong.intValue();
Long subTotalCount = taskItemMapper.selectCount(
new LambdaQueryWrapper<TaskItem>()
.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<Task>()
.eq(Task::getReceiverId, userId)
.eq(Task::getStatus, "in_progress")
.eq(Task::getDeleted, 0)
);
Integer inProgressCount = inProgressCountLong.intValue();
// 同时查询子任务表中进行中的任务数
Long subInProgressCount = taskItemMapper.selectCount(
new LambdaQueryWrapper<TaskItem>()
.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<Task>()
.eq(Task::getReceiverId, userId)
.eq(Task::getStatus, "completed")
.eq(Task::getDeleted, 0)
);
Integer completedCount = completedCountLong.intValue();
Long subCompletedCount = taskItemMapper.selectCount(
new LambdaQueryWrapper<TaskItem>()
.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<Task>()
.eq(Task::getReceiverId, userId)
.eq(Task::getStatus, "completed")
.eq(Task::getDeleted, 0)
);
// 累计获得积分
Integer totalScore = completedTasks.stream()
// 查询已完成子任务(用于计算积分)
List<TaskItem> completedSubTasks = taskItemMapper.selectList(
new LambdaQueryWrapper<TaskItem>()
.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);
// 平均评分