459242451@qq.com 3 năm trước cách đây
mục cha
commit
f978c599a7

+ 32 - 31
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDeptServiceImpl.java

@@ -5,7 +5,6 @@ import cn.hutool.core.util.StrUtil;
 import cn.hutool.json.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
-import com.ruoyi.common.annotation.DataScope;
 import com.ruoyi.common.constant.UserConstants;
 import com.ruoyi.common.core.domain.TreeSelect;
 import com.ruoyi.common.core.domain.entity.SysDept;
@@ -67,13 +66,13 @@ public class SysDeptServiceImpl extends ServicePlusImpl<SysDeptMapper, SysDept,
         for (SysDept dept : depts) {
             tempList.add(dept.getDeptId());
         }
-		for (SysDept dept : depts) {
-			// 如果是顶级节点, 遍历该父节点的所有子节点
-			if (!tempList.contains(dept.getParentId())) {
-				recursionFn(depts, dept);
-				returnList.add(dept);
-			}
-		}
+        for (SysDept dept : depts) {
+            // 如果是顶级节点, 遍历该父节点的所有子节点
+            if (!tempList.contains(dept.getParentId())) {
+                recursionFn(depts, dept);
+                returnList.add(dept);
+            }
+        }
         if (returnList.isEmpty()) {
             returnList = depts;
         }
@@ -124,8 +123,8 @@ public class SysDeptServiceImpl extends ServicePlusImpl<SysDeptMapper, SysDept,
     @Override
     public long selectNormalChildrenDeptById(String deptId) {
         return count(new LambdaQueryWrapper<SysDept>()
-                .eq(SysDept::getStatus, 0)
-                .apply("find_in_set({0}, ancestors)", deptId));
+            .eq(SysDept::getStatus, 0)
+            .apply("find_in_set({0}, ancestors)", deptId));
     }
 
     /**
@@ -137,8 +136,8 @@ public class SysDeptServiceImpl extends ServicePlusImpl<SysDeptMapper, SysDept,
     @Override
     public boolean hasChildByDeptId(String deptId) {
         long result = count(new LambdaQueryWrapper<SysDept>()
-                .eq(SysDept::getParentId, deptId)
-                .last("limit 1"));
+            .eq(SysDept::getParentId, deptId)
+            .last("limit 1"));
         return result > 0;
     }
 
@@ -151,7 +150,7 @@ public class SysDeptServiceImpl extends ServicePlusImpl<SysDeptMapper, SysDept,
     @Override
     public boolean checkDeptExistUser(String deptId) {
         long result = userMapper.selectCount(new LambdaQueryWrapper<SysUser>()
-                .eq(SysUser::getDeptId, deptId));
+            .eq(SysUser::getDeptId, deptId));
         return result > 0;
     }
 
@@ -165,9 +164,9 @@ public class SysDeptServiceImpl extends ServicePlusImpl<SysDeptMapper, SysDept,
     public String checkDeptNameUnique(SysDept dept) {
         String deptId = StrUtil.isBlank(dept.getDeptId()) ? "-1" : dept.getDeptId();
         SysDept info = getOne(new LambdaQueryWrapper<SysDept>()
-                .eq(SysDept::getDeptName, dept.getDeptName())
-                .eq(SysDept::getParentId, dept.getParentId())
-                .last("limit 1"));
+            .eq(SysDept::getDeptName, dept.getDeptName())
+            .eq(SysDept::getParentId, dept.getParentId())
+            .last("limit 1"));
         if (StringUtils.isNotNull(info) && !info.getDeptId().equals(deptId)) {
             return UserConstants.NOT_UNIQUE;
         }
@@ -200,11 +199,13 @@ public class SysDeptServiceImpl extends ServicePlusImpl<SysDeptMapper, SysDept,
     @Override
     public int insertDept(SysDept dept) {
         SysDept info = getById(dept.getParentId());
-        // 如果父节点不为正常状态,则不允许新增子节点
-        if (!UserConstants.DEPT_NORMAL.equals(info.getStatus())) {
-            throw new ServiceException("部门停用,不允许新增");
+        if (info != null) {
+            // 如果父节点不为正常状态,则不允许新增子节点
+            if (!UserConstants.DEPT_NORMAL.equals(info.getStatus())) {
+                throw new ServiceException("部门停用,不允许新增");
+            }
+            dept.setAncestors(info.getAncestors() + "," + dept.getParentId());
         }
-        dept.setAncestors(info.getAncestors() + "," + dept.getParentId());
         return baseMapper.insert(dept);
     }
 
@@ -226,7 +227,7 @@ public class SysDeptServiceImpl extends ServicePlusImpl<SysDeptMapper, SysDept,
         }
         int result = baseMapper.updateById(dept);
         if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()) && StringUtils.isNotEmpty(dept.getAncestors())
-			&& !StringUtils.equals("0", dept.getAncestors())) {
+            && !StringUtils.equals("0", dept.getAncestors())) {
             // 如果该部门是启用状态,则启用该部门的所有上级部门
             updateParentDeptStatusNormal(dept);
         }
@@ -239,23 +240,23 @@ public class SysDeptServiceImpl extends ServicePlusImpl<SysDeptMapper, SysDept,
      * @param dept 当前部门
      */
     private void updateParentDeptStatusNormal(SysDept dept) {
-		String ancestors = dept.getAncestors();
+        String ancestors = dept.getAncestors();
         String[] deptIds = Convert.toStrArray(ancestors);
         update(null, new LambdaUpdateWrapper<SysDept>()
-                .set(SysDept::getStatus, "0")
-                .in(SysDept::getDeptId, Arrays.asList(deptIds)));
+            .set(SysDept::getStatus, "0")
+            .in(SysDept::getDeptId, Arrays.asList(deptIds)));
     }
 
     /**
      * 修改子元素关系
      *
-     * @param deptId 被修改的部门ID
+     * @param deptId       被修改的部门ID
      * @param newAncestors 新的父ID集合
      * @param oldAncestors 旧的父ID集合
      */
     public void updateDeptChildren(String deptId, String newAncestors, String oldAncestors) {
         List<SysDept> children = list(new LambdaQueryWrapper<SysDept>()
-                .apply("find_in_set({0},ancestors)",deptId));
+            .apply("find_in_set({0},ancestors)", deptId));
         for (SysDept child : children) {
             child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
         }
@@ -294,11 +295,11 @@ public class SysDeptServiceImpl extends ServicePlusImpl<SysDeptMapper, SysDept,
      */
     private List<SysDept> getChildList(List<SysDept> list, SysDept t) {
         List<SysDept> tlist = new ArrayList<>();
-		for (SysDept n : list) {
-			if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().equals(t.getDeptId())) {
-				tlist.add(n);
-			}
-		}
+        for (SysDept n : list) {
+            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().equals(t.getDeptId())) {
+                tlist.add(n);
+            }
+        }
         return tlist;
     }