认证-上报库-查看-导出-校验文件内容并重命名bug58358

This commit is contained in:
liyawei
2022-08-25 14:50:31 +08:00
parent 9563e6e93a
commit a2646f8029
@@ -35,6 +35,7 @@ import com.jero.modules.project.util.WordUtil;
import com.jero.modules.split.common.ReadExcel;
import com.jero.modules.system.entity.SysDictItem;
import com.jero.modules.system.service.ISysDictItemService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.util.HSSFColor;
@@ -762,21 +763,21 @@ public class ParamsReportDetailEOServiceImpl extends ServiceImpl<ParamsReportDet
nowFile.mkdirs();
String fileName = fileOriName + ".docx";
// 查询导出数据
List<Map> allParamsInfoList = queryForExportCustom(paramsReportDetailVO);
Map<String, String> params = allParamsInfoList.get(0);
List<Map<String, Object>> imgListAll = (List<Map<String, Object>>) allParamsInfoList.get(1).get("imgListAll");
String templateUrl = "";
ParamsExportTemplateEO paramsExportTemplateEO = paramsExportTemplateEOService.getById(paramsReportDetailVO.getExportTemplateId());
List<OSSFile> ossFiles = ossFileService.getFileInfosByConnectId(paramsExportTemplateEO.getFileConnectId());
if (CollectionUtil.isNotEmpty(ossFiles)) {
templateUrl = ossFiles.get(0).getUrl();
}
// 替换模板中的占位符
try {
// 查询导出数据
List<Map> allParamsInfoList = queryForExportCustom(paramsReportDetailVO);
Map<String, String> params = allParamsInfoList.get(0);
List<Map<String, Object>> imgListAll = (List<Map<String, Object>>) allParamsInfoList.get(1).get("imgListAll");
String templateUrl = "";
ParamsExportTemplateEO paramsExportTemplateEO = paramsExportTemplateEOService.getById(paramsReportDetailVO.getExportTemplateId());
List<OSSFile> ossFiles = ossFileService.getFileInfosByConnectId(paramsExportTemplateEO.getFileConnectId());
if (CollectionUtil.isNotEmpty(ossFiles)) {
templateUrl = ossFiles.get(0).getUrl();
}
// 拿取模板
String repFileName = fileName.replaceAll("/","_");
wordOS = new FileOutputStream(fileNowPath + File.separator + repFileName);
@@ -788,16 +789,36 @@ public class ParamsReportDetailEOServiceImpl extends ServiceImpl<ParamsReportDet
List<Map<String, Object>> fileListAll = (List<Map<String, Object>>) allParamsInfoList.get(2).get("fileListAll");
if (fileListAll != null && !fileListAll.isEmpty()) {
Map<String,String> paramsAfterFilter = WordUtil.filterParams(wordMLPackage, params);
Map<String,String> paramsAfterFilter = WordUtil.filterParams(wordMLPackage, params); // 过滤出需要导出的参数项
if (CollectionUtil.isNotEmpty(paramsAfterFilter)) {
List<String> nioNumberList = paramsAfterFilter.keySet().stream().collect(Collectors.toList());
List<OSSFile> allRelevFileList = new ArrayList<>();
// 判断文件是否重复
for (Map<String, Object> fileMap : fileListAll) {
for (Map.Entry map : fileMap.entrySet()) {
String key = (String) map.getKey();
List<OSSFile> fileList = (List<OSSFile>) map.getValue();
if (nioNumberList.contains(key)) {
allRelevFileList.addAll(fileList);
String newFileName = isRepeat(allRelevFileList, fileList.get(0));
if (!"".equals(newFileName) && !"old".equals(newFileName)) { // 文件名称重复 且文件内容不同
String configDataStr = params.get(key);
if (StringUtils.isNotEmpty(configDataStr)) {
if (configDataStr.contains("#")) {
String newConfigDataStr = configDataStr.substring(0, configDataStr.lastIndexOf("#")) + newFileName;
params.put(key, newConfigDataStr);
} else {
params.put(key, newFileName);
}
}
fileList.get(0).setFileName(newFileName);
allRelevFileList.addAll(fileList);
} else if ("".equals(newFileName)) { // 文件名称不重复
allRelevFileList.addAll(fileList);
}
}
}
}
@@ -893,21 +914,21 @@ public class ParamsReportDetailEOServiceImpl extends ServiceImpl<ParamsReportDet
nowFile.mkdirs();
String fileName = fileOriName + ".xlsx";
// 查询导出数据
List<Map> allParamsInfoList = queryForExportCustom(paramsReportDetailVO);
HashMap<String, String> params = (HashMap<String, String>) allParamsInfoList.get(0);
List<Map<String, Object>> imgListAll = (List<Map<String, Object>>) allParamsInfoList.get(1).get("imgListAll");
String templateUrl = "";
ParamsExportTemplateEO paramsExportTemplateEO = paramsExportTemplateEOService.getById(paramsReportDetailVO.getExportTemplateId());
List<OSSFile> ossFiles = ossFileService.getFileInfosByConnectId(paramsExportTemplateEO.getFileConnectId());
if (CollectionUtil.isNotEmpty(ossFiles)) {
templateUrl = ossFiles.get(0).getUrl();
}
// 替换模板中的占位符
try {
// 查询导出数据
List<Map> allParamsInfoList = queryForExportCustom(paramsReportDetailVO);
HashMap<String, String> params = (HashMap<String, String>) allParamsInfoList.get(0);
List<Map<String, Object>> imgListAll = (List<Map<String, Object>>) allParamsInfoList.get(1).get("imgListAll");
String templateUrl = "";
ParamsExportTemplateEO paramsExportTemplateEO = paramsExportTemplateEOService.getById(paramsReportDetailVO.getExportTemplateId());
List<OSSFile> ossFiles = ossFileService.getFileInfosByConnectId(paramsExportTemplateEO.getFileConnectId());
if (CollectionUtil.isNotEmpty(ossFiles)) {
templateUrl = ossFiles.get(0).getUrl();
}
// 拿取模板
String repFileName = fileName.replaceAll("/","_");
wordOS = new FileOutputStream(fileNowPath + File.separator + repFileName);
@@ -931,7 +952,25 @@ public class ParamsReportDetailEOServiceImpl extends ServiceImpl<ParamsReportDet
String key = (String) map.getKey();
List<OSSFile> fileList = (List<OSSFile>) map.getValue();
if (nioNumberList.contains(key)) {
allRelevFileList.addAll(fileList);
String newFileName = isRepeat(allRelevFileList, fileList.get(0));
if (!"".equals(newFileName) && !"old".equals(newFileName)) { // 文件名称重复 且文件内容不同
String configDataStr = params.get(key);
if (StringUtils.isNotEmpty(configDataStr)) {
if (configDataStr.contains("#")) {
String newConfigDataStr = configDataStr.substring(0, configDataStr.lastIndexOf("#")) + newFileName;
params.put(key, newConfigDataStr);
} else {
params.put(key, newFileName);
}
}
fileList.get(0).setFileName(newFileName);
allRelevFileList.addAll(fileList);
} else if ("".equals(newFileName)) { // 文件名称不重复
allRelevFileList.addAll(fileList);
}
}
}
}
@@ -1064,7 +1103,7 @@ public class ParamsReportDetailEOServiceImpl extends ServiceImpl<ParamsReportDet
bis.close(); // 先开后关
}
private List<Map<String, Object>> queryForExportNormal(ParamsReportDetailVO paramsReportDetailVO) {
private List<Map<String, Object>> queryForExportNormal(ParamsReportDetailVO paramsReportDetailVO) throws IOException {
List<String> idList = new ArrayList<>();
if (StringUtils.isNotEmpty(paramsReportDetailVO.getIds())) {
idList = Arrays.asList(paramsReportDetailVO.getIds().split(","));
@@ -1074,6 +1113,7 @@ public class ParamsReportDetailEOServiceImpl extends ServiceImpl<ParamsReportDet
List<ParamsReportConfigEO> paramsConfigEOList = paramsReportConfigEOService.queryList(paramsReportDetailVO.getParamsManifestId()); // 查询所有配置列
List<OSSFile> fileListAll = new ArrayList<>(); // 仅用于校验文件是否重复
// 查询配置列数据
for (Map<String, Object> record1 : dataList) {
@@ -1086,7 +1126,7 @@ public class ParamsReportDetailEOServiceImpl extends ServiceImpl<ParamsReportDet
//不合并
if ("1".equals(paramsReportDetailVO.getCombineFlag())) {
paramsConfigEOList = paramsConfigEOList.stream().filter(e->configIdList.contains(e.getId())).collect(Collectors.toList()); // 过滤出要导出的配置列
paramsConfigEOList.forEach(paramsConfigEO -> { // 参数配置
for (ParamsConfigEO paramsConfigEO : paramsConfigEOList) { // 参数配置
if (ControlTypeEnum.Title.getValue().equals(controlType)) {
record1.put(paramsConfigEO.getId(), titleDefaultValue);
@@ -1103,9 +1143,24 @@ public class ParamsReportDetailEOServiceImpl extends ServiceImpl<ParamsReportDet
if (StringUtils.isNotEmpty(paramsConfigDataEO.getFileConnectId())) {
List<OSSFile> ossFileList = ossFileService.getFileInfosByConnectId(paramsConfigDataEO.getFileConnectId());
if (CollectionUtil.isNotEmpty(ossFileList)) {
configDataBuilder.append(ossFileList.get(0).getFileName()).append("#");
// 不合并导出时 验证文件名是否相同,相同进一步验证文件内容是否相同,不同时重命名文件
String fileName = ossFileList.get(0).getFileName();
String newFileName = isRepeat(fileListAll, ossFileList.get(0));
if (!"".equals(newFileName) && !"old".equals(newFileName)) { // 文件名称重复 且文件内容不同
configDataBuilder.append(newFileName).append("#");
ossFileList.get(0).setFileName(newFileName);
fileList.addAll(ossFileList);
fileListAll.addAll(ossFileList);
} else if ("old".equals(newFileName)) { // 文件名称重复 且文件内容相同
configDataBuilder.append(fileName).append("#");
fileList.addAll(ossFileList);
}else {
configDataBuilder.append(fileName).append("#");
fileList.addAll(ossFileList);
fileListAll.addAll(ossFileList);
}
fileList.addAll(ossFileList);
}
}
@@ -1116,7 +1171,7 @@ public class ParamsReportDetailEOServiceImpl extends ServiceImpl<ParamsReportDet
record1.put(paramsConfigEO.getId(), configData);
}
});
}
} else if ("2".equals(paramsReportDetailVO.getCombineFlag())) { // 合并
String configData = "";
@@ -1136,9 +1191,35 @@ public class ParamsReportDetailEOServiceImpl extends ServiceImpl<ParamsReportDet
if (StringUtils.isNotEmpty(paramsReportConfigDataEO.getFileConnectId())) {
List<OSSFile> ossFileList = ossFileService.getFileInfosByConnectId(paramsReportConfigDataEO.getFileConnectId());
if (CollectionUtil.isNotEmpty(ossFileList)) {
fileNameList.add(ossFileList.get(0).getFileName());
// 合并导出时 验证文件名是否相同,相同进一步验证文件内容是否形同,不同时重命名文件
String fileName = ossFileList.get(0).getFileName();
String newFileName = isRepeat(fileListAll, ossFileList.get(0));
if (!"".equals(newFileName) && !"old".equals(newFileName)) { // 文件名称重复 且文件内容不同
// 判断 新文件名是不是当前参数下 配置间的 重复
int count = (int) fileList.stream().filter(e->newFileName.equals(e.getFileName())).count();
if (count <= 0) { // 不是 (如果是,不做任何操作)
fileNameList.add(newFileName);
ossFileList.get(0).setFileName(newFileName);
fileList.addAll(ossFileList);
fileListAll.addAll(ossFileList);
}
} else if ("old".equals(newFileName)) { // 文件名称重复 且文件内容相同
// 判断这种重复是不是当前参数下 配置间的
int count = (int) fileList.stream().filter(e->fileName.equals(e.getFileName())).count();
if (count <= 0) { // 不是 (如果是,不做任何操作)
fileNameList.add(fileName);
fileList.addAll(ossFileList);
fileListAll.addAll(ossFileList);
}
}else { // 文件名称不重复
fileNameList.add(fileName);
fileList.addAll(ossFileList);
fileListAll.addAll(ossFileList);
}
fileList.addAll(ossFileList);
}
}
}
@@ -1151,7 +1232,7 @@ public class ParamsReportDetailEOServiceImpl extends ServiceImpl<ParamsReportDet
configDataBuilder.append(textDatas).append("#");
}
if (CollectionUtil.isNotEmpty(fileNameList)) {
fileNameList = fileNameList.stream().distinct().collect(Collectors.toList());
// fileNameList = fileNameList.stream().distinct().collect(Collectors.toList());
configDataBuilder.append(StringUtils.join(fileNameList, paramsReportDetailVO.getSeparator())).append("#");
}
configData = configDataBuilder.toString();
@@ -1168,7 +1249,7 @@ public class ParamsReportDetailEOServiceImpl extends ServiceImpl<ParamsReportDet
return dataList;
}
private List<Map> queryForExportCustom(ParamsReportDetailVO paramsReportDetailVO) {
private List<Map> queryForExportCustom(ParamsReportDetailVO paramsReportDetailVO) throws IOException {
paramsReportDetailVO.setSeparator("*"); // 设置分隔符
List<String> idList = new ArrayList<>();
@@ -1194,8 +1275,8 @@ public class ParamsReportDetailEOServiceImpl extends ServiceImpl<ParamsReportDet
String controlType = (String) record1.get("control_type");
if (CollectionUtil.isNotEmpty(paramsConfigEOList)) {
List<OSSFile> fileList = new ArrayList<>();
List<OSSFile> imgFileList = new ArrayList<>();
List<OSSFile> fileList = new ArrayList<>(); // 存放当前编号下的非图片文件
List<OSSFile> imgFileList = new ArrayList<>(); // 存放当前编号下的图片文件
// 合并配置数据
String configData = "";
@@ -1208,7 +1289,8 @@ public class ParamsReportDetailEOServiceImpl extends ServiceImpl<ParamsReportDet
.map(ParamsReportConfigDataEO::getPullData).distinct().collect(Collectors.joining(paramsReportDetailVO.getSeparator())); // 整合下拉数据
List<String> fileNameList = new ArrayList<>();
for (ParamsReportConfigDataEO paramsReportConfigDataEO : paramsReportConfigDataEOS) { // 获取”导出文件夹“下的文件
if (CollectionUtil.isNotEmpty(paramsReportConfigDataEOS)) {
ParamsReportConfigDataEO paramsReportConfigDataEO = paramsReportConfigDataEOS.get(0); // 获取”导出文件夹“下的文件
if (StringUtils.isNotEmpty(paramsReportConfigDataEO.getFileConnectId())) {
List<OSSFile> ossFileList = ossFileService.getFileInfosByConnectId(paramsReportConfigDataEO.getFileConnectId());
if (CollectionUtil.isNotEmpty(ossFileList)) {
@@ -1561,6 +1643,73 @@ public class ParamsReportDetailEOServiceImpl extends ServiceImpl<ParamsReportDet
return null;
}
private boolean compareFileContent(String fileUrl1, String fileUrl2) throws IOException {
if (CosBootUtil.doesObjectExist(fileUrl1) && CosBootUtil.doesObjectExist(fileUrl2)){
try (InputStream file1 = CosBootUtil.download(fileUrl1);
InputStream file2 = CosBootUtil.download(fileUrl2);) {
String md5Hex1 = DigestUtils.md5Hex(file1);
String md5Hex2 = DigestUtils.md5Hex(file2);
return md5Hex1.equals(md5Hex2);
} catch (IOException e) {
log.error(e.getMessage(), e);
throw new IOException(e.getMessage());
}
} else { // 有文件不存在时,也视为不相同
return false;
}
}
// 判断导出文件是否重复
private String isRepeat(List<OSSFile> fileList, OSSFile ossFile) throws IOException {
String fileName = ossFile.getFileName();
String currentFileUrl = ossFile.getUrl();
List<OSSFile> result = fileList.stream().filter(e-> fileName.equals(e.getFileName())).collect(Collectors.toList());
if (result.size() > 0) { // 文件名重复
String fileUrl = result.get(0).getUrl();
if (!compareFileContent(currentFileUrl, fileUrl)) { // 文件内容不同
List<OSSFile> sameFileList = fileList.stream().filter(e-> compareFileName(fileName, e.getFileName())).collect(Collectors.toList());
int count = sameFileList.size();
if (count > 0) {
// 判断 和重命名的文件 内容是否相同
for (OSSFile file : sameFileList) {
if (compareFileContent(currentFileUrl, file.getUrl())) {
return file.getFileName();
}
}
// 遍历完 没相同的 再重命名
String newFileName = fileName.substring(0, fileName.lastIndexOf(".")) + (count + 1) + fileName.substring(fileName.lastIndexOf("."));
return newFileName;
} else {
String newFileName = fileName.substring(0, fileName.lastIndexOf(".")) + (count + 1) + fileName.substring(fileName.lastIndexOf("."));
return newFileName;
}
} else { // 文件内容相同
return "old";
}
} else { // 文件名不重复
return "";
}
}
// 比较文件名,如 currentFileName 文件.pdf ; fileName: 文件1.pdf
private boolean compareFileName(String currentFileName, String fileName) {
StringBuilder sb = new StringBuilder();
sb.append(fileName.substring(0, fileName.lastIndexOf(".") - 1));
sb.append(fileName.substring(fileName.lastIndexOf("."), fileName.length()));
if (currentFileName.equals(sb.toString())) {
return true;
} else { // 当 currentFileName = fileName 时 也返回false
return false;
}
}
/**
* 实体对象转成Map
* @param obj 实体对象