This commit is contained in:
mashuai 2025-03-19 10:16:12 +08:00
parent e4fd3d5dff
commit b5467a9d3d
2 changed files with 83 additions and 32 deletions

View File

@ -249,7 +249,7 @@ public interface DevInfoMapper {
* @param typeName
* @return
*/
DevInfoVo selectDevTypeByName(String typeName);
List<DevInfoVo> selectDevTypeByName(String typeName);
List<DevInfoVo> getDevNumList(DevInfoVo devInfoVo);

View File

@ -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<DevTemplateVo> util = new ExcelUtil<>(DevTemplateVo.class);
List<DevTemplateVo> maDevList = util.importExcel(file.getInputStream());
List<DevTemplateVo> templateVos = new ArrayList<>();
List<DevTemplateVo> 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<DevInfoVo> 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<String, List<DevTemplateVo>> map = maDevList.stream().collect(Collectors.groupingBy(DevTemplateVo::getDeviceName));
for (Map.Entry<String, List<DevTemplateVo>> entry : map.entrySet()) {
List<DevTemplateVo> dtoList = new ArrayList<>();
// 如果map的数量大于1则进行遍历
DevTemplateVo devTemplateVo = entry.getValue().get(0);
List<DevTemplateVo> 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