diff --git a/laws-module-system/src/main/java/com/jero/modules/oss/entity/OSSFile.java b/laws-module-system/src/main/java/com/jero/modules/oss/entity/OSSFile.java index 95cbfcbe..5032c05c 100644 --- a/laws-module-system/src/main/java/com/jero/modules/oss/entity/OSSFile.java +++ b/laws-module-system/src/main/java/com/jero/modules/oss/entity/OSSFile.java @@ -60,11 +60,11 @@ public class OSSFile extends JeroEntity { private String sysOrgCode; /** - * 带文件下载之后展示的名字 + * 带文件下载的相对路径 */ @TableField(exist = false) - @ApiModelProperty(value = "带文件下载之后展示的名字") - private String fileDisplayName; + @ApiModelProperty(value = "带文件下载的相对路径") + private String fileRelativePath; @TableField(exist = false) diff --git a/laws-modules/src/main/java/com/jero/modules/laws/common/service/impl/LawsCommonServiceImpl.java b/laws-modules/src/main/java/com/jero/modules/laws/common/service/impl/LawsCommonServiceImpl.java index 05d12db8..36a9c09b 100644 --- a/laws-modules/src/main/java/com/jero/modules/laws/common/service/impl/LawsCommonServiceImpl.java +++ b/laws-modules/src/main/java/com/jero/modules/laws/common/service/impl/LawsCommonServiceImpl.java @@ -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> page = new Page<>(pageNo, pageSize); + IPage> 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> page = new Page<>(pageNo, pageSize); + IPage> 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 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 convertList(List originalList) { List 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 lawsTagList = fieldList.stream().filter(lawsTag -> + List 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> commonPage = this.getExportData(parameterMap, headers); List> records = commonPage.getRecords(); - + // 获取表头中的文件相关类型的对象集合 - List standardNos = records.stream().map(r -> r.get(FieldCommon.STANDARD_NUMBER).toString()).collect(Collectors.toList()); + List standardIds = records.stream().map(r -> r.get(FieldCommon.ID).toString()).collect(Collectors.toList()); List fileHeaders = headers.stream().filter(h -> h.getFieldShowType().equals(FieldShowTypeEnum.FILE_UPLOAD.getValue())).collect(Collectors.toList()); - Map fileNameByTypeMap = fileHeaders.stream() - .collect(Collectors.toMap(LawsTag::getDbFieldName, LawsTag::getDbFieldTxt)); - // 获取所有文件 - Map> fileMap = lawsStandardFileService.getAllFiles(standardNos); - - // 设置文件字段 - lawsStandardFileService.setRecordFile(records, standardNos, fileHeaders); + // 获取所有文件并且设置Excel中的字段 + List 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> entry : fileMap.entrySet()) { - String folderName = entry.getKey(); - List files = entry.getValue(); - - // 收集所有文件id - List fileIds = files.stream() - .map(OSSFile::getId) - .collect(Collectors.toList()); - - // 找到所有文件记录 - Map ossFileMap = ossFileService.listByIds(fileIds) - .stream() - .collect(Collectors.toMap(OSSFile::getId, Function.identity())); - - List filePathList = new ArrayList<>(); - Map 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 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); } diff --git a/laws-modules/src/main/java/com/jero/modules/laws/standard/service/ILawsStandardFileService.java b/laws-modules/src/main/java/com/jero/modules/laws/standard/service/ILawsStandardFileService.java index 83d18919..54ed9570 100644 --- a/laws-modules/src/main/java/com/jero/modules/laws/standard/service/ILawsStandardFileService.java +++ b/laws-modules/src/main/java/com/jero/modules/laws/standard/service/ILawsStandardFileService.java @@ -18,16 +18,10 @@ public interface ILawsStandardFileService { /** * 根据标准编号集合获取文件列表 * - * @param standardNos + * @param standardIds + * @param records + * @param fileHeaders * @return */ - Map> getAllFiles(List standardNos); - - /** - * 设置对象中的文件路径 - * @param records - * @param standardNos - * @param fileHeaders - */ - void setRecordFile(List> records, List standardNos, List fileHeaders); + List getAllFilesAndSetRecordFile(List standardIds, List> records, List fileHeaders); } diff --git a/laws-modules/src/main/java/com/jero/modules/laws/standard/service/impl/LawsStandardFileServiceImpl.java b/laws-modules/src/main/java/com/jero/modules/laws/standard/service/impl/LawsStandardFileServiceImpl.java index cf906be3..dc064515 100644 --- a/laws-modules/src/main/java/com/jero/modules/laws/standard/service/impl/LawsStandardFileServiceImpl.java +++ b/laws-modules/src/main/java/com/jero/modules/laws/standard/service/impl/LawsStandardFileServiceImpl.java @@ -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> getAllFiles(List standardNos) { + public List getAllFilesAndSetRecordFile(List standardIds, List> records, List fileHeaders) { // 找到所有相关文件 LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); - queryWrapper.in(OSSFile::getStandardNo, standardNos); + queryWrapper.in(OSSFile::getStandardId, standardIds); List list = ossFileService.list(queryWrapper); - // 将List转成Map - return list.stream() - .collect(Collectors.groupingBy(OSSFile::getStandardNo)); - } - - @Override - public void setRecordFile(List> records, List standardNos, List fileHeaders) { - // 找到所有相关文件 - LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); - queryWrapper.in(OSSFile::getStandardNo, standardNos); - List list = ossFileService.list(queryWrapper); // 按照标准编号和文件类型分组 - Map> groupMap = list.stream() + Map> fileMap = list.stream() + .collect(Collectors.groupingBy(OSSFile::getStandardId)); + + // 按照标准编号和文件类型分组 + Map> fileMapByFileType = list.stream() .collect(Collectors.groupingBy( - sf -> sf.getStandardNo() + "-" + sf.getStandardFileType() // Key: standardNo-fileType + o -> o.getStandardId() + "-" + o.getStandardFileType() )); // 遍历每个记录 + List newFiles = new ArrayList<>(); for (Map record : records) { + String standardId = (String) record.get(FieldCommon.ID); String standardNo = (String) record.get(FieldCommon.STANDARD_NUMBER); + List standardFiles = fileMap.get(standardId); + List filePathNames = new ArrayList<>(); + if (standardFiles == null) { + continue; + } + // 为每个文件字段设置值 for (LawsTag tag : fileHeaders) { - String fileName = tag.getDbFieldTxt(); - String key = standardNo + "-" + tag.getDbFieldName(); - List lawsStandardFiles = groupMap.get(key); - if (lawsStandardFiles == null) { + StringBuilder allFileName = new StringBuilder(); + String key = standardId + "-" + tag.getDbFieldName(); + List 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; } }