ke_junjie
2025-06-04 101c57ec4c28bc3c36e49c50a926e9e7c0dd0247
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
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
using iWareSda_QQJF.SRM.SrmService;
using iWareSda_QQJF.SRMTRAN.SrmTranService;
using iWareSda_QQJF.SrmTranModel;
using iWareSda_QQJF.WCSNEW.EDM;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
 
namespace iWareSda_QQJF.WCSNEW.生成主任务
{
    public static class CreateMainTask
    {
        public static Dictionary<int, string> InErrorMsg = new Dictionary<int, string>()
        {
            {47,""},
            {48,""},
            {61,""},
            {65,""},
            {32,""},
            {15,""},
 
        };
        public static bool[] needMove = { false, false, false, false, false, false, false, false, false };
        static Dictionary<int, string> oldContainerName = new Dictionary<int, string>();
        static Dictionary<int, int> times = new Dictionary<int, int>();
        /// <summary>
        /// 生成入库任务
        /// </summary>
        /// <param name="obj"></param>
        public static void CreatInTask(object obj)
        {
            try
            {
                while (true)
                {
                    string erromsg = "";
 
                    #region 准备数据
                    int tranObj = (int)obj;
                    int tranId = 0;
                    //int tranObj = 0;
                    SRMTRAN.SrmTranService.SrmTranService srv = new SRMTRAN.SrmTranService.SrmTranService();
                    //for (int i = 0; i < 6; i++)
                    //{
                    //tranObj = i;
                    List<int> hasGoodList = srv.HasTranGoods();
                    if (hasGoodList == null || hasGoodList.Count == 0)
                    {
                        continue;
                    }
                    if (hasGoodList[tranObj] == 1)
                    {
                        switch (tranObj)
                        {
                            case 0: tranId = 47; break;
                            case 1: tranId = 48; break;
                            case 2: tranId = 61; break;
                            case 3: tranId = 65; break;
                            case 4: tranId = 32; break;
                            case 5: tranId = 15; break;
                            default: break;
                        }
                    }
                    else
                    {
                        continue;
                    }
                    //}
                    if (tranId == 0)
                    {
                        continue;
                    }
                    //srv.GetSrmConveyorStationInfo(tranId);
                    EntitySrmTranView TranInfo = JsonConvert.DeserializeObject<EntitySrmTranView>(srv.GetSrmConveyorStationInfo(tranId));
                    string containerName = TranInfo.Code;//扫码扫到的
                    decimal weightGet = (decimal)TranInfo.weihgt;
                    if (oldContainerName.ContainsKey(tranId) && times.ContainsKey(tranId))
                    {
                        if (containerName != oldContainerName[tranId])
                        {
                            oldContainerName[tranId] = containerName;
                            times[tranId] = 0;
                            continue;
                        }
                        else
                        {
                            times[tranId]++;
                            if (times[tranId] < 10)
                            {
                                continue;
                            }
                            else
                            {
                                times[tranId] = 0;
                            }
                        }
                    }
                    else
                    {
                        if (!oldContainerName.ContainsKey(tranId))
                        {
                            oldContainerName.Add(tranId, containerName);
                        }
                        if (!times.ContainsKey(tranId))
                        {
                            times.Add(tranId, 0);
                        }
                        continue;
                    }
                    #endregion
 
                    using (Model db = new Model())
                    {
 
                        var container = db.BASE_CONTAINER.FirstOrDefault(x => x.CONTAINERNAME == containerName && x.ENABLE == 1);
                        decimal totalWeightAll = 0;
                        decimal difference = 0;
                        if (container == null || container.ISLOCK == 1)
                        {
                            Helper.Speaking("输送线" + tranId.ToString() + "号器具不可用");
                            InErrorMsg[tranId] = "器具不可用";
                            Thread.Sleep(5000);
                            WZ.Useful.Commons.LogTextHelper.WriteLine("WCS", "生成入库任务", "输送线" + tranId.ToString() + "号器具不可用");
                            continue;
                        }
                        if (container != null)
                        {
                            //判断是否已入库
                            BASE_PLACE_VS_CONTAINER pvcOfContainer = db.BASE_PLACE_VS_CONTAINER.FirstOrDefault(x => x.CONTAINERID == container.ID && x.ENABLE == 1);
                            TASK_TASK taskOfContainer = db.TASK_TASK.FirstOrDefault(x => x.CONTAINERID == container.ID && x.ENABLE == 1 && x.HASFINISHED == 0);
                            if (pvcOfContainer != null || taskOfContainer != null)
                            {
                                //重复入库
                                Helper.Speaking("输送线" + tranId.ToString() + "号重复入库");
                                InErrorMsg[tranId] = "重复入库";
                                Thread.Sleep(5000);
                                WZ.Useful.Commons.LogTextHelper.WriteLine("WCS", "生成入库任务", "输送线" + tranId.ToString() + "号重复入库");
                                continue;
                            }
                            List<BASE_CONTAINER_VS_ITEM> cviList = container.BASE_CONTAINER_VS_ITEM.Where(x => x.ENABLE == 1).ToList();
                            ////最大库容
                            //bool needContinue = false;
                            //foreach (var i in cviList)
                            //{
                            //    int num = db.View_BASE_PLACE_VS_CONTAINER.Where(x => x.itemName == i.BASE_ITEM.ITEMNAME).Sum(x => (x.itemNum ?? 0));
                            //    if ((i.BASE_ITEM.MAXSTORAGE ?? 0) < num)
                            //    {
                            //        Helper.Speaking("零件" + i.BASE_ITEM.ITEMNAME + "超出最大库容");
                            //        Thread.Sleep(5000);
                            //        needContinue = true;
                            //        break;
                            //    }
                            //}
                            //if (needContinue)
                            //{
                            //    continue;
                            //}
                            //称重
                            totalWeightAll = totalWeightAll + container.WEIGHT ?? 0;
                            difference = difference + container.WEIGHTDIFFERENCE ?? 0;
                            foreach (var i in cviList)
                            {
                                totalWeightAll = totalWeightAll + i.TOTALWEIGHT ?? 0;//计算理论重量
                                difference = difference + (i.ITEMNUM ?? 0) * (i.BASE_ITEM.WEIGHTDIFFERENCE ?? 0);//理论总公差
                            }
                            if (totalWeightAll + difference > weightGet && totalWeightAll - difference < weightGet)//符合称重
                            {
                                WZ.Useful.Commons.LogTextHelper.WriteLine("重量:" + weightGet, "器具:" + container.CONTAINERNAME, "输送线:" + tranId.ToString());
                            }
                            else
                            {
                                //超重
                                //WZ.Useful.Commons.LogTextHelper.WriteLine("超重", "器具:"+ container.CONTAINERNAME, "输送线:"+tranId.ToString());
                                Helper.Speaking("输送线" + tranId.ToString() + "号超重" + "器具" + container.CONTAINERNAME + "称重" + weightGet.ToString());
                                InErrorMsg[tranId] = "超重";
                                Thread.Sleep(5000);
                                WZ.Useful.Commons.LogTextHelper.WriteLine("WCS", "生成入库任务", "输送线" + tranId.ToString() + "号超重" + "器具" + container.CONTAINERNAME + "称重" + weightGet.ToString());
                                continue;
                            }
 
                            BASE_PLACE to_place = CreateIn(container.CONTAINERNAME);
                            BASE_CONTAINER_VS_ITEM cvi = db.BASE_CONTAINER_VS_ITEM.FirstOrDefault(x => (x.CONTAINERID ?? 0) == container.ID && x.ENABLE == 1);
                            int userId = 1;
                            if (cvi != null)
                            {
                                userId = cvi.CREATEUSERID ?? 1;
                            }
                            if (to_place != null)
                            {
                                WZ.Useful.Commons.LogTextHelper.WriteLine("满入记录日志:第一次找库位" + to_place.PLACE + ";器具号-" + container.CONTAINERNAME);
                                TASK_TASK task = new TASK_TASK()
                                {
                                    CONTAINERID = container.ID,
                                    CREATETIME = DateTime.Now,
                                    CREATEUSERID = userId,
                                    ENABLE = 1,
                                    ERRORDEVICEID = 0,
                                    ERRORMSG = 0,
                                    HASFINISHED = 0,
                                    HASREADED = 0,
                                    ISERROR = 0,
                                    OUTTYPE = 0,
                                    ISNEEDREDIRECT = 0,
                                    TASKLEVEL = 0,
                                    //ORDERID = 0,
                                    TASKSTATUS = "新建",
                                    TASKTYPE = 1,//入库
                                    SOURCEPLACE = tranId.ToString(),//待填,根据扫码枪扫的位置口确定入库口
                                    TOPLACE = to_place.PLACE,
                                    LASTWEIGHT = weightGet.ToString(),
                                };
                                db.TASK_TASK.Add(task);
                                if (db.SaveChanges() > 0)
                                {
                                    InErrorMsg[tranId] = "";
                                    CreateTaskRecord(task);
                                    srv.ClearTranGoods(tranId);//复位
                                }
                            }
                            else
                            {
                                erromsg = "没有充足的空库位!";
                                InErrorMsg[tranId] = "没有充足的空库位";
                                WZ.Useful.Commons.LogTextHelper.WriteLine("WCS", "生成入库任务", container.CONTAINERNAME + "没有充足的空库位");
 
                            }
                        }
                        else
                        {
                            erromsg = "未识别到该托盘号!";
                            InErrorMsg[tranId] = "未识别到该托盘号";
                            WZ.Useful.Commons.LogTextHelper.WriteLine("WCS", "生成入库任务", container.CONTAINERNAME + "未识别到该托盘号");
                        }
                    }
                    Thread.Sleep(1000);
                }
            }
            catch (Exception ex)
            {
                WZ.Useful.Commons.LogTextHelper.WriteLine("WCS", "生成入库任务", ex.ToString());
                throw;
            }
        }
 
