fuliqi
2025-02-19 8fec9579ae4adb4f7f53440933908c90eea33b20
system/src/main/java/com/ycl/system/service/impl/SysDeptServiceImpl.java
@@ -6,6 +6,7 @@
import com.ycl.common.base.Result;
import com.ycl.common.core.domain.StringTreeSelect;
import com.ycl.system.domain.base.BaseSelect;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ycl.common.annotation.DataScope;
@@ -29,13 +30,12 @@
 * @author ycl
 */
@Service
@RequiredArgsConstructor
public class SysDeptServiceImpl implements ISysDeptService
{
    @Autowired
    private SysDeptMapper deptMapper;
    @Autowired
    private SysRoleMapper roleMapper;
    private final SysDeptMapper deptMapper;
    private final SysRoleMapper roleMapper;
    /**
     * 查询部门管理数据
@@ -262,13 +262,18 @@
    @Override
    public int insertDept(SysDept dept)
    {
        SysDept info = deptMapper.selectDeptById(dept.getParentId());
        // 如果父节点不为正常状态,则不允许新增子节点
        if (!UserConstants.DEPT_NORMAL.equals(info.getStatus()))
        {
            throw new ServiceException("部门停用,不允许新增");
        if (Objects.nonNull(dept.getParentId())) {
            SysDept info = deptMapper.selectDeptById(dept.getParentId());
            // 如果父节点不为正常状态,则不允许新增子节点
            if (!UserConstants.DEPT_NORMAL.equals(info.getStatus()))
            {
                throw new ServiceException("上级部门已停用,不允许新增下级部门");
            }
            dept.setAncestors(info.getAncestors() + "," + dept.getParentId());
        } else {
            dept.setParentId(0L);
            dept.setAncestors("");
        }
        dept.setAncestors(info.getAncestors() + "," + dept.getParentId());
        return deptMapper.insertDept(dept);
    }
@@ -281,22 +286,23 @@
    @Override
    public int updateDept(SysDept dept)
    {
        SysDept newParentDept = deptMapper.selectDeptById(dept.getParentId());
        SysDept oldDept = deptMapper.selectDeptById(dept.getDeptId());
        if (StringUtils.isNotNull(newParentDept) && StringUtils.isNotNull(oldDept))
        {
            String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getDeptId();
            String oldAncestors = oldDept.getAncestors();
            dept.setAncestors(newAncestors);
            updateDeptChildren(dept.getDeptId(), newAncestors, oldAncestors);
        if (Objects.isNull(oldDept)) {
            throw new RuntimeException("修改部门被删除或不存在");
        }
        if (Objects.nonNull(dept.getParentId())) {
            SysDept newParentDept = deptMapper.selectDeptById(dept.getParentId());
            if (Objects.nonNull(newParentDept)) {
                String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getDeptId();
                String oldAncestors = oldDept.getAncestors();
                dept.setAncestors(newAncestors);
                updateDeptChildren(dept.getDeptId(), newAncestors, oldAncestors);
            }
        } else {
            dept.setParentId(0L);
            dept.setAncestors("");
        }
        int result = deptMapper.updateDept(dept);
        if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()) && StringUtils.isNotEmpty(dept.getAncestors())
                && !StringUtils.equals("0", dept.getAncestors()))
        {
            // 如果该部门是启用状态,则启用该部门的所有上级部门
            updateParentDeptStatusNormal(dept);
        }
        return result;
    }