张晓波
2023-09-19 164694c47c35d6654df69b533e8dbf8b5423efc5
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package com.thhy.materials.modules.biz.rebarprint.service.impl;
 
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.sun.jna.Native;
import com.sun.jna.win32.StdCallLibrary;
import com.thhy.general.common.BasicResult;
import com.thhy.general.config.SysUserInfo;
import com.thhy.general.utils.UserInfoUtils;
import com.thhy.materials.modules.biz.rebarprint.entity.TSteelPrint;
import com.thhy.materials.modules.biz.rebarprint.entity.TSteelProduce;
import com.thhy.materials.modules.biz.rebarprint.mapper.TSteelPrintMapper;
import com.thhy.materials.modules.biz.rebarprint.service.TSteelPrintService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import javax.print.*;
import javax.print.attribute.PrintServiceAttribute;
import javax.print.attribute.standard.PrinterName;
import javax.xml.crypto.Data;
import java.awt.print.PrinterJob;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.SimpleFormatter;
 
@Service
public class TSteelPrintServiceImpl implements TSteelPrintService {
 
    @Resource
    private TSteelPrintMapper steelPrintMapper;
 
 
    @Override
    public BasicResult steelPrintInsert(Map<String, Object> values) {
        SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo();
        String userId = sysUserInfo.getUserId();
        values.put("createUser",userId);
        values.put("steelPrintId","");
        steelPrintMapper.steelPrintInsert(values);
        return BasicResult.success("添加成功");
    }
 
    @Override
    public BasicResult steelPrintList(Map<String, Object> values) {
        SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo();
        values.put("companyId",sysUserInfo.getCompanyId());
        Integer pageSize = Integer.valueOf(values.get("pageSize").toString());
        Integer pageNum = Integer.valueOf(values.get("pageNum").toString());
        PageHelper.startPage(pageNum,pageSize);
        List<TSteelPrint> tSteelPrints = steelPrintMapper.steelPrintList(values);
        PageInfo<TSteelPrint> tSteelPrintPageInfo = new PageInfo<>(tSteelPrints);
        return BasicResult.success(tSteelPrintPageInfo);
    }
 
    @Override
    public BasicResult steelPrintDel(String steelPrintId) {
 
        List<String> produces = steelPrintMapper.steelProduce(steelPrintId);
        if(produces!=null && produces.size()>0){
            return BasicResult.faild("11111","已存在打印数据","error");
        }
        steelPrintMapper.steelPrintDel(steelPrintId);
        return BasicResult.success("删除成功");
    }
 
