优化:优化修改部门的代码逻辑

1.启用部门,其所有上级部门需要处于启用状态;禁用部门,其所有下级部门需要处于禁用状态
2.新增部门时,默认为禁用状态
This commit is contained in:
Charles7c 2023-03-26 11:46:26 +08:00
parent bc54acd60b
commit 59d9fd33e4

View File

@ -70,7 +70,7 @@ public class DeptServiceImpl extends BaseServiceImpl<DeptMapper, DeptDO, DeptVO,
CheckUtils.throwIf(isExists, "新增失败,[{}] 已存在", name); CheckUtils.throwIf(isExists, "新增失败,[{}] 已存在", name);
request.setAncestors(this.getAncestors(request.getParentId())); request.setAncestors(this.getAncestors(request.getParentId()));
request.setStatus(DisEnableStatusEnum.ENABLE); request.setStatus(DisEnableStatusEnum.DISABLE);
return super.add(request); return super.add(request);
} }
@ -81,15 +81,27 @@ public class DeptServiceImpl extends BaseServiceImpl<DeptMapper, DeptDO, DeptVO,
boolean isExists = this.checkNameExists(name, request.getParentId(), id); boolean isExists = this.checkNameExists(name, request.getParentId(), id);
CheckUtils.throwIf(isExists, "修改失败,[{}] 已存在", name); CheckUtils.throwIf(isExists, "修改失败,[{}] 已存在", name);
DeptDO oldDept = super.getById(id); DeptDO oldDept = super.getById(id);
String oldName = oldDept.getName();
DisEnableStatusEnum newStatus = request.getStatus();
Long oldParentId = oldDept.getParentId();
if (DataTypeEnum.SYSTEM.equals(oldDept.getType())) { if (DataTypeEnum.SYSTEM.equals(oldDept.getType())) {
CheckUtils.throwIfEqual(DisEnableStatusEnum.DISABLE, request.getStatus(), "[{}] 是系统内置部门,不允许禁用", CheckUtils.throwIfEqual(DisEnableStatusEnum.DISABLE, newStatus, "[{}] 是系统内置部门,不允许禁用", oldName);
oldDept.getName()); CheckUtils.throwIfNotEqual(request.getParentId(), oldParentId, "[{}] 是系统内置部门,不允许变更上级部门", oldName);
CheckUtils.throwIfNotEqual(request.getParentId(), oldDept.getParentId(), "[{}] 是系统内置部门,不允许变更上级部门", }
oldDept.getName()); // 启用/禁用部门
if (ObjectUtil.notEqual(newStatus, oldDept.getStatus())) {
List<DeptDO> children = this.listChildren(id);
long enabledChildrenCount =
children.stream().filter(d -> DisEnableStatusEnum.ENABLE.equals(d.getStatus())).count();
CheckUtils.throwIf(DisEnableStatusEnum.DISABLE.equals(newStatus) && enabledChildrenCount > 0,
"禁用 [{}] 前,请先禁用其所有下级部门", oldName);
DeptDO oldParentDept = this.getByParentId(oldParentId);
CheckUtils.throwIf(DisEnableStatusEnum.ENABLE.equals(newStatus)
&& DisEnableStatusEnum.DISABLE.equals(oldParentDept.getStatus()), "启用 [{}] 前,请先启用其所有上级部门", oldName);
} }
// 变更上级部门 // 变更上级部门
if (ObjectUtil.notEqual(request.getParentId(), oldDept.getParentId())) { if (ObjectUtil.notEqual(request.getParentId(), oldParentId)) {
// 更新祖级列表 // 更新祖级列表
String newAncestors = this.getAncestors(request.getParentId()); String newAncestors = this.getAncestors(request.getParentId());
request.setAncestors(newAncestors); request.setAncestors(newAncestors);
@ -153,9 +165,32 @@ public class DeptServiceImpl extends BaseServiceImpl<DeptMapper, DeptDO, DeptVO,
* @return 祖级列表 * @return 祖级列表
*/ */
private String getAncestors(Long parentId) { private String getAncestors(Long parentId) {
DeptDO parentDept = this.getByParentId(parentId);
return String.format("%s,%s", parentDept.getAncestors(), parentId);
}
/**
* 根据上级部门 ID 查询
*
* @param parentId
* 上级部门 ID
* @return 上级部门信息
*/
private DeptDO getByParentId(Long parentId) {
DeptDO parentDept = baseMapper.selectById(parentId); DeptDO parentDept = baseMapper.selectById(parentId);
CheckUtils.throwIfNull(parentDept, "上级部门不存在"); CheckUtils.throwIfNull(parentDept, "上级部门不存在");
return String.format("%s,%s", parentDept.getAncestors(), parentId); return parentDept;
}
/**
* 查询子部门列表
*
* @param id
* ID
* @return 子部门列表
*/
private List<DeptDO> listChildren(Long id) {
return baseMapper.lambdaQuery().apply(String.format("find_in_set(%s, `ancestors`)", id)).list();
} }
/** /**
@ -169,8 +204,7 @@ public class DeptServiceImpl extends BaseServiceImpl<DeptMapper, DeptDO, DeptVO,
* ID * ID
*/ */
private void updateChildrenAncestors(String newAncestors, String oldAncestors, Long id) { private void updateChildrenAncestors(String newAncestors, String oldAncestors, Long id) {
List<DeptDO> children = List<DeptDO> children = this.listChildren(id);
baseMapper.lambdaQuery().apply(String.format("find_in_set(%s, `ancestors`)", id)).list();
if (CollUtil.isEmpty(children)) { if (CollUtil.isEmpty(children)) {
return; return;
} }