春风项目四线(合箱线、总装线)
wujian
2024-07-12 d2daac02fbc20980162799fc7bf6b1ca59f5686a
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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
package com.jcdm.main.plcserver.sub;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.jcdm.common.core.domain.BaseEntity;
import com.jcdm.main.bs.domain.BsTechnologyRouteChildInfo;
import com.jcdm.main.bs.orderScheduling.domain.BsOrderScheduling;
import com.jcdm.main.bs.orderScheduling.service.IBsOrderSchedulingService;
import com.jcdm.main.bs.technologyRouteChild.service.IBsTechnologyRouteChildInfoService;
import com.jcdm.main.da.collectionParamConf.domain.DaCollectionParamConf;
import com.jcdm.main.da.collectionParamConf.service.IDaCollectionParamConfService;
import com.jcdm.main.da.opcuaconfig.domain.DaOpcuaConfig;
import com.jcdm.main.da.paramCollection.domain.DaParamCollection;
import com.jcdm.main.da.paramCollection.service.IDaParamCollectionService;
import com.jcdm.main.da.passingStationCollection.domain.DaPassingStationCollection;
import com.jcdm.main.da.passingStationCollection.domain.ProductNewPassStation;
import com.jcdm.main.da.passingStationCollection.service.ProductNewPassStationService;
import com.jcdm.main.da.passingStationCollection.service.impl.DaPassingStationCollectionServiceImpl;
import com.jcdm.main.plcserver.conf.OPCElement;
import com.jcdm.main.rm.repairRecord.domain.RmRepairRecord;
import com.jcdm.main.rm.repairRecord.service.IRmRepairRecordService;
import com.kangaroohy.milo.model.ReadWriteEntity;
import com.kangaroohy.milo.runner.subscription.SubscriptionCallback;
import com.kangaroohy.milo.service.MiloService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
 
import java.math.BigDecimal;
import java.sql.Time;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
 
 
@Slf4j
@Component
public class OPCUaSubscription implements SubscriptionCallback {
 
    private static final Logger logger = LoggerFactory.getLogger("sys-user");
    public static MiloService miloService;
 
    public IDaCollectionParamConfService collectionParamConfService;
 
    public IDaParamCollectionService daParamCollectionService;
 
    public DaPassingStationCollectionServiceImpl passingStationCollectionServiceImpl;
 
    public List<DaOpcuaConfig> lists;
 
    public static final HashMap<String,Integer> map = new HashMap<>();
 
    public IBsOrderSchedulingService bsOrderSchedulingService;
 
    public IRmRepairRecordService rmRepairRecordService;
 
    public IBsTechnologyRouteChildInfoService bsTechnologyRouteChildInfoService;
    public ProductNewPassStationService productNewPassStationService;
 
    public HashMap<String,List<DaCollectionParamConf>> allCollectParamList = new HashMap<>();
 
