From fbb52ecf7cb6f15191492ea4b5878d5b5b2d3f10 Mon Sep 17 00:00:00 2001 From: wangxianzhang Date: Wed, 17 Aug 2022 15:51:00 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E6=8A=A5=E8=A1=A8=E3=80=91=E6=89=B9?= =?UTF-8?q?=E9=87=8F=E5=AF=BC=E5=87=BA-=E5=A4=9A=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E5=8F=8A=E7=B3=BB=E5=88=97=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../result/JimuReportDbDataResultDTO.java | 1 + .../impl/IcCustomerReportServiceImpl.java | 84 +++++++++++++------ 2 files changed, 61 insertions(+), 24 deletions(-) diff --git a/epmet-commons/epmet-commons-feignclient/src/main/java/com/epmet/commons/feignclient/dtos/result/JimuReportDbDataResultDTO.java b/epmet-commons/epmet-commons-feignclient/src/main/java/com/epmet/commons/feignclient/dtos/result/JimuReportDbDataResultDTO.java index fe6bf6240b..8eb58e8b74 100644 --- a/epmet-commons/epmet-commons-feignclient/src/main/java/com/epmet/commons/feignclient/dtos/result/JimuReportDbDataResultDTO.java +++ b/epmet-commons/epmet-commons-feignclient/src/main/java/com/epmet/commons/feignclient/dtos/result/JimuReportDbDataResultDTO.java @@ -12,6 +12,7 @@ public class JimuReportDbDataResultDTO { private String apiUrl; private String apiMethod; private String isList; + private String dbChName; } } diff --git a/epmet-module/oper-customize/oper-customize-server/src/main/java/com/epmet/service/impl/IcCustomerReportServiceImpl.java b/epmet-module/oper-customize/oper-customize-server/src/main/java/com/epmet/service/impl/IcCustomerReportServiceImpl.java index 59e2a836a7..3da6d12f4a 100644 --- a/epmet-module/oper-customize/oper-customize-server/src/main/java/com/epmet/service/impl/IcCustomerReportServiceImpl.java +++ b/epmet-module/oper-customize/oper-customize-server/src/main/java/com/epmet/service/impl/IcCustomerReportServiceImpl.java @@ -40,6 +40,7 @@ import com.epmet.service.IcReportFunService; import feign.Response; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.collections4.ListUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -57,6 +58,9 @@ import java.nio.file.Path; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.*; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -83,6 +87,8 @@ public class IcCustomerReportServiceImpl extends BaseServiceImpl getWrapper(Map params) { @@ -299,7 +305,7 @@ public class IcCustomerReportServiceImpl extends BaseServiceImpl ids) { + private Path downloadAndComppress(Path storePath, String reportId, String reportName, String paramKey, List ids) { // 请求头 Map headers = new HashMap<>(); headers.put(Constant.AUTHORIZATION_HEADER, EpmetRequestHolder.getHeader(Constant.AUTHORIZATION_HEADER)); @@ -341,9 +347,9 @@ public class IcCustomerReportServiceImpl extends BaseServiceImpl files = new ArrayList<>(); + ArrayList files = new ArrayList<>(); - final String currentTimeStr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss")); + String currentTimeStr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss")); // 创建存放xlsx的临时目录 Path xlsxStorePath = storePath.resolve(currentTimeStr); @@ -353,38 +359,34 @@ public class IcCustomerReportServiceImpl extends BaseServiceImpl> idParts = ListUtils.partition(ids, 100); // 1. 循环下载所有id对应的excel - for (String id : ids) { - param.getQueryParam().setId(id); - final Response response = jiMuReportOpenFeignClient.exportAllExcelStream(param); - // 取出文件后缀 - final LinkedList header = (LinkedList) response.headers().get("content-disposition"); - final Matcher matcher = Pattern.compile("attachment;filename=(.+)(.xls|.xlsx)").matcher(header.get(0)); - if (!matcher.matches()) { - log.error("【报表批量导出】批量导出失败,后缀不匹配。文件名:{}, 报表ID:{}, 业务数据ID:{}", header.get(0), reportId, id); - continue; - } - - final File excelFile = xlsxStorePath.resolve("file_" + id + matcher.group(2)).toFile(); + CountDownLatch cdl = new CountDownLatch(idParts.size()); + for (List idPart : idParts) { + CompletableFuture.runAsync(() -> { + downloadXlsByBatchByBizId(reportId, idPart, param, xlsxStorePath, files, cdl); + }, executorService); + } - files.add(excelFile); - try (FileOutputStream fos = new FileOutputStream(excelFile)) { - IOUtils.copy(response.body().asInputStream(), fos); - } catch (Exception e) { - log.error("【报表批量导出】生成临时表格文件失败,文件名:{},报表ID:{}, 业务数据ID:{},错误信息:{}", header.get(0), reportId, id, ExceptionUtils.getErrorStackTrace(e)); - } + // 等待多线程执行完成,再执行打包 + try { + cdl.await(); + } catch (InterruptedException e) { + log.error(ExceptionUtils.getErrorStackTrace(e)); + throw new EpmetException("【报表批量导出】CountDownLatch等待被中断"); } // 2,打包 - Path zipFile = storePath.resolve("report_export_" + currentTimeStr + ".zip"); + Path zipFile = storePath.resolve(reportName + "_" + currentTimeStr + ".zip"); try (final FileOutputStream fos = new FileOutputStream(zipFile.toFile()); final ZipOutputStream zos = new ZipOutputStream(fos)) { // 循环每一个文件 for (File file : files) { try (final FileInputStream fis = new FileInputStream(file.getAbsolutePath())) { - zos.putNextEntry(new ZipEntry("report_export_" + currentTimeStr + "/" + file.getName())); + zos.putNextEntry(new ZipEntry(reportName + "_" + currentTimeStr + "/" + file.getName())); final byte[] buffer = new byte[10240]; for (int len; (len = fis.read(buffer)) > 0; ) { zos.write(buffer, 0, len); @@ -406,6 +408,40 @@ public class IcCustomerReportServiceImpl extends BaseServiceImpl bizIds, JimuReportExportRequestDTO param, Path xlsxStorePath, ArrayList files, + CountDownLatch cdl) { + for (String id : bizIds) { + param.getQueryParam().setId(id); + final Response response = jiMuReportOpenFeignClient.exportAllExcelStream(param); + // 取出文件后缀 + final LinkedList header = (LinkedList) response.headers().get("content-disposition"); + final Matcher matcher = Pattern.compile("attachment;filename=(.+)(.xls|.xlsx)").matcher(header.get(0)); + if (!matcher.matches()) { + log.error("【报表批量导出】批量导出失败,后缀不匹配。文件名:{}, 报表ID:{}, 业务数据ID:{}", header.get(0), reportId, id); + continue; + } + + final File excelFile = xlsxStorePath.resolve("file_" + id + matcher.group(2)).toFile(); + + files.add(excelFile); + try (FileOutputStream fos = new FileOutputStream(excelFile)) { + IOUtils.copy(response.body().asInputStream(), fos); + } catch (Exception e) { + log.error("【报表批量导出】生成临时表格文件失败,文件名:{},报表ID:{}, 业务数据ID:{},错误信息:{}", header.get(0), reportId, id, ExceptionUtils.getErrorStackTrace(e)); + } + } + + cdl.countDown(); + } + /** * 清理临时保存文件 */