王佳豪
2021-06-24 428ac06951c28841ef2a3a73ad72282c9f8f2f60
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
package com.slcf.filter;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
 
import javax.annotation.Resource;
 
import org.activiti.engine.impl.transformer.IntegerToString;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
 
import com.slcf.bean.AllBean;
import com.slcf.pojo.WaitPakInBean;
import com.slcf.pojo.WaitPakOutBean;
import com.slcf.service.WaitPakInService;
import com.slcf.service.WaitPakOutService;
import com.slcf.service.impl.WaitPakInServiceImpl;
import com.slcf.util.Constants;
import com.slcf.util.SpringBeanUtils;
 
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import sun.misc.BASE64Encoder;
 
/**
 * 回报,处理与sap交互数据
 * @author admin
 * @Date 2020-02-17
 */
//@Service
public class UpdateThread extends Thread {//implements Runnable{
//    @Resource
//    WaitPakInService waitPakInService;
//    @Resource
//    WaitPakOutService waitPakOutService;
//    @Resource
//    private static WaitPakInServiceImpl waitPakInService;
    
    Logger logger=Logger.getLogger(UpdateThread.class);
    
    public static int count=0;
    
    private WaitPakInService waitPakInService;
    public WaitPakInService getWaitPakInService() {
        return waitPakInService;
    }
    public void setWaitPakInService(WaitPakInService waitPakInService) {
        this.waitPakInService = waitPakInService;
    }
 
    private WaitPakOutService waitPakOutService;
    public WaitPakOutService getWaitPakOutService() {
        return waitPakOutService;
    }
    public void setWaitPakOutService(WaitPakOutService waitPakOutService) {
        this.waitPakOutService = waitPakOutService;
    }
    
