{"record":{"id":"d7bb66e847ef853e","repo":"elunez/eladmin","slug":"error-d7bb66","errorCode":null,"errorMessage":"所选部门存在用户关联，请解除后再试！","messagePattern":"所选部门存在用户关联，请解除后再试！","errorType":"exception","errorClass":"BadRequestException","httpStatus":400,"severity":"error","filePath":"eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/DeptServiceImpl.java","lineNumber":245,"sourceCode":"            } else if(deptDTO.getPid() != null &&  !deptNames.contains(findById(deptDTO.getPid()).getName())) {\n                depts.add(deptDTO);\n            }\n        }\n\n        if (CollectionUtil.isEmpty(trees)) {\n            trees = depts;\n        }\n        Map<String,Object> map = new HashMap<>(2);\n        map.put(\"totalElements\",deptDtos.size());\n        map.put(\"content\",CollectionUtil.isEmpty(trees)? deptDtos :trees);\n        return map;\n    }\n\n    @Override\n    public void verification(Set<DeptDto> deptDtos) {\n        Set<Long> deptIds = deptDtos.stream().map(DeptDto::getId).collect(Collectors.toSet());\n        if(userRepository.countByDepts(deptIds) > 0){\n            throw new BadRequestException(\"所选部门存在用户关联，请解除后再试！\");\n        }\n        if(roleRepository.countByDepts(deptIds) > 0){\n            throw new BadRequestException(\"所选部门存在角色关联，请解除后再试！\");\n        }\n    }\n\n    private void updateSubCnt(Long deptId){\n        if(deptId != null){\n            int count = deptRepository.countByPid(deptId);\n            deptRepository.updateSubCntById(count, deptId);\n        }\n    }\n\n    private List<DeptDto> deduplication(List<DeptDto> list) {\n        List<DeptDto> deptDtos = new ArrayList<>();\n        for (DeptDto deptDto : list) {\n            boolean flag = true;\n            for (DeptDto dto : list) {","sourceCodeStart":227,"sourceCodeEnd":263,"githubUrl":"https://github.com/elunez/eladmin/blob/55fbf705956949697dbd68bf9003776609d3d029/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/DeptServiceImpl.java#L227-L263","documentation":"Thrown by DeptServiceImpl.verification (line 245) during dept deletion when userRepository.countByDepts(deptIds) > 0 — at least one selected department still has users attached. eladmin deletes departments only when they are fully unreferenced; this guard runs before removal so the tree stays consistent.","triggerScenarios":"DELETE /api/dept with an id whose sys_user_dept rows still exist; deleting a whole subtree where one leaf dept contains active or disabled users; batch delete including the default dept that new users were assigned to.","commonSituations":"Admins cleaning up an org restructure while users are still mapped to old depts; test data left assigned to scratch departments; disabled/departed users still counting because delete is the only thing checked, not enabled state.","solutions":["Reassign those users to another dept first (user edit screen or bulk UPDATE sys_user_dept), then retry the delete.","Identify blockers: SELECT user_id, dept_id FROM sys_user_dept WHERE dept_id IN (...).","If users are truly obsolete, delete the users (respecting role-level rules) before deleting their depts."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Before DELETE /api/dept, ensure no users reference the dept(s)\nconst { data } = await axios.get('/api/dept'); // or a dedicated user-by-dept query\n// Practically: surface the server message and link to user reassignment.\n// Server-side pre-check if you build a wrapper service:\nlong n = userRepository.countByDepts(Set.of(deptId));\nif (n > 0) throw new IllegalStateException(\"reassign \" + n + \" users first\");","typeGuard":null,"tryCatchPattern":"Catch the 400, parse the count-free message, and route the admin to user reassignment; do not retry the delete unchanged.","preventionTips":["In the dept tree UI, offer 'move users first' when a delete fails.","Before org cleanups, run a referential sweep of sys_user_dept for the target subtree."],"tags":["referential-integrity","eladmin","dept","delete","user"],"backgroundTag":null,"analyzedSha":"55fbf705956949697dbd68bf9003776609d3d029","analyzedAt":"2026-08-14T11:56:12.758Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}