refactor(system): 优化部门服务中的数据对象使用

- 将 SystemUserVo 中的 dept 字段类型从 SystemDeptVo 改为 SystemDeptTb
- 在 SystemDeptService 中统一使用 SystemDeptTb 对象替代 SystemDeptVo
- 删除了 getDeptVoById 方法并替换为直接使用 getDeptById
- 重构了部门树构建逻辑,优化了部门数据的查询和映射
- 修改了部门删除、查询、验证等相关方法的数据类型处理
- 添加了获取所有部门映射的私有方法以提高查询效率
This commit is contained in:
2026-01-17 09:53:36 +08:00
parent 4eaa383717
commit d413ea4469
3 changed files with 43 additions and 51 deletions
@@ -16,14 +16,15 @@
package cn.odboy.system.dal.model.response; package cn.odboy.system.dal.model.response;
import cn.odboy.base.KitBaseUserTimeTb; 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.SystemJobTb;
import cn.odboy.system.dal.dataobject.SystemRoleTb; import cn.odboy.system.dal.dataobject.SystemRoleTb;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import java.util.Date; import java.util.Date;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
import lombok.Getter;
import lombok.Setter;
@Getter @Getter
@Setter @Setter
@@ -60,7 +61,7 @@ public class SystemUserVo extends KitBaseUserTimeTb {
* 扩展字段 * 扩展字段
*/ */
@ApiModelProperty(value = "用户部门") @ApiModelProperty(value = "用户部门")
private SystemDeptVo dept; private SystemDeptTb dept;
@ApiModelProperty(value = "用户角色") @ApiModelProperty(value = "用户角色")
private Set<SystemRoleTb> roles; private Set<SystemRoleTb> roles;
@ApiModelProperty(value = "用户岗位") @ApiModelProperty(value = "用户岗位")
@@ -102,10 +102,10 @@ public class SystemDeptService {
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void deleteDeptByIds(Set<Long> ids) { public void deleteDeptByIds(Set<Long> ids) {
// 查询部门, 和其所有子部门 // 查询部门, 和其所有子部门
Set<SystemDeptVo> depts = this.traverseDeptByIdWithPids(ids); Set<SystemDeptTb> depts = this.traverseDeptByIdWithPids(ids);
// 验证是否被角色或用户关联 // 验证是否被角色或用户关联
this.verifyBindRelationByIds(depts); this.verifyBindRelationByIds(depts);
for (SystemDeptVo dept : depts) { for (SystemDeptTb dept : depts) {
systemDeptMapper.deleteById(dept.getId()); systemDeptMapper.deleteById(dept.getId());
this.updateDeptSubCnt(dept.getPid()); this.updateDeptSubCnt(dept.getPid());
} }
@@ -166,16 +166,6 @@ public class SystemDeptService {
return list; return list;
} }
/**
* 根据ID查询
*
* @param id /
* @return /
*/
public SystemDeptVo getDeptVoById(Long id) {
return KitBeanUtil.copyToClass(systemDeptMapper.selectById(id), SystemDeptVo.class);
}
/** /**
* 根据部门id遍历所有部门和子部门id集合 * 根据部门id遍历所有部门和子部门id集合
* *
@@ -202,14 +192,15 @@ public class SystemDeptService {
* @return / * @return /
*/ */
public KitPageResult<SystemDeptVo> searchDeptTree(List<Long> ids, Boolean exclude) { public KitPageResult<SystemDeptVo> searchDeptTree(List<Long> ids, Boolean exclude) {
Set<SystemDeptVo> deptSet1 = new LinkedHashSet<>(); Map<Long, SystemDeptTb> allDeptMap = this.getAllDeptMap();
Set<SystemDeptTb> deptSet1 = new LinkedHashSet<>();
for (Long id : ids) { for (Long id : ids) {
// 同级数据 // 同级数据
SystemDeptVo dept = this.getDeptVoById(id); SystemDeptTb dept = allDeptMap.get(id);
// 上级数据 // 上级数据
List<SystemDeptVo> depts = this.querySuperiorDeptByPid(dept, new ArrayList<>()); List<SystemDeptTb> depts = this.querySuperiorDeptByPid(dept, new ArrayList<>(), allDeptMap);
if (exclude) { if (exclude) {
for (SystemDeptVo data : depts) { for (SystemDeptTb data : depts) {
if (data.getId().equals(dept.getPid())) { if (data.getId().equals(dept.getPid())) {
data.setSubCount(data.getSubCount() - 1); data.setSubCount(data.getSubCount() - 1);
} }
@@ -219,18 +210,20 @@ public class SystemDeptService {
} }
deptSet1.addAll(depts); deptSet1.addAll(depts);
} }
List<SystemDeptVo> deptList = new ArrayList<>(deptSet1); List<SystemDeptTb> deptList = new ArrayList<>(deptSet1);
// 构建部门树 // 构建部门树
Set<SystemDeptVo> trees = new LinkedHashSet<>(); Set<SystemDeptVo> trees = new LinkedHashSet<>();
Set<SystemDeptVo> deptSet = new LinkedHashSet<>(); Set<SystemDeptVo> deptSet = new LinkedHashSet<>();
List<String> deptNames = deptList.stream().map(SystemDeptVo::getName).collect(Collectors.toList()); List<String> deptNames = deptList.stream().map(SystemDeptTb::getName).collect(Collectors.toList());
boolean isChild; boolean isChild;
for (SystemDeptVo dept : deptList) { List<SystemDeptVo> systemDeptVos = KitBeanUtil.copyToList(deptList, SystemDeptVo.class);
for (SystemDeptVo dept : systemDeptVos) {
isChild = false; isChild = false;
if (dept.getPid() == null) { if (dept.getPid() == null) {
trees.add(dept); trees.add(dept);
} }
for (SystemDeptVo it : deptList) { for (SystemDeptVo it : systemDeptVos) {
if (it.getPid() != null && dept.getId().equals(it.getPid())) { if (it.getPid() != null && dept.getId().equals(it.getPid())) {
isChild = true; isChild = true;
if (dept.getChildren() == null) { if (dept.getChildren() == null) {
@@ -241,7 +234,7 @@ public class SystemDeptService {
} }
if (isChild) { if (isChild) {
deptSet.add(dept); 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); deptSet.add(dept);
} }
} }
@@ -254,13 +247,18 @@ public class SystemDeptService {
return baseResult; return baseResult;
} }
private Map<Long, SystemDeptTb> getAllDeptMap() {
LambdaQueryWrapper<SystemDeptTb> wrapper = new LambdaQueryWrapper<>();
return systemDeptMapper.selectList(wrapper).stream().collect(Collectors.toMap(SystemDeptTb::getId, i -> i, (m, n) -> m));
}
/** /**
* 验证是否被角色或用户关联 * 验证是否被角色或用户关联
* *
* @param deptSet / * @param deptSet /
*/ */
public void verifyBindRelationByIds(Set<SystemDeptVo> deptSet) { public void verifyBindRelationByIds(Set<SystemDeptTb> deptSet) {
Set<Long> deptIds = deptSet.stream().map(SystemDeptVo::getId).collect(Collectors.toSet()); Set<Long> deptIds = deptSet.stream().map(SystemDeptTb::getId).collect(Collectors.toSet());
if (systemUserDeptService.countUserByDeptIds(deptIds) > 0) { if (systemUserDeptService.countUserByDeptIds(deptIds) > 0) {
throw new BadRequestException("所选部门存在用户关联,请解除后再试!"); throw new BadRequestException("所选部门存在用户关联,请解除后再试!");
} }
@@ -274,13 +272,13 @@ public class SystemDeptService {
* *
* @param ids 部门id集合 * @param ids 部门id集合
*/ */
public Set<SystemDeptVo> traverseDeptByIdWithPids(Set<Long> ids) { public Set<SystemDeptTb> traverseDeptByIdWithPids(Set<Long> ids) {
Set<SystemDeptVo> depts = new HashSet<>(); Set<SystemDeptTb> depts = new HashSet<>();
for (Long id : ids) { for (Long id : ids) {
// 根部门 // 根部门
depts.add(this.getDeptVoById(id)); depts.add(systemDeptMapper.selectById(id));
// 子部门 // 子部门
List<SystemDeptVo> deptList = this.listDeptVoByPid(id); List<SystemDeptTb> deptList = this.listDeptByPid(id);
if (CollUtil.isNotEmpty(deptList)) { if (CollUtil.isNotEmpty(deptList)) {
this.queryRelationDeptByArgs(deptList, depts); this.queryRelationDeptByArgs(deptList, depts);
} }
@@ -337,34 +335,31 @@ public class SystemDeptService {
* @param deptTbList / * @param deptTbList /
* @param depts / * @param depts /
*/ */
private void queryRelationDeptByArgs(List<SystemDeptVo> deptTbList, Set<SystemDeptVo> depts) { private void queryRelationDeptByArgs(List<SystemDeptTb> deptTbList, Set<SystemDeptTb> depts) {
for (SystemDeptVo dept : deptTbList) { for (SystemDeptTb dept : deptTbList) {
depts.add(dept); depts.add(dept);
List<SystemDeptVo> deptList = this.listDeptVoByPid(dept.getId()); List<SystemDeptTb> deptList = this.listDeptByPid(dept.getId());
if (CollUtil.isNotEmpty(deptList)) { if (CollUtil.isNotEmpty(deptList)) {
this.queryRelationDeptByArgs(deptList, depts); this.queryRelationDeptByArgs(deptList, depts);
} }
} }
} }
private List<SystemDeptVo> listDeptVoByPid(Long pid) {
return KitBeanUtil.copyToList(this.listDeptByPid(pid), SystemDeptVo.class);
}
/** /**
* 根据ID查询同级与上级数据 * 根据ID查询同级与上级数据
* *
* @param dept / * @param dept /
* @param deptList / * @param deptList /
* @param allDeptMap
* @return / * @return /
*/ */
private List<SystemDeptVo> querySuperiorDeptByPid(SystemDeptVo dept, List<SystemDeptVo> deptList) { private List<SystemDeptTb> querySuperiorDeptByPid(SystemDeptTb dept, List<SystemDeptTb> deptList, Map<Long, SystemDeptTb> allDeptMap) {
if (dept.getPid() == null) { if (dept.getPid() == null) {
deptList.addAll(this.listRootDept()); deptList.addAll(this.listRootDept());
return deptList; return deptList;
} }
deptList.addAll(this.listDeptVoByPid(dept.getPid())); deptList.addAll(this.listDeptByPid(dept.getPid()));
return querySuperiorDeptByPid(this.getDeptVoById(dept.getPid()), deptList); return querySuperiorDeptByPid(allDeptMap.get(dept.getPid()), deptList, allDeptMap);
} }
/** /**
@@ -372,10 +367,10 @@ public class SystemDeptService {
* *
* @return / * @return /
*/ */
private List<SystemDeptVo> listRootDept() { private List<SystemDeptTb> listRootDept() {
LambdaQueryWrapper<SystemDeptTb> wrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<SystemDeptTb> wrapper = new LambdaQueryWrapper<>();
wrapper.isNull(SystemDeptTb::getPid); 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 * @param id 部门id
* @return / * @return /
*/ */
private SystemDeptTb getDeptById(Long id) { public SystemDeptTb getDeptById(Long id) {
return systemDeptMapper.selectById(id); return systemDeptMapper.selectById(id);
} }
@@ -18,7 +18,6 @@ package cn.odboy.system.service;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.odboy.base.KitPageResult; import cn.odboy.base.KitPageResult;
import cn.odboy.base.KitSelectOptionVo;
import cn.odboy.framework.exception.BadRequestException; import cn.odboy.framework.exception.BadRequestException;
import cn.odboy.framework.properties.AppProperties; import cn.odboy.framework.properties.AppProperties;
import cn.odboy.framework.server.core.KitFileLocalUploadHelper; import cn.odboy.framework.server.core.KitFileLocalUploadHelper;
@@ -368,10 +367,7 @@ public class SystemUserService {
/** /**
* 聚合查询 -> TestPassed * 聚合查询 -> TestPassed
*/ */
public KitPageResult<SystemUserVo> aggregationSearchUserByArgs( public KitPageResult<SystemUserVo> aggregationSearchUserByArgs(Page<SystemUserTb> page, SystemQueryUserArgs args, String currentUsername) {
Page<SystemUserTb> page, SystemQueryUserArgs args,
String currentUsername
) {
if (!ObjectUtils.isEmpty(args.getDeptId())) { if (!ObjectUtils.isEmpty(args.getDeptId())) {
args.getDeptIds().add(args.getDeptId()); args.getDeptIds().add(args.getDeptId());
// 先查找是否存在子节点 // 先查找是否存在子节点
@@ -553,7 +549,7 @@ public class SystemUserService {
SystemUserVo userVo = KitBeanUtil.copyToClass(user, SystemUserVo.class); SystemUserVo userVo = KitBeanUtil.copyToClass(user, SystemUserVo.class);
// 查询关联的部门信息 // 查询关联的部门信息
if (user.getDeptId() != null) { if (user.getDeptId() != null) {
userVo.setDept(systemDeptService.getDeptVoById(user.getDeptId())); userVo.setDept(systemDeptService.getDeptById(user.getDeptId()));
} }
// 查询关联的岗位信息 // 查询关联的岗位信息
userVo.setJobs(systemUserJobService.listUserJobByUserId(user.getId())); userVo.setJobs(systemUserJobService.listUserJobByUserId(user.getId()));