This commit is contained in:
parent
e4fd3d5dff
commit
b5467a9d3d
|
|
@ -249,7 +249,7 @@ public interface DevInfoMapper {
|
||||||
* @param typeName
|
* @param typeName
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
DevInfoVo selectDevTypeByName(String typeName);
|
List<DevInfoVo> selectDevTypeByName(String typeName);
|
||||||
|
|
||||||
List<DevInfoVo> getDevNumList(DevInfoVo devInfoVo);
|
List<DevInfoVo> getDevNumList(DevInfoVo devInfoVo);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -52,7 +52,10 @@ import java.io.OutputStream;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.time.format.DateTimeFormatterBuilder;
|
||||||
import java.time.format.DateTimeParseException;
|
import java.time.format.DateTimeParseException;
|
||||||
|
import java.time.format.ResolverStyle;
|
||||||
|
import java.time.temporal.ChronoField;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
|
@ -953,18 +956,18 @@ public class DevInfoServiceImpl implements DevInfoService {
|
||||||
// 读取表头内容并验证每一列
|
// 读取表头内容并验证每一列
|
||||||
extractedText(headerRow, totalCells);
|
extractedText(headerRow, totalCells);
|
||||||
//读取Excel表格数据,做非空及格式判断
|
//读取Excel表格数据,做非空及格式判断
|
||||||
//extractedCell(sheet, totalRows, totalCells);
|
extractedCell(sheet, totalRows, totalCells);
|
||||||
ExcelUtil<DevTemplateVo> util = new ExcelUtil<>(DevTemplateVo.class);
|
ExcelUtil<DevTemplateVo> util = new ExcelUtil<>(DevTemplateVo.class);
|
||||||
List<DevTemplateVo> maDevList = util.importExcel(file.getInputStream());
|
List<DevTemplateVo> maDevList = util.importExcel(file.getInputStream());
|
||||||
List<DevTemplateVo> templateVos = new ArrayList<>();
|
List<DevTemplateVo> templateVos = new ArrayList<>();
|
||||||
List<DevTemplateVo> dtoList = new ArrayList<>();
|
|
||||||
// 判断装备类目是否为空,查询装备id及价格
|
// 判断装备类目是否为空,查询装备id及价格
|
||||||
if (!CollectionUtils.isEmpty(maDevList)) {
|
if (!CollectionUtils.isEmpty(maDevList)) {
|
||||||
for (DevTemplateVo devTemplateVo : maDevList) {
|
for (DevTemplateVo devTemplateVo : maDevList) {
|
||||||
if (StringUtils.isNotBlank(devTemplateVo.getTypeName())) {
|
if (StringUtils.isNotBlank(devTemplateVo.getTypeName())) {
|
||||||
// 根据装备类目查询装备id及价格
|
// 根据装备类目查询装备详情
|
||||||
DevInfoVo devType = devInfoMapper.selectDevTypeByName(devTemplateVo.getTypeName());
|
List<DevInfoVo> list = devInfoMapper.selectDevTypeByName(devTemplateVo.getTypeName());
|
||||||
if (devType != null) {
|
if (!CollectionUtils.isEmpty(list)) {
|
||||||
|
DevInfoVo devType = list.get(0);
|
||||||
devTemplateVo.setTypeId(devType.getTypeId() != null ? devType.getTypeId() : null);
|
devTemplateVo.setTypeId(devType.getTypeId() != null ? devType.getTypeId() : null);
|
||||||
devTemplateVo.setTypeName(devType.getTypeName() != null ? devType.getTypeName() : null);
|
devTemplateVo.setTypeName(devType.getTypeName() != null ? devType.getTypeName() : null);
|
||||||
devTemplateVo.setDayLeasePrice(devType.getDayLeasePrice() != null ? devType.getDayLeasePrice() : null);
|
devTemplateVo.setDayLeasePrice(devType.getDayLeasePrice() != null ? devType.getDayLeasePrice() : null);
|
||||||
|
|
@ -978,10 +981,15 @@ public class DevInfoServiceImpl implements DevInfoService {
|
||||||
// 对maDevList通过装备名称进行分组
|
// 对maDevList通过装备名称进行分组
|
||||||
Map<String, List<DevTemplateVo>> map = maDevList.stream().collect(Collectors.groupingBy(DevTemplateVo::getDeviceName));
|
Map<String, List<DevTemplateVo>> map = maDevList.stream().collect(Collectors.groupingBy(DevTemplateVo::getDeviceName));
|
||||||
for (Map.Entry<String, List<DevTemplateVo>> entry : map.entrySet()) {
|
for (Map.Entry<String, List<DevTemplateVo>> entry : map.entrySet()) {
|
||||||
|
List<DevTemplateVo> dtoList = new ArrayList<>();
|
||||||
// 如果map的数量大于1,则进行遍历
|
// 如果map的数量大于1,则进行遍历
|
||||||
DevTemplateVo devTemplateVo = entry.getValue().get(0);
|
DevTemplateVo devTemplateVo = entry.getValue().get(0);
|
||||||
List<DevTemplateVo> devTemplateVos = entry.getValue();
|
List<DevTemplateVo> devTemplateVos = entry.getValue();
|
||||||
for (DevTemplateVo templateVo : devTemplateVos) {
|
for (DevTemplateVo templateVo : devTemplateVos) {
|
||||||
|
if (StringUtils.isBlank(templateVo.getIdentifyCode()) && StringUtils.isBlank(templateVo.getCheckMan())
|
||||||
|
&& templateVo.getNextCheckDate() == null && templateVo.getCheckDate() == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
DevTemplateVo dto = new DevTemplateVo();
|
DevTemplateVo dto = new DevTemplateVo();
|
||||||
dto.setIdentifyCode(StringUtils.isNotBlank(templateVo.getIdentifyCode()) ? templateVo.getIdentifyCode() : null);
|
dto.setIdentifyCode(StringUtils.isNotBlank(templateVo.getIdentifyCode()) ? templateVo.getIdentifyCode() : null);
|
||||||
dto.setCheckMan(StringUtils.isNotBlank(templateVo.getCheckMan()) ? templateVo.getCheckMan() : 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);
|
dto.setNextCheckDate(templateVo.getNextCheckDate() != null ? templateVo.getNextCheckDate() : null);
|
||||||
dtoList.add(dto);
|
dtoList.add(dto);
|
||||||
}
|
}
|
||||||
devTemplateVo.setTableList(dtoList);
|
if (!CollectionUtils.isEmpty(dtoList)) {
|
||||||
|
devTemplateVo.setTableList(dtoList);
|
||||||
|
}
|
||||||
templateVos.add(devTemplateVo);
|
templateVos.add(devTemplateVo);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -1011,11 +1021,26 @@ public class DevInfoServiceImpl implements DevInfoService {
|
||||||
//读取Excel表格数据,做非空判断
|
//读取Excel表格数据,做非空判断
|
||||||
// 循环Excel行数
|
// 循环Excel行数
|
||||||
DataFormatter dataFormatter = new DataFormatter();
|
DataFormatter dataFormatter = new DataFormatter();
|
||||||
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
|
||||||
for (int r = 1; r < totalRows; r++) {
|
for (int r = 1; r < totalRows; r++) {
|
||||||
Row row = sheet.getRow(r);
|
Row row = sheet.getRow(r);
|
||||||
// 循环Excel列数
|
// 循环Excel列数
|
||||||
for (int c = 0; c < totalCells; c++) {
|
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) {
|
switch (c) {
|
||||||
case 0:
|
case 0:
|
||||||
checkBlank(cellValue, r, c);
|
checkBlank(cellValue, r, c);
|
||||||
|
|
@ -1067,34 +1092,60 @@ public class DevInfoServiceImpl implements DevInfoService {
|
||||||
*/
|
*/
|
||||||
private void checkDate(String cellValue, int rowIndex, int colIndex) {
|
private void checkDate(String cellValue, int rowIndex, int colIndex) {
|
||||||
if (StringUtils.isNotBlank(cellValue)) {
|
if (StringUtils.isNotBlank(cellValue)) {
|
||||||
// 定义两种日期格式的 DateTimeFormatter
|
boolean b = isValidDate(cellValue);
|
||||||
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
if (!b) {
|
||||||
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy/MM/dd");
|
throw new IllegalArgumentException(
|
||||||
|
String.format("第 %d 行,第 %d 列日期格式错误,请检查后重新导入", rowIndex + 1, colIndex + 1));
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断日期格式是否正确
|
||||||
|
* @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
|
* @param cellValue
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue