| | |
| | | import org.jeecg.modules.eam.constant.BusinessCodeConst; |
| | | import org.jeecg.modules.eam.constant.MaintenanceCategoryEnum; |
| | | import org.jeecg.modules.eam.dto.MaintenanceStandardImport; |
| | | import org.jeecg.modules.eam.dto.WeekMaintenanceStandardImport; |
| | | import org.jeecg.modules.eam.entity.EamEquipment; |
| | | import org.jeecg.modules.eam.entity.EamMaintenanceStandard; |
| | | import org.jeecg.modules.eam.entity.EamMaintenanceStandardDetail; |
| | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.IOException; |
| | | import java.nio.file.Files; |
| | | import java.nio.file.Paths; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | |
| | | } |
| | | |
| | | /** |
| | | * 通过excel导入数据 |
| | | * |
| | | * @param request |
| | | * @param response |
| | | * @return |
| | | */ |
| | | @RequestMapping(value = "/weekMaintenanceImportExcel", method = RequestMethod.POST) |
| | | public Result<?> weekMaintenanceImportExcel(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.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.WEEK_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 |
| | | * @param request |
| | |
| | | log.error("读取Excel信息失败:{}", e.getMessage(), e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 读取Excel 第一行, 第二行的信息 |
| | | * @param file |
| | | * @param request |
| | | */ |
| | | public void readWeekExcel(MultipartFile file, EamMaintenanceStandardRequest request) { |
| | | Workbook book = null; |
| | | boolean isXSSFWorkbook = false; |
| | | try { |
| | | book = WorkbookFactory.create(file.getInputStream()); |
| | | if (book instanceof XSSFWorkbook) { |
| | | isXSSFWorkbook = true; |
| | | } |
| | | |
| | | Sheet sheet = book.getSheetAt(0); |
| | | //第一行读取 |
| | | Row row = sheet.getRow(0); |
| | | //设备编码 |
| | | Cell equipmentCode = row.getCell(10); |
| | | 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 (StringUtils.isBlank(request.getEquipmentCode())) { |
| | | throw new JeecgBootException("Excel【 " + file.getOriginalFilename() + "】没有读取到设备编号,导入失败!"); |
| | | } |
| | | //初始日期 |
| | | Cell initialDate = row.getCell(6); |
| | | if (DateUtil.isCellDateFormatted(initialDate)) { |
| | | request.setInitialDate(initialDate.getDateCellValue()); |
| | | } else { |
| | | request.setInitialDate(new Date()); |
| | | } |
| | | //设备名称 |
| | | Cell equipmentName = row.getCell(8); |
| | | request.setEquipmentName(equipmentName.getStringCellValue()); |
| | | |
| | | //第二行读取 |
| | | row = sheet.getRow(1); |
| | | //保养周期 |
| | | Cell period = row.getCell(6); |
| | | if (CellType.NUMERIC.equals(period.getCellType())) { |
| | | request.setMaintenancePeriod((int) period.getNumericCellValue()); |
| | | } else { |
| | | //默认点检周期 1 |
| | | request.setMaintenancePeriod(1); |
| | | } |
| | | //文件编码 |
| | | Cell fileCode = row.getCell(8); |
| | | request.setFileCode(fileCode.getStringCellValue()); |
| | | } catch (Exception e) { |
| | | log.error("读取Excel信息失败:{}", e.getMessage(), e); |
| | | } |
| | | } |
| | | } |