    public void run() {
        waitPakInService = getWaitPakInService();
        waitPakOutService = getWaitPakOutService();
        while(true) {
            try {
                updateWaitPakInData();
                sendPickPrintCmd();
                updateWaitPakOutData();
//                updateWaitCheckData();
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    
    /**
     * 处理入库通知档数据
     */
    private void updateWaitPakInData() {
        try {
//            logger.info("waitIn:" + count++);
            WaitPakInBean waitPakInBean = waitPakInService.queryWaitPakInFnh();
            if(waitPakInBean!=null) {
                String httpUrl = Constants.SAP_URL + "/sap/opu/odata/SAP/Z_WM_H5_SRV/AsrsTrSet";
                int result = sendPakInStatus(httpUrl,waitPakInBean);
            }
        }catch(Exception e) {
            logger.error("SAP Error updateWaitPakInData---" + e.getMessage());
//            System.out.println(e.getMessage());
        }
    }
    
    /**
     * 入库完成,调用sap接口上传完成信息
     */
    private int sendPakInStatus(String httpUrl,WaitPakInBean waitPakIn) {
        int result = 0;
//        String token = getXCsrfToken(httpUrl);
        String input = Constants.SAP_USER + ":" + Constants.SAP_PASSWORD;//"ZHANGX:654321";
        BASE64Encoder base = new BASE64Encoder();
        String encodedPassword;
        String token="";
        String session_value="";
        
        HttpURLConnection connection1 = null;
        try {
            encodedPassword = base.encode(input.getBytes("UTF-8"));
            URL url1 = new URL(httpUrl);
            connection1 = (HttpURLConnection) url1.openConnection();
            connection1.setDoOutput(true);
            connection1.setDoInput(true);
            connection1.setConnectTimeout(30000);
            connection1.setReadTimeout(30000);
            connection1.setRequestMethod("GET");
            connection1.setRequestProperty("Authorization", "Basic " + encodedPassword);
            connection1.setRequestProperty("x-csrf-token","Fetch");
           
            
            String sessionCookie = connection1.getHeaderField("Set-Cookie");
            if(sessionCookie==null || sessionCookie.length()<1)
            {
                logger.error("SAP ERROR-----调用上架完成接口,get请求获取cookie失败");
                return result;
            }
            String[] sessionId = sessionCookie.split(";");
            session_value = sessionId[0];
            token = connection1.getHeaderField("x-csrf-token");
//            Map<String, List<String>> map = connection1.getHeaderFields();
//            token = map.get("x-csrf-token").toString();
        } catch (Exception e1) {
            logger.error("SAP ERROR-----调用上架完成接口1,get请求获取cookie失败。" + e1.getMessage());
//            e1.printStackTrace();
        }finally {
            if(connection1!=null){
                connection1.disconnect();
            }
        }
        
        try {
            JSONObject jsObj = new JSONObject();
            JSONArray arr = new JSONArray();
            for(int i=0;i<1;i++) {
                JSONObject json = new JSONObject();
                json.put("lgnum",waitPakIn.getLgnum());
                json.put("tbnum",Long.toString(waitPakIn.getTbnum()));
                json.put("tbpos",Integer.toString(waitPakIn.getTbpos()));
                json.put("zmatid",waitPakIn.getZmatid());
                
                json.put("anfme",Double.toString(waitPakIn.getAnfme()));
                json.put("altme",waitPakIn.getAltme());
                json.put("nltyp","A01");
                json.put("nlber","001");
                json.put("nlpla",waitPakIn.getLoc_no());
                arr.add(json);
            }
            jsObj.put("item", arr);
            byte[] buff = jsObj.toString().getBytes();
            int buffLen = buff.length;
            //创建连接
            HttpURLConnection connection = null;
            try {
//                String input = "ZHANGX:654321";
//                BASE64Encoder base = new BASE64Encoder();
//                String encodedPassword = base.encode(input.getBytes("UTF-8"));
                
                URL url = new URL(httpUrl);
                connection = (HttpURLConnection) url.openConnection();
                connection.setDoOutput(true);
                connection.setDoInput(true);
                connection.setRequestMethod("POST");
                connection.setConnectTimeout(30000);
                connection.setReadTimeout(30000);
//                connection.setUseCaches(false);            
//                connection.setInstanceFollowRedirects(true);
//                connection.setRequestProperty("Content-Type","text/json;charset=gb2312");
                connection.setRequestProperty("Cookie", session_value);
                connection.setRequestProperty("Content-Type","application/json");
                connection.setRequestProperty("Accept","application/json");
                connection.setRequestProperty("Accept-Language","zh-CN,zh;q=0.9,en;q=0.8");
                connection.setRequestProperty("Content-Length","" + buffLen);
//                connection.setRequestProperty("Authorization", "Basic " + encodedPassword);
                connection.setRequestProperty("x-csrf-token",token);
                
                //POST请求
                OutputStream out = connection.getOutputStream();
                out.write(buff);
                out.close();
                
                //读取响应
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        connection.getInputStream()));
                String lines;
                StringBuffer sb = new StringBuffer("");
                while ((lines = reader.readLine()) != null) {
                    lines = new String(lines.getBytes(), "utf-8");
                    sb.append(lines);
                }
//                logger.error("SAP INFO-----调用上架完成接口,返回JSON数据:" + sb.toString());
//                System.out.println(sb.toString());
                JSONObject json = JSONObject.fromObject(sb.toString());
                String d = json.get("d").toString();
                JSONObject jsonD = JSONObject.fromObject(d);
                String Item = jsonD.get("item").toString();
                JSONObject jsonItem = JSONObject.fromObject(Item);
                String results = jsonItem.get("results").toString();
                JSONArray array = JSONArray.fromObject(results);
                
                if(array.size()>0) {
                    for(int i=0;i<array.size();i++){
                        JSONObject job = array.getJSONObject(i);
                        String zstatus = job.get("zstatus").toString();
//                        String message = job.get("message").toString();
                        String message="";
                        try {
                            message = java.net.URLDecoder.decode(job.get("message").toString(), "utf-8");
//                            logger.error("SAP TEST-----数据:" + message);
                        }catch(Exception em) {
                        }
                        if(zstatus.equals("s") || message.contains("已经完全提供")) {//上报SAP成功,转历史档
//                        if(zstatus.equals("s")) {
                            waitPakIn.setIo_status("Y");
                            waitPakInService.upWaitPakIn(waitPakIn);
                            waitPakInService.moveToLog(waitPakIn);
                        }else if(zstatus.equals("e")) {//上报SAP失败,更新io_status=X
                            waitPakIn.setIo_status("X");
                            waitPakIn.setMemo(message);
                            waitPakInService.upWaitPakIn(waitPakIn);
                        }
//                        System.out.println("zstatus信息.." + zstatus);
//                        System.out.println("message信息.." + message);
                    }
                }
                result = 1;
            }catch (Exception e) {
                waitPakIn.setIo_status("X");
                waitPakIn.setMemo(e.getMessage());
                waitPakInService.upWaitPakIn(waitPakIn);
                logger.error("SAP Error sendPakInStatus--"+waitPakIn.getMatnr()+"---" + e.getMessage());
//                System.out.println(e.getMessage());
            }finally {
                if(connection!=null){
                    connection.disconnect();
                }
            }
        }catch (Exception e) {
            waitPakIn.setIo_status("X");
            waitPakIn.setMemo(e.getMessage());
            waitPakInService.upWaitPakIn(waitPakIn);
            logger.error("SAP Error sendPakInStatus--"+waitPakIn.getMatnr()+"---" + e.getMessage());
//            System.out.println(e.getMessage());
        }
        return result;
    }
    
    /**
     * 处理出库通知档数据
     */
    private void updateWaitPakOutData() {
        try {
//            logger.info("waitOut:" + count++);
            WaitPakOutBean waitPakOutBean = waitPakOutService.queryWaitPakOutFnh();
            if(waitPakOutBean!=null) {
                String httpUrl = Constants.SAP_URL + "/sap/opu/odata/SAP/Z_WM_H5_SRV/ToConHeadSet";
                int result = sendPakOutStatus(httpUrl,waitPakOutBean);
            }
        }catch(Exception e) {
            logger.error("SAP Error updateWaitPakOutData---" + e.getMessage());
        }
    }
    
    /**
     * 出库完成,调用sap接口上传完成信息
     */
    private int sendPakOutStatus(String httpUrl,WaitPakOutBean waitPakOut) {
        int result = 0;
//        String token = getXCsrfToken(httpUrl);
        String input = Constants.SAP_USER + ":" + Constants.SAP_PASSWORD;//"ZHANGX:654321";
        BASE64Encoder base = new BASE64Encoder();
        String encodedPassword;
        String token="";
        String session_value="";
        
        HttpURLConnection connection1 = null;
        try {
            encodedPassword = base.encode(input.getBytes("UTF-8"));
            URL url1 = new URL(httpUrl);
            connection1 = (HttpURLConnection) url1.openConnection();
            connection1.setDoOutput(true);
            connection1.setDoInput(true);
            connection1.setConnectTimeout(30000);
            connection1.setReadTimeout(30000);
            connection1.setRequestMethod("GET");
            connection1.setRequestProperty("Authorization", "Basic " + encodedPassword);
            connection1.setRequestProperty("x-csrf-token","Fetch");
            String sessionCookie = connection1.getHeaderField("Set-Cookie");
            if(sessionCookie==null || sessionCookie.length()<1)
            {
                logger.error("调用下架完成接口,get请求获取cookie失败");
                return result;
            }
            String[] sessionId = sessionCookie.split(";");
            session_value = sessionId[0];
            token = connection1.getHeaderField("x-csrf-token");
            
//            Map<String, List<String>> map = connection1.getHeaderFields();
//            token = map.get("x-csrf-token").toString();
        } catch (Exception e1) {
            logger.error("调用下架完成接口,get请求获取cookie失败" + e1.getMessage());
//            e1.printStackTrace();
        }finally{
            if(connection1!=null){
                connection1.disconnect();
            }
        }
        
        try {
            JSONObject jsObj = new JSONObject();
            JSONArray arr = new JSONArray();
            for(int i=0;i<1;i++) {
                JSONObject json = new JSONObject();
                json.put("lgnum",waitPakOut.getLgnum());
                json.put("tanum",Long.toString(waitPakOut.getTanum()));
                json.put("tapos",Integer.toString(waitPakOut.getTapos()));
                
                double nista =  waitPakOut.getNista();
//                if(nista>0)                 
                if(nista>=-1 && (int)nista!=0) 
                {
                    double vsolm = waitPakOut.getVsolm();
                    if( (int)nista == -1 ) {
                        nista=0;
                    }
                    double ndifa = vsolm - nista;
                    json.put("nista",Double.toString(nista));  //实际数量
                    json.put("ndifa",Double.toString(ndifa));  //差异数量
                }
                json.put("altme",waitPakOut.getMeins());
                json.put("nltyp","A01");
//                json.put("nlpla",waitPakOut.getNlpla());
                json.put("arbpl","A01-01");
                arr.add(json);
            }
            int action = 3;
            if(waitPakOut.getAction()==1 && waitPakOut.getPrnstatus()==2) {
                action = 1;
            }
            jsObj.put("trart", Integer.toString(action));
            jsObj.put("item", arr);
            byte[] buff = jsObj.toString().getBytes();
            int buffLen = buff.length;
            //创建连接
            HttpURLConnection connection = null;
            try {
//                String input = "ZHANGX:654321";
//                BASE64Encoder base = new BASE64Encoder();
//                String encodedPassword = base.encode(input.getBytes("UTF-8"));
                
                URL url = new URL(httpUrl);
                connection = (HttpURLConnection) url.openConnection();
                connection.setDoOutput(true);
                connection.setDoInput(true);
                connection.setConnectTimeout(30000);
                connection.setReadTimeout(30000);
                connection.setRequestMethod("POST");
        //        connection.setUseCaches(false);            
        //        connection.setInstanceFollowRedirects(true);
//                connection.setRequestProperty("Content-Type","text/json;charset=gb2312");
                connection.setRequestProperty("Cookie", session_value);
                connection.setRequestProperty("Content-Type","application/json");
                connection.setRequestProperty("Accept","application/json");
                connection.setRequestProperty("Accept-Language","zh-CN,zh;q=0.9,en;q=0.8");
                connection.setRequestProperty("Content-Length","" + buffLen);
//                connection.setRequestProperty("Authorization", "Basic " + encodedPassword);
                connection.setRequestProperty("x-csrf-token",token);
                
                //POST请求
                OutputStream out = connection.getOutputStream();
                out.write(buff);
                out.close();
                
                //读取响应
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        connection.getInputStream()));
                String lines;
                StringBuffer sb = new StringBuffer("");
                while ((lines = reader.readLine()) != null) {
                    lines = new String(lines.getBytes(), "utf-8");
                    sb.append(lines);
                }
//                System.out.println(sb.toString());
                JSONObject json = JSONObject.fromObject(sb.toString());
                String d = json.get("d").toString();
                JSONObject jsonD = JSONObject.fromObject(d);
                String Item = jsonD.get("item").toString();
                JSONObject jsonItem = JSONObject.fromObject(Item);
                String results = jsonItem.get("results").toString();
                JSONArray array = JSONArray.fromObject(results);
                
                if(array.size()>0) {
                    for(int i=0;i<array.size();i++){
                        JSONObject job = array.getJSONObject(i);
                        String zstatus = job.get("zstatus").toString();
//                        String message = job.get("message").toString();
                        String message="";
                        try {
                            message = java.net.URLDecoder.decode(job.get("message").toString(), "utf-8");
                        }catch(Exception em) {
                        }
                        if(zstatus.equals("s") || message.contains("已经完全提供") || message.contains("已确认")) {
//                        if(zstatus.equals("s")) {//上报SAP成功,转历史档
                            waitPakOut.setIo_status("Y");
                            waitPakOutService.upWaitPakOut(waitPakOut,0);
                            waitPakOutService.moveToLog(waitPakOut);
                        }else if(zstatus.equals("e")) {//上报SAP失败,更新io_status=X
                            waitPakOut.setIo_status("X");
                            waitPakOut.setMemo(message);
                            waitPakOutService.upWaitPakOut(waitPakOut,0);
                            logger.error("SAP返回异常时,ASRS提交的json--" + jsObj.toString());
                        }
//                        System.out.println("zstatus信息.." + zstatus);
//                        System.out.println("message信息.." + message);
                    }
                }
                result = 1;
            }catch (Exception e) {
                waitPakOut.setIo_status("X");
                waitPakOut.setMemo(e.getMessage());
                waitPakOutService.upWaitPakOut(waitPakOut,0);
                logger.error("出库通知档回报异常--"+waitPakOut.getMatnr() + "---" + e.getMessage());
//                System.out.println(e.getMessage());
            }finally{
                if(connection!=null){
                    connection.disconnect();
                }
            }
        }catch (Exception e) {
            waitPakOut.setIo_status("X");
            waitPakOut.setMemo(e.getMessage());
            waitPakOutService.upWaitPakOut(waitPakOut,0);
            logger.error("出库通知档回报异常--"+waitPakOut.getMatnr() + "---" + e.getMessage());
//            System.out.println(e.getMessage());
        }
        return result;
    }
    
    /**
     * 处理打印标签需求
     */
    private void sendPickPrintCmd() {
        try {
            WaitPakOutBean waitPakOutBean = waitPakOutService.getWaitPakOutPrint();
            if(waitPakOutBean!=null) {
                String httpUrl = Constants.SAP_URL + "/sap/opu/odata/SAP/Z_WM_H5_SRV/ToConHeadSet";
                int result = sendPrinCommand(httpUrl,waitPakOutBean);
            }
        }catch(Exception e) {
            logger.error("SAP Error updateWaitPakOutData---" + e.getMessage());
        }
    }
    
    /**
     * 拣料站,调用SAP打印标签接口,action=2
     */
    private int sendPrinCommand(String httpUrl,WaitPakOutBean waitPakOut) {
        int result = 0;
//        String token = getXCsrfToken(httpUrl);
        String input = Constants.SAP_USER + ":" + Constants.SAP_PASSWORD;//"ZHANGX:654321";
        BASE64Encoder base = new BASE64Encoder();
        String encodedPassword;
        String token="";
        String session_value="";
        
        HttpURLConnection connection1 = null;
        try {
//            logger.error("sendPrinCommand-----调用打印接口" + waitPakOut.getMaktx());
            encodedPassword = base.encode(input.getBytes("UTF-8"));
            URL url1 = new URL(httpUrl);
            connection1 = (HttpURLConnection) url1.openConnection();
            connection1.setDoOutput(true);
            connection1.setDoInput(true);
            connection1.setConnectTimeout(30000);
            connection1.setReadTimeout(30000);
            connection1.setRequestMethod("GET");
            connection1.setRequestProperty("Authorization", "Basic " + encodedPassword);
            connection1.setRequestProperty("x-csrf-token","Fetch");
            String sessionCookie = connection1.getHeaderField("Set-Cookie");
            if(sessionCookie==null || sessionCookie.length()<1)
            {
                logger.error("调用打印接口,get请求获取cookie失败");
                return result;
            }
            String[] sessionId = sessionCookie.split(";");
            session_value = sessionId[0];
            token = connection1.getHeaderField("x-csrf-token");
//            Map<String, List<String>> map = connection1.getHeaderFields();
//            token = map.get("x-csrf-token").toString();
        } catch (Exception e1) {
            logger.error("sendPrinCommand1-----调用打印接口请求cookie失败" + waitPakOut.getMaktx() + e1.getMessage());
            e1.printStackTrace();
        }finally{
            if(connection1!=null){
                connection1.disconnect();
            }
        }
        
        try {
//            logger.error("sendPrinCommand2-----开始调用打印接口" + waitPakOut.getMaktx());
            JSONObject jsObj = new JSONObject();
            JSONArray arr = new JSONArray();
            for(int i=0;i<1;i++) {
                JSONObject json = new JSONObject();
                json.put("lgnum",waitPakOut.getLgnum());
                json.put("tanum",Long.toString(waitPakOut.getTanum()));
                json.put("tapos",Integer.toString(waitPakOut.getTapos()));
                
                double nista =  waitPakOut.getNista();
                if(nista>0) 
                {
                    double vsolm = waitPakOut.getVsolm();
                    double ndifa = vsolm - nista;
                    json.put("nista",Double.toString(nista));  //实际数量
                    json.put("ndifa",Double.toString(ndifa));  //差异数量
                }
                json.put("altme",waitPakOut.getMeins());
                json.put("nltyp","A01");
//                json.put("nlpla",waitPakOut.getNlpla());
                json.put("arbpl","A01-01");
                arr.add(json);
            }
            jsObj.put("trart", "2");
            jsObj.put("item", arr);
            byte[] buff = jsObj.toString().getBytes();
            int buffLen = buff.length;
            //创建连接
            HttpURLConnection connection = null;
            try {
//                String input = "ZHANGX:654321";
//                BASE64Encoder base = new BASE64Encoder();
//                String encodedPassword = base.encode(input.getBytes("UTF-8"));
                
                URL url = new URL(httpUrl);
                connection = (HttpURLConnection) url.openConnection();
                connection.setDoOutput(true);
                connection.setDoInput(true);
                connection.setConnectTimeout(30000);
                connection.setReadTimeout(30000);
                connection.setRequestMethod("POST");
        //        connection.setUseCaches(false);            
        //        connection.setInstanceFollowRedirects(true);
//                connection.setRequestProperty("Content-Type","text/json;charset=gb2312");
                connection.setRequestProperty("Cookie", session_value);
                connection.setRequestProperty("Content-Type","application/json");
                connection.setRequestProperty("Accept","application/json");
                connection.setRequestProperty("Accept-Language","zh-CN,zh;q=0.9,en;q=0.8");
                connection.setRequestProperty("Content-Length","" + buffLen);
//                connection.setRequestProperty("Authorization", "Basic " + encodedPassword);
                connection.setRequestProperty("x-csrf-token",token);
                
                //POST请求
                OutputStream out = connection.getOutputStream();
                out.write(buff);
                out.close();
//                logger.error("sendPrinCommand3-----调用打印接口" + waitPakOut.getMaktx());
                //读取响应
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        connection.getInputStream()));
                String lines;
                StringBuffer sb = new StringBuffer("");
                while ((lines = reader.readLine()) != null) {
                    lines = new String(lines.getBytes(), "utf-8");
                    sb.append(lines);
                }
//                System.out.println(sb.toString());
                JSONObject json = JSONObject.fromObject(sb.toString());
                String d = json.get("d").toString();
                JSONObject jsonD = JSONObject.fromObject(d);
                String Item = jsonD.get("item").toString();
                JSONObject jsonItem = JSONObject.fromObject(Item);
                String results = jsonItem.get("results").toString();
                JSONArray array = JSONArray.fromObject(results);
                logger.error("sendPrinCommand4-----调用打印接口" + waitPakOut.getMaktx());
                if(array.size()>0) {
                    for(int i=0;i<array.size();i++){
                        JSONObject job = array.getJSONObject(i);
                        String zstatus = job.get("zstatus").toString();
//                        String message = job.get("message").toString();
                        String message="";
                        try {
                            message = java.net.URLDecoder.decode(job.get("message").toString(), "utf-8");
                        }catch(Exception em) {
                        }
                        if(zstatus.equals("s") || message.contains("已经完全提供")) {
//                        if(zstatus.equals("s")) {//上报SAP成功,转历史档
                            waitPakOut.setAction(1);     //模式变为1单独确认
                            waitPakOut.setPrnstatus(2);  //打印完成
                            waitPakOutService.upWaitPakOut(waitPakOut,0);
                            logger.error("sendPrinCommand5---调用打印接口成功,请求json数据--" + jsObj.toString());
                            logger.info("sendPrinCommand5---调用打印接口成功,返回json数据--" + sb.toString());
                        }else if(zstatus.equals("e")) {//上报SAP失败,更新io_status=X
                            waitPakOut.setPrnstatus(3);     //打印失败
                            waitPakOut.setMemo(message);
                            waitPakOutService.upWaitPakOut(waitPakOut,0);
                            logger.error("sendPrinCommand6---调用打印接口失败,请求json数据--" + message);
                        }
                    }
                }
                result = 1;
            }catch (Exception e) {
                waitPakOut.setPrnstatus(3);     //打印失败
                waitPakOut.setMemo(e.getMessage());
                waitPakOutService.upWaitPakOut(waitPakOut,0);
                logger.error("sendPrinCommand7---调用打印接口回报异常--"+waitPakOut.getMatnr() + "---" + e.getMessage());
//                System.out.println(e.getMessage());
            }finally{
                if(connection!=null){
                    connection.disconnect();
                }
            }
        }catch (Exception e) {
            waitPakOut.setPrnstatus(3);     //打印失败
            waitPakOut.setMemo(e.getMessage());
            waitPakOutService.upWaitPakOut(waitPakOut,0);
            logger.error("sendPrinCommand8---调用打印接口回报异常--"+waitPakOut.getMatnr() + "---" + e.getMessage());
//            System.out.println(e.getMessage());
        }
        return result;
    }
    
    /**
     * 处理盘点通知档数据
     */
    private void updateWaitCheckData() {
        try {
//            logger.info("waitCheck:" + count++);
        }catch(Exception e) {
            
        }
    }
    
    public static void main(String[] args) {
        UpdateThread Thread1=new UpdateThread();
        Thread mThread1=new Thread(Thread1,"线程1");
//        Thread mThread2=new Thread(Thread1,"线程2");
//        Thread mThread3=new Thread(Thread1,"线程3");
        mThread1.start();
//        mThread2.start();
//        mThread3.start();
    }
}