部门添加祖级列表字段
This commit is contained in:
parent
b58f2a9d67
commit
7e263447be
@ -18,6 +18,9 @@ public class DeptRespVO {
|
|||||||
@Schema(description = "父部门 ID", example = "1024")
|
@Schema(description = "父部门 ID", example = "1024")
|
||||||
private Long parentId;
|
private Long parentId;
|
||||||
|
|
||||||
|
@Schema(description = "祖级列表", example = "1024")
|
||||||
|
private String ancestors;
|
||||||
|
|
||||||
@Schema(description = "显示顺序", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
|
@Schema(description = "显示顺序", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
|
||||||
private Integer sort;
|
private Integer sort;
|
||||||
|
|
||||||
|
@ -25,6 +25,9 @@ public class DeptSaveReqVO {
|
|||||||
@Schema(description = "父部门 ID", example = "1024")
|
@Schema(description = "父部门 ID", example = "1024")
|
||||||
private Long parentId;
|
private Long parentId;
|
||||||
|
|
||||||
|
@Schema(description = "祖级列表", example = "1024")
|
||||||
|
private String ancestors;
|
||||||
|
|
||||||
@Schema(description = "显示顺序", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
|
@Schema(description = "显示顺序", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
|
||||||
@NotNull(message = "显示顺序不能为空")
|
@NotNull(message = "显示顺序不能为空")
|
||||||
private Integer sort;
|
private Integer sort;
|
||||||
|
@ -38,6 +38,10 @@ public class DeptDO extends TenantBaseDO {
|
|||||||
* 关联 {@link #id}
|
* 关联 {@link #id}
|
||||||
*/
|
*/
|
||||||
private Long parentId;
|
private Long parentId;
|
||||||
|
/**
|
||||||
|
* 祖级列表
|
||||||
|
*/
|
||||||
|
private String ancestors;
|
||||||
/**
|
/**
|
||||||
* 显示顺序
|
* 显示顺序
|
||||||
*/
|
*/
|
||||||
|
@ -45,8 +45,8 @@ public class DeptServiceImpl implements DeptService {
|
|||||||
if (createReqVO.getParentId() == null) {
|
if (createReqVO.getParentId() == null) {
|
||||||
createReqVO.setParentId(DeptDO.PARENT_ID_ROOT);
|
createReqVO.setParentId(DeptDO.PARENT_ID_ROOT);
|
||||||
}
|
}
|
||||||
// 校验父部门的有效性
|
// 校验父部门的有效性同时顺便设置祖级列表
|
||||||
validateParentDept(null, createReqVO.getParentId());
|
createReqVO.setAncestors(validateParentDept(null, createReqVO.getParentId()));
|
||||||
// 校验部门名的唯一性
|
// 校验部门名的唯一性
|
||||||
validateDeptNameUnique(null, createReqVO.getParentId(), createReqVO.getName());
|
validateDeptNameUnique(null, createReqVO.getParentId(), createReqVO.getName());
|
||||||
|
|
||||||
@ -66,8 +66,8 @@ public class DeptServiceImpl implements DeptService {
|
|||||||
}
|
}
|
||||||
// 校验自己存在
|
// 校验自己存在
|
||||||
validateDeptExists(updateReqVO.getId());
|
validateDeptExists(updateReqVO.getId());
|
||||||
// 校验父部门的有效性
|
// 校验父部门的有效性同时顺便设置祖级列表
|
||||||
validateParentDept(updateReqVO.getId(), updateReqVO.getParentId());
|
updateReqVO.setAncestors(validateParentDept(null, updateReqVO.getParentId()));
|
||||||
// 校验部门名的唯一性
|
// 校验部门名的唯一性
|
||||||
validateDeptNameUnique(updateReqVO.getId(), updateReqVO.getParentId(), updateReqVO.getName());
|
validateDeptNameUnique(updateReqVO.getId(), updateReqVO.getParentId(), updateReqVO.getName());
|
||||||
|
|
||||||
@ -116,9 +116,10 @@ public class DeptServiceImpl implements DeptService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
void validateParentDept(Long id, Long parentId) {
|
String validateParentDept(Long id, Long parentId) {
|
||||||
|
StringBuilder ancestorsBuilder = new StringBuilder(String.valueOf(parentId));
|
||||||
if (parentId == null || DeptDO.PARENT_ID_ROOT.equals(parentId)) {
|
if (parentId == null || DeptDO.PARENT_ID_ROOT.equals(parentId)) {
|
||||||
return;
|
return ancestorsBuilder.toString();
|
||||||
}
|
}
|
||||||
// 1. 不能设置自己为父部门
|
// 1. 不能设置自己为父部门
|
||||||
if (Objects.equals(id, parentId)) {
|
if (Objects.equals(id, parentId)) {
|
||||||
@ -130,12 +131,26 @@ public class DeptServiceImpl implements DeptService {
|
|||||||
throw exception(DEPT_PARENT_NOT_EXITS);
|
throw exception(DEPT_PARENT_NOT_EXITS);
|
||||||
}
|
}
|
||||||
// 3. 递归校验父部门,如果父部门是自己的子部门,则报错,避免形成环路
|
// 3. 递归校验父部门,如果父部门是自己的子部门,则报错,避免形成环路
|
||||||
if (id == null) { // id 为空,说明新增,不需要考虑环路
|
if (id == null) { // id 为空,说明新增,不需要考虑环路,但是需要拼接祖级列表
|
||||||
return;
|
for (int i = 0; i < Short.MAX_VALUE; i++) {
|
||||||
|
// 3.1 校验环路
|
||||||
|
parentId = parentDept.getParentId();
|
||||||
|
ancestorsBuilder.insert(0, parentId + ",");
|
||||||
|
// 3.2 继续递归下一级父部门
|
||||||
|
if (parentId == null || DeptDO.PARENT_ID_ROOT.equals(parentId)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
parentDept = deptMapper.selectById(parentId);
|
||||||
|
if (parentDept == null) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ancestorsBuilder.toString();
|
||||||
}
|
}
|
||||||
for (int i = 0; i < Short.MAX_VALUE; i++) {
|
for (int i = 0; i < Short.MAX_VALUE; i++) {
|
||||||
// 3.1 校验环路
|
// 3.1 校验环路
|
||||||
parentId = parentDept.getParentId();
|
parentId = parentDept.getParentId();
|
||||||
|
ancestorsBuilder.insert(0, parentId + ",");
|
||||||
if (Objects.equals(id, parentId)) {
|
if (Objects.equals(id, parentId)) {
|
||||||
throw exception(DEPT_PARENT_IS_CHILD);
|
throw exception(DEPT_PARENT_IS_CHILD);
|
||||||
}
|
}
|
||||||
@ -148,6 +163,7 @@ public class DeptServiceImpl implements DeptService {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return ancestorsBuilder.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
|
Loading…
Reference in New Issue
Block a user