wxw
2025-04-24 3e19792c0061879ebe17ea04264c4c1f86bb3504
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
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
using Model.InterFaceModel;
using Model.ModelDto;
using Model.ModelDto.DataDto;
using Model.ModelDto.LogDto;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using Utility.Tools;
using WMS.BLL.LogServer;
using WMS.DAL;
using WMS.Entity.BllAsnEntity;
using WMS.Entity.BllSoEntity;
using WMS.Entity.Context;
using WMS.Entity.DataEntity;
using WMS.Entity.LogEntity;
using WMS.Entity.SysEntity;
using WMS.IBLL.IBllTransServer;
using static Model.InterFaceModel.RCSModel;
using static System.Collections.Specialized.BitVector32;
 
namespace WMS.BLL.BllTransServer
{
    public class RcsServer:IRcsServer
    {
        private readonly object RcsLock = new object();
 
        private static readonly SqlSugarScope Db = DataContext.Db;
        /// <summary>
        ///  RCS叫桶
        /// </summary>
        /// <param name="model"></param>
        /// <param name="url"></param>
        /// <param name="taskNo"></param>
        /// <exception cref="Exception"></exception>
        public void GetPalletNo(Pallnetmsg model, string url, out string taskNo)
        {
            lock (RcsLock)
            {
                try
                {
                    string taskMsg = "";
                    if (string.IsNullOrEmpty(model.Location))
                    {
                        throw new Exception("设备编码不可为空");
                    }
                    //通过设备号查找到所属设备(区域)
                    var deviceInfo = Db.Queryable<SysStorageArea>().First(w => w.IsDel == "0" && w.DeviceCode.Contains(model.Location));
                    if (deviceInfo == null)
                    {
                        throw new Exception("设备信息不存在");
                    }
                    var houseNo = deviceInfo.WareHouseNo;//叫桶设备所属车间编号
 
                    //叫桶设备所在储位信息(目标储位)
                    var endLoction = new SysStorageLocat();
 
                    var urlMes = "";//回传MES的接口地址
                    var noticeNo = "0";//出入库单据明细ID
 
                    //所有待分配的桶
                    var stockDetail = Db.Queryable<DataStockDetail>().Where(w => w.Status == "0");
                    //分配桶信息
                    var pallet = new DataStockDetail();
 
                    //开始事务
                    Db.BeginTran();
                    //判断叫料设备是否是A区的混料设备03,该设备是接料混料一体机,AGV叫桶应该叫净桶接料
                    if (houseNo == "M03" && model.Location == "125" && model.Type == "1")
                    {
                        model.Type = "0";
                    }
                    string agvMsg = string.Empty;
                    switch (model.Type)
                    {
                        case "0"://接料设备叫净桶
                            {
                                //叫桶目标储位地址信息
                                endLoction = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.WareHouseNo == houseNo && w.AreaNo == deviceInfo.AreaNo && w.Column == 1);
                                if (endLoction == null)
                                {
                                    throw new Exception("叫桶设备目标储位信息不存在");
                                }
                                //查找到该车间净桶区
                                List<string> areaNoList = Db.Queryable<SysStorageArea>().Where(w => w.IsDel == "0" && w.Status == "0" && w.Type == "0" && w.WareHouseNo == houseNo).Select(s => s.AreaNo).ToList();
                                //查找净桶
                                if (houseNo == "M03")//大单体车间净桶是绑定接料设备的
                                {
                                    pallet = stockDetail.Where(w => w.WareHouseNo == houseNo && w.PalletStatus == "0" && w.Status == "0"
                                && areaNoList.Contains(w.AreaNo) && w.UDF1 == deviceInfo.DeviceCode).OrderBy(o => o.UpdateTime).First();
                                }
                                else
                                {
                                    pallet = stockDetail.Where(w => w.WareHouseNo == houseNo && w.PalletStatus == "0" && w.Status == "0"
                                && areaNoList.Contains(w.AreaNo)).OrderBy(o => o.UpdateTime).First();
                                }
 
                                if (pallet == null)
                                {
                                    throw new Exception("暂无净桶可分配");
                                }
                                //分配桶后判断该桶外侧储位是否需要移库
                                bool yikuResult = YikuTask(pallet.PalletNo, url);
                                if (!yikuResult)
                                {
                                    throw new Exception("移库失败");
                                }
 
                                pallet.Status = "2";//分配状态 0:待分配,1:部分分配 , 2:已分配
                                                    //更新桶库存明细
                                Db.Updateable(pallet).ExecuteCommand();
 
                                taskMsg = "设备叫净桶任务";
                            }
                            break;
                        case "1"://混料设备叫料桶(混料)
                            {
                                //叫桶目标储位地址信息
                                endLoction = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.WareHouseNo == houseNo && w.AreaNo == deviceInfo.AreaNo && w.Column == 1);
                                if (endLoction == null)
                                {
                                    throw new Exception("叫桶设备目标储位信息不存在");
                                }
                                //判断是否有批次号
                                if (string.IsNullOrWhiteSpace(model.LotNo))
                                {
                                    throw new Exception("批次号为空!");
                                }
                                //查找到该车间预混区
                                List<string> areaNoList2 = Db.Queryable<SysStorageArea>().Where(w => w.IsDel == "0" && w.Status == "0" && w.Type == "1" && w.WareHouseNo == houseNo).Select(s => s.AreaNo).ToList();
                                //查找库存中是否有可用的此批次的混料桶
                                pallet = stockDetail.Where(w => w.WareHouseNo == houseNo && w.PalletStatus == "1" && w.Status == "0" && w.LotNo == model.LotNo
                                && areaNoList2.Contains(w.AreaNo)).OrderBy(o => o.UpdateTime).First();
                                if (pallet == null)
                                {
                                    throw new Exception("暂无混料桶可分配");
                                }
                                //分配桶后判断该桶外侧储位是否需要移库
                                bool yikuResult = YikuTask(pallet.PalletNo, url);
                                if (!yikuResult)
                                {
                                    throw new Exception("移库失败");
                                }
 
                                pallet.Status = "2";//分配状态 0:待分配,1:部分分配 , 2:已分配
                                                    //更新桶库存明细
                                Db.Updateable(pallet).ExecuteCommand();
 
                                taskMsg = "设备叫料桶混料任务";
                            }
                            break;
                        case "2"://下料设备叫料桶(下料)
                            {
                                if (houseNo == "M04")//除了喷干车间下料口都是2个储位(投料储位、投完料储位)
                                {
                                    //叫桶目标储位地址信息
                                    endLoction = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.WareHouseNo == houseNo && w.AreaNo == deviceInfo.AreaNo && w.Column == 1);
                                }
                                else
                                {
                                    //叫桶目标储位地址信息
                                    endLoction = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.WareHouseNo == houseNo && w.AreaNo == deviceInfo.AreaNo && w.Column == 1);// Column=1 投料储位,Column=2 投完料储位                        
                                }
                                if (endLoction == null)
                                {
                                    throw new Exception("叫桶设备目标储位信息不存在");
                                }
                                //判断是否有批次号
                                if (string.IsNullOrWhiteSpace(model.LotNo))
                                {
                                    throw new Exception("批次号为空!");
                                }
                                //查找是否有此批次出库单明细
                                /*var soNoticeDetail = Db.Queryable<BllExportNoticeDetail>().Where(w => w.IsDel == "0" && w.LotNo == model.LotNo).OrderBy(o => o.CreateTime).First();
                                if (soNoticeDetail == null)
                                {
                                    throw new Exception("该批次没有对应的出库单明细");
                                }
                                noticeNo = soNoticeDetail.Id.ToString();
                                //出库总单
                                var soNotice = Db.Queryable<BllExportNotice>().Where(w => w.IsDel == "0" && w.SONo == soNoticeDetail.SONo).First();
                                if (soNotice == null)
                                {
                                    throw new Exception("该批次没有对应的出库单");
                                }
                                soNotice.Status = "3";//正在执行
                                soNotice.UpdateTime = DateTime.Now;
                                //更新出库单
                                Db.Updateable(soNotice).ExecuteCommand();*/
 
                                //查找到该车间半成品区
                                List<string> areaNoList3 = Db.Queryable<SysStorageArea>().Where(w => w.IsDel == "0" && w.Status == "0" && w.Type == "2" && w.WareHouseNo == houseNo).Select(s => s.AreaNo).ToList();
                                //查找库存中是否有此批次的合格下料桶
                                pallet = stockDetail.Where(w => w.WareHouseNo == houseNo && w.PalletStatus == "2" && w.Status == "0" && w.LotNo == model.LotNo && w.InspectStatus == "1"
                                && areaNoList3.Contains(w.AreaNo)).OrderBy(o => o.UpdateTime).First();
                                if (pallet == null)
                                {
                                    throw new Exception("暂无下料桶可分配");
                                }
                                //分配桶后判断该桶外侧储位是否需要移库
                                bool yikuResult = YikuTask(pallet.PalletNo, url);
                                if (!yikuResult)
                                {
                                    throw new Exception("移库失败");
                                }
 
                                pallet.Status = "2";//分配状态 0:待分配,1:部分分配 , 2:已分配
                                                    //更新桶库存明细
                                Db.Updateable(pallet).ExecuteCommand();
 
                                taskMsg = "设备叫料桶下料任务";
                            }
                            break;
                        case "3"://清洗设备叫脏桶
                            {
                                //叫桶目标储位地址信息
                                endLoction = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.WareHouseNo == houseNo && w.AreaNo == deviceInfo.AreaNo && w.Column == 1);// Column=1 清洗储位,Column=2 清洗完料储位 
                                if (endLoction == null)
                                {
                                    throw new Exception("叫桶设备目标储位信息不存在");
                                }
                                //查找到该车间脏桶区
                                List<string> areaNoList4 = Db.Queryable<SysStorageArea>().Where(w => w.IsDel == "0" && w.Status == "0" && w.Type == "3" && w.WareHouseNo == houseNo).Select(s => s.AreaNo).ToList();
 
