PrintHelper.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package com.mes.print;
  2. import com.mes.print.Printer;
  3. import com.mes.print.PrinterException;
  4. import com.mes.print.model.FlowCardData;
  5. import com.mes.print.model.LabelData;
  6. import java.text.SimpleDateFormat;
  7. import java.util.Date;
  8. /**
  9. * 打印辅助类:封装 mes-print 模块调用
  10. *
  11. * 根据 print.properties 配置的打印机类型自动选择打印方式:
  12. * - hp: 打印流转卡
  13. * - tsc: 打印标签
  14. * - none: 不打印
  15. */
  16. public class PrintHelper {
  17. /**
  18. * 打印流转卡(用于 HP 激光打印机)
  19. *
  20. * @param sn 产品序列号
  21. * @param opNo 工位号(如 OP040A)
  22. * @param batch 批次号
  23. * @param operator 操作员
  24. * @param remark 备注
  25. * @return 是否打印成功
  26. */
  27. public static boolean printFlowCard(String sn, String opNo, String batch, String operator, String remark) {
  28. try {
  29. String type = Printer.getCurrentType();
  30. if ("none".equals(type)) {
  31. System.out.println("[打印] 当前配置为不打印,跳过流转卡打印");
  32. return true;
  33. }
  34. if (!"hp".equals(type)) {
  35. System.out.println("[打印] 当前打印机类型为 " + type + ",不支持流转卡打印");
  36. return false;
  37. }
  38. FlowCardData data = new FlowCardData();
  39. data.setSn(sn);
  40. data.setProduct("+KB24"); // 产品型号,根据实际业务调整
  41. data.setOpNo(opNo);
  42. data.setBatch(batch != null ? batch : "");
  43. data.setDate(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));
  44. data.setOperator(operator != null ? operator : "");
  45. data.setRemark(remark != null ? remark : "");
  46. Printer.printFlowCard(data);
  47. System.out.println("[打印] 流转卡打印成功: " + sn);
  48. return true;
  49. } catch (PrinterException e) {
  50. System.err.println("[打印] 流转卡打印失败: " + e.getMessage());
  51. e.printStackTrace();
  52. return false;
  53. } catch (Exception e) {
  54. System.err.println("[打印] 流转卡打印异常: " + e.getMessage());
  55. e.printStackTrace();
  56. return false;
  57. }
  58. }
  59. /**
  60. * 打印标签(用于 TSC 热敏标签机)
  61. *
  62. * @param sn 产品序列号
  63. * @param barcode 条码内容
  64. * @param qrcode 二维码内容
  65. * @param title 标题
  66. * @param copies 打印份数
  67. * @return 是否打印成功
  68. */
  69. public static boolean printLabel(String sn, String barcode, String qrcode, String title, int copies) {
  70. try {
  71. String type = Printer.getCurrentType();
  72. if ("none".equals(type)) {
  73. System.out.println("[打印] 当前配置为不打印,跳过标签打印");
  74. return true;
  75. }
  76. if (!"tsc".equals(type)) {
  77. System.out.println("[打印] 当前打印机类型为 " + type + ",不支持标签打印");
  78. return false;
  79. }
  80. LabelData data = new LabelData();
  81. data.setSn(sn);
  82. data.setBarcode(barcode != null ? barcode : sn);
  83. data.setQrcode(qrcode != null ? qrcode : sn);
  84. data.setTitle(title != null ? title : "产品标签");
  85. data.setCopies(copies > 0 ? copies : 1);
  86. Printer.printLabel(data);
  87. System.out.println("[打印] 标签打印成功: " + sn);
  88. return true;
  89. } catch (PrinterException e) {
  90. System.err.println("[打印] 标签打印失败: " + e.getMessage());
  91. e.printStackTrace();
  92. return false;
  93. } catch (Exception e) {
  94. System.err.println("[打印] 标签打印异常: " + e.getMessage());
  95. e.printStackTrace();
  96. return false;
  97. }
  98. }
  99. /**
  100. * 根据当前配置的打印机类型自动打印
  101. *
  102. * @param sn 产品序列号
  103. * @param opNo 工位号
  104. * @param operator 操作员
  105. * @return 是否打印成功
  106. */
  107. public static boolean autoPrint(String sn, String opNo, String operator) {
  108. try {
  109. String type = Printer.getCurrentType();
  110. if ("none".equals(type)) {
  111. System.out.println("[打印] 当前配置为不打印,跳过");
  112. return true;
  113. } else if ("hp".equals(type)) {
  114. return printFlowCard(sn, opNo, "", operator, "");
  115. } else if ("tsc".equals(type)) {
  116. return printLabel(sn, sn, sn, "产品标签", 1);
  117. } else {
  118. System.err.println("[打印] 未知的打印机类型: " + type);
  119. return false;
  120. }
  121. } catch (Exception e) {
  122. System.err.println("[打印] 自动打印失败: " + e.getMessage());
  123. e.printStackTrace();
  124. return false;
  125. }
  126. }
  127. }