Commit ad4f7569b7d6ebfdf72760806970046326e9a6a8

Authored by 房远帅
1 parent e9dc62d0

采购:订货单-导出优化

... ... @@ -548,7 +548,9 @@ public class PurchaseOrderInfoController extends DefaultBaseController {
548 548 }
549 549 } catch (Exception e) {
550 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 31 import java.io.*;
32 32 import java.math.BigDecimal;
33 33 import java.math.RoundingMode;
  34 +import java.nio.charset.StandardCharsets;
34 35 import java.nio.file.Files;
35 36 import java.time.format.DateTimeFormatter;
36 37 import java.util.ArrayList;
... ... @@ -183,6 +184,8 @@ public class BusinessDataExportHandler implements ExportHandler {
183 184 }
184 185 // 时间格式
185 186 DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
  187 + // 失败单清单:单张失败不影响其余单据导出,最后在 zip 内附失败清单
  188 + List<String> failedOrders = new ArrayList<>();
186 189 try (FileOutputStream fosZip = new FileOutputStream(file);
187 190 ZipOutputStream zos = new ZipOutputStream(fosZip)) {
188 191 for (PurchaseOrderInfo orderInfo : orderInfoList) {
... ... @@ -311,20 +314,31 @@ public class BusinessDataExportHandler implements ExportHandler {
311 314 }
312 315 }
313 316 } else {
314   - ByteArrayOutputStream baos = new ByteArrayOutputStream();
315   - workbook.write(baos);
  317 + // 直接写入 zip 流,避免 ByteArrayOutputStream 整文件拷贝占用双倍内存
316 318 ZipEntry entry = new ZipEntry(orderInfo.getOrderNo() + "-"
317 319 + orderInfo.getOrderingUnitName() + "-" + orderInfo.getWorkshopName() + "-订货单打印-" + createTime + ".xlsx");
318 320 zos.putNextEntry(entry);
319   - zos.write(baos.toByteArray());
  321 + workbook.write(zos);
320 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 337 zos.flush();
325 338 } catch (Exception e) {
326 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 13 import java.awt.*;
14 14 import java.io.File;
15 15 import java.io.IOException;
  16 +import java.io.InputStream;
16 17 import java.math.BigDecimal;
17 18 import java.net.URLEncoder;
18 19 import java.time.LocalDate;
... ... @@ -342,6 +343,9 @@ public class ExcelUtil {
342 343 excelFile.getAbsolutePath()
343 344 );
344 345 Process process = Runtime.getRuntime().exec(command);
  346 + // 消费子进程 stdout/stderr,防止输出缓冲区写满导致子进程阻塞(LibreOffice 转换超时的常见根因)
  347 + drainStream(process.getInputStream());
  348 + drainStream(process.getErrorStream());
345 349 boolean finished = process.waitFor(30, TimeUnit.SECONDS); // 超时 30 秒
346 350 if (!finished) {
347 351 process.destroyForcibly();
... ... @@ -357,6 +361,23 @@ public class ExcelUtil {
357 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 381 public static void adjustVerticalTitleForPdf(Workbook workbook, Sheet sheet, String text, boolean mergedCell) {
361 382 for (Row row : sheet) {
362 383 for (Cell cell : row) {
... ...