                                List<DataStockDetail> palletList = new List<DataStockDetail>();
                                if (houseNo == "M01")
                                {
                                    palletList = Db.Queryable<DataStockDetail>()
                                         .LeftJoin<SysStorageLocat>((d, l) => d.LocatNo == l.LocatNo)
                                        .Where(d => d.WareHouseNo == houseNo && d.PalletStatus == "3" && d.Status == "0" && areaNoList4.Contains(d.AreaNo))
                                        .OrderBy((d, l) => l.Row)
                                        .OrderBy((d, l) => l.Column)
                                        .ToList();
                                }
                                else
                                {
                                    palletList = stockDetail.Where(w => w.WareHouseNo == houseNo && w.PalletStatus == "3" && w.Status == "0"
                                    && areaNoList4.Contains(w.AreaNo)).OrderBy(o => o.UpdateTime).ToList();
                                }
 
                                if (palletList.Count <= 0)
                                {
                                    throw new Exception("暂无脏桶可分配");
                                }
                                taskMsg = "设备叫脏桶任务";
                                //遍历库存脏桶,返回MES验证
                                foreach (var item in palletList)
                                {
                                    #region 去掉MES
                                    //string jsonReq = JsonConvert.SerializeObject(item.PalletNo);
                                    //var response = HttpHelper.DoPost(urlMes, jsonReq, "回传MES脏桶编号", "RCS").ToString();
                                    //var obj = JsonConvert.DeserializeObject<ReMes>(response);//解析返回数据
                                    //if (obj.Success == "0")
                                    //{
                                    //    if (obj.Result != "1")//不可清洗,将脏桶冻结
                                    //    {
                                    //        item.Status = "5";//异常冻结
                                    //        //修改该桶库存状态
                                    //        Db.Updateable(item).ExecuteCommand();
 
                                    //        continue;
                                    //    }
                                    //    //下发小车任务
                                    //    var task2 = new TaskDetial
                                    //    {
                                    //        Startport = item.LocatNo,
                                    //        Endport = model.Location,
                                    //        Pallno = item.PalletNo,
                                    //        Type = model.Type,
                                    //        Crtype = "1",
                                    //        Noticedetailno = int.Parse(noticeno),
                                    //    };
                                    //    CreateLotTask(task2);
                                    //    return;
                                    //}
                                    //else
                                    //{
                                    //    throw new Exception("回传MES净桶编号失败!");
                                    //}
                                    #endregion
 
                                    item.Status = "2";//分配状态 0:待分配,1:部分分配 , 2:已分配
                                                      //更新桶库存明细
                                    Db.Updateable(item).ExecuteCommand();
                                    //起始储位地址信息
                                    var startLoction2 = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.LocatNo == item.LocatNo);
                                    if (startLoction2 == null)
                                    {
                                        throw new Exception($"起始目标储位信息不存在,桶号:{item.LocatNo}");
                                    }
 
