perf: 带文件导出代码优化

导出接口速度提升
导出文件名保留原名
This commit is contained in:
2023-09-28 14:06:58 +08:00
parent ddf4484753
commit 0c63b62788
4 changed files with 75 additions and 127 deletions
@@ -60,11 +60,11 @@ public class OSSFile extends JeroEntity {
private String sysOrgCode; private String sysOrgCode;
/** /**
* 带文件下载之后展示的名字 * 带文件下载的相对路径
*/ */
@TableField(exist = false) @TableField(exist = false)
@ApiModelProperty(value = "带文件下载之后展示的名字") @ApiModelProperty(value = "带文件下载的相对路径")
private String fileDisplayName; private String fileRelativePath;
@TableField(exist = false) @TableField(exist = false)
@@ -589,6 +589,7 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
/** /**
* 保存新增编辑企标时的稽查内容 * 保存新增编辑企标时的稽查内容
*
* @param inspectContents * @param inspectContents
* @param standardId * @param standardId
* @param standardNo * @param standardNo
@@ -607,6 +608,7 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
esInspectContentService.saveBatch(inspectContents); esInspectContentService.saveBatch(inspectContents);
} }
private List<EnterpriseStandardInspectContent> convertList(List<LinkedHashMap> originalList) { private List<EnterpriseStandardInspectContent> convertList(List<LinkedHashMap> originalList) {
List<EnterpriseStandardInspectContent> resultList = new ArrayList<>(); List<EnterpriseStandardInspectContent> resultList = new ArrayList<>();
for (LinkedHashMap map : originalList) { for (LinkedHashMap map : originalList) {
@@ -1128,16 +1130,11 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
List<Map<String, Object>> records = commonPage.getRecords(); List<Map<String, Object>> records = commonPage.getRecords();
// 获取表头中的文件相关类型的对象集合 // 获取表头中的文件相关类型的对象集合
List<String> standardNos = records.stream().map(r -> r.get(FieldCommon.STANDARD_NUMBER).toString()).collect(Collectors.toList()); List<String> standardIds = records.stream().map(r -> r.get(FieldCommon.ID).toString()).collect(Collectors.toList());
List<LawsTag> fileHeaders = headers.stream().filter(h -> h.getFieldShowType().equals(FieldShowTypeEnum.FILE_UPLOAD.getValue())).collect(Collectors.toList()); List<LawsTag> fileHeaders = headers.stream().filter(h -> h.getFieldShowType().equals(FieldShowTypeEnum.FILE_UPLOAD.getValue())).collect(Collectors.toList());
Map<String, String> fileNameByTypeMap = fileHeaders.stream()
.collect(Collectors.toMap(LawsTag::getDbFieldName, LawsTag::getDbFieldTxt));
// 获取所有文件 // 获取所有文件并且设置Excel中的字段
Map<String, List<OSSFile>> fileMap = lawsStandardFileService.getAllFiles(standardNos); List<OSSFile> files = lawsStandardFileService.getAllFilesAndSetRecordFile(standardIds, records, fileHeaders);
// 设置文件字段
lawsStandardFileService.setRecordFile(records, standardNos, fileHeaders);
// 创建Excel // 创建Excel
ByteArrayOutputStream excelOutputStream = new ByteArrayOutputStream(); ByteArrayOutputStream excelOutputStream = new ByteArrayOutputStream();
@@ -1189,73 +1186,14 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
zipOut.write(bytes, 0, bytes.length); zipOut.write(bytes, 0, bytes.length);
zipOut.closeEntry(); zipOut.closeEntry();
// 将文件和文件夹加入ZIP
for (Map.Entry<String, List<OSSFile>> entry : fileMap.entrySet()) {
String folderName = entry.getKey();
List<OSSFile> files = entry.getValue();
// 收集所有文件id
List<String> fileIds = files.stream()
.map(OSSFile::getId)
.collect(Collectors.toList());
// 找到所有文件记录
Map<String, OSSFile> ossFileMap = ossFileService.listByIds(fileIds)
.stream()
.collect(Collectors.toMap(OSSFile::getId, Function.identity()));
List<String> filePathList = new ArrayList<>();
Map<String, Integer> nameCountMap = new HashMap<>();
for (OSSFile file : files) { for (OSSFile file : files) {
String fileNameByType = fileNameByTypeMap.get(file.getStandardFileType()); ZipEntry fileEntry = new ZipEntry(file.getFileRelativePath());
// 企标编号中有特殊字符"/" 需要把特殊字符删掉
String baseFilePath = folderName.replaceAll("/", "") + "/" + fileNameByType;
String currentFilePath = baseFilePath;
if (filePathList.contains(currentFilePath)) {
int count = nameCountMap.getOrDefault(baseFilePath, 1);
// 更新之前的名字
int index = filePathList.indexOf(currentFilePath);
filePathList.set(index, baseFilePath + count);
// 更新当前名字
count++;
currentFilePath = baseFilePath + count;
nameCountMap.put(baseFilePath, count + 1);
} else if (nameCountMap.containsKey(baseFilePath)) {
int count = nameCountMap.get(baseFilePath);
currentFilePath = baseFilePath + count;
nameCountMap.put(baseFilePath, count + 1);
}
filePathList.add(currentFilePath);
}
Iterator<String> iterator = filePathList.iterator();
for (OSSFile file : files) {
if (iterator.hasNext()) {
String updatedFilePath = iterator.next();
file.setFileDisplayName(updatedFilePath);
}
}
for (OSSFile file : files) {
OSSFile ossFile = ossFileMap.get(file.getId());
String fileExtension = FileUtil.extName(ossFile.getFileName());
String filePath = file.getFileDisplayName() + "." + fileExtension;
ZipEntry fileEntry = new ZipEntry(filePath);
zipOut.putNextEntry(fileEntry); zipOut.putNextEntry(fileEntry);
// 将文件取出存入流中 // 将文件取出存入流中
bytes = ossFileService.getMinioFileContent(ossFile); bytes = ossFileService.getMinioFileContent(file);
zipOut.write(bytes, 0, bytes.length); zipOut.write(bytes, 0, bytes.length);
zipOut.closeEntry(); zipOut.closeEntry();
} }
}
zipOut.close(); zipOut.close();
} catch (Exception e) { } catch (Exception e) {
log.error("导出zip异常", e); log.error("导出zip异常", e);
@@ -1265,6 +1203,7 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
/** /**
* 导入模板 * 导入模板
*
* @param response * @param response
* @param tableName * @param tableName
*/ */
@@ -1547,6 +1486,7 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
/** /**
* 设置导入模板的第三行提示内容 * 设置导入模板的第三行提示内容
*
* @param row3 * @param row3
* @param fieldList * @param fieldList
*/ */
@@ -18,16 +18,10 @@ public interface ILawsStandardFileService {
/** /**
* 根据标准编号集合获取文件列表 * 根据标准编号集合获取文件列表
* *
* @param standardNos * @param standardIds
* @param records
* @param fileHeaders
* @return * @return
*/ */
Map<String, List<OSSFile>> getAllFiles(List<String> standardNos); List<OSSFile> getAllFilesAndSetRecordFile(List<String> standardIds, List<Map<String, Object>> records, List<LawsTag> fileHeaders);
/**
* 设置对象中的文件路径
* @param records
* @param standardNos
* @param fileHeaders
*/
void setRecordFile(List<Map<String, Object>> records, List<String> standardNos, List<LawsTag> fileHeaders);
} }
@@ -12,6 +12,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@@ -29,56 +30,69 @@ public class LawsStandardFileServiceImpl implements ILawsStandardFileService {
private IOSSFileService ossFileService; private IOSSFileService ossFileService;
@Override @Override
public Map<String, List<OSSFile>> getAllFiles(List<String> standardNos) { public List<OSSFile> getAllFilesAndSetRecordFile(List<String> standardIds, List<Map<String, Object>> records, List<LawsTag> fileHeaders) {
// 找到所有相关文件 // 找到所有相关文件
LambdaQueryWrapper<OSSFile> queryWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<OSSFile> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.in(OSSFile::getStandardNo, standardNos); queryWrapper.in(OSSFile::getStandardId, standardIds);
List<OSSFile> list = ossFileService.list(queryWrapper); List<OSSFile> list = ossFileService.list(queryWrapper);
// 将List转成Map
return list.stream()
.collect(Collectors.groupingBy(OSSFile::getStandardNo));
}
@Override
public void setRecordFile(List<Map<String, Object>> records, List<String> standardNos, List<LawsTag> fileHeaders) {
// 找到所有相关文件
LambdaQueryWrapper<OSSFile> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.in(OSSFile::getStandardNo, standardNos);
List<OSSFile> list = ossFileService.list(queryWrapper);
// 按照标准编号和文件类型分组 // 按照标准编号和文件类型分组
Map<String, List<OSSFile>> groupMap = list.stream() Map<String, List<OSSFile>> fileMap = list.stream()
.collect(Collectors.groupingBy(OSSFile::getStandardId));
// 按照标准编号和文件类型分组
Map<String, List<OSSFile>> fileMapByFileType = list.stream()
.collect(Collectors.groupingBy( .collect(Collectors.groupingBy(
sf -> sf.getStandardNo() + "-" + sf.getStandardFileType() // Key: standardNo-fileType o -> o.getStandardId() + "-" + o.getStandardFileType()
)); ));
// 遍历每个记录 // 遍历每个记录
List<OSSFile> newFiles = new ArrayList<>();
for (Map<String, Object> record : records) { for (Map<String, Object> record : records) {
String standardId = (String) record.get(FieldCommon.ID);
String standardNo = (String) record.get(FieldCommon.STANDARD_NUMBER); String standardNo = (String) record.get(FieldCommon.STANDARD_NUMBER);
for (LawsTag tag : fileHeaders) { List<OSSFile> standardFiles = fileMap.get(standardId);
String fileName = tag.getDbFieldTxt(); List<String> filePathNames = new ArrayList<>();
String key = standardNo + "-" + tag.getDbFieldName(); if (standardFiles == null) {
List<OSSFile> lawsStandardFiles = groupMap.get(key);
if (lawsStandardFiles == null) {
continue; continue;
} }
// 为每个文件字段设置值
for (LawsTag tag : fileHeaders) {
StringBuilder allFileName = new StringBuilder(); StringBuilder allFileName = new StringBuilder();
String key = standardId + "-" + tag.getDbFieldName();
List<OSSFile> ossFilesByType = fileMapByFileType.get(key);
if (ossFilesByType == null) {
continue;
}
for (OSSFile file : ossFilesByType) {
String folderName = standardNo.replace("/", "") + "/";
// 遍历找到的结果 // 遍历找到的结果
if (lawsStandardFiles.size() == 1) { String currentFileName = file.getFileName();
String fileExtension = "." + FileUtil.extName(lawsStandardFiles.get(0).getFileName()); String pathFileName = folderName + currentFileName;
allFileName = new StringBuilder(standardNo.replace("/", "") + "/" + fileName + fileExtension); // 对文件名做处理
} else { String fileExt = FileUtil.extName(currentFileName);
for (int i = 1; i <= lawsStandardFiles.size(); i++) { String fileOriginName = currentFileName.replace("." + fileExt, "");
String fileExtension = "." + FileUtil.extName(lawsStandardFiles.get(0).getFileName()); int count = 1;
allFileName.append(standardNo.replace("/", "")).append("/").append(fileName).append(i).append(fileExtension).append(","); // 去所有已加入的文件名中找有没有重复的
while (filePathNames.contains(pathFileName)) {
currentFileName = fileOriginName + "(" + count + ")." + fileExt;
pathFileName = folderName + currentFileName;
count++;
}
file.setFileName(currentFileName);
file.setFileRelativePath(pathFileName);
filePathNames.add(pathFileName);
newFiles.add(file);
allFileName.append(pathFileName).append(",");
} }
// 去除最后一个, // 去除最后一个,
allFileName = new StringBuilder(allFileName.substring(0, allFileName.length() - 1)); allFileName = new StringBuilder(allFileName.substring(0, allFileName.length() - 1));
}
// 最终设置 // 最终设置
record.put(tag.getDbFieldName() + FieldCommon._DICT_TEXT, allFileName.toString()); record.put(tag.getDbFieldName() + FieldCommon._DICT_TEXT, allFileName.toString());
} }
} }
// 将List转成Map
return newFiles;
} }
} }