    @Override
    @Transactional
    public BasicResult print(String steelPrintId) {
        //打印
        //钢筋信息
        TSteelPrint steelPrint = steelPrintMapper.steelPrintInfo(steelPrintId);
        String groupName= steelPrintMapper.steelPrintGroup(steelPrint.getCreateUser());
        steelPrint.setGroupName(groupName);
        String proNumBer = steelPrintMapper.proIdSelectByNumber(steelPrint.getProId());
        //需要打印的份数
        Integer printNum = steelPrint.getPrintNum();
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String format1 = format.format(date);
 
        //查询已打印项目下钢筋编号
        List<TSteelProduce> produces= steelPrintMapper.produceList(steelPrint.getProId());
        TSteelProduce produce1 = new TSteelProduce();
        produce1.setProduceNumber("000000000000000000");
 
        List<HashMap<String, Object>> arrayList = new ArrayList<>();
 
        if(produces.size()>0){
            for (TSteelProduce produce :produces){
                String produceNumber = produce.getProduceNumber();
                String substring = produceNumber.substring(4, 12);
                if(substring.equals(format1)){
                    String number = produceNumber.substring(12, 17);
                    if(Integer.valueOf(produce1.getProduceNumber().substring(12, 17))<Integer.valueOf(number)){
                        produce1.setProduceNumber(produce.getProduceNumber());
                    }
                }
            }
        }
        if(!StringUtil.isEmpty(produce1.getProduceNumber())){
            Integer integer = Integer.valueOf(produce1.getProduceNumber().substring(12, 17));
            for (int num =1;num<=printNum;num++){
                HashMap<String, Object> values = new HashMap<>();
                values.put("steelProduceId","");
                values.put("proId",steelPrint.getProId());
                values.put("reinforcementId",steelPrint.getReinforcementId());
                values.put("blockNum",steelPrint.getBlockNum());
                values.put("createUser",steelPrint.getCreateUser());
                values.put("sizeId",steelPrint.getSizeId());
                values.put("steelPrintId",steelPrintId);
                if(integer<9){
                    int i = (integer + 1);
                    values.put("produceNumber","S"+proNumBer+format1+"0000"+i);
                }else if (integer<99){
                    int i = (integer + 1);
                    values.put("produceNumber","S"+proNumBer+format1+"000"+i);
                }else if (integer<999){
                    int i = (integer + 1);
                    values.put("produceNumber","S"+proNumBer+format1+"00"+i);
                }else if (integer<9999){
                    int i = (integer + 1);
                    values.put("produceNumber","S"+proNumBer+format1+"0"+i);
                }else if (integer<99999){
                    int i = (integer + 1);
                    values.put("produceNumber","S"+proNumBer+format1+i);
                }
                steelPrintMapper.produceInsert(values);
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("produceNumber",values.get("produceNumber").toString());
                arrayList.add(hashMap);
//                try {
//                    test(values.get("produceNumber").toString(),steelPrint);
//                } catch (UnsupportedEncodingException e) {
//                    e.printStackTrace();
//                }
                integer++;
            }
        }else {
            // s + 000  项目  + 日期8位  + 后 5位
            for (int i=1 ; i<=printNum;i++){
                HashMap<String, Object> values = new HashMap<>();
                if(i<10){
                    values.put("produceNumber","S"+proNumBer+format1+"0000"+i);
                }else  if(i<100){
                    values.put("produceNumber","S"+proNumBer+format1+"000"+i);
                }else  if(i<1000){
                    values.put("produceNumber","S"+proNumBer+format1+"00"+i);
                }else  if(i<10000){
                    values.put("produceNumber","S"+proNumBer+format1+"0"+i);
                }else  if(i<10000){
                    values.put("produceNumber","S"+proNumBer+format1+i);
                }
                values.put("steelProduceId","");
                values.put("proId",steelPrint.getProId());
                values.put("reinforcementId",steelPrint.getReinforcementId());
                values.put("blockNum",steelPrint.getBlockNum());
                values.put("createUser",steelPrint.getCreateUser());
                values.put("sizeId",steelPrint.getSizeId());
                values.put("steelPrintId",steelPrintId);
                steelPrintMapper.produceInsert(values);
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("produceNumber",values.get("produceNumber").toString());
                arrayList.add(hashMap);
//                try {
//                    test(values.get("produceNumber").toString(),steelPrint);
//                } catch (UnsupportedEncodingException e) {
//                    e.printStackTrace();
//                }
            }
        }
//        for (int i=0 ; i<printNum;i++){
//           // dayin();
//            try {
//                test();
//            } catch (UnsupportedEncodingException e) {
//                e.printStackTrace();
//            }
//        }
        ///缺少 打印信息
        //项目添加
        return BasicResult.success(arrayList);
    }
 
    private void  dayin(){
        try {
            PrintService psZebra = null;
            String sPrinterName = null;
            PrintService[] services = PrinterJob.lookupPrintServices();
//            PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
//            PrintService[] pss = PrinterJob.lookupPrintServices();
            System.out.println(services.length);
            for (int i = 0; i < services.length; i++) {
 
                PrintServiceAttribute attr = services[i].getAttribute(PrinterName.class);
                sPrinterName = ((PrinterName) attr).getValue();
                System.out.println(  sPrinterName = ((PrinterName) attr).getValue());
 
                if (sPrinterName.indexOf("Gprinter GP-H430F") >= 0) {
                    System.out.println("我进来来");
                    psZebra = services[i];
                    System.out.println(psZebra);
                    break;
                }
            }
 
            if (psZebra == null) {
                System.out.println("Zebra printer not found.");
                return;
            }
            DocPrintJob job = psZebra.createPrintJob();
 
           // String s = "${^XA^FO100,100^BY7^BCN,100,Y,N,N^FD123456^FS^XZ}$";
            //^FO{1},{2}^A" + Font + "{3},{4},{5}^FD{0}^FS
//            String erwei = "${^FO50,260^BY1,2^BCN,100,Y,N^FDSMJH2000544610^FS}$";
//            String command = "QRCODE 300,70,L,6,A,0,M2,S3,\"https://www.baidu.com/\"";// 打印二维码
            String erwei = "${^XA^FO20,20^BQ,2,10^FDLA,0123456789ABCD 2D code^FS^XZ}$";
//            byte[] by = s.getBytes();
//            DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
//            Doc doc = new SimpleDoc(by, flavor, null);
//            job.print(doc, null);
 
            byte[] bytes = erwei.getBytes();
            DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
            Doc doc1 = new SimpleDoc(bytes, flavor, null);
            job.print(doc1, null);
 
        } catch (PrintException e) {
            e.printStackTrace();
        }
    }
 
