| | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.apache.poi.ss.usermodel.*; |
| | | import org.apache.poi.xssf.usermodel.*; |
| | | import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
| | | import org.jeecg.common.api.vo.Result; |
| | | import org.jeecg.common.aspect.annotation.AutoLog; |
| | | import org.jeecg.common.constant.CommonConstant; |
| | |
| | | import org.jeecg.modules.eam.entity.EamMaintenanceStandardDetail; |
| | | import org.jeecg.modules.eam.request.EamMaintenanceStandardRequest; |
| | | import org.jeecg.modules.eam.service.IEamEquipmentService; |
| | | import org.jeecg.modules.eam.service.IEamMaintenanceOrderDetailService; |
| | | import org.jeecg.modules.eam.service.IEamMaintenanceStandardDetailService; |
| | | import org.jeecg.modules.eam.service.IEamMaintenanceStandardService; |
| | | import org.jeecg.modules.eam.vo.MaintenanceStandardDetailVo; |
| | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.ByteArrayOutputStream; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.util.*; |
| | | import java.util.Arrays; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | * @return |
| | | */ |
| | | @RequestMapping(value = "/inspectionImportExcel", method = RequestMethod.POST) |
| | | public Result<?> inspectionImportExcel(HttpServletRequest request, HttpServletResponse response) { |
| | | public Result<?> inspectionImportExcel(HttpServletRequest request, HttpServletResponse response) throws IOException { |
| | | MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; |
| | | Map<String, MultipartFile> fileMap = multipartRequest.getFileMap(); |
| | | |
| | | for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) { |
| | | // 获取上传文件对象 |
| | | MultipartFile file = entity.getValue(); |
| | | byte[] bytes = file.getBytes(); |
| | | ByteArrayInputStream bais = new ByteArrayInputStream(bytes); |
| | | |
| | | ImportParams params = new ImportParams(); |
| | | params.setTitleRows(2); |
| | | params.setHeadRows(1); |
| | | params.setSheetNum(1); |
| | | params.setLastOfInvalidRow(23); |
| | | params.setTitleRows(2); // 跳过前2行标题 |
| | | params.setHeadRows(1); // 第3行是表头 |
| | | params.setSheetNum(1); // 读取第一个工作表 |
| | | |
| | | int dataEndRow = findDataEndRow(file.getInputStream()); |
| | | log.info("计算出的数据结束行: {}", dataEndRow); |
| | | |
| | | params.setLastOfInvalidRow(dataEndRow); |
| | | params.setNeedSave(true); |
| | | |
| | | EamMaintenanceStandardRequest standardRequest = new EamMaintenanceStandardRequest(); |
| | | try { |
| | | //读取设备编号,图片等 |
| | | readExcel(file, standardRequest); |
| | | log.info("读取到的设备编码: {}", standardRequest.getEquipmentCode()); |
| | | |
| | | EamEquipment equipment = eamEquipmentService.selectByEquipmentCode(standardRequest.getEquipmentCode()); |
| | | if(equipment == null) { |
| | | log.error("设备不存在:{}", standardRequest.getEquipmentCode()); |
| | | continue; |
| | | } |
| | | |
| | | standardRequest.setStandardName(standardRequest.getEquipmentName() + "点检标准"); |
| | | standardRequest.setMaintenanceCategory(MaintenanceCategoryEnum.POINT_INSPECTION.name()); |
| | | standardRequest.setEquipmentId(equipment.getId()); |
| | | |
| | | //读取保养明细内容 |
| | | // 读取保养明细内容前添加调试信息 |
| | | log.info("Excel导入参数: titleRows={}, headRows={}, lastOfInvalidRow={}", |
| | | params.getTitleRows(), params.getHeadRows(), params.getLastOfInvalidRow()); |
| | | |
| | | List<MaintenanceStandardImport> list = ExcelImportUtil.importExcel(file.getInputStream(), MaintenanceStandardImport.class, params); |
| | | log.info("实际读取到的明细数量: {}", list.size()); |
| | | |
| | | // 打印前几条记录用于调试 |
| | | if (!list.isEmpty()) { |
| | | log.info("前3条记录详情:"); |
| | | for (int i = 0; i < Math.min(3, list.size()); i++) { |
| | | MaintenanceStandardImport item = list.get(i); |
| | | log.info("第{}条: NO={}, 点检条件={}, 部位名称={}, 点检内容={}", |
| | | i+1, item.getItemCode(), item.getCondition(), |
| | | item.getItemPart(), item.getItemName()); |
| | | } |
| | | } else { |
| | | log.warn("未读取到任何明细记录"); |
| | | } |
| | | |
| | | //明细项 |
| | | List<EamMaintenanceStandardDetail> tableList = list.stream().map(EamMaintenanceStandardDetail::new).collect(Collectors.toList()); |
| | | standardRequest.setTableDetailList(tableList); |
| | | log.info("转换后的明细数量: {}", tableList.size()); |
| | | |
| | | String codeSeq = businessCodeRuleService.generateBusinessCodeSeq(BusinessCodeConst.MAINTENANCE_STANDARD_CODE_RULE); |
| | | standardRequest.setStandardCode(codeSeq); |
| | | boolean b = eamMaintenanceStandardService.addMaintenanceStandard(standardRequest); |
| | |
| | | log.error("保存失败! {}", standardRequest.getEquipmentCode()); |
| | | } |
| | | } catch (Exception e) { |
| | | //update-begin-author:taoyan date:20211124 for: 导入数据重复增加提示 |
| | | String msg = e.getMessage(); |
| | | log.error("文件 {} 处理异常: {}", file.getOriginalFilename(), msg, e); |
| | | //update-end-author:taoyan date:20211124 for: 导入数据重复增加提示 |
| | | } finally { |
| | | try { |
| | | file.getInputStream().close(); |
| | |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * 通过excel导入数据 |
| | | * 季保通过excel导入数据 |
| | | * |
| | | * @param request |
| | | * @param response |
| | |
| | | params.setTitleRows(2); |
| | | params.setHeadRows(1); |
| | | params.setSheetNum(1); |
| | | params.setLastOfInvalidRow(23); |
| | | params.setNeedSave(true); |
| | | EamMaintenanceStandardRequest standardRequest = new EamMaintenanceStandardRequest(); |
| | | try { |
| | |
| | | continue; |
| | | } |
| | | standardRequest.setStandardName(standardRequest.getEquipmentName() + "保养标准"); |
| | | |
| | | standardRequest.setMaintenanceCategory(MaintenanceCategoryEnum.QUARTERLY_MAINTENANCE.name()); |
| | | standardRequest.setEquipmentId(equipment.getId()); |
| | | //读取保养明细内容 |
| | |
| | | return Result.ok("文件导入完成!"); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 年保通过excel导入数据 |
| | | * |
| | | * @param request |
| | | * @param response |
| | | * @return |
| | | */ |
| | | @RequestMapping(value = "/annualMaintenanceImportExcel", method = RequestMethod.POST) |
| | | public Result<?> annualMaintenanceImportExcel(HttpServletRequest request, HttpServletResponse response) { |
| | | MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; |
| | | Map<String, MultipartFile> fileMap = multipartRequest.getFileMap(); |
| | | for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) { |
| | | // 获取上传文件对象 |
| | | MultipartFile file = entity.getValue(); |
| | | |
| | | ImportParams params = new ImportParams(); |
| | | params.setTitleRows(2); |
| | | params.setHeadRows(1); |
| | | params.setSheetNum(1); |
| | | params.setLastOfInvalidRow(23); |
| | | params.setNeedSave(true); |
| | | EamMaintenanceStandardRequest standardRequest = new EamMaintenanceStandardRequest(); |
| | | try { |
| | | //读取设备编号,图片等 |
| | | readWeekExcel(file, standardRequest); |
| | | EamEquipment equipment = eamEquipmentService.selectByEquipmentCode(standardRequest.getEquipmentCode()); |
| | | if(equipment == null) { |
| | | log.error("设备不存在:{}", standardRequest.getEquipmentCode()); |
| | | continue; |
| | | } |
| | | standardRequest.setStandardName(standardRequest.getEquipmentName() + "保养标准"); |
| | | |
| | | standardRequest.setMaintenanceCategory(MaintenanceCategoryEnum.ANNUAL_MAINTENANCE.name()); |
| | | standardRequest.setEquipmentId(equipment.getId()); |
| | | //读取保养明细内容 |
| | | List<WeekMaintenanceStandardImport> list = ExcelImportUtil.importExcel(file.getInputStream(), WeekMaintenanceStandardImport.class, params); |
| | | //明细项 |
| | | List<EamMaintenanceStandardDetail> tableList = list.stream().map(EamMaintenanceStandardDetail::new).collect(Collectors.toList()); |
| | | standardRequest.setTableDetailList(tableList); |
| | | String codeSeq = businessCodeRuleService.generateBusinessCodeSeq(BusinessCodeConst.MAINTENANCE_STANDARD_CODE_RULE); |
| | | standardRequest.setStandardCode(codeSeq); |
| | | boolean b = eamMaintenanceStandardService.addMaintenanceStandard(standardRequest); |
| | | if (!b) { |
| | | log.error("保存失败! {}", standardRequest.getEquipmentCode()); |
| | | } |
| | | } catch (Exception e) { |
| | | //update-begin-author:taoyan date:20211124 for: 导入数据重复增加提示 |
| | | String msg = e.getMessage(); |
| | | log.error("文件 {} 处理异常: {}", file.getOriginalFilename(), msg, e); |
| | | //update-end-author:taoyan date:20211124 for: 导入数据重复增加提示 |
| | | } finally { |
| | | try { |
| | | file.getInputStream().close(); |
| | | } catch (IOException e) { |
| | | log.error(e.getMessage(), e); |
| | | } |
| | | } |
| | | } |
| | | return Result.ok("文件导入完成!"); |
| | | } |
| | | /** |
| | | * 读取Excel 第一行, 第二行的信息,包括图片信息 |
| | | * @param file |
| | |
| | | //第二行读取 |
| | | Row row = sheet.getRow(1); |
| | | //设备编码 |
| | | Cell equipmentCode = row.getCell(5); |
| | | Cell equipmentCode = row.getCell(8); |
| | | Cell targetCell = row.getCell(0); |
| | | //文件编码 |
| | | String fileCodeValue = getCellValue(targetCell); |
| | |
| | | throw new JeecgBootException("Excel【" + file.getOriginalFilename() + "】第二行第一列获取到的设备编号为空!"); |
| | | } |
| | | request.setFileCode(fileCodeValue.trim()); |
| | | if(CellType.NUMERIC.equals(equipmentCode.getCellType())) { |
| | | request.setEquipmentCode(String.valueOf((int) equipmentCode.getNumericCellValue())); |
| | | }else if(CellType.STRING.equals(equipmentCode.getCellType())) { |
| | | request.setEquipmentCode(equipmentCode.getStringCellValue()); |
| | | // if(CellType.NUMERIC.equals(equipmentCode.getCellType())) { |
| | | // request.setEquipmentCode(String.valueOf((int) equipmentCode.getNumericCellValue())); |
| | | // }else if(CellType.STRING.equals(equipmentCode.getCellType())) { |
| | | // request.setEquipmentCode(equipmentCode.getStringCellValue()); |
| | | // } |
| | | String equipmentCodeStr = extractEquipmentCode(equipmentCode); |
| | | if (StringUtils.isBlank(equipmentCodeStr)) { |
| | | throw new JeecgBootException("Excel【 " + file.getOriginalFilename() + "】没有读取到有效的设备编号,导入失败!"); |
| | | } |
| | | request.setEquipmentCode(equipmentCodeStr); |
| | | if (StringUtils.isBlank(request.getEquipmentCode())) { |
| | | throw new JeecgBootException("Excel【 " + file.getOriginalFilename() + "】没有读取到设备编号,导入失败!"); |
| | | } |
| | |
| | | } |
| | | |
| | | Sheet sheet = book.getSheetAt(0); |
| | | //第一行读取 |
| | | Row row = sheet.getRow(0); |
| | | //第二行读取 |
| | | Row row = sheet.getRow(1); |
| | | //设备编码 |
| | | Cell equipmentCode = row.getCell(10); |
| | | Cell equipmentCode = row.getCell(13); |
| | | Cell targetCell = row.getCell(0); |
| | | String fileCodeValue = getCellValue(targetCell); |
| | | if (fileCodeValue == null || fileCodeValue.trim().isEmpty()) { |
| | | throw new JeecgBootException("Excel【" + file.getOriginalFilename() + "】第二行第一列获取到的设备编号为空!"); |
| | | } |
| | | request.setFileCode(fileCodeValue.trim()); |
| | | if(CellType.NUMERIC.equals(equipmentCode.getCellType())) { |
| | | request.setEquipmentCode(String.valueOf((int) equipmentCode.getNumericCellValue())); |
| | | }else if(CellType.STRING.equals(equipmentCode.getCellType())) { |
| | |
| | | throw new JeecgBootException("Excel【 " + file.getOriginalFilename() + "】没有读取到设备编号,导入失败!"); |
| | | } |
| | | //初始日期 |
| | | Cell initialDate = row.getCell(6); |
| | | Cell initialDate = row.getCell(11); |
| | | if (DateUtil.isCellDateFormatted(initialDate)) { |
| | | request.setInitialDate(initialDate.getDateCellValue()); |
| | | } else { |
| | | request.setInitialDate(new Date()); |
| | | } |
| | | //设备名称 |
| | | Cell equipmentName = row.getCell(8); |
| | | request.setEquipmentName(equipmentName.getStringCellValue()); |
| | | // Cell equipmentName = row.getCell(8); |
| | | // request.setEquipmentName(equipmentName.getStringCellValue()); |
| | | |
| | | //第二行读取 |
| | | row = sheet.getRow(1); |
| | | row = sheet.getRow(4); |
| | | //保养周期 |
| | | Cell period = row.getCell(6); |
| | | Cell period = row.getCell(7); |
| | | if (CellType.NUMERIC.equals(period.getCellType())) { |
| | | request.setMaintenancePeriod((int) period.getNumericCellValue()); |
| | | } else { |
| | |
| | | log.error("读取Excel信息失败:{}", e.getMessage(), e); |
| | | } |
| | | } |
| | | |
| | | private int findDataEndRow(InputStream inputStream) throws IOException { |
| | | Workbook workbook = null; |
| | | try { |
| | | workbook = WorkbookFactory.create(inputStream); |
| | | Sheet sheet = workbook.getSheetAt(0); |
| | | int lastRowNum = sheet.getLastRowNum(); |
| | | log.info("Excel文件总行数: {}", lastRowNum); |
| | | |
| | | // 找到"实施要领"行,作为数据结束的标志 |
| | | for (int i = 0; i <= lastRowNum; i++) { |
| | | Row row = sheet.getRow(i); |
| | | if (row == null) continue; |
| | | |
| | | // 检查第A列是否包含"实施要领" |
| | | Cell cell = row.getCell(0); |
| | | if (cell != null && cell.getCellType() == CellType.STRING) { |
| | | String value = getCellValue(cell).replaceAll("\\s+", ""); |
| | | if ("实施要领".equals(value)) { |
| | | log.info("找到'实施要领'在第{}行", i); |
| | | return i - 1; // 返回"实施要领"行之前的行号 |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 如果没有找到"实施要领",返回最后一行 |
| | | log.info("未找到'实施要领',返回最后一行: {}", lastRowNum); |
| | | return lastRowNum; |
| | | } finally { |
| | | if (workbook != null) { |
| | | workbook.close(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * 从单元格中提取设备编号,去除前缀如"设备编号:" |
| | | * @param cell 单元格对象 |
| | | * @return 纯设备编号字符串 |
| | | */ |
| | | private String extractEquipmentCode(Cell cell) { |
| | | if (cell == null) { |
| | | return null; |
| | | } |
| | | |
| | | String cellValue = getCellValue(cell); |
| | | if (StringUtils.isBlank(cellValue)) { |
| | | return null; |
| | | } |
| | | |
| | | // 去除前后空格 |
| | | cellValue = cellValue.trim(); |
| | | |
| | | // 使用正则表达式提取数字部分 |
| | | Pattern pattern = Pattern.compile("\\d+"); |
| | | Matcher matcher = pattern.matcher(cellValue); |
| | | |
| | | if (matcher.find()) { |
| | | return matcher.group(); |
| | | } |
| | | |
| | | // 如果没有找到数字,返回原值(可能有其他格式) |
| | | return cellValue; |
| | | } |
| | | } |