package com.mes.print; import com.mes.print.Printer; import com.mes.print.PrinterException; import com.mes.print.model.FlowCardData; import com.mes.print.model.LabelData; import java.text.SimpleDateFormat; import java.util.Date; /** * 打印辅助类:封装 mes-print 模块调用 * * 根据 print.properties 配置的打印机类型自动选择打印方式: * - hp: 打印流转卡 * - tsc: 打印标签 * - none: 不打印 */ public class PrintHelper { /** * 打印流转卡(用于 HP 激光打印机) * * @param sn 产品序列号 * @param opNo 工位号(如 OP040A) * @param batch 批次号 * @param operator 操作员 * @param remark 备注 * @return 是否打印成功 */ public static boolean printFlowCard(String sn, String opNo, String batch, String operator, String remark) { try { String type = Printer.getCurrentType(); if ("none".equals(type)) { System.out.println("[打印] 当前配置为不打印,跳过流转卡打印"); return true; } if (!"hp".equals(type)) { System.out.println("[打印] 当前打印机类型为 " + type + ",不支持流转卡打印"); return false; } FlowCardData data = new FlowCardData(); data.setSn(sn); data.setProduct("+KB24"); // 产品型号,根据实际业务调整 data.setOpNo(opNo); data.setBatch(batch != null ? batch : ""); data.setDate(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date())); data.setOperator(operator != null ? operator : ""); data.setRemark(remark != null ? remark : ""); 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 条码内容 * @param qrcode 二维码内容 * @param title 标题 * @param copies 打印份数 * @return 是否打印成功 */ 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("[打印] 当前配置为不打印,跳过标签打印"); 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; } } /** * 根据当前配置的打印机类型自动打印 * * @param sn 产品序列号 * @param opNo 工位号 * @param operator 操作员 * @return 是否打印成功 */ public static boolean autoPrint(String sn, String opNo, String operator) { try { String type = Printer.getCurrentType(); if ("none".equals(type)) { System.out.println("[打印] 当前配置为不打印,跳过"); return true; } else if ("hp".equals(type)) { return printFlowCard(sn, opNo, "", operator, ""); } 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; } } }