Commit 231f9bd342c99d605d0d07dd33c98dc5741bcbf5

Authored by 房远帅
2 parents 27db5714 ad4f7569

Merge branch 'master_chujiang' into master_warehouse

@@ -548,7 +548,9 @@ public class PurchaseOrderInfoController extends DefaultBaseController { @@ -548,7 +548,9 @@ public class PurchaseOrderInfoController extends DefaultBaseController {
548 } 548 }
549 } catch (Exception e) { 549 } catch (Exception e) {
550 log.error("订货单导出失败: {}", e.getMessage(), e); 550 log.error("订货单导出失败: {}", e.getMessage(), e);
551 - throw new RuntimeException("订货单导出失败:", e); 551 + // 根因消息一并抛出,便于前端/日志直接定位(NPE 等无消息异常用类名兜底)
  552 + throw new RuntimeException("订货单导出失败: "
  553 + + (e.getMessage() == null ? e.getClass().getName() : e.getMessage()), e);
552 } 554 }
553 } 555 }
554 556
@@ -31,6 +31,7 @@ import javax.annotation.Resource; @@ -31,6 +31,7 @@ import javax.annotation.Resource;
31 import java.io.*; 31 import java.io.*;
32 import java.math.BigDecimal; 32 import java.math.BigDecimal;
33 import java.math.RoundingMode; 33 import java.math.RoundingMode;
  34 +import java.nio.charset.StandardCharsets;
34 import java.nio.file.Files; 35 import java.nio.file.Files;
35 import java.time.format.DateTimeFormatter; 36 import java.time.format.DateTimeFormatter;
36 import java.util.ArrayList; 37 import java.util.ArrayList;
@@ -183,6 +184,8 @@ public class BusinessDataExportHandler implements ExportHandler { @@ -183,6 +184,8 @@ public class BusinessDataExportHandler implements ExportHandler {
183 } 184 }
184 // 时间格式 185 // 时间格式
185 DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); 186 DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
  187 + // 失败单清单:单张失败不影响其余单据导出,最后在 zip 内附失败清单
  188 + List<String> failedOrders = new ArrayList<>();
186 try (FileOutputStream fosZip = new FileOutputStream(file); 189 try (FileOutputStream fosZip = new FileOutputStream(file);
187 ZipOutputStream zos = new ZipOutputStream(fosZip)) { 190 ZipOutputStream zos = new ZipOutputStream(fosZip)) {
188 for (PurchaseOrderInfo orderInfo : orderInfoList) { 191 for (PurchaseOrderInfo orderInfo : orderInfoList) {
@@ -311,20 +314,31 @@ public class BusinessDataExportHandler implements ExportHandler { @@ -311,20 +314,31 @@ public class BusinessDataExportHandler implements ExportHandler {
311 } 314 }
312 } 315 }
313 } else { 316 } else {
314 - ByteArrayOutputStream baos = new ByteArrayOutputStream();  
315 - workbook.write(baos); 317 + // 直接写入 zip 流,避免 ByteArrayOutputStream 整文件拷贝占用双倍内存
316 ZipEntry entry = new ZipEntry(orderInfo.getOrderNo() + "-" 318 ZipEntry entry = new ZipEntry(orderInfo.getOrderNo() + "-"
317 + orderInfo.getOrderingUnitName() + "-" + orderInfo.getWorkshopName() + "-订货单打印-" + createTime + ".xlsx"); 319 + orderInfo.getOrderingUnitName() + "-" + orderInfo.getWorkshopName() + "-订货单打印-" + createTime + ".xlsx");
318 zos.putNextEntry(entry); 320 zos.putNextEntry(entry);
319 - zos.write(baos.toByteArray()); 321 + workbook.write(zos);
320 zos.closeEntry(); 322 zos.closeEntry();
321 } 323 }
  324 + } catch (Exception e) {
  325 + // 单张失败:记录原因后跳过,继续导出其余单据
  326 + log.error("订货单{}导出失败,跳过继续导出", orderInfo.getOrderNo(), e);
  327 + failedOrders.add(orderInfo.getOrderNo() + "-" + orderInfo.getOrderingUnitName()
  328 + + ": " + (e.getMessage() == null ? e.getClass().getName() : e.getMessage()));
322 } 329 }
323 } 330 }
  331 + // 有失败单时在 zip 内附失败清单,便于核对补打
  332 + if (!failedOrders.isEmpty()) {
  333 + zos.putNextEntry(new ZipEntry("导出失败清单.txt"));
  334 + zos.write(String.join("\r\n", failedOrders).getBytes(StandardCharsets.UTF_8));
  335 + zos.closeEntry();
  336 + }
