Commit d4b1acb11e9191c508475d03690855cd2b0aeadc
1 parent
51e5294a
feat:【system 系统管理】dept 增加批量删除
Showing
4 changed files
with
62 additions
and
3 deletions
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dept/DeptController.java
| @@ -56,6 +56,15 @@ public class DeptController { | @@ -56,6 +56,15 @@ public class DeptController { | ||
| 56 | return success(true); | 56 | return success(true); |
| 57 | } | 57 | } |
| 58 | 58 | ||
| 59 | + @DeleteMapping("/delete-list") | ||
| 60 | + @Operation(summary = "批量删除部门") | ||
| 61 | + @Parameter(name = "ids", description = "编号列表", required = true) | ||
| 62 | + @PreAuthorize("@ss.hasPermission('system:dept:delete')") | ||
| 63 | + public CommonResult<Boolean> deleteDeptList(@RequestParam("ids") List<Long> ids) { | ||
| 64 | + deptService.deleteDeptList(ids); | ||
| 65 | + return success(true); | ||
| 66 | + } | ||
| 67 | + | ||
| 59 | @GetMapping("/list") | 68 | @GetMapping("/list") |
| 60 | @Operation(summary = "获取部门列表") | 69 | @Operation(summary = "获取部门列表") |
| 61 | @PreAuthorize("@ss.hasPermission('system:dept:query')") | 70 | @PreAuthorize("@ss.hasPermission('system:dept:query')") |
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/dept/DeptService.java
| @@ -37,6 +37,13 @@ public interface DeptService { | @@ -37,6 +37,13 @@ public interface DeptService { | ||
| 37 | void deleteDept(Long id); | 37 | void deleteDept(Long id); |
| 38 | 38 | ||
| 39 | /** | 39 | /** |
| 40 | + * 批量删除部门 | ||
| 41 | + * | ||
| 42 | + * @param ids 部门编号数组 | ||
| 43 | + */ | ||
| 44 | + void deleteDeptList(List<Long> ids); | ||
| 45 | + | ||
| 46 | + /** | ||
| 40 | * 获得部门信息 | 47 | * 获得部门信息 |
| 41 | * | 48 | * |
| 42 | * @param id 部门编号 | 49 | * @param id 部门编号 |
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/dept/DeptServiceImpl.java
| @@ -88,6 +88,21 @@ public class DeptServiceImpl implements DeptService { | @@ -88,6 +88,21 @@ public class DeptServiceImpl implements DeptService { | ||
| 88 | deptMapper.deleteById(id); | 88 | deptMapper.deleteById(id); |
| 89 | } | 89 | } |
| 90 | 90 | ||
| 91 | + @Override | ||
| 92 | + @CacheEvict(cacheNames = RedisKeyConstants.DEPT_CHILDREN_ID_LIST, | ||
| 93 | + allEntries = true) // allEntries 清空所有缓存,因为操作一个部门,涉及到多个缓存 | ||
| 94 | + public void deleteDeptList(List<Long> ids) { | ||
| 95 | + // 校验是否有子部门 | ||
| 96 | + for (Long id : ids) { | ||
| 97 | + if (deptMapper.selectCountByParentId(id) > 0) { | ||
| 98 | + throw exception(DEPT_EXITS_CHILDREN); | ||
| 99 | + } | ||
| 100 | + } | ||
| 101 | + | ||
| 102 | + // 批量删除部门 | ||
| 103 | + deptMapper.deleteByIds(ids); | ||
| 104 | + } | ||
| 105 | + | ||
| 91 | @VisibleForTesting | 106 | @VisibleForTesting |
| 92 | void validateDeptExists(Long id) { | 107 | void validateDeptExists(Long id) { |
| 93 | if (id == null) { | 108 | if (id == null) { |
yudao-module-system/src/test/java/cn/iocoder/yudao/module/system/service/dept/DeptServiceImplTest.java
| @@ -105,12 +105,40 @@ public class DeptServiceImplTest extends BaseDbUnitTest { | @@ -105,12 +105,40 @@ public class DeptServiceImplTest extends BaseDbUnitTest { | ||
| 105 | } | 105 | } |
| 106 | 106 | ||
| 107 | @Test | 107 | @Test |
| 108 | - public void testValidateDeptExists_notFound() { | 108 | + public void testDeleteDeptList_success() { |
| 109 | + // mock 数据 | ||
| 110 | + DeptDO deptDO1 = randomPojo(DeptDO.class); | ||
| 111 | + deptMapper.insert(deptDO1); | ||
| 112 | + DeptDO deptDO2 = randomPojo(DeptDO.class); | ||
| 113 | + deptMapper.insert(deptDO2); | ||
| 109 | // 准备参数 | 114 | // 准备参数 |
| 110 | - Long id = randomLongId(); | 115 | + List<Long> ids = Arrays.asList(deptDO1.getId(), deptDO2.getId()); |
| 116 | + | ||
| 117 | + // 调用 | ||
| 118 | + deptService.deleteDeptList(ids); | ||
| 119 | + // 校验数据不存在了 | ||
| 120 | + assertNull(deptMapper.selectById(deptDO1.getId())); | ||
| 121 | + assertNull(deptMapper.selectById(deptDO2.getId())); | ||
| 122 | + } | ||
| 123 | + | ||
| 124 | + @Test | ||
| 125 | + public void testDeleteDeptList_exitsChildren() { | ||
| 126 | + // mock 数据 | ||
| 127 | + DeptDO parentDept = randomPojo(DeptDO.class); | ||
| 128 | + deptMapper.insert(parentDept); | ||
| 129 | + DeptDO childrenDeptDO = randomPojo(DeptDO.class, o -> { | ||
| 130 | + o.setParentId(parentDept.getId()); | ||
| 131 | + o.setStatus(randomCommonStatus()); | ||
| 132 | + }); | ||
| 133 | + deptMapper.insert(childrenDeptDO); | ||
| 134 | + DeptDO anotherDept = randomPojo(DeptDO.class); | ||
| 135 | + deptMapper.insert(anotherDept); | ||
| 136 | + | ||
| 137 | + // 准备参数(包含有子部门的 parentDept) | ||
| 138 | + List<Long> ids = Arrays.asList(parentDept.getId(), anotherDept.getId()); | ||
| 111 | 139 | ||
| 112 | // 调用, 并断言异常 | 140 | // 调用, 并断言异常 |
| 113 | - assertServiceException(() -> deptService.validateDeptExists(id), DEPT_NOT_FOUND); | 141 | + assertServiceException(() -> deptService.deleteDeptList(ids), DEPT_EXITS_CHILDREN); |
| 114 | } | 142 | } |
| 115 | 143 | ||
| 116 | @Test | 144 | @Test |