                                    taskNo = new Common().GetMaxNo("TN");//任务号
                                    var logTaskEntry2 = new LogTask
                                    {
                                        TaskNo = taskNo,
                                        Sender = "WMS",
                                        Receiver = "RCS",
                                        //IsSuccess = 1, //是否下发成功 0失败 1成功
                                        SendDate = DateTime.Now,  //发送时间
                                                                  //BackDate = DateTime.Now,  //返回时间
                                        StartLocat = item.LocatNo,//起始位置
                                        EndLocat = endLoction.LocatNo,//目标位置
                                        PalletNo = item.PalletNo,//托盘码
                                        IsSend = 1,//是否可再次下发
                                        IsCancel = 1,//是否可取消
                                        IsFinish = 1,//是否可完成
                                        Type = "1",//任务类型 0 入库任务 1 出库任务  2 移库任务
                                        OrderType = "3",//单据类型 0 入库 1 出库 3 移库
                                        Status = "0",//任务状态0:等待执行1正在执行2执行完成
                                        NoticeDetailNo = int.Parse(noticeNo),
                                        Msg = taskMsg, //关键信息
                                        LotNo = item.LotNo//批次号
                                    };
                                    //组织下发小车任务信息
                                    var task2 = new TaskDetial
                                    {
                                        Taskno = taskNo,//任务号
                                        Startport = item.LocatNo,
                                        Endport = model.Location,//endLoction.LocatNo,
                                        Pallno = item.PalletNo,
                                        Crtype = "1",//叫桶
                                        WareHouseNo = houseNo
                                    };
                                    //给下车下发任务
                                    logTaskEntry2.SendDate = DateTime.Now;//发送时间
                                    var agvResult2 = CreateTaskForAgv(task2, url, out agvMsg);
                                    if (agvResult2)//成功
                                    {
                                        //请求成功修改任务表相应字段状态
                                        logTaskEntry2.IsSuccess = 1;
                                        logTaskEntry2.IsSend = 0;
                                        //logTaskEntry2.IsCancel = 0;
                                        logTaskEntry2.BackDate = DateTime.Now;
                                        logTaskEntry2.Status = "1";
                                        Db.Insertable(logTaskEntry2).ExecuteCommand();
 
                                        startLoction2.Status = "3";//出库中
                                        Db.Updateable(startLoction2).ExecuteCommand();
 
                                        endLoction.Status = "2";//入库中
                                        Db.Updateable(endLoction).ExecuteCommand();
                                    }
                                    else//失败
                                    {
                                        logTaskEntry2.IsSuccess = 0;
                                        logTaskEntry2.Information = agvMsg;
                                        Db.Insertable(logTaskEntry2).ExecuteCommand();
 
                                    }
                                    //提交事务
                                    Db.CommitTran();
                                    return;
                                }
                            }
                            break;
                        default:
                            throw new Exception("任务类型无效");
                    }
                    //起始储位地址信息
                    var startLoction = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.LocatNo == pallet.LocatNo);
                    if (startLoction == null)
                    {
                        throw new Exception($"起始目标储位信息不存在,桶号:{pallet.LocatNo}");
                    }
 
                    taskNo = new Common().GetMaxNo("TN");//任务号
                                                         //任务信息
                    var logTaskEntry = new LogTask
                    {
                        TaskNo = taskNo,
                        Sender = "WMS",
                        Receiver = "RCS",
                        //IsSuccess = 1, //是否下发成功 0失败 1成功
                        SendDate = DateTime.Now,  //发送时间
                                                  //BackDate = DateTime.Now,  //返回时间
                        StartLocat = pallet.LocatNo,//起始位置
                        EndLocat = endLoction.LocatNo,//目标位置
                        PalletNo = pallet.PalletNo,//托盘码
                        IsSend = 1,//是否可再次下发
                        IsCancel = 1,//是否可取消
                        IsFinish = 1,//是否可完成
                        Type = "1",//任务类型 0 入库任务 1 出库任务  2 移库任务
                        OrderType = "3",//单据类型 0 入库 1 出库 3移库
                        Status = "0",//任务状态0:等待执行1正在执行2执行完成
                        NoticeDetailNo = int.Parse(noticeNo),
                        Msg = taskMsg, //关键信息
                        LotNo = pallet.LotNo//批次号
                    };
 
                    //组织下发小车任务信息
                    var task = new TaskDetial
                    {
                        Taskno = taskNo,//任务号
                        Startport = pallet.LocatNo,//起始位置
                        Endport = model.Location,//endLoction.LocatNo,//目标位置
                        Pallno = pallet.PalletNo,//桶号
                        Crtype = "1",//叫桶
                        WareHouseNo = houseNo//车间编码
                    };
                    //给下车下发任务
                    logTaskEntry.SendDate = DateTime.Now;//发送时间
                    var agvResult = CreateTaskForAgv(task, url, out agvMsg);
                    if (agvResult)//成功
                    {
                        //请求成功修改任务表相应字段状态
                        logTaskEntry.IsSuccess = 1;
                        logTaskEntry.IsSend = 0;
                        //logTaskEntry.IsCancel = 0;
                        logTaskEntry.BackDate = DateTime.Now;
                        logTaskEntry.Status = "1";//正在执行
                        Db.Insertable(logTaskEntry).ExecuteCommand();
 
                        startLoction.Status = "3";//出库中
                        Db.Updateable(startLoction).ExecuteCommand();
 
                        endLoction.Status = "2";//入库中
                        Db.Updateable(endLoction).ExecuteCommand();
                    }
                    else//失败
                    {
                        logTaskEntry.IsSuccess = 0;
                        logTaskEntry.Information = agvMsg;
                        Db.Insertable(logTaskEntry).ExecuteCommand();
 
                    }
                    //提交事务
                    Db.CommitTran();
                }
                catch (Exception ex)
                {
                    //回滚事务
                    Db.RollbackTran();
                    throw new Exception(ex.Message);
                }
            }
        }
 
        /// <summary>
        /// 申请储位
        /// </summary>
        /// <param name="model"></param>
        /// <param name="url"></param>
        /// <param name="taskNo"></param>
        /// <exception cref="Exception"></exception>
        public void ApplyLocatNo(Pallnetmsg model, string url, out string taskNo)
        {
            lock (RcsLock)
            {
                try
                {
                    string taskMsg = "";
 
                    //通过设备号查找到所属设备(区域)
                    var deviceInfo = Db.Queryable<SysStorageArea>().First(w => w.IsDel == "0" && w.DeviceCode.Contains(model.Location));
                    if (deviceInfo == null)
                    {
                        throw new Exception("设备信息不存在");
                    }
                    //申请储位设备所在车间编号
                    var houseNo = deviceInfo.WareHouseNo;
                    //设备所在储位信息(起始储位)
                    var deviceLocatInfo = new SysStorageLocat();
                    //出入库单据明细ID
                    var noticeno = "0";
                    //目标储位信息
                    var loction = new SysStorageLocat();
                    //桶库存信息
                    var palletInfo = new DataStockDetail();
 
                    //开启事务
                    Db.BeginTran();
                    switch (model.Type)
                    {
                        case "3"://清洗设备申请储位(净桶申请储位)
                            {
                                //设备放桶储位信息(原始叫桶目标储位)
                                var deviceLocatInfo2 = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.WareHouseNo == houseNo && w.AreaNo == deviceInfo.AreaNo && w.Column == 1);// Column=1 清洗储位,Column=2 清洗完料储位 
                                if (deviceLocatInfo2 == null)
                                {
                                    throw new Exception($"未查询到该设备所在储位信息,Location:{model.Location}");
                                }
                                //查找桶库存信息
                                palletInfo = Db.Queryable<DataStockDetail>().Where(w => w.IsDel == "0" && w.WareHouseNo == deviceLocatInfo2.WareHouseNo && w.AreaNo == deviceLocatInfo2.AreaNo && w.LocatNo == deviceLocatInfo2.LocatNo).OrderBy(o => o.UpdateTime).First();
                                if (palletInfo == null)
                                {
                                    throw new Exception($"未查询到该设备上桶的库存信息");
                                }
                                //查找该桶有没有正在执行的任务
                                var tasking = Db.Queryable<LogTask>().First(w => w.IsDel == "0" && w.Status == "1" && w.PalletNo == palletInfo.PalletNo);
                                if (tasking != null)
                                {
                                    throw new Exception($"该桶有正在执行的任务,PalletNo:{palletInfo.PalletNo}");
                                }
 
                                //设备所在储位信息(起始储位)
                                deviceLocatInfo = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.WareHouseNo == houseNo && w.AreaNo == deviceInfo.AreaNo && w.Column == 2);// Column=1 清洗储位,Column=2 清洗完料储位 
                                if (deviceLocatInfo == null)
                                {
                                    throw new Exception($"未查询到该设备所在储位信息,Location:{model.Location}");
                                }
                                //查找到该车间净桶区;
                                loction = GetLocatModel(houseNo, "0");
                                if (loction == null)
                                {
                                    throw new Exception("该车间净桶区暂无空余储位");
                                }
 
                                taskMsg = "清洗设备(净桶)申请储位任务";
                            }
                            break;
                        case "0"://接料设备申请储位(预混料桶申请储位)
                            {
                                //设备所在储位信息(起始储位)
                                deviceLocatInfo = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.WareHouseNo == houseNo && w.AreaNo == deviceInfo.AreaNo);
                                if (deviceLocatInfo == null)
                                {
                                    throw new Exception($"未查询到该设备所在储位信息,Location:{model.Location}");
                                }
                                //判断是否有批次号
                                if (string.IsNullOrWhiteSpace(model.LotNo))
                                {
                                    throw new Exception("批次号为空!");
                                }
                                //查找桶库存信息
                                palletInfo = Db.Queryable<DataStockDetail>().Where(w => w.IsDel == "0" && w.WareHouseNo == deviceLocatInfo.WareHouseNo && w.AreaNo == deviceLocatInfo.AreaNo && w.LocatNo == deviceLocatInfo.LocatNo).OrderBy(o => o.UpdateTime).First();
                                if (palletInfo == null)
                                {
                                    throw new Exception($"未查询到该设备上桶的库存信息");
                                }
                                palletInfo.LotNo = model.LotNo;//批次号
                                //更新桶库存信息
                                Db.Updateable(palletInfo).ExecuteCommand();
 
                                //查找该桶有没有正在执行的任务
                                var tasking = Db.Queryable<LogTask>().First(w => w.IsDel == "0" && w.Status == "1" && w.PalletNo == palletInfo.PalletNo);
                                if (tasking != null)
                                {
                                    throw new Exception($"该桶有正在执行的任务,PalletNo:{palletInfo.PalletNo}");
                                }
                                
                                //查找到该车间预混区
                                loction = GetLocatModel(houseNo, "1");
                                if (loction == null)
                                {
                                    throw new Exception("该车间预混区暂无空余储位");
                                }
 
 
                                taskMsg = "接料设备(预混料桶)申请储位任务";
                            }
                            break;
                        case "1"://混料设备申请储位(半成品桶申请储位)
                            {
                                //设备所在储位信息(起始储位)
                                deviceLocatInfo = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.WareHouseNo == houseNo && w.AreaNo == deviceInfo.AreaNo);
                                if (deviceLocatInfo == null)
                                {
                                    throw new Exception($"未查询到该设备所在储位信息,Location:{model.Location}");
                                }
                                //查找桶库存信息
                                palletInfo = Db.Queryable<DataStockDetail>().Where(w => w.IsDel == "0" && w.WareHouseNo == deviceLocatInfo.WareHouseNo && w.AreaNo == deviceLocatInfo.AreaNo && w.LocatNo == deviceLocatInfo.LocatNo).OrderBy(o => o.UpdateTime).First();
                                if (palletInfo == null)
                                {
                                    throw new Exception($"未查询到该设备上桶的库存信息");
                                }
                                //查找该桶有没有正在执行的任务
                                var tasking = Db.Queryable<LogTask>().First(w => w.IsDel == "0" && w.Status == "1" && w.PalletNo == palletInfo.PalletNo);
                                if (tasking != null)
                                {
                                    throw new Exception($"该桶有正在执行的任务,PalletNo:{palletInfo.PalletNo}");
                                }
 
                                //判断是否有批次号
                                if (string.IsNullOrWhiteSpace(model.LotNo))
                                {
                                    throw new Exception("批次号为空!");
                                }
                                //大单体车间有个接料混料一体机,这里判断批次为空就再维护下批次
                                if (string.IsNullOrEmpty(palletInfo.LotNo))
                                {
                                    palletInfo.LotNo = model.LotNo;
                                    //更新桶库存信息
                                    Db.Updateable(palletInfo).ExecuteCommand();
                                }
                                
                                //校验此批次是否有入库单
                                /*var noticeDetail = Db.Queryable<BllArrivalNoticeDetail>().Where(w => w.IsDel == "0" && w.Status == "0" && w.LotNo == model.LotNo).OrderByDescending(o => o.CreateTime).First();
                                if (noticeDetail == null)
                                {
                                    throw new Exception("该批次没有对应的入库单明细");
                                }
                                noticeno = noticeDetail.Id.ToString();
                                //入库总单
                                var notice = Db.Queryable<BllArrivalNotice>().Where(w => w.IsDel == "0" && w.ASNNo == noticeDetail.ASNNo).First();
                                if (notice == null)
                                {
                                    throw new Exception("该批次没有对应的入库单");
                                }
                                notice.Status = "1";//正在执行
                                //更新入库单状态
                                Db.Updateable(notice).ExecuteCommand();*/
 
                                //查找到该车间半成品区
                                loction = GetLocatModel(houseNo, "2");
                                if (loction == null)
                                {
                                    throw new Exception("该车间半成品区暂无空余储位");
                                }
 
                                taskMsg = "接料设备(混料桶)申请储位任务";
                            }
                            break;
                        case "2"://下料设备申请储位(脏桶申请储位)
                            {
                                //设备放桶储位信息(原始叫桶目标储位)
                                var deviceLocatInfo2 = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.WareHouseNo == houseNo && w.AreaNo == deviceInfo.AreaNo && w.Column == 1);// Column=1 投料储位,Column=2 投完料储位
                                if (deviceLocatInfo2 == null)
                                {
                                    throw new Exception($"未查询到该设备所在储位信息,Location:{model.Location}");
                                }
                                //查找桶库存信息
                                palletInfo = Db.Queryable<DataStockDetail>().Where(w => w.IsDel == "0" && w.WareHouseNo == deviceLocatInfo2.WareHouseNo && w.AreaNo == deviceLocatInfo2.AreaNo && w.LocatNo == deviceLocatInfo2.LocatNo).OrderBy(o => o.UpdateTime).First();
                                if (palletInfo == null)
                                {
                                    throw new Exception($"未查询到该设备上桶的库存信息");
                                }
                                //查找该桶有没有正在执行的任务
                                var tasking = Db.Queryable<LogTask>().First(w => w.IsDel == "0" && w.Status == "1" && w.PalletNo == palletInfo.PalletNo);
                                if (tasking != null)
                                {
                                    throw new Exception($"该桶有正在执行的任务,PalletNo:{palletInfo.PalletNo}");
                                }
                                if (houseNo == "M04")//除了喷干车间下料口都是2个储位(投料储位、投完料储位)
                                {
                                    //设备所在储位信息(起始储位)
                                    deviceLocatInfo = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.WareHouseNo == houseNo && w.AreaNo == deviceInfo.AreaNo && w.Column == 1);
                                }
                                else
                                {
                                    //设备所在储位信息(起始储位)
                                    deviceLocatInfo = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.WareHouseNo == houseNo && w.AreaNo == deviceInfo.AreaNo && w.Column == 2);//Column=1 投料储位,Column=2 投完料储位           
                                }
                                if (deviceLocatInfo == null)
                                {
                                    throw new Exception($"未查询到该设备所在储位信息,Location:{model.Location}");
                                }
                                //查找到该车间脏桶区
                                loction = GetLocatModel(houseNo, "3");
                                if (loction == null)
                                {
                                    throw new Exception("该车间脏桶区暂无空余储位");
                                }
 
                                taskMsg = "下料设备(脏桶)申请储位任务";
                            }
                            break;
                    }
 
                    taskNo = new Common().GetMaxNo("TN");//任务号
                                                         //任务信息
                    var logTaskEntry = new LogTask
                    {
                        TaskNo = taskNo,
                        Sender = "WMS",
                        Receiver = "RCS",
                        //IsSuccess = 1, //是否下发成功 0失败 1成功
                        SendDate = DateTime.Now,  //发送时间
                                                  //BackDate = DateTime.Now,  //返回时间
                        StartLocat = deviceLocatInfo.LocatNo,//起始位置
                        EndLocat = loction.LocatNo,//目标位置
                        PalletNo = palletInfo.PalletNo,//桶号
                        IsSend = 1,//是否可再次下发
                        IsCancel = 1,//是否可取消
                        IsFinish = 1,//是否可完成
                        Type = "0",//任务类型 0 入库任务 1 出库任务  2 移库任务
                        OrderType = "3",//单据类型 0 入库 1 出库 3 移库
                        Status = "0",//任务状态0:等待执行1正在执行2执行完成
                        NoticeDetailNo = int.Parse(noticeno),
                        Msg = taskMsg, //关键信息
                        LotNo = palletInfo.LotNo//批次号
                    };
                    var task = new TaskDetial
                    {
                        Taskno = taskNo,//任务号
                        Startport = model.Location,//deviceLocatInfo.LocatNo,//起始位置
                        Endport = loction.LocatNo,//目标位置
                        Pallno = palletInfo.PalletNo,
                        Crtype = "0",//入库
                        WareHouseNo = houseNo
                    };
                    string agvMsg = string.Empty;
                    //给下车下发任务
                    logTaskEntry.SendDate = DateTime.Now;//发送时间
                    var agvResult = CreateTaskForAgv(task, url, out agvMsg);
                    if (agvResult)//成功
                    {
                        //请求成功修改任务表相应字段状态
                        logTaskEntry.IsSuccess = 1;
                        logTaskEntry.IsSend = 0;
                        //logTaskEntry.IsCancel = 0;
                        logTaskEntry.BackDate = DateTime.Now;
                        logTaskEntry.Status = "1";//正在执行
                        Db.Insertable(logTaskEntry).ExecuteCommand();
 
                        deviceLocatInfo.Status = "3";//出库中
                        Db.Updateable(deviceLocatInfo).ExecuteCommand();
 
                        loction.Status = "2";//入库中
                        Db.Updateable(loction).ExecuteCommand();
                    }
                    else//失败
                    {
                        logTaskEntry.IsSuccess = 0;
                        logTaskEntry.Information = agvMsg;
                        Db.Insertable(logTaskEntry).ExecuteCommand();
                    }
                    //提交事务
                    Db.CommitTran();
                }
                catch (Exception ex)
                {
                    //回滚事务
                    Db.RollbackTran();
                    throw new Exception(ex.Message);
                }
            }
        }
        /// <summary>
        /// 获取空储位
        /// </summary>
        /// <param name="houseNo">车间编号</param>
        /// <param name="Type">类别 0:净桶区 1:预混区 2:半成品区 3:脏桶区</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        private SysStorageLocat GetLocatModel(string houseNo,string type)
        {
            try
            {
                //通过类别查找车间区域
                List<string> areaNoList = Db.Queryable<SysStorageArea>().Where(w => w.IsDel == "0" && w.Status == "0" && w.Type == type && w.WareHouseNo == houseNo).Select(s => s.AreaNo).ToList();
                //查找储位
                var loctionModel = Db.Queryable<SysStorageLocat>().Where(w => w.IsDel == "0" && w.Status == "0" && w.Flag == "0" && w.WareHouseNo == houseNo && areaNoList.Contains(w.AreaNo))
                    .OrderBy(o => o.Row).OrderByDescending(o => o.Column).First();
                //if (loctionModel == null)
                //{
                //    throw new Exception("该车间净桶区暂无空余储位");
                //}
                return loctionModel;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
 
 
        /// <summary>
        /// MES下发将净桶改为脏桶并拉到脏桶区
        /// </summary>
        /// <param name="PalletNo"></param>
        /// <returns></returns>
        public void ChangePalletStatus(string PalletNo,string url)
        {
            try
            {
                var detail = Db.Queryable<DataStockDetail>().First(w => w.PalletNo == PalletNo && w.PalletStatus == "0");
                if (detail == null)
                {
                    throw new Exception($"库存中不存在桶号为:{PalletNo}的库存!");
                }
                //起始目标储位信息
                var statrtLocat = Db.Queryable<SysStorageLocat>().First(w => w.WareHouseNo == detail.WareHouseNo && w.AreaNo == detail.AreaNo && w.LocatNo == detail.LocatNo);
                if (statrtLocat == null)
                {
                    throw new Exception($"该桶所在的储位信息不存在!");
                }
                //开启事务
                Db.BeginTran();
 
                //查找是否有空余脏桶位
                var endLocat = Db.Queryable<SysStorageLocat>().First(w => w.WareHouseNo == detail.WareHouseNo && w.AreaNo.Contains("04") && w.Status == "0" && w.Flag=="0");
                if (endLocat == null)
                {
                    detail.Status = "5";//异常锁定
                    detail.PalletStatus = "3";//脏桶
                    //修改库存状态
                    Db.Updateable(detail).ExecuteCommand();
                }
                else
                {
                    //生成调度小车净桶去脏桶区任务
                    var ztask = new TaskDetial
                    {
                        Startport = detail.LocatNo,
                        Endport = endLocat.LocatNo,
                        Pallno = PalletNo,
                        Crtype = "2",
                    };
                    string taskNo = "";
                    string agvMsg = string.Empty;
                    CreateTaskForAgv(ztask, url, out agvMsg);
 
                    statrtLocat.Status = "3";//出库中
                    //修改起始储位状态
                    Db.Updateable(statrtLocat).ExecuteCommand();
 
                    detail.Status = "5";//异常锁定
                    detail.PalletStatus = "3";//脏桶
                    //修改库存状态
                    Db.Updateable(detail).ExecuteCommand();
                }
                Db.CommitTran();
            }
            catch (Exception ex)
            {
                Db.RollbackTran();
                throw ex;
            }
        }
 
        /// <summary>
        /// MES下发清洗锁定脏桶
        /// </summary>
        /// <param name="pallnetmsg"></param>
        public void CleanPallet(Pallnetmsg pallnetmsg,string url)
        {
            try
            {
                var detail = Db.Queryable<DataStockDetail>().First(w => w.PalletNo == pallnetmsg.PalletNo && w.PalletStatus == "3");
                if (detail == null)
                {
                    throw new Exception($"库存中不存在桶号为:{pallnetmsg.PalletNo}的库存!");
                }
                //下发小车任务
                var task = new TaskDetial
                {
                    Startport = detail.LocatNo,
                    Endport = pallnetmsg.Location,
                    Pallno = detail.PalletNo,
                    Crtype = "1",
                };
                string taskNo = "";
                string agvMsg = string.Empty;
                CreateTaskForAgv(task, url, out agvMsg);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
        /// <summary>
        /// 任务完成
        /// </summary>
        /// <param name="TaskNo"></param>
        /// <param name="Status"></param>
        /// <exception cref="Exception"></exception>
        public void RCSFinishTask(string taskNo, string status, string comeFrom, int userId = 0)
        {
            try
            {
                #region 条件判断
                var resultModel = new ErpModel() { Success = -1, Message = "" };
                if (string.IsNullOrEmpty(taskNo))
                {
                    throw new Exception("任务号不可为空");
                }
                //任务信息
                var taskInfo = Db.Queryable<LogTask>().First(w => w.TaskNo == taskNo);
                if (taskInfo == null)
                {
                    throw new Exception($"任务号为:{taskNo}的任务不存在!");
                }
                if (taskInfo.Status != "1" && comeFrom != "WMS")
                {
                    throw new Exception($"任务号为:{taskNo}的任务状态异常");
                }
                #endregion
                //开启事务
                Db.BeginTran();
                var comTime = DateTime.Now;
 
                if (status == "0")
                {
                    taskInfo.Status = "3";//异常结束
                    //修改任务状态
                    Db.Updateable(taskInfo).ExecuteCommand();
 
                    resultModel.Success = 0;
                    resultModel.Message = "成功";
 
                    //提交事务
                    Db.CommitTran();
 
                    return;
                }
                //库存明细
                var stockDetail = Db.Queryable<DataStockDetail>().First(w => w.PalletNo == taskInfo.PalletNo);
                if (stockDetail == null)
                {
                    throw new Exception($"桶库存信息不存在");
                }
                //起始储位信息
                var startLocatInfo = Db.Queryable<SysStorageLocat>().First(w => w.LocatNo == taskInfo.StartLocat && w.IsDel == "0");
                if (startLocatInfo == null)
                {
                    throw new Exception($"起始储位信息不存在");
                }
                startLocatInfo.Status = "0";//空储位
                //修改起始储位状态
                Db.Updateable(startLocatInfo).ExecuteCommand();
 
                //目标储位信息
                var endLocatInfo = Db.Queryable<SysStorageLocat>().First(w => w.LocatNo == taskInfo.EndLocat && w.IsDel == "0");
                if (endLocatInfo == null)
                {
                    throw new Exception($"目标储位信息不存在");
                }
                endLocatInfo.Status = "1";//有物品
                //修改目标储位状态
                Db.Updateable(endLocatInfo).ExecuteCommand();
 
                //目标储位所属区域
                var endAreaInfo = Db.Queryable<SysStorageArea>().First(w => w.IsDel == "0" && w.WareHouseNo == endLocatInfo.WareHouseNo && w.AreaNo == endLocatInfo.AreaNo);
                if (endAreaInfo == null)
                {
                    throw new Exception($"目标储位所属区域信息不存在");
                }
 
                stockDetail.WareHouseNo = endLocatInfo.WareHouseNo;//所属仓库
                stockDetail.RoadwayNo = endLocatInfo.RoadwayNo;//所属巷道
                stockDetail.AreaNo = endLocatInfo.AreaNo;//所属区域
                stockDetail.LocatNo = endLocatInfo.LocatNo;//储位地址
                stockDetail.UpdateTime = DateTime.Now;//更新时间
                if (endAreaInfo.Type == "0")//洁净区
                {
                    stockDetail.PalletStatus = "0";
                    stockDetail.Status = "0";//待分配
                }
                else if (endAreaInfo.Type == "1")//预混区
                {
                    stockDetail.PalletStatus = "1";
                    stockDetail.Status = "0";//待分配
                }
                else if (endAreaInfo.Type == "2")//半成品区
                {
                    stockDetail.PalletStatus = "2";
                    stockDetail.Status = "0";//待分配
                }
                else if (endAreaInfo.Type == "3")//脏桶区
                {
                    stockDetail.PalletStatus = "3";
                    stockDetail.LotNo = "";//批次
                    stockDetail.SkuNo = "";
                    stockDetail.SkuName = "";
                    stockDetail.Status = "0";//待分配
                    stockDetail.InspectStatus = "0";//待检验
                }
                //修改库存明细
                Db.Updateable(stockDetail).ExecuteCommand();
                //入库任务,处理入库单
                if (taskInfo.Type == "0" && taskInfo.NoticeDetailNo != 0)
                {
                    //入库单明细
                    var noticeDetail = Db.Queryable<BllArrivalNoticeDetail>().First(w => w.IsDel == "0" && w.Id == taskInfo.NoticeDetailNo);
                    if (noticeDetail != null)
                    {                        
                        noticeDetail.CompleteQty += 1;//完成数量
 
                        if (noticeDetail.CompleteQty >= noticeDetail.Qty)
                        {
                            noticeDetail.CompleteTime = DateTime.Now;//完成时间
 
                            //入库单
                            var notice = Db.Queryable<BllArrivalNotice>().First(w => w.IsDel == "0" && w.ASNNo == noticeDetail.ASNNo);
 
                            var noticeDetailList = Db.Queryable<BllArrivalNoticeDetail>().Where(w => w.IsDel == "0" && w.CompleteQty < w.Qty && w.Id != noticeDetail.Id).ToList();
                            if (noticeDetailList.Count <= 0)
                            {
                                notice.Status = "2";//执行完成
                                notice.UpdateTime = DateTime.Now;
                                notice.CompleteTime = DateTime.Now;
                                //修改入库单状态
                                Db.Updateable(notice).ExecuteCommand();
                            }
                        }
                        //修改入库单明细
                        Db.Updateable(noticeDetail).ExecuteCommand();                        
                    }
                }
                //出库任务,处理出库单
                if (taskInfo.Type == "1" && taskInfo.NoticeDetailNo != 0)//入库
                {
                    var noticeDetail = Db.Queryable<BllExportNoticeDetail>().First(w => w.IsDel == "0" && w.Id==taskInfo.NoticeDetailNo);
                    if (noticeDetail != null)
                    {
                        noticeDetail.CompleteQty += 1;//完成数量
 
                        if (noticeDetail.CompleteQty >= noticeDetail.Qty)
                        {
                            var notice = Db.Queryable<BllExportNotice>().First(w => w.IsDel == "0" && w.SONo == noticeDetail.SONo);
 
                            var noticeDetailList = Db.Queryable<BllExportNoticeDetail>().Where(w => w.IsDel == "0" && w.CompleteQty < w.Qty && w.Id != noticeDetail.Id).ToList();
                            if (noticeDetailList.Count <= 0)
                            {
                                notice.Status = "4";//执行完成
                                notice.UpdateTime = DateTime.Now;
                                notice.CompleteTime = DateTime.Now;
                                //修改入库单状态
                                Db.Updateable(notice).ExecuteCommand();
                            }
                        }
                        //修改入库单明细
                        Db.Updateable(noticeDetail).ExecuteCommand();
                    }
                    
                }
 
                #region 去掉MES
                //回传桶状态给MES
                //BackPalletStatus(taskInfo.PalletNo, sd3.PalletStatus);
                #endregion
 
                taskInfo.Status = "2";//执行完成
                taskInfo.IsSend = 0;
                taskInfo.IsCancel = 0;
                taskInfo.IsFinish = 0;
                taskInfo.FinishDate = DateTime.Now;//完成时间
                //修改任务状态
                Db.Updateable(taskInfo).ExecuteCommand();
 
                if (comeFrom == "WMS")
                {
                    //添加操作日志记录
                    var k = new OperationCrServer().AddLogOperationCr("库内作业", "库内日志", taskInfo.TaskNo, "完成", $"点击完成按钮、任务号为:{taskInfo.TaskNo}的任务", userId);
                }
                //提交事务
                Db.CommitTran();
            }
            catch (Exception ex)
            {
                //回滚事务
                Db.RollbackTran();
                throw new Exception(ex.Message);
            }
        }
 
        /// <summary>
        /// 手动取消任务
        /// </summary>
        /// <param name="taskNo"></param>
        /// <param name="userId"></param>
        /// <exception cref="Exception"></exception>
        public void CancelTask(string taskNo, int userId)
        {
            try
            {
                //开启事务
                Db.BeginTran();
                //任务信息
                var task = Db.Queryable<LogTask>().First(m => m.TaskNo == taskNo && m.IsDel == "0");
                if (task == null)
                {
                    throw new Exception("未查询到任务信息");
                }
                //if (task.Status != "0")
                //{
                //    throw new Exception("只有等待执行的任务才可取消");
                //}
                //修改任务 
                task.IsSuccess = 0;
                task.IsSend = 0;
                task.IsCancel = 0;
                task.IsFinish = 0;
                task.Status = "4";//已取消
                task.CancelDate = DateTime.Now;
                Db.Updateable(task).ExecuteCommand();
 
                //修改起始储位信息
                var locat = Db.Queryable<SysStorageLocat>().First(a => a.IsDel == "0" && a.LocatNo == task.StartLocat);
                if (locat == null)
                {
                    throw new Exception("未查询到起始位置储位信息,请核实!");
                }
                locat.Status = "1"; //有物品
                Db.Updateable(locat).ExecuteCommand();
 
                //修改目标储位信息
                var locatEnd = Db.Queryable<SysStorageLocat>().First(a => a.IsDel == "0" && a.LocatNo == task.EndLocat);
                if (locatEnd == null)
                {
                    throw new Exception("未查询到目标位置储位信息,请核实!");                    
                }
                locatEnd.Status = "0"; //空储位
                Db.Updateable(locatEnd).ExecuteCommand();
 
                if (task.Type == "1")//出库任务
                {
                    //修改库存明细
                    var stockDetail = Db.Queryable<DataStockDetail>().First(w => w.IsDel == "0" && w.PalletNo == task.PalletNo && w.Status == "2");
                    if (stockDetail == null)
                    {
                        throw new Exception("未查询到该桶库存信息,请检查!");
                    }
                    stockDetail.Status = "0";//待分配
                    Db.Updateable(stockDetail).ExecuteCommand();
                }
                //添加操作日志记录
                var k = new OperationCrServer().AddLogOperationCr("库内作业", "库内日志", taskNo, "取消", $"点击取消按钮、取消了任务号为:{taskNo}的任务", userId);
                Db.CommitTran();
            }
            catch (Exception e)
            {
                Db.RollbackTran();
                throw new Exception(e.Message);
            }
        }
        /// <summary>
        /// 手动下发任务
        /// </summary>
        /// <param name="taskNo"></param>
        /// <param name="userId"></param>
        /// <exception cref="Exception"></exception>
        public void DownTask(string taskNo, int userId,string url)
        {
            try
            {
                //开启事务
                Db.BeginTran();
                //任务信息
                var logTaskEntry = Db.Queryable<LogTask>().First(m => m.TaskNo == taskNo && m.IsDel == "0");
                if (logTaskEntry == null)
                {
                    throw new Exception("未查询到任务信息");
                }
                if (logTaskEntry.Status != "0")
                {
                    throw new Exception("只有等待执行的任务才可手动下发");
                }
                //查找该桶有没有正在执行的任务
                var tasking = Db.Queryable<LogTask>().First(w => w.IsDel == "0" && w.Status == "1" && w.PalletNo == logTaskEntry.PalletNo);
                if (tasking != null)
                {
                    throw new Exception($"该桶有正在执行的任务");
                }
                //起始储位信息
                var startLocatInfo = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.LocatNo == logTaskEntry.StartLocat);
                if (startLocatInfo == null)
                {
                    throw new Exception($"未查询到起始储位信息");
                }
                if (startLocatInfo.Status != "1")
                {
                    throw new Exception($"起始储位状态异常");
                }
                //目标储位信息
                var endLocatInfo = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.LocatNo == logTaskEntry.EndLocat);
                if (endLocatInfo == null)
                {
                    throw new Exception($"未查询到目标储位信息");
                }
                if (endLocatInfo.Status != "0")
                {
                    throw new Exception($"目标储位状态异常");
                }
                //组织给小车下发任务信息
                var task = new TaskDetial
                {
                    Taskno = taskNo,//任务号
                    Startport = logTaskEntry.StartLocat,
                    Endport = logTaskEntry.EndLocat,
                    Pallno = logTaskEntry.PalletNo,
                    Crtype = logTaskEntry.Type,
                };
                string agvMsg = string.Empty;
                //给下车下发任务
                logTaskEntry.SendDate = DateTime.Now;//发送时间
                var agvResult = CreateTaskForAgv(task, url, out agvMsg);
                if (agvResult)//成功
                {
                    //请求成功修改任务表相应字段状态
                    logTaskEntry.IsSuccess = 1;
                    logTaskEntry.IsSend = 0;
                    logTaskEntry.IsCancel = 0;
                    logTaskEntry.BackDate = DateTime.Now;
                    logTaskEntry.Status = "1";//正在执行
                    Db.Insertable(logTaskEntry).ExecuteCommand();
 
                    startLocatInfo.Status = "3";//出库中
                    Db.Updateable(startLocatInfo).ExecuteCommand();
 
                    endLocatInfo.Status = "2";//入库中
                    Db.Updateable(endLocatInfo).ExecuteCommand();
                }
                else//失败
                {
                    logTaskEntry.IsSuccess = 0;
                    logTaskEntry.Information = agvMsg;
                    Db.Insertable(logTaskEntry).ExecuteCommand();
                }
 
                //添加操作日志记录
                var k = new OperationCrServer().AddLogOperationCr("库内作业", "库内日志", taskNo, "下发", $"点击下发按钮、任务号为:{taskNo}的任务", userId);
                Db.CommitTran();
            }
            catch (Exception e)
            {
                Db.RollbackTran();
                throw new Exception(e.Message);
            }
        }
 
        /// <summary>
        /// 生成任务单到LogTask
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public bool CreateTaskForAgv(TaskDetial taskDetial, string url, out string agvMsg, string priority=null)
        {
            bool result = false;
 
            #region 呼叫小车代码
            List<PositionCodePath> pahtList = new List<PositionCodePath>();
            //起始位置
            PositionCodePath path1 = new PositionCodePath();
            path1.positionCode = taskDetial.Startport;
            if (taskDetial.Crtype == "1")//叫桶(桶出库)
            {
                path1.type = "05";
            }
            else if (taskDetial.Crtype == "0")//申请储位(桶入库)
            {
                path1.type = "00";
            }
            else//同车间平层搬运
            {
                path1.type = "05";
            }
            pahtList.Add(path1);
 
            //目标位置
            PositionCodePath path2 = new PositionCodePath();
            path2.positionCode = taskDetial.Endport;
            if (taskDetial.Crtype == "1")//叫桶(桶出库)
            {
                path2.type = "00";
            }
            else if (taskDetial.Crtype == "1")//申请储位(桶入库)
            {
                path2.type = "05";
            }
            else //同车间平层搬运
            {
                path2.type = "05";
            }
            pahtList.Add(path2);
 
            //下车任务单
            AgvSchedulingTask agvTask = new AgvSchedulingTask();
            agvTask.reqCode = taskDetial.Taskno;//请求编号
            agvTask.taskCode = taskDetial.Taskno;//任务号
            agvTask.ctnrCode = taskDetial.Pallno;//桶号            
            agvTask.reqTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");//请求时间                
            agvTask.wbCode = "";
            agvTask.positionCodePath = pahtList;//小车路径
            agvTask.podCode = "";
            agvTask.userCallCode = "";//taskDetial.Endport;//目标位置
            agvTask.priority = priority;//优先级
            //判断容器类型
            if (taskDetial.WareHouseNo == "M04")//喷干车间
            {
                agvTask.ctnrTyp = "2";// 1:桶 2:桶(小) 3:托盘
            }
            else
            {
                agvTask.ctnrTyp = "1";// 1:桶 2:桶(小) 3:托盘
            }
            //判断任务类型
            if (taskDetial.Crtype == "1")//叫桶(桶出库)
            {
                agvTask.taskTyp = "Z3";//任务类型 线边到托盘收集器 Z1, 托盘垛申请入库 Z2
            }
            else if (taskDetial.Crtype == "0")//申请储位(桶入库)
            {
                agvTask.taskTyp = "Z4";//任务类型 线边到托盘收集器 Z1, 托盘垛申请入库 Z2
            }
            else//同车间平层搬运
            {
                agvTask.taskTyp = "Z5";
            }
 
            // 正式运行程序放开
            var jsonData = JsonConvert.SerializeObject(agvTask);
            string response = HttpHelper.DoPost(url, jsonData, "下发给AGV转运命令", "AGV");
            //解析返回数据 
            var agvModel = JsonConvert.DeserializeObject<OutCommanAgvDto>(response);
            if (agvModel.Code == "0")
            {
                result = true;//给下车下发任务成功
 
                agvMsg = "";
            }
            else
            {
                string logMsg = "";
                if (taskDetial.Crtype == "1")//叫桶(桶出库)
                {
                    logMsg = "申请叫桶";
                }
                else if (taskDetial.Crtype == "0")//申请储位(桶入库)
                {
                    logMsg = "申请储位";
                }
                var logStr = $@".\log\AGV\AGV{logMsg}" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
                LogFile.SaveLogToFile($"AGV{logMsg}异常:( {agvModel.Message} ),", logStr);
 
                agvMsg = agvModel.Message;
            }
            return result;
            #endregion
        }
 
        /// <summary>
        /// 分配桶后判断该桶外侧储位是否需要移库
        /// </summary>
        /// <param name="palletNo"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        private bool YikuTask(string palletNo,string url)
        {
            try
            {
                //分配桶的库存信息
                var palletInfo = Db.Queryable<DataStockDetail>().First(w => w.IsDel == "0" && w.PalletNo == palletNo);
                if (palletInfo == null)
                {
                    throw new Exception("未查询到分配桶的库存信息");
                }
                //判断分配的桶有没有正在执行的任务(移库中)
                var taskIng= Db.Queryable<LogTask>().First(w => w.IsDel == "0" && (w.Status == "0" || w.Status == "1") && w.PalletNo == palletNo);
                if (taskIng != null)
                {
                    throw new Exception("分配的桶有正在执行的任务,请稍后再试");
                }
                //分配桶的储位信息
                var locatInfo = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.LocatNo == palletInfo.LocatNo);
                if (locatInfo == null)
                {
                    throw new Exception("未查询到分配桶所在的储位信息");
                }
                
                //判断该储位是否是内侧储位
                if (!string.IsNullOrEmpty(locatInfo.AisleOne))
                {
                    //判断外侧储位是否有桶
                    var palletInfoYi = Db.Queryable<DataStockDetail>().First(w => w.IsDel == "0" && w.LocatNo == locatInfo.AisleOne);
                    if (palletInfoYi != null)
                    {
                        //判断要移库的桶是否被分配
                        if (palletInfoYi.Status != "0")
                        {
                            throw new Exception("要移库的桶已被分配,请稍后再试");
                        }
                        //判断要移库的桶是否有正在执行的任务
                        var taskInfo = Db.Queryable<LogTask>().First(w => w.IsDel == "0" && (w.Status == "0" || w.Status == "1") && w.PalletNo == palletInfoYi.PalletNo);
                        if (taskInfo != null)
                        {
                            throw new Exception("要移库的桶有正在执行的任务,请稍后再试");
                        }
                        #region#给要移库的桶分配新储位
                        var locatInfoNew = new SysStorageLocat();
                        //优先分配内侧储位
                        var locatListNei = Db.Queryable<SysStorageLocat>().Where(w => w.IsDel == "0" && w.Status == "0" && !string.IsNullOrEmpty(w.AisleOne) && w.WareHouseNo == palletInfoYi.WareHouseNo && w.AreaNo == palletInfoYi.AreaNo).ToList();
                        foreach (var item in locatListNei)
                        {
                            //判断该内侧储位的外侧储位是否有桶
                            var detailWai = Db.Queryable<DataStockDetail>().First(w => w.IsDel == "0" && w.LocatNo == item.AisleOne);
                            if (detailWai != null)
                            {
                                continue;
                            }
                            locatInfoNew = item;
                            break;
                        }
                        if (locatInfoNew == null)
                        {
                            //没有内侧储位,再查找外侧储位
                            var locatListWai = Db.Queryable<SysStorageLocat>().Where(w => w.IsDel == "0" && w.Status == "0" && string.IsNullOrEmpty(w.AisleOne) && w.WareHouseNo == palletInfoYi.WareHouseNo && w.AreaNo == palletInfoYi.AreaNo).OrderBy(o => o.Row).ToList();
                            foreach (var item in locatListWai)
                            {
                                //判断该储位的内侧储位是否有正在执行的任务
                                var locatItemNei = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.AisleOne == item.LocatNo);
                                if (locatItemNei != null && locatItemNei.Status != "0")
                                {
                                    continue;
                                }
                                locatInfoNew = item;
                                break;
                            }
                        }
                        if (locatInfoNew == null)
                        {
                            throw new Exception("没有空余的空储位进行移库");
                        }
                        #endregion
 
                        //给下车下发移库任务
                        var taskNo = new Common().GetMaxNo("TN");//任务号
                        //任务信息
                        var logTaskEntry = new LogTask
                        {
                            TaskNo = taskNo,
                            Sender = "WMS",
                            Receiver = "RCS",
                            //IsSuccess = 1, //是否下发成功 0失败 1成功
                            SendDate = DateTime.Now,  //发送时间
                            //BackDate = DateTime.Now,  //返回时间
                            StartLocat = palletInfoYi.LocatNo,//起始位置
                            EndLocat = locatInfoNew.LocatNo,//目标位置
                            PalletNo = palletInfoYi.PalletNo,//托盘码
                            IsSend = 1,//是否可再次下发
                            IsCancel = 1,//是否可取消
                            IsFinish = 1,//是否可完成
                            Type = "1",//任务类型 0 入库任务 1 出库任务  2 移库任务
                            OrderType = "3",//单据类型 0 入库 1 出库 3移库
                            Status = "0",//任务状态0:等待执行1正在执行2执行完成
                            NoticeDetailNo = 0,
                            Msg = $"将桶{palletInfoYi.PalletNo}从{palletInfoYi.LocatNo}移到{locatInfoNew.LocatNo}", //关键信息
                            LotNo = ""//批次号
                        };
 
                        //组织下发小车任务信息
                        var task = new TaskDetial
                        {
                            Taskno = taskNo,//任务号
                            Startport = palletInfoYi.LocatNo,//起始位置
                            Endport = locatInfoNew.LocatNo,//endLoction.LocatNo,//目标位置
                            Pallno = palletInfoYi.PalletNo,//桶号
                            Crtype = "1",//叫桶
                            WareHouseNo = palletInfoYi.WareHouseNo//车间编码
                        };
                        string agvMsg = string.Empty;
                        //给下车下发任务
                        logTaskEntry.SendDate = DateTime.Now;//发送时间
                        var agvResult = CreateTaskForAgv(task, url, out agvMsg, "70");
                        if (agvResult)//成功
                        {
                            //请求成功修改任务表相应字段状态
                            logTaskEntry.IsSuccess = 1;
                            logTaskEntry.IsSend = 0;
                            //logTaskEntry.IsCancel = 0;
                            logTaskEntry.BackDate = DateTime.Now;
                            logTaskEntry.Status = "1";//正在执行
                            Db.Insertable(logTaskEntry).ExecuteCommand();
 
 
                            locatInfo.Status = "5";//移出中
                            Db.Updateable(locatInfo).ExecuteCommand();
 
                            locatInfoNew.Status = "4";//移入中
                            Db.Updateable(locatInfoNew).ExecuteCommand();
                        }
                        else//失败
                        {
                            logTaskEntry.IsSuccess = 0;
                            logTaskEntry.Information = agvMsg;
                            Db.Insertable(logTaskEntry).ExecuteCommand();
 
                            throw new Exception($"给小车下发移库任务失败,桶号:{palletInfoYi.PalletNo}");
                        }
                    }
                }
                return true;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
    }
}