lyh
2025-07-09 8ef2ea1ef685b39c15d194fbd8f6d8046d353656
修改word导入
已修改2个文件
414 ■■■■■ 文件已修改
lxzn-module-eam-common/src/main/java/org/jeecg/modules/eam/service/impl/EamEquipmentServiceImpl.java 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
lxzn-module-eam/src/main/java/org/jeecg/modules/eam/service/impl/EamMaintenanceStandardServiceImpl.java 412 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
lxzn-module-eam-common/src/main/java/org/jeecg/modules/eam/service/impl/EamEquipmentServiceImpl.java
@@ -290,7 +290,7 @@
        }
        if (StringUtils.isNotBlank(sysUser.getEquipmentIds())) {
            //选择了设备,根据设备id过滤设备
            List<String> equipArr = Arrays.asList(sysUser.getEquipmentIds().split(","));
            List<String> equipArr = Arrays.asList(sysUser.getEamEquipmentIds().split(","));
            queryWrapper.in("equipment_code", equipArr);
        } else {
            //没有选择设备,根据车间过滤设备
lxzn-module-eam/src/main/java/org/jeecg/modules/eam/service/impl/EamMaintenanceStandardServiceImpl.java
@@ -949,40 +949,66 @@
                return Result.error(fileName + ": 文档中未找到表格");
            }
            XWPFTable table = doc.getTables().get(0);
            List<XWPFTable> tables = doc.getTables();
            EamMaintenanceStandard standard = null;
            boolean firstTableProcessed = false;
            List<EamMaintenanceStandardDetail> items = new ArrayList<>();
            String standardId = null;
            // 文档类型校验 - 防止二保传入三保或反之
            if (isWrongDocumentType(table, type)) {
                return Result.error(fileName + ": 文档类型不匹配 - " +
                        ("SECOND".equals(type) ? "请导入二级保养文档" : "请导入三级保养文档"));
            // 1. 处理所有表格
            for (int i = 0; i < tables.size(); i++) {
                XWPFTable table = tables.get(i);
                if (i == 0) { // 第一页表格
                    // 验证设备信息表格
                    if (isWrongDocumentType(table, type)) {
                        return Result.error(fileName + ": 文档类型不匹配 - " +
                                ("SECOND".equals(type) ? "请导入二级保养文档" : "请导入三级保养文档"));
                    }
                    // 提取设备信息
                    standard = extractDeviceInfo(table);
                    if (standard == null) {
                        return Result.error(fileName + ": 设备信息提取失败");
                    }
                    // 配置类型相关参数
                    configureStandard(standard, type, file);
                    eamMaintenanceStandardMapper.insert(standard);
                    standardId = standard.getId();
                    // 提取第一页的保养项目
                    if ("SECOND".equals(type)) {
                        items.addAll(extractSecondMaintenanceItems(table, standardId, true));
                    } else if ("THIRD".equals(type)) {
                        items.addAll(extractThirdMaintenanceItems(table, standardId, true));
                    }
                    firstTableProcessed = true;
                } else if (firstTableProcessed) { // 后续页面
                    // 提取后续页面的保养项目
                    if ("SECOND".equals(type)) {
                        items.addAll(extractSecondMaintenanceItems(table, standardId, false));
                    } else if ("THIRD".equals(type)) {
                        items.addAll(extractThirdMaintenanceItems(table, standardId, false));
                    }
                }
            }
            EamMaintenanceStandard standard = extractDeviceInfo(table);
            // 验证设备信息提取
            if (standard == null) {
                return Result.error(fileName + ": 设备信息提取失败");
            }
            // 配置类型相关参数
            configureStandard(standard, type, file);
            eamMaintenanceStandardMapper.insert(standard);
            String standardId = standard.getId();
            // 2. 后处理:根据不同类型进行处理
            processItemsAfterExtraction(items, type);
            // 提取保养项目
            List<EamMaintenanceStandardDetail> items;
            if ("SECOND".equals(type)) {
                items = extractSecondMaintenanceItems(table, standardId);
            } else if ("THIRD".equals(type)) {
                items = extractThirdMaintenanceItems(table, standardId);
            } else {
                return Result.error(fileName + ": 不支持的保养类型: " + type);
            }
            // 项目验证
            // 3. 项目验证
            if (items.isEmpty()) {
                return Result.error(fileName + ": 未提取到任何保养项目");
            }
            // 保存项目
            // 4. 保存项目
            eamMaintenanceStandardDetailService.saveBatch(items);
            return Result.ok(fileName + ": 导入成功, 项目数: " + items.size());
@@ -992,6 +1018,194 @@
        } catch (Exception e) {
            return Result.error(fileName + ": 系统错误 - " + e.getClass().getSimpleName());
        }
    }
    /**
     * 后处理方法:根据不同类型进行处理
    */
    private void processItemsAfterExtraction(List<EamMaintenanceStandardDetail> items, String type) {
        if ("SECOND".equals(type)) {
            // 二级保养: 删除没有序号的数据
            items.removeIf(item -> item.getItemCode() == null);
        } else {
            // 三级保养:
            // 1. 删除第一条数据(通常是标题行)
            if (!items.isEmpty()) {
                items.remove(0);
            }
            // 2. 为缺失部位的数据填充前一条的保养部位
            String lastPart = "";
            for (EamMaintenanceStandardDetail item : items) {
                if (item.getItemPart() != null && !item.getItemPart().isEmpty()) {
                    lastPart = item.getItemPart();
                } else if (!lastPart.isEmpty()) {
                    item.setItemPart(lastPart);
                }
            }
        }
    }
    /**
     * 提取二级保养项目(区分第一页和后续页面)
     */
    private List<EamMaintenanceStandardDetail> extractSecondMaintenanceItems(
            XWPFTable table, String standardId, boolean isFirstTable) {
        List<EamMaintenanceStandardDetail> items = new ArrayList<>();
        String currentCategory = null;
        int startRow = 0;
        // 对于第一页表格,跳过前两行(设备信息行)
        if (isFirstTable && table.getNumberOfRows() > 2) {
            startRow = 2; // 从第三行开始是保养内容
        }
        for (int i = startRow; i < table.getNumberOfRows(); i++) {
            XWPFTableRow row = table.getRow(i);
            if (row == null) continue;
            // 检查是否是标题行(维修人员保养内容或操作人员保养内容)
            String firstCell = getCellText(row.getCell(0));
            // 处理空行后的部位继承
            if (firstCell.contains(REPAIR_TITLE)) {
                currentCategory = "REPAIRER_MAINTENANCE";
            } else if (firstCell.contains(OPERATOR_TITLE)) {
                currentCategory = "OPERATOR_MAINTENANCE";
            }
            // 处理普通项目行
            if (currentCategory != null && isValidItemRow(row)) {
                EamMaintenanceStandardDetail item = new EamMaintenanceStandardDetail();
                item.setItemCategory(currentCategory);
                item.setStandardId(standardId);
                // 提取序号(第二列)
                if (row.getTableCells().size() > 1) {
                    String seqText = getCellText(row.getCell(1));
                    try {
                        if (!seqText.equals("序号")){
                            item.setItemCode(Integer.parseInt(seqText.trim()));
                        }
                    } catch (NumberFormatException e) {
                        // 忽略序号解析错误
                    }
                }
                // 提取内容(第三列)
                if (row.getTableCells().size() > 2) {
                    String seqText = getCellText(row.getCell(2));
                    item.setItemName(seqText);
                }
                items.add(item);
            }
        }
        return items;
    }
    /**
     * 提取三级保养项目(解决跨页空行问题)
     */
    private List<EamMaintenanceStandardDetail> extractThirdMaintenanceItems(
            XWPFTable table, String standardId, boolean isFirstTable) {
        List<EamMaintenanceStandardDetail> items = new ArrayList<>();
        String currentPart = "";
        int startRow = 0;
        // 对于第一页表格,跳过前两行(设备信息行)
        if (isFirstTable && table.getNumberOfRows() > 2) {
            startRow = 2; // 从第三行开始是保养内容
        }
        for (int i = startRow; i < table.getNumberOfRows(); i++) {
            XWPFTableRow row = table.getRow(i);
            if (row == null) continue;  // 确保行对象不为空
            // 检查是否是空行(包含所有单元格都为空的情况)
            if (isRowEmpty(row)) {
                // 空行处理:保留当前位置但不创建项目
                continue;
            }
            // 创建保养项目
            EamMaintenanceStandardDetail item = new EamMaintenanceStandardDetail();
            item.setItemCategory("THIRD_MAINTENANCE");
            item.setStandardId(standardId);
            int colCount = row.getTableCells().size();
            // 处理部位列(第一列)
            if (colCount > 0) {
                String firstCell = getCellText(row.getCell(0)).trim();
                // 关键改进:正确处理空行后的部位继承
                if (!firstCell.isEmpty() && !firstCell.equals("保养部位")) {
                    // 更新当前部位
                    currentPart = firstCell;
                }
                item.setItemPart(currentPart);
            } else {
                // 如果没有单元格,使用当前部位
                item.setItemPart(currentPart);
            }
            // 根据列数提取内容(考虑合并单元格情况)
            List<String> cellContents = new ArrayList<>();
            for (int j = 0; j < colCount; j++) {
                XWPFTableCell cell = row.getCell(j);
                String text = getCellText(cell).trim();
                // 特殊处理:第二页第一行可能是空行后的内容
                if (j == 0 && !text.isEmpty() && !text.equals(currentPart)) {
                    // 如果不是部位列,则添加为内容
                    cellContents.add(text);
                } else if (j > 0) {
                    // 其他列作为内容
                    cellContents.add(text);
                }
            }
            // 智能解析单元格内容
            if (cellContents.size() >= 2) {
                // 默认处理方式:最后两个作为内容和标准
                item.setItemName(cellContents.get(cellContents.size() - 2));
                item.setItemDemand(cellContents.get(cellContents.size() - 1));
            } else if (cellContents.size() == 1) {
                // 单列模式:视为内容
                item.setItemName(cellContents.get(0));
            } else if (cellContents.isEmpty() && !isRowEmpty(row)) {
                // 特殊处理:行非空但没有提取到内容(可能是复杂合并单元格)
                // 尝试提取整行文本作为内容
                StringBuilder content = new StringBuilder();
                for (XWPFTableCell cell : row.getTableCells()) {
                    content.append(getCellText(cell).trim()).append(" ");
                }
                item.setItemName(content.toString().trim());
            }
            items.add(item);
        }
        return items;
    }
    /**
     * 优化后的空行检测(解决跨页空行问题)
     */
    private boolean isRowEmpty(XWPFTableRow row) {
        if (row == null || row.getTableCells().isEmpty()) {
            return true;
        }
        boolean allCellsEmpty = true;
        for (XWPFTableCell cell : row.getTableCells()) {
            String text = getCellText(cell).trim();
            // 保留包含换行符等的单元格作为非空行
            if (!text.isEmpty() && !text.replaceAll("\\s+", "").isEmpty()) {
                allCellsEmpty = false;
                break;
            }
        }
        return allCellsEmpty;
    }
    /**
@@ -1157,87 +1371,6 @@
    }
    /**
     * 提取二级保养项目
     */
    private List<EamMaintenanceStandardDetail> extractSecondMaintenanceItems(
            XWPFTable table, String standardId) {
        List<EamMaintenanceStandardDetail> items = new ArrayList<>();
        EamMaintenanceStandardDetailCategory currentCategory = null;
        for (int i = 0; i < table.getNumberOfRows(); i++) {
            XWPFTableRow row = table.getRow(i);
            if (row == null) continue;
            // 检查是否是标题行(维修人员保养内容或操作人员保养内容)
            String firstCell = getCellText(row.getCell(0));
            if (firstCell != null) {
                for (String title : SECOND_CATEGORY_MAPPING.keySet()) {
                    if (firstCell.contains(title)) {
                        currentCategory = SECOND_CATEGORY_MAPPING.get(title);
                        // 检查第二列和第三列是否是"序号"和"保养内容"
                        if (row.getTableCells().size() > 2) {
                            String secondCell = getCellText(row.getCell(1));
                            String thirdCell = getCellText(row.getCell(2));
                            // 明确跳过标题行
                            if ("序号".equals(secondCell) && "保养内容".equals(thirdCell)) {
                                continue; // 跳过这一行
                            }
                        }
                        // 尝试提取标题行中的项目(如果存在)
                        if (row.getTableCells().size() > 2) {
                            String content = getCellText(row.getCell(2));
                            if (content != null && !content.trim().isEmpty() &&
                                    !"保养内容".equals(content.trim())) { // 过滤无效内容
                                items.add(createItem(currentCategory, content.trim(), standardId));
                            }
                        }
                        continue;
                    }
                }
            }
            // 处理普通项目行
            if (currentCategory != null && isValidItemRow(row)) {
                // 获取内容
                String content = row.getTableCells().size() > 2 ?
                        getCellText(row.getCell(2)) : "";
                // 排除标题内容
                if ("保养内容".equals(content) ||
                        "序号".equals(content) ||
                        content.contains("维修人员保养内容") ||
                        content.contains("操作人员保养内容")) {
                    continue;
                }
                EamMaintenanceStandardDetail item = new EamMaintenanceStandardDetail();
                item.setItemCategory(String.valueOf(currentCategory));
                item.setStandardId(standardId);
                item.setItemName(cleanContent(content));
                // 处理序号(第二列)
                if (row.getTableCells().size() > 1) {
                    String seqText = getCellText(row.getCell(1));
                    try {
                        if (seqText != null && !seqText.trim().isEmpty()) {
                            item.setItemCode(Integer.parseInt(seqText.trim()));
                        }
                    } catch (NumberFormatException e) {
                        // 忽略序号解析错误
                    }
                }
                items.add(item);
            }
        }
        return items;
    }
    /**
     * 创建保养项目
     */
    private EamMaintenanceStandardDetail createItem(
@@ -1250,65 +1383,6 @@
        item.setStandardId(standardId);
        item.setItemName(cleanContent(content));
        return item;
    }
    /**
     * 提取三级保养项目(无类型)
     */
    private List<EamMaintenanceStandardDetail> extractThirdMaintenanceItems(
            XWPFTable table, String standardId) {
        List<EamMaintenanceStandardDetail> items = new ArrayList<>();
        String currentPart = "";
        int itemCount = 1;
        // 从第三行开始(跳过表头和设备信息)
        for (int i = 2; i < table.getNumberOfRows(); i++) {
            XWPFTableRow row = table.getRow(i);
            if (row == null || isRowEmpty(row)) continue;
            // 跳过"保养部位"标题行
            String firstCell = getCellText(row.getCell(0));
            if ("保养部位".equals(firstCell)) {
                continue;
            }
            EamMaintenanceStandardDetail item = new EamMaintenanceStandardDetail();
            // 三级保养不需要类型,不设置itemCategory
            item.setStandardId(standardId);
            item.setItemCode(itemCount++);
            // 处理部位列
            if (!row.getTableCells().isEmpty()) {
                String partCell = getCellText(row.getCell(0));
                if (!partCell.trim().isEmpty()) {
                    currentPart = partCell.trim();
                }
            }
            item.setItemPart(currentPart);
            // 根据列数确定内容和标准的位置
            int cellCount = row.getTableCells().size();
            if (cellCount == 3) { // 部位|内容|标准
                item.setItemName(getCellText(row.getCell(1)));
                item.setItemDemand(getCellText(row.getCell(2)));
            }
            else if (cellCount == 2) { // 内容|标准
                item.setItemName(getCellText(row.getCell(0)));
                item.setItemDemand(getCellText(row.getCell(1)));
            }
            else if (cellCount == 1) { // 单列内容
                item.setItemName(getCellText(row.getCell(0)));
            }
            else if (cellCount > 3) { // 多列处理
                // 取第2列作为内容,最后一列作为标准
                item.setItemName(getCellText(row.getCell(1)));
                item.setItemDemand(getCellText(row.getCell(cellCount - 1)));
            }
            items.add(item);
        }
        return items;
    }
    /**
@@ -1362,20 +1436,6 @@
        return row != null &&
                row.getTableCells().size() >= 2 &&
                !getCellText(row.getCell(1)).trim().isEmpty();
    }
    /**
     * 空行检测
     */
    private boolean isRowEmpty(XWPFTableRow row) {
        if (row == null) return true;
        for (XWPFTableCell cell : row.getTableCells()) {
            String text = getCellText(cell);
            if (text != null && !text.trim().isEmpty()) {
                return false;
            }
        }
        return true;
    }
    /*导入二保三保文件Excel--------------------------结束*/