Commit ffe036a6 authored by zhuxiang's avatar zhuxiang
Browse files

Merge remote-tracking branch 'origin/dev' into dev

parents 62df0303 029e3e71
......@@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
/**
......@@ -29,7 +30,7 @@ public class AnalysisController {
@PostMapping("/canvas")
public Result<?> canvas(@RequestParam(value = "files") List<MultipartFile> files,
@RequestParam(value = "caseId") int caseId,
@RequestParam(value = "folderName", required = false) String folderName) {
@RequestParam(value = "folderName", required = false) String folderName) throws IOException {
return analysisService.canvas(files,caseId,folderName);
}
......
package com.platform.model.vo;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
......@@ -17,6 +18,7 @@ import java.util.List;
@AllArgsConstructor
@NoArgsConstructor
@Builder
@JsonInclude(JsonInclude.Include.NON_NULL)
public class AnalysisVO {
@ApiModelProperty("文件夹数")
......
......@@ -4,6 +4,7 @@ import com.platform.model.resp.Result;
import com.platform.model.vo.TableHeaderVO;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List;
/**
......@@ -19,7 +20,7 @@ public interface AnalysisService {
* @param files
* @return
*/
Result<?> canvas(List<MultipartFile> files,int caseId,String folderName);
Result<?> canvas(List<MultipartFile> files,int caseId,String folderName) throws IOException;
/**
* 读取模板文件
......
......@@ -61,7 +61,34 @@ public class AnalysisServiceImpl implements AnalysisService {
@Override
@Transactional
public Result<?> canvas(List<MultipartFile> files, int caseId, String folderName) {
public Result<?> canvas(List<MultipartFile> files, int caseId, String folderName) throws IOException {
//检查文件
List<AnalysisVO> detail = Lists.newArrayList();
int errorCount = 0;
int totalCount = 0;
for (MultipartFile file : files) {
if (isExcelFile(file.getOriginalFilename())) {
File tempFile = convertMultipartFileToFile(file);
Workbook workbook = getWorkbook(tempFile);
if (ObjectUtils.isEmpty(workbook)) {
errorCount++;
detail.add(AnalysisVO.builder().fileName(file.getOriginalFilename()).errorReason("文件格式错误").build());
}
totalCount++;
} else {
errorCount++;
totalCount++;
detail.add(AnalysisVO.builder().fileName(file.getOriginalFilename()).errorReason("文件格式错误").build());
}
}
if (errorCount > 0) {
return Result.data(400, AnalysisVO.builder()
.detail(detail)
.totalCount(totalCount)
.errorCount(errorCount)
.build(), "部分文件格式错误");
}
//查询模板
ExcelTemplateEntity ExcelTemplate = excelTemplateService.getMaxId();
List<TableHeaderVO> tableHeaderList;
......@@ -94,10 +121,6 @@ public class AnalysisServiceImpl implements AnalysisService {
return Result.fail("上传的文件为空");
}
List<FileUploadVO> fileUploadList = Lists.newArrayList();
//错误数量
List<AnalysisVO> detail = Lists.newArrayList();
int errorCount = 0;
int totalCount = 0;
Map<String, String> groupedMap = Maps.newHashMap();
List<UserAccountEntity> userAccountList = Lists.newArrayList();
List<ExcelDataEntity> excelData = Lists.newArrayList();
......@@ -111,18 +134,11 @@ public class AnalysisServiceImpl implements AnalysisService {
} catch (IOException e) {
throw new BusinessException("文件转换错误");
}
try {
processSingleExcelFile(tempFile, tableHeaderList, folderName, fileUploadVO.getId(), caseId, groupedMap, excelData);
} catch (IOException e) {
throw new BusinessException("文件解析错误");
}
boolean b = processSingleExcelFile(tempFile, tableHeaderList, folderName, fileUploadVO.getId(), caseId, groupedMap, excelData);
if (b) {
//文件上传
fileUploadList.add(fileUploadVO);
totalCount++;
} else {
errorCount++;
totalCount++;
detail.add(AnalysisVO.builder().folderName(folderName).fileName(file.getOriginalFilename()).errorReason("文件格式错误").build());
}
}
}
//把tableHeaderList中filter为true索引组成一个数字类型的集合
......@@ -229,11 +245,7 @@ public class AnalysisServiceImpl implements AnalysisService {
.caseId(caseId).build());
}
}
if (errorCount == 0) {
return Result.data(fileUploadList);
} else {
return Result.data(AnalysisVO.builder().folderCount(1).totalCount(totalCount).errorCount(errorCount).detail(detail).build());
}
}
private void deduplicationData(Map<String, ExcelDataEntity> excelDataMap, List<ExcelDataEntity> excelData, List<Integer> filterIndex) {
......@@ -401,9 +413,13 @@ public class AnalysisServiceImpl implements AnalysisService {
return false;
}
private void processSingleExcelFile(File excelFile, List<TableHeaderVO> tableHeaderList, String folderName, String fileId, int caseId, Map<String, String> groupedMap, List<ExcelDataEntity> excelData) throws IOException {
private boolean processSingleExcelFile(File excelFile, List<TableHeaderVO> tableHeaderList, String folderName, String fileId, int caseId, Map<String, String> groupedMap, List<ExcelDataEntity> excelData) throws IOException {
Workbook workbook = getWorkbook(excelFile);
if (ObjectUtils.isEmpty(workbook)) {
return false;
}
//读取第一个sheet
Sheet sheet = getWorkbook(excelFile).getSheetAt(0);
Sheet sheet = workbook.getSheetAt(0);
//获取sheet长度
int sheetLength = sheet.getLastRowNum();
for (int i = 1; i < sheetLength; i++) {
......@@ -424,24 +440,25 @@ public class AnalysisServiceImpl implements AnalysisService {
}
}
}
return true;
}
// 根据文件类型创建Workbook
private Workbook getWorkbook(File excelFile) throws IOException {
private Workbook getWorkbook(File excelFile) {
String fileName = excelFile.getName().toLowerCase();
if (fileName.endsWith(".xlsx")) {
try {
return new XSSFWorkbook(new FileInputStream(excelFile));
} catch (Exception e) {
return new HSSFWorkbook(new FileInputStream(excelFile));
return null;
}
} else if (fileName.endsWith(".xls")) {
try {
return new HSSFWorkbook(new FileInputStream(excelFile));
} catch (Exception e) {
return new XSSFWorkbook(new FileInputStream(excelFile));
return null;
}
} else {
throw new IllegalArgumentException("不支持的Excel文件格式");
......
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