多单位版国产化地质资料管理系统
zs
2026-02-04 fe02f176b512a9d6a4e12437d929e04e99bb7567
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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<Long> 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<String> 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;
    }
}