分支自 SuZhouGuanHong/TaiYuanTaiZhong

dengjunjie
2024-04-16 f0e2d9d2c7c41b311217bdb2c4d114ff53f6a146
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
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
using FreeSql.Internal.Model;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.Xml;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using WIDESEA_Comm;
using WIDESEA_Comm.LogInfo;
using WIDESEA_Comm.TaskNo;
using WIDESEA_Core.EFDbContext;
using WIDESEA_Core.FreeDB;
using WIDESEA_Entity.DomainModels;
using WIDESEA_WCS.IRepositories;
using WIDESEA_WCS.JobsPart.Common;
using WIDESEA_WCS.Repositories;
using WIDESEA_WCS.WCSClient;
using WIDESEA_WMS.IRepositories;
using WIDESEA_WMS.Repositories;
using static FreeSql.Internal.GlobalFilter;
using static System.Collections.Specialized.BitVector32;
 
namespace WIDESEA_WCS
{
    public class Gantry
    {
        #region 查询车轮数据
        /// <summary>
        /// 查询车轮数据
        /// </summary>
        /// <param name="client"></param>
        public void QueryWheeldata(PLCClient client)
        {
            try
            {
                VOLContext Context = new VOLContext();
                Idt_plcinfoheadRepository repository = new dt_plcinfoheadRepository(Context);
                Idt_plcinfodetailRepository plcRepository = new dt_plcinfodetailRepository(Context);
                IVV_Mes_WorkinfoRepository workinfoRepository = new VV_Mes_WorkinfoRepository(Context);
                Idt_geometry_dataRepository dataRepository = new dt_geometry_dataRepository(Context);
                Idt_mes_detailRepository mes_DetailRepository = new dt_mes_detailRepository(Context);
                Idt_mes_headRepository mes_HeadRepository = new dt_mes_headRepository(Context);
                Idt_geometry_data_detectionlineRepository detectionlineRepository = new dt_geometry_data_detectionlineRepository(Context);
                var plc = repository.FindFirst(x => x.plcinfo_name == client.PLCName);
                List<string> names = new List<string>() { "1单元辊道下料查询车轮", "2单元辊道下料查询车轮", "3单元辊道下料查询车轮", "辊道上料查询车轮" };
                foreach (string name in names)
                {
                    var details = plcRepository.Find(x => x.plcdetail_iotype == plc.plcinfo_iotyep && x.plcdetail_number == name).ToList();
                    var on = DBExtension.Read(details.Where(x => x.plcdetail_name == "R_oi_on").First(), client);
                    if ((byte)on == 1)
                    {
                        var wp_type = DBExtension.Read(details.Where(x => x.plcdetail_name == "R_o_wp_type").First(), client);
 
                        var number = "1单元辊道下料查询订单";
                        if (name == "2单元辊道下料查询车轮") number = "2单元辊道下料查询订单";
                        if (name == "3单元辊道下料查询车轮") number = "3单元辊道下料查询订单";
                        if (name == "辊道上料查询车轮") number = "辊道上料查询订单";
                        var wp_id = client.ReadByOrder<string>("R_o_wp_id", number);
 
                        var Mes_Work = workinfoRepository.Find(x => x.SN == wp_id && x.processCode == (name != "辊道上料查询车轮" ? "17" : "28")).FirstOrDefault();
                        if (Mes_Work == null)
                        {
                            client.WriteByOrder("W_i_status", (byte)2, name);
                            client.WriteByOrder("R_oi_on", (byte)0, name);
                            #region 日志记录
                            WriteDBLog.Error(name, $"读取桁架信息:\n车轮SN:{wp_id}\n\n写入桁架信息:\nW_i_status:2\nR_oi_on:0\n\n未查询到工单信息", "PCS");
                            #endregion
                            continue;
                        }
 
                        #region MyRegion
                        //var mes_Detail = mes_DetailRepository.FindFirst(x => x.SN == wp_id);
                        //if (mes_Detail == null)
                        //{
                        //    client.WriteByOrder("W_i_status", (byte)2, name);
                        //    client.WriteByOrder("R_oi_on", (byte)0, name);
                        //    #region 日志记录
                        //    WriteDBLog.Error(name, $"读取桁架信息:\n车轮SN:{wp_id}\n\n写入桁架信息:\nW_i_status:2\nR_oi_on:0\n\n未查询到工单详情", "PCS");
                        //    #endregion
                        //    continue;
                        //}
                        //var mes_Head = mes_HeadRepository.FindFirst(x => x.jobID == mes_Detail.jobID);
                        //if (mes_Head == null)
                        //{
                        //    client.WriteByOrder("W_i_status", (byte)2, name);
                        //    client.WriteByOrder("R_oi_on", (byte)0, name);
                        //    #region 日志记录
                        //    WriteDBLog.Error(name, $"读取桁架信息:\n车轮SN:{wp_id}\n\n写入桁架信息:\nW_i_status:2\nR_oi_on:0\n\n未查询到工单头表", "PCS");
                        //    #endregion
                        //    continue;
                        //}
                        #endregion
 
 
                        if (name != "辊道上料查询车轮")
                        {
                            //var geometry = dataRepository.FindFirst(x => x.TypeId == (Int32)wp_type);
                            var geometry = dataRepository.Find(x => x.Description == Mes_Work.drawingNo).FirstOrDefault();
                            var geometry1 = detectionlineRepository.Find(x => x.Description == Mes_Work.drawingNo).FirstOrDefault();
                            if (geometry == null && geometry1 == null)
                            {
                                //DBExtension.Write(details.Where(x => x.plcdetail_name == "W_i_status").First(), client, (Int16)2);
                                //1-找到数据,2-未找到工件类型数据
                                client.WriteByOrder("W_i_status", (byte)2, name);
                                client.WriteByOrder("R_oi_on", (byte)0, name);
                                #region 日志记录
                                WriteDBLog.Error(name, $"读取桁架信息:\n轮型ID:{wp_type}\n\n写入桁架信息:\nW_i_status:2\nR_oi_on:0\n\n未查询到机加工车轮信息", "PCS");
                                #endregion
                            }
                            else
                            {
                                //1-找到数据,2-未找到工件类型数据
                                client.WriteByOrder("W_i_status", (byte)1, name);
                                client.WriteByOrder("W_i_parameter_a", geometry == null ? (float)geometry1.a : (float)geometry.a, name);
                                client.WriteByOrder("W_i_parameter_b", geometry == null ? (float)geometry1.b : (float)geometry.b, name);
                                client.WriteByOrder("W_i_parameter_c", geometry == null ? (float)geometry1.c : (float)geometry.c, name);
                                client.WriteByOrder("W_i_parameter_d", geometry == null ? (float)geometry1.d : (float)geometry.d, name);
                                client.WriteByOrder("W_i_parameter_e", geometry == null ? (float)geometry1.e : (float)geometry.e, name);
                                client.WriteByOrder("W_i_parameter_f", geometry == null ? (float)geometry1.f : (float)geometry.f, name);
                                client.WriteByOrder("W_i_parameter_g", geometry == null ? (float)geometry1.g : (float)geometry.g, name);
                                client.WriteByOrder("W_i_parameter_h", geometry == null ? (float)geometry1.h : (float)geometry.h, name);
                                client.WriteByOrder("R_oi_on", (byte)0, name);
                                #region 日志记录
                                WriteDBLog.Success(name, $"读取桁架信息:\n轮型ID:{wp_type}\n\n写入桁架信息:\nW_i_status:1" + $"\n" +
                                    $"a:{(geometry == null ? (float)geometry1.a : (float)geometry.a)}\n" +
                                    $"b:{(geometry == null ? (float)geometry1.b : (float)geometry.b)}\n" +
                                    $"c:{(geometry == null ? (float)geometry1.c : (float)geometry.c)}\n" +
                                    $"d:{(geometry == null ? (float)geometry1.d : (float)geometry.d)}\n" +
                                    $"e:{(geometry == null ? (float)geometry1.e : (float)geometry.e)}\n" +
                                    $"f:{(geometry == null ? (float)geometry1.f : (float)geometry.f)}\n" +
                                    $"g:{(geometry == null ? (float)geometry1.g : (float)geometry.g)}\n" +
                                    $"h:{(geometry == null ? (float)geometry1.h : (float)geometry.h)}\n" +
                                    $"R_oi_on:0", "PCS");
                                #endregion
                            }
                        }
                        else
                        {
                            var geometry = detectionlineRepository.Find(x => x.Description == Mes_Work.drawingNo).FirstOrDefault();
                            var geometry1 = dataRepository.Find(x => x.Description == Mes_Work.drawingNo).FirstOrDefault();
                            if (geometry == null && geometry1 == null)
                            {
                                client.WriteByOrder("W_i_status", (byte)2, name);
                                client.WriteByOrder("R_oi_on", (byte)0, name);
                                #region 日志记录
                                WriteDBLog.Error(name, $"读取桁架信息:\n轮型ID:{wp_type}\n\n写入桁架信息:\nW_i_status:2\nR_oi_on:0\n\n未查询到检测线车轮信息", "PCS");
                                #endregion
                            }
                            else
                            {
                                client.WriteByOrder("W_i_status", (byte)1, name);
                                client.WriteByOrder("W_i_parameter_a", geometry == null ? (float)geometry1.a : (float)geometry.a, name);
                                client.WriteByOrder("W_i_parameter_b", geometry == null ? (float)geometry1.b : (float)geometry.b, name);
                                client.WriteByOrder("W_i_parameter_c", geometry == null ? (float)geometry1.c : (float)geometry.c, name);
                                client.WriteByOrder("W_i_parameter_d", geometry == null ? (float)geometry1.d : (float)geometry.d, name);
                                client.WriteByOrder("W_i_parameter_e", geometry == null ? (float)geometry1.e : (float)geometry.e, name);
                                client.WriteByOrder("W_i_parameter_f", geometry == null ? (float)geometry1.f : (float)geometry.f, name);
                                client.WriteByOrder("W_i_parameter_g", geometry == null ? (float)geometry1.g : (float)geometry.g, name);
                                client.WriteByOrder("W_i_parameter_h", geometry == null ? (float)geometry1.h : (float)geometry.h, name);
                                client.WriteByOrder("R_oi_on", (byte)0, name);
                                #region 日志记录
                                WriteDBLog.Success(name, $"读取桁架信息:\n轮型ID:{wp_type}\n\n写入桁架信息:\nW_i_status:1" + $"\n" +
                                    $"a:{(geometry == null ? (float)geometry1.a : (float)geometry.a)}\n" +
                                    $"b:{(geometry == null ? (float)geometry1.b : (float)geometry.b)}\n" +
                                    $"c:{(geometry == null ? (float)geometry1.c : (float)geometry.c)}\n" +
                                    $"d:{(geometry == null ? (float)geometry1.d : (float)geometry.d)}\n" +
                                    $"e:{(geometry == null ? (float)geometry1.e : (float)geometry.e)}\n" +
                                    $"f:{(geometry == null ? (float)geometry1.f : (float)geometry.f)}\n" +
                                    $"g:{(geometry == null ? (float)geometry1.g : (float)geometry.g)}\n" +
                                    $"h:{(geometry == null ? (float)geometry1.h : (float)geometry.h)}\n" +
                                    $"R_oi_on:0", "PCS");
                                #endregion
                            }
                        }
 
                    }
                }
            }
            catch (Exception ex)
            {
                WriteDBLog.Error("查询车轮数据", $"错误信息:{ex.Message}", "PCS");
            }
        }
        #endregion
 
