Administrator
2024-02-02 ec3e4e46da50f911b9d3eaef98b399962f3e4c70
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
using Model.ModelDto;
using Model.ModelVm;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using WMS.BLL.LogServer;
using WMS.Entity.Context;
using WMS.IBLL.IPdaServer;
using WMS.DAL;
using WMS.Entity.BllAsnEntity;
using WMS.Entity.DataEntity;
using WMS.Entity.SysEntity;
using WMS.Entity.LogEntity;
using WMS.Entity.BllSoEntity;
using Model.ModelVm.PdaVm;
using Model.ModelDto.PdaDto;
 
namespace WMS.BLL.BllPdaServer
{
    public class PdaAsnServer : IPdaAsnServer
    {
        private static readonly SqlSugarScope Db = DataContext.Db;
 
        // 获取单据列表
        public List<ArrivalNoticeDto> GetArrivalNotices(ArrivalNoticeVm model)
        {
            string sqlString = string.Empty;
            try
            {
                // 未关单的单据
                sqlString = $"select * from BllArrivalNotice where Type in ({model.Type}) and Status != '3' and Status != '4'  and IsDel='0' order by CreateTime;";
                var modelList = Db.Ado.SqlQuery<ArrivalNoticeDto>(sqlString);
 
                return modelList;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
        // 根据单据号获取单据明细列表
        public List<ArrivalNoticeDetailDto> GetArrivalNoticeDetails(ArrivalNoticeVm model)
        {
            string sqlString = string.Empty;
            try
            {
                sqlString = $"select * from BllArrivalNoticeDetail where ASNNo = '{model.ASNNo}' and isdel='0' order by CreateTime;";
                var modelList = Db.Ado.SqlQuery<ArrivalNoticeDetailDto>(sqlString);
 
                return modelList;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
        // 根据单据明细ID获取明细详情
        public ArrivalNoticeDetailDto GetArrivalNoticeDetail(ArrivalNoticeDetailVm model)
        {
            string sqlString = string.Empty;
            try
            {
                if (model.Id == null || model.Id == 0)
                {
                    throw new Exception("明细ID不可为空!");
                }
 
                sqlString = $"select * from BllArrivalNoticeDetail where Id = '{model.Id}' and isdel='0' order by CreateTime;";
                var modelList = Db.Ado.SqlQuery<ArrivalNoticeDetailDto>(sqlString);
 
                if (modelList.Count > 0)
                {
                    return modelList[0];
                }
 
                return new ArrivalNoticeDetailDto();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
        /// <summary>
        /// 验证托盘是否存在
        /// </summary>
        /// <param name="palletNo">托盘号</param>
        /// <returns>"":可使用 -1:不可使用(原因)</returns>
        public string IsEnablePalletNo(string palletNo)
        {
            string sqlMsg = "";
            string sqlString = string.Empty;
            try
            {
                sqlString = $"select * from SysPallets where PalletNo = '{palletNo}' and isdel = '0';";
                var models = Db.Ado.SqlQuery<PalletBindVm>(sqlString);
 
                if (models.Count > 1)
                {
                    sqlMsg = "-1:存在重复托盘号,请检查!";
                    return sqlMsg;
                }
                if (models.Count > 0)
                {
                    if (models[0].Status == "1")
                    {
                        sqlString = $"select count(id) from DataStockDetail where PalletNo = '{palletNo}' and isnull(LocatNo,'') != '' and isdel = '0';";
                        int rowNum = Db.Ado.GetInt(sqlString);
                        if (rowNum > 0)
                        {
                            sqlMsg = "-1:托盘使用中,此托盘应在库内请核实!";
                        }
                    }
                }
                else
                {
                    sqlMsg = "-1:托盘号不存在!";
                }
 
                return sqlMsg;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
        // 根据箱码或托盘号获取箱支信息   liudl 
        public List<BoxInfoDto> GetBoxInfos(BoxInfoVm model)
        {
            try
            {
                string sqlString = string.Empty;
                if (model.IsHuiKu == 0)
                {
                    sqlString = "select BoxNo, SkuNo,SkuName, LotNo, SUM(Qty) as Qty from BllBoxInfo where IsDel = '0' ";
                    if (!string.IsNullOrEmpty(model.PalletNo))
                    {
                        sqlString += $"and PalletNo = '{model.PalletNo}' and Status in ('0','1') ";
                    }
                    if (!string.IsNullOrEmpty(model.BoxNo))
                    {
                        sqlString += $"and BoxNo = '{model.BoxNo}' and Status in ('0','1') ";
                    }
                    sqlString += $"group by BoxNo,SkuNo,SkuName,LotNo; ";
                }
                else
                {
                    sqlString = "select BoxNo, SkuNo,SkuName, LotNo, SUM(Qty) as Qty from DataBoxInfo where IsDel = '0' ";
                    if (!string.IsNullOrEmpty(model.PalletNo))
                    {
                        sqlString += $"and PalletNo = '{model.PalletNo}' ";
                    }
                    sqlString += $"group by BoxNo,SkuNo,SkuName,LotNo; ";
                }
                var models = Db.Ado.SqlQuery<BoxInfoDto>(sqlString);
 
                return models;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
        // 绑定空托盘
        public string BindNullPallet(PalletBindVm model)
        {
            string strMsg = "";
            try
            {
                var datetime = Db.GetDate();
 
                //获取托盘绑定信息
                string str = "select * from BllPalletBind where IsDel = '0' and PalletNo = @palletno and Status = '0' ";
                List<PalletBindVm> list = Db.Ado.SqlQuery<PalletBindVm>(str, new
                {
                    palletno = model.PalletNo //托盘号
                });
                //判断是否已绑定该托盘
                if (list.Count > 0)
                {
                    strMsg = "-1:该托盘已被绑定!";
                    return strMsg;
                }
                if (string.IsNullOrEmpty(model.PalletNo))
                {
                    strMsg = "-1:托盘号不可为空!";
                    return strMsg;
                }
                if (model.Qty == null || model.Qty == 0)
                {
                    strMsg = "-1:空托盘数量不可为空!";
                    return strMsg;
                }
                //获取托盘信息
                var pallet = Db.Queryable<SysPallets>().First(a => a.IsDel == "0" && a.PalletNo == model.PalletNo);
 
                if (pallet == null)
                {
                    strMsg = "-1:托盘信息为空!";
                    return strMsg;
                }
                if (pallet.Status != "0")
                {
                    strMsg = "-1:该托盘正在使用!";
                    return strMsg;
                }
 
                Db.BeginTran();
 
                //var taskNo = new Common().GetMaxNo("TK");
                //var exTask = new LogTask    //入库任务
                //{
                //    TaskNo = taskNo,
                //    Sender = "WMS",
                //    Receiver = "",
                //    IsSuccess = 1, //是否下发成功 0失败 1成功
                //    SendDate = DateTime.Now,  //发送时间
                //    BackDate = DateTime.Now,  //返回时间
                //    StartLocat = "",//起始位置
                //    EndLocat = "",//目标位置
                //    PalletNo = model.PalletNo,//托盘码
                //    IsSend = 1,//是否可再次下发
                //    IsCancel = 1,//是否可取消
                //    IsFinish = 1,//是否可完成
                //    Type = "0",//任务类型 0 入库任务 1 出库任务  2 移库任务
                //    Status = "0",//任务状态0:等待执行1正在执行2执行完成
                //    OrderType = "0",//0 入库单 1 出库单  2 盘点单  3 移库单
                //    Msg = "母托盘的入库任务",
                //};
                //Db.Insertable(exTask).ExecuteCommand();
 
                // 插入托盘绑定表
                var modelpb = new BllPalletBind
                {
                    ASNNo = "",
                    ASNDetailNo = 0,
                    TaskNo = "", //任务号
                    PalletNo = model.PalletNo,
                    PalletNo2 = model.PalletNo2,
                    PalletNo3 = model.PalletNo3,
                    Qty = (int)model.Qty,
                    FullQty = model.FullQty,
                    Status = "0", //等待执行
                    Type = "1", //0 物料托 1 空托
                    LotNo = "",
                    LotText = "",
                    SupplierLot = "",
                    InspectMark = "0", //0 否 1 是
                    BitPalletMark = "0",
                    IsBale = "0",
                    IsBelt = "0",
                    CreateUser = (int)model.CreateUser,
                    CreateTime = Db.GetDate()
                };
                var id = Db.Insertable(modelpb).ExecuteReturnIdentity();
 
                var modelbb = new BllBoxInfo
                {
                    ASNNo = "",
                    ASNDetailNo = null,
                    BindNo = id,
                    PalletNo = model.PalletNo,
                    PalletNo2 = model.PalletNo2,
                    PalletNo3 = model.PalletNo3,
                    Status = "1",
                    CompleteTime = DateTime.Now,
                    Qty = (int)model.Qty,
                    FullQty = null,
                    SkuNo = "100099",
                    SkuName = "托盘",
                    LotNo = "",
                    LotText = "",
                    SupplierLot = "",
                    InspectStatus = "1",
                    Origin = "PDA",
                    BoxNo = "",
                    BoxNo2 = "",
                    BoxNo3 = "",
                    InspectMark = "",
                    BitBoxMark = "0",
 
                    CreateUser = (int)model.CreateUser,
                    CreateTime = datetime
                };
                Db.Insertable(modelbb).ExecuteCommand();
 
 
                // 更改托盘使用状态
                string sqlStr = string.Empty;
                sqlStr = $"update SysPallets set Status = '1' where PalletNo = '{model.PalletNo}';";
                Db.Ado.ExecuteCommand(sqlStr);
                Db.CommitTran();
 
                // 插入操作日志
                new OperationASNServer().AddLogOperationAsn("PDA模块", "空托入库", model.PalletNo, "添加", $"在PDA上添加了空托盘跺", (int)model.CreateUser);
 
                return strMsg;
            }
            catch (Exception ex)
            {
                Db.Ado.RollbackTran();
                throw ex;
            }
        }
 
        /// <summary>
        /// 解绑空托盘
        /// </summary>
        /// <param name="upBindPalletNo">解绑托盘号</param>
        /// <param name="createUser">创建人</param>
        /// <returns></returns>
        public string UnBindNullPallet(string upBindPalletNo, int createUser)
        {
            string strMsg = "";
            //捕获异常
            try
            {
                //获取任务信息
                var logtask = Db.Queryable<LogTask>().First(a => a.PalletNo == upBindPalletNo && a.IsDel == "0" && a.Status == "1");
                if (logtask == null)
                {
                    strMsg = "该托盘任务已完成 或未成功创建任务 请核实!";
                    return strMsg;
                }
                string bindstr = "select * from BllPalletBind Where IsDel = @isdel and PalletNo = @palletno and ASNDetailNo = 0 and ASNNo = ''";
                //获取绑定托盘表信息
                List<PalletBindVm> bindVms = Db.Ado.SqlQuery<PalletBindVm>(bindstr, new
                {
                    isdel = "0", //是否删除
                    palletno = upBindPalletNo //托盘号
                });
                //获取箱码信息
                var box = Db.Queryable<BllBoxInfo>().First(a => a.PalletNo == upBindPalletNo && a.IsDel == "0" && a.Status != "2" && a.BindNo == bindVms[0].Id);
                //判断空托入库的托盘是否有该托盘
                if (bindVms.Count != 1)
                {
                    strMsg = "该托盘不是空托托盘 或未绑定 请核实托盘";
                    return strMsg;
                }
                //删除绑定托盘表信息
                string delstr = "delete from BllPalletBind Where PalletNo = @palletno";
                int i = Db.Ado.ExecuteCommand(delstr, new
                {
                    palletno = upBindPalletNo
                });
                //修改托盘状态
                Db.BeginTran();
                // 插入解绑托盘表
                BllPalletUnbind modelpb = new BllPalletUnbind
                {
 
                    UpbindPalletNo = upBindPalletNo,
                    PalletNo2 = bindVms[0].PalletNo2,
                    PalletNo3 = bindVms[0].PalletNo3,
                    Qty = (int)bindVms[0].Qty,
                    LotNo = bindVms[0].LotNo,
                    LotText = bindVms[0].LotText,
                    SupplierLot = bindVms[0].SupplierLot,
                    CreateUser = createUser,
                    CreateTime = Db.GetDate()
                };
                Db.Insertable(modelpb).ExecuteCommand();
 
                //修改箱码信息
                box.Status = "3";
                box.UpdateTime = DateTime.Now;
                box.UpdateUser = createUser;
                box.IsDel = "1";
                Db.Updateable(box).ExecuteCommand();
 
                //修改任务状态
                logtask.IsDel = "1";
                logtask.Status = "4"; //3 已取消
                logtask.UpdateUser = createUser; //取消人
                logtask.UpdateTime = DateTime.Now; //取消时间
                Db.Updateable(logtask).ExecuteCommand();
                //更改库存数量
                //string str = $"update DataStock set Qty = Qty - {(int)bindVms[0].Qty} Where SkuNo = '100099'";
                // 更改托盘使用状态
                string sqlStr = string.Empty;
                sqlStr = $"update SysPallets set Status = '0' where PalletNo = '{upBindPalletNo}';";
                Db.Ado.ExecuteCommand(sqlStr);
                Db.CommitTran();
 
                // 插入操作日志
                new OperationASNServer().AddLogOperationAsn("PDA模块", "空托入库", upBindPalletNo, "添加", $"在PDA上解绑了空托盘跺", createUser);
 
                return strMsg;
            }
            catch (Exception ex)
            {
                //抛出异常
                Db.Ado.RollbackTran();
                throw ex;
            }
        }
 
        // 删除已组信息
        public string DelBoxInfo(BoxInfoVm model)
        {
            string strMsg = "";
            string sqlString = string.Empty;
            try
            {
                if (model.Id == null || model.Id == 0)
                {
                    strMsg = "-1:箱支ID不可为空!";
                    return strMsg;
                }
 
                // 获取箱支信息和托盘状态(已组未入库的可删除;入库成功又出库的不可删除)
                sqlString = $"select * from BllBoxInfo where id = '{model.Id}' and Status='1' and isdel = '0';";
                var boxModel = Db.Ado.SqlQuery<BoxInfoDto>(sqlString);
                if (boxModel.Count <= 0)
                {
                    strMsg = "-1:箱支信息状态已变更或已存在库存信息,请检查!";
                }
 
                // 查询托盘上其他物料数量
                sqlString = string.Empty;
                sqlString += $"select count(id) from BllBoxInfo where PalletNo = '{boxModel[0].PalletNo}' and isdel = '0' and id != '{model.Id}';";
                int rowNum = Db.Ado.GetInt(sqlString);
 
                // 查询入库单明细是否有其他托盘绑定信息
                sqlString = string.Empty;
                sqlString += $"select count(id) from BllBoxInfo where ASNDetailNo = '{boxModel[0].ASNDetailNo}' and isdel = '0' and id != '{model.Id}';";
                int asnDetailNum = Db.Ado.GetInt(sqlString);
 
                // 查询入库单明细是否有其他托盘绑定信息
                sqlString = string.Empty;
                sqlString += $"select count(id) from BllBoxInfo where ASNNo = '{boxModel[0].ASNNo}' and isdel = '0' and id != '{model.Id}';";
                int asnNum = Db.Ado.GetInt(sqlString);
 
                Db.BeginTran();
                // 解除箱支和托盘的关联
                sqlString = string.Empty;
                sqlString = $"Update BllBoxInfo set ASNNo = '',ASNDetailNo='',BindNo = '',PalletNo='' ";
                sqlString += $",UpdateTime=GETDATE(),UpdateUser='{model.CreateUser} where id = '{model.Id}';";
 
                // 判断托盘上是否还有物料 无:删除绑定的托盘信息并更改托盘状态
                if (rowNum <= 0)
                {
                    // 删除托盘绑定信息
                    sqlString = string.Empty;
                    sqlString = $"Update BllPalletBind set IsDel = '1' where id = '{model.Id}';";
 
 
                    // 判断入库单明细是否存在其他已组托盘的
                    if (asnDetailNum <= 0)
                    {
                        // 更改入库单明细状态
                        sqlString = string.Empty;
                        sqlString += $"update BllArrivalNoticeDetail set FactQty = FactQty - '{boxModel[0].Qty}',Status = '0', ";
                        sqlString += $"CompleteTime=GETDATE() where id = '{boxModel[0].ASNDetailNo}';";
                        Db.Ado.ExecuteCommand(sqlString);
 
                        // 判断入库单是否有其他组托信息
                        if (asnNum <= 0)
                        {
                            // 更改入库单状态
                            sqlString = string.Empty;
                            sqlString += $"update BllArrivalNotice set Status = '0',CompleteTime=GETDATE() where ASNNo = '{boxModel[0].ASNNo}';";
                            Db.Ado.ExecuteCommand(sqlString);
                        }
                    }
 
                    // 更改托盘状态
                    sqlString = string.Empty;
                    sqlString = $"update SysPallets set Status = '0' where PalletNo = '{boxModel[0].PalletNo}';";
                    Db.Ado.ExecuteCommand(sqlString);
                }
 
                Db.CommitTran();
                return strMsg;
            }
            catch (Exception ex)
            {
                Db.RollbackTran();
                throw ex;
            }
        }
 
        // 平库完成入库操作
        public string CompleteInStock(PalletBindVm model)
        {
            string strMsg = "";
            string sqlString = string.Empty;
            try
            {
                ///平库入库业务
                ///1:判断一系列条件
                ///1.1:判断托盘状态 是否为使用中
                ///1.2:判断组托、箱码信息 是否已入库
                ///1.3:判断单据是否关单 已关单无法入库 请重新创建其它单据
                ///1.4:判断任务信息是否为已完成(先判断 等确定组托后是否创建任务再决定是否注释)
                ///2:更改库存
                ///2.1:更改前判断库存是否存在该物料 如存在 直接增加数量 不存在 新增库存信息 增加库存明细信息
                ///3:更改单据
                ///3.1:更改单据时判断单据状态是否为已完成 如已完成 则只增加完成数量
                ///3.1.1:如正在执行 则判断完成数量加入库数量是否等于或大于入库单数量
                ///3.2:如正在执行或等待执行 则判断完成数量加入库数量是否等于或大于入库单数量
                ///3.2.1:不大于或等于 则变更状态为正在执行
                ///3.2.2:如大于或等于 则变更状态为已完成
                ///4:更改组托信息和箱码信息
                ///4.1:组托信息更改为已入库
                ///4.2:更改任务信息
 
                //获取当前时间
                DateTime serverTime = Db.Ado.GetDateTime("select GETDATE();");
                #region 判断
 
                #region 是否回流入库
                int iscount = 0;
                //库存明细信息
                var stockDetail = Db.Queryable<DataStockDetail>().Where(a => a.IsDel == "0" && a.PalletNo == model.PalletNo).ToList();
                //验证库存是否拥有该托信息
                if (stockDetail != null && stockDetail.Count > 0)
                {
                    foreach (var item in stockDetail)
                    {
                        if (!string.IsNullOrEmpty(item.WareHouseNo))
                        {
                            strMsg = "-1:该托盘未在库外,请核查!";
                            return strMsg;
                        }
                    }
                    iscount = 1; //回流入库
                }
                #endregion
 
                #region 地码信息(储位信息)
                var storageLocat = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.LocatNo == model.LocatNo && w.Status == "0");
                if (storageLocat == null)
                {
                    strMsg = "-1:储位信息不存在或非空闲状态,请核查!";
                    return strMsg;
                }
                #endregion
 
                #region 组托信息
                // 验证组托信息
                var palletbind = Db.Queryable<BllPalletBind>().Where(p => p.IsDel == "0" && p.ASNNo == model.ASNNo && p.PalletNo == model.PalletNo && p.Status != "2").ToList();
 
                if (iscount == 0)
                {
                    //验证组托信息是否为空
                    if (palletbind == null || palletbind.Count <= 0)
                    {
                        strMsg = "-1:组托信息为空,请核查!";
                        return strMsg;
                    }
                }
                #endregion
 
                #region 箱码信息
                //获取箱码信息
                var boxinfo = new List<BllBoxInfo>();
                if (iscount == 0)
                {
                    string boxstr = $"select * from BLLBoxInfo where IsDel = '0' and ASNNo = '{model.ASNNo}'";
                    boxinfo = Db.Ado.SqlQuery<BllBoxInfo>(boxstr).ToList();
 
                    //验证箱码信息是否存在
                    if (boxinfo.Count <= 0)
                    {
                        strMsg = "-1:箱码信息不存在,请核查!";
                        return strMsg;
                    }
                }
                #endregion
 
                #region 单据信息
                //获取入库单据总单
                var arrival = new BllArrivalNotice();
                if (iscount == 0)
                {
                    arrival = Db.Queryable<BllArrivalNotice>().First(a => a.IsDel == "0" && a.Status != "3" && a.ASNNo == model.ASNNo);
 
                    //验证入库单总单是否关闭
                    if (arrival == null)
                    {
                        strMsg = "-1:入库单总单为空,请核查!";
                        return strMsg;
                    }
                    //判断入库单总单是否为已关闭
                    if (arrival.Status == "3")
                    {
                        strMsg = "-1:入库单总单已关闭,请核查!";
                        return strMsg;
                    }
                }
                //获取入库单明细信息
                var arrivalnotice = new List<BllArrivalNoticeDetail>();
                if (iscount == 0)
                {
                    arrivalnotice = Db.Queryable<BllArrivalNoticeDetail>().Where(d => d.IsDel == "0" && d.ASNNo == arrival.ASNNo && d.Status == "1").ToList();
                    //验证入库单明细是否存在
                    if (arrivalnotice == null || arrivalnotice.Count <= 0)
                    {
                        strMsg = "-1:入库单明细为空,请核查!";
                        return strMsg;
                    }
                }
                #endregion
 
                #endregion
 
                Db.BeginTran();//开启事务
                //判断是否回流入库
                if (iscount == 0)
                {
                    #region 正常入库
                    bool isFinsh = true;//入库单明细是否全部完成
                    //遍历入库单详情
                    foreach (var noticeItem in arrivalnotice)
                    {
                        #region 物料信息
                        var sku = Db.Queryable<SysMaterials>().First(s => s.SkuNo == noticeItem.SkuNo && s.IsDel == "0" && s.SkuName == noticeItem.SkuName);
                        //判断物料信息是否为空
                        if (sku == null)
                        {
                            strMsg = "-1:物料信息为空,请核查!";
                            return strMsg;
                        }
                        #endregion
                        var pallQty = 0;//托盘上数量
                        var boxinfo2 = boxinfo.Where(w => w.SkuNo == noticeItem.SkuNo && w.LotNo == noticeItem.LotNo && w.PalletNo == model.PalletNo).ToList();
                        if (boxinfo2.Count <= 0)
                        {
                            strMsg = "-1:箱码信息不存在,请核查!";
                            return strMsg;
                        }
                        else
                        {
                            // 改变箱支关系表状态:已入库
                            sqlString = $"Update BllBoxInfo set Status = '2' where PalletNo = '{model.PalletNo}' and ASNNo = '{model.ASNNo}' and Status = '1'";
                            Db.Ado.ExecuteCommand(sqlString);
 
                            pallQty = boxinfo2.Sum(w => w.Qty);
                            //入库单明细完成数量增加
                            noticeItem.CompleteQty += pallQty;
                        }
                        //判断入库数量增加后是否大于等于单据数量
                        if (noticeItem.CompleteQty >= noticeItem.Qty)
                        {
                            noticeItem.Status = "2";
                            noticeItem.CompleteTime = serverTime;
                        }
                        else
                        {
                            noticeItem.Status = "1";
 
                            isFinsh = false;
                        }
                        noticeItem.UpdateUser = model.CreateUser;
                        noticeItem.UpdateTime = serverTime;
                        Db.Updateable(arrivalnotice).ExecuteCommand();
 
                        #region 库存总表
                        // 判断当前物料库存表是否已存在
                        string str = $"select * from DataStock where SkuNo = '{noticeItem.SkuNo}' and LotNo = '{noticeItem.LotNo}' and Standard = '{noticeItem.Standard}' and SkuName = '{noticeItem.SkuName}' and IsDel = '0'";
                        var stockModel = Db.Ado.SqlQuerySingle<DataStock>(str);
 
                        if (stockModel == null || stockModel.Id == 0)
                        {
                            stockModel = new DataStock
                            {
                                LotNo = noticeItem.LotNo,
                                LotText = noticeItem.LotText,
                                SupplierLot = noticeItem.SupplierLot,
                                SkuNo = noticeItem.SkuNo,
                                SkuName = noticeItem.SkuName,
                                Qty = pallQty,
                                Standard = noticeItem.Standard,
                                CreateUser = (int)model.CreateUser,
                                CreateTime = serverTime
                            };
                            Db.Insertable<DataStock>(stockModel).ExecuteCommand();
                        }
                        else
                        {
                            sqlString = $"update DataStock set Qty = Qty + {pallQty}," +
                                $"UpdateTime = GETDATE(),UpdateUser='{model.CreateUser}' where id = '{stockModel.Id}';";
                            Db.Ado.ExecuteCommand(sqlString);
                        }
                        #endregion
 
                        #region 库存明细表
                        var palletbindInfo = palletbind.First(w => w.ASNDetailNo == noticeItem.Id);
                        // 判断当前托盘是否已存在(拣货回库托盘)
                        var detailModel = Db.Queryable<DataStockDetail>()
                            .First(it => it.PalletNo == palletbindInfo.PalletNo && it.LotNo == noticeItem.LotNo && it.SkuNo == noticeItem.SkuNo
                             && it.Standard == noticeItem.Standard && it.IsDel == "0");
                        var stId = 0;
                        if (detailModel == null || detailModel.Id == 0)
                        {
                            // 获取可抽检数量
                            sqlString = "select sum(qty) from BllBoxInfo where isdel = 0 and ";
                            sqlString += $"BindNo = '{palletbindInfo.Id}'";
                            int inspectQty = Db.Ado.GetInt(sqlString);
 
                            // 添加库存明细表
                            detailModel = new DataStockDetail()
                            {
                                LotNo = noticeItem.LotNo,
                                LotText = noticeItem.LotText,
                                SupplierLot = noticeItem.SupplierLot,
                                SkuNo = noticeItem.SkuNo,
                                SkuName = noticeItem.SkuName,
                                Standard = noticeItem.Standard,
                                Qty = pallQty,
                                LockQty = 0,
                                FrozenQty = 0,
                                InspectQty = inspectQty,
                                ASNNo = noticeItem.ASNNo,
                                ASNDetailNo = (int)palletbindInfo.ASNDetailNo,
                                WareHouseNo = "W02",
                                RoadwayNo = "",
                                AreaNo = "",
                                LocatNo = model.LocatNo,
                                PalletNo = model.PalletNo,
                                PalletNo2 = palletbindInfo.PalletNo2,
                                PalletNo3 = palletbindInfo.PalletNo3,
                                CompleteTime = serverTime,
                                ProductionTime = palletbindInfo.ProductionTime,
                                ExpirationTime = palletbindInfo.ExpirationTime,
                                Status = "0",
                                InspectMark = palletbindInfo.InspectMark,
                                BitPalletMark = palletbindInfo.BitPalletMark,
                                InspectStatus = sku.IsInspect,       // 组盘的时候就要默认设定好是否合格
                                PackagNo = sku.PackagNo,
                                IsBale = palletbindInfo.IsBale,
                                IsBelt = palletbindInfo.IsBelt,
                                CreateUser = (int)model.CreateUser,
                                CreateTime = serverTime
                            };
 
                            stId = Db.Insertable<DataStockDetail>(detailModel).ExecuteReturnIdentity();
 
                        }
                        else
                        {
                            stId = detailModel.Id;
                            detailModel.LocatNo = model.LocatNo;
                            detailModel.UpdateTime = serverTime;
                            detailModel.UpdateUser = (int)model.CreateUser;
                            // 变更储位地址
                            Db.Updateable<DataStockDetail>(detailModel).UpdateColumns(it => new { it.LocatNo, it.UpdateTime, it.UpdateUser }).ExecuteCommand();
                        }
                        #endregion
 
                        #region 任务及组托信息
                        //创建任务信息
                        var taskNo = new Common().GetMaxNo("TK");
                        var exTask = new LogTask    //入库任务
                        {
                            TaskNo = taskNo,
                            Sender = "WMS",
                            Receiver = "PDA",
                            IsSuccess = 1, //是否下发成功 0失败 1成功
                            SendDate = DateTime.Now,  //发送时间
                            BackDate = DateTime.Now,  //返回时间
                            StartLocat = "",//起始位置
                            EndLocat = "零箱库",//目标位置
                            PalletNo = palletbindInfo.PalletNo,//托盘码
                            IsSend = 0,//是否可再次下发
                            IsCancel = 0,//是否可取消
                            IsFinish = 0,//是否可完成
                            Type = "0",//任务类型 0 入库任务 1 出库任务  2 移库任务
                            Status = "2",//任务状态0:等待执行1正在执行2执行完成
                            OrderType = "0",//0 入库单 1 出库单  2 盘点单  3 移库单
                            Msg = "零箱库的入库任务",
                        };
                        Db.Insertable(exTask).ExecuteCommand();
 
                        //修改组托状态
                        palletbindInfo.Status = "2"; //2 入库完成
                        palletbindInfo.UpdateTime = serverTime;
                        palletbindInfo.UpdateUser = model.CreateUser;
                        palletbindInfo.CompleteTime = serverTime; //完成时间
                        palletbindInfo.TaskNo = taskNo; //任务号
                        Db.Updateable(palletbind).ExecuteCommand();
                        #endregion
 
                        #region 库存箱支明细表
                        // 插入新组的箱支信息
                        var comTime = DateTime.Now;
                        foreach (var item in boxinfo2)
                        {
                            var info = new DataBoxInfo()
                            {
                                StockDetailId = stId,
                                BindNo = palletbindInfo.Id,
                                BoxNo = item.BoxNo,
                                BoxNo2 = item.BoxNo2,
                                BoxNo3 = item.BoxNo3,
                                PalletNo = item.PalletNo,
                                PalletNo2 = item.PalletNo2,
                                PalletNo3 = item.PalletNo3,
                                Qty = item.Qty,
                                FullQty = item.FullQty,
                                Status = "2",
                                LotNo = item.LotNo,
                                LotText = item.LotText,
                                SkuNo = item.SkuNo,
                                SkuName = item.SkuName,
                                Standard = sku.Standard,
                                ProductionTime = item.ProductionTime,
                                SupplierLot = item.SupplierLot,
                                InspectMark = item.InspectMark,
                                BitBoxMark = item.BitBoxMark,
                                InspectStatus = item.InspectStatus,
                                //InspectTime = item.,
 
                                IsDel = "0",
                                CreateUser = 0,
                                CreateTime = comTime
                            };
                            Db.Insertable(info).ExecuteCommand();
                        }
 
 
                        //sqlString = $"insert into DataBoxInfo select a.Id StockDetailId,b.BindNo,b.BoxNo,b.BoxNo2,b.BoxNo3,b.PalletNo,b.PalletNo2,b.PalletNo3,b.Qty,b.FullQty,b.Status,b.LotNo,b.LotText,b.SkuNo,b.SkuName,a.Standard,a.ProductionTime,a.SupplierLot,a.InspectMark,b.BitBoxMark,a.InspectStatus,a.InspectTime,'0' IsDel,GETDATE() CreateTime,{model.CreateUser} CreateUser,null UpdateTime,null UpdateUser from DataStockDetail a left join BllBoxInfo b on a.SkuNo=b.skuno where b.isdel = '0' and b.Status='2' and b.BindNo = '{palletbind.Id}'   and b.PalletNo = '{model.PalletNo}'";
 
                        //Db.Ado.ExecuteCommand(sqlString);
 
                        //sqlString = $"insert into DataBoxInfo select stock.Id StockDetailId,box.BindNo,box.BoxNo,box.BoxNo2,box.BoxNo3,box.PalletNo,box.PalletNo2,box.PalletNo3,box.Qty,box.FullQty,box.Status,box.LotNo,box.LotText,box.SkuNo,box.SkuName,stock.Standard,stock.ProductionTime,stock.SupplierLot,stock.InspectMark,box.BitBoxMark,stock.InspectStatus,stock.InspectTime,'0' IsDel,GETDATE() CreateTime,{model.CreateUser} CreateUser,null UpdateTime,null UpdateUser from DataStockDetail stock left join BllBoxInfo box on stock.PalletNo = box.PalletNo where box.isdel = '0' and box.Status='2' and box.BindNo = '{palletbind.Id}' and stock.WareHouseNo = 'W02' and box.PalletNo = '{model.PalletNo}'";
                        //sqlString = $"select stock.Id StockDetailId,box.BindNo,box.BoxNo,box.BoxNo2,box.BoxNo3,box.PalletNo,box.PalletNo2,box.PalletNo3,box.Qty,box.FullQty,box.Status,box.LotNo,box.LotText,box.SkuNo,box.SkuName,stock.Standard,stock.ProductionTime,stock.SupplierLot,stock.InspectMark,box.BitBoxMark,stock.InspectStatus,stock.InspectTime,'0' IsDel,GETDATE() CreateTime,{model.CreateUser} CreateUser from DataStockDetail stock left join BllBoxInfo box on stock.PalletNo = box.PalletNo where box.isdel = '0' and box.Status='2' and box.BindNo = '{palletbind.Id}' and stock.WareHouseNo = 'W02' and box.PalletNo = '{model.PalletNo}'";
                        ////and a.LocatNo = '{model.LocatNo},null UpdateTime,null UpdateUser'
 
                        //var databox = Db.Ado.SqlQuery<DataBoxInfo>(sqlString);
 
                        //foreach (var item in databox)
                        //{
                        //    var boxdata = Db.Queryable<DataBoxInfo>().First(b => b.BoxNo == item.BoxNo);
 
                        //    if (boxdata == null)
                        //    {
                        //        Db.Insertable(item).ExecuteCommand();
                        //    }
                        //    else
                        //    {
                        //        boxdata.Qty += item.Qty;
                        //        Db.Updateable(boxdata).ExecuteCommand();
                        //    }
                        //}
 
                        //sqlString = $"insert into DataBoxInfo ";
                        //sqlString += "select BindNo,BoxNo,BoxNo2,BoxNo3,PalletNo,PalletNo2,PalletNo3, ";
                        //sqlString += "Qty,FullQty,'0' as Status,LotNo,LotText,ProductionTime,SupplierLot,";
                        //sqlString += "InspectMark,BitBoxMark,InspectStatus ";
                        //sqlString += $"from BllBoxInfo where isdel = 0 and Status='1' and BindNo = '{bindModel.Id}';";
 
                        #endregion
                    }
                    if (isFinsh)//入库单明细全部完成
                    {
                        //修改入库单总单信息
                        arrival.Status = "2";
                        arrival.UpdateUser = model.CreateUser;
                        arrival.UpdateTime = serverTime;
                        Db.Updateable(arrival).ExecuteCommand();
                    }
                    else
                    {
                        //判断总单状态是否为正在执行
                        if (arrival.Status == "0")
                        {
                            //修改入库单总单信息
                            arrival.Status = "1";
                            arrival.UpdateUser = model.CreateUser;
                            arrival.UpdateTime = serverTime;
                            Db.Updateable(arrival).ExecuteCommand();
 
                        }
                    }
                    #endregion
                }
                if (iscount == 1)
                {
                    //回流入库
                    #region 回流入库
 
                    //不修改库存 入库单据不做更改
                    //修改库存明细信息
                    //修改拣货信息为已完成
 
                    //修改库存明细信息
                    foreach (var item in stockDetail)
                    {
                        item.Status = "0"; //状态更改为待分配
                        item.WareHouseNo = "W02"; //所属仓库
                        item.UpdateTime = serverTime; //修改时间
                        item.UpdateUser = model.CreateUser; //修改人
                        //修改库存明细信息                                           
                        Db.Updateable(item).ExecuteCommand();
                    }
                    //获取拣货信息
                    var alotr = Db.Queryable<BllExportAllot>().First(a => a.IsDel == "0" && a.PalletNo == model.PalletNo && a.Status == "4");
                    if (alotr != null)
                    {
                        alotr.Status = "5"; //5 已完成
                        alotr.UpdateUser = model.CreateUser; //修改人
                        alotr.UpdateTime = serverTime; //修改时间
                                                       //修改拣货信息
                        Db.Updateable(alotr).ExecuteCommand();
                    }
                    #region 任务及组托信息
                    //创建任务信息
                    var taskNo = new Common().GetMaxNo("TK");
                    var exTask = new LogTask    //入库任务
                    {
                        TaskNo = taskNo,
                        Sender = "WMS",
                        Receiver = "PDA",
                        IsSuccess = 1, //是否下发成功 0失败 1成功
                        SendDate = DateTime.Now,  //发送时间
                        BackDate = DateTime.Now,  //返回时间
                        StartLocat = "",//起始位置
                        EndLocat = "零箱库",//目标位置
                        PalletNo = model.PalletNo,//托盘码
                        IsSend = 0,//是否可再次下发
                        IsCancel = 0,//是否可取消
                        IsFinish = 0,//是否可完成
                        Type = "0",//任务类型 0 入库任务 1 出库任务  2 移库任务
                        Status = "2",//任务状态0:等待执行1正在执行2执行完成
                        OrderType = "0",//0 入库单 1 出库单  2 盘点单  3 移库单
                        Msg = "零箱库的回库任务",
                    };
                    Db.Insertable(exTask).ExecuteCommand();
 
                    //修改组托状态
                    //palletbind.Status = "2"; //2 入库完成
                    //palletbind.UpdateTime = serverTime;
                    //palletbind.UpdateUser = model.CreateUser;
                    //palletbind.CompleteTime = serverTime; //完成时间
                    //palletbind.TaskNo = taskNo; //任务号
                    //Db.Updateable(palletbind).ExecuteCommand();
 
                    #endregion
 
                    #endregion
                }
 
                new OperationASNServer().AddLogOperationAsn("PDA模块", "平库入库", model.PalletNo, "完成", $"在PDA上完成单据号为:{model.ASNNo}的托盘码为:{model.PalletNo}的平库入库操作", (int)model.CreateUser);
 
                Db.CommitTran();
 
                #region 平库入库注释代码
                // 改变储位状态; 有货物
                //sqlString = $"Update SysStorageLocat set Status = '1' where LocatNo = '{model.LocatNo}' and WareHouseNo = 'W01';";
                //Db.Ado.ExecuteCommand(sqlString);
 
                //// 改变箱支关系表状态:已入库
                //sqlString = $"Update BllBoxInfo set Status = '2' where PalletNo = '{model.PalletNo}' and ASNNo = '{model.ASNNo}' and Status = '1';";
                //Db.Ado.ExecuteCommand(sqlString);
 
                //// 改变托盘绑定表状态:已入库 完成时间:当前时间 
                //sqlString = $"Update BllPalletBind set LocatNo = '{model.LocatNo}',Status = '2',CompleteTime=GETDATE() where PalletNo = '{model.PalletNo}' and ASNNo = '{model.ASNNo}';";
                //Db.Ado.ExecuteCommand(sqlString);
 
                // 改变入库明细表状态:若完成数量和数量相等:执行完成 不相等:正在执行;
                //foreach (PalletBindVm bindModel in bindModels)
                //{
                //    // 判断入库明细是否完成
                //    sqlString = $"select count(id) from BllArrivalNoticeDetail where isdel = '0' ";
                //    sqlString += $"and  id = '{bindModel.ASNDetailNo}';";/*Qty = CompleteQty + {bindModel.Qty} and*/
                //    int detailNum = Db.Ado.GetInt(sqlString);
                //    if (detailNum > 0)
                //    {
                //        // 改变入库单明细状态和完成时间
                //        sqlString = $"Update BllArrivalNoticeDetail set Status = '2',CompleteTime=GETDATE() ";
                //        sqlString += $"where id = '{bindModel.ASNDetailNo}';";
                //        Db.Ado.ExecuteCommand(sqlString);
 
                //        // 判断入库单总表是否完成
                //        sqlString = $"select count(id) from BllArrivalNoticeDetail where isdel = '0' and Status != '2' ";
                //        sqlString += $"and ASNNo = '{bindModel.ASNNo}' and id !='{bindModel.ASNDetailNo}';";
                //        int asnNum = Db.Ado.GetInt(sqlString);
                //        if (detailNum <= 0)
                //        {
                //            // 改变入库单状态和完成时间   若入库明细表所有状态为执行完毕:执行完毕 否:正在执行;
                //            sqlString = $"Update BllArrivalNotice set  CompleteQty = CompleteQty+{bindModel.Qty},Status = '2',CompleteTime=GETDATE() ";
                //            sqlString += $"where ASNNo = '{bindModel.ASNNo}';";
                //            Db.Ado.ExecuteCommand(sqlString);
                //        }
                //    }
 
                //    #region 库存总表
 
                //    // var stockModel = Db.Queryable<DataStock>()
                //    //.First(it => it.SkuNo == bindModel.SkuNo && it.LotNo == bindModel.LotNo
                //    // && it.Standard == bindModel.Standard && it.SupplierLot == bindModel.SupplierLot && it.IsDel == "0");
 
                //    //获取当前入库物料信息
                //    string str = string.Empty;
                //    str += $"select * from BllArrivalNoticeDetail where id in (select ASNDetailNo from BllBoxInfo where ASNNo = '{bindModel.ASNNo}' and PalletNo = '{bindModel.PalletNo}' and Status = '1');";
                //    //string sttr = "select * from BllArrivalNoticeDetail where id in (select ASNDetailNo from BllBoxInfo where ASNNo = 'ASN2023042400002' and PalletNo = 'T2300004' and Status = '1');";
                //    var noticeDetail = Db.Ado.SqlQuery<BllArrivalNoticeDetail>(str);
                //    // 判断当前物料库存表是否已存在
                //    str = $"select * from DataStock where SkuNo = '{noticeDetail[0].SkuNo}' and LotNo = '{noticeDetail[0].LotNo}' and Standard = '{noticeDetail[0].Standard}' and SkuName = '{noticeDetail[0].SkuName}' and IsDel = '0'";
                //    var stockModel = Db.Ado.SqlQuerySingle<DataStock>(str);
 
                //    if (stockModel == null || stockModel.Id == 0)
                //    {
                //        stockModel = new DataStock
                //        {
                //            LotNo = bindModel.LotNo,
                //            LotText = bindModel.LotText,
                //            SupplierLot = bindModel.SupplierLot,
                //            SkuNo = bindModel.SkuNo,
                //            SkuName = bindModel.SkuName,
                //            Qty = (int)bindModel.Qty,
                //            Standard = bindModel.Standard,
                //            CreateUser = (int)model.CreateUser
                //        };
                //        Db.Insertable<DataStock>(stockModel).ExecuteCommand();
                //    }
                //    else
                //    {
                //        sqlString = $"update DataStock set Qty = Qty + {bindModel.Qty}," +
                //            $"UpdateTime = GETDATE(),UpdateUser='{model.CreateUser}' where id = '{stockModel.Id}';";
                //        Db.Ado.ExecuteCommand(sqlString);
                //    }
                //    #endregion
 
                //    #region 库存明细表
                //    // 判断当前托盘是否已存在(拣货回库托盘)
                //    var detailModel = Db.Queryable<DataStockDetail>()
                //        .First(it => it.PalletNo == bindModel.PalletNo && it.LotNo == noticeDetail[0].LotNo && it.SkuNo == noticeDetail[0].SkuNo
                //         && it.Standard == noticeDetail[0].Standard && it.IsDel == "0");
                //    if (detailModel == null || detailModel.Id == 0)
                //    {
                //        // 获取可抽检数量
                //        sqlString = "select sum(qty) from BllBoxInfo where isdel = 0 and ";
                //        sqlString += $"BindNo = '{bindModel.Id}'";
                //        int inspectQty = Db.Ado.GetInt(sqlString);
 
                //        // 添加库存明细表
                //        detailModel = new DataStockDetail()
                //        {
                //            LotNo = noticeDetail[0].LotNo,
                //            LotText = noticeDetail[0].LotText,
                //            SupplierLot = noticeDetail[0].SupplierLot,
                //            SkuNo = noticeDetail[0].SkuNo,
                //            SkuName = noticeDetail[0].SkuName,
                //            Standard = noticeDetail[0].Standard,
                //            Qty = noticeDetail[0].Qty,
                //            LockQty = 0,
                //            FrozenQty = 0,
                //            InspectQty = inspectQty,
                //            ASNNo = bindModel.ASNNo,
                //            ASNDetailNo = (int)bindModel.ASNDetailNo,
                //            WareHouseNo = locatModel.WareHouseNo,
                //            RoadwayNo = locatModel.RoadwayNo,
                //            AreaNo = locatModel.AreaNo,
                //            LocatNo = model.LocatNo,
                //            PalletNo = model.PalletNo,
                //            PalletNo2 = bindModel.PalletNo2,
                //            PalletNo3 = bindModel.PalletNo3,
                //            CompleteTime = serverTime,
                //            ProductionTime = bindModel.ProductionTime,
                //            ExpirationTime = bindModel.ExpirationTime,
                //            Status = "0",
                //            InspectMark = bindModel.InspectMark,
                //            BitPalletMark = bindModel.BitPalletMark,
                //            InspectStatus = bindModel.InspectMark,       // 组盘的时候就要默认设定好是否合格
                //            PackagNo = bindModel.PackagNo,
                //            IsBale = bindModel.IsBale,
                //            IsBelt = bindModel.IsBelt,
                //            CreateUser = (int)model.CreateUser
                //        };
 
                //        Db.Insertable<DataStockDetail>(detailModel).ExecuteCommand();
                //    }
                //    else
                //    {
                //        detailModel.LocatNo = model.LocatNo;
                //        detailModel.UpdateTime = serverTime;
                //        detailModel.UpdateUser = (int)model.CreateUser;
                //        // 变更储位地址
                //        Db.Updateable<DataStockDetail>(detailModel).UpdateColumns(it => new { it.LocatNo, it.UpdateTime, it.UpdateUser }).ExecuteCommand();
                //    }
                //    #endregion
 
 
                //}
                #endregion
 
 
 
                return strMsg;
            }
            catch (Exception ex)
            {
                Db.RollbackTran();
                throw ex;
            }
        }
 
        //根据托盘号获取入库单号
        public string GetASNNoByPalletNo(string palletNo)
        {
            try
            {
                //入库中
                var aSNNoList = Db.Ado.SqlQuery<string>($"SELECT DISTINCT ASNNo from BllPalletBind where IsDel=0 and Status=0 and PalletNo='{palletNo}' ").ToList();
                if (aSNNoList.Count <= 0)
                {
                    //库外托盘或平库托盘
                    var dataStockList = Db.Ado.SqlQuery<string>($"select DISTINCT PalletNo from DataStockDetail where IsDel=0 and isnull(LocatNo,'') = '' and PalletNo='{palletNo}' ").ToList();
                    if (dataStockList.Count <= 0)
                    {
                        throw new Exception("托盘信息不存在或托盘在立库中!");
                    }
                    return "";
                }
                if (aSNNoList.Count > 1)
                {
                    throw new Exception("同一个托盘号存在多个入库单,请检查!");
                }
                return aSNNoList[0];
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
 
        //根据箱码获取物料、批次、数量等信息
        public PdaPalletNoCheckDto GetBoxInfoByBoxNo(string boxNo)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(boxNo))
                {
                    throw new Exception("箱码不能为空");
                }
 
                var info = Db.Queryable<BllBoxInfo>().Where(m => m.IsDel == "0" && m.BoxNo == boxNo)
                    .GroupBy(m => new { m.BoxNo, m.SkuNo, m.SkuName, m.LotNo }).Select(a => new PdaPalletNoCheckDto
                    {
                        BoxNo = a.BoxNo,
                        SkuNo = a.SkuNo,
                        SkuName = a.SkuName,
                        LotNo = a.LotNo,
                        Qty = SqlFunc.AggregateSum(a.Qty)
                    }).ToList();
                if (info.Count > 1)
                {
                    throw new Exception("当前箱码查询出多条物料或批次信息,请核实");
                }
 
                return info.FirstOrDefault();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        //拼托入库
        public void CompleteInStockLing(PdaLingAsnVm model, int userId)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(model.AsnNo))
                {
                    throw new Exception("-1:盘点单据不能为空!");
                }
                if (model.AsnDetailId == null || model.AsnDetailId == 0)
                {
                    throw new Exception("-1:物料批次不能为空!");
                }
                if (string.IsNullOrWhiteSpace(model.PalletNo))
                {
                    throw new Exception("-1:托盘条码不能为空!");
                }
                if (string.IsNullOrWhiteSpace(model.BoxNo))
                {
                    throw new Exception("-1:箱码不能为空!");
                }
                //获取当前时间
                DateTime serverTime = Db.Ado.GetDateTime("select GETDATE();");
 
                Db.BeginTran();//开启事务
 
                #region 箱码信息
                //获取箱码信息
                string boxstr = $"select * from BLLBoxInfo where IsDel = '0' and BoxNo = '{model.BoxNo}'";
                List<BllBoxInfo> boxinfo = Db.Ado.SqlQuery<BllBoxInfo>(boxstr).ToList();
                //验证箱码信息是否存在
                if (boxinfo.Count <= 0)
                {
                    throw new Exception("-1:箱码信息不存在,请核查!");
                }
                var bNum = 0;//数量
                //验证箱码是否已绑定托盘
                foreach (var item in boxinfo)
                {
                    if (!string.IsNullOrEmpty(item.PalletNo))
                    {
                        throw new Exception("-1:该箱码已绑定其他托盘,请先解绑托盘!");
                    }
                    bNum += item.Qty;
                }
                if (bNum == boxinfo[0].FullQty)
                {
                    throw new Exception("-1:该箱子不是零箱,不能入零箱库!");
                }
                #endregion       
 
                #region 单据信息
                //获取入库单据总单
                var isFinsh = true;
                var arrival = new BllArrivalNotice();
                arrival = Db.Queryable<BllArrivalNotice>().First(a => a.IsDel == "0" && a.ASNNo == model.AsnNo);
                //验证入库单总单是否关闭
                if (arrival == null)
                {
                    throw new Exception("-1:入库单总单为空,请核查!");
                }
                //判断入库单总单是否为已关闭
                if (arrival.Status == "3")
                {
                    throw new Exception("-1:入库单总单已关闭,请核查!");
                }
                // 验证入库单明细是否存在
                var arrivalnotice = Db.Queryable<BllArrivalNoticeDetail>().First(m => m.IsDel == "0" && m.Id == model.AsnDetailId && m.ASNNo == model.AsnNo);
                if (arrivalnotice == null)
                {
                    throw new Exception("-1:当前物料及批次与单据无关联,请核实!");
                }
                arrivalnotice.FactQty += bNum;
                arrivalnotice.CompleteQty += bNum;
                //判断入库数量增加后是否大于等于单据数量
                if (arrivalnotice.CompleteQty >= arrivalnotice.Qty)
                {
                    arrivalnotice.Status = "2";
                    arrivalnotice.CompleteTime = serverTime;
                }
                else
                {
                    arrivalnotice.Status = "1";
 
                    isFinsh = false;
                }
                arrivalnotice.UpdateUser = userId;
                arrivalnotice.UpdateTime = serverTime;
                Db.Updateable(arrivalnotice).ExecuteCommand();
 
                var arrivalnoticeList = Db.Queryable<BllArrivalNoticeDetail>().Where(m => m.IsDel == "0" && m.ASNNo == model.AsnNo).ToList();
                foreach (var item in arrivalnoticeList)
                {
                    if (item.CompleteQty < item.Qty)
                    {
                        isFinsh = false;
                        break;
                    }
                }
                if (isFinsh)
                {
                    //修改入库单总单信息
                    arrival.Status = "2";
                    arrival.CompleteTime = serverTime;
                    arrival.UpdateUser = userId;
                    arrival.UpdateTime = serverTime;
                    Db.Updateable(arrival).ExecuteCommand();
                }
                else
                {
                    //判断总单状态是否为正在执行
                    if (arrival.Status == "0")
                    {
                        //修改入库单总单信息
                        arrival.Status = "1";
                        arrival.UpdateUser = userId;
                        arrival.UpdateTime = serverTime;
                        Db.Updateable(arrival).ExecuteCommand();
                    }
                }
                #endregion
 
                #region 托盘信息
                //托盘是否存在
                var pallet = Db.Queryable<SysPallets>().First(m => m.IsDel == "0" && m.PalletNo == model.PalletNo);
                if (pallet == null)
                {
                    throw new Exception("-1:未查询到托盘信息,请核实!");
                }
                if (pallet.Status == "0")
                {
                    // 更改托盘使用状态
                    var sqlStr = $"update SysPallets set Status = '1' where PalletNo = '{model.PalletNo}';";
                    Db.Ado.ExecuteCommand(sqlStr);
                }
 
                var palletStockDetail = Db.Ado.SqlQuery<DataStock>($"select * from DataStockDetail where PalletNo='{model.PalletNo}' and ISNULL(LocatNo,'')<>''").ToList();
                if (palletStockDetail != null && palletStockDetail.Count > 0)
                {
                    throw new Exception("-1:托盘在立库中不允许拼托,请核查!");
                }
                #endregion
 
                #region 物料信息
                var sku = Db.Queryable<SysMaterials>().First(s => s.SkuNo == arrivalnotice.SkuNo && s.IsDel == "0" && s.SkuName == arrivalnotice.SkuName);
                //判断物料信息是否为空
                if (sku == null)
                {
                    throw new Exception("-1:物料信息为空,请核查!");
                }
                #endregion
 
                #region 绑定托盘
                var bindId = 0;
                var bind = new BllPalletBind
                {
                    ASNNo = model.AsnNo,
                    ASNDetailNo = (int)model.AsnDetailId,
                    PalletNo = model.PalletNo,
                    PalletNo2 = "",
                    PalletNo3 = "",
                    Qty = bNum,
                    FullQty = 0,//整托数量,托盘上可能放的是不同包装的箱子
                    Status = "2",
                    CompleteTime = serverTime,
                    Type = "0",
                    LotNo = arrivalnotice.LotNo,
                    LotText = arrivalnotice.LotText,
                    SupplierLot = "",
                    InspectMark = "0",
                    BitPalletMark = "1",
                    IsBale = "0",
                    IsBelt = "0",
                    CreateUser = userId
                };
                // 插入托盘绑定表
                bindId = Db.Insertable(bind).ExecuteReturnIdentity();
                #endregion
 
                #region 库存总表
                // 判断当前物料库存表是否已存在
                string str = $"select * from DataStock where SkuNo = '{arrivalnotice.SkuNo}' and LotNo = '{arrivalnotice.LotNo}' and Standard = '{arrivalnotice.Standard}' and SkuName = '{arrivalnotice.SkuName}' and IsDel = '0'";
                var stockModel = Db.Ado.SqlQuerySingle<DataStock>(str);
 
                if (stockModel == null || stockModel.Id == 0)
                {
                    stockModel = new DataStock
                    {
                        LotNo = arrivalnotice.LotNo,
                        LotText = arrivalnotice.LotText,
                        SupplierLot = arrivalnotice.SupplierLot,
                        SkuNo = arrivalnotice.SkuNo,
                        SkuName = arrivalnotice.SkuName,
                        Qty = bNum,
                        Standard = arrivalnotice.Standard,
                        CreateUser = userId,
                        CreateTime = serverTime
                    };
                    Db.Insertable<DataStock>(stockModel).ExecuteCommand();
                }
                else
                {
                    string sqlString = $"update DataStock set Qty = Qty + {bNum}," +
                        $"UpdateTime = GETDATE(),UpdateUser='{userId}' where id = '{stockModel.Id}';";
                    Db.Ado.ExecuteCommand(sqlString);
                }
                #endregion
 
                #region 库存明细表
                var boxinfoItem = boxinfo.First();
                var detailModel = Db.Queryable<DataStockDetail>().First(it => it.PalletNo == model.PalletNo && it.LotNo == arrivalnotice.LotNo
                     && it.SkuNo == arrivalnotice.SkuNo && it.Standard == arrivalnotice.Standard && it.IsDel == "0");
                var stId = 0;
                if (detailModel == null || detailModel.Id == 0)
                {
                    // 添加库存明细表
                    detailModel = new DataStockDetail()
                    {
                        LotNo = arrivalnotice.LotNo,
                        LotText = arrivalnotice.LotText,
                        SupplierLot = arrivalnotice.SupplierLot,
                        SkuNo = arrivalnotice.SkuNo,
                        SkuName = arrivalnotice.SkuName,
                        Standard = arrivalnotice.Standard,
                        Qty = bNum,
                        LockQty = 0,
                        FrozenQty = 0,
                        InspectQty = bNum,
                        ASNNo = arrivalnotice.ASNNo,
                        ASNDetailNo = (int)bind.ASNDetailNo,
                        WareHouseNo = "W02",
                        RoadwayNo = "",
                        AreaNo = "",
                        LocatNo = "",
                        PalletNo = model.PalletNo,
                        PalletNo2 = "",
                        PalletNo3 = "",
                        CompleteTime = serverTime,
                        ProductionTime = boxinfoItem.ProductionTime,
                        ExpirationTime = boxinfoItem.ProductionTime.Value.AddDays((int)sku.Warranty),//过期时间
                        Status = "0",
                        InspectMark = boxinfoItem.InspectMark,
                        BitPalletMark = "1",
                        InspectStatus = sku.IsInspect,// 组盘的时候就要默认设定好是否合格
                        PackagNo = sku.PackagNo,
                        IsBale = null,
                        IsBelt = null,
                        CreateUser = userId,
                        CreateTime = serverTime
                    };
                    stId = Db.Insertable<DataStockDetail>(detailModel).ExecuteReturnIdentity();
                }
                else
                {
                    stId = detailModel.Id;
                    detailModel.Qty += bNum;
                    detailModel.InspectQty += bNum;
                    detailModel.LocatNo = "";
                    detailModel.UpdateTime = serverTime;
                    detailModel.UpdateUser = userId;
                    // 变更储位地址
                    Db.Updateable<DataStockDetail>(detailModel).UpdateColumns(it => new { it.Qty, it.InspectQty, it.LocatNo, it.UpdateTime, it.UpdateUser }).ExecuteCommand();
                }
                #endregion
 
                #region 库存箱支明细表                
                var comTime = DateTime.Now;
                foreach (var item in boxinfo)
                {
                    // 维护箱支关系表
                    string sqlString = $"Update BLLBoxInfo set ASNNo='{model.AsnNo}',ASNDetailNo='{model.AsnDetailId}',BindNo='{bindId}',PalletNo='{model.PalletNo}',Status='2'  where IsDel = '0' and BoxNo = '{model.BoxNo}'";
                    Db.Ado.ExecuteCommand(sqlString);
                    // 插入新组的箱支信息
                    var info = new DataBoxInfo()
                    {
                        StockDetailId = stId,
                        BindNo = bindId,
                        BoxNo = item.BoxNo,
                        BoxNo2 = item.BoxNo2,
                        BoxNo3 = item.BoxNo3,
                        PalletNo = model.PalletNo,
                        PalletNo2 = item.PalletNo2,
                        PalletNo3 = item.PalletNo3,
                        Qty = item.Qty,
                        FullQty = item.FullQty,
                        Status = "2",
                        LotNo = item.LotNo,
                        LotText = item.LotText,
                        SkuNo = item.SkuNo,
                        SkuName = item.SkuName,
                        Standard = sku.Standard,
                        ProductionTime = item.ProductionTime,
                        SupplierLot = item.SupplierLot,
                        InspectMark = item.InspectMark,
                        BitBoxMark = item.BitBoxMark,
                        InspectStatus = item.InspectStatus,
 
                        IsDel = "0",
                        CreateUser = 0,
                        CreateTime = comTime
                    };
                    Db.Insertable(info).ExecuteCommand();
                }
                #endregion
 
                new OperationASNServer().AddLogOperationAsn("PDA模块", "零箱入库", model.PalletNo, "完成", $"在PDA上完成单据号为:{model.AsnNo}的托盘码为:{model.PalletNo}的零箱入库操作", userId);
 
                Db.CommitTran();
 
            }
            catch (Exception ex)
            {
                Db.Ado.RollbackTran();//事务回滚
                throw ex;
            }
        }
 
        #region 产品组托
        /// <summary>
        /// 根据箱码获取标签箱码信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public List<BoxInfoDto> GetBindBoxInfos(BoxInfoVm model)
        {
            try
            {
                if (string.IsNullOrEmpty(model.BoxNo))
                {
                    throw new Exception("请扫描外箱条码!");
                }
                string sqlString = $@"SELECT 
                                        ASNNo,
                                        BoxNo, 
                                        SkuNo,
                                        SkuName, 
                                        LotNo, 
                                        SUM(Qty) as Qty 
                                      FROM BllBoxInfo 
                                      WHERE IsDel = '0' 
                                        AND Status='0'  
                                        AND BoxNo = '{model.BoxNo}'
                                      GROUP BY ASNNo,BoxNo,SkuNo,SkuName,LotNo; ";
                var models = Db.Ado.SqlQuery<BoxInfoDto>(sqlString);
                if (models == null)
                {
                    throw new Exception("箱码信息不存在!");
                }
                return models;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
 
        public void BindPallet(PdaPalletBindVm model, int userId, string origin)
        {
            try
            {
                #region 判断
 
                if (string.IsNullOrEmpty(model.AsnNo))
                {
                    throw new Exception("-1:单据号不可为空!");
                }
                if (model.AsnDetailId == null || model.AsnDetailId == 0)
                {
                    throw new Exception("-1:物料不可为空!");
                }
                if (string.IsNullOrEmpty(model.PalletNo))
                {
                    throw new Exception("-1:托盘号不可为空!");
                }
                //判断物料数量是否为0 为0判断箱码信息 不为0继续
                if (model.SkuQty == 0)
                {
                    if (string.IsNullOrEmpty(model.BoxNo))
                    {
                        throw new Exception("-1:箱码信息不可为空!");
                    }
 
                    if (model.IsContinue == "1")
                    {
                        if (string.IsNullOrWhiteSpace(model.TailBoxNo))
                        {
                            throw new Exception("-1:开启连续组托时,尾箱码信息不可为空!");
                        }
 
                    }
                }
                #endregion
 
                Db.BeginTran();
 
                //托盘是否存在
                var pallet = Db.Queryable<SysPallets>().First(m => m.IsDel == "0" && m.PalletNo == model.PalletNo);
                if (pallet == null)
                {
                    throw new Exception("未查询到托盘信息,请核实!");
                }
                // 验证入库单明细是否存在
                var detail = Db.Queryable<BllArrivalNoticeDetail>().First(m => m.IsDel == "0" && m.Id == model.AsnDetailId && m.ASNNo == model.AsnNo);
                if (detail == null)
                {
                    throw new Exception("-1:当前物料及批次与单据无关联,请核实!");
                }
                #region 包装
                var package = Db.Queryable<SysPackag>().Where(m => m.IsDel == "0");
                var sku = Db.Queryable<SysMaterials>().First(m => m.IsDel == "0" && m.SkuNo == detail.SkuNo);
                var pack = package.First(m => m.IsDel == "0" && m.PackagNo == sku.PackagNo);
                if (pack == null)
                {
                    throw new Exception("-1:获取物料包装失败,请核实!");
                }
                var pNum = 0;//托盘物品数量 
                var bNum = 0;//箱码物品数量 
                if (pack.L5Num.HasValue)
                {
                    pNum = (int)pack.L5Num;
                    bNum = (int)pack.L4Num;
                }
                else if (pack.L4Num.HasValue)
                {
                    pNum = (int)pack.L4Num;
                    bNum = (int)pack.L3Num;
                }
                else if (pack.L3Num.HasValue)
                {
                    pNum = (int)pack.L3Num;
                    bNum = (int)pack.L2Num;
                }
                else if (pack.L2Num.HasValue)
                {
                    pNum = (int)pack.L2Num;
                    bNum = (int)pack.L1Num;
                }
                else if (pack.L1Num.HasValue)
                {
                    pNum = (int)pack.L1Num;
                    bNum = (int)pack.L1Num;
                }
                if (pNum == 0 || bNum == 0)
                {
                    throw new Exception($"绑定失败,{detail.SkuNo}物品包装未找到!");
                }
                #endregion
 
                #region 验证是否允许立库同托盘不同物料入库或同托盘同物料不同批次入库
                var palletBind = Db.Queryable<BllPalletBind>().First(m => m.IsDel == "0" && m.ASNNo == model.AsnNo && m.PalletNo == model.PalletNo && m.ASNDetailNo != model.AsnDetailId);
                if (palletBind != null)
                {
                    var box = Db.Queryable<BllBoxInfo>().First(m => m.IsDel == "0" && m.BindNo == palletBind.Id && m.BitBoxMark == "0");
                    if (box != null && (box.SkuNo != detail.SkuNo || box.LotNo != detail.LotNo))
                    {
                        var funSetting = Db.Queryable<SysFunSetting>().First(a => a.IsDel == "0" && a.FunSetNo == "Fun045");
                        if (funSetting == null || funSetting.IsEnable == "OFF")
                        {
                            throw new Exception($"不允许立库同托盘不同物料入库或同托盘不同批次入库!");
                        }
                    }
                }
                #endregion
                var bind = Db.Queryable<BllPalletBind>().First(m => m.IsDel == "0" && m.ASNDetailNo == model.AsnDetailId && m.PalletNo == model.PalletNo);
                var bindId = 0;
                if (bind == null)
                {
                    bind = new BllPalletBind
                    {
                        ASNNo = model.AsnNo,
                        ASNDetailNo = (int)model.AsnDetailId,
                        PalletNo = model.PalletNo,
                        PalletNo2 = "",
                        PalletNo3 = "",
                        Qty = 0,
                        FullQty = pNum,
                        Status = "0",
                        Type = "0",
                        LotNo = detail.LotNo,
                        LotText = detail.LotText,
                        SupplierLot = "",
                        InspectMark = "0",
                        BitPalletMark = "1",
                        IsBale = "0",
                        IsBelt = "0",
                        CreateUser = userId
                    };
                    // 插入托盘绑定表
                    bindId = Db.Insertable(bind).ExecuteReturnIdentity();
                }
                else
                {
                    if (bind.Status != "0")
                    {
                        throw new Exception("-1:当前托盘正在执行中,绑定失败,请核实!");
                    }
                    bindId = bind.Id;
                }
 
                #region 箱码信息
                var msgStr = $"箱号为{model.BoxNo}";
                var boxInfoList = new List<BllBoxInfo>();
                //首箱
                var boxInfo = Db.Queryable<BllBoxInfo>().Where(m => m.IsDel == "0" && m.BoxNo == model.BoxNo && m.Status == "0").ToList();
                if (boxInfo.Count == 0)
                {
                    throw new Exception("-1:箱码信息不存在!");
                }
                boxInfoList.AddRange(boxInfo);
                //是否连续组托
                if (model.IsContinue == "1")
                {
                    //尾箱
                    var boxInfo2 = Db.Queryable<BllBoxInfo>().Where(m => m.IsDel == "0" && m.BoxNo == model.TailBoxNo && m.Status == "0").ToList();
                    if (boxInfo2.Count == 0)
                    {
                        throw new Exception("-1:尾箱码信息不存在!");
                    }
                    boxInfoList.AddRange(boxInfo2);
                    var sql = $"select * from BllBoxInfo where IsDel = '0' and Status = '0' and boxNo>'{model.BoxNo}' and boxNo<'{model.TailBoxNo}'; ";
                    var list = Db.Ado.SqlQuery<BllBoxInfo>(sql);
                    boxInfoList.AddRange(list);
                    msgStr += $"尾箱号为{model.TailBoxNo}";
                }
                #endregion
 
                // 更改箱支关系表
                decimal factQty = 0.00m;//托盘总数量
                var boxGroup = boxInfoList.GroupBy(m => m.BoxNo).ToList();
                foreach (var g in boxGroup)
                {
                    var boxFullQty = 0;//箱内总数量
                    foreach (var box in g)
                    {
                        if (box.BindNo != null && box.BindNo != 0)
                        {
                            continue;
                        }
                        //箱内物料批次与单据明细不符合
                        if (box.SkuNo != detail.SkuNo || box.LotNo != detail.LotNo)
                        {
                            throw new Exception($"-1:{box.BoxNo}箱内物料及批次与单据不一致,请核实!");
                        }
 
                        box.ASNNo = model.AsnNo;
                        box.ASNDetailNo = model.AsnDetailId;
                        box.BindNo = bindId;
                        box.PalletNo = model.PalletNo;
                        box.Status = "1";
                        box.CompleteTime = DateTime.Now;
                        box.UpdateTime = DateTime.Now;
                        box.UpdateUser = userId;
 
                        factQty += box.Qty;
                        boxFullQty += box.Qty;
                    }
                    if (boxFullQty > bNum)
                    {
                        throw new Exception($"绑定失败,{g.Key}箱码绑定数量大于该物品包装数量!");
                    }
                }
                Db.Updateable(boxInfoList).ExecuteCommand();
 
                // 更新托盘绑定表
                bind.Qty += factQty;
                if (bind.FullQty < bind.Qty)
                {
                    throw new Exception("托盘绑定数量已超出该物料包装数量");
                }
 
                if (bind.FullQty == bind.Qty)
                {
                    bind.BitPalletMark = "0";
                }
                Db.Updateable(bind).Where(m => m.Id == bindId).ExecuteCommand();
 
                // 更改入库单明细已组数量
                var sqlString = string.Empty;
                sqlString += $"update BllArrivalNoticeDetail set FactQty = FactQty + '{factQty}' where id = '{model.AsnDetailId}';";
                Db.Ado.ExecuteCommand(sqlString);
 
                // 更改入库单及入库明细状态
                if (detail.Status == "0")
                {
                    var sqlString2 = string.Empty;
                    sqlString2 += $"update BllArrivalNotice set Status = '1',CompleteTime= getDate() where ASNNo = '{model.AsnNo}' and Status ='0';";
                    sqlString2 += $"update BllArrivalNoticeDetail set Status = '1',CompleteTime= getDate() where id = '{model.AsnDetailId}' and Status ='0';";
                    Db.Ado.ExecuteCommand(sqlString2);
                }
 
                // 更改托盘使用状态
                var sqlStr = $"update SysPallets set Status = '1' where PalletNo = '{model.PalletNo}';";
                //添加托盘记录表数据
                sqlStr += $"insert into LogPalletTrack values('{model.PalletNo}','{model.AsnNo}','组盘','0',getDate(),{userId},NULL,NULL);";
                Db.Ado.ExecuteCommand(sqlStr);
                new OperationASNServer().AddLogOperationAsn("PDA模块", "产品组托", model.AsnNo, "添加", $"添加了托盘码为:{model.PalletNo}、{msgStr}的组盘信息", userId);
 
                Db.CommitTran();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}