        /// <summary>
        /// 根据出库单确定要出库库位并生成主任务
        /// 任务分解
        /// </summary>
        /// <param name="obj"></param>
        public static void CreatOutTask(object obj)
        {
 
            while (true)
            {
                try
                {
                    using (Model db = new Model())
                    {
                        var outOrdePlan = db.ORDER_OUTORDER.OrderBy(x => x.DOTIME).FirstOrDefault(x => (x.CHECKOUTNUM ?? 0) < x.TOTALOUTNUM && x.ENABLE == 1 && x.ORDERSTATUS == "执行中");
                        if (outOrdePlan != null)
                        {
                            if (outOrdePlan.ORDERTYPE != "空器具出库")
                            {
                                string itemname = outOrdePlan.BASE_ITEM.ITEMNAME;
                                int planeOutCount = (int)outOrdePlan.TOTALOUTNUM - (int)outOrdePlan.CHECKOUTNUM;
 
                                int errorCount = 0;
                                int taskCount = 0;
                                View_BASE_PLACE_VS_CONTAINER pvcOut = CreateOut(itemname, planeOutCount, outOrdePlan.ORDERTYPE, out errorCount, out taskCount);
                                if (pvcOut != null)
                                {
                                    WZ.Useful.Commons.LogTextHelper.WriteLine("", pvcOut.place, (pvcOut.isLock ?? 0).ToString());
                                    BASE_CONTAINER_VS_ITEM cvi = db.BASE_CONTAINER_VS_ITEM.FirstOrDefault(x => x.BASE_CONTAINER.CONTAINERNAME == pvcOut.containerName && x.ENABLE == 1 && x.BASE_ITEM.ITEMNAME == pvcOut.itemName);
                                    if (CreateTask(db, pvcOut, outOrdePlan))
                                    {
                                        cvi.ORDER_OUTORDER = outOrdePlan;
                                        outOrdePlan.CHECKOUTNUM = outOrdePlan.CHECKOUTNUM + pvcOut.itemNum;
                                        if (outOrdePlan.CHECKOUTNUM >= outOrdePlan.TOTALOUTNUM)
                                        {
                                            //outOrdePlan.ORDERSTATUS = "完成";
                                            outOrdePlan.ISFINISH = 1;
                                        }
                                    }
 
                                    db.SaveChanges();
                                }
                                else
                                {
                                    string status = "库存不足 ";
                                    //无可用库位
                                    if (errorCount != 9)
                                    {
                                        status = status + "设备异常 ";
                                    }
                                    if (taskCount != 0)
                                    {
                                        status = status + "任务占用 ";
                                    }
                                    outOrdePlan.ISFINISH = 1;
                                    outOrdePlan.ORDERSTATUS = status;
                                    db.SaveChanges();
                                }
                            }
                            else
                            {//表示 该出库计划是 空器具出库类型的
                                try
                                {
                                    List<TASK_TASK> taskList = db.TASK_TASK.Where(x => x.HASFINISHED == 0 && x.ENABLE == 1).ToList();
                                    List<string> containerNameList = new List<string>();
                                    foreach (var i in taskList)
                                    {
                                        containerNameList.Add(i.BASE_CONTAINER.CONTAINERNAME);
                                    }
                                    //排除故障设备的库位
                                    List<int> errorList = new List<int>();
                                    SrmService srmService = new SrmService();
                                    for (int i = 0; i < 9; i++)
                                    {
                                        if (srmService.IsNotAlarm(i + 1))
                                        {
                                            errorList.Add(i + 1);
                                        }
                                    }
                                    var list = outOrdePlan.BASE_ITEM.USECONTAINERTYPE.Split(',').ToList();
                                    List<BASE_PLACE_VS_CONTAINER> pvcList = db.BASE_PLACE_VS_CONTAINER.Where(x => list.Contains(x.BASE_CONTAINER.CONTAINERTYPE) && x.ENABLE == 1 && x.BASE_CONTAINER.BASE_CONTAINER_VS_ITEM.Count() == 0 && !containerNameList.Contains(x.BASE_CONTAINER.CONTAINERNAME) && errorList.Contains((x.BASE_PLACE.SRMID ?? 0)) && (x.BASE_PLACE.ISLOCK ?? 0) != 1).ToList();
                                    var group = pvcList.GroupBy(x => x.BASE_PLACE.SRMID);
                                    int srmid = 0;
                                    int count = 0;
                                    foreach (var i in group)
                                    {
                                        if (i.Count() > count || srmid == 0)
                                        {
                                            count = i.Count();
                                            srmid = i.Key ?? 0;
                                        }
                                    }
                                    BASE_PLACE_VS_CONTAINER pvc = pvcList.OrderByDescending(x => x.BASE_PLACE.PLACELEVEL).FirstOrDefault(x => x.BASE_PLACE.SRMID == srmid);
                                    if (pvc != null)
                                    {
                                        WZ.Useful.Commons.LogTextHelper.WriteLine("", pvc.BASE_PLACE.PLACE, (pvc.BASE_PLACE.ISLOCK ?? 0).ToString());
                                        if (CreateTask(db, pvc, outOrdePlan))
                                        {
                                            //cvi.ORDER_OUTORDER = outOrdePlan;
                                            outOrdePlan.CHECKOUTNUM = outOrdePlan.CHECKOUTNUM + 1;
                                            if (outOrdePlan.CHECKOUTNUM >= outOrdePlan.TOTALOUTNUM)
                                            {
                                                //outOrdePlan.ORDERSTATUS = "完成";
                                                outOrdePlan.ISFINISH = 1;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        string status = "库存不足 ";
                                        //无可用库位
                                        if (errorList.Count != 9)
                                        {
                                            status = status + "设备异常 ";
                                        }
                                        if (taskList.Count != 0)
                                        {
                                            status = status + "任务占用 ";
                                        }
                                        outOrdePlan.ISFINISH = 1;
                                        outOrdePlan.ORDERSTATUS = status;
                                        db.SaveChanges();
 
                                    }
 
                                    db.SaveChanges();
                                }
                                catch (Exception ex)
                                {
                                    WZ.Useful.Commons.LogTextHelper.WriteLine("WCS", "生成空器具出库任务", ex.ToString());
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    WZ.Useful.Commons.LogTextHelper.WriteLine("WCS", "生成出库任务", ex.ToString());
                }
                Thread.Sleep(1000);
 
            }
 
        }
 
        /// <summary>
        /// 创建主任务
        /// </summary>
        /// <param name="db"></param>
        /// <param name="pvcvi"></param>
        /// <returns></returns>
        private static bool CreateTask(Model db, View_BASE_PLACE_VS_CONTAINER pvcvi, ORDER_OUTORDER order)
        {
            try
            {
                string tranLine = (order.TRANLINE ?? 0).ToString();
                if (string.IsNullOrEmpty(tranLine) || tranLine == "0")
                {
                    tranLine = "67";
                }
                using (Model edm = new Model())
                {
                    //获取每个口的任务占用数量
                    int tranNum1 = edm.TASK_TASK.Where(x => x.TOPLACE == "67" && x.HASFINISHED == 0).Count();//东分拣
                    int tranNum2 = edm.TASK_TASK.Where(x => x.TOPLACE == "51" && x.HASFINISHED == 0).Count();//东冲压1
                    int tranNum3 = edm.TASK_TASK.Where(x => x.TOPLACE == "64" && x.HASFINISHED == 0).Count();//东冲压2
                    int tranNum4 = edm.TASK_TASK.Where(x => x.TOPLACE == "34" && x.HASFINISHED == 0).Count();//西分拣
                    int tranNum5 = edm.TASK_TASK.Where(x => x.TOPLACE == "12" && x.HASFINISHED == 0).Count();//西空托盘
                    int tranNum6 = edm.TASK_TASK.Where(x => x.TOPLACE == "19" && x.HASFINISHED == 0).Count();//西焊装1
                    int tranNum7 = edm.TASK_TASK.Where(x => x.TOPLACE == "30" && x.HASFINISHED == 0).Count();//西焊装2
 
                    if ((order.TRANLINE ?? 0) == 999)
                    {
                        //if (tranNum6 <= tranNum7)
                        if (int.Parse(pvcvi.place.Substring(0, 1)) > 5)//5号巷道以上走19
                        {
                            tranLine = "19";
                        }
                        else
                        {
                            tranLine = "30";
                        }
                    }
                    else if ((order.TRANLINE ?? 0) == 997)
                    {
                        //if (tranNum2 <= tranNum3 && tranNum2 <= tranNum1)
                        if (int.Parse(pvcvi.place.Substring(0, 1)) >= 5)//5号巷道以上走51
                        {
                            tranLine = "51";
                        }
                        //else if (tranNum3 <= tranNum1)
                        //{
                        //    tranLine = "64";
                        //}
                        //else
                        //{
                        //    tranLine = "67";
                        //}
                        else
                        {
                            tranLine = "64";
                        }
                    }
 
                }
                if (pvcvi != null)
                {
                    var container = db.BASE_CONTAINER.FirstOrDefault(x => x.CONTAINERNAME == pvcvi.containerName);
                    //创建叉车任务
                    TASK_TASK task = new TASK_TASK()
                    {
 
                        CONTAINERID = container.ID,
                        CREATETIME = DateTime.Now,
                        CREATEUSERID = order.CREATORID,
                        ENABLE = 1,
                        ERRORDEVICEID = 0,
                        ERRORMSG = 0,
                        HASFINISHED = 0,
                        HASREADED = 0,
                        ISERROR = 0,
                        OUTTYPE = 0,
                        ISNEEDREDIRECT = 0,
                        TASKLEVEL = 0,
                        ORDERID = order.ID,
                        TASKSTATUS = "新建",
                        TASKTYPE = 2,//出库
                        SOURCEPLACE = pvcvi.place,
                        TOPLACE = tranLine,//待填根据ordertype是焊装还是调价,在选择出库口的位置
                    };
                    db.TASK_TASK.Add(task);
 
                    db.SaveChanges();
                    CreateTaskRecord(task);
                }
                return true;
            }
            catch (Exception ex)
            {
                WZ.Useful.Commons.LogTextHelper.WriteLine("WCS", "CreateTask", ex.ToString());
                return false;
            }
        }
 
 
        /// <summary>
        /// 创建主任务
        /// </summary>
        /// <param name="db"></param>
        /// <param name="pvc"></param>
        /// <returns></returns>
        private static bool CreateTask(Model db, BASE_PLACE_VS_CONTAINER pvc, ORDER_OUTORDER order)
        {
            try
            {
                string tranLine = order.TRANLINE.ToString();
                using (Model edm = new Model())
                {
                    //获取每个口的任务占用数量
                    int tranNum1 = edm.TASK_TASK.Where(x => x.TOPLACE == "67" && x.HASFINISHED == 0).Count();//东分拣
                    int tranNum2 = edm.TASK_TASK.Where(x => x.TOPLACE == "51" && x.HASFINISHED == 0).Count();//东冲压1
                    int tranNum3 = edm.TASK_TASK.Where(x => x.TOPLACE == "64" && x.HASFINISHED == 0).Count();//东冲压2
                    int tranNum4 = edm.TASK_TASK.Where(x => x.TOPLACE == "34" && x.HASFINISHED == 0).Count();//西分拣
                    int tranNum5 = edm.TASK_TASK.Where(x => x.TOPLACE == "12" && x.HASFINISHED == 0).Count();//西空托盘
                    int tranNum6 = edm.TASK_TASK.Where(x => x.TOPLACE == "19" && x.HASFINISHED == 0).Count();//西焊装1
                    int tranNum7 = edm.TASK_TASK.Where(x => x.TOPLACE == "30" && x.HASFINISHED == 0).Count();//西焊装2
                    int tranNum8 = edm.TASK_TASK.Where(x => x.TOPLACE == "68" && x.HASFINISHED == 0).Count();//东冲压3,[Editby kejj,20230614]
 
                    if ((order.TRANLINE ?? 0) == 999)
                    {
                        //if (tranNum6 <= tranNum7)
                        if (int.Parse(pvc.BASE_PLACE.PLACE.Substring(0, 1)) > 5)//5号巷道以上走19
                        {
                            tranLine = "19";
                        }
                        else
                        {
                            tranLine = "30";
                        }
                    }
                    else if ((order.TRANLINE ?? 0) == 997)
                    {
                        //if (tranNum2 <= tranNum3 && tranNum2 <= tranNum1)
                        if (int.Parse(pvc.BASE_PLACE.PLACE.Substring(0, 1)) >= 5)//5号巷道以上走51
                        {
                            tranLine = "51";
                        }
                        //else if (tranNum3 <= tranNum1)
                        //{
                        //    tranLine = "64";
                        //}
                        //else
                        //{
                        //    tranLine = "67";
                        //}
                        else
                        {
                            tranLine = "64";
                        }
                    }
                    //else if ((order.TRANLINE ?? 0) == 1000)
                    //{
                    //    tranLine = "68";
                    //}
                }
                if (pvc != null)
                {
                    var container = pvc.BASE_CONTAINER;
                    TASK_TASK task = new TASK_TASK()
                    {
 
                        CONTAINERID = container.ID,
                        CREATETIME = DateTime.Now,
                        CREATEUSERID = order.CREATORID,
                        ENABLE = 1,
                        ERRORDEVICEID = 0,
                        ERRORMSG = 0,
                        HASFINISHED = 0,
                        HASREADED = 0,
                        ISERROR = 0,
                        OUTTYPE = 0,
                        ISNEEDREDIRECT = 0,
                        TASKLEVEL = 0,
                        ORDERID = order.ID,
                        TASKSTATUS = "新建",
                        TASKTYPE = 2,//出库
                        SOURCEPLACE = pvc.BASE_PLACE.PLACE,
                        TOPLACE = tranLine,//待填根据ordertype是焊装还是调价,在选择出库口的位置
                    };
                    db.TASK_TASK.Add(task);
                    //创建叉车任务
                    //去掉验证PDA上选择目的地才能生成叉车任务的限制 【Editby shaocx,2023-06-07】
                    //我靠,还不能直接 去掉验证PDA上选择目的地才能生成叉车任务的限制,因为会在这一行报错  carTask.TODESTINATION = order.BASE_PRODUCTIONLINE.PRODUCTIONLINENAME; 【Editby shaocx,2023-06-11】
                    //if ((order.TOLINEID ?? 0) != 0)[Editby kejj,20230621]
                    //{
                    //CAR_CARTASK carTask = new CAR_CARTASK();
                    ////通过出库口找目的地
                    //BASE_PRODUCTIONLINE pl = db.BASE_PRODUCTIONLINE.FirstOrDefault(x => x.PRODUCTIONLINECODE == task.TOPLACE);
                    //string detail = "";
                    //foreach (var i in task.BASE_CONTAINER.BASE_CONTAINER_VS_ITEM)
                    //{
                    //    if (!string.IsNullOrEmpty(i.BASE_ITEM.ITEMDES))
                    //    {
                    //        detail = detail + i.BASE_ITEM.ITEMNAME + " " + i.BASE_ITEM.ITEMDES + ":" + i.ITEMNUM + "个;\n";
                    //    }
                    //}
                    //if (pl != null)
                    //{
                    //    carTask.CARTASKNAME = IWareDataAccess.Car.CARTASK.CarTaskSqlFunc.GetCode();
                    //    carTask.FROMDESTINATION = pl.PRODUCTIONLINENAME;
                    //    carTask.TODESTINATION = order.BASE_PRODUCTIONLINE?.PRODUCTIONLINENAME;
                    //    carTask.CONTAINERID = task.CONTAINERID;
                    //    carTask.TASKSTATUS = "新建";
                    //    carTask.ENABLE = 1;
                    //    carTask.UPDATETIME = DateTime.Now;
                    //    carTask.CREATORID = order.CREATORID;
                    //    carTask.ORDER_OUTORDER = order;
                    //    carTask.ITEMDETAIL = detail;
                    //    db.CAR_CARTASK.Add(carTask);
                    //}
                    //}
                    db.SaveChanges();
                    //创建任务记录
                    CreateTaskRecord(task);
                    return true;
 
                }
                return false;
 
            }
            catch (Exception ex)
            {
                WZ.Useful.Commons.LogTextHelper.WriteLine("WCS", "CreateTask", ex.ToString());
                return false;
            }
        }
 
        /// <summary>
        /// 找出出库库存
        /// </summary>
        /// <param name="itemName"></param>
        /// <param name="itemNum"></param>
        /// <returns></returns>
        public static View_BASE_PLACE_VS_CONTAINER CreateOut(string itemName, int itemNum, string type, out int errorCount, out int taskCount)
        {
            errorCount = 0;
            taskCount = 0;
            using (Model edm = new Model())
            {
                //筛选任务中的
                List<TASK_TASK> taskList = edm.TASK_TASK.Where(x => x.HASFINISHED == 0 && x.ENABLE == 1).ToList();
                List<string> containerNameList = new List<string>();
                foreach (var i in taskList)
                {
                    containerNameList.Add(i.BASE_CONTAINER.CONTAINERNAME);
                }
 
                //排除故障设备的库位
                List<string> errorList = new List<string>();
                SrmService srmService = new SrmService();
                for (int i = 1; i < 10; i++)
                {
 
                    if (srmService.IsNotAlarm(i))
                    {
                        errorList.Add((i).ToString());
                    }
                    //*/
                    //模拟,暂时写死  [Editby shaocx,2022-10-08]
                    //errorList.Add((i).ToString());
                }
 
                List<View_BASE_PLACE_VS_CONTAINER> pvcList = new List<View_BASE_PLACE_VS_CONTAINER>();
                if (type != null && (type.Contains("冲压待返修") || type.Contains("焊装待返修")))
                {
                    pvcList = edm.View_BASE_PLACE_VS_CONTAINER.Where(x => x.itemName == itemName && x.enable == 1 && x.status == "IN" && !containerNameList.Contains(x.containerName) && (x.isLock ?? 0) == 0 && errorList.Contains(x.place.Substring(0, 1)) && (x.inType != null && x.inType == type)).ToList();
                }
                else
                {
                    pvcList = edm.View_BASE_PLACE_VS_CONTAINER.Where(x => x.itemName == itemName && x.enable == 1 && x.status == "IN" && !containerNameList.Contains(x.containerName) && (x.isLock ?? 0) == 0 && errorList.Contains(x.place.Substring(0, 1)) && (x.inType == null || !x.inType.Contains("返修"))).ToList();
                }
                //优先余料回库
                List<View_BASE_PLACE_VS_CONTAINER> pvcReturnList = pvcList.Where(x => x.inType == "余料回库").ToList();
                if (pvcReturnList.Count != 0)//存在
                {
                    View_BASE_PLACE_VS_CONTAINER pvcReturnOut = pvcReturnList.OrderBy(x => x.itemNum).ThenByDescending(x => x.placeLevel).FirstOrDefault();
                    return pvcReturnOut;
                }
 
                //选择零件数量大的巷道
                var group = pvcList.GroupBy(x => x.place.Substring(0, 1)).ToList();
                string srmid = "";
                int count = 0;
                foreach (var i in group)
                {
                    if (i.Count() < count || srmid == "")
                    {
                        count = i.Count();
                        srmid = i.Key;
                    }
                }
                //判断是否故障
 
                List<View_BASE_PLACE_VS_CONTAINER> pvcSrmList = edm.View_BASE_PLACE_VS_CONTAINER.Where(x =>
                    x.place.Substring(0, 1) == srmid &&
                    x.itemName == itemName &&
                    x.enable == 1 &&
                    (x.isLock ?? 0) == 0 &&
                    (x.isFull ?? 0) == 1 &&
                    !containerNameList.Contains(x.containerName)).ToList();
                //View_BASE_PLACE_VS_CONTAINER pvcOut = pvcSrmList.OrderByDescending(x => x.placeLevel).FirstOrDefault(x => x.itemNum >= itemNum);
                //if (pvcOut != null)
                //{
                //    //存在一箱满足
                //}
                //else
                //{
                //    //不存在一箱满足,则出最少
                //    pvcOut = pvcSrmList.OrderByDescending(x => x.itemNum).ThenByDescending(x => x.placeLevel).FirstOrDefault();
                //}
 
                //优先出少的
                DateTime timeNow = DateTime.Now;
 
                View_BASE_PLACE_VS_CONTAINER pvcOut = pvcSrmList.OrderBy(x => x.CVIUpdateTime).ThenBy(x => x.itemNum).ThenByDescending(x => x.placeLevel).FirstOrDefault(x => (timeNow - (x.CVIUpdateTime ?? DateTime.Now)).TotalDays > 5);
                if (pvcOut == null)
                {
                    pvcOut = pvcSrmList.OrderBy(x => x.itemNum).ThenByDescending(x => x.placeLevel).FirstOrDefault();
                }
                errorCount = errorList.Count;
                taskCount = taskList.Count;
                return pvcOut;
            }
        }
 
 
        #region 寻找空库位,原先的方法
        /// <summary>
        /// 找出入库库位(寻找空库位)
        /// </summary>
        //public static BASE_PLACE CreateIn(string containerName)
        //{
        //    using (Model edm = new Model())
        //    {
        //        BASE_CONTAINER container = edm.BASE_CONTAINER.FirstOrDefault(x => x.CONTAINERNAME == containerName);
        //        //排除故障设备的库位
        //        List<int> goodSrmList = new List<int>();//可用的堆垛机集合 [Editby shaocx,2022-10-08]
        //        SrmService srmService = new SrmService();
        //        for (int i = 1; i < 10; i++)
        //        {
        //            if (srmService.IsNotAlarm(i))
        //            {
        //                goodSrmList.Add(i);
        //            }
        //        }
 
        //        //注意:表BASE_PALLET的PALLETTYPE 要对应 表BASE_PLACETYPE的PlaceType字段
        //        //下面是根据托盘类型去寻找 能存放该托盘类型的库区 
        //        //例如:2巷道和4巷道,都是能存放托盘类型 G和H的
        //        List<BASE_PLACE> placeList = edm.BASE_PLACE.Where(x => container.BASE_PALLET.PALLETTYPE.Contains(x.BASE_PLACETYPE.PLACETYPE) && (x.ISLOCK ?? 0) == 0 && (x.ISFULL ?? 0) == 0 && goodSrmList.Contains((x.SRMID ?? 0))).ToList();
        //        //记录任务中的占用数量
        //        List<TASK_TASK> taskList = edm.TASK_TASK.Where(x => x.HASFINISHED == 0 && x.TASKTYPE == 1 && x.BASE_CONTAINER.BASE_PALLET.PALLETTYPE == container.BASE_PALLET.PALLETTYPE && x.TASKTYPE == 1).ToList();
        //        var groupTask = taskList.GroupBy(x => x.TOPLACE.Substring(0, 1)).ToList();//当前未结束的、指定托盘类型的入库任务目标位置的分组集合
 
        //        //计算此种的数量
        //        BASE_CONTAINER_VS_ITEM cvi = edm.BASE_CONTAINER_VS_ITEM.FirstOrDefault(x => x.BASE_CONTAINER.CONTAINERNAME == containerName);
        //        List<View_BASE_PLACE_VS_CONTAINER> placeFullList = new List<View_BASE_PLACE_VS_CONTAINER>();
        //        if (cvi == null)//空器具
        //        {
        //            placeFullList = edm.View_BASE_PLACE_VS_CONTAINER.Where(x => x.containerName.Contains(container.CONTAINERTYPE) && string.IsNullOrEmpty(x.itemName)).ToList();
 
        //        }
        //        else
        //        {
        //            placeFullList = edm.View_BASE_PLACE_VS_CONTAINER.Where(x => x.itemName == cvi.BASE_ITEM.ITEMNAME).ToList();
        //        }
 
        //        //数量少的先
        //        var groupFull = placeFullList.GroupBy(x => x.place.Substring(0, 1)).ToList();
 
        //        var groupSrmList = placeList.GroupBy(x => x.SRMID).ToList();//按照可用的堆垛机分组的库位集合
        //        int srmid = 0;//要寻找空库位的堆垛机区域
        //        List<string> fullPlaceList = new List<string>();
        //        int count = 0;
        //        int countFull = 0;
 
        //        foreach (var groupSrm in groupSrmList)
        //        {
        //            int placeCount = 0;//可用的 库位数量
        //            var task = groupTask.FirstOrDefault(x => x.Key == groupSrm.Key.ToString());
        //            if (task != null)
        //            {
        //                placeCount = groupSrm.Count() - task.Count();
        //                foreach (var z in task)
        //                {
        //                    fullPlaceList.Add(z.TOPLACE);
        //                }
        //            }
        //            else
        //            {
        //                placeCount = groupSrm.Count();
        //            }
        //            if (placeCount > 2)//避免满库
        //            {
 
        //                //器具=数量比较
        //                var groupFullOne = groupFull.FirstOrDefault(x => x.Key == (groupSrm.Key ?? 0).ToString());
        //                int fullPlaceCount = 0;
        //                if (groupFullOne != null)
        //                {
        //                    fullPlaceCount = groupFullOne.Count();
        //                }
        //                //int fullPlaceCount = groupFull[(i.Key ?? 0)].Count();
        //                if (fullPlaceCount < countFull || srmid == 0)
        //                {
        //                    if (fullPlaceCount == countFull)
        //                    {
        //                        if (placeCount > count)
        //                        {
        //                            countFull = fullPlaceCount;
        //                            srmid = groupSrm.Key ?? 0;
        //                            count = placeCount;
        //                        }
        //                    }
        //                    else
        //                    {
        //                        countFull = fullPlaceCount;
        //                        srmid = groupSrm.Key ?? 0;
        //                        count = placeCount;
        //                    }
 
        //                }
        //                ////空库位比较
        //                //if (placeCount > count || srmid == 0)
        //                //{
        //                //    count = placeCount;
        //                //    srmid = i.Key ?? 0;
        //                //}
        //            }
        //        }
        //        ////兼容
        //        BASE_PLACE finPlace = placeList.OrderBy(x => x.PLACELEVEL).ThenBy(x => x.LAYER).ThenBy(x => System.Math.Abs((x.COL ?? 0) - 9)).FirstOrDefault(x => x.SRMID == srmid && !fullPlaceList.Contains(x.PLACE));
        //        if (finPlace == null)
        //        {
 
        //            if (container.BASE_PALLET.PALLETTYPE.Contains("H"))
        //            {
 
        //                List<BASE_PLACE> placeList2 = edm.BASE_PLACE.Where(x => x.BASE_PLACETYPE.PLACETYPE.Contains("G") && (x.ISLOCK ?? 0) == 0 && (x.ISFULL ?? 0) == 0 && goodSrmList.Contains((x.SRMID ?? 0))).ToList();
        //                //记录任务中的占用数量
        //                List<TASK_TASK> taskList2 = edm.TASK_TASK.Where(x => x.HASFINISHED == 0 && x.TASKTYPE == 1 && x.BASE_CONTAINER.BASE_PALLET.PALLETTYPE == container.BASE_PALLET.PALLETTYPE && x.TASKTYPE == 1).ToList();
        //                var groupTask2 = taskList2.GroupBy(x => x.TOPLACE.Substring(0, 1)).ToList();
 
        //                var group2 = placeList2.GroupBy(x => x.SRMID).ToList();
        //                int srmid2 = 0;
        //                List<string> fullPlaceList2 = new List<string>();
        //                count = 0;
        //                countFull = 0;
 
        //                foreach (var i in group2)
        //                {
        //                    int placeCount = 0;
        //                    var task = groupTask2.FirstOrDefault(x => x.Key == i.Key.ToString());
        //                    if (task != null)
        //                    {
        //                        placeCount = i.Count() - task.Count();
        //                        foreach (var z in task)
        //                        {
        //                            fullPlaceList2.Add(z.TOPLACE);
        //                        }
        //                    }
        //                    else
        //                    {
        //                        placeCount = i.Count();
        //                    }
        //                    if (placeCount > 2)//避免满库
        //                    {
 
        //                        //器具=数量比较
        //                        var groupFullOne = groupFull.FirstOrDefault(x => x.Key == (i.Key ?? 0).ToString());
        //                        int fullPlaceCount = 0;
        //                        if (groupFullOne != null)
        //                        {
        //                            fullPlaceCount = groupFullOne.Count();
        //                        }
        //                        //int fullPlaceCount = groupFull[(i.Key ?? 0)].Count();
        //                        if (fullPlaceCount < countFull || srmid2 == 0)
        //                        {
        //                            if (fullPlaceCount == countFull)
        //                            {
        //                                if (placeCount > count)
        //                                {
        //                                    countFull = fullPlaceCount;
        //                                    srmid2 = i.Key ?? 0;
        //                                    count = placeCount;
        //                                }
        //                            }
        //                            else
        //                            {
        //                                countFull = fullPlaceCount;
        //                                srmid2 = i.Key ?? 0;
        //                                count = placeCount;
        //                            }
 
        //                        }
        //                        ////空库位比较
        //                        //if (placeCount > count || srmid2 == 0)
        //                        //{
        //                        //    count = placeCount;
        //                        //    srmid2 = i.Key ?? 0;
        //                        //}
        //                    }
        //                }
        //                finPlace = placeList2.OrderBy(x => x.PLACELEVEL).ThenBy(x => x.LAYER).ThenBy(x => System.Math.Abs((x.COL ?? 0) - 9)).FirstOrDefault(x => x.SRMID == srmid2 && !fullPlaceList2.Contains(x.PLACE));
        //            }
        //        }
        //        return finPlace;
        //    }
        //}
        #endregion
 
        #region 寻找空库位,现在的方法
        /// <summary>
        /// 找出入库库位(寻找空库位)
        /// </summary>
        public static BASE_PLACE CreateIn(string containerName, int iSrm = 0)
        {
            using (Model edm = new Model())
            {
                #region 组织数据
 
                BASE_CONTAINER container = edm.BASE_CONTAINER.FirstOrDefault(x => x.CONTAINERNAME == containerName);
 
                //排除故障设备的库位
                List<int> goodSrmList = new List<int>();//可用的堆垛机集合 [Editby shaocx,2022-10-08]
                SrmService srmService = new SrmService();
                if (iSrm == 0)
                {
                    for (int i = 1; i < 10; i++)
                    {
 
                        if (srmService.IsNotAlarm(i))
                        {
                            goodSrmList.Add(i);
                        }
                        //*/
                        //模拟,暂时写死  [Editby shaocx,2022-10-08]
                        //goodSrmList.Add(i);
                    }
                }
                else
                {
                    goodSrmList.Add(iSrm);
                }
 
                //计算此种的数量
                BASE_CONTAINER_VS_ITEM cvi = edm.BASE_CONTAINER_VS_ITEM.FirstOrDefault(x => x.BASE_CONTAINER.CONTAINERNAME == containerName);
                List<View_BASE_PLACE_VS_CONTAINER> pvcList = new List<View_BASE_PLACE_VS_CONTAINER>();//PVC列表
                if (cvi == null)
                {//表示该托盘没有被物料绑定
                    //寻找 库存中,托盘类型一致,并且没有绑定物料的 PVC列表
                    pvcList = edm.View_BASE_PLACE_VS_CONTAINER.Where(x => x.containerName.Contains(container.CONTAINERTYPE) && string.IsNullOrEmpty(x.itemName)).ToList();
                }
                else
                {//表示该托盘已经被物料绑定
                    //寻找 库存中,绑定该中物料的 PVC列表
                    pvcList = edm.View_BASE_PLACE_VS_CONTAINER.Where(x => x.itemName == cvi.BASE_ITEM.ITEMNAME).ToList();
                }
 
                //数量少的先
                var groupPvcList = pvcList.GroupBy(x => x.place.Substring(0, 1)).ToList();//按照巷道分组的PVC分组
 
 
                //记录任务中的占用数量
                List<TASK_TASK> doingTaskList = edm.TASK_TASK.Where(x => x.HASFINISHED == 0 && x.TASKTYPE == 1 && x.BASE_CONTAINER.BASE_PALLET.PALLETTYPE == container.BASE_PALLET.PALLETTYPE && x.TASKTYPE == 1).ToList();
                var groupDoingTask = doingTaskList.GroupBy(x => x.TOPLACE.Substring(0, 1)).ToList();//当前未结束的、指定托盘类型的入库任务目标位置的分组集合
 
 
                #endregion
 
                //第一次获取空库位
                //注意:表BASE_PALLET的PALLETTYPE 要对应 表BASE_PLACETYPE的PlaceType字段
                //下面是根据托盘类型去寻找 能存放该托盘类型的库区 
                //例如:2巷道和4巷道,都是能存放托盘类型 G和H的
                List<BASE_PLACE> emptyPlaceList = edm.BASE_PLACE.Where(x => container.BASE_PALLET.PALLETTYPE.Contains(x.BASE_PLACETYPE.PLACETYPE) && (x.ISLOCK ?? 0) == 0 && (x.ISFULL ?? 0) == 0 && goodSrmList.Contains((x.SRMID ?? 0))).ToList();
 
 
                BASE_PLACE finPlace = null;
 
                bool isContainsH = container.BASE_PALLET.PALLETTYPE.Contains("H");
                finPlace = CommonGetEmptyPlace(emptyPlaceList, groupDoingTask, groupPvcList);
                if (finPlace == null)
                {
                    if (isContainsH)
                    {
                        List<BASE_PLACE> emptyPlaceList2 = edm.BASE_PLACE.Where(x => x.BASE_PLACETYPE.PLACETYPE.Contains("G") && (x.ISLOCK ?? 0) == 0 && (x.ISFULL ?? 0) == 0 && goodSrmList.Contains((x.SRMID ?? 0))).ToList();
 
 
                        finPlace = CommonGetEmptyPlace(emptyPlaceList2, groupDoingTask, groupPvcList);
                    }
                }
                return finPlace;
            }
        }
 
        /// <summary>
        /// 通用获取空库位方法
        /// </summary>
        /// <param name="emptyPlaceList"></param>
        /// <param name="groupDoingTask"></param>
        /// <param name="groupPvcList"></param>
        /// <returns></returns>
        private static BASE_PLACE CommonGetEmptyPlace(List<BASE_PLACE> emptyPlaceList, List<IGrouping<string, TASK_TASK>> groupDoingTask, List<IGrouping<string, View_BASE_PLACE_VS_CONTAINER>> groupPvcList)
        {
            var groupEmptyPlaceList = emptyPlaceList.GroupBy(x => x.SRMID).ToList();//按照可用的堆垛机分组的库位集合
            int srmid = 0;//要寻找空库位的堆垛机区域
            List<string> fullPlaceList = new List<string>();
            int last_empty_placeCount = 0;//循环的上个巷道的可用空库位的个数
            int last_fullPlaceCount = 0;//循环的上个巷道的满库位个数
 
            foreach (var groupEmptyPlace in groupEmptyPlaceList)
            {
                int empty_placeCount = 0;//可用的 库位数量
                int fullPlaceCount = 0;//本巷道的已经有托盘占用的个数
 
                //获取剩余空闲库位数
                CommonGetEmpty_placeCount(ref empty_placeCount, ref fullPlaceList, ref fullPlaceCount, groupDoingTask, groupEmptyPlace, groupPvcList);
 
                if (empty_placeCount > 4)//避免满库,由2修改为4,2024/1/9
                {//这里为什么要判断必须空库位数量大于2,因为必须要留2个位置用于 双伸位的,转运功能!
 
                    //if (fullPlaceCount == 0)
                    //{//说明该巷道没有满库位的情况啊,要优先入他 【Editby shaocx,2022-11-8】
                    //    CommonSetCountValue(fullPlaceCount, groupEmptyPlace, empty_placeCount, ref last_fullPlaceCount, ref srmid, ref last_empty_placeCount);
                    //    break;//终止循环 
                    //}
                    //int fullPlaceCount = groupFull[(i.Key ?? 0)].Count();
                    if (srmid == 0)
                    {//第一次循环
                        CommonSetCountValue(fullPlaceCount, groupEmptyPlace, empty_placeCount, ref last_fullPlaceCount, ref srmid, ref last_empty_placeCount);
                        continue;
                    }
                    if (empty_placeCount > last_empty_placeCount)
                    {//如果本巷道可用空库位的数量 大于 循环的上个巷道的可用空库位的个数
                        //即一个原则:哪个巷道的空库位多,就放哪个巷道!
                        CommonSetCountValue(fullPlaceCount, groupEmptyPlace, empty_placeCount, ref last_fullPlaceCount, ref srmid, ref last_empty_placeCount);
                    }
 
                    //if (fullPlaceCount < last_fullPlaceCount)
                    //{//如果是第一次循环 或者是 本巷道的满库位的个数 小于 循环的上个巷道的满库位个数
                    //    if (fullPlaceCount == last_fullPlaceCount)
                    //    {//如果本次巷道的满库位个数 等于 循环的上个巷道的满库位个数
                    //        if (empty_placeCount > last_empty_placeCount)
                    //        {//如果本巷道可用空库位的数量 大于 循环的上个巷道的可用空库位的个数
                    //            //last_countFull = fullPlaceCount;
                    //            //srmid = groupSrm.Key ?? 0;
                    //            //last_count = placeCount;
                    //            CommonSetCountValue(fullPlaceCount, groupEmptyPlace, empty_placeCount, ref last_fullPlaceCount, ref srmid, ref last_empty_placeCount);
                    //        }
                    //    }
                    //    else
                    //    {
                    //        //last_countFull = fullPlaceCount;
                    //        //srmid = groupSrm.Key ?? 0;
                    //        //last_count = placeCount;
                    //        CommonSetCountValue(fullPlaceCount, groupEmptyPlace, empty_placeCount, ref last_fullPlaceCount, ref srmid, ref last_empty_placeCount);
                    //    }
 
                    //}
                    ////空库位比较
                    //if (placeCount > count || srmid == 0)
                    //{
                    //    count = placeCount;
                    //    srmid = i.Key ?? 0;
                    //}
                }
            }
            //优先查询 指定srmid的库区
            BASE_PLACE finPlace = CommonQueryPlace(emptyPlaceList, srmid, fullPlaceList);
            return finPlace;
        }
 
        /// <summary>
        /// 获取空库位数
        /// </summary>
        /// <param name="empty_placeCount"></param>
        /// <param name="fullPlaceList"></param>
        /// <param name="fullPlaceCount"></param>
        /// <param name="groupDoingTask"></param>
        /// <param name="groupEmptyPlace"></param>
        /// <param name="groupPvcList"></param>
        private static void CommonGetEmpty_placeCount(ref int empty_placeCount, ref List<string> fullPlaceList, ref int fullPlaceCount,
            List<IGrouping<string, TASK_TASK>> groupDoingTask, IGrouping<int?, BASE_PLACE> groupEmptyPlace, List<IGrouping<string, View_BASE_PLACE_VS_CONTAINER>> groupPvcList)
        {
            //此处再增加筛选,去掉有库存的、和正在有任务占用的,防止满入 【EditBy shaocx,2022-11-09】
            var doingTasks = groupDoingTask.FirstOrDefault(x => x.Key == groupEmptyPlace.Key.ToString());
            if (doingTasks != null)
            {
                empty_placeCount = groupEmptyPlace.Count() - doingTasks.Count();
                foreach (var z in doingTasks)
                {
                    fullPlaceList.Add(z.TOPLACE);//正在有任务占用的
                }
            }
            else
            {
                empty_placeCount = groupEmptyPlace.Count();
            }
            //过滤有PVC数据的
            var groupFullOne = groupPvcList.FirstOrDefault(x => x.Key == (groupEmptyPlace.Key ?? 0).ToString());//寻找该巷道的PVC分组
            if (groupFullOne != null)
            {
                foreach (var z in groupFullOne)
                {
                    fullPlaceList.Add(z.place);//有库存的
                }
                //empty_placeCount = empty_placeCount - groupFullOne.Count();
 
                fullPlaceCount = groupFullOne.Count();
            }
        }
 
        private static BASE_PLACE CommonQueryPlace(List<BASE_PLACE> placeList, int srmid, List<string> fullPlaceList)
        {
            BASE_PLACE finPlace = placeList.OrderBy(x => x.PLACELEVEL).ThenBy(x => x.LAYER).ThenBy(x => System.Math.Abs((x.COL ?? 0) - 9)).FirstOrDefault(x => x.SRMID == srmid && !fullPlaceList.Contains(x.PLACE));
            return finPlace;
        }
 
        /// <summary>
        /// 统一设置数值
        /// </summary>
        /// <param name="fullPlaceCount"></param>
        /// <param name="groupEmptyPlace"></param>
        /// <param name="empty_placeCount"></param>
        /// <param name="last_fullPlaceCount"></param>
        /// <param name="srmid"></param>
        /// <param name="last_empty_placeCount"></param>
        private static void CommonSetCountValue(int fullPlaceCount, IGrouping<int?, BASE_PLACE> groupEmptyPlace, int empty_placeCount, ref int last_fullPlaceCount, ref int srmid, ref int last_empty_placeCount)
        {
            last_fullPlaceCount = fullPlaceCount;
            srmid = groupEmptyPlace.Key ?? 0;
            last_empty_placeCount = empty_placeCount;
        }
        #endregion
 
 
 
        /// <summary>
        /// 创建任务记录
        /// </summary>
        /// <param name="db"></param>
        /// <param name="pvc"></param>
        /// <param name="order"></param>
        public static bool CreateTaskRecord(TASK_TASK task)
        {
            using (Model edm = new Model())
            {
                if (task.BASE_CONTAINER.BASE_CONTAINER_VS_ITEM.Count != 0)
                {
                    foreach (var i in task.BASE_CONTAINER.BASE_CONTAINER_VS_ITEM)
                    {
                        ORDER_OUTORDER order = edm.ORDER_OUTORDER.FirstOrDefault(x => x.ID == (task.ORDERID ?? 0));
                        TASK_RECORD taskRecord = new TASK_RECORD()
                        {
 
                            TYPE = task.TASKTYPE ?? 0,
                            CREATETIME = DateTime.Now,
                            ITEMID = i.BASE_ITEM.ID,
                            CONTAINERID = task.BASE_CONTAINER.ID,
                            TASKID = task.ID,
                            ENABLE = 1,
                            ITEMCOUNT = i.ITEMNUM ?? 0,
                            ISMAINOUT = 0,
                            CREATEUSERID = task.CREATEUSERID ?? 1
                        };
                        if (order != null)
                        {
                            taskRecord.OUTORDERCODE = order.OUTORDERCODE;
                            if (order.ITEMID == taskRecord.ITEMID)
                            {
                                taskRecord.ISMAINOUT = 1;//属于主要出库零件
                            }
                        }
                        edm.TASK_RECORD.Add(taskRecord);
                    }
                }
                else
                {
                    ORDER_OUTORDER order = edm.ORDER_OUTORDER.FirstOrDefault(x => x.ID == (task.ORDERID ?? 0));
                    TASK_RECORD taskRecord = new TASK_RECORD()
                    {
 
                        TYPE = task.TASKTYPE ?? 0,
                        CREATETIME = DateTime.Now,
                        CONTAINERID = task.BASE_CONTAINER.ID,
                        TASKID = task.ID,
                        ENABLE = 1,
                        ISMAINOUT = 0,
                        CREATEUSERID = task.CREATEUSERID ?? 1
                    };
                    if (order != null)
                    {
                        taskRecord.OUTORDERCODE = order.OUTORDERCODE;
                    }
                    edm.TASK_RECORD.Add(taskRecord);
                }
 
                if (edm.SaveChanges() > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
 
        /// <summary>
        /// 闲时优化
        /// </summary>
        public static void CreateMove(object obj)
        {
            SrmService srmService = new SrmService();
            while (true)
            {
                Thread.Sleep(5000);
                int srmId = (int)obj;
                if (needMove[srmId - 1] && srmService.IsNotUse(srmId))
                {
                    using (Model edm = new Model())
                    {
                        if (srmId == 2 || srmId == 3 || srmId == 4 || srmId == 6 || srmId == 7 || srmId == 8 || srmId == 9)
                        {
                            BASE_PLACE_VS_CONTAINER pvcFrom = edm.BASE_PLACE_VS_CONTAINER.FirstOrDefault(x => x.BASE_PLACE.ROW == 2 || x.BASE_PLACE.ROW == 3);
                            if (pvcFrom != null)
                            {
                                BASE_PLACE nextPlace = pvcFrom.BASE_PLACE;
                                if (nextPlace != null && nextPlace.ISFULL == 1)//需要移库
                                {
                                    TASK_TASK task = new TASK_TASK();
                                    task.SOURCEPLACE = nextPlace.PLACE;
 
                                    BASE_PLACE newPlace = edm.BASE_PLACE.OrderBy(x => x.PLACELEVEL).ThenBy(x => x.LAYER).ThenBy(x => x.COL).FirstOrDefault(x => x.SRMID == srmId && (x.ISFULL ?? 0) == 0 && (x.ISLOCK ?? 0) == 0 && x.BASE_PLACETYPE.PLACETYPE == nextPlace.BASE_PLACETYPE.PLACETYPE);
                                    if (newPlace != null)
                                    {
                                        task.TOPLACE = newPlace.PLACE;
                                        task.HASREADED = 0;
                                        task.HASFINISHED = 0;
                                        task.TASKTYPE = 3;
                                        //task.CREATEUSERID = 1;
                                        task.TASKSTATUS = "新建";
                                        task.TASKLEVEL = 0;
                                        task.CONTAINERID = pvcFrom.CONTAINERID;
                                        task.ENABLE = 1;
                                        BASE_PLACE_VS_CONTAINER nextPvc = edm.BASE_PLACE_VS_CONTAINER.FirstOrDefault(x => x.BASE_PLACE.PLACE == nextPlace.PLACE && x.ENABLE == 1);
                                        if (nextPvc == null)
                                        {
                                            nextPlace.ISFULL = 0;
                                            edm.SaveChanges();
                                            continue;//自动校正
                                        }
                                        edm.TASK_TASK.Add(task);
                                        edm.SaveChanges();
                                        continue;
                                    }
                                    else
                                    {
                                        //无库位可移库
                                        continue;
                                    }
                                }
                            }
 
                        }
                    }
                }
            }
        }
 
        public static void UpdateCarTask()
        {
            while (true)
            {
                try
                {
                    using (Model model = new Model())
                    {
                        var carTaskList = model.CAR_CARTASK.Where(u => DbFunctions.DiffHours(u.UPDATETIME, DateTime.Now) > 23 && u.TASKSTATUS == "新建").ToList();
                        foreach (var item in carTaskList)
                        {
                            item.TASKSTATUS = "完成";
                        }
                        model.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    WZ.Useful.Commons.LogTextHelper.WriteLine("更新叉车任务异常", "UpdateCarTask", ex.ToString());
                }
                Thread.Sleep(20000);
            }
        }
    }
}