fix: honor secondary demand memberships in member queries

This commit is contained in:
Your Name 2026-03-28 16:22:23 +08:00
parent 7f60c484a0
commit eb6f0c28b1
2 changed files with 66 additions and 20 deletions

View File

@ -1818,14 +1818,14 @@ public class TaskService {
boolean isDemandDeptMemberForPrimaryTask = false;
if (existTask.getIsPrimaryTask() != null && existTask.getIsPrimaryTask() == 1
&& "pending_detail_completion".equals(existTask.getApprovalStatus())) {
// 获取当前用户的部门类型
User currentUser = userMapper.selectById(userId);
if (currentUser != null && currentUser.getDeptId() != null) {
Department userDept = departmentMapper.selectById(currentUser.getDeptId());
if (userDept != null && "demand".equals(userDept.getDeptType())) {
isDemandDeptMemberForPrimaryTask = true;
log.info("需求部门成员 {} 编辑一级需求任务 {}", userId, id);
}
List<Department> userDepartments = userService.getUserDepartments(userId);
isDemandDeptMemberForPrimaryTask = userDepartments.stream()
.filter(java.util.Objects::nonNull)
.anyMatch(dept -> "demand".equals(dept.getDeptType())
&& dept.getStatus() != null && dept.getStatus() == 1
&& dept.getDeleted() != null && dept.getDeleted() == 0);
if (isDemandDeptMemberForPrimaryTask) {
log.info("需求部门成员 {} 编辑一级需求任务 {}", userId, id);
}
}

View File

@ -101,16 +101,15 @@ public class UserService {
);
}
if (deptId != null) {
if (includeChildren != null && includeChildren) {
// 包含子部门:获取当前部门及所有子部门的ID列表
List<Long> deptIds = departmentService.getAllChildDeptIds(deptId);
deptIds.add(deptId); // 包含当前部门
queryWrapper.in(User::getDeptId, deptIds);
log.info("查询用户列表,包含子部门: deptId={}, 所有部门IDs={}", deptId, deptIds);
List<Long> deptIds = resolveScopedDeptIds(deptId, includeChildren);
Set<Long> userIds = resolveUserIdsByDeptIds(deptIds);
if (userIds.isEmpty()) {
queryWrapper.eq(User::getId, -1L);
} else {
// 只查询当前部门
queryWrapper.eq(User::getDeptId, deptId);
queryWrapper.in(User::getId, userIds);
}
log.info("查询用户列表按部门成员过滤: deptId={}, includeChildren={}, deptIds={}, userCount={}",
deptId, includeChildren, deptIds, userIds.size());
}
if (status != null) {
queryWrapper.eq(User::getStatus, status);
@ -416,12 +415,18 @@ public class UserService {
log.info("查询需求部门成员,部门IDs: {}", demandDeptIds);
// 2. 查询这些部门下的所有成员
// 2. 查询这些部门下的所有成员(兼容多部门归属)
Set<Long> userIds = resolveUserIdsByDeptIds(demandDeptIds);
if (userIds.isEmpty()) {
log.info("需求部门成员为空");
return new java.util.ArrayList<>();
}
List<User> users = userMapper.selectList(
new LambdaQueryWrapper<User>()
.in(User::getDeptId, demandDeptIds)
.eq(User::getStatus, 1) // 只查询启用状态的用户
.eq(User::getDeleted, 0) // 只查询未删除的用户
.in(User::getId, userIds)
.eq(User::getStatus, 1)
.eq(User::getDeleted, 0)
.orderByAsc(User::getDeptId)
.orderByAsc(User::getRealName)
);
@ -440,6 +445,47 @@ public class UserService {
return users;
}
private List<Long> resolveScopedDeptIds(Long deptId, Boolean includeChildren) {
List<Long> deptIds = new ArrayList<>();
deptIds.add(deptId);
if (includeChildren != null && includeChildren) {
deptIds.addAll(departmentService.getAllChildDeptIds(deptId));
}
return deptIds.stream().distinct().toList();
}
private Set<Long> resolveUserIdsByDeptIds(List<Long> deptIds) {
if (deptIds == null || deptIds.isEmpty()) {
return Collections.emptySet();
}
Set<Long> userIds = new LinkedHashSet<>();
// 多部门归属是当前组织调整后的主数据来源。
List<UserDepartment> userDepartments = userDepartmentMapper.selectList(
new LambdaQueryWrapper<UserDepartment>()
.in(UserDepartment::getDeptId, deptIds)
);
for (UserDepartment relation : userDepartments) {
if (relation.getUserId() != null) {
userIds.add(relation.getUserId());
}
}
// 兼容旧数据:保留对 sys_user.dept_id 的兜底。
List<User> primaryDeptUsers = userMapper.selectList(
new LambdaQueryWrapper<User>()
.in(User::getDeptId, deptIds)
);
for (User user : primaryDeptUsers) {
if (user.getId() != null) {
userIds.add(user.getId());
}
}
return userIds;
}
/**
* 调整用户所属部门
*/