package com.mes.print; import com.mes.print.model.FlowCardData; import com.mes.print.model.LabelData; /** * 打印辅助类:封装 mes-print 模块调用 * * 根据 print.properties 配置的打印机类型自动选择打印方式: * - hp: 打印流转卡(A4/A5,走 HP 激光) * - tsc: 打印标签(走 TSC 热敏) * - none: 不打印 */ public class PrintHelper { /** * 预热打印模块(异步调用,不阻塞主流程) * 首次打印约 3-5 秒的 PDF 模板加载 + 底图渲染放到启动阶段完成 */ public static void warmUpAsync() { Thread t = new Thread(() -> { try { Printer.warmUp(); } catch (Throwable e) { System.err.println("[打印] 预热异常: " + e.getMessage()); } }, "print-warmup"); t.setDaemon(true); t.start(); } /** * 打印流转卡(HP 激光打印机) * * @param sn 产品序列号(客户码) * @param steelSn 钢印码(可为 null) * @return true=成功或已跳过;false=失败 */ public static boolean printFlowCard(String sn, String steelSn) { try { String type = Printer.getCurrentType(); if ("none".equals(type)) { System.out.println("[打印] 配置为不打印,跳过流转卡: " + sn); return true; } if (!"hp".equals(type)) { System.out.println("[打印] 打印机类型为 " + type + ",不支持流转卡"); return false; } FlowCardData data = new FlowCardData(); data.setSn(sn); if (steelSn != null) { data.setSteelSn(steelSn); } Printer.printFlowCard(data); System.out.println("[打印] 流转卡打印成功: " + sn); return true; } catch (PrinterException e) { System.err.println("[打印] 流转卡打印失败: " + e.getMessage()); e.printStackTrace(); return false; } catch (Exception e) { System.err.println("[打印] 流转卡打印异常: " + e.getMessage()); e.printStackTrace(); return false; } } /** * 打印标签(TSC 热敏标签机) * * @param sn 产品序列号 * @param barcode 条码内容(null 时用 sn) * @param qrcode 二维码内容(null 时用 sn) * @param title 标题(null 时用默认「产品标签」) * @param copies 打印份数(≤0 时按 1 份) * @return true=成功或已跳过;false=失败 */ public static boolean printLabel(String sn, String barcode, String qrcode, String title, int copies) { try { String type = Printer.getCurrentType(); if ("none".equals(type)) { System.out.println("[打印] 配置为不打印,跳过标签: " + sn); return true; } if (!"tsc".equals(type)) { System.out.println("[打印] 打印机类型为 " + type + ",不支持标签"); return false; } LabelData data = new LabelData(); data.setSn(sn); data.setBarcode(barcode != null ? barcode : sn); data.setQrcode(qrcode != null ? qrcode : sn); data.setTitle(title != null ? title : "产品标签"); data.setCopies(copies > 0 ? copies : 1); Printer.printLabel(data); System.out.println("[打印] 标签打印成功: " + sn); return true; } catch (PrinterException e) { System.err.println("[打印] 标签打印失败: " + e.getMessage()); e.printStackTrace(); return false; } catch (Exception e) { System.err.println("[打印] 标签打印异常: " + e.getMessage()); e.printStackTrace(); return false; } } /** * 根据当前配置自动打印 * - hp: 打流转卡(steelSn 传 null) * - tsc: 打标签(barcode/qrcode 都用 sn) * - none: 跳过 */ public static boolean autoPrint(String sn) { return autoPrint(sn, null); } /** * 根据当前配置自动打印 * * @param sn 产品序列号 * @param steelSn 钢印码(仅流转卡使用,可为 null) */ public static boolean autoPrint(String sn, String steelSn) { try { String type = Printer.getCurrentType(); if ("none".equals(type)) { System.out.println("[打印] 配置为不打印,跳过"); return true; } else if ("hp".equals(type)) { return printFlowCard(sn, steelSn); } else if ("tsc".equals(type)) { return printLabel(sn, sn, sn, "产品标签", 1); } else { System.err.println("[打印] 未知的打印机类型: " + type); return false; } } catch (Exception e) { System.err.println("[打印] 自动打印失败: " + e.getMessage()); e.printStackTrace(); return false; } } }