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
@@ -103,7 +103,7 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
@Autowired
private ILawsReplacedStandardService lawsReplacedStandardService;
@Autowired
private ILawsStandardFileService lawsStandardFileService;
private ILawsStandardFileService lawsStandardFileService;
@Autowired
private ILawsTreeNodeService lawsTreeNodeService;
@Autowired
@@ -145,7 +145,7 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
// 获取分页参数
Integer pageNo = Integer.valueOf(parameterMap.get("pageNo").toString());
Integer pageSize = Integer.valueOf(parameterMap.get("pageSize").toString());
IPage<Map<String,Object>> page = new Page<>(pageNo, pageSize);
IPage<Map<String, Object>> page = new Page<>(pageNo, pageSize);
// 封装查询返回字段
String selectField = getSelectField(fieldListSelect);
// 封装查询条件
@@ -184,15 +184,15 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
int pageNo = 1;
int pageSize = Integer.MAX_VALUE;
IPage<Map<String,Object>> page = new Page<>(pageNo, pageSize);
IPage<Map<String, Object>> page = new Page<>(pageNo, pageSize);
// 封装查询条件
String selectCondition = getSelectCondition(parameterMap, fieldListCondition);
String ids = (String)parameterMap.get(FieldCommon.SELECTIONS);
String ids = (String) parameterMap.get(FieldCommon.SELECTIONS);
// 勾选导出
if (StrUtil.isNotBlank(ids)) {
List<String> list = Arrays.asList(ids.split(","));
selectCondition = selectCondition.replace("where"," and");
selectCondition = selectCondition.replace("where", " and");
String sql = "where id in (";
StringBuilder sql2 = new StringBuilder();
list.forEach(id -> {
@@ -589,6 +589,7 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
/**
* 保存新增编辑企标时的稽查内容
*
* @param inspectContents
* @param standardId
* @param standardNo
@@ -607,6 +608,7 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
esInspectContentService.saveBatch(inspectContents);
}
private List<EnterpriseStandardInspectContent> convertList(List<LinkedHashMap> originalList) {
List<EnterpriseStandardInspectContent> resultList = new ArrayList<>();
for (LinkedHashMap map : originalList) {
@@ -755,13 +757,13 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
String oldValue = "";
if (!Objects.isNull(oldDataMap.get(key))) oldValue = (String) oldDataMap.get(key);
List<LawsTag> lawsTagList = fieldList.stream().filter(lawsTag ->
List<LawsTag> lawsTagList = fieldList.stream().filter(lawsTag ->
key.equals(lawsTag.getDbFieldName())).collect(Collectors.toList());
if (CollectionUtils.isEmpty(lawsTagList)) {
return;
}
LawsTag lawsTag = lawsTagList.get(0);
// 对特殊值做处理(新旧值)
if (LawsFieldTypeEnum.FILE_UP.getValue().equals(lawsTag.getFieldShowType())) {
// 文件处理
@@ -1126,18 +1128,13 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
// 获取数据
IPage<Map<String, Object>> commonPage = this.getExportData(parameterMap, headers);
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());
Map<String, String> fileNameByTypeMap = fileHeaders.stream()
.collect(Collectors.toMap(LawsTag::getDbFieldName, LawsTag::getDbFieldTxt));
// 获取所有文件
Map<String, List<OSSFile>> fileMap = lawsStandardFileService.getAllFiles(standardNos);
// 设置文件字段
lawsStandardFileService.setRecordFile(records, standardNos, fileHeaders);
// 获取所有文件并且设置Excel中的字段
List<OSSFile> files = lawsStandardFileService.getAllFilesAndSetRecordFile(standardIds, records, fileHeaders);
// 创建Excel
ByteArrayOutputStream excelOutputStream = new ByteArrayOutputStream();
@@ -1189,72 +1186,13 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
zipOut.write(bytes, 0, bytes.length);
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) {
String fileNameByType = fileNameByTypeMap.get(file.getStandardFileType());
// 企标编号中有特殊字符"/" 需要把特殊字符删掉
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);
// 将文件取出存入流中
bytes = ossFileService.getMinioFileContent(ossFile);
zipOut.write(bytes, 0, bytes.length);
zipOut.closeEntry();
}
for (OSSFile file : files) {
ZipEntry fileEntry = new ZipEntry(file.getFileRelativePath());
zipOut.putNextEntry(fileEntry);
// 将文件取出存入流中
bytes = ossFileService.getMinioFileContent(file);
zipOut.write(bytes, 0, bytes.length);
zipOut.closeEntry();
}
zipOut.close();
} catch (Exception e) {
@@ -1265,6 +1203,7 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
/**
* 导入模板
*
* @param response
* @param tableName
*/
@@ -1547,6 +1486,7 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
/**
* 设置导入模板的第三行提示内容
*
* @param row3
* @param fieldList
*/
@@ -1794,7 +1734,7 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
if (FieldCommon.MY_COLLECTION.equals(nodeCode)) {
LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
selectCondition.append(" and id in (select distinct standard_id from laws_standard_collection" +
" where collector_id = '"+ loginUser.getId() +"')");
" where collector_id = '" + loginUser.getId() + "')");
} else {
selectCondition.append(" and id in (select distinct unique_relation_flag from laws_node_relation " +
"where node_code like concat('" + nodeCode + "','%'))");
@@ -1873,7 +1813,7 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
} else if (LawsFieldTypeEnum.DATE_SINGLE.getValue().equals(fieldShowType)) {
// 单日期查询
processDateSingleTypeCondition(selectCondition, key, value);
}else if (LawsFieldTypeEnum.DATE_MANY.getValue().equals(fieldShowType)) {
} else if (LawsFieldTypeEnum.DATE_MANY.getValue().equals(fieldShowType)) {
// 多日期查询
processDateManyTypeCondition(selectCondition, key, value);
}
@@ -18,16 +18,10 @@ public interface ILawsStandardFileService {
/**
* 根据标准编号集合获取文件列表
*
* @param standardNos
* @param standardIds
* @param records
* @param fileHeaders
* @return
*/
Map<String, List<OSSFile>> getAllFiles(List<String> standardNos);
/**
* 设置对象中的文件路径
* @param records
* @param standardNos
* @param fileHeaders
*/
void setRecordFile(List<Map<String, Object>> records, List<String> standardNos, List<LawsTag> fileHeaders);
List<OSSFile> getAllFilesAndSetRecordFile(List<String> standardIds, List<Map<String, Object>> records, List<LawsTag> fileHeaders);
}
@@ -12,6 +12,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@@ -29,56 +30,69 @@ public class LawsStandardFileServiceImpl implements ILawsStandardFileService {
private IOSSFileService ossFileService;
@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<>();
queryWrapper.in(OSSFile::getStandardNo, standardNos);
queryWrapper.in(OSSFile::getStandardId, standardIds);
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(
sf -> sf.getStandardNo() + "-" + sf.getStandardFileType() // Key: standardNo-fileType
o -> o.getStandardId() + "-" + o.getStandardFileType()
));
// 遍历每个记录
List<OSSFile> newFiles = new ArrayList<>();
for (Map<String, Object> record : records) {
String standardId = (String) record.get(FieldCommon.ID);
String standardNo = (String) record.get(FieldCommon.STANDARD_NUMBER);
List<OSSFile> standardFiles = fileMap.get(standardId);
List<String> filePathNames = new ArrayList<>();
if (standardFiles == null) {
continue;
}
// 为每个文件字段设置值
for (LawsTag tag : fileHeaders) {
String fileName = tag.getDbFieldTxt();
String key = standardNo + "-" + tag.getDbFieldName();
List<OSSFile> lawsStandardFiles = groupMap.get(key);
if (lawsStandardFiles == null) {
StringBuilder allFileName = new StringBuilder();
String key = standardId + "-" + tag.getDbFieldName();
List<OSSFile> ossFilesByType = fileMapByFileType.get(key);
if (ossFilesByType == null) {
continue;
}
StringBuilder allFileName = new StringBuilder();
// 遍历找到的结果
if (lawsStandardFiles.size() == 1) {
String fileExtension = "." + FileUtil.extName(lawsStandardFiles.get(0).getFileName());
allFileName = new StringBuilder(standardNo.replace("/", "") + "/" + fileName + fileExtension);
} else {
for (int i = 1; i <= lawsStandardFiles.size(); i++) {
String fileExtension = "." + FileUtil.extName(lawsStandardFiles.get(0).getFileName());
allFileName.append(standardNo.replace("/", "")).append("/").append(fileName).append(i).append(fileExtension).append(",");
for (OSSFile file : ossFilesByType) {
String folderName = standardNo.replace("/", "") + "/";
// 遍历找到的结果
String currentFileName = file.getFileName();
String pathFileName = folderName + currentFileName;
// 对文件名做处理
String fileExt = FileUtil.extName(currentFileName);
String fileOriginName = currentFileName.replace("." + fileExt, "");
int count = 1;
// 去所有已加入的文件名中找有没有重复的
while (filePathNames.contains(pathFileName)) {
currentFileName = fileOriginName + "(" + count + ")." + fileExt;
pathFileName = folderName + currentFileName;
count++;
}
// 去除最后一个,
allFileName = new StringBuilder(allFileName.substring(0, allFileName.length() - 1));
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));
// 最终设置
record.put(tag.getDbFieldName() + FieldCommon._DICT_TEXT, allFileName.toString());
}
}
// 将List转成Map
return newFiles;
}
}