    public interface TscLibDll extends StdCallLibrary {
        TscLibDll INSTANCE = (TscLibDll) Native.loadLibrary("TSCLIB", TscLibDll.class);
        int about();
        int openport(String pirnterName);
        int closeport();
        int sendcommand(String printerCommand);
        int setup(String width, String height, String speed, String density, String sensor, String vertical, String offset);
        int downloadpcx(String filename, String image_name);
        int barcode(String x, String y, String type, String height, String readable, String rotation, String narrow, String wide, String code);
        int printerfont(String x, String y, String fonttype, String rotation, String xmul, String ymul, String text);
        int clearbuffer();
        int printlabel(String set, String copy);
        int formfeed();
        int nobackfeed();
        int windowsfont(int x, int y, int fontheight, int rotation, int fontstyle, int fontunderline, String szFaceName, String content);
    }
 
    public static void test(String produceNumber,TSteelPrint steelPrint) throws UnsupportedEncodingException {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format1 = format.format(date);
        System.setProperty("jna.encoding", "GBK");// 支持中文
        // TscLibDll.INSTANCE.about();
//            TscLibDll.INSTANCE.openport("TSC TTP-244 Pro");
        TscLibDll.INSTANCE.openport("TSC TTP-244 Pro");
        // TscLibDll.INSTANCE.downloadpcx("C:\\UL.PCX", "UL.PCX");// 打印图片时需要先下载到设备
        // TscLibDll.INSTANCE.sendcommand("REM ***** This is a test by JAVA. *****");
        TscLibDll.INSTANCE.setup("80", "60", "3", "15", "0", "2", "0");
        TscLibDll.INSTANCE.sendcommand("SET TEAR ON");
        TscLibDll.INSTANCE.clearbuffer();
//        String command = "QRCODE 300,70,L,6,A,0,M2,S3,\""+produceNumber+"\"";// 打印二维码
        String command = "QRCODE 1,2,L,10,A,0,M2,S3,\""+produceNumber+"\"";// 打印二维码
        TscLibDll.INSTANCE.sendcommand(command);
        // TscLibDll.INSTANCE.sendcommand("PUTPCX 550,10,\"UL.PCX\"");// 图片位置
        // TscLibDll.INSTANCE.printerfont("100", "50", "TSS24.BF2", "0", "1", "1", "Technology");
        // TscLibDll.INSTANCE.barcode("70", "140", "128", "90", "0", "0", "2", "2", "A123456789");// 打印内容,参数是位置和字体
//        TscLibDll.INSTANCE.windowsfont(15, 15, 32, 0, 2, 0, "Arial", "尺寸:"+steelPrint.getSizeName());
//        TscLibDll.INSTANCE.windowsfont(30, 90, 32, 0, 2, 0, "Arial", "配筋:"+steelPrint.getReinforcementName());
        TscLibDll.INSTANCE.windowsfont(230, 1, 40, 0, 2, 0, "Arial", "钢筋笼信息牌");
        TscLibDll.INSTANCE.windowsfont(230, 45, 32, 0, 2, 0, "Arial", "项目名称:"+steelPrint.getProName());
        TscLibDll.INSTANCE.windowsfont(230, 90, 32, 0, 2, 0, "Arial", "尺寸:"+steelPrint.getSizeName());
        TscLibDll.INSTANCE.windowsfont(230, 120, 32, 0, 2, 0, "Arial", "配筋:"+steelPrint.getReinforcementName());
        TscLibDll.INSTANCE.windowsfont(230, 150, 32, 0, 2, 0, "Arial", "块号:"+steelPrint.getBlockName());
        TscLibDll.INSTANCE.windowsfont(230, 180, 32, 0, 2, 0, "Arial", "制作人:"+steelPrint.getRealName());
        TscLibDll.INSTANCE.windowsfont(230, 210, 32, 0, 2, 0, "Arial", "班组:"+steelPrint.getGroupName());
        TscLibDll.INSTANCE.windowsfont(230, 240, 32, 0, 2, 0, "Arial", "打印时间:"+format1);
        TscLibDll.INSTANCE.printlabel("1", "1");
        TscLibDll.INSTANCE.closeport();
    }
 
    public static void main(String[] args) {
        String s = "s0012023050500001";
        String substring = s.substring(12, 17);
        System.out.println(substring);
        String i = "000000000000000000";
        Integer integer = Integer.valueOf(i);
        String substring1 = i.substring(12, 17);
        Integer integer1 = Integer.valueOf(substring1);
 
        System.out.println(substring1+"---"+integer1);
 
    }
 
}