"src/main/java/git@gitlab.ruzhoukj.com:wujipeng/relation.git" did not exist on "02f71096577b2f4b87d790a009cb6b10c8d73100"
Commit 837109a0 authored by wujipeng's avatar wujipeng
Browse files

解析excel优化

parent 5b9c00a9
...@@ -53,7 +53,7 @@ public class ExcelDataEntity { ...@@ -53,7 +53,7 @@ public class ExcelDataEntity {
@TableField("payee_bank_card") @TableField("payee_bank_card")
private String payeeBankCard; private String payeeBankCard;
@ApiModelProperty("'收款方支付账号'") @ApiModelProperty("收款方支付账号")
@TableField("payee_account") @TableField("payee_account")
private String payeeAccount; private String payeeAccount;
......
package com.platform.service.impl; package com.platform.service.impl;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.platform.exception.BusinessException; import com.platform.exception.BusinessException;
import com.platform.model.entity.*; import com.platform.model.entity.*;
import com.platform.model.resp.Result; import com.platform.model.resp.Result;
...@@ -91,60 +92,77 @@ public class AnalysisServiceImpl implements AnalysisService { ...@@ -91,60 +92,77 @@ public class AnalysisServiceImpl implements AnalysisService {
tableHeaderList.add(TableHeaderVO.builder().name("支付帐号").index(29).build()); tableHeaderList.add(TableHeaderVO.builder().name("支付帐号").index(29).build());
tableHeaderList.add(TableHeaderVO.builder().name("备注").index(30).build()); tableHeaderList.add(TableHeaderVO.builder().name("备注").index(30).build());
try {
if (files.size() == 0) {
return Result.fail("上传的文件为空");
}
List<FileUploadVO> fileUploadList = Lists.newArrayList();
//错误数量 if (files.size() == 0) {
List<AnalysisVO> detail = Lists.newArrayList(); return Result.fail("上传的文件为空");
int errorCount = 0; }
int totalCount = 0; List<FileUploadVO> fileUploadList = Lists.newArrayList();
for (MultipartFile file : files) {
// 检查是否是Excel文件 //错误数量
if (isExcelFile(file.getOriginalFilename())) { List<AnalysisVO> detail = Lists.newArrayList();
FileUploadVO fileUploadVO = fileUploadService.uploadFile(file); int errorCount = 0;
File tempFile = convertMultipartFileToFile(file); int totalCount = 0;
processSingleExcelFile(tempFile, tableHeaderList, folderName, fileUploadVO.getId(), caseId); Map<String, String> groupedMap = Maps.newHashMap();
//文件上传 List<UserAccountEntity> userAccountList = Lists.newArrayList();
fileUploadList.add(fileUploadVO); for (MultipartFile file : files) {
totalCount++; // 检查是否是Excel文件
}else { if (isExcelFile(file.getOriginalFilename())) {
errorCount++; FileUploadVO fileUploadVO = fileUploadService.uploadFile(file);
totalCount++; File tempFile = null;
detail.add(AnalysisVO.builder() try {
.folderName(folderName) tempFile = convertMultipartFileToFile(file);
.fileName(file.getOriginalFilename()) } catch (IOException e) {
.errorReason("文件格式错误") throw new BusinessException("文件转换错误");
.build());
} }
} try {
if (StringUtils.isNotEmpty(folderName)) { processSingleExcelFile(tempFile, tableHeaderList, folderName, fileUploadVO.getId(), caseId, userAccountList, groupedMap);
String userName = trimChineseNameFromFilename(folderName); } catch (IOException e) {
Map<String, String> idCardInfo = IdCardUtils.parseIdCardInfo(folderName); throw new BusinessException("文件解析错误");
if (!ObjectUtils.isEmpty(idCardInfo)) {
userService.save(UserEntity.builder().userName(userName)
.age(Integer.parseInt(idCardInfo.get("age")))
.idCard(idCardInfo.get("idCard"))
.sex(idCardInfo.get("sex"))
.idType("身份证")
.caseId(caseId)
.build());
} }
//文件上传
fileUploadList.add(fileUploadVO);
totalCount++;
} else {
errorCount++;
totalCount++;
detail.add(AnalysisVO.builder()
.folderName(folderName)
.fileName(file.getOriginalFilename())
.errorReason("文件格式错误")
.build());
} }
if (errorCount == 0){ }
return Result.data(fileUploadList); //保存账户
}else { String userName = trimChineseNameFromFilename(folderName);
return Result.data(AnalysisVO.builder() groupedMap.forEach((paymentAccount, paymentBankName) ->
.folderCount(1) userAccountList.add(UserAccountEntity.builder()
.totalCount(totalCount) .userName(userName)
.errorCount(errorCount) .account(paymentBankName)
.detail(detail) .caseId(caseId)
.code(paymentAccount)
.build()));
userAccountService.saveUserBatch(userAccountList);
if (StringUtils.isNotEmpty(folderName)) {
Map<String, String> idCardInfo = IdCardUtils.parseIdCardInfo(folderName);
if (!ObjectUtils.isEmpty(idCardInfo)) {
userService.save(UserEntity.builder().userName(userName)
.age(Integer.parseInt(idCardInfo.get("age")))
.idCard(idCardInfo.get("idCard"))
.sex(idCardInfo.get("sex"))
.idType("身份证")
.caseId(caseId)
.build()); .build());
} }
} catch (Exception e) { }
throw new BusinessException(e.getMessage()); if (errorCount == 0) {
return Result.data(fileUploadList);
} else {
return Result.data(AnalysisVO.builder()
.folderCount(1)
.totalCount(totalCount)
.errorCount(errorCount)
.detail(detail)
.build());
} }
} }
...@@ -250,7 +268,7 @@ public class AnalysisServiceImpl implements AnalysisService { ...@@ -250,7 +268,7 @@ public class AnalysisServiceImpl implements AnalysisService {
return false; return false;
} }
private void processSingleExcelFile(File excelFile, List<TableHeaderVO> tableHeaderList, String folderName, String fileId, String caseId) throws IOException { private void processSingleExcelFile(File excelFile, List<TableHeaderVO> tableHeaderList, String folderName, String fileId, String caseId, List<UserAccountEntity> userAccountList, Map<String, String> groupedMap) throws IOException {
//读取第一个sheet //读取第一个sheet
Sheet sheet = getWorkbook(excelFile).getSheetAt(0); Sheet sheet = getWorkbook(excelFile).getSheetAt(0);
//获取sheet长度 //获取sheet长度
...@@ -266,30 +284,18 @@ public class AnalysisServiceImpl implements AnalysisService { ...@@ -266,30 +284,18 @@ public class AnalysisServiceImpl implements AnalysisService {
} }
if (StringUtils.isNotEmpty(folderName)) { if (StringUtils.isNotEmpty(folderName)) {
String userName = trimChineseNameFromFilename(folderName);
if (!excelData.isEmpty()) { if (!excelData.isEmpty()) {
//excelData转化成以paymentAccount为键paymentBankName为值的map //excelData转化成以paymentAccount为键paymentBankName为值的map
Map<String, String> groupedMap = excelData.stream().collect(Collectors.toMap(ExcelDataEntity::getPaymentAccount, ExcelDataEntity::getPaymentBankName)); for (ExcelDataEntity excelDatum : excelData) {
// List<String> paymentBankNames = excelData.stream().map(ExcelDataEntity::getPaymentBankName).distinct().collect(Collectors.toList()); if("出账".equals(excelDatum.getDirection())){
List<UserAccountEntity> userAccountList = Lists.newArrayList(); groupedMap.put(excelDatum.getPaymentAccount(), excelDatum.getPaymentBankName());
// for (String paymentBankName : paymentBankNames) { }else if("入账".equals(excelDatum.getDirection())){
// userAccountList.add(UserAccountEntity.builder() groupedMap.put(excelDatum.getPayeeAccount(), excelDatum.getPayeeBankCard());
// .userName(userName) }
// .account(paymentBankName) }
// .caseId(caseId)
// .build());
// }
groupedMap.forEach((paymentAccount, paymentBankName) ->
userAccountList.add(UserAccountEntity.builder()
.userName(userName)
.account(paymentBankName)
.caseId(caseId)
.code(paymentAccount)
.build()));
userAccountService.saveUserBatch(userAccountList);
} }
} }
//保存excel数据 //保存excel数据
excelDataService.saveBatch(excelData); excelDataService.saveBatch(excelData);
} }
......
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