From d4b1acb11e9191c508475d03690855cd2b0aeadc Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sat, 2 Aug 2025 16:40:09 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E3=80=90system=20=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=E7=AE=A1=E7=90=86=E3=80=91dept=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=89=B9=E9=87=8F=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/admin/dept/DeptController.java | 9 +++++ .../system/service/dept/DeptService.java | 7 ++++ .../system/service/dept/DeptServiceImpl.java | 15 ++++++++ .../service/dept/DeptServiceImplTest.java | 34 +++++++++++++++++-- 4 files changed, 62 insertions(+), 3 deletions(-) diff --git a/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dept/DeptController.java b/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dept/DeptController.java index 7873d00f0a..7a243b778d 100644 --- a/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dept/DeptController.java +++ b/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dept/DeptController.java @@ -56,6 +56,15 @@ public class DeptController { return success(true); } + @DeleteMapping("/delete-list") + @Operation(summary = "批量删除部门") + @Parameter(name = "ids", description = "编号列表", required = true) + @PreAuthorize("@ss.hasPermission('system:dept:delete')") + public CommonResult deleteDeptList(@RequestParam("ids") List ids) { + deptService.deleteDeptList(ids); + return success(true); + } + @GetMapping("/list") @Operation(summary = "获取部门列表") @PreAuthorize("@ss.hasPermission('system:dept:query')") diff --git a/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/dept/DeptService.java b/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/dept/DeptService.java index a0b765e590..06a688e606 100644 --- a/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/dept/DeptService.java +++ b/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/dept/DeptService.java @@ -36,6 +36,13 @@ public interface DeptService { */ void deleteDept(Long id); + /** + * 批量删除部门 + * + * @param ids 部门编号数组 + */ + void deleteDeptList(List ids); + /** * 获得部门信息 * diff --git a/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/dept/DeptServiceImpl.java b/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/dept/DeptServiceImpl.java index 946d92df3b..6086474c60 100644 --- a/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/dept/DeptServiceImpl.java +++ b/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/dept/DeptServiceImpl.java @@ -88,6 +88,21 @@ public class DeptServiceImpl implements DeptService { deptMapper.deleteById(id); } + @Override + @CacheEvict(cacheNames = RedisKeyConstants.DEPT_CHILDREN_ID_LIST, + allEntries = true) // allEntries 清空所有缓存,因为操作一个部门,涉及到多个缓存 + public void deleteDeptList(List ids) { + // 校验是否有子部门 + for (Long id : ids) { + if (deptMapper.selectCountByParentId(id) > 0) { + throw exception(DEPT_EXITS_CHILDREN); + } + } + + // 批量删除部门 + deptMapper.deleteByIds(ids); + } + @VisibleForTesting void validateDeptExists(Long id) { if (id == null) { diff --git a/yudao-module-system/src/test/java/cn/iocoder/yudao/module/system/service/dept/DeptServiceImplTest.java b/yudao-module-system/src/test/java/cn/iocoder/yudao/module/system/service/dept/DeptServiceImplTest.java index bcf55bda66..df16d4fc97 100644 --- a/yudao-module-system/src/test/java/cn/iocoder/yudao/module/system/service/dept/DeptServiceImplTest.java +++ b/yudao-module-system/src/test/java/cn/iocoder/yudao/module/system/service/dept/DeptServiceImplTest.java @@ -105,12 +105,40 @@ public class DeptServiceImplTest extends BaseDbUnitTest { } @Test - public void testValidateDeptExists_notFound() { + public void testDeleteDeptList_success() { + // mock 数据 + DeptDO deptDO1 = randomPojo(DeptDO.class); + deptMapper.insert(deptDO1); + DeptDO deptDO2 = randomPojo(DeptDO.class); + deptMapper.insert(deptDO2); // 准备参数 - Long id = randomLongId(); + List ids = Arrays.asList(deptDO1.getId(), deptDO2.getId()); + + // 调用 + deptService.deleteDeptList(ids); + // 校验数据不存在了 + assertNull(deptMapper.selectById(deptDO1.getId())); + assertNull(deptMapper.selectById(deptDO2.getId())); + } + + @Test + public void testDeleteDeptList_exitsChildren() { + // mock 数据 + DeptDO parentDept = randomPojo(DeptDO.class); + deptMapper.insert(parentDept); + DeptDO childrenDeptDO = randomPojo(DeptDO.class, o -> { + o.setParentId(parentDept.getId()); + o.setStatus(randomCommonStatus()); + }); + deptMapper.insert(childrenDeptDO); + DeptDO anotherDept = randomPojo(DeptDO.class); + deptMapper.insert(anotherDept); + + // 准备参数(包含有子部门的 parentDept) + List ids = Arrays.asList(parentDept.getId(), anotherDept.getId()); // 调用, 并断言异常 - assertServiceException(() -> deptService.validateDeptExists(id), DEPT_NOT_FOUND); + assertServiceException(() -> deptService.deleteDeptList(ids), DEPT_EXITS_CHILDREN); } @Test