优化:优化删除部门的代码逻辑
1.如果存在下级部门,不允许删除
This commit is contained in:
parent
59d9fd33e4
commit
28334f6c9a
@ -119,12 +119,11 @@ public class DeptServiceImpl extends BaseServiceImpl<DeptMapper, DeptDO, DeptVO,
|
|||||||
Optional<DeptDO> isSystemData = list.stream().filter(d -> DataTypeEnum.SYSTEM.equals(d.getType())).findFirst();
|
Optional<DeptDO> isSystemData = list.stream().filter(d -> DataTypeEnum.SYSTEM.equals(d.getType())).findFirst();
|
||||||
CheckUtils.throwIf(isSystemData::isPresent, "所选部门 [{}] 是系统内置部门,不允许删除",
|
CheckUtils.throwIf(isSystemData::isPresent, "所选部门 [{}] 是系统内置部门,不允许删除",
|
||||||
isSystemData.orElseGet(DeptDO::new).getName());
|
isSystemData.orElseGet(DeptDO::new).getName());
|
||||||
|
CheckUtils.throwIf(this.countChildren(ids) > 0, "所选部门存在下级部门,不允许删除");
|
||||||
CheckUtils.throwIf(userService.countByDeptIds(ids) > 0, "所选部门存在用户关联,请解除关联后重试");
|
CheckUtils.throwIf(userService.countByDeptIds(ids) > 0, "所选部门存在用户关联,请解除关联后重试");
|
||||||
|
|
||||||
// 删除角色和部门关联
|
// 删除角色和部门关联
|
||||||
roleDeptService.deleteByDeptIds(ids);
|
roleDeptService.deleteByDeptIds(ids);
|
||||||
// 删除子部门
|
|
||||||
baseMapper.lambdaUpdate().in(DeptDO::getParentId, ids).remove();
|
|
||||||
// 删除部门
|
// 删除部门
|
||||||
super.delete(ids);
|
super.delete(ids);
|
||||||
}
|
}
|
||||||
@ -193,6 +192,22 @@ public class DeptServiceImpl extends BaseServiceImpl<DeptMapper, DeptDO, DeptVO,
|
|||||||
return baseMapper.lambdaQuery().apply(String.format("find_in_set(%s, `ancestors`)", id)).list();
|
return baseMapper.lambdaQuery().apply(String.format("find_in_set(%s, `ancestors`)", id)).list();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询子部门数量
|
||||||
|
*
|
||||||
|
* @param ids
|
||||||
|
* ID 列表
|
||||||
|
* @return 子部门数量
|
||||||
|
*/
|
||||||
|
private Long countChildren(List<Long> ids) {
|
||||||
|
if (CollUtil.isEmpty(ids)) {
|
||||||
|
return 0L;
|
||||||
|
}
|
||||||
|
return ids.stream()
|
||||||
|
.mapToLong(id -> baseMapper.lambdaQuery().apply(String.format("find_in_set(%s, `ancestors`)", id)).count())
|
||||||
|
.sum();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 更新子部门祖级列表
|
* 更新子部门祖级列表
|
||||||
*
|
*
|
||||||
|
@ -158,7 +158,7 @@
|
|||||||
<template #icon><icon-edit /></template>修改
|
<template #icon><icon-edit /></template>修改
|
||||||
</a-button>
|
</a-button>
|
||||||
<a-popconfirm
|
<a-popconfirm
|
||||||
content="确定要删除当前选中的数据吗?如果存在下级部门则一并删除,此操作不能撤销!"
|
content="确定要删除当前选中的数据吗?"
|
||||||
type="warning"
|
type="warning"
|
||||||
@ok="handleDelete([record.id])"
|
@ok="handleDelete([record.id])"
|
||||||
>
|
>
|
||||||
@ -490,8 +490,7 @@
|
|||||||
proxy.$modal.warning({
|
proxy.$modal.warning({
|
||||||
title: '警告',
|
title: '警告',
|
||||||
titleAlign: 'start',
|
titleAlign: 'start',
|
||||||
content:
|
content: '确定要删除当前选中的数据吗?',
|
||||||
'确定要删除当前选中的数据吗?如果存在下级部门则一并删除,此操作不能撤销!',
|
|
||||||
hideCancel: false,
|
hideCancel: false,
|
||||||
onOk: () => {
|
onOk: () => {
|
||||||
handleDelete(ids.value);
|
handleDelete(ids.value);
|
||||||
|
Loading…
Reference in New Issue
Block a user