324 zos.flush(); 337 zos.flush();
325 } catch (Exception e) { 338 } catch (Exception e) {
326 log.error("订货单批量打印失败: {}", e.getMessage(), e); 339 log.error("订货单批量打印失败: {}", e.getMessage(), e);
327 - throw new DefaultClientException(e.getMessage()); 340 + throw new DefaultClientException("订货单批量导出失败: "
  341 + + (e.getMessage() == null ? e.getClass().getName() : e.getMessage()));
328 } 342 }
329 } 343 }
330 344
@@ -13,6 +13,7 @@ import javax.swing.*; @@ -13,6 +13,7 @@ import javax.swing.*;
13 import java.awt.*; 13 import java.awt.*;
14 import java.io.File; 14 import java.io.File;
15 import java.io.IOException; 15 import java.io.IOException;
  16 +import java.io.InputStream;
16 import java.math.BigDecimal; 17 import java.math.BigDecimal;
17 import java.net.URLEncoder; 18 import java.net.URLEncoder;
18 import java.time.LocalDate; 19 import java.time.LocalDate;
@@ -342,6 +343,9 @@ public class ExcelUtil { @@ -342,6 +343,9 @@ public class ExcelUtil {
342 excelFile.getAbsolutePath() 343 excelFile.getAbsolutePath()
343 ); 344 );
344 Process process = Runtime.getRuntime().exec(command); 345 Process process = Runtime.getRuntime().exec(command);
  346 + // 消费子进程 stdout/stderr,防止输出缓冲区写满导致子进程阻塞(LibreOffice 转换超时的常见根因)
  347 + drainStream(process.getInputStream());
  348 + drainStream(process.getErrorStream());
345 boolean finished = process.waitFor(30, TimeUnit.SECONDS); // 超时 30 秒 349 boolean finished = process.waitFor(30, TimeUnit.SECONDS); // 超时 30 秒
346 if (!finished) { 350 if (!finished) {
347 process.destroyForcibly(); 351 process.destroyForcibly();
@@ -357,6 +361,23 @@ public class ExcelUtil { @@ -357,6 +361,23 @@ public class ExcelUtil {
357 return pdfFile; 361 return pdfFile;
358 } 362 }
359 363
  364 + /**
  365 + * 异步消费并丢弃子进程输出流,仅防止缓冲区写满阻塞子进程
  366 + */
  367 + private static void drainStream(InputStream in) {
  368 + Thread drainer = new Thread(() -> {
  369 + try (InputStream input = in) {
  370 + byte[] buf = new byte[4096];
  371 + while (input.read(buf) != -1) {
  372 + // 丢弃输出
  373 + }
  374 + } catch (IOException ignored) {
  375 + }
  376 + });
  377 + drainer.setDaemon(true);
  378 + drainer.start();
  379 + }
  380 +
360 public static void adjustVerticalTitleForPdf(Workbook workbook, Sheet sheet, String text, boolean mergedCell) { 381 public static void adjustVerticalTitleForPdf(Workbook workbook, Sheet sheet, String text, boolean mergedCell) {
361 for (Row row : sheet) { 382 for (Row row : sheet) {
362 for (Cell cell : row) { 383 for (Cell cell : row) {