From 7e263447be33fda5e421c705d3230f655eb4a3dc Mon Sep 17 00:00:00 2001 From: Vinjor Date: Wed, 7 Aug 2024 16:40:54 +0800 Subject: [PATCH] =?UTF-8?q?=E9=83=A8=E9=97=A8=E6=B7=BB=E5=8A=A0=E7=A5=96?= =?UTF-8?q?=E7=BA=A7=E5=88=97=E8=A1=A8=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/dept/vo/dept/DeptRespVO.java | 3 ++ .../admin/dept/vo/dept/DeptSaveReqVO.java | 3 ++ .../system/dal/dataobject/dept/DeptDO.java | 4 +++ .../system/service/dept/DeptServiceImpl.java | 32 ++++++++++++++----- 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dept/vo/dept/DeptRespVO.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dept/vo/dept/DeptRespVO.java index 83e6ed08..bd111e3b 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dept/vo/dept/DeptRespVO.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dept/vo/dept/DeptRespVO.java @@ -18,6 +18,9 @@ public class DeptRespVO { @Schema(description = "父部门 ID", example = "1024") private Long parentId; + @Schema(description = "祖级列表", example = "1024") + private String ancestors; + @Schema(description = "显示顺序", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024") private Integer sort; diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dept/vo/dept/DeptSaveReqVO.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dept/vo/dept/DeptSaveReqVO.java index 0d6276fe..1cbb086d 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dept/vo/dept/DeptSaveReqVO.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dept/vo/dept/DeptSaveReqVO.java @@ -25,6 +25,9 @@ public class DeptSaveReqVO { @Schema(description = "父部门 ID", example = "1024") private Long parentId; + @Schema(description = "祖级列表", example = "1024") + private String ancestors; + @Schema(description = "显示顺序", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024") @NotNull(message = "显示顺序不能为空") private Integer sort; diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/dept/DeptDO.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/dept/DeptDO.java index a59fa8b6..d9f9a2e7 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/dept/DeptDO.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/dept/DeptDO.java @@ -38,6 +38,10 @@ public class DeptDO extends TenantBaseDO { * 关联 {@link #id} */ private Long parentId; + /** + * 祖级列表 + */ + private String ancestors; /** * 显示顺序 */ diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/dept/DeptServiceImpl.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/dept/DeptServiceImpl.java index 8530f882..48bcefa4 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/dept/DeptServiceImpl.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/dept/DeptServiceImpl.java @@ -45,8 +45,8 @@ public class DeptServiceImpl implements DeptService { if (createReqVO.getParentId() == null) { createReqVO.setParentId(DeptDO.PARENT_ID_ROOT); } - // 校验父部门的有效性 - validateParentDept(null, createReqVO.getParentId()); + // 校验父部门的有效性同时顺便设置祖级列表 + createReqVO.setAncestors(validateParentDept(null, createReqVO.getParentId())); // 校验部门名的唯一性 validateDeptNameUnique(null, createReqVO.getParentId(), createReqVO.getName()); @@ -66,8 +66,8 @@ public class DeptServiceImpl implements DeptService { } // 校验自己存在 validateDeptExists(updateReqVO.getId()); - // 校验父部门的有效性 - validateParentDept(updateReqVO.getId(), updateReqVO.getParentId()); + // 校验父部门的有效性同时顺便设置祖级列表 + updateReqVO.setAncestors(validateParentDept(null, updateReqVO.getParentId())); // 校验部门名的唯一性 validateDeptNameUnique(updateReqVO.getId(), updateReqVO.getParentId(), updateReqVO.getName()); @@ -116,9 +116,10 @@ public class DeptServiceImpl implements DeptService { } @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)) { - return; + return ancestorsBuilder.toString(); } // 1. 不能设置自己为父部门 if (Objects.equals(id, parentId)) { @@ -130,12 +131,26 @@ public class DeptServiceImpl implements DeptService { throw exception(DEPT_PARENT_NOT_EXITS); } // 3. 递归校验父部门,如果父部门是自己的子部门,则报错,避免形成环路 - if (id == null) { // id 为空,说明新增,不需要考虑环路 - return; + if (id == null) { // id 为空,说明新增,不需要考虑环路,但是需要拼接祖级列表 + 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++) { // 3.1 校验环路 parentId = parentDept.getParentId(); + ancestorsBuilder.insert(0, parentId + ","); if (Objects.equals(id, parentId)) { throw exception(DEPT_PARENT_IS_CHILD); } @@ -148,6 +163,7 @@ public class DeptServiceImpl implements DeptService { break; } } + return ancestorsBuilder.toString(); } @VisibleForTesting