feat: Enhance department management with parent ID validation and department scope handling

This commit is contained in:
2026-07-16 09:25:35 +08:00
parent e21e51de8a
commit 8dea752d48
7 changed files with 81 additions and 14 deletions

View File

@@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.annotation.DataScope;
@@ -211,7 +212,17 @@ public class SysDeptServiceImpl implements ISysDeptService
@Override
public int insertDept(SysDept dept)
{
if (StringUtils.isNull(dept.getParentId()))
{
throw new ServiceException("上级部门不能为空");
}
SysDept info = deptMapper.selectDeptById(dept.getParentId());
if (StringUtils.isNull(info) || !"0".equals(info.getDelFlag()))
{
throw new ServiceException("上级部门不存在");
}
// 如果父节点不为正常状态,则不允许新增子节点
if (!UserConstants.DEPT_NORMAL.equals(info.getStatus()))
{
@@ -230,13 +241,36 @@ public class SysDeptServiceImpl implements ISysDeptService
@Override
public int updateDept(SysDept dept)
{
if (StringUtils.isNull(dept.getDeptId()))
{
throw new ServiceException("部门编号不能为空");
}
if (StringUtils.isNull(dept.getParentId()))
{
throw new ServiceException("上级部门不能为空");
}
SysDept newParentDept = deptMapper.selectDeptById(dept.getParentId());
SysDept oldDept = deptMapper.selectDeptById(dept.getDeptId());
if (StringUtils.isNotNull(newParentDept) && StringUtils.isNotNull(oldDept))
if (StringUtils.isNull(oldDept))
{
throw new ServiceException("部门不存在");
}
if (StringUtils.isNull(newParentDept) || !"0".equals(newParentDept.getDelFlag()))
{
throw new ServiceException("上级部门不存在");
}
if (dept.getDeptId().equals(newParentDept.getDeptId())
|| ArrayUtils.contains(StringUtils.split(newParentDept.getAncestors(), ","), dept.getDeptId().toString()))
{
throw new ServiceException("不能选择当前部门或其下级部门作为上级部门");
}
String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getDeptId();
String oldAncestors = oldDept.getAncestors();
dept.setAncestors(newAncestors);
if (!StringUtils.equals(newAncestors, oldAncestors))
{
String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getDeptId();
String oldAncestors = oldDept.getAncestors();
dept.setAncestors(newAncestors);
updateDeptChildren(dept.getDeptId(), newAncestors, oldAncestors);
}
int result = deptMapper.updateDept(dept);