    public OPCUaSubscription(MiloService miloService,
                             IDaCollectionParamConfService collectionParamConfService,
                             IDaParamCollectionService daParamCollectionService,
                             DaPassingStationCollectionServiceImpl passingStationCollectionServiceImpl,
                             IBsOrderSchedulingService bsOrderSchedulingService,
                             List<DaOpcuaConfig> lists,
                             IRmRepairRecordService rmRepairRecordService,
                             IBsTechnologyRouteChildInfoService bsTechnologyRouteChildInfoService,
                             ProductNewPassStationService productNewPassStationService) {
        OPCUaSubscription.miloService = miloService;
        this.collectionParamConfService = collectionParamConfService;
        this.daParamCollectionService = daParamCollectionService;
        this.passingStationCollectionServiceImpl = passingStationCollectionServiceImpl;
        this.bsOrderSchedulingService = bsOrderSchedulingService;
        this.lists = lists;
        this.rmRepairRecordService = rmRepairRecordService;
        this.bsTechnologyRouteChildInfoService = bsTechnologyRouteChildInfoService;
        this.productNewPassStationService = productNewPassStationService;
 
 
    }
    @Override
    public void onSubscribe(String identifier, Object value) {
        String ecpStr = "";//异常记录标记
        try {
            if(null != value && "1".equals(value.toString())) {
                //1、检索SN号
                //2、过站参数采集
                //3、扫码枪数据回传
                List<String> collect1 = lists.stream().filter(x -> OPCElement.SN_CHECK.equals(x.getrFunction()))
                        .map(DaOpcuaConfig::getNode).collect(Collectors.toList());
                List<String> collect2 = lists.stream().filter(x -> OPCElement.SAVE_DATA.equals(x.getrFunction()))
                        .map(DaOpcuaConfig::getNode).collect(Collectors.toList());
                if (collect1.contains(identifier)){
                    logger.info("-------监听到,{}的CODE_CHECK的信号",identifier);
                    //logger.info("-------time--------------,{}",LocalDateTime.now());
                    //sn
                    if (OPCElement.OP010_CODE_CHECK.equals(identifier)){
                        ReadWriteEntity entity = new ReadWriteEntity("CFL4HX.OP010.CodeCheckFeed", 1);
                        OPCUaSubscription.miloService.writeToOpcByte(entity);//写完成
                    } else if (OPCElement.OP020_CODE_CHECK.equals(identifier)) {
                        ReadWriteEntity entity = new ReadWriteEntity("CFL4HX.OP020.CodeCheckFeed", 1);
                        OPCUaSubscription.miloService.writeToOpcByte(entity);//写完成
                    } else if (OPCElement.OP030_CODE_CHECK.equals(identifier)) {
                        ReadWriteEntity entity = new ReadWriteEntity("CFL4HX.OP030.CodeCheckFeed", 1);
                        OPCUaSubscription.miloService.writeToOpcByte(entity);//写完成
                        Object SNCodeObject = miloService.readFromOpcUa("CFL4HX.OP030.Code").getValue();
                        if (ObjectUtil.isNotEmpty(SNCodeObject)){
                            String snCode = SNCodeObject.toString();
                            if (StrUtil.isNotBlank(snCode)){
                                String type = productNewFlag(snCode);
                                if ("280".equals(type) || "380".equals(type)){
                                    //获取箱体码
                                    ProductNewPassStation productNewPassStation = new ProductNewPassStation();
                                    productNewPassStation.setFinishFlag(OPCElement.UN_FINISH);
                                    productNewPassStation.setSfcCode(snCode);
                                    List<ProductNewPassStation> productPassStationList = productNewPassStationService.getProductPassStationList(productNewPassStation);
                                    if (CollUtil.isNotEmpty(productPassStationList)){
                                        List<ProductNewPassStation> collect = productPassStationList.stream().sorted(Comparator.comparing(ProductNewPassStation::getCreateTime).reversed()).collect(Collectors.toList());
                                        ProductNewPassStation productNewPassStation1 = collect.get(0);
                                        String boxCode = productNewPassStation1.getBoxCode();
                                        passingStationCollectionServiceImpl.sendMessage(type+"*"+boxCode,"OP030");
                                    }
 
                                }
                            }
 
                        }
                    } else if (OPCElement.OP040_CODE_CHECK.equals(identifier)) {
                        ReadWriteEntity entity = new ReadWriteEntity("CFL4HX.OP040.CodeCheckFeed", 1);
                        OPCUaSubscription.miloService.writeToOpcByte(entity);//写完成
                        //推送数据
                        log.info("进入OP040.CodeCheck");
                        tile2("CFL4HX.OP040.CodeCheck");
                    } else {
                        this.SNRetrieval(identifier,value.toString());
                        if (identifier.equals(OPCElement.OP120_ZZ_CODE_CHECK) && "1".equals(value.toString())){
                            //总装上线扫码传输数据
                            logger.info("-------监听到,{}的扫码枪扫码的CODE_CHECK的信号",identifier);
                            scannerGunMessage();
                        }
                    }
 
                }
                if (collect2.contains(identifier)){
                    if ("1".equals(value.toString())){
                        logger.info("-------监听到,{}的SAVE_REQUEST_LAST的信号",identifier);
                        //logger.info("-------time--------------,{}",LocalDateTime.now());
                        //HX线改装
                        if (OPCElement.OP010_REQUEST_PRODUCT_TYPE.equals(identifier)){
                           Object SNCodeObject = miloService.readFromOpcUa("CFL4HX.OP010.Code").getValue();
                           if (ObjectUtil.isNotEmpty(SNCodeObject)){
                               String snCode = SNCodeObject.toString();
                               ReadWriteEntity entity3 = new ReadWriteEntity("CFL4HX.OP010.ProductTypeSN",snCode);
                               OPCUaSubscription.miloService.writeToOpcUa(entity3);//写产品SN号反馈
                               ReadWriteEntity entity2 = new ReadWriteEntity("CFL4HX.OP010.SNDone", 1);
                               OPCUaSubscription.miloService.writeToOpcByte(entity2);//写完成
 
                           }
                        } else if (OPCElement.OP010_SAVE_REQUEST.equals(identifier)) {
                            ReadWriteEntity entity2 = new ReadWriteEntity("CFL4HX.OP010.SaveFeed", 1);
                            OPCUaSubscription.miloService.writeToOpcByte(entity2);//写完成
                        } else if (OPCElement.OP020_PRINT_REQUEST.equals(identifier)) {
                            Object SNCodeObject = miloService.readFromOpcUa("CFL4HX.OP020.Code").getValue();
                            if (ObjectUtil.isNotEmpty(SNCodeObject)){
                                String snCode = SNCodeObject.toString();
                                if (snCode.startsWith("380")){
                                    log.info("enter-380");
                                    String[] split = snCode.split(StrUtil.SPACE);
                                    if (split.length>1){
                                        ReadWriteEntity entity = new ReadWriteEntity("CFL4HX.OP020.PrintSN1", split[0]);
                                        OPCUaSubscription.miloService.writeToOpcUa(entity);//写完成
                                        ReadWriteEntity entity2 = new ReadWriteEntity("CFL4HX.OP020.PrintSN2", "☆"+split[1]+"☆");
                                        OPCUaSubscription.miloService.writeToOpcUa(entity2);//写完成
                                        ReadWriteEntity entity3 = new ReadWriteEntity("CFL4HX.OP020.SendSNDone", 1);
                                        OPCUaSubscription.miloService.writeToOpcByte(entity3);//写完成
                                    }
                                } else {
                                    ReadWriteEntity entity = new ReadWriteEntity("CFL4HX.OP020.PrintSN1", snCode);
                                    OPCUaSubscription.miloService.writeToOpcByte(entity);//写完成
                                    ReadWriteEntity entity3 = new ReadWriteEntity("CFL4HX.OP020.SendSNDone", 1);
                                    OPCUaSubscription.miloService.writeToOpcByte(entity3);//写完成
                                }
                            }
                        } else if (OPCElement.OP030_SAVE_REQUEST.equals(identifier)) {
                            cleanForm("CFL4HX.OP030.SaveFeed");
                            ReadWriteEntity entity2 = new ReadWriteEntity("CFL4HX.OP030.SaveFeed", 1);
                            OPCUaSubscription.miloService.writeToOpcByte(entity2);//写完成
 
                        } else if (OPCElement.OP040_SAVE_REQUEST.equals(identifier)) {
                            cleanForm040(OPCElement.OP040_SAVE_REQUEST);
                            ReadWriteEntity entity2 = new ReadWriteEntity("CFL4HX.OP040.SaveFeed", 1);
                            OPCUaSubscription.miloService.writeToOpcByte(entity2);//写完成
                        } else {
                            this.SaveData(identifier);
                        }
 
                    }
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            if (!"".equals(ecpStr)) {
                System.out.println(ecpStr + "\r\n");
            }
        }
    }
 
    private void scannerGunMessage() throws Exception {
        String[] parts = OPCElement.OP120_ZZ_CODE_CHECK.split("[.]");
        Object SNCodeObject = miloService.readFromOpcUa(parts[0] + "." + parts[1] + ".Code1").getValue();
        if (null == SNCodeObject){
            SNCodeObject = miloService.readFromOpcUa(parts[0] + "." + parts[1] + ".Code").getValue();
        }
        if (null != SNCodeObject){
            String SNCode = SNCodeObject.toString();
            passingStationCollectionServiceImpl.sendMessage(SNCode,"OP120");
        }
    }
 
    public void SNRetrieval(String Node, String value) throws Exception {
        String[] parts = Node.split("[.]");
        if(value.equals("1")) {
            //SN号检索
            Object SNCodeObject = miloService.readFromOpcUa(parts[0] + "." + parts[1] + ".Code").getValue();
            if(null != SNCodeObject) {
                String SNCode=SNCodeObject.toString();
//              String a=passingStationCollectionServiceImpl.SelectSN(SNCode,parts[1]);
                String a;
                BsOrderScheduling bsOrderSchedulingQuery = new BsOrderScheduling();
                bsOrderSchedulingQuery.setEngineNo(SNCode);
                List<BsOrderScheduling> queryOrderList = bsOrderSchedulingService.selectBsOrderSchedulingList(bsOrderSchedulingQuery);
                if (CollUtil.isNotEmpty(queryOrderList)){
                    a = "1";
                }else {
                    a = "4";
                }
                //如果是返修工位需要传输返修工位号
                List<DaPassingStationCollection> daPassingStationCollections = new ArrayList<>();
                List<RmRepairRecord> rmRepairRecords = new ArrayList<>();
                if (OPCElement.OP465_ZZ_CODE_CHECK.equals(Node) || OPCElement.OP355_ZZ_CODE_CHECK.equals(Node)
                        || OPCElement.OP695_ZZ_CODE_CHECK.equals(Node) || OPCElement.OP755_ZZ_CODE_CHECK.equals(Node)){
                    //过站记录
                    DaPassingStationCollection PassingStationCollection=new DaPassingStationCollection();
                    PassingStationCollection.setSfcCode(SNCode);
                    daPassingStationCollections = passingStationCollectionServiceImpl.selectDaPassingStationCollectionList(PassingStationCollection);
//                    if (CollUtil.isNotEmpty(daPassingStationCollections)){
//                        DaPassingStationCollection lastOne = daPassingStationCollections.get(daPassingStationCollections.size() - 1);
//                        String outRsSign = lastOne.getOutRsSign();
//                        if ("合格".equals(outRsSign)){
//                            a = "1";
//                        }else {
//                            a = "2";
//                        }
//                    }
                    RmRepairRecord rmRepairRecord = new RmRepairRecord();
                    rmRepairRecord.setBoxCode(SNCode);
                    rmRepairRecords = rmRepairRecordService.selectRmRepairRecordList(rmRepairRecord);
                    if (CollUtil.isNotEmpty(rmRepairRecords)){
                        a = "1";
                    }else {
                        a = "2";
                    }
                }
                logger.info("-----返回codeCheckFeed-----,{}",a);
//                String a="1";
                // 1:OK可生产 2:NG不可生产 3:NG可返工 4:PC检索失败(无记录)5:PC检索失败(软件)
                if (StrUtil.isNotBlank(a)){
                    int input = Integer.parseInt(a);
                    ReadWriteEntity entity = new ReadWriteEntity(parts[0]+"."+parts[1]+".CodeCheckFeed", input);
                    logger.info("发动机号:{},监听到{}的CodeCheck的信号",SNCode,Node);
                    miloService.writeToOpcByte(entity);
                    logger.info("监听到返回codecheckfeed信号,{}",entity);
                    //首站传输订单号
//                    if (OPCElement.OP050_HX_CODE_CHECK.equals(Node)){
//                        //查询订单号
//                        BsOrderScheduling bsOrderScheduling = new BsOrderScheduling();
//                        bsOrderScheduling.setEngineNo(SNCode);
//                        List<BsOrderScheduling> tempList = bsOrderSchedulingService.selectBsOrderSchedulingList(bsOrderScheduling);
//                        String orderNum = "";
//                        if (CollUtil.isNotEmpty(tempList)){
//                            BsOrderScheduling bsOrderScheduling1 = tempList.get(0);
//                            orderNum = bsOrderScheduling1.getOrderNo();
//                        }
//                        ReadWriteEntity entity2 = new ReadWriteEntity(parts[0]+"."+parts[1]+".OrderNumber", orderNum);
//                        miloService.writeToOpcUa(entity2);
//                    }
                    //如果是返修工位需要传输返修工位号
                    if (OPCElement.OP465_ZZ_CODE_CHECK.equals(Node) || OPCElement.OP355_ZZ_CODE_CHECK.equals(Node)
                    || OPCElement.OP695_ZZ_CODE_CHECK.equals(Node) || OPCElement.OP755_ZZ_CODE_CHECK.equals(Node)){
                        rework(SNCode,Node,daPassingStationCollections,rmRepairRecords);
                    }
                    if (OPCElement.OP050_CODE_CHECK.equals(Node)) {
                        //新机型050
                        String type = productNewFlag(SNCode);
                        if ("280".equals(type) || "380".equals(type)){
                            ProductNewPassStation productNewPassStation = new ProductNewPassStation();
                            productNewPassStation.setSfcCode(SNCode);
                            List<ProductNewPassStation> passStationList = productNewPassStationService.getProductPassStationList(productNewPassStation);
                            ProductNewPassStation productNewPassStation2 = new ProductNewPassStation();
                            productNewPassStation2.setFinishFlag("0");
                            List<ProductNewPassStation> collect = productNewPassStationService.getProductPassStationList(productNewPassStation2)
                                    .stream().sorted(Comparator.comparing(ProductNewPassStation::getCreateTime))
                                    .collect(Collectors.toList());
                            String partSfcCode = "";
                            if (CollUtil.isNotEmpty(collect)){
                                ProductNewPassStation passStation = collect.get(0);
                                partSfcCode = passStation.getSfcCode();
                            }
                            if (CollUtil.isNotEmpty(passStationList)){
                                ProductNewPassStation passStation = passStationList.get(0);
                                String productType = "";
                                if (ObjectUtils.isNotEmpty(passStation)){
                                    productType = passStation.getProductType();
                                }
                                passingStationCollectionServiceImpl.sendMessage(SNCode+"*sfcCode*"+productType+"*"+partSfcCode,"OP050");
                            }
                        }
                    }
                    if (OPCElement.OP060_CODE_CHECK.equals(Node)){
                        String type = productNewFlag(SNCode);
                        if ("280".equals(type) || "380".equals(type)){
                            checkBoxRod(Node);
                        }
                    }
                    System.out.println(entity);
 
                }
 
            }
        }
    }
 
    private void rework(String SNCode,String Node,List<DaPassingStationCollection> daPassingStationCollections,List<RmRepairRecord> rmRepairRecords)  throws Exception{
        BsOrderScheduling bsOrderScheduling = new BsOrderScheduling();
        bsOrderScheduling.setEngineNo(SNCode);
        List<String> allProcessCoed = new ArrayList<>();
        List<BsTechnologyRouteChildInfo> bsTechnologyRouteChildInfos = new ArrayList<>();
        List<String> collect = bsOrderSchedulingService.selectBsOrderSchedulingList(bsOrderScheduling).stream().map(BsOrderScheduling::getModel).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(collect)){
            String s = collect.get(0);
            //查找次机型的完整工序
            bsTechnologyRouteChildInfos = bsTechnologyRouteChildInfoService.selectAllTechnologyRouteByProductCode(s);
            if (CollUtil.isNotEmpty(bsTechnologyRouteChildInfos)){
                allProcessCoed = bsTechnologyRouteChildInfos.stream().map(BsTechnologyRouteChildInfo::getProcessesCode).collect(Collectors.toList());
            }
        }
//        RmRepairRecord rmRepairRecord = new RmRepairRecord();
//        rmRepairRecord.setBoxCode(SNCode);
        List<ReadWriteEntity> writeList = new ArrayList<>();
//        List<RmRepairRecord> rmRepairRecords = rmRepairRecordService.selectRmRepairRecordList(rmRepairRecord);
        //过站记录
//        DaPassingStationCollection PassingStationCollection=new DaPassingStationCollection();
//        PassingStationCollection.setSfcCode(SNCode);
        //过站记录
//        List<DaPassingStationCollection> daPassingStationCollections = passingStationCollectionServiceImpl.selectDaPassingStationCollectionList(PassingStationCollection);
 
        //所有返工工位
        List<String> collect1 = rmRepairRecords.stream().map(RmRepairRecord::getProcessesCode).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(collect1)){
            //查询出所有需要返修工位的最小op块
            Integer minOP = 0;
            if (CollUtil.isNotEmpty(collect1)){
 
                List<Integer> sortList = new ArrayList<>();
                collect1.stream().forEach(x ->{
                    String op = x.replace("OP", "");
                    if (StrUtil.isNotBlank(op)){
                        int i = Integer.parseInt(op);
                        sortList.add(i);
                    }
                });
                if (CollUtil.isNotEmpty(sortList)){
                    List<Integer> collect2 = sortList.stream().filter(Objects::nonNull).sorted().collect(Collectors.toList());
                    minOP = collect2.get(0);
                }
            }
            //获取过站记录的最大工位
            String maxCode = null;
            if (CollUtil.isNotEmpty(daPassingStationCollections)){
                List<DaPassingStationCollection> collect2 = daPassingStationCollections.stream().filter(x -> "不合格".equals(x.getOutRsSign())).collect(Collectors.toList());
                if (CollUtil.isNotEmpty(collect2)){
                    DaPassingStationCollection lastOne = collect2.get(collect2.size() - 1);
                    maxCode = lastOne.getLocationCode();
                }else {
                    DaPassingStationCollection lastOne = daPassingStationCollections.get(daPassingStationCollections.size() - 1);
                    maxCode = lastOne.getLocationCode();
                }
 
            }
            //所有放行工位
            List<String> passStation = new ArrayList<>();
            //所有生产工位
            List<String> productStation = new ArrayList<>();
            //最大工位编号
            Integer maxCodeNum = 0;
            if (StrUtil.isNotEmpty(maxCode)){
                String finalMaxCode = maxCode;
                //获取当前工位对应工步号
                List<String> collect2 = bsTechnologyRouteChildInfos.stream()
                        .filter(x -> x.getProcessesCode().equals(finalMaxCode))
                        .map(BsTechnologyRouteChildInfo::getStepNo).collect(Collectors.toList());
                if (CollUtil.isNotEmpty(collect2)){
                    String s = collect2.get(0);
                    if (StrUtil.isNotBlank(s)){
                        maxCodeNum = Integer.parseInt(s);
                    }
                }
                if (CollUtil.isNotEmpty(bsTechnologyRouteChildInfos)){
                    bsTechnologyRouteChildInfos.forEach(x -> {
                        x.setStepNoNum(Integer.parseInt(x.getStepNo()));
                    });
                    Integer finalMaxCodeNum = maxCodeNum;
                    //勾选的返修工位中最大的工位之后的工位----1
                    List<BsTechnologyRouteChildInfo> collect3 = bsTechnologyRouteChildInfos.stream()
                            .filter(x -> x.getStepNoNum() >= finalMaxCodeNum).collect(Collectors.toList());
                    if (CollUtil.isNotEmpty(collect3)){
                        //需要生产
                        productStation.addAll(collect3.stream().map(BsTechnologyRouteChildInfo::getProcessesCode).collect(Collectors.toList()));
                    }
                    //从第一个工位到返修的最大工位之间,
                    //勾选了返修标记的需要生产---1,未勾选的不生产---2
                    productStation.addAll(collect1);
                }
            }
            if (CollUtil.isNotEmpty(productStation)){
                List<String> collect2 = allProcessCoed.stream().filter(x -> !productStation.contains(x)).collect(Collectors.toList());
                passStation.addAll(collect2);
            }
            String[] parts = Node.split("[.]");
            String par = parts[0]+"."+parts[1];
            if (CollUtil.isNotEmpty(passStation)){
                passStation.forEach(x -> {
                    ReadWriteEntity readWriteEntity = new ReadWriteEntity();
                    readWriteEntity.setIdentifier(par+"."+x);
                    readWriteEntity.setValue(2);
                    writeList.add(readWriteEntity);
                });
            }
            if (CollUtil.isNotEmpty(productStation)){
                productStation.forEach(x -> {
                    ReadWriteEntity readWriteEntity = new ReadWriteEntity();
                    readWriteEntity.setIdentifier(par+"."+x);
                    readWriteEntity.setValue(1);
                    writeList.add(readWriteEntity);
                });
            }
            //对OP755做特殊处理
            List<ReadWriteEntity> OP755List = new ArrayList<>();
            if (OPCElement.OP755_ZZ_CODE_CHECK.equals(Node)){
                List<String> patchList = new ArrayList<>();
                patchList.add(par+".OP740");
                patchList.add(par+".OP760");
                patchList.add(par+".OP770");
                patchList.add(par+".OP780");
                for (String s : patchList) {
                    ReadWriteEntity readWriteEntity = new ReadWriteEntity();
                    readWriteEntity.setIdentifier(s);
                    readWriteEntity.setValue(0);
                    OP755List.add(readWriteEntity);
                }
            }
            ReadWriteEntity readWriteEntity = new ReadWriteEntity();
            readWriteEntity.setIdentifier(parts[0]+"."+parts[1]+".Repair_sign");
            readWriteEntity.setValue(minOP);
            miloService.writeToOpcShort(readWriteEntity);
            miloService.writeToOpcByte(writeList);
            logger.info("{}返修写入:,{}",SNCode,writeList);
            if (CollUtil.isNotEmpty(OP755List)){
                miloService.writeToOpcByte(OP755List);
            }
        }
 
    }
 
 
    public void SaveData(String Node) throws Exception {
 
        String[] parts = Node.split("[.]");
        Object SNCodeObject = miloService.readFromOpcUa(parts[0] + "." + parts[1] + ".Code1").getValue();
        if (null == SNCodeObject){
            SNCodeObject = miloService.readFromOpcUa(parts[0] + "." + parts[1] + ".Code").getValue();
        }
        if(null != SNCodeObject)
        {
            String SNCode = SNCodeObject.toString();
            Object object = miloService.readFromOpcUa(parts[0] + "." + parts[1] + ".Runtime").getValue();
            String gatherAddress = parts[0] + "." + parts[1];
            List<DaCollectionParamConf> list = new ArrayList<>();
            if (allCollectParamList.containsKey(gatherAddress)){
                list = allCollectParamList.get(gatherAddress);
            }else {
                DaCollectionParamConf daCollectionParamConf = new DaCollectionParamConf();
                daCollectionParamConf.setGatherAddress(gatherAddress);
                daCollectionParamConf.setRemarks("1");
                list = collectionParamConfService.selectDaCollectionParamConfList(daCollectionParamConf);
                if (CollUtil.isNotEmpty(list)){
                    //加入hashmap
                    allCollectParamList.put(gatherAddress,list);
                }
            }
            //60只采集老车型
            if (OPCElement.OP060_SAVE_REQUEST.equals(Node) || OPCElement.OP060_SAVE_REQUEST_LAST.equals(Node)){
                String s = productNewFlag(SNCode);
                if ("280".equals(s) || "380".equals(s)){
                    list = new ArrayList<>();
                }
            }
            //80、100只采集新车型
            if (OPCElement.OP080_SAVE_REQUEST.equals(Node)
                    || OPCElement.OP080_SAVE_REQUEST_LAST.equals(Node)
                    || OPCElement.OP100_SAVE_REQUEST.equals(Node)
                    || OPCElement.OP100_SAVE_REQUEST_LAST.equals(Node)){
                String s = productNewFlag(SNCode);
                if (StrUtil.isNotEmpty(s)){
                    if (!"280".equals(s) && !"380".equals(s)){
                        list = new ArrayList<>();
                    }
                }
 
            }
            List<String> nodeIdList = list.stream().map(info -> {
                String nodeid = info.getGatherAddress();
                return nodeid;
            }).collect(Collectors.toList());
 
            List<ReadWriteEntity> readWriteEntityList = new ArrayList<>();
            if(CollUtil.isNotEmpty(nodeIdList)){
                //long time1 = new Date().getTime();
                readWriteEntityList = miloService.readFromOpcUa(nodeIdList);
                //long time2 = new Date().getTime();
                //logger.info("读取点位花费时间:{},",time2-time1);
            }
                //返回plc保存成功
                if (parts.length==3){
                    if ("SaveRequest".equals(parts[2])){
                        if (OPCElement.OP050_SAVE_REQUEST.equals(Node)){
                            cleanForm050(Node);
                        } else if (OPCElement.OP060_SAVE_REQUEST.equals(Node)){
                            cleanForm060(Node);
                        }
                        ReadWriteEntity entity = new ReadWriteEntity(parts[0] + "." + parts[1] + ".SaveFeed", 1);
                                miloService.writeToOpcByte(entity);
                    }else if ("SaveRequestLast".equals(parts[2])){
                        if (OPCElement.OP050_SAVE_REQUEST_LAST.equals(Node)){
                            cleanForm050(Node);
                        } else if (OPCElement.OP060_SAVE_REQUEST_LAST.equals(Node)){
                            cleanForm060(Node);
                        }
                        ReadWriteEntity entity = new ReadWriteEntity(parts[0] + "." + parts[1] + ".SaveFeedLast", 1);
                                miloService.writeToOpcByte(entity);
                    }
                    logger.info("{}保存请求信号返回,MES->PLC的SaveFeedLast信号",Node);
                    //logger.info("-------请求返回-----,{}",LocalDateTime.now());
                }
 
                BsOrderScheduling bsOrderScheduling=bsOrderSchedulingService.selectBsOrderSchedulingSNCode(SNCode);
 
                List<DaParamCollection> daParamCollectionlist = new ArrayList<>();
                if (CollUtil.isNotEmpty(nodeIdList)){
                    for(int i=0;i<nodeIdList.size();i++){
                        if(ObjectUtil.isNotEmpty(readWriteEntityList.get(i).getValue()) && !readWriteEntityList.get(i).getValue().toString().equals("0.0")){
                            DaParamCollection ParamCollection = new DaParamCollection();
                            ParamCollection.setParamCode(readWriteEntityList.get(i).getIdentifier().toString().split("[.]")[2]);
                            ParamCollection.setLocationCode(parts[1]);
                            if (readWriteEntityList.get(i) == null){
                                ParamCollection.setParamValue("0");
                            }else {
                                ParamCollection.setParamValue(readWriteEntityList.get(i).getValue().toString());
                            }
                            ParamCollection.setSfcCode(SNCode);
                            ParamCollection.setParamName(list.get(i).getCollectParameterName());
                            ParamCollection.setParamUpper(list.get(i).getParamUpper());
                            ParamCollection.setParamLower(list.get(i).getParamLower());
                            ParamCollection.setUnit(list.get(i).getCollectParameterUnit());
                            ParamCollection.setState("合格");
                            ParamCollection.setType(list.get(i).getCollectParameterType());
                            ParamCollection.setCollectionTime(new Date());
                            ParamCollection.setWorkOrderNo(bsOrderScheduling.getOrderNo());
                            daParamCollectionlist.add(ParamCollection);
                        }
                    }
                    daParamCollectionService.saveBeachDaParamCollection(daParamCollectionlist);
                    logger.info("{}发动机({})采集数据保存完成",SNCode,Node);
                }
 
            Object ProductStatus = miloService.readFromOpcUa(parts[0] + "." + parts[1] + ".ProductStatus").getValue();
            String str1="合格" ;
            if (ObjectUtil.isNotEmpty(ProductStatus)){
                logger.info("ProductStatus,{}",ProductStatus.toString());
                String productStatusString = ProductStatus.toString();
                if (productStatusString.contains("2") || productStatusString.contains("3")){
                    str1="不合格";
                }else {
                    str1="合格";
                }
            }
            Long beatTime = 0L;
            if (ObjectUtil.isNotEmpty(object)){
                String str = object.toString();
                beatTime = Float.valueOf(str).longValue();
            }
            DaPassingStationCollection PassingStationCollection=new DaPassingStationCollection();
            PassingStationCollection.setSfcCode(SNCode);
            PassingStationCollection.setLocationCode(parts[1]);
            PassingStationCollection.setOutboundTime(new Date());
            PassingStationCollection.setInboundTime(new Date(new Date().getTime()-beatTime*1000));
            PassingStationCollection.setCollectionTime(new Date());
            PassingStationCollection.setCreateTime(new Date());
            PassingStationCollection.setBeatTime(beatTime.toString());
            PassingStationCollection.setOutRsSign(str1);
            PassingStationCollection.setWorkOrderNo(bsOrderScheduling.getOrderNo());
            passingStationCollectionServiceImpl.insertDaPassingStationCollection(PassingStationCollection);
            logger.info("{}发动机({})过站数据保存完成",SNCode,Node);
 
            //更新工单状态
            if ("OP790".equals(parts[1])) {
                //bsOrderScheduling.setReport20("0");
                bsOrderScheduling.setProductionStatus("3");
                bsOrderScheduling.setFinalAssemblyOfflineTime(new Date());
            }else if("OP050".equals(parts[1])){
                bsOrderScheduling.setReport10("0");
                bsOrderScheduling.setProductionStatus("2");
                bsOrderScheduling.setBoxClosingOnlineTime(new Date());
            }else if("OP120".equals(parts[1])){
                bsOrderScheduling.setReport20("0");
                bsOrderScheduling.setFinalAssemblyLaunchTime(new Date());
                bsOrderScheduling.setClosingBoxOfflineTime(new Date());
            }
            bsOrderScheduling.setQualityStatus(str1);
            bsOrderScheduling.setCurrentWorkstation(parts[1]);
            bsOrderSchedulingService.updateBsOrderScheduling(bsOrderScheduling);
        }
    }
 
 
    public String productNewFlag(String SNCode){
        String type;
        if (SNCode.startsWith("280")){
            type = "280";
        } else if (SNCode.startsWith("380")){
            type = "380";
        }else {
            type = "oldType";
        }
        return type;
    }
 
