diff --git a/ServiceSite/src/main/java/com/seasky/ledgerincome/application/command/EntryCommandServiceImpl.java b/ServiceSite/src/main/java/com/seasky/ledgerincome/application/command/EntryCommandServiceImpl.java index f942cefd91af8c479b5e6f0df6f900434eebd768..345dfd64f6d6b7d91752e2bfef975b2fe3237c1a 100644 --- a/ServiceSite/src/main/java/com/seasky/ledgerincome/application/command/EntryCommandServiceImpl.java +++ b/ServiceSite/src/main/java/com/seasky/ledgerincome/application/command/EntryCommandServiceImpl.java @@ -1378,7 +1378,10 @@ public class EntryCommandServiceImpl implements EntryCommandService { public void generatePDF(List<IncomeAggregate> incomeAggregates,String smallTitle,Map<Long,IncomeAggregate> map,UserInfoOut infoOut,Long batchId) { String userName = infoOut.getUserName(); try { + long start = System.currentTimeMillis(); FileUtil.downloadVoucherPDF(smallTitle, userName, incomeAggregates,false,batchId,pdfResource); + long end = System.currentTimeMillis(); + log.info(("鍏ヨ处鐢熸垚pdf鏂囦欢鑰楁椂"+((end - start) / 1000.00) + "(绉�)" )); } catch (Exception e) { e.printStackTrace(); log.error("鍏ヨ处鐢宠鍗曠敓鎴愬紓甯革細"+e.getMessage()); diff --git a/ServiceSite/src/main/java/com/seasky/ledgerincome/utils/FileUtil.java b/ServiceSite/src/main/java/com/seasky/ledgerincome/utils/FileUtil.java index 8c1ce8e25dde9d45db3285211b4a31598f67621d..894d4269e488ced9d9689ae3e3d5a135c0a74e15 100644 --- a/ServiceSite/src/main/java/com/seasky/ledgerincome/utils/FileUtil.java +++ b/ServiceSite/src/main/java/com/seasky/ledgerincome/utils/FileUtil.java @@ -37,6 +37,11 @@ import java.nio.charset.StandardCharsets; import java.text.SimpleDateFormat; import java.util.*; import java.util.List; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -161,7 +166,7 @@ public class FileUtil { * @throws IOException * @throws DocumentException */ - public synchronized static void downloadVoucherPDF(String smallTile, String userName, List<IncomeAggregate> incomeAggregates, Boolean download, Long batchId,String pdfResource) throws IOException, DocumentException { + public static void downloadVoucherPDF(String smallTile, String userName, List<IncomeAggregate> incomeAggregates, Boolean download, Long batchId,String pdfResource) throws IOException, DocumentException { String fileName = "鏀舵鍥炲崟鍏ヨ处鐢宠_"+batchId+".pdf"; File tempFile = new File(System.getProperty("java.io.tmpdir") + "/" + fileName); if (!tempFile.exists()) { @@ -230,11 +235,8 @@ public class FileUtil { if (!tempFile.delete()) { throw new RuntimeException("鍒犻櫎鏂囦欢鍑洪敊"); } - /* if (1 == 1) { - throw new RuntimeException("娴嬭瘯"); - }*/ } - public synchronized static byte[] downloadVoucherPDFNotSave(String smallTile, String userName, List<IncomeAggregate> incomeAggregates, Long batchId,String pdfResource) throws IOException, DocumentException { + public static byte[] downloadVoucherPDFNotSave(String smallTile, String userName, List<IncomeAggregate> incomeAggregates, Long batchId,String pdfResource) throws IOException, DocumentException { String fileName = "鏀舵鍥炲崟鍏ヨ处鐢宠_"+batchId+".pdf"; File tempFile = new File(System.getProperty("java.io.tmpdir") + "/" + fileName); if (!tempFile.exists()) { @@ -250,26 +252,49 @@ public class FileUtil { Document document = new Document(pageSize, 50, 50, 50, 50); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(tempFile)); document.open(); + ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); + List<Future<Void>> futures = new ArrayList<>(); + AtomicBoolean hasError = new AtomicBoolean(false); try { //鏍囬 addTitle(document, smallTile,batchId,writer,headerFont,bodyFont); for (IncomeAggregate incomeAggregate:incomeAggregates) { - //鏀舵绫诲瀷 - addIncomeType(document,incomeAggregate,headerFont,bodyFont); - //鍥炲崟淇℃伅 - addIncomeInfo(document, incomeAggregate,headerFont,bodyFont); - if (!smallTile.equals("鏆傚瓨鍏ヨ处") && !Arrays.asList("A-宸ヨ祫閫€鍥�","搴斾氦璐㈡斂涓撴埛娆�","瀹氭湡瀛樻") - .contains(incomeAggregate.getReceiptBillVo().getIncomeTypeName())) { - //椤圭洰淇℃伅 - addProjectInfo(document, smallTile, incomeAggregate,headerFont,bodyFont); - //绁ㄦ嵁 - addInvoiceInfo(document, smallTile, incomeAggregate.getIncomeId(),headerFont,bodyFont); - } - if(Arrays.asList("A-宸ヨ祫閫€鍥�","搴斾氦璐㈡斂涓撴埛娆�","瀹氭湡瀛樻").contains(incomeAggregate.getReceiptBillVo().getIncomeTypeName())){ - addSplitAmount(document,incomeAggregate,headerFont,bodyFont); + Future<Void> future = executorService.submit(() -> { + if (hasError.get()) { + return null; + } + try{ + //鏀舵绫诲瀷 + addIncomeType(document,incomeAggregate,headerFont,bodyFont); + //鍥炲崟淇℃伅 + addIncomeInfo(document, incomeAggregate,headerFont,bodyFont); + if (!smallTile.equals("鏆傚瓨鍏ヨ处") && !Arrays.asList("A-宸ヨ祫閫€鍥�","搴斾氦璐㈡斂涓撴埛娆�","瀹氭湡瀛樻") + .contains(incomeAggregate.getReceiptBillVo().getIncomeTypeName())) { + //椤圭洰淇℃伅 + addProjectInfo(document, smallTile, incomeAggregate,headerFont,bodyFont); + //绁ㄦ嵁 + addInvoiceInfo(document, smallTile, incomeAggregate.getIncomeId(),headerFont,bodyFont); + } + if(Arrays.asList("A-宸ヨ祫閫€鍥�","搴斾氦璐㈡斂涓撴埛娆�","瀹氭湡瀛樻").contains(incomeAggregate.getReceiptBillVo().getIncomeTypeName())){ + addSplitAmount(document,incomeAggregate,headerFont,bodyFont); + } + //闄勪欢 + addFileInfo(document, incomeAggregate.getIncomeId(), userName,headerFont,bodyFont); + } catch (Exception e) { + hasError.set(true); // 鏍囪浠诲姟澶辫触 + throw e; // 鍚戜笂鎶涘嚭寮傚父 + } + return null; + }); + futures.add(future); + } + for (Future<Void> future : futures) { + try { + future.get(); // 闃诲绛夊緟浠诲姟瀹屾垚锛屾鏌ュ紓甯� + } catch (ExecutionException | InterruptedException e) { + hasError.set(true); + throw new RuntimeException("鍏ヨ处鐢宠鍗昿df鐢熸垚浠诲姟鎵ц鍑洪敊", e.getCause()); // 鎶涘嚭浠诲姟涓殑鍘熷寮傚父 } - //闄勪欢 - addFileInfo(document, incomeAggregate.getIncomeId(), userName,headerFont,bodyFont); } //鐢宠浜轰俊鎭� addApplyInfo(document,userName,headerFont,bodyFont); @@ -280,6 +305,7 @@ public class FileUtil { return bytes; } finally { document.close(); + executorService.shutdown(); if (!tempFile.delete()) { throw new RuntimeException("鍒犻櫎鏂囦欢鍑洪敊"); }