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)
@@ -103,7 +103,7 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
@Autowired @Autowired
private ILawsReplacedStandardService lawsReplacedStandardService; private ILawsReplacedStandardService lawsReplacedStandardService;
@Autowired @Autowired
private ILawsStandardFileService lawsStandardFileService; private ILawsStandardFileService lawsStandardFileService;
@Autowired @Autowired
private ILawsTreeNodeService lawsTreeNodeService; private ILawsTreeNodeService lawsTreeNodeService;
@Autowired @Autowired
@@ -145,7 +145,7 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
// 获取分页参数 // 获取分页参数
Integer pageNo = Integer.valueOf(parameterMap.get("pageNo").toString()); Integer pageNo = Integer.valueOf(parameterMap.get("pageNo").toString());
Integer pageSize = Integer.valueOf(parameterMap.get("pageSize").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); String selectField = getSelectField(fieldListSelect);
// 封装查询条件 // 封装查询条件
@@ -184,15 +184,15 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
int pageNo = 1; int pageNo = 1;
int pageSize = Integer.MAX_VALUE; 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 selectCondition = getSelectCondition(parameterMap, fieldListCondition);
String ids = (String)parameterMap.get(FieldCommon.SELECTIONS); String ids = (String) parameterMap.get(FieldCommon.SELECTIONS);
// 勾选导出 // 勾选导出
if (StrUtil.isNotBlank(ids)) { if (StrUtil.isNotBlank(ids)) {
List<String> list = Arrays.asList(ids.split(",")); List<String> list = Arrays.asList(ids.split(","));
selectCondition = selectCondition.replace("where"," and"); selectCondition = selectCondition.replace("where", " and");
String sql = "where id in ("; String sql = "where id in (";
StringBuilder sql2 = new StringBuilder(); StringBuilder sql2 = new StringBuilder();
list.forEach(id -> { list.forEach(id -> {
@@ -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) {
@@ -755,13 +757,13 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
String oldValue = ""; String oldValue = "";
if (!Objects.isNull(oldDataMap.get(key))) oldValue = (String) oldDataMap.get(key); 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()); key.equals(lawsTag.getDbFieldName())).collect(Collectors.toList());
if (CollectionUtils.isEmpty(lawsTagList)) { if (CollectionUtils.isEmpty(lawsTagList)) {
return; return;
} }
LawsTag lawsTag = lawsTagList.get(0); LawsTag lawsTag = lawsTagList.get(0);
// 对特殊值做处理(新旧值) // 对特殊值做处理(新旧值)
if (LawsFieldTypeEnum.FILE_UP.getValue().equals(lawsTag.getFieldShowType())) { 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); IPage<Map<String, Object>> commonPage = this.getExportData(parameterMap, headers);
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,72 +1186,13 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
zipOut.write(bytes, 0, bytes.length); zipOut.write(bytes, 0, bytes.length);
zipOut.closeEntry(); zipOut.closeEntry();
// 将文件和文件夹加入ZIP for (OSSFile file : files) {
for (Map.Entry<String, List<OSSFile>> entry : fileMap.entrySet()) { ZipEntry fileEntry = new ZipEntry(file.getFileRelativePath());
String folderName = entry.getKey(); zipOut.putNextEntry(fileEntry);
List<OSSFile> files = entry.getValue(); // 将文件取出存入流中
bytes = ossFileService.getMinioFileContent(file);
// 收集所有文件id zipOut.write(bytes, 0, bytes.length);
List<String> fileIds = files.stream() zipOut.closeEntry();
.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();
}
} }
zipOut.close(); zipOut.close();
} catch (Exception e) { } catch (Exception 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
*/ */
@@ -1794,7 +1734,7 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
if (FieldCommon.MY_COLLECTION.equals(nodeCode)) { if (FieldCommon.MY_COLLECTION.equals(nodeCode)) {
LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal(); LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
selectCondition.append(" and id in (select distinct standard_id from laws_standard_collection" + selectCondition.append(" and id in (select distinct standard_id from laws_standard_collection" +
" where collector_id = '"+ loginUser.getId() +"')"); " where collector_id = '" + loginUser.getId() + "')");
} else { } else {
selectCondition.append(" and id in (select distinct unique_relation_flag from laws_node_relation " + selectCondition.append(" and id in (select distinct unique_relation_flag from laws_node_relation " +
"where node_code like concat('" + nodeCode + "','%'))"); "where node_code like concat('" + nodeCode + "','%'))");
@@ -1873,7 +1813,7 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
} else if (LawsFieldTypeEnum.DATE_SINGLE.getValue().equals(fieldShowType)) { } else if (LawsFieldTypeEnum.DATE_SINGLE.getValue().equals(fieldShowType)) {
// 单日期查询 // 单日期查询
processDateSingleTypeCondition(selectCondition, key, value); processDateSingleTypeCondition(selectCondition, key, value);
}else if (LawsFieldTypeEnum.DATE_MANY.getValue().equals(fieldShowType)) { } else if (LawsFieldTypeEnum.DATE_MANY.getValue().equals(fieldShowType)) {
// 多日期查询 // 多日期查询
processDateManyTypeCondition(selectCondition, key, value); processDateManyTypeCondition(selectCondition, key, value);
} }
@@ -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);
List<OSSFile> standardFiles = fileMap.get(standardId);
List<String> filePathNames = new ArrayList<>();
if (standardFiles == null) {
continue;
}
// 为每个文件字段设置值
for (LawsTag tag : fileHeaders) { for (LawsTag tag : fileHeaders) {
String fileName = tag.getDbFieldTxt(); StringBuilder allFileName = new StringBuilder();
String key = standardNo + "-" + tag.getDbFieldName(); String key = standardId + "-" + tag.getDbFieldName();
List<OSSFile> lawsStandardFiles = groupMap.get(key); List<OSSFile> ossFilesByType = fileMapByFileType.get(key);
if (lawsStandardFiles == null) { if (ossFilesByType == null) {
continue; continue;
} }
StringBuilder allFileName = new StringBuilder(); for (OSSFile file : ossFilesByType) {
// 遍历找到的结果 String folderName = standardNo.replace("/", "") + "/";
if (lawsStandardFiles.size() == 1) { // 遍历找到的结果
String fileExtension = "." + FileUtil.extName(lawsStandardFiles.get(0).getFileName()); String currentFileName = file.getFileName();
allFileName = new StringBuilder(standardNo.replace("/", "") + "/" + fileName + fileExtension); String pathFileName = folderName + currentFileName;
} else { // 对文件名做处理
for (int i = 1; i <= lawsStandardFiles.size(); i++) { String fileExt = FileUtil.extName(currentFileName);
String fileExtension = "." + FileUtil.extName(lawsStandardFiles.get(0).getFileName()); String fileOriginName = currentFileName.replace("." + fileExt, "");
allFileName.append(standardNo.replace("/", "")).append("/").append(fileName).append(i).append(fileExtension).append(","); int count = 1;
// 去所有已加入的文件名中找有没有重复的
while (filePathNames.contains(pathFileName)) {
currentFileName = fileOriginName + "(" + count + ")." + fileExt;
pathFileName = folderName + currentFileName;
count++;
} }
// 去除最后一个, file.setFileName(currentFileName);
allFileName = new StringBuilder(allFileName.substring(0, allFileName.length() - 1)); 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()); record.put(tag.getDbFieldName() + FieldCommon._DICT_TEXT, allFileName.toString());
} }
} }
// 将List转成Map
return newFiles;
} }
} }