    public void tile2(String Node) throws Exception {
        String[] parts = Node.split("[.]");
        Object SNCodeObject = miloService.readFromOpcUa(parts[0] + "." + parts[1] + ".Code").getValue();
        if (null != SNCodeObject) {
            String SNCode = SNCodeObject.toString();
            String productNewFlag = productNewFlag(SNCode);
            if ("380".equals(productNewFlag)){
                ProductNewPassStation passStation = new ProductNewPassStation();
                passStation.setSfcCode(SNCode);
                List<ProductNewPassStation> productPassStationList = productNewPassStationService.getProductPassStationList(passStation);
                if (CollUtil.isNotEmpty(productPassStationList)){
                    ProductNewPassStation productNewPassStation = productPassStationList.get(0);
                    String boxCode = productNewPassStation.getBoxCode();
                    String productType = productNewPassStation.getProductType();
                    log.info("发送到OP040的页面数据productType,{}",productType);
                    log.info("发送到OP040的页面数据boxCode,{}",boxCode);
                    passingStationCollectionServiceImpl.sendMessage(productType+"*"+boxCode, "OP040");
                }
            } else if ("280".equals(productNewFlag)){
 
            }
        }
    }
 
 
    public void cleanForm(String Node) throws Exception {
        String[] parts = Node.split("[.]");
        Object SNCodeObject = miloService.readFromOpcUa(parts[0] + "." + parts[1] + ".Code").getValue();
        if (ObjectUtils.isNotEmpty(SNCodeObject)){
            String snCode = SNCodeObject.toString();
            String s = productNewFlag(snCode);
            if ("280".equals(s) || "380".equals(s)){
                log.info("发送到OP030的清除");
                passingStationCollectionServiceImpl.sendMessage("clean","OP030");
            }
        }
 
    }
 
