From b5467a9d3d75f58f5e3be4d39b44b0d785bad248 Mon Sep 17 00:00:00 2001 From: mashuai Date: Wed, 19 Mar 2025 10:16:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../material/device/mapper/DevInfoMapper.java | 2 +- .../service/impl/DevInfoServiceImpl.java | 113 +++++++++++++----- 2 files changed, 83 insertions(+), 32 deletions(-) diff --git a/bonus-modules/bonus-material-mall/src/main/java/com/bonus/material/device/mapper/DevInfoMapper.java b/bonus-modules/bonus-material-mall/src/main/java/com/bonus/material/device/mapper/DevInfoMapper.java index 3569eec..af29a4d 100644 --- a/bonus-modules/bonus-material-mall/src/main/java/com/bonus/material/device/mapper/DevInfoMapper.java +++ b/bonus-modules/bonus-material-mall/src/main/java/com/bonus/material/device/mapper/DevInfoMapper.java @@ -249,7 +249,7 @@ public interface DevInfoMapper { * @param typeName * @return */ - DevInfoVo selectDevTypeByName(String typeName); + List selectDevTypeByName(String typeName); List getDevNumList(DevInfoVo devInfoVo); diff --git a/bonus-modules/bonus-material-mall/src/main/java/com/bonus/material/device/service/impl/DevInfoServiceImpl.java b/bonus-modules/bonus-material-mall/src/main/java/com/bonus/material/device/service/impl/DevInfoServiceImpl.java index a70d3a2..ac15248 100644 --- a/bonus-modules/bonus-material-mall/src/main/java/com/bonus/material/device/service/impl/DevInfoServiceImpl.java +++ b/bonus-modules/bonus-material-mall/src/main/java/com/bonus/material/device/service/impl/DevInfoServiceImpl.java @@ -52,7 +52,10 @@ import java.io.OutputStream; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeFormatterBuilder; import java.time.format.DateTimeParseException; +import java.time.format.ResolverStyle; +import java.time.temporal.ChronoField; import java.util.*; import java.util.stream.Collectors; @@ -953,18 +956,18 @@ public class DevInfoServiceImpl implements DevInfoService { // 读取表头内容并验证每一列 extractedText(headerRow, totalCells); //读取Excel表格数据,做非空及格式判断 - //extractedCell(sheet, totalRows, totalCells); + extractedCell(sheet, totalRows, totalCells); ExcelUtil util = new ExcelUtil<>(DevTemplateVo.class); List maDevList = util.importExcel(file.getInputStream()); List templateVos = new ArrayList<>(); - List dtoList = new ArrayList<>(); // 判断装备类目是否为空,查询装备id及价格 if (!CollectionUtils.isEmpty(maDevList)) { for (DevTemplateVo devTemplateVo : maDevList) { if (StringUtils.isNotBlank(devTemplateVo.getTypeName())) { - // 根据装备类目查询装备id及价格 - DevInfoVo devType = devInfoMapper.selectDevTypeByName(devTemplateVo.getTypeName()); - if (devType != null) { + // 根据装备类目查询装备详情 + List list = devInfoMapper.selectDevTypeByName(devTemplateVo.getTypeName()); + if (!CollectionUtils.isEmpty(list)) { + DevInfoVo devType = list.get(0); devTemplateVo.setTypeId(devType.getTypeId() != null ? devType.getTypeId() : null); devTemplateVo.setTypeName(devType.getTypeName() != null ? devType.getTypeName() : null); devTemplateVo.setDayLeasePrice(devType.getDayLeasePrice() != null ? devType.getDayLeasePrice() : null); @@ -978,10 +981,15 @@ public class DevInfoServiceImpl implements DevInfoService { // 对maDevList通过装备名称进行分组 Map> map = maDevList.stream().collect(Collectors.groupingBy(DevTemplateVo::getDeviceName)); for (Map.Entry> entry : map.entrySet()) { + List dtoList = new ArrayList<>(); // 如果map的数量大于1,则进行遍历 DevTemplateVo devTemplateVo = entry.getValue().get(0); List devTemplateVos = entry.getValue(); for (DevTemplateVo templateVo : devTemplateVos) { + if (StringUtils.isBlank(templateVo.getIdentifyCode()) && StringUtils.isBlank(templateVo.getCheckMan()) + && templateVo.getNextCheckDate() == null && templateVo.getCheckDate() == null) { + continue; + } DevTemplateVo dto = new DevTemplateVo(); dto.setIdentifyCode(StringUtils.isNotBlank(templateVo.getIdentifyCode()) ? templateVo.getIdentifyCode() : null); dto.setCheckMan(StringUtils.isNotBlank(templateVo.getCheckMan()) ? templateVo.getCheckMan() : null); @@ -989,7 +997,9 @@ public class DevInfoServiceImpl implements DevInfoService { dto.setNextCheckDate(templateVo.getNextCheckDate() != null ? templateVo.getNextCheckDate() : null); dtoList.add(dto); } - devTemplateVo.setTableList(dtoList); + if (!CollectionUtils.isEmpty(dtoList)) { + devTemplateVo.setTableList(dtoList); + } templateVos.add(devTemplateVo); } @@ -1011,11 +1021,26 @@ public class DevInfoServiceImpl implements DevInfoService { //读取Excel表格数据,做非空判断 // 循环Excel行数 DataFormatter dataFormatter = new DataFormatter(); + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd"); for (int r = 1; r < totalRows; r++) { Row row = sheet.getRow(r); // 循环Excel列数 for (int c = 0; c < totalCells; c++) { - String cellValue = dataFormatter.formatCellValue(row.getCell(c)); + Cell cell = row.getCell(c); + String cellValue = ""; + if (cell != null && cell.getCellType() == CellType.NUMERIC) { + if (DateUtil.isCellDateFormatted(cell)) { + Date date = cell.getDateCellValue(); + cellValue = dateFormat.format(date); + } else { + cellValue = String.valueOf(cell.getNumericCellValue()); + } + } else if (cell != null && cell.getCellType() == CellType.STRING) { + cellValue = cell.getStringCellValue(); + } + if (c == 5) { + cellValue = dataFormatter.formatCellValue(row.getCell(c)); + } switch (c) { case 0: checkBlank(cellValue, r, c); @@ -1067,34 +1092,60 @@ public class DevInfoServiceImpl implements DevInfoService { */ private void checkDate(String cellValue, int rowIndex, int colIndex) { if (StringUtils.isNotBlank(cellValue)) { - // 定义两种日期格式的 DateTimeFormatter - DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd"); - DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy/MM/dd"); - - boolean isValid = false; - try { - // 尝试使用 yyyy-MM-dd 格式解析 - LocalDate.parse(cellValue, formatter1); - isValid = true; - } catch (DateTimeParseException e1) { - try { - // 若第一种格式解析失败,尝试使用 yyyy/MM/dd 格式解析 - LocalDate.parse(cellValue, formatter2); - isValid = true; - } catch (DateTimeParseException e2) { - // 两种格式都解析失败,抛出异常 - if (StringUtils.isNotBlank(cellValue)) { - throw new IllegalArgumentException( - String.format("第 %d 行,第 %d 列时间日期不符合格式要求,请检查后重新导入", rowIndex + 1, colIndex + 1)); - } - } - } - if (isValid) { - System.out.println(true); + boolean b = isValidDate(cellValue); + if (!b) { + throw new IllegalArgumentException( + String.format("第 %d 行,第 %d 列日期格式错误,请检查后重新导入", rowIndex + 1, colIndex + 1)); } } } + /** + * 判断日期格式是否正确 + * @param dateStr + * @return + */ + private static boolean isValidDate(String dateStr) { + // 构建三种允许单位数月份和日期的格式化器,并设置严格解析模式 + DateTimeFormatter[] formatters = { + new DateTimeFormatterBuilder() + .appendValue(ChronoField.YEAR, 4) + .appendLiteral('-') + .appendValue(ChronoField.MONTH_OF_YEAR, 1, 2, java.time.format.SignStyle.NOT_NEGATIVE) + .appendLiteral('-') + .appendValue(ChronoField.DAY_OF_MONTH, 1, 2, java.time.format.SignStyle.NOT_NEGATIVE) + .toFormatter() + .withResolverStyle(ResolverStyle.STRICT), + new DateTimeFormatterBuilder() + .appendValue(ChronoField.YEAR, 4) + .appendLiteral('/') + .appendValue(ChronoField.MONTH_OF_YEAR, 1, 2, java.time.format.SignStyle.NOT_NEGATIVE) + .appendLiteral('/') + .appendValue(ChronoField.DAY_OF_MONTH, 1, 2, java.time.format.SignStyle.NOT_NEGATIVE) + .toFormatter() + .withResolverStyle(ResolverStyle.STRICT), + new DateTimeFormatterBuilder() + .appendValue(ChronoField.YEAR, 4) + .appendLiteral('.') + .appendValue(ChronoField.MONTH_OF_YEAR, 1, 2, java.time.format.SignStyle.NOT_NEGATIVE) + .appendLiteral('.') + .appendValue(ChronoField.DAY_OF_MONTH, 1, 2, java.time.format.SignStyle.NOT_NEGATIVE) + .toFormatter() + .withResolverStyle(ResolverStyle.STRICT) + }; + + // 遍历格式化器数组,尝试解析日期 + for (DateTimeFormatter formatter : formatters) { + try { + LocalDate.parse(dateStr, formatter); + return true; + } catch (DateTimeParseException e) { + // 若解析失败,继续尝试下一个格式化器 + } + } + return false; + } + /** * 检查数据是否为空 * @param cellValue