Commit 61391b39 authored by wujipeng's avatar wujipeng
Browse files

解析excel新增字段

parent 703fa13c
...@@ -86,6 +86,14 @@ ...@@ -86,6 +86,14 @@
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins> </plugins>
</build> </build>
......
...@@ -28,8 +28,10 @@ public class AnalysisController { ...@@ -28,8 +28,10 @@ public class AnalysisController {
@ApiOperation("解析画布边文件上传excel内容") @ApiOperation("解析画布边文件上传excel内容")
@PostMapping("/canvas") @PostMapping("/canvas")
public Result<?> canvas(@RequestParam(value = "files") MultipartFile[] files, public Result<?> canvas(@RequestParam(value = "files") MultipartFile[] files,
@RequestParam(value = "templateId") Integer templateId) { @RequestParam(value = "templateId") Integer templateId,
return analysisService.canvas(files,templateId); @RequestParam(value = "caseId") String caseId,
@RequestParam(value = "folderName", required = false) String folderName) {
return analysisService.canvas(files,templateId,caseId,folderName);
} }
@ApiOperation("解析模板表头") @ApiOperation("解析模板表头")
......
...@@ -19,7 +19,7 @@ public interface AnalysisService { ...@@ -19,7 +19,7 @@ public interface AnalysisService {
* @param files * @param files
* @return * @return
*/ */
Result<?> canvas(MultipartFile[] files,Integer templateId); Result<?> canvas(MultipartFile[] files,Integer templateId,String caseId,String folderName);
/** /**
* 读取模板文件 * 读取模板文件
......
...@@ -11,11 +11,13 @@ import com.platform.service.ExcelDataService; ...@@ -11,11 +11,13 @@ import com.platform.service.ExcelDataService;
import com.platform.service.ExcelTemplateService; import com.platform.service.ExcelTemplateService;
import com.platform.service.FileUploadService; import com.platform.service.FileUploadService;
import com.platform.utils.DateUtils; import com.platform.utils.DateUtils;
import com.platform.utils.StringUtils;
import org.apache.commons.compress.utils.Lists; import org.apache.commons.compress.utils.Lists;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; 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;
...@@ -29,6 +31,8 @@ import java.util.Arrays; ...@@ -29,6 +31,8 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException; import java.util.concurrent.CompletionException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
...@@ -50,115 +54,37 @@ public class AnalysisServiceImpl implements AnalysisService { ...@@ -50,115 +54,37 @@ 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,String caseId, String folderName) {
// 1. 预检查 //查询模板
if (files == null || files.length == 0) { ExcelTemplateEntity ExcelTemplate = excelTemplateService.getById(templateId);
return Result.fail("上传的文件为空"); String json = ExcelTemplate.getJson();
List<TableHeaderVO> tableHeaderList = JSON.parseArray(json, TableHeaderVO.class);
if (ObjectUtils.isEmpty(ExcelTemplate)) {
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 {
List<FileUploadVO> fileUploadList = futures.stream() if (files.length == 0) {
.map(CompletableFuture::join) return Result.fail("上传的文件为空");
.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 = Lists.newArrayList();
List<TableHeaderVO> tableHeaderList = JSON.parseArray(excelTemplate.getJson(), TableHeaderVO.class); for (MultipartFile file : files) {
// 检查是否是Excel文件
// 2. 使用内存处理替代临时文件 if (isExcelFile(file.getOriginalFilename())) {
Workbook workbook = getWorkbook(file); FileUploadVO fileUploadVO = fileUploadService.uploadFile(file);
Sheet sheet = workbook.getSheetAt(0); File tempFile = convertMultipartFileToFile(file);
processSingleExcelFile(tempFile, tableHeaderList,folderName, fileUploadVO.getId(),caseId);
// 3. 批量处理数据 //文件上传
List<ExcelDataEntity> excelData = new ArrayList<>(sheet.getLastRowNum()); fileUploadList.add(fileUploadVO);
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) {
throw new RuntimeException("处理文件 " + file.getOriginalFilename() + " 失败: " + e.getMessage(), e); return Result.fail("文件处理失败: " + e.getMessage());
} }
} }
// 直接从MultipartFile创建Workbook private ExcelDataEntity handleExcelData(List<TableHeaderVO> tableHeaderList, Row row, String folderName,String fileId,String caseId) {
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(); ExcelDataEntity excelDataEntity = new ExcelDataEntity();
trySetValue(() -> excelDataEntity.setType(getCellValueAsString(row.getCell(tableHeaderList.get(0).getIndex())))); trySetValue(() -> excelDataEntity.setType(getCellValueAsString(row.getCell(tableHeaderList.get(0).getIndex()))));
...@@ -183,10 +109,28 @@ public class AnalysisServiceImpl implements AnalysisService { ...@@ -183,10 +109,28 @@ public class AnalysisServiceImpl implements AnalysisService {
trySetValue(() -> excelDataEntity.setExternalChannel(getCellValueAsString(row.getCell(tableHeaderList.get(14).getIndex())))); trySetValue(() -> excelDataEntity.setExternalChannel(getCellValueAsString(row.getCell(tableHeaderList.get(14).getIndex()))));
trySetValue(() -> excelDataEntity.setPayAccount(getCellValueAsString(row.getCell(tableHeaderList.get(15).getIndex())))); trySetValue(() -> excelDataEntity.setPayAccount(getCellValueAsString(row.getCell(tableHeaderList.get(15).getIndex()))));
trySetValue(() -> excelDataEntity.setRemark(getCellValueAsString(row.getCell(tableHeaderList.get(16).getIndex())))); trySetValue(() -> excelDataEntity.setRemark(getCellValueAsString(row.getCell(tableHeaderList.get(16).getIndex()))));
if(StringUtils.isNotEmpty(folderName)){
//切割文件名前面是中文的人名,使用正则表达式
trySetValue(() -> excelDataEntity.setPayUserName(trimChineseNameFromFilename(folderName)));
}
excelDataEntity.setFileId(fileId);
excelDataEntity.setUseFlag(true);
excelDataEntity.setCaseId(caseId);
return excelDataEntity; return excelDataEntity;
} }
public static String trimChineseNameFromFilename(String filename) {
// 正则表达式匹配连续的中文字符
Pattern pattern = Pattern.compile("[\u4e00-\u9fa5]+");
Matcher matcher = pattern.matcher(filename);
// 只获取第一组匹配的中文
if (matcher.find()) {
return matcher.group();
} else {
return "";
}
}
// 辅助方法:简化异常处理 // 辅助方法:简化异常处理
private void trySetValue(Runnable setter) { private void trySetValue(Runnable setter) {
try { try {
...@@ -229,7 +173,9 @@ public class AnalysisServiceImpl implements AnalysisService { ...@@ -229,7 +173,9 @@ public class AnalysisServiceImpl implements AnalysisService {
// 检查是否是Excel文件 // 检查是否是Excel文件
private boolean isExcelFile(String filename) { private boolean isExcelFile(String filename) {
if (filename == null) return false; if (filename == null) {
return false;
}
String lowerCaseFilename = filename.toLowerCase(); String lowerCaseFilename = filename.toLowerCase();
for (String extension : EXCEL_EXTENSIONS) { for (String extension : EXCEL_EXTENSIONS) {
if (lowerCaseFilename.endsWith(extension)) { if (lowerCaseFilename.endsWith(extension)) {
...@@ -239,7 +185,7 @@ public class AnalysisServiceImpl implements AnalysisService { ...@@ -239,7 +185,7 @@ public class AnalysisServiceImpl implements AnalysisService {
return false; return false;
} }
private void processSingleExcelFile(File excelFile, List<TableHeaderVO> tableHeaderList) throws IOException { private void processSingleExcelFile(File excelFile, List<TableHeaderVO> tableHeaderList, String folderName,String fileId,String caseId) throws IOException {
//读取第一个sheet //读取第一个sheet
Sheet sheet = getWorkbook(excelFile).getSheetAt(0); Sheet sheet = getWorkbook(excelFile).getSheetAt(0);
//获取sheet长度 //获取sheet长度
...@@ -250,84 +196,13 @@ public class AnalysisServiceImpl implements AnalysisService { ...@@ -250,84 +196,13 @@ public class AnalysisServiceImpl implements AnalysisService {
for (int i = 1; i < sheetLength; i++) { for (int i = 1; i < sheetLength; i++) {
Row row = sheet.getRow(i); Row row = sheet.getRow(i);
//处理excel数据 //处理excel数据
handleExcelData(tableHeaderList, row, excelData); ExcelDataEntity excelDataEntity = handleExcelData(tableHeaderList, row, folderName,fileId,caseId);
excelData.add(excelDataEntity);
} }
//保存excel数据 //保存excel数据
excelDataService.saveBatch(excelData); excelDataService.saveBatch(excelData);
} }
private void handleExcelData(List<TableHeaderVO> tableHeaderList, Row row, List<ExcelDataEntity> excelData) {
ExcelDataEntity excelDataEntity = new ExcelDataEntity();
try {
excelDataEntity.setType(getCellValueAsString(row.getCell(tableHeaderList.get(0).getIndex())));
} catch (Exception ignored) {
}
try {
excelDataEntity.setDirection(getCellValueAsString(row.getCell(tableHeaderList.get(1).getIndex())));
} catch (Exception ignored) {
}
try {
excelDataEntity.setTime(DateUtils.parseCompactDateTime(getCellValueAsString(row.getCell(tableHeaderList.get(2).getIndex()))));
} catch (Exception ignored) {
}
try {
excelDataEntity.setAmount(Double.parseDouble(getCellValueAsString(row.getCell(tableHeaderList.get(3).getIndex()))));
} catch (Exception ignored) {
}
try {
excelDataEntity.setSerial(getCellValueAsString(row.getCell(tableHeaderList.get(4).getIndex())));
} catch (Exception ignored) {
}
try {
excelDataEntity.setPayeeBankCard(getCellValueAsString(row.getCell(tableHeaderList.get(5).getIndex())));
} catch (Exception ignored) {
}
try {
excelDataEntity.setPayeeAccount(getCellValueAsString(row.getCell(tableHeaderList.get(6).getIndex())));
} catch (Exception ignored) {
}
try {
excelDataEntity.setPayeeMerchant(getCellValueAsString(row.getCell(tableHeaderList.get(7).getIndex())));
} catch (Exception ignored) {
}
try {
excelDataEntity.setPayeeName(getCellValueAsString(row.getCell(tableHeaderList.get(8).getIndex())));
} catch (Exception ignored) {
}
try {
excelDataEntity.setPaymentBankName(getCellValueAsString(row.getCell(tableHeaderList.get(9).getIndex())));
} catch (Exception ignored) {
}
try {
excelDataEntity.setPaymentBankCard(getCellValueAsString(row.getCell(tableHeaderList.get(10).getIndex())));
} catch (Exception ignored) {
}
try {
excelDataEntity.setPaymentAccount(getCellValueAsString(row.getCell(tableHeaderList.get(11).getIndex())));
} catch (Exception ignored) {
}
try {
excelDataEntity.setIp(getCellValueAsString(row.getCell(tableHeaderList.get(12).getIndex())));
} catch (Exception ignored) {
}
try {
excelDataEntity.setMac(getCellValueAsString(row.getCell(tableHeaderList.get(13).getIndex())));
} catch (Exception ignored) {
}
try {
excelDataEntity.setExternalChannel(getCellValueAsString(row.getCell(tableHeaderList.get(14).getIndex())));
} catch (Exception ignored) {
}
try {
excelDataEntity.setPayAccount(getCellValueAsString(row.getCell(tableHeaderList.get(15).getIndex())));
} catch (Exception ignored) {
}
try {
excelDataEntity.setRemark(getCellValueAsString(row.getCell(tableHeaderList.get(16).getIndex())));
} catch (Exception ignored) {
}
excelData.add(excelDataEntity);
}
// 根据文件类型创建Workbook // 根据文件类型创建Workbook
private Workbook getWorkbook(File excelFile) throws IOException { private Workbook getWorkbook(File excelFile) throws IOException {
...@@ -368,7 +243,6 @@ public class AnalysisServiceImpl implements AnalysisService { ...@@ -368,7 +243,6 @@ public class AnalysisServiceImpl implements AnalysisService {
} }
} }
// 将MultipartFile转换为File // 将MultipartFile转换为File
private File convertMultipartFileToFile(MultipartFile file) throws IOException { private File convertMultipartFileToFile(MultipartFile file) throws IOException {
File convFile = new File(System.getProperty("java.io.tmpdir") + "/" + file.getOriginalFilename()); File convFile = new File(System.getProperty("java.io.tmpdir") + "/" + file.getOriginalFilename());
......
...@@ -62,7 +62,7 @@ public class FileUploadServiceImpl extends ...@@ -62,7 +62,7 @@ public class FileUploadServiceImpl extends
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.save(bsFileUploadVO);
return bsFileUploadVO; return bsFileUploadVO;
} catch (IOException e) { } catch (IOException e) {
throw new BusinessException("文件上传失败"); throw new BusinessException("文件上传失败");
......
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