    public void cleanForm040(String Node) throws Exception {
        String[] parts = Node.split("[.]");
        Object SNCodeObject = miloService.readFromOpcUa(parts[0] + "." + parts[1] + ".Code").getValue();
        if (ObjectUtils.isNotEmpty(SNCodeObject)){
            String snCode = SNCodeObject.toString();
            String s = productNewFlag(snCode);
            if ("280".equals(s) || "380".equals(s)){
                log.info("发送到OP040的页面清除");
                passingStationCollectionServiceImpl.sendMessage("clean*clean","OP040");
            }
        }
    }
 
    public void cleanForm050(String Node) throws Exception {
        String[] parts = Node.split("[.]");
        Object SNCodeObject = miloService.readFromOpcUa(parts[0] + "." + parts[1] + ".Code").getValue();
        if (ObjectUtils.isNotEmpty(SNCodeObject)){
            String snCode = SNCodeObject.toString();
            String s = productNewFlag(snCode);
            if ("280".equals(s) || "380".equals(s)){
                ProductNewPassStation passStation = new ProductNewPassStation();
                passStation.setSfcCode(snCode);
                List<ProductNewPassStation> productPassStationList = productNewPassStationService.getProductPassStationList(passStation);
                if (CollUtil.isNotEmpty(productPassStationList)){
                    productNewPassStationService.updatePassStationBySfcCode(snCode);
                }
                passingStationCollectionServiceImpl.sendMessage("clean*clean","OP050");
            }
        }
    }
 
