zhangherong
2025-07-09 c800257cb6c8b45e7edc20e2e9018cd90b230806
lxzn-module-eam/src/main/java/org/jeecg/modules/eam/service/impl/EamMaintenanceStandardServiceImpl.java
@@ -127,9 +127,11 @@
            List<BaseFactoryUser> baseFactoryUserList=baseFactoryUserService.
                    list(new LambdaQueryWrapper<BaseFactoryUser>().eq(BaseFactoryUser::getUserId,sysUser.getId()));
            if(!CollectionUtils.isEmpty(baseFactoryUserList)){
                List<String> factoryIds = baseFactoryUserList.stream().map(BaseFactoryUser::getFactoryId).collect(Collectors.toList());
                List<String> factoryCode= baseFactoryService.listByIds(factoryIds).stream().map(BaseFactory::getFactoryCode).collect(Collectors.toList());
                queryWrapper.in("e.factory_code", factoryCode);
                Set<String> factoryIds = baseFactoryUserList.stream().map(BaseFactoryUser::getFactoryId).collect(Collectors.toSet());
                Set<String> factoryCode= baseFactoryService.listByIds(factoryIds).stream().map(BaseFactory::getOrgCode).collect(Collectors.toSet());
                queryWrapper.in("e.factory_org_code", factoryCode);
            } else {
                return page;
            }
        }
        if(eamMaintenanceStandard != null) {
@@ -181,7 +183,7 @@
        //删除标记
        entity.setDelFlag(CommonConstant.DEL_FLAG_0);
        //重复性校验
        EamMaintenanceStandard exist = checkDuplicate(entity.getEquipmentId(), entity.getMaintenanceCategory());
        EamMaintenanceStandard exist = checkDuplicate(entity.getEquipmentId(), entity.getMaintenanceCategory(),MaintenanceStandardStatusEnum.WAIT_SUBMIT.name());
        if(exist != null){
            throw new JeecgBootException("设备标准已存在,不能重复添加!");
        }
@@ -265,7 +267,7 @@
        //删除标记
        newEntity.setDelFlag(CommonConstant.DEL_FLAG_0);
        //重复性校验
        EamMaintenanceStandard exist = checkDuplicate(newEntity.getEquipmentId(), newEntity.getMaintenanceCategory());
        EamMaintenanceStandard exist = checkDuplicate(newEntity.getEquipmentId(), newEntity.getMaintenanceCategory(),MaintenanceStandardStatusEnum.WAIT_SUBMIT.name());
        if(exist != null){
            throw new JeecgBootException("设备标准已存在,不能重复添加!");
        }
@@ -286,12 +288,12 @@
    }
    @Override
    public EamMaintenanceStandard checkDuplicate(String equipmentId, String maintenanceCategory) {
    public EamMaintenanceStandard checkDuplicate(String equipmentId, String maintenanceCategory,String standardStatus) {
        LambdaQueryWrapper<EamMaintenanceStandard> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EamMaintenanceStandard::getEquipmentId, equipmentId);
        queryWrapper.eq(EamMaintenanceStandard::getMaintenanceCategory, maintenanceCategory);
        queryWrapper.eq(EamMaintenanceStandard::getDelFlag, CommonConstant.DEL_FLAG_0);
        queryWrapper.eq(EamMaintenanceStandard::getStandardStatus, MaintenanceStandardStatusEnum.WAIT_SUBMIT.name());
        queryWrapper.eq(EamMaintenanceStandard::getStandardStatus, standardStatus);
        queryWrapper.orderByDesc(EamMaintenanceStandard::getStandardVersion);
        List<EamMaintenanceStandard> list = eamMaintenanceStandardMapper.selectList(queryWrapper);
@@ -560,9 +562,19 @@
            EamMaintenanceStandard eamMaintenanceStandard = extractDeviceInfo(sheet);
            if (eamMaintenanceStandard == null) {
                return Result.error("设备信息提取失败");
            }else {
                if (eamMaintenanceStandard.getEquipmentId() == null) {
                    return Result.error("设备信息提取失败");
                }
            }
            eamMaintenanceStandard.setStandardName(name);
            EamMaintenanceStandard exist = checkDuplicate(eamMaintenanceStandard.getEquipmentId(), eamMaintenanceStandard.getMaintenanceCategory(),MaintenanceStandardStatusEnum.START.name());
            if(exist != null){
                return Result.error(name + ": 设备标准已存在,不能重复添加");
            }
            eamMaintenanceStandardMapper.insert(eamMaintenanceStandard);
            // 2. 提取每日点检项目
@@ -646,6 +658,7 @@
        EamMaintenanceStandard standard = new EamMaintenanceStandard();
        standard.setEquipmentName(extractField(headerText, "设备名称[::]\\s*(\\S+)"));
        standard.setEquipmentCode(extractField(headerText, "统一编号[::]\\s*(\\S+)"));
        // 日期处理
@@ -662,22 +675,14 @@
        }
        // 关联设备ID
        if (isNotBlank(standard.getEquipmentCode()) &&
                isNotBlank(standard.getEquipmentName())) {
            List<EamEquipment> equipments = eamEquipmentService.list(
                    new QueryWrapper<EamEquipment>()
                            .eq("equipment_code", standard.getEquipmentCode())
                            .eq("equipment_name", standard.getEquipmentName())
            );
            if (!equipments.isEmpty()) {
                standard.setEquipmentId(equipments.get(0).getId());
            }else {
        if (StrUtil.isNotEmpty(standard.getEquipmentCode())) {
            EamEquipment equipments = eamEquipmentService.selectByEquipmentCode(standard.getEquipmentCode());
            if (equipments == null) {
                return null;
            }else {
                standard.setEquipmentId(equipments.getId());
            }
        }
        String codeSeq = businessCodeRuleService.generateBusinessCodeSeq(BusinessCodeConst.MAINTENANCE_STANDARD_CODE_RULE);
        standard.setStandardCode(codeSeq);
        standard.setMaintenanceCategory("POINT_INSPECTION");
@@ -935,6 +940,7 @@
    /*导入点检文件Excel--------------------------结束*/
    /*导入二保三保文件Excel--------------------------开始*/
    /**
     * 统一保养规范导入入口
     * @param file 上传文件
@@ -952,40 +958,68 @@
                return Result.error(fileName + ": 文档中未找到表格");
            }
            XWPFTable table = doc.getTables().get(0);
            List<XWPFTable> tables = doc.getTables();
            EamMaintenanceStandard standard = null;
            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,type);
                    if (standard == null) {
                        return Result.error(fileName + ": 设备信息提取失败");
                    }
                    // 配置类型相关参数
                    configureStandard(standard, type, file);
                    EamMaintenanceStandard exist = checkDuplicate(standard.getEquipmentId(), standard.getMaintenanceCategory(),MaintenanceStandardStatusEnum.START.name());
                    if(exist != null){
                        return Result.error(fileName + ": 设备标准已存在,不能重复添加");
                    }
                    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));
                    }
                } else { // 后续页面
                    // 提取后续页面的保养项目
                    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());
@@ -995,6 +1029,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 (!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;
    }
    /**
@@ -1046,7 +1268,7 @@
    /**
     * 提取设备基本信息
     */
    private EamMaintenanceStandard extractDeviceInfo(XWPFTable table) {
    private EamMaintenanceStandard extractDeviceInfo(XWPFTable table,String type) {
        if (table.getNumberOfRows() < 2) return null;
        // 提取前两行数据
@@ -1061,29 +1283,22 @@
        standard.setEquipmentModel(row2Data.get("设备型号"));
        // 关联设备ID
        if (isNotBlank(standard.getEquipmentCode()) &&
                isNotBlank(standard.getEquipmentName())) {
            List<EamEquipment> equipments = eamEquipmentService.list(
                    new QueryWrapper<EamEquipment>()
                            .eq("equipment_code", standard.getEquipmentCode())
                            .eq("equipment_name", standard.getEquipmentName())
            );
            if (!equipments.isEmpty()) {
                standard.setEquipmentId(equipments.get(0).getId());
            }else {
        if (StrUtil.isNotEmpty(standard.getEquipmentCode())) {
            EamEquipment equipments = eamEquipmentService.selectByEquipmentCode(standard.getEquipmentCode());
            if (equipments == null) {
                return null;
            }else {
                standard.setEquipmentId(equipments.getId());
            }
            if (type.equals("THIRD")){
                EamEquipmentExtend eamEquipmentExtend=eamEquipmentExtendService.getById(standard.getEquipmentId());
                standard.setMaintenancePeriod(eamEquipmentExtend.getThirdMaintenancePeriod());
            }
        }
        return standard;
    }
    // 简易版字符串非空判断
    private boolean isNotBlank(String str) {
        return str != null && !str.trim().isEmpty();
    }
    /**
     * 表格行数据解析
@@ -1167,161 +1382,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(
            EamMaintenanceStandardDetailCategory category,
            String content,
            String standardId) {
        EamMaintenanceStandardDetail item = new EamMaintenanceStandardDetail();
        item.setItemCategory(category.name());
        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;
    }
    /**
     * 兼容版单元格文本提取
     */
    private String getCellText(XWPFTableCell cell) {
@@ -1344,48 +1404,12 @@
    }
    /**
     * 内容清理
     */
    private String cleanContent(String text) {
        if (text == null) return "";
        // 替换特殊空格和合并连续空格
        text = text.replace('\u00A0', ' ')
                .replace('\u2007', ' ')
                .replace('\u202F', ' ')
                .replaceAll("\\s+", " ");
        // 规范标点符号
        return text.replace(',', '、')
                .replace(',', '、')
                .replace(';', ';')
                .replace(';', ';')
                .replace(':', ':')
                .replace(':', ':')
                .trim();
    }
    /**
     * 验证有效项目行
     */
    private boolean isValidItemRow(XWPFTableRow row) {
        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--------------------------结束*/