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());
|
}
|
}
|
}
|
}
|