Browse Source

BUG修复:解决部门查找的问题

GDW 1 week ago
parent
commit
58d2427c9d

+ 45 - 3
jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/controller/SysUserController.java

@@ -34,6 +34,7 @@ import org.jeecg.config.mybatis.MybatisPlusSaasConfig;
 import org.jeecg.modules.base.service.BaseCommonService;
 import org.jeecg.modules.system.entity.*;
 import org.jeecg.modules.system.model.DepartIdModel;
+import org.jeecg.modules.system.model.SysDepartTreeModel;
 import org.jeecg.modules.system.model.SysUserSysDepartModel;
 import org.jeecg.modules.system.service.*;
 import org.jeecg.modules.system.vo.SysDepartUsersVO;
@@ -110,7 +111,8 @@ public class SysUserController {
 
     @Autowired
     private JeecgRedisClient jeecgRedisClient;
-    
+    private StringBuilder deptStr;
+
     /**
      * 获取租户下用户数据(支持租户隔离)
      * @param user
@@ -621,8 +623,8 @@ public class SysUserController {
                 String[] expectedHeaders = {"登录账号", "真实姓名", "性别", "电话", "工号", "所属部门"};
                 boolean isHeaderValid = checkHeader(headerRow, expectedHeaders);
 
-
                 List<SysUser> listSysUsers = ExcelImportUtil.importExcel(file.getInputStream(), SysUser.class, params);
+
                 if (!isHeaderValid) {
                     errorMessage.add("文件 " + fileName + " 格式不正确");
                     errorLines =listSysUsers.size();
@@ -630,6 +632,13 @@ public class SysUserController {
                 }
                 for (int i = 0; i < listSysUsers.size(); i++) {
                     SysUser sysUserExcel = listSysUsers.get(i);
+                    String depart = sysUserExcel.getDepartmentId();
+                    if (depart.contains("/")) {
+                        String[] departNames = depart.split("/");
+                        List<SysDepartTreeModel> list = sysDepartService.queryMyDeptTreeList(null);
+                        SysDepartTreeModel nodeByPath = findNodeByPath(list, departNames);
+                        sysUserExcel.setDepartmentId(nodeByPath.getId());
+                    }
                     if (StringUtils.isBlank(sysUserExcel.getPassword())) {
                         // 密码默认为 “123456”
                         sysUserExcel.setPassword("123456");
@@ -676,7 +685,16 @@ public class SysUserController {
                         int lineNumber = i + 1;
                         // 通过索引名判断出错信息
                         if (message.contains(CommonConstant.SQL_INDEX_UNIQ_SYS_USER_USERNAME)) {
-                            errorMessage.add("第 " + lineNumber + " 行:用户名已经存在,忽略导入。");
+                            String errMessage = "第 " + lineNumber + " 行:用户名已经存在,忽略导入。";
+                            List<SysUser> logicDeletedUserList = sysUserService.queryLogicDeleted();
+                            if (logicDeletedUserList.size() > 0) {
+                                List<String> usernames = logicDeletedUserList.stream().map(SysUser::getUsername).collect(Collectors.toList());
+                                //sysUserExcel的username是否在usernames中
+                                if (usernames.contains(sysUserExcel.getUsername())) {
+                                    errMessage = errMessage + "用户在回收站中存在。";
+                                }
+                            }
+                            errorMessage.add(errMessage);
                         } else if (message.contains(CommonConstant.SQL_INDEX_UNIQ_SYS_USER_WORK_NO)) {
                             errorMessage.add("第 " + lineNumber + " 行:工号已经存在,忽略导入。");
                         } else if (message.contains(CommonConstant.SQL_INDEX_UNIQ_SYS_USER_PHONE)) {
@@ -2103,5 +2121,29 @@ public class SysUserController {
         return true;
     }
 
+// 递归查找指定路径对应的节点
+    public static SysDepartTreeModel findNodeByPath(List<SysDepartTreeModel> tree, String[] path) {
+        if (path == null || path.length == 0 || tree == null){
+            return null;
+        }
+
+        for (SysDepartTreeModel node : tree) {
+            if (node.getDepartName().equals(path[0])) {
+                if (path.length == 1) {
+                    // 找到了路径的最后一部分
+                    return node;
+                } else {
+                    String[] paths = Arrays.copyOfRange(path, 1, path.length);
+                    // 递归查找子节点
+                    SysDepartTreeModel result = findNodeByPath(node.getChildren(), paths);
+                    if (result != null) {
+                        return result;
+                    }
+                }
+            }
+        }
+        // 如果没有找到匹配的节点,返回null
+        return null;
+    }
 
 }