diff --git a/cutejava/cutejava-module-system/src/main/java/cn/odboy/system/dal/model/response/SystemUserVo.java b/cutejava/cutejava-module-system/src/main/java/cn/odboy/system/dal/model/response/SystemUserVo.java index 971ca461..cb947f10 100644 --- a/cutejava/cutejava-module-system/src/main/java/cn/odboy/system/dal/model/response/SystemUserVo.java +++ b/cutejava/cutejava-module-system/src/main/java/cn/odboy/system/dal/model/response/SystemUserVo.java @@ -16,14 +16,15 @@ package cn.odboy.system.dal.model.response; import cn.odboy.base.KitBaseUserTimeTb; +import cn.odboy.system.dal.dataobject.SystemDeptTb; import cn.odboy.system.dal.dataobject.SystemJobTb; import cn.odboy.system.dal.dataobject.SystemRoleTb; import io.swagger.annotations.ApiModelProperty; +import lombok.Getter; +import lombok.Setter; import java.util.Date; import java.util.Objects; import java.util.Set; -import lombok.Getter; -import lombok.Setter; @Getter @Setter @@ -60,7 +61,7 @@ public class SystemUserVo extends KitBaseUserTimeTb { * 扩展字段 */ @ApiModelProperty(value = "用户部门") - private SystemDeptVo dept; + private SystemDeptTb dept; @ApiModelProperty(value = "用户角色") private Set roles; @ApiModelProperty(value = "用户岗位") diff --git a/cutejava/cutejava-module-system/src/main/java/cn/odboy/system/service/SystemDeptService.java b/cutejava/cutejava-module-system/src/main/java/cn/odboy/system/service/SystemDeptService.java index f6633afb..a2a6cf86 100644 --- a/cutejava/cutejava-module-system/src/main/java/cn/odboy/system/service/SystemDeptService.java +++ b/cutejava/cutejava-module-system/src/main/java/cn/odboy/system/service/SystemDeptService.java @@ -102,10 +102,10 @@ public class SystemDeptService { @Transactional(rollbackFor = Exception.class) public void deleteDeptByIds(Set ids) { // 查询部门, 和其所有子部门 - Set depts = this.traverseDeptByIdWithPids(ids); + Set depts = this.traverseDeptByIdWithPids(ids); // 验证是否被角色或用户关联 this.verifyBindRelationByIds(depts); - for (SystemDeptVo dept : depts) { + for (SystemDeptTb dept : depts) { systemDeptMapper.deleteById(dept.getId()); this.updateDeptSubCnt(dept.getPid()); } @@ -166,16 +166,6 @@ public class SystemDeptService { return list; } - /** - * 根据ID查询 - * - * @param id / - * @return / - */ - public SystemDeptVo getDeptVoById(Long id) { - return KitBeanUtil.copyToClass(systemDeptMapper.selectById(id), SystemDeptVo.class); - } - /** * 根据部门id遍历所有部门和子部门id集合 * @@ -202,14 +192,15 @@ public class SystemDeptService { * @return / */ public KitPageResult searchDeptTree(List ids, Boolean exclude) { - Set deptSet1 = new LinkedHashSet<>(); + Map allDeptMap = this.getAllDeptMap(); + Set deptSet1 = new LinkedHashSet<>(); for (Long id : ids) { // 同级数据 - SystemDeptVo dept = this.getDeptVoById(id); + SystemDeptTb dept = allDeptMap.get(id); // 上级数据 - List depts = this.querySuperiorDeptByPid(dept, new ArrayList<>()); + List depts = this.querySuperiorDeptByPid(dept, new ArrayList<>(), allDeptMap); if (exclude) { - for (SystemDeptVo data : depts) { + for (SystemDeptTb data : depts) { if (data.getId().equals(dept.getPid())) { data.setSubCount(data.getSubCount() - 1); } @@ -219,18 +210,20 @@ public class SystemDeptService { } deptSet1.addAll(depts); } - List deptList = new ArrayList<>(deptSet1); + List deptList = new ArrayList<>(deptSet1); // 构建部门树 Set trees = new LinkedHashSet<>(); Set deptSet = new LinkedHashSet<>(); - List deptNames = deptList.stream().map(SystemDeptVo::getName).collect(Collectors.toList()); + List deptNames = deptList.stream().map(SystemDeptTb::getName).collect(Collectors.toList()); + boolean isChild; - for (SystemDeptVo dept : deptList) { + List systemDeptVos = KitBeanUtil.copyToList(deptList, SystemDeptVo.class); + for (SystemDeptVo dept : systemDeptVos) { isChild = false; if (dept.getPid() == null) { trees.add(dept); } - for (SystemDeptVo it : deptList) { + for (SystemDeptVo it : systemDeptVos) { if (it.getPid() != null && dept.getId().equals(it.getPid())) { isChild = true; if (dept.getChildren() == null) { @@ -241,7 +234,7 @@ public class SystemDeptService { } if (isChild) { deptSet.add(dept); - } else if (dept.getPid() != null && !deptNames.contains(this.getDeptVoById(dept.getPid()).getName())) { + } else if (dept.getPid() != null && !deptNames.contains(allDeptMap.get(dept.getPid()).getName())) { deptSet.add(dept); } } @@ -254,13 +247,18 @@ public class SystemDeptService { return baseResult; } + private Map getAllDeptMap() { + LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); + return systemDeptMapper.selectList(wrapper).stream().collect(Collectors.toMap(SystemDeptTb::getId, i -> i, (m, n) -> m)); + } + /** * 验证是否被角色或用户关联 * * @param deptSet / */ - public void verifyBindRelationByIds(Set deptSet) { - Set deptIds = deptSet.stream().map(SystemDeptVo::getId).collect(Collectors.toSet()); + public void verifyBindRelationByIds(Set deptSet) { + Set deptIds = deptSet.stream().map(SystemDeptTb::getId).collect(Collectors.toSet()); if (systemUserDeptService.countUserByDeptIds(deptIds) > 0) { throw new BadRequestException("所选部门存在用户关联,请解除后再试!"); } @@ -274,13 +272,13 @@ public class SystemDeptService { * * @param ids 部门id集合 */ - public Set traverseDeptByIdWithPids(Set ids) { - Set depts = new HashSet<>(); + public Set traverseDeptByIdWithPids(Set ids) { + Set depts = new HashSet<>(); for (Long id : ids) { // 根部门 - depts.add(this.getDeptVoById(id)); + depts.add(systemDeptMapper.selectById(id)); // 子部门 - List deptList = this.listDeptVoByPid(id); + List deptList = this.listDeptByPid(id); if (CollUtil.isNotEmpty(deptList)) { this.queryRelationDeptByArgs(deptList, depts); } @@ -337,34 +335,31 @@ public class SystemDeptService { * @param deptTbList / * @param depts / */ - private void queryRelationDeptByArgs(List deptTbList, Set depts) { - for (SystemDeptVo dept : deptTbList) { + private void queryRelationDeptByArgs(List deptTbList, Set depts) { + for (SystemDeptTb dept : deptTbList) { depts.add(dept); - List deptList = this.listDeptVoByPid(dept.getId()); + List deptList = this.listDeptByPid(dept.getId()); if (CollUtil.isNotEmpty(deptList)) { this.queryRelationDeptByArgs(deptList, depts); } } } - private List listDeptVoByPid(Long pid) { - return KitBeanUtil.copyToList(this.listDeptByPid(pid), SystemDeptVo.class); - } - /** * 根据ID查询同级与上级数据 * - * @param dept / - * @param deptList / + * @param dept / + * @param deptList / + * @param allDeptMap * @return / */ - private List querySuperiorDeptByPid(SystemDeptVo dept, List deptList) { + private List querySuperiorDeptByPid(SystemDeptTb dept, List deptList, Map allDeptMap) { if (dept.getPid() == null) { deptList.addAll(this.listRootDept()); return deptList; } - deptList.addAll(this.listDeptVoByPid(dept.getPid())); - return querySuperiorDeptByPid(this.getDeptVoById(dept.getPid()), deptList); + deptList.addAll(this.listDeptByPid(dept.getPid())); + return querySuperiorDeptByPid(allDeptMap.get(dept.getPid()), deptList, allDeptMap); } /** @@ -372,10 +367,10 @@ public class SystemDeptService { * * @return / */ - private List listRootDept() { + private List listRootDept() { LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.isNull(SystemDeptTb::getPid); - return KitBeanUtil.copyToList(systemDeptMapper.selectList(wrapper), SystemDeptVo.class); + return systemDeptMapper.selectList(wrapper); } /** @@ -384,7 +379,7 @@ public class SystemDeptService { * @param id 部门id * @return / */ - private SystemDeptTb getDeptById(Long id) { + public SystemDeptTb getDeptById(Long id) { return systemDeptMapper.selectById(id); } diff --git a/cutejava/cutejava-module-system/src/main/java/cn/odboy/system/service/SystemUserService.java b/cutejava/cutejava-module-system/src/main/java/cn/odboy/system/service/SystemUserService.java index c12633de..7ac4ab7c 100644 --- a/cutejava/cutejava-module-system/src/main/java/cn/odboy/system/service/SystemUserService.java +++ b/cutejava/cutejava-module-system/src/main/java/cn/odboy/system/service/SystemUserService.java @@ -18,7 +18,6 @@ package cn.odboy.system.service; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.StrUtil; import cn.odboy.base.KitPageResult; -import cn.odboy.base.KitSelectOptionVo; import cn.odboy.framework.exception.BadRequestException; import cn.odboy.framework.properties.AppProperties; import cn.odboy.framework.server.core.KitFileLocalUploadHelper; @@ -368,10 +367,7 @@ public class SystemUserService { /** * 聚合查询 -> TestPassed */ - public KitPageResult aggregationSearchUserByArgs( - Page page, SystemQueryUserArgs args, - String currentUsername - ) { + public KitPageResult aggregationSearchUserByArgs(Page page, SystemQueryUserArgs args, String currentUsername) { if (!ObjectUtils.isEmpty(args.getDeptId())) { args.getDeptIds().add(args.getDeptId()); // 先查找是否存在子节点 @@ -553,7 +549,7 @@ public class SystemUserService { SystemUserVo userVo = KitBeanUtil.copyToClass(user, SystemUserVo.class); // 查询关联的部门信息 if (user.getDeptId() != null) { - userVo.setDept(systemDeptService.getDeptVoById(user.getDeptId())); + userVo.setDept(systemDeptService.getDeptById(user.getDeptId())); } // 查询关联的岗位信息 userVo.setJobs(systemUserJobService.listUserJobByUserId(user.getId()));