Browse Source

Merge branch 'GDW-FIX' into dev

GDW 6 days ago
parent
commit
29f43783ec

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

@@ -16,6 +16,7 @@ import lombok.extern.slf4j.Slf4j;
 import org.apache.catalina.User;
 import org.apache.commons.lang.StringUtils;
 import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.apache.shiro.SecurityUtils;
 import org.apache.shiro.authz.annotation.RequiresPermissions;
 import org.apache.shiro.authz.annotation.RequiresRoles;
@@ -34,6 +35,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;
@@ -45,6 +47,7 @@ import org.jeecgframework.poi.excel.def.NormalExcelConstants;
 import org.jeecgframework.poi.excel.entity.ExportParams;
 import org.jeecgframework.poi.excel.entity.ImportParams;
 import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
+import org.jeecgframework.poi.util.PoiPublicUtil;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.web.bind.annotation.*;
@@ -54,7 +57,7 @@ import org.springframework.web.servlet.ModelAndView;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
-import java.io.IOException;
+import java.io.*;
 import java.net.URLDecoder;
 import java.util.*;
 import java.util.stream.Collectors;
@@ -110,7 +113,8 @@ public class SysUserController {
 
     @Autowired
     private JeecgRedisClient jeecgRedisClient;
-    
+    private StringBuilder deptStr;
+
     /**
      * 获取租户下用户数据(支持租户隔离)
      * @param user
@@ -621,15 +625,72 @@ public class SysUserController {
                 String[] expectedHeaders = {"登录账号", "真实姓名", "性别", "电话", "工号", "所属部门"};
                 boolean isHeaderValid = checkHeader(headerRow, expectedHeaders);
 
+                Workbook book = WorkbookFactory.create(file.getInputStream());
+                Sheet tempSheet = book.getSheetAt(0);
+                int lastRowNum = tempSheet.getLastRowNum();
+                int startRowNum = 3;
+
+                // 创建一个新的Workbook和Sheet
+                Workbook newBook = new XSSFWorkbook();
+                Sheet newSheet = newBook.createSheet("Filtered Data");
+
+                int newRowNum = 0; // 新Sheet的行号
+
+                // 复制前三行
+                for (int i = 0; i < startRowNum; i++) {
+                    Row row = tempSheet.getRow(i);
+                    if (row != null) {
+                        Row newRow = newSheet.createRow(newRowNum++);
+                        for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) {
+                            Cell cell = row.getCell(j);
+                            if (cell != null) {
+                                Cell newCell = newRow.createCell(j);
+                                copyCell(cell, newCell);
+                            }
+                        }
+                    }
+                }
+
+                for (int i = startRowNum; i <= lastRowNum; i++) {
+                    Row row = tempSheet.getRow(i);
+                    if (row != null) {
+                        short firstCellNum = row.getFirstCellNum();
+                        if (firstCellNum != -1) {
+                            // 复制行到新的Sheet
+                            Row newRow = newSheet.createRow(newRowNum++);
+                            for (int j = firstCellNum; j <= row.getLastCellNum(); j++) {
+                                Cell cell = row.getCell(j);
+                                if (cell != null) {
+                                    Cell newCell = newRow.createCell(j);
+                                    copyCell(cell, newCell);
+                                }
+                            }
+                        }
+                    }
+                }
+                // 将新的Workbook写入ByteArrayOutputStream
+                ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
+                newBook.write(byteArrayOut);
+                newBook.close();
+                book.close();
+
+                // 获取InputStream
+                InputStream inputStream = new ByteArrayInputStream(byteArrayOut.toByteArray());
+                byteArrayOut.close();
+
+                List<SysUser> listSysUsers = ExcelImportUtil.importExcel(inputStream, SysUser.class, params);
 
-                List<SysUser> listSysUsers = ExcelImportUtil.importExcel(file.getInputStream(), SysUser.class, params);
                 if (!isHeaderValid) {
                     errorMessage.add("文件 " + fileName + " 格式不正确");
                     errorLines =listSysUsers.size();
                     throw new IllegalArgumentException("文件格式不正确");
                 }
                 for (int i = 0; i < listSysUsers.size(); i++) {
+
                     SysUser sysUserExcel = listSysUsers.get(i);
+                    String depart = sysUserExcel.getDepartmentId();
+
+
                     if (StringUtils.isBlank(sysUserExcel.getPassword())) {
                         // 密码默认为 “123456”
                         sysUserExcel.setPassword("123456");
@@ -644,6 +705,15 @@ public class SysUserController {
                     String passwordEncode = PasswordUtil.encrypt(sysUserExcel.getUsername(), sysUserExcel.getPassword(), salt);
                     sysUserExcel.setPassword(passwordEncode);
                     try {
+                        if (depart.contains("/")) {
+                            String[] departNames = depart.split("/");
+                            List<SysDepartTreeModel> list = sysDepartService.queryMyDeptTreeList(null);
+                            SysDepartTreeModel nodeByPath = findNodeByPath(list, departNames);
+                            if (ObjectUtil.isEmpty(nodeByPath)) {
+                                throw new IllegalArgumentException("组织不存在");
+                            }
+                            sysUserExcel.setDepartmentId(nodeByPath.getId());
+                        }
 
                         if (ObjectUtil.isNull(sysUserExcel.getUsername()) || sysUserExcel.getUsername().isEmpty()) {
 //                            errorMessage.add("第 " + i+1 + " 行:用户名为空。");
@@ -676,7 +746,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)) {
@@ -688,7 +767,9 @@ public class SysUserController {
                         } else if (message.contains("用户名为空")) {
                             errorMessage.add("第 " + lineNumber + " 行:用户名为空。");
                         } else if (message.contains("用户真实姓名为空")) {
-                            errorMessage.add("第 " + lineNumber + " 行:用户真实姓名为空。");
+                            errorMessage.add("第 " + lineNumber + " 行:用户真实姓名为空。用户名:" + sysUserExcel.getUsername());
+                        } else if (message.contains("组织不存在")) {
+                            errorMessage.add("第 " + lineNumber + " 行:组织不存在。用户名:" + sysUserExcel.getUsername());
                         } else {
                             errorMessage.add("第 " + lineNumber + " 行:未知错误,忽略导入");
                             log.error(e.getMessage(), e);
@@ -2103,5 +2184,48 @@ 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;
+    }
+
+    private void copyCell(Cell oldCell, Cell newCell) {
+        switch (oldCell.getCellType()) {
+            case STRING:
+                newCell.setCellValue(oldCell.getStringCellValue());
+                break;
+            case NUMERIC:
+                newCell.setCellValue(oldCell.getNumericCellValue());
+                break;
+            case BOOLEAN:
+                newCell.setCellValue(oldCell.getBooleanCellValue());
+                break;
+            case FORMULA:
+                newCell.setCellFormula(oldCell.getCellFormula());
+                break;
+            default:
+                break;
+        }
+    }
 
 }

+ 13 - 1
jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/service/impl/SysDepartServiceImpl.java

@@ -1283,6 +1283,19 @@ public class SysDepartServiceImpl extends ServiceImpl<SysDepartMapper, SysDepart
 		Map<String,SysDepart> departMap = new HashMap<>();
 		// orgCode编码长度
 		int codeLength = YouBianCodeUtil.ZHANWEI_LENGTH;
+
+		//对空数据进行删除
+		for (int i = 0; i < listSysDeparts.size(); i++) {
+			if (oConvertUtils.isEmpty(listSysDeparts.get(i).getDepartName()) &&
+				oConvertUtils.isEmpty(listSysDeparts.get(i).getDepartOrder()) &&
+				oConvertUtils.isEmpty(listSysDeparts.get(i).getDepartNameEn()) &&
+				oConvertUtils.isEmpty(listSysDeparts.get(i).getDescription()) &&
+				oConvertUtils.isEmpty(listSysDeparts.get(i).getMemo())) {
+				listSysDeparts.remove(i);
+				i--;
+			}
+		}
+
 		//循环第二遍导入数据
 		for (SysDepartExportVo departExportVo : listSysDeparts) {
 			SysDepart sysDepart = new SysDepart();
@@ -1394,7 +1407,6 @@ public class SysDepartServiceImpl extends ServiceImpl<SysDepartMapper, SysDepart
 			item.setParentId(null);
 		});
 		importSysDepart(sysDepartExportVos, errorMessageList);
-		System.out.println(sysDepartExportVos);
 	}