学员部门包含子部门所有学员数量

This commit is contained in:
wsw 2024-06-02 14:57:15 +08:00
parent 4d527cff77
commit 7c84e185a6
4 changed files with 69 additions and 6 deletions

View File

@ -38,6 +38,7 @@ import xyz.playedu.common.domain.Department;
import xyz.playedu.common.domain.User;
import xyz.playedu.common.exception.NotFoundException;
import xyz.playedu.common.service.DepartmentService;
import xyz.playedu.common.service.UserDepartmentService;
import xyz.playedu.common.service.UserService;
import xyz.playedu.common.types.JsonResponse;
import xyz.playedu.common.types.paginate.PaginationResult;
@ -71,12 +72,39 @@ public class DepartmentController {
@Autowired private LDAPBus ldapBus;
@Autowired private UserDepartmentService userDepartmentService;
@GetMapping("/index")
@Log(title = "部门-列表", businessType = BusinessTypeConstant.GET)
public JsonResponse index() {
HashMap<String, Object> data = new HashMap<>();
data.put("departments", departmentService.groupByParent());
data.put("dep_user_count", departmentService.getDepartmentsUserCount());
HashMap<Integer, Integer> depUserCount = new HashMap<>();
List<Department> allDepartmentList = departmentService.all();
if (StringUtil.isNotEmpty(allDepartmentList)) {
for (Department dep : allDepartmentList) {
List<Integer> depIds = new ArrayList<>();
depIds.add(dep.getId());
String parentChain = "";
if (StringUtil.isEmpty(dep.getParentChain())) {
parentChain = dep.getId() + "";
} else {
parentChain = dep.getParentChain() + "," + dep.getId();
}
// 获取所有子部门ID
List<Department> childDepartmentList =
departmentService.getChildDepartmentsByParentChain(
dep.getId(), parentChain);
if (StringUtil.isNotEmpty(childDepartmentList)) {
depIds.addAll(childDepartmentList.stream().map(Department::getId).toList());
}
List<Integer> departmentUserIds = userDepartmentService.getUserIdsByDepIds(depIds);
depUserCount.put(
dep.getId(), departmentUserIds.stream().distinct().toList().size());
}
}
data.put("dep_user_count", depUserCount);
data.put("user_total", userService.total());
return JsonResponse.data(data);
}

View File

@ -107,11 +107,31 @@ public class UserController {
String createdAt = MapUtils.getString(params, "created_at");
String depIdsStr = MapUtils.getString(params, "dep_ids");
List<Integer> depIds = null;
if (depIdsStr != null && !depIdsStr.trim().isEmpty()) {
if ("0".equals(depIdsStr)) {
depIds = new ArrayList<>();
} else {
depIds = Arrays.stream(depIdsStr.split(",")).map(Integer::valueOf).toList();
if (StringUtil.isNotEmpty(depIdsStr)) {
depIds = new ArrayList<>();
if (!"0".equals(depIdsStr)) {
List<Department> departmentList =
departmentService.chunk(
Arrays.stream(depIdsStr.split(",")).map(Integer::valueOf).toList());
if (StringUtil.isNotEmpty(departmentList)) {
for (Department dep : departmentList) {
depIds.add(dep.getId());
String parentChain = "";
if (StringUtil.isEmpty(dep.getParentChain())) {
parentChain = dep.getId() + "";
} else {
parentChain = dep.getParentChain() + "," + dep.getId();
}
// 获取所有子部门ID
List<Department> childDepartmentList =
departmentService.getChildDepartmentsByParentChain(
dep.getId(), parentChain);
if (StringUtil.isNotEmpty(childDepartmentList)) {
depIds.addAll(
childDepartmentList.stream().map(Department::getId).toList());
}
}
}
}
}

View File

@ -70,4 +70,6 @@ public interface DepartmentService extends IService<Department> {
Department findByName(String name, Integer parentId);
List<Department> getChildDepartmentsByParentId(Integer parentId);
List<Department> getChildDepartmentsByParentChain(Integer parentId, String parentChain);
}

View File

@ -32,6 +32,7 @@ import xyz.playedu.common.mapper.DepartmentMapper;
import xyz.playedu.common.service.DepartmentService;
import xyz.playedu.common.service.UserDepartmentService;
import xyz.playedu.common.types.mapper.DepartmentsUserCountMapRes;
import xyz.playedu.common.util.StringUtil;
import java.util.*;
import java.util.stream.Collectors;
@ -302,4 +303,16 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
.or()
.likeRight("parent_chain", parentId + ","));
}
@Override
public List<Department> getChildDepartmentsByParentChain(Integer parentId, String parentChain) {
if (StringUtil.isEmpty(parentChain)) {
return new ArrayList<>();
}
return list(
query().getWrapper()
.eq("parent_id", parentId)
.or()
.likeRight("parent_chain", parentChain + ","));
}
}