    public void cleanForm060(String Node) throws Exception {
        String[] parts = Node.split("[.]");
        Object SNCodeObject = miloService.readFromOpcUa(parts[0] + "." + parts[1] + ".Code").getValue();
        if (ObjectUtils.isNotEmpty(SNCodeObject)){
            String snCode = SNCodeObject.toString();
            String s = productNewFlag(snCode);
            if ("280".equals(s) || "380".equals(s)){
                passingStationCollectionServiceImpl.sendMessage("clean*clean","OP060");
            }
        }
    }
 
 
    public void checkBoxRod(String Node) throws Exception {
        String[] parts = Node.split("[.]");
        Object SNCodeObject = miloService.readFromOpcUa(parts[0] + "." + parts[1] + ".Code").getValue();
        if (null != SNCodeObject) {
            String SNCode = SNCodeObject.toString();
            String s = productNewFlag(SNCode);
            if ("280".equals(s) || "380".equals(s)){
                sendTo060(SNCode,parts);
            }
        }
    }
 
    private void sendTo060(String SNCode, String[] parts) throws Exception {
        String boxCode = "";
        String rodCode = "";
        ProductNewPassStation productNewPassStation = new ProductNewPassStation();
        productNewPassStation.setSfcCode(SNCode);
        List<ProductNewPassStation> productPassStationList = productNewPassStationService.getProductPassStationList(productNewPassStation);
        if (CollUtil.isNotEmpty(productPassStationList)){
            ProductNewPassStation passStation = productPassStationList.get(0);
            boxCode = passStation.getBoxCode();
            rodCode = passStation.getRodCode();
        }
        //配瓦校验
        passingStationCollectionServiceImpl.sendMessage(boxCode + "*" + rodCode + "*" + SNCode, parts[1]);
    }
}