|
@@ -214,5 +214,87 @@ public class LifeSysDeptServiceImpl implements LifeSysDeptService {
|
|
|
return R.fail("更新排序失败:" + e.getMessage());
|
|
return R.fail("更新排序失败:" + e.getMessage());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public R<Boolean> deleteDept(Long deptId) {
|
|
|
|
|
+ log.info("LifeSysDeptServiceImpl.deleteDept?deptId={}", deptId);
|
|
|
|
|
+
|
|
|
|
|
+ // 参数校验
|
|
|
|
|
+ if (deptId == null) {
|
|
|
|
|
+ log.warn("删除部门失败:部门ID为空");
|
|
|
|
|
+ return R.fail("部门ID不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 查询部门信息
|
|
|
|
|
+ LifeSysDept dept = lifeSysDeptMapper.selectById(deptId);
|
|
|
|
|
+ if (dept == null) {
|
|
|
|
|
+ return R.fail("部门不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 检查是否已被删除
|
|
|
|
|
+ if (!"0".equals(dept.getDelFlag())) {
|
|
|
|
|
+ return R.fail("该部门已被删除");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 检查是否有子部门(未删除的子部门)
|
|
|
|
|
+ QueryWrapper<LifeSysDept> childQueryWrapper = new QueryWrapper<>();
|
|
|
|
|
+ childQueryWrapper.eq("parent_id", deptId);
|
|
|
|
|
+ childQueryWrapper.eq("del_flag", "0"); // 未删除的子部门
|
|
|
|
|
+ Integer childCount = lifeSysDeptMapper.selectCount(childQueryWrapper);
|
|
|
|
|
+
|
|
|
|
|
+ if (childCount != null && childCount > 0) {
|
|
|
|
|
+ log.warn("删除部门失败:该部门下存在{}个子部门,不允许删除", childCount);
|
|
|
|
|
+ return R.fail("该部门下存在子部门,请先删除子部门后再删除");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ // 获取被删除部门的父部门ID和排序值
|
|
|
|
|
+ Long parentId = dept.getParentId() != null ? dept.getParentId() : 0L;
|
|
|
|
|
+ Integer deletedSort = dept.getDeptSort();
|
|
|
|
|
+
|
|
|
|
|
+ // 执行逻辑删除(设置 del_flag = "2")
|
|
|
|
|
+ LambdaUpdateWrapper<LifeSysDept> updateWrapper = new LambdaUpdateWrapper<>();
|
|
|
|
|
+ updateWrapper.eq(LifeSysDept::getDeptId, deptId)
|
|
|
|
|
+ .set(LifeSysDept::getDelFlag, "2");
|
|
|
|
|
+
|
|
|
|
|
+ int result = lifeSysDeptMapper.update(null, updateWrapper);
|
|
|
|
|
+
|
|
|
|
|
+ if (result > 0) {
|
|
|
|
|
+ // 删除成功后,自动更新同级别部门的排序
|
|
|
|
|
+ // 将排在删除部门后面的所有部门的排序值减1
|
|
|
|
|
+ if (deletedSort != null) {
|
|
|
|
|
+ QueryWrapper<LifeSysDept> sortUpdateQueryWrapper = new QueryWrapper<>();
|
|
|
|
|
+ sortUpdateQueryWrapper.eq("parent_id", parentId);
|
|
|
|
|
+ sortUpdateQueryWrapper.eq("del_flag", "0"); // 只更新未删除的部门
|
|
|
|
|
+ sortUpdateQueryWrapper.gt("dept_sort", deletedSort); // 排序值大于被删除部门的排序值
|
|
|
|
|
+ List<LifeSysDept> deptsToUpdate = lifeSysDeptMapper.selectList(sortUpdateQueryWrapper);
|
|
|
|
|
+
|
|
|
|
|
+ // 批量更新排序值(减1)
|
|
|
|
|
+ for (LifeSysDept deptToUpdate : deptsToUpdate) {
|
|
|
|
|
+ if (deptToUpdate.getDeptSort() != null) {
|
|
|
|
|
+ LambdaUpdateWrapper<LifeSysDept> sortUpdateWrapper = new LambdaUpdateWrapper<>();
|
|
|
|
|
+ sortUpdateWrapper.eq(LifeSysDept::getDeptId, deptToUpdate.getDeptId())
|
|
|
|
|
+ .set(LifeSysDept::getDeptSort, deptToUpdate.getDeptSort() - 1);
|
|
|
|
|
+ lifeSysDeptMapper.update(null, sortUpdateWrapper);
|
|
|
|
|
+ log.debug("删除后更新排序:部门ID={}的排序值从{}调整为{}",
|
|
|
|
|
+ deptToUpdate.getDeptId(), deptToUpdate.getDeptSort(), deptToUpdate.getDeptSort() - 1);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (!deptsToUpdate.isEmpty()) {
|
|
|
|
|
+ log.info("删除部门后,已更新{}个同级别部门的排序值", deptsToUpdate.size());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ log.info("删除部门成功,部门ID={}, 部门名称={}", deptId, dept.getDeptName());
|
|
|
|
|
+ return R.success("删除部门成功");
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return R.fail("删除部门失败");
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("删除部门失败,部门ID={}", deptId, e);
|
|
|
|
|
+ return R.fail("删除部门失败:" + e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|