        #region 查询订单,是否需要加工
        /// <summary>
        /// 查询订单
        /// </summary>
        /// <param name="client"></param>
        public void QueryOrder(PLCClient client)
        {
            try
            {
                VOLContext Context = new VOLContext();
                Idt_plcinfoheadRepository repository = new dt_plcinfoheadRepository(Context);
                Idt_plcinfodetailRepository plcRepository = new dt_plcinfodetailRepository(Context);
                Idt_geometry_dataRepository dataRepository = new dt_geometry_dataRepository(Context);
                Idt_geometry_data_detectionlineRepository detectionlineRepository = new dt_geometry_data_detectionlineRepository(Context);
                Idt_mes_detailRepository mes_DetailRepository = new dt_mes_detailRepository(Context);
                IVV_Mes_WorkinfoRepository workinfoRepository = new VV_Mes_WorkinfoRepository(Context);
                Idt_mes_headRepository mes_HeadRepository = new dt_mes_headRepository(Context);
                Idt_patternRepository patternRepository = new dt_patternRepository(Context);
                var plc = repository.FindFirst(x => x.plcinfo_name == client.PLCName);
                List<string> names = new List<string>() { "1单元辊道下料查询订单", "2单元辊道下料查询订单", "3单元辊道下料查询订单", "辊道上料查询订单" };
                foreach (string name in names)
                {
                    var details = plcRepository.Find(x => x.plcdetail_iotype == plc.plcinfo_iotyep && x.plcdetail_number == name).ToList();
                    var on = DBExtension.Read(details.Where(x => x.plcdetail_name == "R_oi_on").First(), client);
                    //var on = DBExtension.Read(details.Where(x => x.plcdetail_name == "R_oi_on" && x.plcdetail_number == name).First(), client);
                    if ((byte)on == 1)
                    {
                        //var wp_id = DBExtension.Read(details.Where(x => x.plcdetail_name == "R_o_wp_id").First(), client).ToString();
                        var wp_id = client.ReadByOrder<string>("R_o_wp_id", name);
                        if (string.IsNullOrEmpty(wp_id))
                        {
                            #region 日志记录
                            client.WriteByOrder("W_i_status", (byte)2, name);
                            client.WriteByOrder("R_oi_on", (byte)0, name);
                            WriteDBLog.Error(name, $"读取桁架信息:\n车轮SN:{wp_id}\n\n写入桁架信息:\nW_i_status:2\nR_oi_on:0\n\n车轮SN号为空", "PCS");
                            #endregion
                            continue;
                        }
                        var wp_type = DBExtension.Read(details.Where(x => x.plcdetail_name == "R_o_wp_type").First(), client);
 
                        var Mes_Work = workinfoRepository.Find(x => x.SN == wp_id && x.processCode == (name != "辊道上料查询订单" ? "17" : "28")).FirstOrDefault();
                        if (Mes_Work == null)
                        {
                            client.WriteByOrder("W_i_status", (byte)2, name);
                            client.WriteByOrder("R_oi_on", (byte)0, name);
                            #region 日志记录
                            WriteDBLog.Error(name, $"读取桁架信息:\n车轮SN:{wp_id}\n\n写入桁架信息:\nW_i_status:2\nR_oi_on:0\n\n未查询到工单详情", "PCS");
                            #endregion
                            continue;
                        }
                        #region MyRegion
                        //var mes_Detail = mes_DetailRepository.FindFirst(x => x.SN == wp_id);
                        //if (mes_Detail == null)
                        //{
                        //    client.WriteByOrder("W_i_status", (byte)2, name);
                        //    client.WriteByOrder("R_oi_on", (byte)0, name);
                        //    #region 日志记录
                        //    WriteDBLog.Error(name, $"读取桁架信息:\n车轮SN:{wp_id}\n\n写入桁架信息:\nW_i_status:2\nR_oi_on:0\n\n未查询到工单详情", "PCS");
                        //    #endregion
                        //    continue;
                        //}
                        //var mes_Head = mes_HeadRepository.FindFirst(x => x.jobID == mes_Detail.jobID);
                        //if (mes_Head == null)
                        //{
                        //    client.WriteByOrder("W_i_status", (byte)2, name);
                        //    client.WriteByOrder("R_oi_on", (byte)0, name);
                        //    #region 日志记录
                        //    WriteDBLog.Error(name, $"读取桁架信息:\n车轮SN:{wp_id}\n\n写入桁架信息:\nW_i_status:2\nR_oi_on:0\n\n未查询到工单头表", "PCS");
                        //    #endregion
                        //    continue;
                        //}
                        #endregion
 
                        var mes_Head = mes_HeadRepository.FindFirst(x => x.jobID == Mes_Work.jobID);
 
                        if (name != "辊道上料查询订单")
                        {
                            //需添加查询车轮SN号订单逻辑   需添加入库模式判断,手动入库模式需人工添加入库库区,否则报警
                            var pattern = patternRepository.FindFirst(x => x.pattern_name == "入库模式").pattern_state;
                            if (pattern != 1 && string.IsNullOrEmpty(mes_Head.area))
                            {
                                client.WriteByOrder("W_i_status", (byte)4, name);
                                client.WriteByOrder("R_oi_on", (byte)0, name);
                                #region 日志记录
                                WriteDBLog.Error(name, $"读取桁架信息:\n车轮SN:{wp_id}\n\n写入桁架信息:\nW_i_status:4\nR_oi_on:0\n\n手动入库模式未选择库区", "PCS");
                                #endregion
                                continue;
                            }
                            //var geometry = dataRepository.FindFirst(x => x.TypeId == (Int32)wp_type);
                            var geometry = dataRepository.Find(x => x.Description == mes_Head.drawingNo /*&& x.TypeId == (Int32)wp_type*/).FirstOrDefault();
                            var geometry1 = detectionlineRepository.Find(x => x.Description == mes_Head.drawingNo).FirstOrDefault();
                            if (geometry == null && geometry1 == null)
                            {
                                //1-好,允许加工,2-不在列表中,3-不加工车轮类型,4-手动入库模式人工未选择库区
                                client.WriteByOrder("W_i_status", (byte)2, name);
                                client.WriteByOrder("R_oi_on", (byte)0, name);
                                #region 日志记录
                                WriteDBLog.Error(name, $"读取桁架信息:\n车轮SN:{wp_id}\n轮型ID:{wp_type}\n\n写入桁架信息:\nW_i_status:2\nR_oi_on:0\n\n未查询到机加工车轮信息", "PCS");
                                #endregion
                                continue;
                            }
                            else
                            {
                                //1-好,允许加工,2-不在列表中,3-不加工车轮类型,4-手动入库模式人工未选择库区
                                client.WriteByOrder("W_i_status", (byte)1, name);
                                client.WriteByOrder("W_i_job_id", Mes_Work.jobID, name);
                                client.WriteByOrder("W_i_drawing_id", Mes_Work.drawingNo, name);
                                client.WriteByOrder("W_i_heat_id", Mes_Work.heatID, name);
                                //client.WriteByOrder("W_i_sourceheat", "", name);
                                client.WriteByOrder("W_i_batch_id", Mes_Work.heatBatchID, name);
 
                                //屏蔽工艺(屏蔽视觉检测/屏蔽涂油)
                                //if (name != "辊道上料查询车轮")
                                //{
                                client.WriteByOrder("W_i_skip_op_SJ1", name == "3单元辊道下料查询订单" ? true : mes_Head.skip_op_1, name);//屏蔽视觉检测设备1
                                client.WriteByOrder("W_i_skip_op_SJ2", name == "3单元辊道下料查询订单" ? true : mes_Head.skip_op_2, name);//屏蔽视觉检测设备2
                                client.WriteByOrder("W_i_skip_op_LT", mes_Head.skip_op_3, name);//屏蔽链条机
                                //}
                                client.WriteByOrder("R_oi_on", (byte)0, name);
                                #region 日志记录
                                WriteDBLog.Success(name, $"读取桁架信息:\n车轮SN:{wp_id}\n轮型ID:{wp_type}\n\n写入桁架信息:\nW_i_status:1" +
                                    $"\nW_i_job_id:{Mes_Work.jobID}\nW_i_drawing_id:{Mes_Work.drawingNo}\nW_i_heat_id:{Mes_Work.heatID}\nW_i_batch_id:{Mes_Work.heatBatchID}" +
                                    $"\nW_i_skip_op_SJ1:{(name == "3单元辊道下料查询订单" ? true : mes_Head.skip_op_1)}\nW_i_skip_op_SJ2:{(name == "3单元辊道下料查询订单" ? true : mes_Head.skip_op_2)}" +
                                    $"\nW_i_skip_op_LT:{mes_Head.skip_op_3}\nR_oi_on:0", "PCS");
                                #endregion
                            }
                        }
                        else
                        {
                            var geometry = detectionlineRepository.Find(x => x.Description == mes_Head.drawingNo /*&& x.TypeId == (Int32)wp_type*/).FirstOrDefault();
                            var geometry1 = dataRepository.Find(x => x.Description == mes_Head.drawingNo).FirstOrDefault();
                            if (geometry == null && geometry1 == null)
                            {
                                //1-好,允许加工,2-不在列表中,3-不加工车轮类型,4-手动入库模式人工未选择库区
                                client.WriteByOrder("W_i_status", (byte)2, name);
                                client.WriteByOrder("R_oi_on", (byte)0, name);
                                #region 日志记录
                                WriteDBLog.Error(name, $"读取桁架信息:\n车轮SN:{wp_id}\n轮型ID:{wp_type}\n\n写入桁架信息:\nW_i_status:2\nR_oi_on:0\n\n未查询到检测线车轮信息", "PCS");
                                #endregion
                                continue;
                            }
                            else
                            {
                                //1-好,允许加工,2-不在列表中,3-不加工车轮类型,4-手动入库模式人工未选择库区
                                client.WriteByOrder("W_i_status", (byte)1, name);
                                client.WriteByOrder("W_i_job_id", Mes_Work.jobID, name);
                                client.WriteByOrder("W_i_drawing_id", Mes_Work.drawingNo, name);
                                client.WriteByOrder("W_i_heat_id", Mes_Work.heatID, name);
                                //client.WriteByOrder("W_i_sourceheat", "", name);
                                client.WriteByOrder("W_i_batch_id", Mes_Work.heatBatchID, name);
                                client.WriteByOrder("R_oi_on", (byte)0, name);
                                #region 日志记录
                                WriteDBLog.Success(name, $"读取桁架信息:\n车轮SN:{wp_id}\n轮型ID:{wp_type}\n\n写入桁架信息:\nW_i_status:1" + $"\nW_i_job_id:{Mes_Work.jobID}" +
                                    $"\nW_i_drawing_id:{mes_Head.drawingNo}\nW_i_heat_id:{Mes_Work.heatID}\nW_i_batch_id:{Mes_Work.heatBatchID}\nR_oi_on:0", "PCS");
                                #endregion
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                WriteDBLog.Error("查询车轮订单", $"错误信息:{ex.Message}", "PCS");
            }
        }
        #endregion
 
        #region 下料位交互
        /// <summary>
        /// 下料
        /// </summary>
        /// <param name="client"></param>
        public void Layofflevel(PLCClient client)
        {
            try
            {
                VOLContext Context = new VOLContext();
                Idt_plcinfoheadRepository repository = new dt_plcinfoheadRepository(Context);
                Idt_plcinfodetailRepository plcRepository = new dt_plcinfodetailRepository(Context);
                Idt_stationinfoRepository stationinfoRepository = new dt_stationinfoRepository(Context);
                Idt_geometry_dataRepository dataRepository = new dt_geometry_dataRepository(Context);
                Idt_geometry_data_detectionlineRepository detectionlineRepository = new dt_geometry_data_detectionlineRepository(Context);
                Idt_mes_detailRepository mes_DetailRepository = new dt_mes_detailRepository(Context);
                Idt_mes_headRepository mes_HeadRepository = new dt_mes_headRepository(Context);
                IVV_Mes_WorkinfoRepository workinfoRepository = new VV_Mes_WorkinfoRepository(Context);
                Idt_patternRepository patternRepository = new dt_patternRepository(Context);
                var plc = repository.FindFirst(x => x.plcinfo_name == client.PLCName);
                var Pipeline_client = PLCClient.Clients.FirstOrDefault(t => t.PLCName == "链条机");
                if (Pipeline_client == null) throw new Exception("链条机调度服务未开启!");
                if (!Pipeline_client.IsConnected) throw new Exception("与链条机连接超时!");
                var Pipelineplc = repository.FindFirst(x => x.plcinfo_name == Pipeline_client.PLCName);
                List<string> names = new List<string>() { "1单元下料区", "2单元下料区", "3单元下料区" };
 
                foreach (string name in names)
                {
                    try
                    {
                        var details = plcRepository.Find(x => x.plcdetail_iotype == plc.plcinfo_iotyep && x.plcdetail_number == name).ToList();
 
                        var NGStation = "X01001003";
                        if (name == "2单元下料区") NGStation = "X02001003";
                        if (name == "3单元下料区") NGStation = "X03001002";
                        #region MyRegion
                        //var Wheel_Type = DBExtension.Read(details.Where(x => x.plcdetail_name == "R_Wheel_Type").First(), client);//车轮类型
                        //var Wheel_id = client.ReadByOrder<string>("R_Wheel_id", name);//车轮SN号
                        //if (string.IsNullOrEmpty(Wheel_id)) continue;
                        ////var Wheel_id = DBExtension.Read(details.Where(x => x.plcdetail_name == "R_Wheel_id").First(), client).ToString();
 
                        //var mes_Detail = mes_DetailRepository.FindFirst(x => x.SN == Wheel_id);
                        //if (mes_Detail == null)
                        //{
                        //    #region 日志记录
                        //    WriteDBLog.Error(name, $"读取桁架信息:\n车轮SN:{Wheel_id}\n\n未查询到工单详情", "PCS");
                        //    #endregion
                        //    continue;
                        //}
                        //var mes_Head = mes_HeadRepository.FindFirst(x => x.jobID == mes_Detail.jobID);
                        //if (mes_Head == null)
                        //{
                        //    #region 日志记录
                        //    WriteDBLog.Error(name, $"读取桁架信息:\n车轮SN:{Wheel_id}\n\n未查询到工单头表", "PCS");
                        //    #endregion
                        //    continue;
                        //}
                        #endregion
 
                        var finished = (bool)DBExtension.Read(details.Where(x => x.plcdetail_name == "R_load_finished").First(), client);//放料完成
                        var updatefinished = (bool)DBExtension.Read(details.Where(x => x.plcdetail_name == "W_Storage_update").First(), client);//货位状态更新
 
                        #region 货位状态更新复位
                        //if (!finished && updatefinished)
                        //{
                        //    client.WriteByOrder("W_Storage_update", false, name);//货位状态更新
                        //    #region 日志记录
                        //    WriteDBLog.Success(name + "货位状态更新复位", $"读取桁架信息:\nR_load_finished:{finished}\n\n写入桁架信息:\nW_Storage_update:false", "PCS");
                        //    #endregion
                        //}
                        #endregion
 
 
                        if (finished && !updatefinished)
                        {
                            client.WriteByOrder("W_Enabl_Load", false, name);//是否允许
 
                            #region 读取桁架信息
                            var WheelType = DBExtension.Read(details.Where(x => x.plcdetail_name == "R_Wheel_Type").First(), client);//车轮类型
                            var Wheel_SN = client.ReadByOrder<string>("R_Wheel_id", name);//车轮SN号
                            if (string.IsNullOrEmpty(Wheel_SN))
                            {
                                WriteDBLog.Error(name + "放料完成", $"读取桁架信息:\n车轮SN号为空", "PCS");
                                continue;
                            }
 
                            //var Wheel_id = DBExtension.Read(details.Where(x => x.plcdetail_name == "R_Wheel_id").First(), client).ToString();
                            var Mes_Work = workinfoRepository.Find(x => x.SN == Wheel_SN && x.processCode == "17").FirstOrDefault();
                            if (Mes_Work == null)
                            {
                                #region 日志记录
                                WriteDBLog.Error(name, $"读取桁架信息:\n车轮SN号:{Wheel_SN}\n\n未查询到工单详情", "PCS");
                                #endregion
                                continue;
                            }
                            #region MyRegion
                            //var Detail = mes_DetailRepository.FindFirst(x => x.SN == Wheel_SN);
                            //if (Detail == null)
                            //{
                            //    #region 日志记录
                            //    WriteDBLog.Error(name, $"读取桁架信息:\n车轮SN号:{Wheel_SN}\n\n未查询到工单详情", "PCS");
                            //    #endregion
                            //    continue;
                            //}
                            //var Head = mes_HeadRepository.FindFirst(x => x.jobID == Detail.jobID && x.processCode == "17");
                            //if (Head == null)
                            //{
                            //    #region 日志记录
                            //    WriteDBLog.Error(name, $"读取桁架信息:\n车轮SN号:{Wheel_SN}\n\n未查询到工单头表{Detail.jobID}", "PCS");
                            //    #endregion
                            //    continue;
                            //}
                            #endregion
 
                            #endregion
 
                            var AreaNr = (Int16)DBExtension.Read(details.Where(x => x.plcdetail_name == "W_AreaNr").First(), client);
                            if (AreaNr == 0)
                            {
                                client.WriteByOrder("W_Storage_update", true, name);//货位状态更新
                                continue;
                            }
 
 
                            var areaCode = PipelineJob.area_code(name);   //根据下料口单元名获取区域代码
                            var station = stationinfoRepository.FindFirst(x => x.area == areaCode && x.stationCode.Substring(x.stationCode.Length - 1, 1) == AreaNr.ToString());
                            if (station.quantity >= 5)
                                throw new Exception($"下料位信息更新失败!下料位车轮数量大于5:{station.stationCode}");
                            //station.bindSN = station.bindSN == string.Empty ? Wheel_id : station.bindSN + "," + Wheel_id;
                            if (!string.IsNullOrEmpty(station.bindSN))
                            {
                                if (station.bindSN.Contains(Wheel_SN))
                                    throw new Exception($"{station.stationCode}已存在车轮SN号:{Wheel_SN}");
                            }
 
                            station.bindSN = string.IsNullOrEmpty(station.bindSN) ? Wheel_SN : station.bindSN + "," + Wheel_SN;
                            station.billetID = string.IsNullOrEmpty(station.billetID) ? Mes_Work.billetID.ToString() : station.billetID + "," + Mes_Work.billetID;
                            station.quantity++;
                            if (station.quantity == 1)
                            {
                                station.stationType = Mes_Work.drawingNo;
                                station.Number = Mes_Work.workOrder;
                                station.heatNumber = Mes_Work.heatID;
                                station.tray_status = "StrogeTray";
                            }
                            //else if (station.stationCode == NGStation)
                            //{
                            //    station.heatNumber = string.IsNullOrEmpty(station.heatNumber) ? Mes_Work.heatID : station.heatNumber + "," + Mes_Work.heatID;
                            //}
 
                            var count = stationinfoRepository.Update(station, true);
                            if (count < 1)
                                throw new Exception($"下料位信息更新失败!下料位编号:{station.stationCode}");
 
                            #region 清空给桁架的下料信息
                            client.WriteByOrder("W_AreaNr", (Int16)0, name);//区域货位号
                            client.WriteByOrder("W_IndexNr", (Int16)0, name);//托盘上的第几个车轮
                            client.WriteByOrder("W_Storage_Type", (Int16)0, name);//托盘类型1-横放;2-竖放(暂时只有横放托盘)
                            client.WriteByOrder("W_Storage_update", true, name);//货位状态更新
                            WriteDBLog.Success(name + "桁架下料完成",
                                $"读取桁架信息:\nR_load_finished:{finished}\n车轮SN:{Wheel_SN}\n轮型ID:{WheelType}\n下料位号:{AreaNr}\n\n" +
                                $"处理写入桁架信息:\nW_AreaNr:{0}\nW_IndexNr:{0}\nW_Storage_Type:{0}\n\n" +
                                $"写入桁架信息:\nW_Storage_update:{true}\nW_Enabl_Load:{false}", "PCS");
                            #endregion
 
 
                            //client.WriteByOrder("W_Enabl_Load", false, name);//是否允许
 
                            #region 修改工单状态
                            var Detail = mes_DetailRepository.FindFirst(x => x.jobID == Mes_Work.jobID && x.SN == Mes_Work.SN);
                            Detail.Status = "下线";
                            Detail.FinishTime = DateTime.Now;
                            count = mes_DetailRepository.Update(Detail, true);
                            if (count < 1)
                                throw new Exception($"工单子表信息更新失败!车轮SN号:{Detail.SN}");
                            var Head = mes_HeadRepository.FindFirst(x => x.jobID == Detail.jobID);
                            Head.finishNum = Head.finishNum + 1;
                            count = mes_HeadRepository.Update(Head, true);
                            if (count < 1)
                                throw new Exception($"工单头表信息更新失败!工单号:{Head.jobID}");
                            #endregion
 
                            #region 日志记录
                            //WriteDBLog.Success(name + "放料完成", $"读取桁架信息:\n车轮SN:{Wheel_id}\n轮型ID:{Wheel_Type}\n下料位号:{AreaNr}" +
                            //    $"\n\n写入桁架信息:\nW_Storage_update:true" + $"\nW_Enabl_Load:false", "PCS");
                            #endregion
                        }
 
                        #region 货位状态更新复位
                        finished = (bool)DBExtension.Read(details.Where(x => x.plcdetail_name == "R_load_finished").First(), client);//放料完成
                        updatefinished = (bool)DBExtension.Read(details.Where(x => x.plcdetail_name == "W_Storage_update").First(), client);//货位状态更新
                        if (!finished && updatefinished)
                        {
                            client.WriteByOrder("W_Storage_update", false, name);//货位状态更新
                            #region 日志记录
                            WriteDBLog.Success(name + "货位状态更新复位", $"读取桁架信息:\nR_load_finished:{finished}\n\n写入桁架信息:\nW_Storage_update:{false}", "PCS");
                            #endregion
                        }
                        #endregion
 
                        var Request = (bool)DBExtension.Read(details.Where(x => x.plcdetail_name == "R_Request_Load").First(), client);
                        if (!Request) continue;
 
                        //var Area = client.ReadByOrder<Int16>("W_AreaNr", name);//读取货位号信息
                        //if (Area != 0) continue;
 
                        #region 读取桁架信息
                        var Wheel_Type = DBExtension.Read(details.Where(x => x.plcdetail_name == "R_Wheel_Type").First(), client);//车轮类型
                        var Wheel_id = client.ReadByOrder<string>("R_Wheel_id", name);//车轮SN号
                        if (string.IsNullOrEmpty(Wheel_id)) continue;
 
                        var Work = workinfoRepository.Find(x => x.SN == Wheel_id && x.processCode == "17").FirstOrDefault();
                        if (Work == null)
                        {
                            client.WriteByOrder("W_Enabl_Load", false, name);//信号为false桁架停止进入
                            WriteDBLog.Error(name, $"读取桁架信息:\n车轮SN:{Wheel_id}\n\n未查询到工单信息", "PCS");
                        }
 
                        #region MyRegion
                        //var mes_Detail = mes_DetailRepository.FindFirst(x => x.SN == Wheel_id);
                        //if (mes_Detail == null)
                        //{
                        //    #region 日志记录
                        //    client.WriteByOrder("W_Enabl_Load", false, name);//信号为false桁架停止进入
                        //    WriteDBLog.Error(name, $"读取桁架信息:\n车轮SN:{Wheel_id}\n\n未查询到工单详情", "PCS");
                        //    #endregion
                        //    continue;
                        //}
                        //var mes_Head = mes_HeadRepository.FindFirst(x => x.jobID == mes_Detail.jobID);
                        //if (mes_Head == null)
                        //{
                        //    #region 日志记录
                        //    client.WriteByOrder("W_Enabl_Load", false, name);//信号为false桁架停止进入
                        //    WriteDBLog.Error(name, $"读取桁架信息:\n车轮SN:{Wheel_id}\n\n未查询到工单头表", "PCS");
                        //    #endregion
                        //    continue;
                        //}
                        #endregion
 
                        #endregion
 
 
                        var Stations = stationinfoRepository.Find(x => x.area == PipelineJob.area_code(name)).OrderBy(x => x.lastUpdateTime).ToList();
 
 
 
                        //foreach (var station in Stations)     //任何一个下料位为空或禁用时,都会停止桁架
                        //{
                        //    if (!station.enable || station.location_state == LocationStateEnum.Empty.ToString())
                        //        client.WriteByOrder("W_Enabl_Load", false, name);//信号为false桁架停止进入
                        //}
 
                        #region MyRegion
                        //var currentAreaNr = (Int16)DBExtension.Read(details.Where(x => x.plcdetail_name == "W_Are00aNr").First(), client);
                        //var currentAreaCode = PipelineJob.area_code(name);   //根据下料口单元名获取区域代码
                        //var currentStation = stationinfoRepository.FindFirst(x => x.area == currentAreaCode && x.stationCode.Substring(x.stationCode.Length - 1, 1) == currentAreaNr.ToString());
                        //if (!currentStation.enable || currentStation.location_state == LocationStateEnum.Empty.ToString())
                        //    client.WriteByOrder("W_Enabl_Load", false, name);//信号为false桁架停止进入
                        #endregion
 
 
                        var wheel = PipelineJob.QueryMateriel(Work.drawingNo);
                        var wheel1 = PipelineJob.QueryMateriel1(Work.drawingNo);
                        if (wheel == null && wheel1 == null)
                        {
                            #region 日志记录
                            client.WriteByOrder("W_Enabl_Load", false, name);//信号为false桁架停止进入
                            WriteDBLog.Error(name, $"未查询到车轮信息!", "PCS");
                            #endregion
                            continue;
                        }
 
                        var PartStatus = DBExtension.Read(details.Where(x => x.plcdetail_name == "R_PartStatus").First(), client);//1:ok;2:nok
 
                        var load_zone = (bool)DBExtension.Read(details.Where(x => x.plcdetail_name == "R_load_zone").First(), client);
                        #region 日志记录
                        WriteDBLog.Success(name + "申请放料", $"读取桁架信息:\n车轮SN:{Wheel_id}\n轮型ID:{Wheel_Type}\n车轮状态:{PartStatus}\n是否进入下料区:{load_zone}", "PCS");
                        if (load_zone) continue;
                        #endregion
                        if ((Int16)PartStatus == 1)
                        {
                            dt_stationinfo Stationinfo = null;
                            if (Work.heatID != null)
                            {
                                Stationinfo = Stations
                           .Where(x => x.enable
                           && x.stationCode != NGStation
                           && x.location_state == LocationStateEnum.Stroge.ToString()
                           && x.quantity < 5
                           && x.stationType == Work.drawingNo
                           //&& x.tray_type == ((wheel == null ? wheel1.e : wheel.e) < 1100 ? "SmallTray" : "LargeTray")
                           && x.Number == Work.workOrder
                           && x.heatNumber == Work.heatID)
                           .OrderByDescending(x => x.quantity)
                           .FirstOrDefault();
                            }
                            else
                            {
                                Stationinfo = Stations
                           .Where(x => x.enable
                           && x.stationCode != NGStation
                           && x.location_state == LocationStateEnum.Stroge.ToString()
                           && x.quantity < 5
                           && x.stationType == Work.drawingNo
                           //&& x.tray_type == ((wheel == null ? wheel1.e : wheel.e) < 1100 ? "SmallTray" : "LargeTray")
                           && x.Number == Work.workOrder)
                           .OrderByDescending(x => x.quantity)
                           .FirstOrDefault();
                            }
 
                            if (Stationinfo == null)
                            {
                                #region 下料位条件不匹配直接触发入库任务
                                //var stationinfos = Stations.Where(x => x.enable && x.stationCode != NGStation && x.location_state == LocationStateEnum.Stroge.ToString() && x.quantity > 0).ToList();
                                //foreach (var stationinfo in stationinfos)
                                //{
                                //    agvtask(stationinfoRepository, stationinfo, (wheel == null ? wheel1.e : wheel.e), name == "3单元下料区" ? "TaskType_OutsourceInbound" : "TaskType_Inbound");
                                //}
                                #endregion
 
 
                                #region 触发NG任务
                                var NG = Stations.Where(x => x.enable && x.stationCode == NGStation && x.location_state == LocationStateEnum.Stroge.ToString() && x.quantity > 0).FirstOrDefault();
                                if (NG != null)
                                {
                                    if (NG.stationType != Work.drawingNo || NG.Number != Work.workOrder)
                                        agvtask(stationinfoRepository, NG, Work, "TaskType_OutsourceInbound");
                                }
                                #endregion
 
                                Stationinfo = Stations.Where(x => x.enable && x.stationCode != NGStation && x.location_state == LocationStateEnum.Stroge.ToString() && x.quantity == 0).FirstOrDefault();
 
                                #region MyRegion
                                //var stationinfo = Stations.Where(x => x.enable && x.stationCode != NGStation && x.location_state == LocationStateEnum.Stroge.ToString() && x.quantity > 0).FirstOrDefault();
                                //if (stationinfo != null)
                                //{
                                //    agvtask(stationinfoRepository, mes_Head, stationinfo, (wheel == null ? wheel1.e : wheel.e), name == "3单元下料区" ? "TaskType_OutsourceInbound" : "TaskType_Inbound");
                                //}
                                #endregion
                            }
 
                            if (Stationinfo != null)
                            {
                                #region 托盘车轮堆垛第四个时下发另外一个炉号的入库任务
                                if (Stationinfo.quantity + 1 >= 4)
                                {
                                    var stationinfo = Stations.Where(x => x.enable && x.stationCode != NGStation && x.stationCode != Stationinfo.stationCode && x.location_state == LocationStateEnum.Stroge.ToString() && x.quantity > 0).FirstOrDefault();
                                    //foreach (var stationinfo in stationinfos)
                                    //{
                                    if (stationinfo != null)
                                        agvtask(stationinfoRepository, stationinfo, Work, name == "3单元下料区" ? "TaskType_OutsourceInbound" : "TaskType_Inbound");
                                    //}
                                }
 
                                #endregion
 
                                #region 托盘类型不一致需更换托盘
                                //if (Stationinfo.tray_type != ((wheel == null ? wheel1.e : wheel.e) < 1100 ? "SmallTray" : "LargeTray"))
                                if (Stationinfo.tray_type != "LargeTray" && (wheel == null ? wheel1.e : wheel.e) > 1100)
                                {
                                    client.WriteByOrder("W_Enabl_Load", false, name);//信号为false桁架停止进入
                                    Stationinfo.location_state = LocationStateEnum.Abnormal.ToString();
                                    Stationinfo.tray_type = Stationinfo.tray_type == "SmallTray" ? "SmallTray/LargeTray" : "LargeTray/SmallTray";
                                    Stationinfo.remark = "托盘类型不一致需更换托盘";
                                    stationinfoRepository.Update(Stationinfo, true);
                                    continue;
                                }
                                #endregion
 
                                var Pipelinedetails = plcRepository.Find(x => x.plcdetail_iotype == Pipelineplc.plcinfo_iotyep && x.plcdetail_number == Stationinfo.stationCode).ToList();
                                var PalletSignal = (Int16)DBExtension.Read(Pipelinedetails.Where(x => x.plcdetail_name == "R_PalletSignal").First(), Pipeline_client);//读取托盘信号:1:有,2无
 
                                var MaterialSignal = (Int16)DBExtension.Read(Pipelinedetails.Where(x => x.plcdetail_name == "R_MaterialSignal").First(), Pipeline_client);//读取货物信号:1:有,2无
 
                                //var Wheel_id = client.ReadByOrder<string>("R_Wheel_id", name);
                                if (PalletSignal == 1)
                                {
                                    List<string> list = new List<string>();
                                    if (Stationinfo.bindSN != null)
                                    {
                                        var SNS = Stationinfo.bindSN.Split(",");
                                        foreach (var SN in SNS)
                                        {
                                            if (!string.IsNullOrEmpty(SN))
                                                list.Add(SN);
                                        }
                                    }
 
                                    #region 货位数量与光电不匹配,报警
                                    if (list.Count > 0 && MaterialSignal != 1)
                                    {
                                        client.WriteByOrder("W_Enabl_Load", false, name);//信号为false桁架停止进入
                                        Pipeline_client.WriteByOrder("W_AlarmSignal", (Int16)2, name);//报警
                                        Stationinfo.location_state = LocationStateEnum.Abnormal.ToString();
                                        Stationinfo.remark = "车轮数量与光电不匹配";
                                        stationinfoRepository.Update(Stationinfo, true);
                                        WriteDBLog.Error(name + "报警", $"货位编号:{Stationinfo.stationCode};错误信息:{Stationinfo.remark}", "PCS");
                                        //WriteDBLog.Error(name+"申请下料",$"{Stationinfo.stationCode}的货位","PCS");
                                    }
                                    else if (list.Count < 1 && MaterialSignal == 1)
                                    {
                                        client.WriteByOrder("W_Enabl_Load", false, name);//信号为false桁架停止进入
                                        Pipeline_client.WriteByOrder("W_AlarmSignal", (Int16)2, name);//报警
                                        Stationinfo.location_state = LocationStateEnum.Abnormal.ToString();
                                        Stationinfo.remark = "车轮数量与光电不匹配";
                                        stationinfoRepository.Update(Stationinfo, true);
                                        WriteDBLog.Error(name + "报警", $"货位编号:{Stationinfo.stationCode};错误信息:{Stationinfo.remark}", "PCS");
                                    }
 
                                    #endregion
 
                                    //缓存架上车轮数量与SN号数量不一致
                                    if (list.Count != Stationinfo.quantity)
                                    {
                                        Stationinfo.location_state = LocationStateEnum.Abnormal.ToString();
                                        Stationinfo.remark = "车轮数量与SN号数量不一致";
                                        stationinfoRepository.Update(Stationinfo, true);
                                        continue;
                                        //写入桁架报警信号
                                        //throw new Exception("下料位车轮数量与SN号数量不一致,下料位编号:" + Stationinfo.stationCode);
                                    }
                                    var area = Convert.ToInt16(Stationinfo.stationCode.Substring(Stationinfo.stationCode.Length - 1, 1));
                                    client.WriteByOrder("W_AreaNr", (Int16)area, name);//区域货位号
                                    client.WriteByOrder("W_IndexNr", (Int16)(Stationinfo.quantity + 1), name);//托盘上的第几个车轮
                                    client.WriteByOrder("W_Storage_Type", (Int16)1, name);//托盘类型1-横放;2-竖放(暂时只有横放托盘)
                                    client.WriteByOrder("W_Enabl_Load", true, name);//是否允许
                                    #region 日志记录
                                    WriteDBLog.Success(name + "允许放料", $"读取桁架信息:\n车轮SN:{Wheel_id}\n轮型ID:{Wheel_Type}\n车轮状态:OK" +
                                        $"\n读取托盘光电信号:{PalletSignal}" +
                                        $"\n\n写入桁架信息:\n下料位号:{area}\n第几个车轮:{(Stationinfo.quantity + 1)}\n托盘类型:1\nW_Enabl_Load:true", "PCS");
                                    #endregion
                                    continue;
                                }
                            }
                            //else
                            //{
                            //    #region 没托盘可用,触发入库任务
                            //    var stationinfos = Stations.Where(x => x.enable && x.stationCode != NGStation && x.location_state == LocationStateEnum.Stroge.ToString() && x.quantity > 0).ToList();
                            //    foreach (var stationinfo in stationinfos)
                            //    {
                            //        agvtask(stationinfoRepository, stationinfo, Work, name == "3单元下料区" ? "TaskType_OutsourceInbound" : "TaskType_Inbound");
                            //    }
                            //    #endregion
                            //}
 
                        }
                        else if ((Int16)PartStatus == 2)
                        {
                            dt_stationinfo Stationinfo = null;
                            //if (mes_Detail.heatID != null)
                            //{
                            //    Stationinfo = Stations
                            //.Where(x => x.enable
                            //&& x.stationCode == NGStation
                            //&& x.location_state == LocationStateEnum.Stroge.ToString()
                            //&& x.quantity < 5
                            //&& x.stationType == mes_Head.drawingNo
                            //&& x.tray_type == ((wheel == null ? wheel1.e : wheel.e) < 1100 ? "SmallTray" : "LargeTray")
                            //&& x.Number == mes_Detail.jobID
                            //&& x.heatNumber == mes_Detail.heatID).FirstOrDefault();
                            //}
                            //else
                            //{
                            //var tray_type = "SmallTray";
                            //if ((wheel == null ? wheel1.e : wheel.e) > 1100) tray_type = "LargeTray";
                            Stationinfo = Stations
                        .Where(x => x.enable
                        && x.stationCode == NGStation
                        && x.location_state == LocationStateEnum.Stroge.ToString()
                        && x.quantity < 5
                        && x.stationType == Work.drawingNo
                        //&& x.tray_type == ((wheel == null ? wheel1.e : wheel.e) < 1100 ? "SmallTray" : "LargeTray")
                        /*&& x.Number == mes_Detail.jobID*/).FirstOrDefault();
                            //}
 
                            if (Stationinfo == null)
                            {
                                //var stationinfo = Stations.Where(x => x.enable && x.stationCode == NGStation && x.location_state == LocationStateEnum.Stroge.ToString() && x.quantity > 0).FirstOrDefault();
                                //if (stationinfo != null)
                                //{
                                //    agvtask(stationinfoRepository, stationinfo, (wheel == null ? wheel1.e : wheel.e), "TaskType_OutsourceInbound");
                                //}
                                Stationinfo = Stations.Where(x => x.enable && x.stationCode == NGStation && x.location_state == LocationStateEnum.Stroge.ToString() && x.quantity == 0).FirstOrDefault();
                            }
                            if (Stationinfo != null)
                            {
                                #region 托盘类型不一致需更换托盘
                                //if (Stationinfo.tray_type != ((wheel == null ? wheel1.e : wheel.e) < 1100 ? "SmallTray" : "LargeTray"))
                                if (Stationinfo.tray_type != "LargeTray" && (wheel == null ? wheel1.e : wheel.e) > 1100)
                                {
                                    Stationinfo.location_state = LocationStateEnum.Abnormal.ToString();
                                    Stationinfo.tray_type = Stationinfo.tray_type == "SmallTray" ? "SmallTray/LargeTray" : "LargeTray/SmallTray";
                                    Stationinfo.remark = "托盘类型不一致需更换托盘";
                                    stationinfoRepository.Update(Stationinfo, true);
                                }
                                #endregion
                                var Pipelinedetails = plcRepository.Find(x => x.plcdetail_iotype == Pipelineplc.plcinfo_iotyep && x.plcdetail_number == Stationinfo.stationCode).ToList();
                                var PalletSignal = (Int16)DBExtension.Read(Pipelinedetails.Where(x => x.plcdetail_name == "R_PalletSignal").First(), Pipeline_client);//读取托盘信号:1:有,2无
                                var MaterialSignal = (Int16)DBExtension.Read(Pipelinedetails.Where(x => x.plcdetail_name == "R_MaterialSignal").First(), Pipeline_client);//读取货物信号:1:有,2无
                                if (PalletSignal == 1)
                                {
                                    List<string> list = new List<string>();
                                    if (Stationinfo.bindSN != null)
                                    {
                                        var SNS = Stationinfo.bindSN.Split(",");
                                        foreach (var SN in SNS)
                                        {
                                            if (!string.IsNullOrEmpty(SN))
                                                list.Add(SN);
                                        }
                                    }
 
                                    #region 货位数量与光电不匹配,报警
                                    //if (list.Count > 0 && MaterialSignal != 1)
                                    //    Pipeline_client.WriteByOrder("W_AlarmSignal", (Int16)2, name);//报警
                                    //else if (list.Count < 1 && MaterialSignal == 1)
                                    //    Pipeline_client.WriteByOrder("W_AlarmSignal", (Int16)2, name);//报警
 
                                    if (list.Count > 0 && MaterialSignal != 1)
                                    {
                                        client.WriteByOrder("W_Enabl_Load", false, name);//信号为false桁架停止进入
                                        Pipeline_client.WriteByOrder("W_AlarmSignal", (Int16)2, name);//报警
                                        Stationinfo.location_state = LocationStateEnum.Abnormal.ToString();
                                        Stationinfo.remark = "车轮数量与光电不匹配";
                                        stationinfoRepository.Update(Stationinfo, true);
                                        WriteDBLog.Error(name + "报警", $"货位编号:{Stationinfo.stationCode};错误信息:{Stationinfo.remark}", "PCS");
                                        //WriteDBLog.Error(name+"申请下料",$"{Stationinfo.stationCode}的货位","PCS");
                                    }
                                    else if (list.Count < 1 && MaterialSignal == 1)
                                    {
                                        client.WriteByOrder("W_Enabl_Load", false, name);//信号为false桁架停止进入
                                        Pipeline_client.WriteByOrder("W_AlarmSignal", (Int16)2, name);//报警
                                        Stationinfo.location_state = LocationStateEnum.Abnormal.ToString();
                                        Stationinfo.remark = "车轮数量与光电不匹配";
                                        stationinfoRepository.Update(Stationinfo, true);
                                        WriteDBLog.Error(name + "报警", $"货位编号:{Stationinfo.stationCode};错误信息:{Stationinfo.remark}", "PCS");
                                    }
                                    #endregion
 
                                    //缓存架上车轮数量与SN号数量不一致
                                    if (list.Count != Stationinfo.quantity)
                                    {
                                        Stationinfo.location_state = LocationStateEnum.Abnormal.ToString();
                                        Stationinfo.remark = "车轮数量与SN号数量不一致";
                                        stationinfoRepository.Update(Stationinfo, true);
                                        continue;
                                        //写入桁架报警信号
                                        //throw new Exception("下料位车轮数量与SN号数量不一致,下料位编号:" + Stationinfo.stationCode);
                                    }
                                    var area = Convert.ToInt16(Stationinfo.stationCode.Substring(Stationinfo.stationCode.Length - 1, 1));
                                    client.WriteByOrder("W_AreaNr", (Int16)area, name);//区域货位号
                                    client.WriteByOrder("W_IndexNr", (Int16)(Stationinfo.quantity + 1), name);//托盘上的第几个车轮
                                    client.WriteByOrder("W_Storage_Type", (Int16)1, name);//托盘类型1-横放;2-竖放(暂时只有横放托盘)
                                    client.WriteByOrder("W_Enabl_Load", true, name);//是否允许
                                    #region 日志记录
                                    WriteDBLog.Success(name + "允许放料", $"读取桁架信息:\n车轮SN:{Wheel_id}\n轮型ID:{Wheel_Type}\n车轮状态:NG" +
                                        $"\n读取托盘光电信号:{PalletSignal}" +
                                        $"\n\n写入桁架信息:\n下料位号:{area}\n第几个车轮:{(Stationinfo.quantity + 1)}\n托盘类型:1\nW_Enabl_Load:true", "PCS");
                                    #endregion
                                    continue;
                                }
                            }
                        }
                        #region
                        //var Stationinfo = Stations
                        //    .Where(x => x.enable
                        //    && x.location_state == LocationStateEnum.Stroge.ToString()
                        //    && x.quantity < 5
                        //    && x.stationType == PipelineJob.QueryMateriel(Wheel_Type)
                        //    && x.Number == mes_Detail.jobID
                        //    && x.heatNumber == mes_Detail.heatID).FirstOrDefault();
                        //if (Stationinfo == null)
                        //    Stationinfo = Stations.Where(x => x.enable && x.location_state == LocationStateEnum.Stroge.ToString() && x.quantity == 0).FirstOrDefault();
                        //if (Stationinfo != null)
                        //{
 
                        //}
 
                        //List<string> StationCodes = new List<string>() { "X01001001", "X01001002", "X01001003" };
                        //if (name == "2单元下料区")
                        //    StationCodes = new List<string>() { "X02001001", "X02001002", "X02001003" };
                        //if (name == "3单元下料区")
                        //    StationCodes = new List<string>() { "X03001001", "X03001002" };
 
                        #endregion
 
                        #region
                        //for (int i = 0; i < StationCodes.Count - 1; i++)
                        //{
                        //    var PartStatus = (Int32)DBExtension.Read(details.Where(x => x.plcdetail_name == "R_PartStatus").First(), client);//1:ok;2:nok
                        //    string NG = StationCodes[StationCodes.Count - 1];//NG货位
                        //    var station = stationinfoRepository.FindFirst(x => x.stationCode == StationCodes[i] && x.enable && x.location_state == LocationStateEnum.Stroge.ToString() && x.quantity < 5);
                        //    if (station == null) continue;
                        //    var Pipelinedetails = plcRepository.Find(x => x.plcdetail_iotype == Pipelineplc.plcinfo_iotyep && x.plcdetail_number == StationCodes[i]).ToList();
                        //    var PalletSignal = (Int16)DBExtension.Read(Pipelinedetails.Where(x => x.plcdetail_name == "R_PalletSignal").First(), client);//读取托盘信号:1:有,2无
                        //    if (PalletSignal == 1)
                        //    {
                        //        if (PartStatus == 1 && NG != station.stationCode)
                        //        {
                        //            client.WriteByOrder("W_AreaNr", (Int16)i + 1, name);//区域货位号
                        //            client.WriteByOrder("W_IndexNr", (Int16)station.quantity + 1, name);//托盘上的第几个车轮
                        //            client.WriteByOrder("W_Storage_Type", (Int16)1, name);//托盘类型1-横放;2-竖放(暂时只有横放托盘)
                        //            client.WriteByOrder("W_Enabl_Load", true, name);//是否允许
                        //            return;
                        //        }
                        //        else if (PartStatus == 2 && NG == station.stationCode)
                        //        {
                        //            client.WriteByOrder("W_AreaNr", (Int16)i + 1, name);//区域货位号
                        //            client.WriteByOrder("W_IndexNr", (Int16)station.quantity + 1, name);//托盘上的第几个车轮
                        //            client.WriteByOrder("W_Storage_Type", (Int16)1, name);//托盘类型1-横放;2-竖放(暂时只有横放托盘)
                        //            client.WriteByOrder("W_Enabl_Load", true, name);//是否允许
                        //            return;
                        //        }
                        //    }
                        //}
                        #endregion
                    }
                    catch (Exception ex)
                    {
                        WriteDBLog.Error(name, $"错误信息:{ex.Message}", "PCS");
                    }
                }
            }
            catch (Exception ex)
            {
                WriteDBLog.Error("下料区", $"错误信息:{ex.Message}", "PCS");
            }
        }
        #endregion
 
        /// <summary>
        /// AGV任务
        /// </summary>
        public void agvtask(Idt_stationinfoRepository stationinfoRepository, dt_stationinfo stationinfo, VV_Mes_Workinfo Work, string tasktype)
        {
            VOLContext Context = new VOLContext();
            Idt_agvtaskRepository agvtaskRepository = new dt_agvtaskRepository(Context);
            Idt_mes_headRepository mes_HeadRepository = new dt_mes_headRepository(Context);
 
            var task = agvtaskRepository.Find(x => x.agv_fromaddress == stationinfo.stationCode).FirstOrDefault();
            if (task != null) return;
            //var mes_Head = mes_HeadRepository.Find(x => x.jobID == Work.jobID).FirstOrDefault();
            //if (mes_Head == null)
            //{
            //    stationinfo.remark = "触发入库任务,但未找到工单头表!";
            //    stationinfo.location_state = LocationStateEnum.Abnormal.ToString();
            //    stationinfoRepository.Update(stationinfo, true);
            //    return;
            //}
 
            dt_stationinfo TargetLocation = null;
            //int CompeletedNum = Convert.ToInt32(mes_Head.quantity) - Convert.ToInt32(mes_Head.finishNum);
            //if (mes_Head.quantity <= 50) tasktype = "TaskType_OutsourceInbound";
 
            if (tasktype == "TaskType_OutsourceInbound" || !string.IsNullOrEmpty(Work.area))
                TargetLocation = StationTask.GetEmptyLocation(stationinfoRepository);
            else if (tasktype == "TaskType_Inbound")
                TargetLocation = GetLocation.GetEmptyLocation(stationinfoRepository, Work, stationinfo);
            //TargetLocation = GetLocation.GetEmptyLocation(stationinfoRepository, mes_Head, stationinfo);
            if (TargetLocation != null)
            {
                dt_agvtask agvtask = new dt_agvtask()
                {
                    agv_fromaddress = stationinfo.stationCode,
                    agv_id = Guid.NewGuid(),
                    agv_tasknum = IdenxManager.GetTaskNo("KH-", "WMS"),
                    agv_grade = 1,
                    agv_createtime = DateTime.Now,
                    agv_taskstate = "Create",
                    agv_materielid = stationinfo.stationType,
                    agv_qty = stationinfo.quantity,
                    agv_tasktype = tasktype,
                    agv_toaddress = TargetLocation.stationCode,
                    agv_userid = "系统",
                    bindSN = stationinfo.bindSN,
                    agv_worktype = Convert.ToInt32(Work.processCode),
                    agv_materbarcode = Work.materialCode,
                    agv_Traytype = stationinfo.tray_type,
                    jobID = stationinfo.Number,
                    agv_TrayStatus = stationinfo.tray_status
                };
                agvtaskRepository.Add(agvtask, true);
                stationinfo.location_state = LocationStateEnum.InBusy.ToString();
                stationinfoRepository.Update(stationinfo, true);
                TargetLocation.location_state = LocationStateEnum.InBusy.ToString();
                TargetLocation.stationType = agvtask.agv_materielid;
                TargetLocation.heatNumber = stationinfo.heatNumber;
                TargetLocation.Number = agvtask.jobID;
                stationinfoRepository.Update(TargetLocation, true);
            }
        }
    }
}