Commit 0b2120c2 authored by wujipeng's avatar wujipeng
Browse files

解析性能优化

parent ef8c067c
...@@ -16,18 +16,20 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook; ...@@ -16,18 +16,20 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.io.InputStream;
import java.nio.file.Path; import java.text.ParseException;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.stream.Collectors;
/** /**
* @author : wujipeng * @author : wujipeng
...@@ -48,31 +50,149 @@ public class AnalysisServiceImpl implements AnalysisService { ...@@ -48,31 +50,149 @@ public class AnalysisServiceImpl implements AnalysisService {
private static final String[] EXCEL_EXTENSIONS = {".xlsx", ".xls"}; private static final String[] EXCEL_EXTENSIONS = {".xlsx", ".xls"};
// @Override
// public Result<?> canvas(MultipartFile[] files, Integer templateId) {
// //查询模板
// ExcelTemplateEntity ExcelTemplate = excelTemplateService.getById(templateId);
// String json = ExcelTemplate.getJson();
// List<TableHeaderVO> tableHeaderList = JSON.parseArray(json, TableHeaderVO.class);
// if (ObjectUtils.isEmpty(ExcelTemplate)) {
// return Result.fail("模板不存在");
// }
// try {
// if (files.length == 0) {
// return Result.fail("上传的文件为空");
// }
// List<FileUploadVO> fileUploadList = Lists.newArrayList();
// for (MultipartFile file : files) {
// // 检查是否是Excel文件
// if (isExcelFile(file.getOriginalFilename())) {
// File tempFile = convertMultipartFileToFile(file);
// processSingleExcelFile(tempFile, tableHeaderList);
// //文件上传
// FileUploadVO fileUploadVO = fileUploadService.uploadFile(file);
// fileUploadList.add(fileUploadVO);
// }
// }
// return Result.data(fileUploadList);
// } catch (Exception e) {
// return Result.fail("文件处理失败: " + e.getMessage());
// }
// }
@Override @Override
public Result<?> canvas(MultipartFile[] files, Integer templateId) { public Result<?> canvas(MultipartFile[] files, Integer templateId) {
//查询模板 // 1. 预检查
ExcelTemplateEntity ExcelTemplate = excelTemplateService.getById(templateId); if (files == null || files.length == 0) {
if(ObjectUtils.isEmpty(ExcelTemplate)){ return Result.fail("上传的文件为空");
return Result.fail("模板不存在");
} }
// 2. 异步处理文件
List<CompletableFuture<FileUploadVO>> futures = Arrays.stream(files)
.filter(file -> !file.isEmpty() && isExcelFile(file.getOriginalFilename()))
.map(file -> CompletableFuture.supplyAsync(() -> processFile(file, templateId)))
.collect(Collectors.toList());
// 3. 等待所有任务完成并收集结果
try { try {
if (files.length == 0) { List<FileUploadVO> fileUploadList = futures.stream()
return Result.fail("上传的文件为空"); .map(CompletableFuture::join)
.collect(Collectors.toList());
return Result.data(fileUploadList);
} catch (CompletionException e) {
return Result.fail("文件处理失败: " + e.getCause().getMessage());
}
}
private FileUploadVO processFile(MultipartFile file, Integer templateId) {
try {
// 1. 获取模板
ExcelTemplateEntity excelTemplate = excelTemplateService.getById(templateId);
if (excelTemplate == null) {
throw new RuntimeException("模板不存在");
} }
List<FileUploadVO> fileUploadList = com.google.common.collect.Lists.newArrayList();
for (MultipartFile file : files) { List<TableHeaderVO> tableHeaderList = JSON.parseArray(excelTemplate.getJson(), TableHeaderVO.class);
// 检查是否是Excel文件
if (isExcelFile(file.getOriginalFilename())) { // 2. 使用内存处理替代临时文件
File tempFile = convertMultipartFileToFile(file); Workbook workbook = getWorkbook(file);
processSingleExcelFile(tempFile,ExcelTemplate); Sheet sheet = workbook.getSheetAt(0);
//文件上传
FileUploadVO fileUploadVO = fileUploadService.uploadFile(file); // 3. 批量处理数据
fileUploadList.add(fileUploadVO); List<ExcelDataEntity> excelData = new ArrayList<>(sheet.getLastRowNum());
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
if (row != null) {
ExcelDataEntity entity = handleExcelData(tableHeaderList, row);
if (entity != null) {
excelData.add(entity);
}
} }
} }
return Result.data(fileUploadList);
// 4. 批量保存
if (!excelData.isEmpty()) {
excelDataService.saveBatch(excelData);
}
// 5. 上传文件
return fileUploadService.uploadFile(file);
} catch (Exception e) { } catch (Exception e) {
return Result.fail("文件处理失败: " + e.getMessage()); throw new RuntimeException("处理文件 " + file.getOriginalFilename() + " 失败: " + e.getMessage(), e);
}
}
// 直接从MultipartFile创建Workbook
private Workbook getWorkbook(MultipartFile file) throws IOException {
String fileName = file.getOriginalFilename().toLowerCase();
InputStream inputStream = file.getInputStream();
if (fileName.endsWith(".xlsx")) {
return new XSSFWorkbook(inputStream);
} else if (fileName.endsWith(".xls")) {
return new HSSFWorkbook(inputStream);
} else {
throw new IllegalArgumentException("不支持的Excel文件格式");
}
}
// 重构handleExcelData返回单个实体
private ExcelDataEntity handleExcelData(List<TableHeaderVO> tableHeaderList, Row row) {
ExcelDataEntity excelDataEntity = new ExcelDataEntity();
trySetValue(() -> excelDataEntity.setType(getCellValueAsString(row.getCell(tableHeaderList.get(0).getIndex()))));
trySetValue(() -> excelDataEntity.setDirection(getCellValueAsString(row.getCell(tableHeaderList.get(1).getIndex()))));
trySetValue(() -> {
try {
excelDataEntity.setTime(DateUtils.parseCompactDateTime(getCellValueAsString(row.getCell(tableHeaderList.get(2).getIndex()))));
} catch (ParseException ignored) {
}
});
trySetValue(() -> excelDataEntity.setAmount(Double.parseDouble(getCellValueAsString(row.getCell(tableHeaderList.get(3).getIndex())))));
trySetValue(() -> excelDataEntity.setSerial(getCellValueAsString(row.getCell(tableHeaderList.get(4).getIndex()))));
trySetValue(() -> excelDataEntity.setPayeeBankCard(getCellValueAsString(row.getCell(tableHeaderList.get(5).getIndex()))));
trySetValue(() -> excelDataEntity.setPayeeAccount(getCellValueAsString(row.getCell(tableHeaderList.get(6).getIndex()))));
trySetValue(() -> excelDataEntity.setPayeeMerchant(getCellValueAsString(row.getCell(tableHeaderList.get(7).getIndex()))));
trySetValue(() -> excelDataEntity.setPayeeName(getCellValueAsString(row.getCell(tableHeaderList.get(8).getIndex()))));
trySetValue(() -> excelDataEntity.setPaymentBankName(getCellValueAsString(row.getCell(tableHeaderList.get(9).getIndex()))));
trySetValue(() -> excelDataEntity.setPaymentBankCard(getCellValueAsString(row.getCell(tableHeaderList.get(10).getIndex()))));
trySetValue(() -> excelDataEntity.setPaymentAccount(getCellValueAsString(row.getCell(tableHeaderList.get(11).getIndex()))));
trySetValue(() -> excelDataEntity.setIp(getCellValueAsString(row.getCell(tableHeaderList.get(12).getIndex()))));
trySetValue(() -> excelDataEntity.setMac(getCellValueAsString(row.getCell(tableHeaderList.get(13).getIndex()))));
trySetValue(() -> excelDataEntity.setExternalChannel(getCellValueAsString(row.getCell(tableHeaderList.get(14).getIndex()))));
trySetValue(() -> excelDataEntity.setPayAccount(getCellValueAsString(row.getCell(tableHeaderList.get(15).getIndex()))));
trySetValue(() -> excelDataEntity.setRemark(getCellValueAsString(row.getCell(tableHeaderList.get(16).getIndex()))));
return excelDataEntity;
}
// 辅助方法:简化异常处理
private void trySetValue(Runnable setter) {
try {
setter.run();
} catch (Exception ignored) {
// 忽略单个字段设置失败
} }
} }
...@@ -119,10 +239,7 @@ public class AnalysisServiceImpl implements AnalysisService { ...@@ -119,10 +239,7 @@ public class AnalysisServiceImpl implements AnalysisService {
return false; return false;
} }
private void processSingleExcelFile(File excelFile,ExcelTemplateEntity ExcelTemplate) throws IOException { private void processSingleExcelFile(File excelFile, List<TableHeaderVO> tableHeaderList) throws IOException {
String json = ExcelTemplate.getJson();
List<TableHeaderVO> tableHeaderList = JSON.parseArray(json, TableHeaderVO.class);
//读取第一个sheet //读取第一个sheet
Sheet sheet = getWorkbook(excelFile).getSheetAt(0); Sheet sheet = getWorkbook(excelFile).getSheetAt(0);
//获取sheet长度 //获取sheet长度
...@@ -143,55 +260,72 @@ public class AnalysisServiceImpl implements AnalysisService { ...@@ -143,55 +260,72 @@ public class AnalysisServiceImpl implements AnalysisService {
ExcelDataEntity excelDataEntity = new ExcelDataEntity(); ExcelDataEntity excelDataEntity = new ExcelDataEntity();
try { try {
excelDataEntity.setType(getCellValueAsString(row.getCell(tableHeaderList.get(0).getIndex()))); excelDataEntity.setType(getCellValueAsString(row.getCell(tableHeaderList.get(0).getIndex())));
} catch (Exception ignored) {} } catch (Exception ignored) {
}
try { try {
excelDataEntity.setDirection(getCellValueAsString(row.getCell(tableHeaderList.get(1).getIndex()))); excelDataEntity.setDirection(getCellValueAsString(row.getCell(tableHeaderList.get(1).getIndex())));
} catch (Exception ignored) {} } catch (Exception ignored) {
}
try { try {
excelDataEntity.setTime(DateUtils.parseCompactDateTime(getCellValueAsString(row.getCell(tableHeaderList.get(2).getIndex())))); excelDataEntity.setTime(DateUtils.parseCompactDateTime(getCellValueAsString(row.getCell(tableHeaderList.get(2).getIndex()))));
} catch (Exception ignored) {} } catch (Exception ignored) {
}
try { try {
excelDataEntity.setAmount(Double.parseDouble(getCellValueAsString(row.getCell(tableHeaderList.get(3).getIndex())))); excelDataEntity.setAmount(Double.parseDouble(getCellValueAsString(row.getCell(tableHeaderList.get(3).getIndex()))));
} catch (Exception ignored) {} } catch (Exception ignored) {
}
try { try {
excelDataEntity.setSerial(getCellValueAsString(row.getCell(tableHeaderList.get(4).getIndex()))); excelDataEntity.setSerial(getCellValueAsString(row.getCell(tableHeaderList.get(4).getIndex())));
} catch (Exception ignored) {} } catch (Exception ignored) {
}
try { try {
excelDataEntity.setPayeeBankCard(getCellValueAsString(row.getCell(tableHeaderList.get(5).getIndex()))); excelDataEntity.setPayeeBankCard(getCellValueAsString(row.getCell(tableHeaderList.get(5).getIndex())));
} catch (Exception ignored) {} } catch (Exception ignored) {
}
try { try {
excelDataEntity.setPayeeAccount(getCellValueAsString(row.getCell(tableHeaderList.get(6).getIndex()))); excelDataEntity.setPayeeAccount(getCellValueAsString(row.getCell(tableHeaderList.get(6).getIndex())));
} catch (Exception ignored) {} } catch (Exception ignored) {
}
try { try {
excelDataEntity.setPayeeMerchant(getCellValueAsString(row.getCell(tableHeaderList.get(7).getIndex()))); excelDataEntity.setPayeeMerchant(getCellValueAsString(row.getCell(tableHeaderList.get(7).getIndex())));
} catch (Exception ignored) {} } catch (Exception ignored) {
}
try { try {
excelDataEntity.setPayeeName(getCellValueAsString(row.getCell(tableHeaderList.get(8).getIndex()))); excelDataEntity.setPayeeName(getCellValueAsString(row.getCell(tableHeaderList.get(8).getIndex())));
} catch (Exception ignored) {} } catch (Exception ignored) {
}
try { try {
excelDataEntity.setPaymentBankName(getCellValueAsString(row.getCell(tableHeaderList.get(9).getIndex()))); excelDataEntity.setPaymentBankName(getCellValueAsString(row.getCell(tableHeaderList.get(9).getIndex())));
} catch (Exception ignored) {} } catch (Exception ignored) {
}
try { try {
excelDataEntity.setPaymentBankCard(getCellValueAsString(row.getCell(tableHeaderList.get(10).getIndex()))); excelDataEntity.setPaymentBankCard(getCellValueAsString(row.getCell(tableHeaderList.get(10).getIndex())));
} catch (Exception ignored) {} } catch (Exception ignored) {
}
try { try {
excelDataEntity.setPaymentAccount(getCellValueAsString(row.getCell(tableHeaderList.get(11).getIndex()))); excelDataEntity.setPaymentAccount(getCellValueAsString(row.getCell(tableHeaderList.get(11).getIndex())));
} catch (Exception ignored) {} } catch (Exception ignored) {
}
try { try {
excelDataEntity.setIp(getCellValueAsString(row.getCell(tableHeaderList.get(12).getIndex()))); excelDataEntity.setIp(getCellValueAsString(row.getCell(tableHeaderList.get(12).getIndex())));
} catch (Exception ignored) {} } catch (Exception ignored) {
}
try { try {
excelDataEntity.setMac(getCellValueAsString(row.getCell(tableHeaderList.get(13).getIndex()))); excelDataEntity.setMac(getCellValueAsString(row.getCell(tableHeaderList.get(13).getIndex())));
} catch (Exception ignored) {} } catch (Exception ignored) {
}
try { try {
excelDataEntity.setExternalChannel(getCellValueAsString(row.getCell(tableHeaderList.get(14).getIndex()))); excelDataEntity.setExternalChannel(getCellValueAsString(row.getCell(tableHeaderList.get(14).getIndex())));
} catch (Exception ignored) {} } catch (Exception ignored) {
}
try { try {
excelDataEntity.setPayAccount(getCellValueAsString(row.getCell(tableHeaderList.get(15).getIndex()))); excelDataEntity.setPayAccount(getCellValueAsString(row.getCell(tableHeaderList.get(15).getIndex())));
} catch (Exception ignored) {} } catch (Exception ignored) {
}
try { try {
excelDataEntity.setRemark(getCellValueAsString(row.getCell(tableHeaderList.get(16).getIndex()))); excelDataEntity.setRemark(getCellValueAsString(row.getCell(tableHeaderList.get(16).getIndex())));
} catch (Exception ignored) {} } catch (Exception ignored) {
}
excelData.add(excelDataEntity); excelData.add(excelDataEntity);
} }
......
...@@ -56,11 +56,11 @@ public class FileUploadServiceImpl extends ...@@ -56,11 +56,11 @@ public class FileUploadServiceImpl extends
//获取文件绝对路径 //获取文件绝对路径
String fileUrl = path.toAbsolutePath().toString(); String fileUrl = path.toAbsolutePath().toString();
//获取当前登陆人信息 //获取当前登陆人信息
String loginUserId = SecurityUtils.getLoginUserId(); // String loginUserId = SecurityUtils.getLoginUserId();
FileUploadVO bsFileUploadVO = new FileUploadVO(); FileUploadVO bsFileUploadVO = new FileUploadVO();
bsFileUploadVO.setFileName(fileName); bsFileUploadVO.setFileName(fileName);
bsFileUploadVO.setPath(fileUrl); bsFileUploadVO.setPath(fileUrl);
bsFileUploadVO.setUploadBy(loginUserId); // bsFileUploadVO.setUploadBy(loginUserId);
bsFileUploadVO.setCreateTime(new Date()); bsFileUploadVO.setCreateTime(new Date());
this.saveOrUpdate(bsFileUploadVO, new LambdaQueryWrapper<FileUploadEntity>().eq(FileUploadEntity::getFileName, fileName)); this.saveOrUpdate(bsFileUploadVO, new LambdaQueryWrapper<FileUploadEntity>().eq(FileUploadEntity::getFileName, fileName));
return bsFileUploadVO; return bsFileUploadVO;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment