package com.zbooksoft.gdmis.common; import com.ruili.wcp.common.CustomConfigUtil; import com.ruili.wcp.common.SpringContextUtil; import com.zbooksoft.gdmis.config.ArchivesCustomConfig; import com.zbooksoft.gdmis.data.entity.CatYswjxx; import com.zbooksoft.gdmis.service.CatYswjxxService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import tsc_sdk.TcpComm; import java.util.List; import static com.zbooksoft.gdmis.common.TscUtil.getTextLong; import static com.zbooksoft.gdmis.common.TscUtil.wrapText; public class UsbSyncExampleWindowsItem { CatYswjxxService catYswjxxService = (CatYswjxxService) SpringContextUtil.getBean("catYswjxxServiceImpl"); CustomConfigUtil customConfigUtil = (CustomConfigUtil) SpringContextUtil.getBean("customConfigUtil"); private static final Logger logger = LoggerFactory.getLogger(UsbSyncExampleWindowsItem.class); public void printItemLabel(List idList) { ArchivesCustomConfig archivesCustomConfig = customConfigUtil.getConfigObj(ArchivesCustomConfig.class); String fileIp = archivesCustomConfig.getFileIp(); TcpComm asyncComm = new TcpComm(fileIp, 9100); try { asyncComm.open(); for (Long id : idList) { CatYswjxx catYswjxx = catYswjxxService.getById(id); String all = catYswjxx.getWjtm(); String itemCode = catYswjxx.getDh(); String isSecurity = catYswjxx.getSfsm(); String newBarCode = itemCode.replace("底", "D").replace("测", "C").replace("观", "G").replace("探", "T").replace("样", "Y").replace("试", "S").replace("录", "L").replace("像", "X").replace("综", "Z").replace("文", "W").replace("实", "S").replace("物", "W").replace("钻", "Z").replace("设", "S").replace("相", "X").replace("油", "Y").replace("电", "D").replace("地", "D"); String boxMum = catYswjxx.getHh(); String newBoxMum = ""; newBoxMum = boxMum; newBarCode = newBarCode + "-" + newBoxMum; // 使用示例 List wrappedLines = wrapText(all, 28, 3); // 每行最大宽度20 StringBuilder commandBuilder = new StringBuilder(); commandBuilder.append("SET RESPONSE ON\r\n"); commandBuilder.append("CLS\r\n"); int yPosition = 25; if (wrappedLines.size() == 1) { yPosition = 65; } else if (wrappedLines.size() == 2) { yPosition = 50; } for (String line : wrappedLines) { commandBuilder.append("TEXT 25,").append(yPosition).append(",\"A123.TTF\",0,7,7,\"").append(line).append("\"\r\n"); yPosition += 35; } int barcodeTextLength = newBarCode.length(); long barcodeWidth = barcodeTextLength * 20L; long barcodeX = (400 - barcodeWidth) / 2; commandBuilder.append("BARCODE ").append(barcodeX).append(",145,\"128\",140,0,0,2,2,\"").append(newBarCode).append("\"\r\n"); int textLong = getTextLong(itemCode); long itemCodeWidth = textLong * 30L; long itemCodeX = (400 - itemCodeWidth) / 2; commandBuilder.append("TEXT ").append(itemCodeX).append(",315,\"A123.TTF\",0,16,16,\"").append(itemCode).append("\"\r\n"); int wordWidth = getTextLong("盒" + boxMum); long boxMumWidth = wordWidth * 30L; long boxMumX = (400 - boxMumWidth) / 2; if ("是".equals(isSecurity)) { commandBuilder.append("TEXT ").append(boxMumX).append(",390,\"A123.TTF\",0,16,16,\"").append("盒").append(boxMum).append("★\"\r\n"); } else { boxMumX = boxMumX + 10; commandBuilder.append("TEXT ").append(boxMumX).append(",390,\"A123.TTF\",0,16,16,\"").append("盒").append(boxMum).append("\"\r\n"); } commandBuilder.append("PRINT 1\r\n"); System.out.println(commandBuilder.toString()); byte[] dataToSend = commandBuilder.toString().getBytes("UTF-8"); asyncComm.writeSync(dataToSend); } Thread.sleep(25000); } catch (Exception e) { logger.error("打印错误: {}", e.getMessage()); } finally { try { asyncComm.close(); logger.info("连接关闭"); } catch (Exception e) { logger.error("关闭打印机连接错误: {}", e.getMessage()); } } } public static void main(String[] args) { String he = "C10-06-11"; if (he.contains("盒")) { //获取盒字后面的内容 String he1 = he.substring(1); } else { formatConverter(he); } } public static String formatConverter(String boxNum) { String newBoxNum = ""; String[] split = boxNum.split("-"); if (split.length == 3) { String f = split[0]; String fFormat = ""; if (f.length() == 2) { fFormat = f.charAt(0) + "0" + f.substring(1); } else { fFormat = f; } String s = split[1]; String sFormat = String.format("%02d", Integer.parseInt(s)); String t = split[2]; String tFormat = String.format("%02d", Integer.parseInt(t)); newBoxNum = fFormat + sFormat + tFormat; } return newBoxNum; } }