多单位版国产化地质资料管理系统
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
package com.zbooksoft.gdmis.common;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruili.wcp.common.CustomConfigUtil;
import com.ruili.wcp.common.SpringContextUtil;
import com.zbooksoft.gdmis.config.ArchivesCustomConfig;
import com.zbooksoft.gdmis.data.entity.CatCgwjxx;
import com.zbooksoft.gdmis.data.entity.PackingManage;
import com.zbooksoft.gdmis.service.CatCgwjxxService;
import com.zbooksoft.gdmis.service.PackingManageService;
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 UsbSyncExampleWindowsItemCg {
    CustomConfigUtil customConfigUtil = (CustomConfigUtil) SpringContextUtil.getBean("customConfigUtil");
    CatCgwjxxService catCgwjxxService = (CatCgwjxxService) SpringContextUtil.getBean("catCgwjxxServiceImpl");
    PackingManageService packingManageService = (PackingManageService) SpringContextUtil.getBean("packingManageServiceImpl");
 
    public void printItemLabelCg(List<Long> idList) {
 
        ArchivesCustomConfig archivesCustomConfig = customConfigUtil.getConfigObj(ArchivesCustomConfig.class);
        String fileIp = archivesCustomConfig.getFileIp();
 
        TcpComm asyncComm = new TcpComm(fileIp, 9100);
 
        try {
            asyncComm.open();
            System.out.println("链接打印机.....");
            for (Long id : idList) {
                CatCgwjxx itemData = catCgwjxxService.getById(id);
                QueryWrapper<PackingManage> packingManageQueryWrapper = new QueryWrapper<>();
                packingManageQueryWrapper.eq("item_id", id);
                List<PackingManage> packingManageList = packingManageService.list(packingManageQueryWrapper);
                String all = itemData.getWjtm();
                String isSecurity = itemData.getSfsm();
                for (PackingManage packingManage : packingManageList) {
                    String itemNum = packingManage.getItemNum();
                    String newBarCode = itemNum;
                    String newItemCode = itemNum;
                    String remake = itemData.getBz();
 
 
                    // 使用示例
                    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 * 30L;
                    long barcodeX = (400 - barcodeWidth) / 2;
 
                    commandBuilder.append("BARCODE ").append(barcodeX).append(",160,\"128\",140,0,0,2,2,\"" + newBarCode + "\"\r\n");
                    int textLong = getTextLong(newItemCode);
                    long itemCodeWidth = textLong * 25L;
                    long itemCodeX = (400 - itemCodeWidth) / 2;
                    commandBuilder.append("TEXT ").append(itemCodeX).append(",320,\"A123.TTF\",0,13,13,\"" + newItemCode + "\"\r\n");
                    if (remake != null && !remake.isEmpty()) {
                        int wordWidth = getTextLong(remake);
                        long boxMumWidth = wordWidth * 30;
                        long boxMumX = (400 - boxMumWidth) / 2;
                        if ("是".equals(isSecurity)) {
                            commandBuilder.append("TEXT " + boxMumX + ",395,\"A123.TTF\",0,13,13,\"" + remake + "★\"\r\n");
                        } else {
                            boxMumX = boxMumX + 10;
                            commandBuilder.append("TEXT " + boxMumX + ",395,\"A123.TTF\",0,13,13,\"" + remake + "\"\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) {
            System.err.println("打印错误: " + e.getMessage());
        } finally {
            try {
                asyncComm.close();
                System.out.println("连接关闭");
            } catch (Exception e) {
                System.err.println("关闭打印机连接错误: " + e.getMessage());
            }
        }
    }
}