杨张扬
2025-05-30 6ed0553e8d5bc38b728979c2078d3cbffdc88bf2
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
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
using HH.WCS.Mobox3.DoubleCoin.device;
using HH.WCS.Mobox3.DoubleCoin.models;
using HH.WCS.Mobox3.DoubleCoin.process;
using HH.WCS.Mobox3.DoubleCoin.util;
using HH.WCS.Mobox3.DoubleCoin.wms;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Runtime.ConstrainedExecution;
using System.Threading;
using System.Threading.Tasks;
using static HH.WCS.Mobox3.DoubleCoin.api.ApiModel;
using static HH.WCS.Mobox3.DoubleCoin.api.OtherModel;
 
namespace HH.WCS.Mobox3.DoubleCoin.api
{
    /// <summary>
    /// api接口辅助类
    /// </summary>
    public class ApiHelper
    {
        static ApiHelper()
        {
 
        }
 
        /// <summary>
        /// PDA满托出库抽检
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        internal static SimpleResult PDAFullCheckOut(PDAFullCheckInfo model)
        {
            LogHelper.Info("触发API:PDA满托出库抽检" + JsonConvert.SerializeObject(model), "API");
            var db = new SqlHelper<object>().GetInstance();
            var result = new SimpleResult();//返回结果
            try
            {
                var locCnt = db.Queryable<TN_Loc_Container>().First(a => a.S_CNTR_CODE == model.rfId);
                if (locCnt == null)
                {
                    result.resultCode = 1;
                    result.resultMsg = $"容器{ model.rfId}没有绑定过货位";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
                var cG_Detail = db.Queryable<TN_CG_Detail>().First(a => a.S_CNTR_CODE == locCnt.S_CNTR_CODE);
                if (cG_Detail == null)
                {
                    result.resultCode = 2;
                    result.resultMsg = $"容器{locCnt.S_CNTR_CODE}没有货品";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
 
 
                var startLoc = db.Queryable<TN_Location>().First(a => a.S_AREA_CODE == Settings.Areas[1] && a.S_CODE == locCnt.S_LOC_CODE && a.N_LOCK_STATE == 0 && a.S_LOCK_STATE == "无" && a.C_ENABLE == "Y");
                if (startLoc == null)
                {
                    result.resultCode = 3;
                    result.resultMsg = $"起点货位{locCnt.S_LOC_CODE}未解锁,或者不属于抽检区";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
 
                var endLoc = db.Queryable<TN_Location>().
                    Where(o => o.S_AREA_CODE == Settings.Areas[7] && o.N_LOCK_STATE == 0 && o.S_LOCK_STATE == "无" && o.C_ENABLE == "Y" && o.N_CURRENT_NUM == 0).
                    OrderBy(o => o.T_MODIFY, OrderByType.Asc).First();//查询合适的终点货位
 
                if (endLoc == null)
                {
                    result.resultCode = 4;
                    result.resultMsg = $"容器{locCnt.S_CNTR_CODE}未找到合适的终点货位";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
 
                //创建人工拆盘出库任务
                if (WCSHelper.CreateTask(startLoc.S_CODE, endLoc.S_CODE, "人工出库抽检", 3, locCnt.S_CNTR_CODE, cG_Detail.S_SPE))//创建搬送任务,起点终点容器
                {
                    LocationHelper.LockLoc(startLoc.S_CODE, 2);//起点出库锁,
                    LocationHelper.LockLoc(endLoc.S_CODE, 1);//终点入库锁
                    LogHelper.Info($"生成人工出库抽检任务成功,容器号{locCnt.S_CNTR_CODE},起点{startLoc.S_CODE},终点{endLoc.S_CODE}");
                    cG_Detail.S_ITEM_STATE = "待检";
                    cG_Detail.N_ITEM_STATE = 1;
                    db.Updateable<TN_CG_Detail>().UpdateColumns(it => new { it.S_ITEM_STATE, it.N_ITEM_STATE }).ExecuteCommand();
 
                    Task task99 = Task.Run(() =>
                    {
                        WMSHelper.InsertOpInfo(model.staff, "人工出库抽检", locCnt.S_CNTR_CODE);
                    });
 
                    result.resultCode = 0;
                    result.resultMsg = $"成功";
                    return result;
                }
                else
                {
                    result.resultCode = 5;
                    result.resultMsg = $"生成人工出库抽检任务失败,容器号{locCnt.S_CNTR_CODE},起点{startLoc.S_CODE},终点{endLoc.S_CODE}";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
 
            }
            catch (Exception ex)
            {
                result.resultCode = -1;
                result.resultMsg = $"发生了异常:{ex.Message}";
                LogHelper.Info(result.resultMsg);
                return result;
            }
        }
 
        /// <summary>
        /// PDA满托复检判断
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        internal static SimpleResult PDAFullCheckIn(PDAFullCheckInfo model)
        {
            LogHelper.Info("触发API:PDA满托复检判断" + JsonConvert.SerializeObject(model), "API");
            var db = new SqlHelper<object>().GetInstance();
            var result = new SimpleResult();//返回结果
            try
            {
                var locCnt = db.Queryable<TN_Loc_Container>().First(a => a.S_CNTR_CODE == model.rfId);
                if (locCnt == null)
                {
                    result.resultCode = 1;
                    result.resultMsg = $"容器{ model.rfId}没有绑定过货位";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
 
                var cG_Detail = db.Queryable<TN_CG_Detail>().First(a => a.S_CNTR_CODE == locCnt.S_CNTR_CODE);
                if (cG_Detail == null)
                {
                    result.resultCode = 2;
                    result.resultMsg = $"容器{locCnt.S_CNTR_CODE}没有货品";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
 
 
                var startLoc = db.Queryable<TN_Location>().First(a => a.S_AREA_CODE == Settings.Areas[7] && a.S_CODE == locCnt.S_LOC_CODE && a.N_LOCK_STATE == 0 && a.S_LOCK_STATE == "无" && a.C_ENABLE == "Y");
                if (startLoc == null)
                {
                    result.resultCode = 3;
                    result.resultMsg = $"起点货位{locCnt.S_LOC_CODE}未解锁,或者不属于复检区";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
 
                var endLoc = new TN_Location();
                if (model.cgState == 0)//合格回库
                {
                    cG_Detail.N_ITEM_STATE = 0;
                    cG_Detail.S_ITEM_STATE = "合格";
                    endLoc = db.Queryable<TN_Location>().
                            Where(o => o.S_AREA_CODE == Settings.Areas[1] && o.N_LOCK_STATE == 0 && o.S_LOCK_STATE == "无" && o.C_ENABLE == "Y" && o.N_CURRENT_NUM == 0).
                            OrderBy(o => o.T_MODIFY, OrderByType.Asc).First();//查询合适的终点货位
                }
                else if (model.cgState == 4)//技术合格回库
                {
                    cG_Detail.N_ITEM_STATE = 4;
                    cG_Detail.S_ITEM_STATE = "技术合格";
                    endLoc = db.Queryable<TN_Location>().
                            Where(o => o.S_AREA_CODE == Settings.Areas[8] && o.N_LOCK_STATE == 0 && o.S_LOCK_STATE == "无" && o.C_ENABLE == "Y" && o.N_CURRENT_NUM == 0).
                            OrderBy(o => o.T_MODIFY, OrderByType.Asc).First();//查询合适的终点货位
                }
                else if (model.cgState == 2)//不合格回炉
                {
                    cG_Detail.N_ITEM_STATE = 2;
                    cG_Detail.S_ITEM_STATE = "不合格";
                    endLoc = db.Queryable<TN_Location>().
                            Where(o => o.S_AREA_CODE == Settings.Areas[8] && o.N_LOCK_STATE == 0 && o.S_LOCK_STATE == "无" && o.C_ENABLE == "Y" && o.N_CURRENT_NUM == 0).
                            OrderBy(o => o.T_MODIFY, OrderByType.Asc).First();//查询合适的终点货位
                }
                else if (model.cgState == 5)//混合也回库,通过点对点再发线边
                {
                    cG_Detail.N_ITEM_STATE = 5;
                    cG_Detail.S_ITEM_STATE = "混合";
                    endLoc = db.Queryable<TN_Location>().
                            Where(o => o.S_AREA_CODE == Settings.Areas[2] && o.N_LOCK_STATE == 0 && o.S_LOCK_STATE == "无" && o.C_ENABLE == "Y" && o.N_CURRENT_NUM == 0).
                            OrderBy(o => o.T_MODIFY, OrderByType.Asc).First();//查询合适的终点货位
                }
                else
                {
                    result.resultCode = 6;
                    result.resultMsg = $"物料状态只能为0、2、4、5,如果为其他值,则不处理,0合格,2不合格,4技术合格,5混合";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
                
 
                if (endLoc == null)
                {
                    result.resultCode = 4;
                    result.resultMsg = $"容器{locCnt.S_CNTR_CODE}未找到合适的终点货位";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
 
                var typeName = (model.cgState == 0) ? "人工复检回库" : "人工复检回炉";
 
                //创建人工拆盘出库任务
                if (WCSHelper.CreateTask(startLoc.S_CODE, endLoc.S_CODE, typeName, 3, locCnt.S_CNTR_CODE,out string taskNo,cG_Detail.S_SPE))//创建搬送任务,起点终点容器
                {
                    LocationHelper.LockLoc(startLoc.S_CODE, 2);//起点出库锁,
                    LocationHelper.LockLoc(endLoc.S_CODE, 1);//终点入库锁
                    LogHelper.Info($"生成{typeName}任务成功,容器号{locCnt.S_CNTR_CODE},起点{startLoc.S_CODE},终点{endLoc.S_CODE}");
 
                    db.Updateable<TN_CG_Detail>().UpdateColumns(it => new { it.S_ITEM_STATE, it.N_ITEM_STATE }).ExecuteCommand();
 
                    Task task99 = Task.Run(() =>
                    {
                        WMSHelper.InsertOpInfo(model.staff, typeName, locCnt.S_CNTR_CODE);
                    });
 
                    Task task2 = Task.Run(() =>
                    {
                        if (model.cgState == 2)
                        {
                            var target = new TN_RemeltDetail()
                            {
                                CNTCODE = locCnt.S_CNTR_CODE,
                                STARTLOC = startLoc.S_CODE,
                                LOGINNAME = model.staff,
                                ITEMCODE = cG_Detail.S_ITEM_CODE,
                                SPEC = cG_Detail.S_SPE,
                                CARCODE = cG_Detail.S_CAR_CODE,
                                WEIGHT = cG_Detail.F_QTY,
                                REMELTTIME = DateTime.Now,
                                TASKNO = taskNo,
                                SHIFT = model.shift,
                            };
                            SpecHelper.InsertRemeltDetail(target);
                        }
                    });
 
                    Task task27 = Task.Run(() =>
                    {
                        if (model.cgState != 2)
                        {
                            var target = new TN_InventoryM()
                            {
                                S_ID = cG_Detail.S_ID,
                                RFID = cG_Detail.S_CNTR_CODE,
                                SPEC = cG_Detail.S_SPE,
                                WEIGHT = cG_Detail.F_QTY,
                                ITEMSTATE = cG_Detail.S_ITEM_STATE,
                                ITEMCODE = cG_Detail.S_ITEM_CODE,
                                LOGINNAME = model.staff,
                                SHIFT = model.shift,
                            };
                            SpecHelper.InsertInventoryM(target);
                        }
                    });
 
                    result.resultCode = 0;
                    result.resultMsg = $"成功";
                    return result;
                }
                else
                {
                    result.resultCode = 5;
                    result.resultMsg = $"生成{typeName}任务失败,容器号{locCnt.S_CNTR_CODE},起点{startLoc.S_CODE},终点{endLoc.S_CODE}";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
            }
            catch (Exception ex)
            {
                result.resultCode = -1;
                result.resultMsg = $"PDA满托复检判断,发生了异常:{ex.Message}";
                LogHelper.Info(result.resultMsg);
                return result;
            }
        }
 
        /// <summary>
        /// 人工次品回炉
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        internal static SimpleResult PDAReturnReset(PDAReturnResetInfo model)
        {
            LogHelper.Info("触发API:人工次品回炉" + JsonConvert.SerializeObject(model), "API");
            var db = new SqlHelper<object>().GetInstance();
            var result = new SimpleResult();//返回结果
 
            try
            {
                var startLoc  =  db.Queryable<TN_Location>().First(o => o.S_CODE == model.startLoc && o.N_CURRENT_NUM ==0 && o.N_LOCK_STATE == 0 && o.S_LOCK_STATE=="无" && o.S_AREA_CODE == Settings.Areas[4]);
                if (startLoc == null)
                {
                    result.resultCode = 1;
                    result.resultMsg = $"未找到合适的起点信息,要求:o.S_CODE == { model.startLoc} && o.N_CURRENT_NUM ==0 && o.N_LOCK_STATE == 0 && o.S_LOCK_STATE == 无 && o.S_AREA_CODE == {Settings.Areas[4]}";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
 
                var endLoc = db.Queryable<TN_Location>().First(o =>o.S_AREA_CODE == Settings.Areas[11] && o.N_CURRENT_NUM == 0 && o.N_LOCK_STATE == 0 && o.S_LOCK_STATE == "无");
                if (endLoc == null)
                {
                    result.resultCode = 2;
                    result.resultMsg = $"未找到合适的终点信息,要求:o.S_AREA_CODE == {Settings.Areas[11]} && o.N_CURRENT_NUM == 0 && o.N_LOCK_STATE == 0 && o.S_LOCK_STATE == 无";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
 
                var cgInfo = db.Queryable<TN_CG_Detail>().First(a=>a.S_CNTR_CODE == model.rfId);
                if (cgInfo == null)
                {
                    result.resultCode = 3;
                    result.resultMsg = $"未找到对应的物料信息,要求:a.S_CNTR_CODE == {model.rfId}";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
 
                var task1 = new TN_Task()
                {
                    S_CODE = WCSHelper.GenerateTaskNo(),
                    S_START_AREA = startLoc.S_AREA_CODE,
                    S_END_AREA = endLoc.S_AREA_CODE,
                    S_START_LOC = startLoc.S_CODE,
                    S_END_LOC = endLoc.S_CODE,
                    S_TYPE = "人工次品回炉",
                    N_B_STATE = 0,
                    S_B_STATE = "等待",
                    S_CNTR_CODE = model.rfId,
                    S_SPEC = cgInfo.S_ITEM_SPEC,
                };
 
                startLoc.N_LOCK_STATE = 2;
                startLoc.S_LOCK_STATE = "出库锁";
                startLoc.N_CURRENT_NUM = 1;
                startLoc.T_MODIFY = System.DateTime.Now;
 
                endLoc.N_LOCK_STATE = 1;
                endLoc.S_LOCK_STATE = "入库锁";
                startLoc.T_MODIFY = System.DateTime.Now;
 
                using (var tran = db.Ado.UseTran())
                {
                    var locCnt = db.Queryable<TN_Loc_Container>().First(o => o.S_CNTR_CODE == model.rfId && o.S_LOC_CODE != model.startLoc);
                    TN_Location locOld = null;
                    if (locCnt != null)
                    {
                        locOld = db.Queryable<TN_Location>().First(o => o.S_CODE == locCnt.S_LOC_CODE);
                        if (locOld != null)
                        {
                            locOld.N_CURRENT_NUM = 0;
                            if (db.Updateable<TN_Location>(locOld).UpdateColumns(it => new { it.N_CURRENT_NUM }).ExecuteCommand() <= 0)
                            {
                                tran.RollbackTran();
                                result.resultCode = 4;
                                result.resultMsg = $"更新旧货位失败,{locOld.S_CODE}";
                                LogHelper.Info(result.resultMsg);
                                return result;
                            }
                        }
 
                        locCnt.S_LOC_CODE = model.startLoc;
                        if (db.Updateable<TN_Loc_Container>(locCnt).UpdateColumns(it => new { it.S_LOC_CODE }).ExecuteCommand() <= 0)
                        {
                            tran.RollbackTran();
                            result.resultCode = 5;
                            result.resultMsg = $"更新货位容器关系表(换绑)失败,{model.startLoc}";
                            LogHelper.Info(result.resultMsg);
                            return result;
                        }
                    }
                    else
                    {
                        locCnt = new TN_Loc_Container()
                        {
                            S_LOC_CODE = model.startLoc,
                            S_CNTR_CODE = model.rfId,
                        };
                        if (db.Insertable<TN_Loc_Container>(locCnt).ExecuteCommand() <= 0)
                        {
                            tran.RollbackTran();
                            result.resultCode = 6;
                            result.resultMsg = $"插入货位容器关系表(绑定)失败,{model.startLoc}";
                            LogHelper.Info(result.resultMsg);
                            return result;
                        }
                    }
 
                    if (db.Insertable<TN_Task>(task1).ExecuteCommand() > 0 &&
                        db.Updateable<TN_Location>(startLoc).ExecuteCommand() > 0 &&
                        db.Updateable<TN_Location>(endLoc).ExecuteCommand() > 0)
                    {
 
                        Task task99 = Task.Run(() =>
                        {
                            WMSHelper.InsertOpInfo(model.staff, "人工次品回炉", locCnt.S_CNTR_CODE);
                        });
 
                        Task task2 = Task.Run(() =>
                        {
                            var target = new TN_RemeltDetail()
                            {
                                TASKNO = task1.S_CODE,
                                CNTCODE = model.rfId,
                                STARTLOC = model.startLoc,
                                LOGINNAME = model.staff,
                                ITEMCODE = cgInfo.S_ITEM_CODE,
                                SPEC = cgInfo.S_SPE,
                                CARCODE = cgInfo.S_CAR_CODE,
                                WEIGHT = cgInfo.F_QTY,
                                REMELTTIME = DateTime.Now,
                                SHIFT = model.shift,
                            };
                            SpecHelper.InsertRemeltDetail(target);
                        });
 
                        tran.CommitTran();
                        result.resultCode = 0;
                        result.resultMsg = "成功";
                        LogHelper.Info($"生成人工次品回炉任务成功,容器:{model.rfId},起点:{model.startLoc},终点:{endLoc.S_CODE}");
                        return result;
                    }
                    else
                    {
                        tran.RollbackTran();
                        result.resultCode = 7;
                        result.resultMsg = $"生成人工次品回炉任务失败,容器:{model.rfId},起点:{model.startLoc},终点:{endLoc.S_CODE}";
                        LogHelper.Info(result.resultMsg);
                        return result;
                    }
                }
 
            }
            catch(Exception ex)
            {
                result.resultCode = -1;
                result.resultMsg = $"生成人工次品回炉任务失败,发生了异常:{ex.Message}";
                LogHelper.Info(result.resultMsg);
                return result;
            }
        }
 
        /// <summary>
        /// 显示满托缓存库当前规格的托盘数量
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        internal static string ShowCntCountBySpe(ShowCntCountBySpeInfo model)
        {
            try
            {
                var db = new SqlHelper<object>().GetInstance();
                //查询符合规格的物料所在的未锁定已启用指定货区的货位,起点
                var allCg = db.Queryable<TN_CG_Detail>().
                    LeftJoin<TN_Loc_Container>((o, i) => o.S_CNTR_CODE == i.S_CNTR_CODE).
                    LeftJoin<TN_Location>((o, i, s) => i.S_LOC_CODE == s.S_CODE).
                    Where((o, i, s) => s.N_CURRENT_NUM > 0 && s.S_AREA_CODE == Settings.Areas[1] && s.N_LOCK_STATE == 0 && s.S_LOCK_STATE == "无" && s.C_ENABLE == "Y"
                    && (o.S_ITEM_SPEC == model.Spe || o.S_SPE == model.Spe)).//指定规格
                    ToList();//筛选有容器货位关系表信息,筛选有容器货品明细表信息
 
                var allCount = allCg.Count;
                var waittime = SpecHelper.GetExportTime(model.Spe);
                var okCount = allCg.Count(o => o.N_ITEM_STATE == 0 && o.S_ITEM_STATE == "合格" && o.T_MODIFY.AddHours(waittime) <= System.DateTime.Now);
                return $"满足{model.Spe}规格的总托盘数量:{allCount},其中合格且静置时间已过的托盘数量:{okCount}";
            }
            catch (Exception ex)
            {
                LogHelper.Info($"发生了异常:{ex.Message}");
                return $"发生了异常:{ex.Message}";
            }
        }
 
        /// <summary>
        /// 接收工单信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        //internal static SimpleResult ReceiveWorkOrder(WorkOrderInfo model)
        //{
        //    string modelstr = JsonConvert.SerializeObject(model);
        //    LogHelper.Info("触发API:接收工单信息" + modelstr, "API");
 
        //    var result = new SimpleResult();//返回结果
 
        //    var db = new SqlHelper<object>().GetInstance();
 
        //    try
        //    {
        //        //查询是否有这个对应id的工单          
        //        var modelWO = db.Queryable<TN_WorkOrder>().First(a => a.S_WORK_NO == model.S_WORK_NO);
        //        //是否有这个产线
        //        var plInfo = Settings.ProductionLines.FirstOrDefault(a => a.ProductionLine_AreaCode == model.S_LINE_NO);
 
        //        if (plInfo == null)//不存在的产线
        //        {
        //            result.resultCode = 1;
        //            result.resultMsg = $"不存在或未录入的产线{model.S_LINE_NO}";
        //            LogHelper.Info(result.resultMsg);
        //            return result;
        //        }
 
        //        if (modelWO != null)//重复工单不插入
        //        {
        //            result.resultCode = 2;
        //            result.resultMsg = $"工单已收到无法重复插入此工单{modelWO.S_WORK_NO}";
        //            LogHelper.Info(result.resultMsg);
        //            return result;
        //        }
 
        //        //TODO modelQuery工单属性赋值
        //        modelWO = new TN_WorkOrder()
        //        {
        //            S_WORK_NO = model.S_WORK_NO,
        //            S_LINE_NO = model.S_LINE_NO,
        //            S_ITEM_CODE = model.S_ITEM_CODE,
        //            S_B_STATE = "新建",
        //            S_BATCH_NO = model.S_BATCH_NO,
        //            S_ITEM_SPEC = model.S_ITEM_SPEC,
        //        };
 
        //        string modelWOstr = JsonConvert.SerializeObject(modelWO);
 
        //        if (db.Insertable<TN_WorkOrder>(modelWO).ExecuteCommand() > 0) //插入工单
        //        {
        //            result.resultCode = 0;
        //            result.resultMsg = $"收到工单开始执行{modelWO.S_WORK_NO}";
        //            LogHelper.Info(result.resultMsg);
        //            return result;
        //        }
        //        else
        //        {
        //            result.resultCode = 3;
        //            result.resultMsg = $"工单插入失败{modelWO.S_WORK_NO}";
        //            LogHelper.Info(result.resultMsg);
        //            return result;
        //        }
 
        //    }
        //    catch (Exception ex)
        //    {
        //        result.resultCode = -1;
        //        result.resultMsg = $"发生了异常:{ex.Message}";
        //        LogHelper.Info(result.resultMsg);
        //        return result;
        //    }
 
        //}
 
        /// <summary>
        /// 绑定容器生产车数信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        //internal static SimpleResult CarNumBind(BindInfo model)
        //{
        //    LogHelper.Info("触发API:绑定容器生产车数信息" + JsonConvert.SerializeObject(model), "API");
 
        //    var result = new SimpleResult();
        //    //查询者
        //    var db = new SqlHelper<object>().GetInstance();
 
        //    try
        //    {
        //        if (model.CarNum > 0)
        //        {
        //            //查询符合要求
        //            var modelCA = db.Queryable<TN_CAR_IN>().First(b => b.S_CNTR_CODE == model.RFID);
 
        //            if (modelCA == null)//没有找到该条数据,可以插入
        //            {
        //                TN_CAR_IN carIn = new TN_CAR_IN();
        //                carIn.S_CNTR_CODE = model.RFID;
        //                carIn.S_CAR_CODE = model.CarNum;
        //                int count = db.Insertable<TN_CAR_IN>(carIn).ExecuteCommand();//插入子表
        //                if (count > 0)
        //                {
        //                    result.resultCode = 0;
        //                    result.resultMsg = $"绑定成功{model.RFID}";
        //                    LogHelper.Info(result.resultMsg);
        //                }
        //                else
        //                {
        //                    result.resultCode = 2;
        //                    result.resultMsg = $"绑定数据插入失败{model.RFID}";
        //                    LogHelper.Info(result.resultMsg);
        //                }
        //            }
        //            else
        //            {   //有该车数,更新
        //                modelCA.S_CNTR_CODE = model.RFID;
        //                modelCA.S_CAR_CODE = model.CarNum;
        //                int count = db.Updateable<TN_CAR_IN>(modelCA).ExecuteCommand();//更新子表
        //                if (count > 0)
        //                {
        //                    result.resultCode = 0;
        //                    result.resultMsg = $"绑定成功{model.RFID}";
        //                    LogHelper.Info(result.resultMsg);
        //                }
        //                else
        //                {
        //                    result.resultCode = 3;
        //                    result.resultMsg = $"绑定数据更新失败{model.RFID}";
        //                    LogHelper.Info(result.resultMsg);
        //                }
        //            }
        //        }
        //        else
        //        {
        //            result.resultCode = 7;
        //            result.resultMsg = $"绑定失败,传输的信息不能为空{JsonConvert.SerializeObject(model)}";
        //            LogHelper.Info(result.resultMsg);
        //        }
        //        return result;
        //    }
        //    catch (Exception ex)
        //    {
        //        result.resultCode = -1;
        //        result.resultMsg = $"发生了异常:{ex.Message}";
        //        LogHelper.Info(result.resultMsg);
        //        return result;
        //    }
        //}
 
 
        /// <summary>
        /// PDA满托下线入库呼叫
        /// </summary>
        internal static SimpleResult PDAFullInArea(PDAFullInAreaInfo model)
        {
            var db = new SqlHelper<object>().GetInstance();
            var result = new SimpleResult();//返回结果
            LogHelper.Info("触发API:PDA满托下线入库" + JsonConvert.SerializeObject(model), "API");
            try
            {
                //该产线的满托下线位置,起点
                var startPoint = Settings.ProductionLines.FirstOrDefault(a => a.PointOut == model.StartLoc);
                if (startPoint != null)//起点存在
                {
                    var startLoc = db.Queryable<TN_Location>().First(a => a.S_LOCK_STATE == "无" && a.N_LOCK_STATE == 0 && a.S_CODE == model.StartLoc && a.C_ENABLE == "Y");
                    if (startLoc != null)//该产线的起点没锁住
                    {
                        var cgInfo = db.Queryable<TN_CG_Detail>().First(a => a.S_CNTR_CODE == model.RfId);
                        if (cgInfo != null)//有残留的容器货品明细关系表信息
                        {
                            result.resultCode = 1;
                            result.resultMsg = $"该容器{model.RfId}已绑定货品{cgInfo.S_ITEM_CODE},如果要使用,容器先解绑货品";
                            LogHelper.Info(result.resultMsg);
                            return result;
                        }
                        else
                        {
                            var locCntrRel = db.Queryable<TN_Loc_Container>().First(a => a.S_CNTR_CODE == model.RfId);
                            if (locCntrRel != null)//当前容器是否绑定了货位
                            {
                                result.resultCode = 2;
                                result.resultMsg = $"该容器{model.RfId}已绑定货位{locCntrRel.S_LOC_CODE},如果要使用,容器先解绑货位";
                                LogHelper.Info(result.resultMsg);
                                return result;
                            }
                            else
                            {
                                //新增货位容器关系表
                                locCntrRel = new TN_Loc_Container()
                                {
                                    S_LOC_CODE = startPoint.PointOut,
                                    S_CNTR_CODE = model.RfId,
                                };
 
                                var readBytes1 = S7Helper.ReadBytes(startPoint.ProductionLine_IP, 20, 2, 4);
                                byte[] rfid = new byte[4] { readBytes1[0], readBytes1[1], readBytes1[2], readBytes1[3] };
                                string rfids16 = BitConverter.ToString(rfid);
                                string rfids = (((readBytes1[0]) << 24) + (readBytes1[1] << 16) + (readBytes1[2] << 8) + readBytes1[3]).ToString();
                                LogHelper.Info($"产线容器号:{rfids},其16进制形式:{rfids16}");
 
                                if (rfids != model.RfId)
                                {
                                    result.resultCode = 9;
                                    result.resultMsg = $"PDA填入的容器号{model.RfId}与S7产线通讯的容器号{rfids}不同";
                                    LogHelper.Info(result.resultMsg);
                                    return result;
                                }
                                var readBytes2 = S7Helper.ReadBytes(startPoint.ProductionLine_IP, 20, 26, 2);
                                var modelWeight = ((readBytes2[0]) << 8) + readBytes2[1];
 
                                var modelCI = db.Queryable<TN_CAR_IN>().First(i => i.S_CAR_CODE == model.CarCode && i.S_CNTR_CODE == model.RfId && i.S_B_STATE == "0");//查询是否有这个对应的且状态正常的容器车号子表
 
                                TN_CG_Detail tN_CG_Detail = new TN_CG_Detail()//新增容器货品明细表
                                {
                                    S_CNTR_CODE = model.RfId,
                                    S_ITEM_SPEC = model.Spe,
                                    S_SPE = model.Spe,
                                    S_CAR_CODE = model.CarCode,
                                    N_ITEM_STATE = 1,
                                    S_ITEM_STATE = "待检",
                                    F_QTY = modelWeight,//这里的重量要当成数量来使用
                                };
 
                                var endLoc = db.Queryable<TN_Location>().
                                                      Where(c => c.S_AREA_CODE == Settings.Areas[1] && c.S_LOCK_STATE == "无" && c.N_LOCK_STATE == 0 && c.N_CURRENT_NUM == 0 && c.C_ENABLE == "Y").
                                                      OrderBy(o => o.T_MODIFY, OrderByType.Asc).First();//查询合适的终点货位,直接判断当前数量为0即可
 
                                if (endLoc != null)
                                {
                                    var task1 = new TN_Task()
                                    {
                                        S_CODE = WCSHelper.GenerateTaskNo(),
                                        S_START_AREA = startLoc.S_AREA_CODE,
                                        S_END_AREA = endLoc.S_AREA_CODE,
                                        S_START_LOC = startLoc.S_CODE,
                                        S_END_LOC = endLoc.S_CODE,
                                        S_TYPE = "PDA满托下线入库",
                                        N_B_STATE = 0,
                                        S_CNTR_CODE = model.RfId,
                                        S_SPEC = model.Spe,
                                    };
 
                                    startLoc.N_LOCK_STATE = 2;
                                    startLoc.S_LOCK_STATE = "出库锁";
                                    startLoc.N_CURRENT_NUM = 1;
                                    startLoc.T_MODIFY = DateTime.Now;
 
                                    endLoc.N_LOCK_STATE = 1;
                                    endLoc.S_LOCK_STATE = "入库锁";
                                    startLoc.T_MODIFY = DateTime.Now;
 
                                    using (var tran = db.Ado.UseTran())
                                    {
                                        if (modelCI == null)//容器车号子表不存在
                                        {
                                            modelCI = new TN_CAR_IN()
                                            {
                                                S_CNTR_CODE = model.RfId,
                                                S_CAR_CODE = model.CarCode,
                                                S_B_STATE = "0",
                                            };
                                            if (db.Insertable<TN_CAR_IN>(modelCI).ExecuteCommand() <= 0)
                                            {
                                                tran.RollbackTran();
                                                result.resultCode = 8;
                                                result.resultMsg = $"新增的TN_CAR_IN表插入失败,容器:{modelCI.S_CNTR_CODE},车号:{modelCI.S_CAR_CODE}";
                                                LogHelper.Info(result.resultMsg);
                                                return result;
                                            }
                                        }
 
                                        if (db.Insertable<TN_Task>(task1).ExecuteCommand() > 0 &&
                                            db.Updateable<TN_Location>(startLoc).ExecuteCommand() > 0 &&
                                            db.Updateable<TN_Location>(endLoc).ExecuteCommand() > 0 &&
                                            db.Insertable<TN_Loc_Container>(locCntrRel).ExecuteCommand() > 0 &&
                                            db.Insertable<TN_CG_Detail>(tN_CG_Detail).ExecuteCommand() > 0)//创建搬送任务,起点终点容器
                                        {
                                            tran.CommitTran();
                                            result.resultCode = 0;
                                            result.resultMsg = "成功";
                                            LogHelper.Info($"生成PDA满托下线入库任务成功,容器:{modelCI.S_CNTR_CODE},起点:{startPoint.PointOut},终点:{endLoc.S_CODE}");
 
                                            Task task99 = Task.Run(() =>
                                            {
                                                WMSHelper.InsertOpInfo(model.staff, "PDA满托下线入库", modelCI.S_CNTR_CODE);
                                            });
 
                                            Task task98 = Task.Run(() =>
                                            {
                                                var target = new TN_EquipProDetail()
                                                {
                                                    S_ID = tN_CG_Detail.S_ID,
                                                    TASKTYPE = "PDA满托下线入库",
                                                    RFID = model.RfId,
                                                    SPEC = model.Spe,
                                                    CARCODE = model.CarCode,
                                                    WEIGHT = modelWeight,
                                                    ITEMSTATE = "待检",
                                                    LOGINNAME = model.staff,
                                                    SHIFT = model.shift,
                                                    STARTLOC = startLoc.S_CODE,
                                                };
                                                SpecHelper.InsertEquipProDetail(target);
                                            });
 
                                            Task task27 = Task.Run(() =>
                                            {
                                                var target = new TN_InventoryM()
                                                {
                                                    S_ID = tN_CG_Detail.S_ID,
                                                    RFID = model.RfId,
                                                    SPEC = model.Spe,
                                                    WEIGHT = modelWeight,
                                                    ITEMSTATE = "待检",
                                                    LOGINNAME = model.staff,
                                                    SHIFT = model.shift,
                                                };
                                                SpecHelper.InsertInventoryM(target);
                                            });
 
                                            return result;
                                        }
                                        else
                                        {
                                            tran.RollbackTran();
                                            result.resultCode = 7;
                                            result.resultMsg = $"生成满托下线入库任务失败,容器:{modelCI.S_CNTR_CODE},起点:{startPoint.PointOut},终点:{endLoc.S_CODE}";
                                            LogHelper.Info(result.resultMsg);
                                            return result;
                                        }
                                    }
                                }
                                else
                                {
                                    result.resultCode = 4;
                                    result.resultMsg = $"未找到合适的终点,容器:{modelCI.S_CNTR_CODE}";
                                    LogHelper.Info(result.resultMsg);
                                    return result;
                                }
                            }
                        }
                    }
                    else
                    {
                        result.resultCode = 9;
                        result.resultMsg = $"此位置:{startPoint.PointOut}已锁住";
                        LogHelper.Info(result.resultMsg);
                        return result;
                    }
                }
                else
                {
                    result.resultCode = 11;
                    result.resultMsg = $"未找到该起点:{model.StartLoc}";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
            }
            catch (Exception ex)
            {
                result.resultCode = -1;
                result.resultMsg = $"发生了异常{ex.Message}";
                LogHelper.Info(result.resultMsg);
                return result;
            }
        }
 
        /// <summary>
        /// 更新物料明细的状态,0合格1待检2不合格,下线即待检
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        internal static SimpleResult UpCgDetailModel(UpCgDetailModel model)
        {
            LogHelper.Info("触发API:更新物料明细的状态" + JsonConvert.SerializeObject(model), "API");
 
            var result = new SimpleResult();//返回结果
            var db = new SqlHelper<object>().GetInstance();
 
            try
            {
                var cG_Detail = db.Queryable<TN_CG_Detail>().First(a => a.S_CNTR_CODE == model.cntID);
                if (cG_Detail != null)
                {
                    cG_Detail.N_ITEM_STATE = model.state;
                    cG_Detail.S_ITEM_STATE = model.stateInfo;
                    cG_Detail.T_MODIFY = System.DateTime.Now;
                    if (db.Updateable<TN_CG_Detail>(cG_Detail).UpdateColumns(it => new { it.N_ITEM_STATE, it.S_ITEM_STATE, it.T_MODIFY }).ExecuteCommand() > 0)
                    {
                        result.resultCode = 0;
                        result.resultMsg = "成功";
                        LogHelper.Info(result.resultMsg);
 
                        Task task99 = Task.Run(() =>
                        {
                            WMSHelper.InsertOpInfo(model.staff, "更新物料明细状态", model.cntID);
                        });
                    }
                    else
                    {
                        result.resultCode = 1;
                        result.resultMsg = "更新失败";
                        LogHelper.Info(result.resultMsg);
                    }
                }
                else
                {
                    result.resultCode = 2;
                    result.resultMsg = "未找到该物料信息";
                    LogHelper.Info(result.resultMsg);
                }
 
                return result;
            }
            catch (Exception ex)
            {
                result.resultCode = -1;
                result.resultMsg = $"发生了异常:{ex.Message}";
                LogHelper.Info(result.resultMsg);
                return result;
            }
        }
 
        /// <summary>
        /// 满托出库上线
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        internal static SimpleResult FullOutWarehouse(FullOutWarehouseInfo model)
        {
            LogHelper.Info("触发API:满托出库上线" + JsonConvert.SerializeObject(model), "API");
 
            var result = new SimpleResult();
            var db = new SqlHelper<object>().GetInstance();
            TN_Location endLoc = new TN_Location();
            try
            {
                if (model.EndLoc != string.Empty)//指定终点货位
                {
                    endLoc = db.Queryable<TN_Location>().First(a => a.S_CODE == model.EndLoc);
 
                    if (endLoc == null)
                    {
                        result.resultCode = 2;
                        result.resultMsg = $"未找到终点货位{model.EndLoc}";
                        LogHelper.Info(result.resultMsg);
                        return result;
                    }
 
                    if (endLoc.S_AREA_CODE != Settings.Areas[2])
                    {
                        result.resultCode = 3;
                        result.resultMsg = $"该终点货位{model.EndLoc}不在线边区{Settings.Areas[2]}";
                        LogHelper.Info(result.resultMsg);
                        return result;
                    }
 
                    if (endLoc.N_LOCK_STATE != 0 || endLoc.S_LOCK_STATE != "无" || endLoc.C_ENABLE != "Y")
                    {
                        result.resultCode = 4;
                        result.resultMsg = $"该终点货位{model.EndLoc}未解锁";
                        LogHelper.Info(result.resultMsg);
                        return result;
                    }
 
                    if (endLoc.N_CURRENT_NUM != 0)
                    {
                        result.resultCode = 5;
                        result.resultMsg = $"该终点货位{model.EndLoc}已满";
                        LogHelper.Info(result.resultMsg);
                        return result;
                    }
                }
                else//未指定
                {
                    result.resultCode = 6;
                    result.resultMsg = $"终点货位参数不能为空";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
 
                var waitTime = SpecHelper.GetWaitTime(model.Spe);
 
                //优先技术合格
                var startLoc = db.Queryable<TN_Location>().
                    LeftJoin<TN_Loc_Container>((o, i) => o.S_CODE == i.S_LOC_CODE).
                    LeftJoin<TN_CG_Detail>((o, i, s) => i.S_CNTR_CODE == s.S_CNTR_CODE).
                    Where((o, i, s) => o.N_CURRENT_NUM > 0 && o.S_AREA_CODE == Settings.Areas[1] && o.N_LOCK_STATE == 0 && o.S_LOCK_STATE == "无" && o.C_ENABLE == "Y"
                    && (s.S_ITEM_SPEC == model.Spe || s.S_SPE == model.Spe)//指定规格
                    && s.N_ITEM_STATE == 4 && s.S_ITEM_STATE == "技术合格"). //技术合格
                    First();//筛选有容器货位关系表信息,筛选有容器货品明细表信息
 
                if (startLoc == null)//其次合格
                {
                    //查询符合规格的物料所在的未锁定已启用指定货区的货位,起点
                    startLoc = db.Queryable<TN_Location>().
                       LeftJoin<TN_Loc_Container>((o, i) => o.S_CODE == i.S_LOC_CODE).
                       LeftJoin<TN_CG_Detail>((o, i, s) => i.S_CNTR_CODE == s.S_CNTR_CODE).
                       Where((o, i, s) => o.N_CURRENT_NUM > 0 && o.S_AREA_CODE == Settings.Areas[1] && o.N_LOCK_STATE == 0 && o.S_LOCK_STATE == "无" && o.C_ENABLE == "Y"
                       && (s.S_ITEM_SPEC == model.Spe || s.S_SPE == model.Spe)//指定规格
                       && s.N_ITEM_STATE == 0 && s.S_ITEM_STATE == "合格" //合格的
                       && s.T_MODIFY.AddMinutes(waitTime) <= System.DateTime.Now).//根据规格的不同静置不同分钟才能出库
                       OrderBy((o, i, s) => s.T_CREATE, OrderByType.Asc).//先进先出
                       First();//筛选有容器货位关系表信息,筛选有容器货品明细表信息
                }
 
                if (startLoc == null)
                {
                    result.resultCode = 7;
                    result.resultMsg = $"未找到合适的起点货位,需要满足:属于{Settings.Areas[1]}的,当前数量大于0的,未锁定的货位,且物料明细表有满足数量S_SPE等于{model.Spe}的合格的静置时间足够的物料";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
 
                var container = db.Queryable<TN_Loc_Container>().First(a => a.S_LOC_CODE == startLoc.S_CODE).S_CNTR_CODE;//根据起点货位找到容器
                if (container == string.Empty)
                {
                    result.resultCode = 8;
                    result.resultMsg = $"货位容器关系表未找到起点{startLoc.S_CODE}的容器号";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
 
                var cgInfo = db.Queryable<TN_CG_Detail>().First(a => a.S_CNTR_CODE == container);
 
 
                //创建满托出库上线任务
                if (WCSHelper.CreateTask(startLoc.S_CODE, endLoc.S_CODE, "满托出库上线", 3, container,out string taskNo, cgInfo.S_SPE))//创建搬送任务,起点终点容器
                {
                    LocationHelper.LockLoc(startLoc.S_CODE, 2);//起点出库锁,
                    LocationHelper.LockLoc(endLoc.S_CODE, 1);//终点入库锁
                    LogHelper.Info($"生成满托出库上线任务成功,容器号{container},起点{startLoc.S_CODE},终点{endLoc.S_CODE}");
 
                    Task task1 = Task.Run(() =>
                    {
                        WMSHelper.InsertOpInfo(model.staff, "满托出库上线", container);
                    });
 
                    Task task2 = Task.Run(() =>
                    {
                        var target = new TN_Component_Detail()
                        {
                           ITEMCODE = cgInfo.S_ITEM_CODE,
                           CNTRCODE = container,
                           CARCODE = cgInfo.S_CAR_CODE,
                           ENDLOC = endLoc.S_CODE,
                           STAFF = model.staff,
                           WEIGHT = cgInfo.F_QTY,
                           SPEC = model.Spe,
                           TASKNO = taskNo,
                           SHIFT = model.shift,
                        };
                        SpecHelper.InsertComponentDetail(target);
                    });
 
                    Task task27 = Task.Run(() =>
                    {
                        SpecHelper.DeleteInventoryM(cgInfo.S_ID);
                    });
                }
                else
                {
                    result.resultCode = 9;
                    result.resultMsg = $"生成满托出库上线任务失败,容器号{container},起点{startLoc.S_CODE},终点{endLoc.S_CODE}";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
            }
            catch (Exception ex)
            {
                result.resultCode = -1;
                result.resultMsg = $"发生了异常:{ex.Message}";
                LogHelper.Info(result.resultMsg);
                return result;
            }
            return result;
        }
 
        /// <summary>
        /// 空托下线堆叠(只绑定货位容器不创建任务)
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        internal static SimpleResult EmptyInStackArea(EmptyInStackAreaInfo model)
        {
            LogHelper.Info("触发API:空托下线堆叠" + JsonConvert.SerializeObject(model), "API");
 
          
            var result = new SimpleResult();
            var db = new SqlHelper<object>().GetInstance();
            TN_Location startLoc = new TN_Location();
 
            try
            {
                var startLocCntrRel = db.Queryable<TN_Loc_Container>().First(a => a.S_LOC_CODE == model.startLoc && a.S_CNTR_CODE == model.cntID);
                if (startLocCntrRel != null)
                {
                    result.resultCode = 1;
                    result.resultMsg = $"货位{model.startLoc}已绑定容器{model.cntID}";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
 
                var cG_Detail = db.Queryable<TN_CG_Detail>().Where(a => a.S_CNTR_CODE == model.cntID).ToList();
                if (cG_Detail.Count > 0)//防止现实中未清空货品而直接调用
                {
                    result.resultCode = 2;
                    result.resultMsg = $"容器{model.cntID}未解绑货品";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
 
                startLoc = db.Queryable<TN_Location>().First(a => a.S_CODE == model.startLoc);
 
                if (startLoc == null)
                {
                    result.resultCode = 7;
                    result.resultMsg = $"货位{model.startLoc}不存在";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
 
                if (startLoc.N_CURRENT_NUM > 0)
                {
                    result.resultCode = 3;
                    result.resultMsg = $"货位{model.startLoc}已绑定容器,无法再次绑定";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
 
                if (startLoc.S_AREA_CODE != Settings.Areas[6])
                {
                    result.resultCode = 4;
                    result.resultMsg = $"空托{model.cntID}货位{startLoc.S_CODE}所在区域不为人工空托区";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
 
                if (startLoc.N_LOCK_STATE != 0 || startLoc.S_LOCK_STATE != "无")
                {
                    result.resultCode = 5;
                    result.resultMsg = $"货位{startLoc.S_CODE}未解锁";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
 
                if (LocationHelper.BindingLoc(model.startLoc, new List<string>() { model.cntID }).Contains("成功"))
                {
                    result.resultCode = 0;
                    result.resultMsg = "成功";
                    LogHelper.Info($"货位{startLoc.S_CODE}绑定容器{model.cntID}成功");
 
                    Task task1 = Task.Run(() =>
                    {
                        WMSHelper.InsertOpInfo(model.staff, "空托下线堆叠", model.cntID);
                    });
 
                    return result;
                }
                else
                {
                    result.resultCode = 6;
                    result.resultMsg = $"货位{startLoc.S_CODE}绑定容器{model.cntID}失败";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
            }
            catch (Exception ex)
            {
                result.resultCode = 1;
                result.resultMsg = $"发生了异常:{ex.Message}";
                LogHelper.Info(result.resultMsg);
                return result;
            }
        }
 
 
        /// <summary>
        /// 余料下线入库
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        internal static SimpleResult SurplusInWarehouse(InWarehouseInfo model)
        {
 
            LogHelper.Info("触发API:余料下线入库" + JsonConvert.SerializeObject(model), "API");
 
            var result = new SimpleResult();
            var db = new SqlHelper<object>().GetInstance();
            TN_Location startLoc_Old = new TN_Location();
            TN_Location startLoc_New = new TN_Location();
 
            try
            {
 
                var cG_Detail = db.Queryable<TN_CG_Detail>().First(a => a.S_CNTR_CODE == model.cntID);
                if (cG_Detail == null)
                {
                    result.resultCode = 7;
                    result.resultMsg = $"容器{model.cntID}已清空货品";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
 
                var startLocCntrRel = db.Queryable<TN_Loc_Container>().First(a => a.S_CNTR_CODE == model.cntID);
                if (startLocCntrRel == null)
                {
                    result.resultCode = 2;
                    result.resultMsg = $"容器{ model.cntID}未找到货位信息,";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
                startLoc_Old = db.Queryable<TN_Location>().First(a => a.S_CODE == startLocCntrRel.S_LOC_CODE);
 
                if (startLoc_Old == null)
                {
                    result.resultCode = 3;
                    result.resultMsg = $"未找到旧的货位信息{startLocCntrRel.S_LOC_CODE}";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
 
                if (startLoc_Old.N_LOCK_STATE != 0 || startLoc_Old.S_LOCK_STATE != "无" || startLoc_Old.C_ENABLE != "Y")
                {
                    result.resultCode = 5;
                    result.resultMsg = $"旧的货位{startLoc_Old.S_CODE}未解锁";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
 
                startLoc_New = db.Queryable<TN_Location>().First(a => a.S_CODE == model.startLoc && a.N_LOCK_STATE == 0 && a.S_LOCK_STATE == "无" && a.S_AREA_CODE == Settings.Areas[10]);
 
                if (startLoc_New == null)
                {
                    result.resultCode = 8;
                    result.resultMsg = $"新的起点货位{model.startLoc}在{Settings.Areas[10]}未解锁";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
 
                if (startLoc_New.S_CODE != startLoc_Old.S_CODE && startLoc_New.N_CURRENT_NUM > 0)
                {
                    result.resultCode = 9;
                    result.resultMsg = $"新的起点货位{model.startLoc}在{Settings.Areas[10]}有其他货品";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
 
                var weightPoint = Settings.WeightDevices[0]?.Point ?? "Null";
 
                if (db.Queryable<TN_Task>().Count(a => a.N_B_STATE < 3 && a.S_END_LOC == weightPoint) > 2)
                {
                    result.resultCode = 11;
                    result.resultMsg = $"终点称重货位{weightPoint}的正在执行的任务数量大于2";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
 
                //查询符合的未锁定已启用指定货区的货位,终点
                var endLoc = db.Queryable<TN_Location>().
                          First(o => o.S_AREA_CODE == Settings.Areas[9] && o.S_CODE == weightPoint && o.C_ENABLE == "Y");//查询合适的终点货位
 
                if (endLoc == null)
                {
                    result.resultCode = 6;
                    result.resultMsg = $"容器{ model.cntID}货位{startLoc_Old.S_CODE}未找到合适的终点";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
 
                //换绑
                startLoc_Old.N_CURRENT_NUM = 0;
                db.Updateable<TN_Location>(startLoc_Old).UpdateColumns(it => new { it.N_CURRENT_NUM }).ExecuteCommand();
                startLocCntrRel.S_LOC_CODE = model.startLoc;
                db.Updateable<TN_Loc_Container>(startLocCntrRel).UpdateColumns(it => new { it.S_LOC_CODE }).ExecuteCommand();
                startLoc_New.N_CURRENT_NUM = 1;
                db.Updateable<TN_Location>(startLoc_New).UpdateColumns(it => new { it.N_CURRENT_NUM }).ExecuteCommand();
 
                //创建余料下线入库任务,第一阶段
                if (WCSHelper.CreateTask(startLoc_New.S_CODE, endLoc.S_CODE, "余料下线入库", 3, model.cntID,out string taksNo,cG_Detail.S_SPE))//创建搬送任务,起点终点容器
                {
                    LocationHelper.LockLoc(startLoc_New.S_CODE, 2);//起点出库锁,
                    LocationHelper.LockLoc(endLoc.S_CODE, 1);//终点入库锁
                    LogHelper.Info($"生成余料下线入库任务成功,容器号{model.cntID},起点{startLoc_Old.S_CODE},终点{endLoc.S_CODE}");
 
                    Task task1 = Task.Run(() =>
                    {
                        WMSHelper.InsertOpInfo(model.staff, "余料下线入库", model.cntID);
                    });
 
                    Task task2 = Task.Run(() =>
                    {
                        var target = new TN_SurplusDetail()
                        {
                            CNTCODE = model.cntID,
                            STARTLOC = startLoc_New.S_CODE,
                            LOGINNAME = model.staff,
                            ITEMCODE = cG_Detail.S_ITEM_CODE,
                            SPEC = cG_Detail.S_SPE,
                            CARCODE = cG_Detail.S_CAR_CODE,
                            WEIGHT = cG_Detail.F_QTY,
                            REMELTTIME = DateTime.Now,
                            TASKNO = taksNo,
                            SHIFT = model.shift,
                        };
                        SpecHelper.InsertSurplusDetail(target);
                    });
 
                    Task task27 = Task.Run(() =>
                    {
                        var target = new TN_InventoryM()
                        {
                            S_ID = cG_Detail.S_ID,
                            RFID = cG_Detail.S_CNTR_CODE,
                            SPEC = cG_Detail.S_SPE,
                            WEIGHT = cG_Detail.F_QTY,
                            ITEMSTATE = cG_Detail.S_ITEM_STATE,
                            ITEMCODE = cG_Detail.S_ITEM_CODE,
                            LOGINNAME = model.staff,
                            SHIFT = model.shift,
                        };
                        SpecHelper.InsertInventoryM(target);
                    });
                }
                else
                {
                    result.resultCode = 10;
                    result.resultMsg = $"生成余料下线入库任务失败,容器号{model.cntID},起点{startLoc_Old.S_CODE},终点{endLoc.S_CODE}";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
            }
            catch (Exception ex)
            {
                result.resultCode = 1;
                result.resultMsg = $"发生了异常:{ex.Message}";
                LogHelper.Info(result.resultMsg);
                return result;
            }
            return result;
        }
 
        /// <summary>
        /// 人工拆盘出库
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        internal static SimpleResult SplitOutWarehouse(SplitOutWarehouseInfo model)
        {
            LogHelper.Info("触发API:人工拆盘出库" + JsonConvert.SerializeObject(model), "API");
 
            var result = new SimpleResult();
            var db = new SqlHelper<object>().GetInstance();
 
            try
            {
                var locCntrRel = db.Queryable<TN_Loc_Container>().First(a => a.S_CNTR_CODE == model.cntID);
                if (locCntrRel == null)
                {
                    result.resultCode = 2;
                    result.resultMsg = $"容器号{model.cntID}没有货位容器信息";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
                var cG_Detail = db.Queryable<TN_CG_Detail>().First(a => a.S_CNTR_CODE == locCntrRel.S_CNTR_CODE);
                if (cG_Detail == null)
                {
                    result.resultCode = 3;
                    result.resultMsg = $"容器{locCntrRel.S_CNTR_CODE}没有货品";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
                var startLoc = db.Queryable<TN_Location>().First(a => a.S_AREA_CODE == Settings.Areas[1] && a.S_CODE == locCntrRel.S_LOC_CODE && a.N_LOCK_STATE == 0 && a.S_LOCK_STATE == "无" && a.C_ENABLE == "Y");
                if (startLoc == null)
                {
                    result.resultCode = 4;
                    result.resultMsg = $"起点货位{locCntrRel.S_LOC_CODE}未解锁,或不属于{Settings.Areas[1]}";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
 
                TN_Location endLoc = new TN_Location();
                if (model.endLoc != string.Empty)//指定终点
                {
                    endLoc = db.Queryable<TN_Location>().First(a => a.S_CODE == model.endLoc);
 
                    if (endLoc.S_AREA_CODE != Settings.Areas[5])
                    {
                        result.resultCode = 5;
                        result.resultMsg = $"该终点货位{model.endLoc}不在拆盘区{Settings.Areas[5]}";
                        LogHelper.Info(result.resultMsg);
                        return result;
                    }
 
                    if (endLoc.N_CURRENT_NUM > 0)
                    {
                        result.resultCode = 6;
                        result.resultMsg = $"该终点货位{model.endLoc}有货品信息";
                        LogHelper.Info(result.resultMsg);
                        return result;
                    }
 
                    if (endLoc.N_LOCK_STATE != 0 || endLoc.S_LOCK_STATE != "无" || endLoc.C_ENABLE != "Y")
                    {
                        result.resultCode = 7;
                        result.resultMsg = $"该终点货位{model.endLoc}未解锁";
                        LogHelper.Info(result.resultMsg);
                        return result;
                    }
                }
                else//未指定,随机查询合适终点货位
                {
                    endLoc = db.Queryable<TN_Location>().
                    Where(o => o.S_AREA_CODE == Settings.Areas[5] && o.N_LOCK_STATE == 0 && o.S_LOCK_STATE == "无" && o.C_ENABLE == "Y" && o.N_CURRENT_NUM == 0).
                    OrderBy(o => o.T_MODIFY, OrderByType.Asc).First();//查询合适的终点货位
 
                }
                if (endLoc == null)
                {
                    result.resultCode = 8;
                    result.resultMsg = $"容器{locCntrRel.S_CNTR_CODE}未找到合适的终点货位";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
                //创建人工拆盘出库任务
                if (WCSHelper.CreateTask(startLoc.S_CODE, endLoc.S_CODE, "人工拆盘出库", 3, locCntrRel.S_CNTR_CODE,cG_Detail.S_SPE))//创建搬送任务,起点终点容器
                {
                    LocationHelper.LockLoc(startLoc.S_CODE, 2);//起点出库锁,
                    LocationHelper.LockLoc(endLoc.S_CODE, 1);//终点入库锁
                    LogHelper.Info($"生成人工拆盘出库任务成功,容器号{locCntrRel.S_CNTR_CODE},起点{startLoc.S_CODE},终点{endLoc.S_CODE}");
 
                    Task task1 = Task.Run(() =>
                    {
                        WMSHelper.InsertOpInfo(model.staff, "人工拆盘出库", model.cntID);
                    });
                }
                else
                {
                    result.resultCode = 9;
                    result.resultMsg = $"生成人工拆盘出库任务失败,容器号{locCntrRel.S_CNTR_CODE},起点{startLoc.S_CODE},终点{endLoc.S_CODE}";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
            }
            catch (Exception ex)
            {
                result.resultCode = 1;
                result.resultMsg = $"发生了异常:{ex.Message}";
                LogHelper.Info(result.resultMsg);
                return result;
            }
            return result;
        }
 
 
        /// <summary>
        /// 人工拆盘入库
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        internal static SimpleResult ResultInWarehouse(ResultInWarehouseInfo model)
        {
            LogHelper.Info("触发API:人工拆盘入库" + JsonConvert.SerializeObject(model), "API");
 
            var result = new SimpleResult();
            var db = new SqlHelper<object>().GetInstance();
            string cntLog = $"旧的容器号{model.old_CntId}";
            try
            {
                if (model.new_IsOK == model.old_IsOK)
                {
                    result.resultCode = 19;
                    result.resultMsg = $"人工拆盘入库,物料需要一个合格一个不合格";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
 
                if (model.new_CntIds == model.old_CntId)
                {
                    result.resultCode = 13;
                    result.resultMsg = $"拆出的托盘不能与要拆的托盘一样";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
 
                if (model.old_StartLoc == model.new_StartLoc)
                {
                    result.resultCode = 14;
                    result.resultMsg = $"拆出的托盘的起点不能与要拆的托盘的起点一样";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
 
                TN_CG_Detail cG_old_Detail = db.Queryable<TN_CG_Detail>().First(a => a.S_CNTR_CODE == model.old_CntId);
                if (cG_old_Detail == null)
                {
                    result.resultCode = 2;
                    result.resultMsg = $"未找到被拆的容器货品明细表信息," + cntLog;
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
 
                var startLoc_Old = db.Queryable<TN_Location>().
                    First(o => o.S_CODE == model.old_StartLoc && o.N_CURRENT_NUM == 0 && o.S_AREA_CODE == Settings.Areas[5] && o.N_LOCK_STATE == 0 && o.S_LOCK_STATE == "无");
 
                if (startLoc_Old == null)
                {
                    result.resultCode = 4;
                    result.resultMsg = $"要拆分的旧托盘的起点货位{model.old_StartLoc}未解锁或不属于区域{Settings.Areas[5]}";
                    LogHelper.Info(result.resultMsg); LogHelper.Info(result.resultMsg);
                    return result;
                }
 
                var startLoc_New = db.Queryable<TN_Location>().
                    First(b => b.S_CODE == model.new_StartLoc && b.N_CURRENT_NUM == 0 && b.S_AREA_CODE == Settings.Areas[5] && b.N_LOCK_STATE == 0 && b.S_LOCK_STATE == "无");
 
 
                if (startLoc_New == null)
                {
                    result.resultCode = 5;
                    result.resultMsg = $"拆分后的新托盘的起点货位{model.new_StartLoc}未解锁或不属于区域{Settings.Areas[5]}";
                    LogHelper.Info(result.resultMsg); LogHelper.Info(result.resultMsg);
                    return result;
                }
 
                var locCntrRel_New1 = db.Queryable<TN_Loc_Container>().First(a => a.S_CNTR_CODE == model.new_CntIds);
                if (locCntrRel_New1 != null)
                {
                    LocationHelper.PdaUnBind(new PdaUnBindInfo() { cntID = model.new_CntIds ,reqCode=1});
                    result.resultCode = 6;
                    result.resultMsg = $"拆分后的新托盘存在残留的货位容器关系表,已解绑重置,容器号{model.new_CntIds},货位{locCntrRel_New1.S_LOC_CODE}";
                    LogHelper.Info(result.resultMsg);
                }
 
                var locCntrRel_Old1 = db.Queryable<TN_Loc_Container>().First(a => a.S_CNTR_CODE == model.old_CntId);
                if (locCntrRel_Old1 != null)
                {
                    LocationHelper.PdaUnBind(new PdaUnBindInfo() { cntID = model.old_CntId, reqCode = 1 });
                    result.resultCode = 6;
                    result.resultMsg = $"被拆的旧托盘存在残留的货位容器关系表,已解绑重置,容器号{model.old_CntId},货位{locCntrRel_Old1.S_LOC_CODE}";
                    LogHelper.Info(result.resultMsg);
                }
 
                locCntrRel_Old1 = new TN_Loc_Container()
                {
                    S_LOC_CODE = model.old_StartLoc,
                    S_CNTR_CODE = model.old_CntId,
                    T_MODIFY = System.DateTime.Now
                };
 
                locCntrRel_New1 = new TN_Loc_Container()
                {
                    S_LOC_CODE = model.new_StartLoc,
                    S_CNTR_CODE = model.new_CntIds,
                    T_MODIFY = System.DateTime.Now
                };
 
                cG_old_Detail.S_ITEM_STATE = LocationHelper.GetStrByOk(model.old_IsOK);
                cG_old_Detail.N_ITEM_STATE = model.new_IsOK;
                cG_old_Detail.S_Separate_ID = Guid.NewGuid().ToString("D");
 
                var cG_new_Detail_Ins = new TN_CG_Detail()
                {
                    S_CNTR_CODE = model.new_CntIds,
                    S_BATCH_NO = cG_old_Detail.S_BATCH_NO,
                    S_ITEM_SPEC = cG_old_Detail.S_ITEM_SPEC,
                    S_SPE = cG_old_Detail.S_SPE,
                    S_ITEM_STATE = LocationHelper.GetStrByOk(model.new_IsOK),
                    N_ITEM_STATE = model.new_IsOK,
                    S_CAR_CODE = cG_old_Detail.S_CAR_CODE,
                    T_MODIFY = System.DateTime.Now,
                    //C_ITEM_MERGE = model.new_CntIds,
                    S_ITEM_CODE = cG_old_Detail.S_ITEM_CODE,
                    S_Separate_ID = cG_old_Detail.S_Separate_ID
                };
 
                var weightPoint = Settings.WeightDevices[0]?.Point ?? "Null";
 
                if (db.Queryable<TN_Task>().Count(a => a.N_B_STATE < 3 && a.S_END_LOC == weightPoint) > 2)
                {
                    result.resultCode = 11;
                    result.resultMsg = $"终点称重货位{weightPoint}的正在执行的任务数量大于2";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
 
                var endLoc_Ok = db.Queryable<TN_Location>().
                          First(o => o.S_AREA_CODE == Settings.Areas[9] && o.S_CODE == weightPoint);//查询合适的终点货位,去称重
 
                var endLoc_NG = db.Queryable<TN_Location>().
                          First(o => o.S_AREA_CODE == Settings.Areas[8] && o.N_LOCK_STATE == 0 && o.S_LOCK_STATE == "无" && o.N_CURRENT_NUM == 0);//查询合适的终点货位
 
                if (endLoc_Ok == null)
                {
                    result.resultCode = 10;
                    result.resultMsg = $"合格的物料未找到合适的称重终点货位{weightPoint}";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
 
                if (endLoc_NG == null)
                {
                    result.resultCode = 16;
                    result.resultMsg = $"不合格的物料未找到合适的回炉区终点货位{Settings.Areas[8]}";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
 
                var taskLog1_Old = $"旧容器号{model.old_CntId},旧容器号的起点{startLoc_Old.S_CODE},终点{endLoc_Ok.S_CODE}";
 
                var taskLog_New = $"新容器号{model.new_CntIds},新容器号的起点{model.new_StartLoc},终点{endLoc_NG.S_CODE}";
 
                List<CreateTasks> modesCreateTask = new List<CreateTasks>();
 
                var newTaskCreate = new CreateTasks()
                {
                    taskNo = WCSHelper.GenerateTaskNo(),
                    from = model.new_StartLoc,
                    fromArea = startLoc_New.S_AREA_CODE,
                    to = (model.new_IsOK == 2) ? endLoc_NG.S_CODE : endLoc_Ok.S_CODE,
                    toArea = (model.new_IsOK == 2) ? endLoc_NG.S_AREA_CODE : endLoc_Ok.S_AREA_CODE,
                    taskType = (model.new_IsOK == 2) ? "人工拆盘回炉" : "人工拆盘回库",
                    pri = 3,
                    cntrInfo = model.new_CntIds,
                    spec = cG_new_Detail_Ins.S_SPE,
                };
 
                modesCreateTask.Add(newTaskCreate);
 
                var oldTaskCreate = new CreateTasks()
                {
                    taskNo = WCSHelper.GenerateTaskNo(),
                    from = startLoc_Old.S_CODE,
                    fromArea = startLoc_Old.S_AREA_CODE,
                    to = (model.old_IsOK == 2) ? endLoc_NG.S_CODE : endLoc_Ok.S_CODE,
                    toArea = (model.old_IsOK == 2) ? endLoc_NG.S_AREA_CODE : endLoc_Ok.S_AREA_CODE,
                    taskType = (model.old_IsOK == 2) ? "人工拆盘回炉" : "人工拆盘回库",
                    pri = 3,
                    cntrInfo = model.old_CntId,
                    spec = cG_old_Detail.S_SPE,
                };
                modesCreateTask.Add(oldTaskCreate);
 
                startLoc_Old.N_LOCK_STATE = 2;
                startLoc_Old.S_LOCK_STATE = "出库锁";
                startLoc_Old.N_CURRENT_NUM = 1;
 
 
                startLoc_New.N_LOCK_STATE = 2;
                startLoc_New.S_LOCK_STATE = "出库锁";
                startLoc_New.N_CURRENT_NUM = 1;
 
                endLoc_Ok.N_LOCK_STATE = 1;
                endLoc_Ok.S_LOCK_STATE = "入库锁";
 
                using (var tran = db.Ado.UseTran())
                {
                    if (model.old_IsOK != 2 || model.new_IsOK != 2)
                    {
                        if (db.Updateable<TN_Location>(endLoc_Ok).ExecuteCommand()<=0)
                        {
                            tran.RollbackTran();
                            result.resultCode = 12;
                            result.resultMsg = $"生成人工拆盘入库任务失败," + taskLog_New + taskLog1_Old;
                            LogHelper.Info(result.resultMsg);
                            return result;
                        }
                    }
 
                    if (
                        db.Updateable<TN_Location>(startLoc_Old).ExecuteCommand() > 0 &&
                        db.Updateable<TN_Location>(startLoc_New).ExecuteCommand() > 0 &&
                        db.Insertable<TN_Loc_Container>(locCntrRel_New1).ExecuteCommand() > 0 &&
                        db.Insertable<TN_Loc_Container>(locCntrRel_Old1).ExecuteCommand() > 0 &&
                        db.Updateable<TN_CG_Detail>(cG_old_Detail).ExecuteCommand() > 0 &&
                        db.Insertable<TN_CG_Detail>(cG_new_Detail_Ins).ExecuteCommand()>0
                        )
                    {
                        //创建人工拆盘入库任务,合格的盘回去称重,不合格的回炉,即去不合格区
                        if (WCSHelper.CreateTask(modesCreateTask))//创建搬送任务,起点终点容器
                        {
                            tran.CommitTran();
                            LogHelper.Info($"生成人工拆盘入库任务成功," + taskLog_New + taskLog1_Old);
 
                            Task task1 = Task.Run(() =>
                            {
                                WMSHelper.InsertOpInfo(model.staff, "人工拆盘入库", model.new_CntIds + "," + model.old_CntId);
                            });
 
                            Task task2 = Task.Run(() =>
                            {
                                if (model.old_IsOK == 1)
                                {
                                    var target = new TN_RemeltDetail()
                                    {
                                        TASKNO = oldTaskCreate.taskNo,
                                        CNTCODE = cG_old_Detail.S_CNTR_CODE,
                                        STARTLOC = startLoc_Old.S_CODE,
                                        LOGINNAME = model.staff,
                                        ITEMCODE = cG_old_Detail.S_ITEM_CODE,
                                        SPEC = cG_old_Detail.S_SPE,
                                        CARCODE = cG_old_Detail.S_CAR_CODE,
                                        WEIGHT = cG_old_Detail.F_QTY,
                                        REMELTTIME = DateTime.Now,
                                        SHIFT = model.shift,
                                    };
                                    SpecHelper.InsertRemeltDetail(target);
                                }
                                if (model.new_IsOK == 1)
                                {
                                    var target = new TN_RemeltDetail()
                                    {
                                        TASKNO = newTaskCreate.taskNo,
                                        CNTCODE = cG_new_Detail_Ins.S_CNTR_CODE,
                                        STARTLOC = startLoc_New.S_CODE,
                                        LOGINNAME = model.staff,
                                        ITEMCODE = cG_new_Detail_Ins.S_ITEM_CODE,
                                        SPEC = cG_new_Detail_Ins.S_SPE,
                                        CARCODE = cG_new_Detail_Ins.S_CAR_CODE,
                                        WEIGHT = cG_new_Detail_Ins.F_QTY,
                                        REMELTTIME = DateTime.Now,
                                        SHIFT = model.shift,
                                    };
                                    SpecHelper.InsertRemeltDetail(target);
                                }
                            });
 
                            Task task27 = Task.Run(() =>
                            {
                                if (model.old_IsOK == 1)
                                {
                                    var target = new TN_InventoryM()
                                    {
                                        S_ID = cG_old_Detail.S_ID,
                                        RFID = cG_old_Detail.S_CNTR_CODE,
                                        SPEC = cG_old_Detail.S_SPE,
                                        WEIGHT = cG_old_Detail.F_QTY,
                                        ITEMSTATE = cG_old_Detail.S_ITEM_STATE,
                                        ITEMCODE = cG_old_Detail.S_ITEM_CODE,
                                        LOGINNAME = model.staff,
                                        SHIFT = model.shift,
                                    };
                                    SpecHelper.InsertInventoryM(target);
                                }
                                if (model.new_IsOK == 1)
                                {
                                    var target = new TN_InventoryM()
                                    {
                                        S_ID = cG_new_Detail_Ins.S_ID,
                                        RFID = cG_new_Detail_Ins.S_CNTR_CODE,
                                        SPEC = cG_new_Detail_Ins.S_SPE,
                                        WEIGHT = cG_new_Detail_Ins.F_QTY,
                                        ITEMSTATE = cG_new_Detail_Ins.S_ITEM_STATE,
                                        ITEMCODE = cG_new_Detail_Ins.S_ITEM_CODE,
                                        LOGINNAME = model.staff,
                                        SHIFT = model.shift,
                                    };
                                    SpecHelper.InsertInventoryM(target);
                                }
                            });
                        }
                        else
                        {
                            tran.RollbackTran();
                            result.resultCode = 17;
                            result.resultMsg = $"生成人工拆盘入库任务失败," + taskLog_New + taskLog1_Old;
                            LogHelper.Info(result.resultMsg);
                            return result;
                        }
                    }
                    else
                    {
                        tran.RollbackTran();
                        result.resultCode = 18;
                        result.resultMsg = $"生成人工拆盘入库任务失败," + taskLog_New + taskLog1_Old;
                        LogHelper.Info(result.resultMsg);
                        return result;
                    }
                }
            }
            catch (Exception ex)
            {
                result.resultCode = -1;
                result.resultMsg = $"发生了异常:{ex.Message}";
                LogHelper.Info(result.resultMsg);
                return result;
            }
 
            return result;
        }
 
 
        /// <summary>
        /// 人工创建点到点任务
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        internal static SimpleResult CreateP2PTask(CreateP2PTaskInfo model)
        {
            LogHelper.Info("触发API:人工创建点到点任务" + JsonConvert.SerializeObject(model), "API");
 
            var result = new SimpleResult();
            var db = new SqlHelper<object>().GetInstance();
            try
            {
                var startLoc = db.Queryable<TN_Location>().First(a => a.S_CODE == model.startLoc);
                
                if(!(startLoc != null && startLoc.N_CURRENT_NUM > 0 && startLoc.N_LOCK_STATE == 0 && startLoc.S_LOCK_STATE == "无"))
                {
                    result.resultCode = 1;
                    result.resultMsg = $"起始位置{model.startLoc}不满足条件:startLoc != null && startLoc.N_CURRENT_NUM > 0 && startLoc.N_LOCK_STATE == 0 && startLoc.S_LOCK_STATE == 无";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
 
                var endLoc = db.Queryable<TN_Location>().First(a => a.S_CODE == model.endLoc);
 
                if (!(endLoc != null && endLoc.N_CURRENT_NUM == 0 && endLoc.N_LOCK_STATE == 0 && endLoc.S_LOCK_STATE == "无"))
                {
                    result.resultCode = 2;
                    result.resultMsg = $"终点位置{model.endLoc}不满足条件:endLoc != null && endLoc.N_CURRENT_NUM == 0 && endLoc.N_LOCK_STATE == 0 && endLoc.S_LOCK_STATE == 无";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
 
                var startLocCnt = db.Queryable<TN_Loc_Container>().First(a => a.S_LOC_CODE == model.startLoc);
                
                if (startLocCnt == null)
                {
                    startLocCnt = new TN_Loc_Container()
                    {
                        S_LOC_CODE = model.startLoc,
                        S_CNTR_CODE = Guid.NewGuid().ToString("D"),
                    };
                    db.Insertable<TN_Loc_Container>(startLocCnt).ExecuteCommand();
                }
 
                //创建点对点任务
                if (WCSHelper.CreateTask(startLoc.S_CODE, endLoc.S_CODE, "点对点", 3, startLocCnt.S_CNTR_CODE))//创建搬送任务,起点终点容器
                {
                    LocationHelper.LockLoc(startLoc.S_CODE, 2);//起点出库锁,
                    LocationHelper.LockLoc(endLoc.S_CODE, 1);//终点入库锁
                    LogHelper.Info($"生成点对点任务成功,容器号{startLocCnt.S_CNTR_CODE},起点{startLoc.S_CODE},终点{endLoc.S_CODE}");
 
                    Task task1 = Task.Run(() =>
                    {
                        WMSHelper.InsertOpInfo(model.staff, "点对点", startLocCnt.S_CNTR_CODE);
                    });
 
                    var cginfo = db.Queryable<TN_CG_Detail>().First(a => a.S_CNTR_CODE == startLocCnt.S_CNTR_CODE);
                    Task task27 = Task.Run(() =>
                    {
                        if (endLoc.S_AREA_CODE == Settings.Areas[2] && cginfo != null)
                        {
                            var target = new TN_InventoryM()
                            {
                                S_ID = cginfo.S_ID,
                                RFID = cginfo.S_CNTR_CODE,
                                SPEC = cginfo.S_SPE,
                                WEIGHT = cginfo.F_QTY,
                                ITEMSTATE = cginfo.S_ITEM_STATE,
                                ITEMCODE = cginfo.S_ITEM_CODE,
                                LOGINNAME = model.staff,
                                SHIFT = model.shift,
                            };
                            SpecHelper.InsertInventoryM(target);
                        }
                    });
                    Task task26 = Task.Run(() =>
                    {
                        if (startLoc.S_AREA_CODE == Settings.Areas[2] && cginfo != null)
                        {
                            SpecHelper.DeleteInventoryM(cginfo.S_ID);
                        }
                    });
 
                    result.resultCode = 0;
                    result.resultMsg =  "成功";
                    LogHelper.Info($"生成点对点失败,容器号{ startLocCnt.S_CNTR_CODE},起点{startLoc.S_CODE},终点{endLoc.S_CODE}");
                    return result;
                }
                else
                {
                    result.resultCode = 9;
                    result.resultMsg = $"生成点对点失败,容器号{ startLocCnt.S_CNTR_CODE},起点{startLoc.S_CODE},终点{endLoc.S_CODE}";
                    LogHelper.Info(result.resultMsg);
                    return result;
                }
 
            }
            catch (Exception ex)
            {
                result.resultCode = -1;
                result.resultMsg = $"人工创建点到点任务,发生了异常,{ex.Message}";
                LogHelper.Error(result.resultMsg, ex);
                return result;
            }
        }
    }
}