Browse Source

feat:【system 系统管理】dept 增加批量删除

YunaiV 1 year ago
parent
commit
d4b1acb11e

+ 9 - 0
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<Boolean> deleteDeptList(@RequestParam("ids") List<Long> ids) {
+        deptService.deleteDeptList(ids);
+        return success(true);
+    }
+
     @GetMapping("/list")
     @Operation(summary = "获取部门列表")
     @PreAuthorize("@ss.hasPermission('system:dept:query')")

+ 7 - 0
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<Long> ids);
+
     /**
      * 获得部门信息
      *

+ 15 - 0
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<Long> 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) {

+ 31 - 3
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<Long> 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<Long> ids = Arrays.asList(parentDept.getId(), anotherDept.getId());
 
         // 调用, 并断言异常
-        assertServiceException(() -> deptService.validateDeptExists(id), DEPT_NOT_FOUND);
+        assertServiceException(() -> deptService.deleteDeptList(ids), DEPT_EXITS_CHILDREN);
     }
 
     @Test