fix: 切换部门后数据总览同步更新
- 后端 getDashboardStats 方法添加 deptId 参数支持 - 需求部门视角:统计该部门发起的任务 - 接单部门视角:统计该部门成员接单的任务 - 前端 loadDashboardStats 在切换部门后传递 deptId
This commit is contained in:
parent
a7fce8e618
commit
4ed9a98810
@ -314,11 +314,12 @@ public class TaskController {
|
||||
@GetMapping("/dashboard/stats")
|
||||
public Result<DashboardStatsVO> getDashboardStats(
|
||||
@Parameter(description = "月份,格式:YYYY-MM,不传则查询当月") @RequestParam(required = false) String month,
|
||||
@Parameter(description = "指定部门ID(部门负责人切换视角时使用)") @RequestParam(required = false) Long deptId,
|
||||
@RequestHeader("Authorization") String authHeader) {
|
||||
String token = jwtUtil.extractTokenFromHeader(authHeader);
|
||||
Long userId = jwtUtil.getUserIdFromToken(token);
|
||||
log.info("获取工作台统计数据: userId={}, month={}", userId, month);
|
||||
return Result.success(taskService.getDashboardStats(userId, month));
|
||||
log.info("获取工作台统计数据: userId={}, month={}, deptId={}", userId, month, deptId);
|
||||
return Result.success(taskService.getDashboardStats(userId, month, deptId));
|
||||
}
|
||||
|
||||
@Operation(summary = "获取我的进行中任务")
|
||||
|
||||
@ -1784,14 +1784,30 @@ public class TaskService {
|
||||
/**
|
||||
* 获取工作台统计数据
|
||||
*/
|
||||
public DashboardStatsVO getDashboardStats(Long userId, String month) {
|
||||
public DashboardStatsVO getDashboardStats(Long userId, String month, Long deptId) {
|
||||
try {
|
||||
// 获取用户角色,判断是否为管理员
|
||||
List<Role> roles = roleService.getRolesByUserId(userId);
|
||||
boolean isAdmin = roles.stream()
|
||||
.anyMatch(role -> "ADMIN".equals(role.getRoleCode()) || "SUPER_ADMIN".equals(role.getRoleCode()));
|
||||
|
||||
log.info("获取工作台统计数据: userId={}, isAdmin={}, month={}", userId, isAdmin, month);
|
||||
// 检查是否是部门负责人切换视角(传入了deptId)
|
||||
boolean isDeptView = deptId != null;
|
||||
List<Long> deptAndChildIds = null;
|
||||
String deptType = null;
|
||||
|
||||
if (isDeptView) {
|
||||
// 获取指定部门及其所有子部门
|
||||
deptAndChildIds = getDeptAndChildIds(deptId);
|
||||
// 获取部门类型
|
||||
Department dept = departmentMapper.selectById(deptId);
|
||||
if (dept != null) {
|
||||
deptType = dept.getDeptType();
|
||||
}
|
||||
log.info("部门负责人视角: deptId={}, deptType={}, childDeptCount={}", deptId, deptType, deptAndChildIds.size());
|
||||
}
|
||||
|
||||
log.info("获取工作台统计数据: userId={}, isAdmin={}, month={}, deptId={}", userId, isAdmin, month, deptId);
|
||||
|
||||
// 解析月份,如果不传则使用当前月份
|
||||
LocalDate targetMonth;
|
||||
@ -1828,28 +1844,63 @@ public class TaskService {
|
||||
LocalDateTime lastWeekEndTime = LocalDateTime.of(lastWeekEnd, LocalTime.MAX);
|
||||
|
||||
// 进行中的任务数
|
||||
LambdaQueryWrapper<Task> inProgressQuery = new LambdaQueryWrapper<Task>()
|
||||
.eq(Task::getStatus, "in_progress");
|
||||
if (!isAdmin) {
|
||||
inProgressQuery.eq(Task::getReceiverId, userId);
|
||||
Long inProgressCount = 0L;
|
||||
if (isDeptView && "receiving".equals(deptType)) {
|
||||
// 接单部门视角:统计该部门(含子部门)设计师接单的任务
|
||||
inProgressCount = taskMapper.selectCount(new LambdaQueryWrapper<Task>()
|
||||
.eq(Task::getStatus, "in_progress")
|
||||
.in(Task::getReceiverDeptId, deptAndChildIds));
|
||||
} else if (isDeptView && "demand".equals(deptType)) {
|
||||
// 需求部门视角:统计该部门发起的任务
|
||||
inProgressCount = taskMapper.selectCount(new LambdaQueryWrapper<Task>()
|
||||
.eq(Task::getStatus, "in_progress")
|
||||
.in(Task::getPublisherDeptId, deptAndChildIds));
|
||||
} else if (!isAdmin) {
|
||||
inProgressCount = taskMapper.selectCount(new LambdaQueryWrapper<Task>()
|
||||
.eq(Task::getStatus, "in_progress")
|
||||
.eq(Task::getReceiverId, userId));
|
||||
} else {
|
||||
inProgressCount = taskMapper.selectCount(new LambdaQueryWrapper<Task>()
|
||||
.eq(Task::getStatus, "in_progress"));
|
||||
}
|
||||
Long inProgressCount = taskMapper.selectCount(inProgressQuery);
|
||||
|
||||
// 已完成的任务数
|
||||
LambdaQueryWrapper<Task> completedQuery = new LambdaQueryWrapper<Task>()
|
||||
.eq(Task::getStatus, "completed");
|
||||
if (!isAdmin) {
|
||||
completedQuery.eq(Task::getReceiverId, userId);
|
||||
Long completedCount = 0L;
|
||||
if (isDeptView && "receiving".equals(deptType)) {
|
||||
completedCount = taskMapper.selectCount(new LambdaQueryWrapper<Task>()
|
||||
.eq(Task::getStatus, "completed")
|
||||
.in(Task::getReceiverDeptId, deptAndChildIds));
|
||||
} else if (isDeptView && "demand".equals(deptType)) {
|
||||
completedCount = taskMapper.selectCount(new LambdaQueryWrapper<Task>()
|
||||
.eq(Task::getStatus, "completed")
|
||||
.in(Task::getPublisherDeptId, deptAndChildIds));
|
||||
} else if (!isAdmin) {
|
||||
completedCount = taskMapper.selectCount(new LambdaQueryWrapper<Task>()
|
||||
.eq(Task::getStatus, "completed")
|
||||
.eq(Task::getReceiverId, userId));
|
||||
} else {
|
||||
completedCount = taskMapper.selectCount(new LambdaQueryWrapper<Task>()
|
||||
.eq(Task::getStatus, "completed"));
|
||||
}
|
||||
Long completedCount = taskMapper.selectCount(completedQuery);
|
||||
|
||||
// 待审核的任务数
|
||||
LambdaQueryWrapper<Task> reviewQuery = new LambdaQueryWrapper<Task>()
|
||||
.eq(Task::getStatus, "review");
|
||||
if (!isAdmin) {
|
||||
reviewQuery.eq(Task::getReceiverId, userId);
|
||||
Long reviewCount = 0L;
|
||||
if (isDeptView && "receiving".equals(deptType)) {
|
||||
reviewCount = taskMapper.selectCount(new LambdaQueryWrapper<Task>()
|
||||
.eq(Task::getStatus, "review")
|
||||
.in(Task::getReceiverDeptId, deptAndChildIds));
|
||||
} else if (isDeptView && "demand".equals(deptType)) {
|
||||
reviewCount = taskMapper.selectCount(new LambdaQueryWrapper<Task>()
|
||||
.eq(Task::getStatus, "review")
|
||||
.in(Task::getPublisherDeptId, deptAndChildIds));
|
||||
} else if (!isAdmin) {
|
||||
reviewCount = taskMapper.selectCount(new LambdaQueryWrapper<Task>()
|
||||
.eq(Task::getStatus, "review")
|
||||
.eq(Task::getReceiverId, userId));
|
||||
} else {
|
||||
reviewCount = taskMapper.selectCount(new LambdaQueryWrapper<Task>()
|
||||
.eq(Task::getStatus, "review"));
|
||||
}
|
||||
Long reviewCount = taskMapper.selectCount(reviewQuery);
|
||||
|
||||
// 今日开始的任务数
|
||||
LambdaQueryWrapper<Task> todayStartedQuery = new LambdaQueryWrapper<Task>()
|
||||
@ -1924,13 +1975,30 @@ public class TaskService {
|
||||
.mapToInt(t -> t.getScoreReward().intValue())
|
||||
.sum();
|
||||
|
||||
// 获取当前用户当月发布的任务数
|
||||
LambdaQueryWrapper<Task> publishedQuery = new LambdaQueryWrapper<Task>()
|
||||
.eq(Task::getPublisherId, userId)
|
||||
.ge(Task::getCreateTime, monthStart)
|
||||
.le(Task::getCreateTime, monthEnd)
|
||||
.eq(Task::getDeleted, 0);
|
||||
Long publishedCount = taskMapper.selectCount(publishedQuery);
|
||||
// 获取当月发布的任务数
|
||||
Long publishedCount = 0L;
|
||||
if (isDeptView && "demand".equals(deptType)) {
|
||||
// 需求部门视角:统计该部门发起的任务数
|
||||
publishedCount = taskMapper.selectCount(new LambdaQueryWrapper<Task>()
|
||||
.in(Task::getPublisherDeptId, deptAndChildIds)
|
||||
.ge(Task::getCreateTime, monthStart)
|
||||
.le(Task::getCreateTime, monthEnd)
|
||||
.eq(Task::getDeleted, 0));
|
||||
} else if (isDeptView && "receiving".equals(deptType)) {
|
||||
// 接单部门视角:统计该部门接单的任务数
|
||||
publishedCount = taskMapper.selectCount(new LambdaQueryWrapper<Task>()
|
||||
.in(Task::getReceiverDeptId, deptAndChildIds)
|
||||
.ge(Task::getCreateTime, monthStart)
|
||||
.le(Task::getCreateTime, monthEnd)
|
||||
.eq(Task::getDeleted, 0));
|
||||
} else {
|
||||
// 当前用户发布的任务数
|
||||
publishedCount = taskMapper.selectCount(new LambdaQueryWrapper<Task>()
|
||||
.eq(Task::getPublisherId, userId)
|
||||
.ge(Task::getCreateTime, monthStart)
|
||||
.le(Task::getCreateTime, monthEnd)
|
||||
.eq(Task::getDeleted, 0));
|
||||
}
|
||||
|
||||
DashboardStatsVO result = DashboardStatsVO.builder()
|
||||
.availableCount(availableCount.intValue())
|
||||
@ -2527,5 +2595,30 @@ public class TaskService {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定部门及其所有子部门的ID列表
|
||||
*/
|
||||
private List<Long> getDeptAndChildIds(Long deptId) {
|
||||
List<Long> result = new java.util.ArrayList<>();
|
||||
result.add(deptId);
|
||||
collectChildDeptIds(deptId, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归收集所有子部门ID
|
||||
*/
|
||||
private void collectChildDeptIds(Long parentId, List<Long> result) {
|
||||
List<Department> children = departmentMapper.selectList(
|
||||
new LambdaQueryWrapper<Department>()
|
||||
.eq(Department::getParentId, parentId)
|
||||
.eq(Department::getStatus, 1)
|
||||
);
|
||||
for (Department child : children) {
|
||||
result.add(child.getId());
|
||||
collectChildDeptIds(child.getId(), result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -343,7 +343,7 @@ export interface DashboardStats {
|
||||
/**
|
||||
* 获取工作台统计数据
|
||||
*/
|
||||
export const getDashboardStats = (params?: { month?: string }) => {
|
||||
export const getDashboardStats = (params?: { month?: string; deptId?: number }) => {
|
||||
return request.get<DashboardStats>('/tasks/dashboard/stats', { params })
|
||||
}
|
||||
|
||||
|
||||
@ -3930,7 +3930,13 @@ const handlePublishedFiltersReset = () => {
|
||||
const loadDashboardStats = async () => {
|
||||
try {
|
||||
const month = selectedMonth.value?.format('YYYY-MM')
|
||||
const response = await taskApi.getDashboardStats(month ? { month } : undefined)
|
||||
const params: { month?: string; deptId?: number } = {}
|
||||
if (month) params.month = month
|
||||
// 如果切换了部门视角,传递部门ID
|
||||
if (currentDeptView.value !== undefined) {
|
||||
params.deptId = currentDeptView.value
|
||||
}
|
||||
const response = await taskApi.getDashboardStats(Object.keys(params).length > 0 ? params : undefined)
|
||||
if (response.code === 200) {
|
||||
const data = response.data
|
||||
dataCards.value[0].value = data.publishedCount || 0 // 发布的任务数(当月)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user