11
yanjinhui
2 天以前 517afb9079abcdecf99ec3ed4d71f90a4d479e7e
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
using HslCommunication;
using MailKit.Search;
using Microsoft.Data.SqlClient;
using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json;
using OfficeOpenXml.Style;
using Org.BouncyCastle.Asn1.X509;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common;
using WIDESEA_Common.LocationEnum;
using WIDESEA_Common.OrderEnum;
using WIDESEA_Common.StockEnum;
using WIDESEA_Common.TaskEnum;
using WIDESEA_Common.WareHouseEnum;
using WIDESEA_Core;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_DTO.Outbound;
using WIDESEA_DTO.SquareCabin;
using WIDESEA_IBasicService;
using WIDESEA_ISquareCabinServices;
using WIDESEA_IWMsInfoServices;
using WIDESEA_Model.Models;
using static WIDESEA_DTO.SquareCabin.OrderDto;
 
 
 
namespace WIDESEA_SquareCabinServices
{
    public partial class DeliveryOrderServices : ServiceBase<Dt_DeliveryOrder, IRepository<Dt_DeliveryOrder>>, IDeliveryOrderServices
    {
        private readonly ICabinOrderServices _cabinOrderServices;
        private readonly IUnitOfWorkManage _unitOfWorkManage;
        private readonly IInventory_BatchServices _inventory_BatchServices;
        private readonly ILocationInfoService _locationInfoService;
        private readonly IInventoryInfoService _inventoryInfoService;
        private readonly IDeliveryOrderDetailServices _deliveryOrderDetailServices;
        private readonly ISupplyTaskService _supplyTaskService;
        private readonly ISupplyTaskHtyService _supplyTaskHtyService;
        private readonly ITacticsService _tacticsService;
        private readonly IMaterielInfoService _materielInfoService;
        private readonly IMessageInfoService _messageInfoService;
        public IRepository<Dt_DeliveryOrder> Repository => BaseDal;
        public DeliveryOrderServices(IRepository<Dt_DeliveryOrder> BaseDal, IUnitOfWorkManage unitOfWorkManage, IInventory_BatchServices inventory_BatchServices, IInventoryInfoService inventoryInfoService, IDeliveryOrderDetailServices deliveryOrderDetailServices, ISupplyTaskService supplyTaskService, ICabinOrderServices cabinOrderServices, ITacticsService tacticsService, ISupplyTaskHtyService supplyTaskHtyService, IMessageInfoService messageInfoService, IMaterielInfoService materielInfoService, ILocationInfoService locationInfoService) : base(BaseDal)
        {
            _unitOfWorkManage = unitOfWorkManage;
            _deliveryOrderDetailServices = deliveryOrderDetailServices;
            _supplyTaskService = supplyTaskService;
            _inventory_BatchServices = inventory_BatchServices;
            _inventoryInfoService = inventoryInfoService;
            _cabinOrderServices = cabinOrderServices;
            _tacticsService = tacticsService;
            _supplyTaskHtyService = supplyTaskHtyService;
            _messageInfoService = messageInfoService;
            _materielInfoService = materielInfoService;
            _locationInfoService = locationInfoService;
        }
        public override WebResponseContent UpdateData(SaveModel saveModel)
        {
            try
            {
                int id = saveModel.MainData["id"].ObjToInt();
                var warehouse_no = saveModel.MainData["warehouse_no"].ToString();
                var out_no = saveModel.MainData["out_no"].ToString();
                //OutboundOrderAddDTO outboundOrder = saveModel.MainData.DicToModel<OutboundOrderAddDTO>();
                //Dt_DeliveryOrder deliveryOrder = BaseDal.QueryFirst(x => x.Id == id);
                List<DeliveryOrderDetailAddDTO> orderDetailAddDTOs = saveModel.DetailData.DicToIEnumerable<DeliveryOrderDetailAddDTO>();
                orderDetailAddDTOs = orderDetailAddDTOs.Where(x => x.id == 0).ToList();
                if (orderDetailAddDTOs.Count < 1) return WebResponseContent.Instance.OK();
                if (orderDetailAddDTOs.Where(x => string.IsNullOrEmpty(x.locationCode)).Any()) return WebResponseContent.Instance.Error("货位号为必填字段!");
                if (orderDetailAddDTOs.Where(x => string.IsNullOrEmpty(x.exp_date)).Any()) return WebResponseContent.Instance.Error("效期为必填字段!");
                var LocationCodes = orderDetailAddDTOs.Select(x => x.locationCode);
                List<Dt_LocationInfo> locationInfos = _locationInfoService.Repository.QueryData(x => x.WarehouseCode == warehouse_no && LocationCodes.Contains(x.LocationCode));
                var diff = LocationCodes.Except(locationInfos.Select(x => x.LocationCode)).ToArray();
                if (diff.Length > 0) return WebResponseContent.Instance.Error($"货位编号【{string.Join(", ", diff)}】不属于当前库房");
                var array1 = orderDetailAddDTOs.Select(x => x.goods_no);
                var MaterielInfos = _materielInfoService.Repository.QueryData(x => array1.Contains(x.MaterielCode));
                var array2 = MaterielInfos.Select(x => x.MaterielCode);
                diff = array1.Except(array2).ToArray();
                if (diff.Length > 0) return WebResponseContent.Instance.Error($"请维护物料编号【{string.Join(", ", diff)}】的物料信息");
                if (warehouse_no == WarehouseEnum.立库.ObjToInt().ToString("000"))
                {
                    var MaterielInfos1 = MaterielInfos.Where(x => x.MaterielSourceType == MaterielSourceTypeEnum.PurchasePart).ToList();
                    if (MaterielInfos1.Count > 0)
                    {
                        return WebResponseContent.Instance.Error($"物料编号【{string.Join(", ", MaterielInfos1.Select(x => x.MaterielCode))}】的物料属性分类为大件,不可入立库");
                    }
                }
                var InventoryInfos = _inventoryInfoService.Repository.QueryData(x => x.WarehouseCode == warehouse_no && array2.ToList().Contains(x.MaterielCode));
                var Batchs = _inventory_BatchServices.Repository.QueryData(x => array2.Contains(x.MaterielCode));
                List<Dt_DeliveryOrderDetail> deliveryOrderDetails = new List<Dt_DeliveryOrderDetail>();
                List<Dt_SupplyTask> supplyTasks = new List<Dt_SupplyTask>();
                List<Dt_InventoryInfo> inventoryInfos = new List<Dt_InventoryInfo>();
                List<Dt_Inventory_Batch> inventory_Batches = new List<Dt_Inventory_Batch>();
                foreach (var item in orderDetailAddDTOs)
                {
                    if (InventoryInfos.Where(x => x.MaterielCode == item.goods_no && x.BatchNo == item.batch_num).Any())
                        return WebResponseContent.Instance.Error($"物料编号【{item.goods_no}】物料批次【{item.batch_num}】已存在库存");
                    #region 添加盘点单详情
                    Dt_DeliveryOrderDetail dt_DeliveryOrde = new Dt_DeliveryOrderDetail()
                    {
                        DeliveryOrderId = id,
                        Reservoirarea = warehouse_no,
                        Status = 2,
                        Order_qty = 0,
                        Order_Outqty = item.order_Outqty,
                        Goods_no = item.goods_no,
                        OotDetailStatus = "新建",
                        Batch_num = item.batch_num,
                        Creater = App.User.UserName,
                        CreateDate = DateTime.Now,
                    };
                    deliveryOrderDetails.Add(dt_DeliveryOrde);
                    #endregion
 
                    #region 添加库存、批次信息、盘点任务
                    var MaterielInfo = MaterielInfos.First(x => x.MaterielCode == item.goods_no);
                    Dt_InventoryInfo inventoryInfo = new Dt_InventoryInfo()
                    {
                        BatchNo = item.batch_num,
                        MaterielCode = MaterielInfo.MaterielCode,
                        AvailableQuantity = 0,
                        CreateDate = DateTime.Now,
                        Creater = App.User.UserName,
                        InDate = DateTime.Now,
                        LocationCode = item.locationCode,
                        MaterielName = MaterielInfo.MaterielName,
                        MaterielSpec = MaterielInfo.MaterielSpec,
                        OutboundQuantity = 0,
                        StockQuantity = 0,
                        StockStatus = StockStatusEmun.盘点锁定.ObjToInt(),
                        SupplyQuantity = 0,
                        WarehouseCode = warehouse_no,
                        ValidityPeriod = item.exp_date
                    };
                    inventoryInfos.Add(inventoryInfo);
                    Dt_Inventory_Batch? inventory_Batch = Batchs.FirstOrDefault(x => x.BatchNo == item.batch_num && x.MaterielCode == item.goods_no);
                    if (inventory_Batch == null)
                    {
                        inventory_Batch = new Dt_Inventory_Batch()
                        {
                            BatchNo = inventoryInfo.BatchNo,
                            CreateDate = inventoryInfo.CreateDate,
                            Creater = inventoryInfo.Creater,
                            MaterielCode = inventoryInfo.MaterielCode,
                            ERPStockQuantity = 0,
                            MaterielName = inventoryInfo.MaterielName,
                            MaterielSpec = inventoryInfo.MaterielSpec,
                            OutboundQuantity = inventoryInfo.OutboundQuantity,
                            ProductionDate = inventoryInfo.ProductionDate,
                            Status = false,
                            StockQuantity = inventoryInfo.StockQuantity,
                            AvailableQuantity = inventoryInfo.AvailableQuantity,
                            ValidityPeriod = inventoryInfo.ValidityPeriod.ObjToDate(),
                            SupplyQuantity = inventoryInfo.SupplyQuantity,
                        };
                        inventory_Batches.Add(inventory_Batch);
                    }
                    Dt_SupplyTask supplyTask = new Dt_SupplyTask()
                    {
                        WarehouseCode = warehouse_no,
                        TaskStatus = SupplyStatusEnum.NewCheck.ObjToInt(),
                        BatchNo = inventoryInfo.BatchNo,
                        MaterielName = inventoryInfo.MaterielName,
                        MaterielCode = inventoryInfo.MaterielCode,
                        MaterielSpec = inventoryInfo.MaterielSpec,
                        TaskType = TaskTypeEnum.OutInventory.ObjToInt(),
                        CreateDate = DateTime.Now,
                        Creater = App.User.UserName,
                        LocationCode = inventoryInfo.LocationCode,
                        OrderNo = out_no,
                        StockQuantity = inventoryInfo.StockQuantity,
                        SupplyQuantity = 0,
                        Remark = "盘点"
                    };
                    supplyTasks.Add(supplyTask);
                    #endregion
                }
                _unitOfWorkManage.BeginTran();
                _deliveryOrderDetailServices.AddData(deliveryOrderDetails);
                _inventoryInfoService.AddData(inventoryInfos);
                if (inventory_Batches.Count > 0) _inventory_BatchServices.AddData(inventory_Batches);
                _supplyTaskService.AddData(supplyTasks);
                _unitOfWorkManage.CommitTran();
                return WebResponseContent.Instance.OK("盘点详情添加成功,请通过ERP平账!");
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
 
        #region 创建出库单
        /// <summary>
        /// 创建出库单
        /// </summary>
        /// <param name="outorder"></param>
        /// <returns></returns>
        public WebResponseContent CreateOutboundOrder(UpstramOutOrderInfo outorder)
        {
            WebResponseContent webResponseContent = new WebResponseContent();
            try
            {
                Dt_Tactics tactics = _tacticsService.Repository.QueryFirst(x => x.TacticeName == "出库策略");
                #region 特殊库房出库
                string WareCodeMJ = WarehouseEnum.麻精库.ObjToInt().ToString("000");
                string WareCodeLD = WarehouseEnum.冷冻库.ObjToInt().ToString("000");
                if (outorder.warehouse_no == WareCodeMJ || outorder.warehouse_no == WareCodeLD)
                {
                    #region 添加出库单
                    var entityOrder = new Dt_DeliveryOrder
                    {
                        Out_no = outorder.order_no,
                        Out_type = outorder.order_type,
                        Client_no = outorder.client_no,
                        Client_name = outorder.client_name,
                        Account_time = outorder.account_time,
                        Warehouse_no = outorder.warehouse_no,
                        OutStatus = "新建",
                        Details = outorder.details.Select(d => new Dt_DeliveryOrderDetail
                        {
                            Reservoirarea = outorder.warehouse_no,
                            Goods_no = d.goods_no,
                            Order_qty = Math.Abs(d.order_qty), // 出库数量转为正数
                            Batch_num = d.batch_num,
                            Exp_date = d.exp_date,
                            OotDetailStatus = "新建",
                            Status = 2, // pad平库,无需同步
                        }).ToList()
                    };
                    #endregion
                    #region 处理库存、库存批次、添加出库任务
                    List<Dt_SupplyTask> supplyTasks = new List<Dt_SupplyTask>();
                    List<Dt_Inventory_Batch> batchesUp = new List<Dt_Inventory_Batch>();
                    List<Dt_InventoryInfo> inventoryInfosUp = new List<Dt_InventoryInfo>();
                    var inventory_Batchs = _inventory_BatchServices.Repository.QueryData(x => entityOrder.Details.Select(e => e.Goods_no).Contains(x.MaterielCode));
                    var InventoryInfos = _inventoryInfoService.Repository.QueryData(x => entityOrder.Details.Select(e => e.Goods_no).Contains(x.MaterielCode) && x.StockStatus == StockStatusEmun.入库完成.ObjToInt() && x.AvailableQuantity > 0 && x.WarehouseCode == outorder.warehouse_no);
                    foreach (var item in entityOrder.Details)
                    {
                        Dt_Inventory_Batch? inventory_Batch = inventory_Batchs.Where(x => x.MaterielCode == item.Goods_no && x.BatchNo == item.Batch_num).FirstOrDefault();
                        if (inventory_Batch == null) throw new Exception($"未找到出库单号【{entityOrder.Out_no}】中物料编号【{item.Goods_no}】物料批次【{item.Batch_num}】的库存批次信息");
                        if (inventory_Batch.AvailableQuantity < item.Order_qty) throw new Exception($"出库单号【{entityOrder.Out_no}】中物料编号【{item.Goods_no}】物料批次【{item.Batch_num}】的库存批次信息可用数量不足");
                        inventory_Batch.AvailableQuantity -= item.Order_qty;
                        inventory_Batch.OutboundQuantity += item.Order_qty;
                        List<Dt_InventoryInfo> dt_InventoryInfos = InventoryInfos.Where(x => x.MaterielCode == item.Goods_no && x.BatchNo == item.Batch_num).ToList();
                        if (dt_InventoryInfos.Count < 1) throw new Exception($"出库单号【{entityOrder.Out_no}】中物料编号【{item.Goods_no}】物料批次【{item.Batch_num}】的可用库存不足");
                        #region 按出库策略查找库存
                        if (tactics.SelectTactice == TacticsEnum.ComeOutonFirstTime.ObjToInt())
                            dt_InventoryInfos = dt_InventoryInfos.OrderBy(x => x.ValidityPeriod).ToList();
                        else
                            dt_InventoryInfos = dt_InventoryInfos.OrderBy(x => x.InDate).ToList();
                        #endregion
                        var Order_qty = item.Order_qty;//出库单数量
                        foreach (var InventoryInfo in dt_InventoryInfos)
                        {
                            if (Order_qty <= 0) break;
                            if (InventoryInfo.AvailableQuantity <= Order_qty)
                            {
                                Order_qty -= InventoryInfo.AvailableQuantity;
                                InventoryInfo.OutboundQuantity += InventoryInfo.AvailableQuantity;
                                Dt_SupplyTask supplyTask = new Dt_SupplyTask()
                                {
                                    WarehouseCode = InventoryInfo.WarehouseCode,
                                    BatchNo = InventoryInfo.BatchNo,
                                    MaterielName = InventoryInfo.MaterielName,
                                    MaterielCode = InventoryInfo.MaterielCode,
                                    MaterielSpec = InventoryInfo.MaterielSpec,
                                    TaskStatus = SupplyStatusEnum.NewOut.ObjToInt(),
                                    TaskType = outorder.order_type == "1" ? TaskTypeEnum.Out.ObjToInt() : TaskTypeEnum.InReturn.ObjToInt(),
                                    CreateDate = DateTime.Now,
                                    Creater = App.User.UserName ?? "System",
                                    LocationCode = InventoryInfo.LocationCode,
                                    OrderNo = entityOrder.Out_no,
                                    StockQuantity = InventoryInfo.AvailableQuantity,
                                    SupplyQuantity = 0,
                                    Remark = "出库"
                                };
                                supplyTasks.Add(supplyTask);
                                InventoryInfo.AvailableQuantity = 0;
                            }
                            else
                            {
                                InventoryInfo.AvailableQuantity -= Order_qty;
                                InventoryInfo.OutboundQuantity += Order_qty;
                                Dt_SupplyTask supplyTask = new Dt_SupplyTask()
                                {
                                    WarehouseCode = InventoryInfo.WarehouseCode,
                                    BatchNo = InventoryInfo.BatchNo,
                                    MaterielName = InventoryInfo.MaterielName,
                                    MaterielCode = InventoryInfo.MaterielCode,
                                    MaterielSpec = InventoryInfo.MaterielSpec,
                                    TaskStatus = SupplyStatusEnum.NewOut.ObjToInt(),
                                    TaskType = outorder.order_type == "1" ? TaskTypeEnum.Out.ObjToInt() : TaskTypeEnum.InReturn.ObjToInt(),
                                    CreateDate = DateTime.Now,
                                    Creater = App.User.UserName ?? "System",
                                    LocationCode = InventoryInfo.LocationCode,
                                    OrderNo = entityOrder.Out_no,
                                    StockQuantity = Order_qty,
                                    SupplyQuantity = 0,
                                    Remark = "出库"
                                };
                                supplyTasks.Add(supplyTask);
                                Order_qty = 0;
                            }
                            inventoryInfosUp.Add(InventoryInfo);
                        }
                        batchesUp.Add(inventory_Batch);
                    }
                    _unitOfWorkManage.BeginTran();
                    _supplyTaskService.AddData(supplyTasks);
                    _inventory_BatchServices.UpdateData(batchesUp);
                    _inventoryInfoService.UpdateData(inventoryInfosUp);
                    BaseDal.Db.CopyNew().InsertNav(entityOrder).Include(x => x.Details).ExecuteCommand();
                    _unitOfWorkManage.CommitTran();
                    #endregion
                }
                #endregion
                else
                {
                    string WareCodeDJ = WarehouseEnum.大件库.ObjToInt().ToString("000");
                    string WareCodeLK = WarehouseEnum.立库.ObjToInt().ToString("000");
                    #region 创建大件库、立库出库头表
                    var entityOrder = new Dt_DeliveryOrder
                    {
                        Out_no = outorder.order_no,
                        Out_type = outorder.order_type,
                        OutStatus = "新建",
                        Client_name = outorder.client_name,
                        Account_time = outorder.account_time,
                        Client_no = outorder.client_no,
                        Warehouse_no = WareCodeDJ,
                        Details = new List<Dt_DeliveryOrderDetail>()
                    };
                    var entityOrderLK = new Dt_DeliveryOrder
                    {
                        Out_no = outorder.order_no,
                        Out_type = outorder.order_type,
                        OutStatus = "新建",
                        Client_name = outorder.client_name,
                        Account_time = outorder.account_time,
                        Client_no = outorder.client_no,
                        Warehouse_no = WareCodeLK,
                        Details = new List<Dt_DeliveryOrderDetail>()
                    };
                    #endregion
                    #region 查找库存
                    List<Dt_Inventory_Batch> batchesUp = new List<Dt_Inventory_Batch>();
                    List<Dt_InventoryInfo> inventoryInfosUp = new List<Dt_InventoryInfo>();
                    List<Dt_SupplyTask> supplyTasks = new List<Dt_SupplyTask>();
                    List<Dt_MaterielInfo> materielInfos = _materielInfoService.Repository.QueryData(x => outorder.details.Select(e => e.goods_no).Contains(x.MaterielCode));
                    var inventory_Batchs = _inventory_BatchServices.Repository.QueryData(x => outorder.details.Select(e => e.goods_no).Contains(x.MaterielCode));
                    var InventoryInfos = _inventoryInfoService.Repository.QueryData(x => outorder.details.Select(e => e.goods_no).Contains(x.MaterielCode) && x.StockStatus == StockStatusEmun.入库完成.ObjToInt() && x.AvailableQuantity > 0 && (x.WarehouseCode == WareCodeDJ || x.WarehouseCode == WareCodeLK));
                    foreach (var detail in outorder.details)
                    {
                        #region 查询库存批次和库存
                        Dt_Inventory_Batch? inventory_Batch = inventory_Batchs.Where(x => x.MaterielCode == detail.goods_no && x.BatchNo == detail.batch_num).FirstOrDefault();
                        if (inventory_Batch == null) throw new Exception($"未找到出库单号【{outorder.order_no}】中物料编号【{detail.goods_no}】物料批次【{detail.batch_num}】的库存批次信息");
                        if (inventory_Batch.AvailableQuantity < detail.order_qty) throw new Exception($"出库单号【{outorder.order_no}】中物料编号【{detail.goods_no}】物料批次【{detail.batch_num}】的库存批次信息可用数量不足");
                        inventory_Batch.AvailableQuantity -= detail.order_qty;
                        inventory_Batch.OutboundQuantity += detail.order_qty;
                        List<Dt_InventoryInfo> dt_InventoryInfos = InventoryInfos.Where(x => x.MaterielCode == inventory_Batch.MaterielCode && x.BatchNo == inventory_Batch.BatchNo).ToList();
                        if (dt_InventoryInfos.Count < 1) throw new Exception($"出库单号【{outorder.order_no}】中物料编号【{detail.goods_no}】物料批次【{detail.batch_num}】的可用库存不足");
                        #endregion
                        #region 按出库策略查找库存
                        if (tactics.SelectTactice == TacticsEnum.ComeOutonFirstTime.ObjToInt())
                            dt_InventoryInfos = dt_InventoryInfos.OrderBy(x => x.ValidityPeriod).ToList();
                        else
                            dt_InventoryInfos = dt_InventoryInfos.OrderBy(x => x.InDate).ToList();
                        #endregion
                        var Order_qty = Math.Abs(detail.order_qty);//出库单数量
                        #region 根据物料编码查询物料信息
                        Dt_MaterielInfo? materielInfo = materielInfos.Where(x => x.MaterielCode == detail.goods_no).FirstOrDefault();
                        if (materielInfo == null) throw new Exception($"未找到药品编码【{detail.goods_no}】的信息");
                        if (!Enum.IsDefined(typeof(MaterielSourceTypeEnum), materielInfo.MaterielSourceType))
                            throw new Exception($"请设置药品编号【{detail.goods_no}】的属性分类");
                        #endregion
                        #region 大件
                        if (materielInfo.MaterielSourceType == MaterielSourceTypeEnum.PurchasePart)//如果物料是大件
                        {
                            #region 添加出库详情
                            Dt_DeliveryOrderDetail orderDetail = new Dt_DeliveryOrderDetail()
                            {
                                Reservoirarea = entityOrder.Warehouse_no,
                                Goods_no = detail.goods_no,
                                Order_qty = detail.order_qty,
                                Batch_num = detail.batch_num,
                                Exp_date = detail.exp_date,
                                OotDetailStatus = "新建",
                                Order_Outqty = 0,
                                Status = 2
                            };
                            entityOrder.Details.Add(orderDetail);
                            #endregion
 
                            #region 计算库存、添加出库任务
                            foreach (var InventoryInfo in dt_InventoryInfos)
                            {
                                if (Order_qty <= 0) break;
                                if (InventoryInfo.AvailableQuantity < Order_qty)
                                {
                                    Order_qty -= InventoryInfo.AvailableQuantity;
                                    InventoryInfo.OutboundQuantity += InventoryInfo.AvailableQuantity;
                                    Dt_SupplyTask supplyTask = new Dt_SupplyTask()
                                    {
                                        WarehouseCode = InventoryInfo.WarehouseCode,
                                        BatchNo = InventoryInfo.BatchNo,
                                        MaterielName = InventoryInfo.MaterielName,
                                        MaterielCode = InventoryInfo.MaterielCode,
                                        MaterielSpec = InventoryInfo.MaterielSpec,
                                        TaskStatus = SupplyStatusEnum.NewOut.ObjToInt(),
                                        TaskType = outorder.order_type == "1" ? TaskTypeEnum.Out.ObjToInt() : TaskTypeEnum.InReturn.ObjToInt(),
                                        CreateDate = DateTime.Now,
                                        Creater = App.User.UserName ?? "System",
                                        LocationCode = InventoryInfo.LocationCode,
                                        OrderNo = entityOrder.Out_no,
                                        StockQuantity = InventoryInfo.AvailableQuantity,
                                        SupplyQuantity = 0,
                                        Remark = "出库"
                                    };
                                    supplyTasks.Add(supplyTask);
                                    InventoryInfo.AvailableQuantity = 0;
                                }
                                else
                                {
                                    InventoryInfo.AvailableQuantity -= Order_qty;
                                    InventoryInfo.OutboundQuantity += Order_qty;
                                    Dt_SupplyTask supplyTask = new Dt_SupplyTask()
                                    {
                                        WarehouseCode = InventoryInfo.WarehouseCode,
                                        BatchNo = InventoryInfo.BatchNo,
                                        MaterielName = InventoryInfo.MaterielName,
                                        MaterielCode = InventoryInfo.MaterielCode,
                                        MaterielSpec = InventoryInfo.MaterielSpec,
                                        TaskStatus = SupplyStatusEnum.NewOut.ObjToInt(),
                                        TaskType = outorder.order_type == "1" ? TaskTypeEnum.Out.ObjToInt() : TaskTypeEnum.InReturn.ObjToInt(),
                                        CreateDate = DateTime.Now,
                                        Creater = App.User.UserName ?? "System",
                                        LocationCode = InventoryInfo.LocationCode,
                                        OrderNo = entityOrder.Out_no,
                                        StockQuantity = Order_qty,
                                        SupplyQuantity = 0,
                                        Remark = "出库"
                                    };
                                    supplyTasks.Add(supplyTask);
                                    Order_qty = 0;
                                }
                            }
                            #endregion
                            inventoryInfosUp.AddRange(dt_InventoryInfos);
                            batchesUp.Add(inventory_Batch);
                            //_inventory_BatchServices.Repository.UpdateData(inventory_Batch);
                            //_inventoryInfoService.Repository.UpdateData(dt_InventoryInfos);
                            //_supplyTaskService.AddData(supplyTasks);
                        }
                        #endregion
                        else
                        {
                            if (materielInfo.BoxQty < 1) throw new Exception($"请设置药品编号【{detail.goods_no}】的箱规数量");
                            Dt_DeliveryOrderDetail orderDetail = null;
                            Dt_DeliveryOrderDetail orderDetailLK = null;
                            var ys = Order_qty % materielInfo.BoxQty; //不能整除箱规的散件数 
                            var xs = (int)(Order_qty / materielInfo.BoxQty);//保留整数
                            #region 散件优先分配立库
                            if (ys > 0)
                            {
                                orderDetailLK = new Dt_DeliveryOrderDetail()
                                {
                                    Reservoirarea = entityOrderLK.Warehouse_no,
                                    Goods_no = detail.goods_no,
                                    Order_qty = ys,
                                    Batch_num = detail.batch_num,
                                    Exp_date = detail.exp_date,
                                    OotDetailStatus = "新建",
                                    Order_Outqty = 0,
                                    Status = 0
                                };
                            }
                            #endregion
 
                            #region 整件优先分配大件库,计划库存,添加出库任务
                            foreach (var item in dt_InventoryInfos.Where(x => x.WarehouseCode == WareCodeDJ))
                            {
                                if (xs <= 0 || item.AvailableQuantity <= 0) break;
                                decimal outqty = 0;
                                while (item.AvailableQuantity > 0 && xs > 0)
                                {
                                    xs--;
                                    if (orderDetail == null)
                                    {
                                        orderDetail = new Dt_DeliveryOrderDetail()
                                        {
                                            Reservoirarea = entityOrder.Warehouse_no,
                                            Goods_no = detail.goods_no,
                                            Order_qty = materielInfo.BoxQty,
                                            Batch_num = detail.batch_num,
                                            Exp_date = detail.exp_date,
                                            OotDetailStatus = "新建",
                                            Order_Outqty = 0,
                                            Status = 2
                                        };
                                        item.AvailableQuantity -= materielInfo.BoxQty;
                                        item.OutboundQuantity += materielInfo.BoxQty;
                                        outqty += materielInfo.BoxQty;
                                    }
                                    else
                                    {
                                        orderDetail.Order_qty += materielInfo.BoxQty;
                                        item.AvailableQuantity -= materielInfo.BoxQty;
                                        item.OutboundQuantity += materielInfo.BoxQty;
                                        outqty += materielInfo.BoxQty;
                                    }
                                }
                                Dt_SupplyTask supplyTask = new Dt_SupplyTask()
                                {
                                    WarehouseCode = item.WarehouseCode,
                                    BatchNo = item.BatchNo,
                                    MaterielName = item.MaterielName,
                                    MaterielCode = item.MaterielCode,
                                    MaterielSpec = item.MaterielSpec,
                                    TaskStatus = SupplyStatusEnum.NewOut.ObjToInt(),
                                    TaskType = outorder.order_type == "1" ? TaskTypeEnum.Out.ObjToInt() : TaskTypeEnum.InReturn.ObjToInt(),
                                    CreateDate = DateTime.Now,
                                    Creater = App.User.UserName ?? "System",
                                    LocationCode = item.LocationCode,
                                    OrderNo = entityOrder.Out_no,
                                    StockQuantity = outqty,
                                    SupplyQuantity = 0,
                                    Remark = "出库"
                                };
                                supplyTasks.Add(supplyTask);
                                inventoryInfosUp.Add(item);
                                //_inventoryInfoService.Repository.UpdateData(item);
                            }
                            #endregion
 
                            #region 分配完大件库如果还有箱数,再选择分配立库
                            if (xs > 0)
                            {
                                if (orderDetailLK == null)
                                {
                                    orderDetailLK = new Dt_DeliveryOrderDetail()
                                    {
                                        Reservoirarea = entityOrderLK.Warehouse_no,
                                        Goods_no = detail.goods_no,
                                        Order_qty = xs * materielInfo.BoxQty,
                                        Batch_num = detail.batch_num,
                                        Exp_date = detail.exp_date,
                                        OotDetailStatus = "新建",
                                        Order_Outqty = 0,
                                        Status = 0
                                    };
                                }
                                else
                                {
                                    orderDetailLK.Order_qty += xs * materielInfo.BoxQty;
                                }
                            }
                            #endregion
 
                            #region 立库出库单,修改立库库存,添加立库出库任务
                            if (orderDetailLK != null)
                            {
                                #region 添加出库任务、修改库存信息
                                Dt_InventoryInfo? inventoryInfo = dt_InventoryInfos.FirstOrDefault(x => x.WarehouseCode == WareCodeLK);
                                if (inventoryInfo == null)
                                    throw new Exception($"出库单【{outorder.order_no}】详情存在散件,物料编号【{detail.goods_no}】物料批次【{detail.batch_num}】所需数量【{Convert.ToInt32(orderDetailLK.Order_qty)}】请人工调拨补货入立库");
                                inventoryInfo.AvailableQuantity -= orderDetailLK.Order_qty;
                                inventoryInfo.OutboundQuantity += orderDetailLK.Order_qty;
                                Dt_SupplyTask supplyTask = new Dt_SupplyTask()
                                {
                                    WarehouseCode = inventoryInfo.WarehouseCode,
                                    BatchNo = inventoryInfo.BatchNo,
                                    MaterielName = inventoryInfo.MaterielName,
                                    MaterielCode = inventoryInfo.MaterielCode,
                                    MaterielSpec = inventoryInfo.MaterielSpec,
                                    TaskStatus = SupplyStatusEnum.NewOut.ObjToInt(),
                                    TaskType = outorder.order_type == "1" ? TaskTypeEnum.Out.ObjToInt() : TaskTypeEnum.InReturn.ObjToInt(),
                                    CreateDate = DateTime.Now,
                                    Creater = App.User.UserName ?? "System",
                                    LocationCode = inventoryInfo.LocationCode,
                                    OrderNo = entityOrder.Out_no,
                                    StockQuantity = orderDetailLK.Order_qty,
                                    SupplyQuantity = 0,
                                    Remark = "出库"
                                };
                                supplyTasks.Add(supplyTask);
                                #endregion
                                //_inventoryInfoService.Repository.UpdateData(inventoryInfo);
                                inventoryInfosUp.Add(inventoryInfo);
                                entityOrderLK.Details.Add(orderDetailLK);
                            }
                            #endregion
                            if (orderDetail != null) entityOrder.Details.Add(orderDetail);
                            batchesUp.Add(inventory_Batch);
                            //_inventory_BatchServices.Repository.UpdateData(inventory_Batch);
                            //_supplyTaskService.AddData(supplyTasks);
                        }
                    }
                    try
                    {
                        _unitOfWorkManage.BeginTran();
                        _supplyTaskService.AddData(supplyTasks);
                        _inventory_BatchServices.UpdateData(batchesUp);
                        _inventoryInfoService.UpdateData(inventoryInfosUp);
                        if (entityOrder.Details.Count > 0)
                            BaseDal.Db.CopyNew().InsertNav(entityOrder).Include(x => x.Details).ExecuteCommand();
                        if (entityOrderLK.Details.Count > 0)
                            BaseDal.Db.CopyNew().InsertNav(entityOrderLK).Include(x => x.Details).ExecuteCommand();
                        _unitOfWorkManage.CommitTran();
                    }
                    catch (Exception ex)
                    {
                        _unitOfWorkManage.RollbackTran();
                    }
                    #endregion
                }
                webResponseContent.OK();
            }
            catch (Exception ex)
            {
                _messageInfoService.AddMessageInfo(MessageGroupByEnum.OutOrderAlarm, (outorder.order_type == "3" ? "入库退货" : "正常出库") + $":单号【{outorder.order_no}】", ex.Message);
                webResponseContent.Error(ex.Message);
            }
            return webResponseContent;
        }
        #endregion
 
        #region 创建盘亏出库单
        public WebResponseContent CreateCheckOutOrder(UpstramOutOrderInfo order)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                string WareCodeMJ = WarehouseEnum.麻精库.ObjToInt().ToString("000");
                string WareCodeLD = WarehouseEnum.冷冻库.ObjToInt().ToString("000");
                List<Dt_SupplyTask_Hty> supplyTask_Hties = new List<Dt_SupplyTask_Hty>();
                List<Dt_Inventory_Batch> batchesUp = new List<Dt_Inventory_Batch>();
                List<Dt_InventoryInfo> infosUp = new List<Dt_InventoryInfo>();
                var codes = order.details.Select(x => x.goods_no).ToList();
                #region 特殊库房
                if (order.warehouse_no == WareCodeMJ || order.warehouse_no == WareCodeLD)
                {
                    List<Dt_Inventory_Batch> inventory_Batchs = _inventory_BatchServices.Repository.QueryData(x => codes.Contains(x.MaterielCode));
                    List<Dt_InventoryInfo> _InventoryInfos = _inventoryInfoService.Repository.QueryData(x => codes.Contains(x.MaterielCode));
                    #region 库存、库存批次平账
                    foreach (var item in order.details)
                    {
                        //找库存批次信息
                        Dt_Inventory_Batch inventory_Batch = inventory_Batchs.Where(x => x.MaterielCode == item.goods_no && x.BatchNo == item.batch_num).First();
                        var Qty = Math.Abs(inventory_Batch.SupplyQuantity);
                        if (Qty != item.order_qty) throw new Exception($"盘亏出库单【{order.order_no}】物料编号【{item.goods_no}】物料批次【{item.batch_num}】的盘亏数量有误");
                        //找所有库存
                        List<Dt_InventoryInfo> inventoryInfos = _InventoryInfos.Where(x => x.MaterielCode == inventory_Batch.MaterielCode && x.BatchNo == inventory_Batch.BatchNo).ToList();
                        foreach (var inventoryInfo in inventoryInfos)
                        {
                            #region 添加盘亏出库任务
                            if (inventoryInfo.StockQuantity != inventoryInfo.SupplyQuantity)
                            {
                                Dt_SupplyTask_Hty supplyTask_Hty = new Dt_SupplyTask_Hty()
                                {
                                    WarehouseCode = inventoryInfo.WarehouseCode,
                                    OperateType = OperateTypeEnum.自动完成.ToString(),
                                    InsertTime = DateTime.Now,
                                    TaskStatus = SupplyStatusEnum.OutFinish.ObjToInt(),
                                    BatchNo = inventoryInfo.BatchNo,
                                    MaterielName = inventoryInfo.MaterielName,
                                    MaterielCode = inventoryInfo.MaterielCode,
                                    MaterielSpec = inventoryInfo.MaterielSpec,
                                    TaskType = TaskTypeEnum.ChenckOut.ObjToInt(),
                                    CreateDate = DateTime.Now,
                                    Creater = App.User.UserName,
                                    LocationCode = inventoryInfo.LocationCode,
                                    OrderNo = order.order_no,
                                    StockQuantity = Math.Abs(inventoryInfo.SupplyQuantity),
                                    SupplyQuantity = 0,
                                    Remark = "盘亏入库"
                                };
                                //_supplyTaskHtyService.AddData(supplyTask_Hty);
                                supplyTask_Hties.Add(supplyTask_Hty);
                            }
                            #endregion
                            inventoryInfo.StockQuantity += inventoryInfo.SupplyQuantity;
                            inventoryInfo.AvailableQuantity = inventoryInfo.StockQuantity;
                            inventoryInfo.SupplyQuantity = 0;
                            inventoryInfo.StockStatus = StockStatusEmun.入库完成.ObjToInt();
                        }
                        inventory_Batch.StockQuantity += inventory_Batch.SupplyQuantity;
                        inventory_Batch.AvailableQuantity = inventory_Batch.StockQuantity;
                        inventory_Batch.SupplyQuantity = 0;
                        infosUp.AddRange(inventoryInfos);
                        batchesUp.Add(inventory_Batch);
                        //_inventoryInfoService.UpdateData(inventoryInfos);
                        //_inventory_BatchServices.UpdateData(inventory_Batch);
                    }
                    #endregion
 
                    #region 创建盘点单
                    var entityOrder = new Dt_DeliveryOrder
                    {
                        Out_no = order.order_no,
                        Out_type = order.order_type,
                        Client_no = order.client_no,
                        Client_name = order.client_name,
                        OutStatus = "已完成",
                        Account_time = order.account_time,
                        Warehouse_no = order.warehouse_no,
                        Details = order.details.Select(d => new Dt_DeliveryOrderDetail
                        {
                            Goods_no = d.goods_no,
                            Order_qty = Math.Abs(d.order_qty),
                            Order_Outqty = Math.Abs(d.order_qty),
                            Batch_num = d.batch_num,
                            Exp_date = d.exp_date,
                            Reservoirarea = order.warehouse_no,
                            OotDetailStatus = "已完成",
                            Status = 2,
                        }).ToList()
                    };
                    _unitOfWorkManage.BeginTran();
                    _supplyTaskHtyService.AddData(supplyTask_Hties);
                    _inventoryInfoService.UpdateData(infosUp);
                    _inventory_BatchServices.UpdateData(batchesUp);
                    BaseDal.Db.InsertNav(entityOrder).Include(it => it.Details).ExecuteCommand();
                    _unitOfWorkManage.CommitTran();
                    #endregion
                }
                #endregion
                else
                {
                    List<Dt_DeliveryOrder> deliveryOrdersAdd = new List<Dt_DeliveryOrder>();
                    string WareCodeLK = WarehouseEnum.立库.ObjToInt().ToString("000");
                    string WareCodeDJ = WarehouseEnum.大件库.ObjToInt().ToString("000");
                    List<Dt_Inventory_Batch> inventory_Batchs = _inventory_BatchServices.Repository.QueryData(x => codes.Contains(x.MaterielCode));
                    List<Dt_InventoryInfo> _InventoryInfos = _inventoryInfoService.Repository.QueryData(x => codes.Contains(x.MaterielCode));
                    foreach (var item in order.details)
                    {
                        //找库存批次信息
                        Dt_Inventory_Batch inventory_Batch = inventory_Batchs.Where(x => x.MaterielCode == item.goods_no && x.BatchNo == item.batch_num).First();
                        var Qty = Math.Abs(inventory_Batch.SupplyQuantity);
                        if (Qty != item.order_qty) throw new Exception($"盘亏出库单【{order.order_no}】物料编号【{item.goods_no}】物料批次【{item.batch_num}】的盘亏数量有误");
                        //找所有库存
                        List<Dt_InventoryInfo> inventoryInfos = _InventoryInfos.Where(x => x.MaterielCode == inventory_Batch.MaterielCode && x.BatchNo == inventory_Batch.BatchNo).ToList();
                        //获取立库盘点差异数.。。。。。。。。。。。。
                        var inventoryLK = inventoryInfos.Where(x => x.WarehouseCode == WareCodeLK).First();
                        //var LkQty = Math.Abs(inventoryLK.SupplyQuantity);
                        var LkQty = inventoryLK.SupplyQuantity;
                        //获取大件库盘点差异数
                        var inventoryDJ = inventoryInfos.Where(x => x.WarehouseCode == WareCodeDJ).ToList();
                        //var DJQty = Math.Abs(inventoryDJ.Sum(x => x.SupplyQuantity));
                        var DJQty = inventoryDJ.Sum(x => x.SupplyQuantity);
                        var count = Math.Abs(LkQty + DJQty);
                        if (count != Qty) throw new Exception($"【{order.order_no}】物料编号【{item.goods_no}】物料批次【{item.batch_num}】的物料信息与物料批次信息盘亏数量不符");
                        if (LkQty == 0)//立库无差异
                        {
                            #region 库存、库存批次平账
                            foreach (var inventoryInfo in inventoryInfos)
                            {
                                #region 添加盘亏出库任务
                                if (inventoryInfo.StockQuantity != inventoryInfo.SupplyQuantity)
                                {
                                    Dt_SupplyTask_Hty supplyTask_Hty = new Dt_SupplyTask_Hty()
                                    {
                                        WarehouseCode = inventoryInfo.WarehouseCode,
                                        OperateType = OperateTypeEnum.自动完成.ToString(),
                                        InsertTime = DateTime.Now,
                                        TaskStatus = SupplyStatusEnum.OutFinish.ObjToInt(),
                                        BatchNo = inventoryInfo.BatchNo,
                                        MaterielName = inventoryInfo.MaterielName,
                                        MaterielCode = inventoryInfo.MaterielCode,
                                        MaterielSpec = inventoryInfo.MaterielSpec,
                                        TaskType = TaskTypeEnum.ChenckOut.ObjToInt(),
                                        CreateDate = DateTime.Now,
                                        Creater = App.User.UserName,
                                        LocationCode = inventoryInfo.LocationCode,
                                        OrderNo = order.order_no,
                                        StockQuantity = Math.Abs(inventoryInfo.SupplyQuantity),
                                        SupplyQuantity = 0,
                                        Remark = "盘亏入库"
                                    };
                                    //_supplyTaskHtyService.AddData(supplyTask_Hty);
                                    supplyTask_Hties.Add(supplyTask_Hty);
                                }
                                #endregion
                                inventoryInfo.StockQuantity += inventoryInfo.SupplyQuantity;
                                inventoryInfo.AvailableQuantity = inventoryInfo.StockQuantity;
                                inventoryInfo.SupplyQuantity = 0;
                                inventoryInfo.StockStatus = StockStatusEmun.入库完成.ObjToInt();
                            }
                            //_inventoryInfoService.UpdateData(inventoryInfos);
                            inventory_Batch.StockQuantity += inventory_Batch.SupplyQuantity;
                            inventory_Batch.AvailableQuantity = inventory_Batch.StockQuantity;
                            inventory_Batch.SupplyQuantity = 0;
                            //_inventory_BatchServices.UpdateData(inventory_Batch);
                            infosUp.AddRange(inventoryInfos);
                            batchesUp.Add(inventory_Batch);
                            #endregion
 
                            #region 创建大件库盘点单
                            var entityOrder = new Dt_DeliveryOrder
                            {
                                Out_no = order.order_no,
                                Out_type = order.order_type,
                                Client_no = order.client_no,
                                Account_time = order.account_time,
                                OutStatus = "已完成",
                                Client_name = order.client_name,
                                Warehouse_no = WareCodeDJ,
                                Details = order.details.Select(d => new Dt_DeliveryOrderDetail
                                {
                                    Goods_no = d.goods_no,
                                    Order_qty = Math.Abs(d.order_qty),
                                    Order_Outqty = Math.Abs(d.order_qty),
                                    Batch_num = d.batch_num,
                                    Exp_date = d.exp_date,
                                    Reservoirarea = WareCodeDJ,
                                    OotDetailStatus = "已完成",
                                    Status = 2,
                                }).ToList()
                            };
                            //Db.InsertNav(entityOrder).Include(it => it.Details).ExecuteCommand();
                            //Repository.AddData(entityOrder);
                            deliveryOrdersAdd.Add(entityOrder);
                            #endregion
                        }
                        else
                        {
                            #region 大件库库存平账
                            inventoryInfos = inventoryInfos.Where(x => x.WarehouseCode == WareCodeDJ).ToList();
                            foreach (var inventoryInfo in inventoryInfos)
                            {
                                #region 添加盘亏出库任务
                                if (inventoryInfo.StockQuantity != inventoryInfo.SupplyQuantity)
                                {
                                    Dt_SupplyTask_Hty supplyTask_Hty = new Dt_SupplyTask_Hty()
                                    {
                                        WarehouseCode = inventoryInfo.WarehouseCode,
                                        OperateType = OperateTypeEnum.自动完成.ToString(),
                                        InsertTime = DateTime.Now,
                                        TaskStatus = SupplyStatusEnum.OutFinish.ObjToInt(),
                                        BatchNo = inventoryInfo.BatchNo,
                                        MaterielName = inventoryInfo.MaterielName,
                                        MaterielCode = inventoryInfo.MaterielCode,
                                        MaterielSpec = inventoryInfo.MaterielSpec,
                                        TaskType = TaskTypeEnum.ChenckOut.ObjToInt(),
                                        CreateDate = DateTime.Now,
                                        Creater = App.User.UserName,
                                        LocationCode = inventoryInfo.LocationCode,
                                        OrderNo = order.order_no,
                                        StockQuantity = Math.Abs(inventoryInfo.SupplyQuantity),
                                        SupplyQuantity = 0,
                                        Remark = "盘亏出库"
                                    };
                                    //_supplyTaskHtyService.AddData(supplyTask_Hty);
                                    supplyTask_Hties.Add(supplyTask_Hty);
                                }
                                #endregion
                                inventoryInfo.StockQuantity += inventoryInfo.SupplyQuantity;
                                inventoryInfo.AvailableQuantity = inventoryInfo.StockQuantity;
                                inventoryInfo.SupplyQuantity = 0;
                                inventoryInfo.StockStatus = StockStatusEmun.入库完成.ObjToInt();
                            }
                            //_inventoryInfoService.UpdateData(inventoryInfos);
                            infosUp.AddRange(inventoryInfos);
                            #endregion
 
                            #region 创建大件库盘点单
                            if (DJQty != 0)
                            {
                                var cabinOrder = new Dt_DeliveryOrder
                                {
                                    Out_no = order.order_no,
                                    Out_type = order.order_type,
                                    Client_name = order.client_name,
                                    Account_time = order.account_time,
                                    OutStatus = "已完成",
                                    Client_no = order.client_no,
                                    Warehouse_no = WareCodeDJ,
                                    Details = order.details.Select(d => new Dt_DeliveryOrderDetail
                                    {
                                        Goods_no = d.goods_no,
                                        Order_qty = DJQty,
                                        Order_Outqty = DJQty,
                                        Batch_num = d.batch_num,
                                        Exp_date = d.exp_date,
                                        Reservoirarea = WareCodeDJ,
                                        OotDetailStatus = "已完成",
                                        Status = 2,
                                    }).ToList()
                                };
                                //Repository.AddData(cabinOrder);
                                //Db.InsertNav(cabinOrder).Include(it => it.Details).ExecuteCommand();
                                deliveryOrdersAdd.Add(cabinOrder);
                            }
                            #endregion
                            //这里
                            #region 创建立库盘点单
                            var entityOrder = new Dt_DeliveryOrder
                            {
                                Out_no = order.order_no,
                                Out_type = order.order_type,
                                Client_no = order.client_no,
                                Account_time = order.account_time,
                                OutStatus = "新建",
                                Client_name = order.client_name,
                                Warehouse_no = WareCodeLK,
                                Details = order.details.Select(d => new Dt_DeliveryOrderDetail
                                {
                                    Goods_no = d.goods_no,
                                    Order_qty = Math.Abs(LkQty), //给下游WCS的是要整数
                                    Batch_num = d.batch_num,
                                    Exp_date = d.exp_date,
                                    Reservoirarea = WareCodeLK,
                                    OotDetailStatus = "新建",
                                    Status = 0,
                                }).ToList()
                            };
                            //Db.InsertNav(entityOrder).Include(it => it.Details).ExecuteCommand();
                            //Repository.AddData(entityOrder);
                            deliveryOrdersAdd.Add(entityOrder);
                            #endregion
                        }
                        //return WebResponseContent.Instance.OK("成功");
                    }
                    _unitOfWorkManage.BeginTran();
                    _supplyTaskHtyService.AddData(supplyTask_Hties);
                    _inventoryInfoService.UpdateData(infosUp);
                    _inventory_BatchServices.UpdateData(batchesUp);
                    BaseDal.Db.InsertNav(deliveryOrdersAdd).Include(it => it.Details).ExecuteCommand();
                    _unitOfWorkManage.CommitTran();
                }
                return WebResponseContent.Instance.OK("成功");
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                content.Error(ex.Message);
            }
            return content;
        }
        #endregion
 
 
        //盘点 拿整个批次信息表的商品批号和商品编号来进行盘点
        public WebResponseContent InventoryGood(string batchNo, string goodsNo)
        {
            var response = new WebResponseContent();
            try
            {
                // 1️⃣ 查找指定批次与物料的库存信息
                var batchInfo = BaseDal.Db.CopyNew().Queryable<Dt_Inventory_Batch>()
                    .Where(x => x.BatchNo == batchNo && x.MaterielCode == goodsNo)
                    .First();
 
                if (batchInfo == null)
                    return response.Error($"未找到该物料 [{goodsNo}] 批次 [{batchNo}] 的库存信息");
 
                // 2️⃣ 组装请求 DTO(完全符合接口文档结构)
                var ediDto = new TowcsDto.ToediOutInfo
                {
                    customerCode = "905",
                    materialCode = "YY",                              // 物料类型CODE
                    externalOrderNo = $"PDCK-{batchInfo.Id}",          // 外部出库单号
                    outOrderType = "20",                               // 盘点出库单
                    priority = 1,
                    Is_cancel = 0,
                    details = new List<TowcsDto.ToeOutdiInDetail>
            {
                new TowcsDto.ToeOutdiInDetail
                {
                    batchNo = batchInfo.BatchNo,
                    productCode = batchInfo.MaterielCode,
                    productName = batchInfo.MaterielName,
                    productSpecifications = batchInfo.MaterielSpec,
                    quantity = (int)batchInfo.SupplyQuantity,
                    //stocktakingDetails = new List<TowcsDto.ToOutediInStock>
                    //{
                    //    // 盘点明细可根据实际托盘拆分;此处示例仅1条
                    //    new TowcsDto.ToOutediInStock
                    //    {
                    //        palletCode = "FC00001",
                    //        quantity = batchInfo.SupplyQuantity.ToString()
                    //    }
                    //}
                }
            }
                };
 
                // 3️⃣ 调用接口
                string url = "http://172.16.1.2:9357/file-admin/api/out/ediOut";
                var result = HttpHelper.Post(url, ediDto.ToJsonString());
                var resp = JsonConvert.DeserializeObject<TowcsDto.TowcsResponse<object>>(result);
 
                // 4️⃣ 响应处理
                if (resp == null)
                    return response.Error("WCS 无响应");
                if (resp.code != "0")
                    return response.Error($"WCS返回失败: {resp.msg}");
 
                return response.OK("盘点出库下发成功");
            }
            catch (Exception ex)
            {
                return response.Error("盘点失败:" + ex.Message);
            }
        }
 
 
        /// <summary>
        /// 查询出库单列表
        /// </summary>
        /// <param name="saveModel"></param>
        /// <returns></returns>
        public WebResponseContent GetDeliveryOrders(SaveModel saveModel)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                int pageNo = saveModel.MainData["pageNo"].ObjToInt();
                string warehouseCode = saveModel.MainData["warehouseId"].ToString();
                string orderNo = saveModel.MainData["orderNo"].ToString();
                List<Dt_DeliveryOrder> dt_ReceiveOrders = new List<Dt_DeliveryOrder>();
                if (string.IsNullOrEmpty(orderNo))
                {
                    dt_ReceiveOrders = Db.Queryable<Dt_DeliveryOrder>().Where(x => (x.OutStatus == "新建" || x.OutStatus == "开始") && x.Warehouse_no == warehouseCode && x.Out_type != "20").Includes(x => x.Details).OrderByDescending(x => x.CreateDate).ToPageList(pageNo, 5);
                }
                else
                {
                    dt_ReceiveOrders = Db.Queryable<Dt_DeliveryOrder>().Where(x => (x.Out_no.Contains(orderNo) || x.Client_no.Contains(orderNo)) && (x.OutStatus == "新建" || x.OutStatus == "开始") && x.Out_type != "20" && x.Warehouse_no == warehouseCode).OrderByDescending(x => x.CreateDate).Includes(x => x.Details).ToPageList(pageNo, 5);
                }
 
                content.OK(data: dt_ReceiveOrders);
            }
            catch (Exception)
            {
 
                throw;
            }
            return content;
        }
 
        /// <summary>
        /// 查询出库/盘点单详情 看出库单明细。
        /// </summary>
        /// <param name="pageNo"></param>
        /// <param name="orderNo"></param>
        /// <param name="isPick"></param>
        /// <returns></returns>
        public WebResponseContent GetDeliveryOrderDetail(int pageNo, string orderNo, bool isPick)
        {
            WebResponseContent content = new WebResponseContent();
            Dt_DeliveryOrder cabinOrder = new Dt_DeliveryOrder();
            if (isPick)
                cabinOrder = Db.Queryable<Dt_DeliveryOrder>().Includes(x => x.Details).First(x => x.Out_no == orderNo && x.Out_type == "20");
            else
                cabinOrder = Db.Queryable<Dt_DeliveryOrder>().Includes(x => x.Details).First(x => x.Out_no == orderNo && x.Out_type != "20");
            //List<Dt_DeliveryOrderDetail>? cabinOrderDetails = cabinOrder.Details?.Where(x => x.Reservoirarea == pageNo.ToString()).ToList(); 
            List<Dt_DeliveryOrderDetail>? cabinOrderDetails = cabinOrder.Details?.Where(x => x.Status == 2).ToList();
            content.OK(data: cabinOrderDetails);
            return content;
        }
 
 
        /// <summary>
        /// pad出库完成
        /// </summary>
        /// <param name="saveModel"></param>
        /// <returns></returns>
        public WebResponseContent OutFinish(SaveModel saveModel)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                var LocationCode = saveModel.MainData["locationCode"].ToString();
                var TaskId = saveModel.MainData["taskId"].ObjToInt();
                Dt_SupplyTask supplyTask = _supplyTaskService.Repository.QueryFirst(x => x.TaskId == TaskId && x.TaskStatus == SupplyStatusEnum.NewOut.ObjToInt());
                if (supplyTask == null) throw new Exception("当前出库任务已完成");
                if (supplyTask.LocationCode != LocationCode) throw new Exception($"当前出库货位【{LocationCode}】与任务分配货位不匹配");
                Dt_DeliveryOrder deliveryOrder = BaseDal.Db.Queryable<Dt_DeliveryOrder>().Where(x => x.Out_no == supplyTask.OrderNo && x.Warehouse_no == supplyTask.WarehouseCode).Includes(x => x.Details).First();
                if (deliveryOrder == null) return WebResponseContent.Instance.OK($"出库单已完成");
                content = OutTaskFinish(supplyTask, deliveryOrder);
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
 
        public WebResponseContent OutTaskFinish(Dt_SupplyTask supplyTask, Dt_DeliveryOrder deliveryOrder)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                Dt_DeliveryOrderDetail? cabinOrderDetail = deliveryOrder.Details.FirstOrDefault(x => x.Batch_num == supplyTask.BatchNo && x.Goods_no == supplyTask.MaterielCode);
                if (cabinOrderDetail == null) throw new Exception($"出库单明细未找到");
                Dt_MaterielInfo materielInfo = _materielInfoService.Repository.QueryFirst(x => x.MaterielCode == supplyTask.MaterielCode);
                if (materielInfo == null) throw new Exception($"请维护物料编号【{supplyTask.MaterielCode}】的物料信息");
                cabinOrderDetail.Order_Outqty += supplyTask.StockQuantity;
                if (cabinOrderDetail.Order_Outqty > cabinOrderDetail.Order_qty) throw new Exception($"出库数量不可超出单据数量");
                deliveryOrder.OutStatus = "开始";
                cabinOrderDetail.OotDetailStatus = "开始";
                if (cabinOrderDetail.Order_Outqty == cabinOrderDetail.Order_qty) cabinOrderDetail.OotDetailStatus = "已完成";
 
                #region 库存
                Dt_InventoryInfo inventoryInfo = _inventoryInfoService.Repository.QueryFirst(x => x.BatchNo == cabinOrderDetail.Batch_num && x.MaterielCode == cabinOrderDetail.Goods_no && x.LocationCode == supplyTask.LocationCode);
                if (inventoryInfo == null) throw new Exception($"未找到货位【{supplyTask.LocationCode}】的库存信息");
                inventoryInfo.StockQuantity -= supplyTask.StockQuantity;
                inventoryInfo.OutboundQuantity -= supplyTask.StockQuantity;
                #endregion
                Dt_LocationInfo location = _locationInfoService.Repository.QueryFirst(x => x.LocationCode == supplyTask.LocationCode);
                #region 货位
                if (supplyTask.WarehouseCode != WarehouseEnum.立库.ObjToInt().ToString("000"))
                {
                    if (location == null)
                        return WebResponseContent.Instance.Error($"请维护货位编号【{supplyTask.LocationCode}】的货位信息");
                    Dt_InventoryInfo inventoryInfo1 = _inventoryInfoService.Repository.QueryFirst(x => x.LocationCode == supplyTask.LocationCode);
                    if (inventoryInfo1 == null) location.LocationStatus = LocationStatusEnum.Free.ObjToInt();
                }
                #endregion
 
                #region 库存批次 如果任务类型是调拨出库任务(8)就不修改批次库存
                Dt_Inventory_Batch inventory_Batch = _inventory_BatchServices.Repository.QueryFirst(x => x.BatchNo == inventoryInfo.BatchNo && x.MaterielCode ==
                    inventoryInfo.MaterielCode);
                if (supplyTask.TaskType != TaskTypeEnum.AllocatOut.ObjToInt())
                {
                    inventory_Batch.StockQuantity -= supplyTask.StockQuantity;
                    inventory_Batch.OutboundQuantity -= supplyTask.StockQuantity;
                }
                #endregion
                supplyTask.TaskStatus = SupplyStatusEnum.OutFinish.ObjToInt();
 
                _unitOfWorkManage.BeginTran();
                if (inventory_Batch.StockQuantity <= 0)
                    _inventory_BatchServices.DeleteData(inventory_Batch);
                else
                    _inventory_BatchServices.UpdateData(inventory_Batch);
                _materielInfoService.UpdateData(materielInfo);
                if (inventoryInfo.StockQuantity <= 0)
                    _inventoryInfoService.DeleteData(inventoryInfo);
                else
                    _inventoryInfoService.UpdateData(inventoryInfo);
                if (location != null) _locationInfoService.UpdateData(location);
                _deliveryOrderDetailServices.UpdateData(cabinOrderDetail);
                _supplyTaskService.Repository.DeleteAndMoveIntoHty(supplyTask, OperateTypeEnum.人工完成);
 
                #region 判断详情是否全部完成
                if (!_deliveryOrderDetailServices.Repository.QueryData(x => x.DeliveryOrderId == deliveryOrder.Id && x.OotDetailStatus != "已完成").Any())
                    deliveryOrder.OutStatus = "已完成";
                BaseDal.UpdateData(deliveryOrder);
                if (supplyTask.WarehouseCode == WarehouseEnum.立库.ObjToInt().ToString("000"))
                {
                    materielInfo.Business_qty -= supplyTask.StockQuantity;
                    if (materielInfo.Business_qty < materielInfo.MinQty)
                        CreateAllocatInOut(materielInfo);//创建调拨任务
                }
                #endregion
                _unitOfWorkManage.CommitTran();
 
                content.OK();
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                content.Error(ex.Message);
            }
            return content;
        }
 
        public WebResponseContent OutTaskFinish(Dt_SupplyTask supplyTask)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                Dt_DeliveryOrder? cabinOrder = BaseDal.Db.CopyNew().Queryable<Dt_DeliveryOrder>().Where(x => x.Out_no == supplyTask.OrderNo && x.Warehouse_no == supplyTask.WarehouseCode).Includes(x => x.Details).First();
                if (cabinOrder == null) return WebResponseContent.Instance.OK($"出库单已完成");
 
                Dt_DeliveryOrderDetail? cabinOrderDetail = cabinOrder.Details.Where(x => x.Batch_num == supplyTask.BatchNo && x.Reservoirarea == supplyTask.WarehouseCode && x.Goods_no == supplyTask.MaterielCode).FirstOrDefault();
                if (cabinOrderDetail == null) return WebResponseContent.Instance.Error($"出库单明细未找到");
                Dt_MaterielInfo materielInfo = _materielInfoService.Repository.QueryFirst(x => x.MaterielCode == cabinOrderDetail.Goods_no);
                if (materielInfo == null) return WebResponseContent.Instance.Error($"请维护物料编号【{cabinOrderDetail.Goods_no}】的物料信息");
                cabinOrderDetail.Order_Outqty += supplyTask.StockQuantity;
                if (cabinOrderDetail.Order_Outqty > cabinOrderDetail.Order_qty)
                    return WebResponseContent.Instance.Error($"出库数量不可超出单据数量");
 
 
                #region 处理出库单,货位,库存,库存批次信息,出库任务
                //_unitOfWorkManage.BeginTran();
 
                #region 出库单
                cabinOrder.OutStatus = "开始";
                cabinOrderDetail.OotDetailStatus = "开始";
                if (cabinOrderDetail.Order_Outqty == cabinOrderDetail.Order_qty)
                {
                    cabinOrderDetail.OotDetailStatus = "已完成";
                    //_deliveryOrderDetailServices.Repository.DeleteAndMoveIntoHty(cabinOrderDetail, OperateTypeEnum.自动完成);
                }
                _deliveryOrderDetailServices.Repository.UpdateData(cabinOrderDetail);
                var cabinOrder1 = BaseDal.Db.CopyNew().Queryable<Dt_DeliveryOrder>().Where(x => x.Out_no == cabinOrder.Out_no && x.Warehouse_no == supplyTask.WarehouseCode && x.Out_type != "20").Includes(x => x.Details).First();
                if (!cabinOrder1.Details.Where(x => x.OotDetailStatus != "已完成").Any())
                    cabinOrder.OutStatus = "已完成";
                Repository.UpdateData(cabinOrder);
                #endregion
 
                #region 库存
                Dt_InventoryInfo inventoryInfo = _inventoryInfoService.Repository.QueryFirst(x => x.BatchNo == cabinOrderDetail.Batch_num && x.MaterielCode == cabinOrderDetail.Goods_no && x.LocationCode == supplyTask.LocationCode);
                if (inventoryInfo == null) return WebResponseContent.Instance.Error($"未找到货位【{supplyTask.LocationCode}】的库存信息");
                inventoryInfo.StockQuantity -= supplyTask.StockQuantity;
                inventoryInfo.OutboundQuantity -= supplyTask.StockQuantity;
                if (inventoryInfo.StockQuantity <= 0)
                    _inventoryInfoService.DeleteData(inventoryInfo);
                else
                    _inventoryInfoService.UpdateData(inventoryInfo);
                #endregion
 
                #region 货位
                if (supplyTask.WarehouseCode != WarehouseEnum.立库.ObjToInt().ToString("000"))
                {
                    var location = _locationInfoService.Repository.QueryFirst(x => x.LocationCode == supplyTask.LocationCode);
                    if (location == null)
                        return WebResponseContent.Instance.Error($"请维护货位编号【{supplyTask.LocationCode}】的货位信息");
                    //if (location.EnableStatus == EnableStatusEnum.Disable.ObjToInt())
                    //    return WebResponseContent.Instance.Error($"货位编号【{supplyTask.LocationCode}】已禁用,请恢复正常再使用");
                    Dt_InventoryInfo inventoryInfo1 = _inventoryInfoService.Repository.QueryFirst(x => x.LocationCode == supplyTask.LocationCode);
                    if (inventoryInfo1 == null)
                    {
                        location.LocationStatus = LocationStatusEnum.Free.ObjToInt();
                        _locationInfoService.UpdateData(location);
                    }
                }
                #endregion
 
                //_supplyTaskService.UpdateData(supplyTask);
                supplyTask.TaskNum = cabinOrderDetail.Id;
                supplyTask.TaskStatus = SupplyStatusEnum.OutFinish.ObjToInt();
                _supplyTaskService.Repository.DeleteAndMoveIntoHty(supplyTask, OperateTypeEnum.人工完成);
 
                #region 库存批次 如果任务类型是调拨出库任务(8)就不修改批次库存
                if (supplyTask.TaskType != TaskTypeEnum.AllocatOut.ObjToInt())
                {
                    Dt_Inventory_Batch inventory_Batch = _inventory_BatchServices.Repository.QueryFirst(x => x.BatchNo == inventoryInfo.BatchNo && x.MaterielCode == inventoryInfo.MaterielCode);
                    if (inventory_Batch != null)
                    {
                        inventory_Batch.StockQuantity -= supplyTask.StockQuantity;
                        inventory_Batch.OutboundQuantity -= supplyTask.StockQuantity;
                        if (inventory_Batch.StockQuantity <= 0)
                            _inventory_BatchServices.DeleteData(inventory_Batch);
                        else
                            _inventory_BatchServices.UpdateData(inventory_Batch);
                    }
                }
                #endregion
                if (supplyTask.WarehouseCode == WarehouseEnum.立库.ObjToInt().ToString("000"))
                {
                    materielInfo.Business_qty -= supplyTask.StockQuantity;
                    _materielInfoService.UpdateData(materielInfo);
                    if (materielInfo.Business_qty < materielInfo.MinQty)
                        CreateAllocatInOut(materielInfo);//创建调拨任务
                }
                //_unitOfWorkManage.CommitTran();
                #endregion
                content.OK();
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
 
        /// <summary>
        /// 人工在出库单中点击完成按钮实现上报
        /// </summary>
        /// <param name="key">id</param>
        /// <returns></returns>
        public WebResponseContent FinishOutOrder(int key)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                Dt_DeliveryOrder deliveryOrder = BaseDal.QueryFirst(x => x.Id == key);
                List<Dt_DeliveryOrder> deliveryOrders = Db.Queryable<Dt_DeliveryOrder>().Where(x => x.Out_no == deliveryOrder.Out_no).Includes(x => x.Details).ToList();//找出所有出库单号相同的出库单
                List<Dt_DeliveryOrderDetail> deliveryOrderDetails = new List<Dt_DeliveryOrderDetail>();
                foreach (var item in deliveryOrders)
                {
                    if (item.Details != null) deliveryOrderDetails.AddRange(item.Details);
                    item.Modifier = App.User.UserName;
                    item.ModifyDate = DateTime.Now;
                    item.Details = null;
                }
                if (deliveryOrder.Out_type == OutOrderTypeEnum.Allocate.ObjToInt().ToString())
                {
                    _deliveryOrderDetailServices.Repository.DeleteAndMoveIntoHty(deliveryOrderDetails, OperateTypeEnum.人工完成);
                    BaseDal.DeleteAndMoveIntoHty(deliveryOrders, OperateTypeEnum.人工完成);
                }
                else
                {
                    var url = "http://121.37.118.63:80/GYZ2/95fck/outOrderOk";
                    if (deliveryOrder.Out_type == "3") url = "http://121.37.118.63:80/GYZ2/95fck/inOrderOk";
                    var requestDate = new
                    {
                        order_no = deliveryOrder.Out_no
                    };
                    var result = HttpHelper.Post(url, requestDate.ToJsonString());
                    var response = JsonConvert.DeserializeObject<UpstreamOrderResponse>(result);
                    if (response == null) throw new Exception("上报ERP出库单完成失败!");
                    if (response.resultCode != "0" && response.resultMsg != "未找到合法单据") throw new Exception(response.resultMsg);
                    _deliveryOrderDetailServices.Repository.DeleteAndMoveIntoHty(deliveryOrderDetails, OperateTypeEnum.人工完成);
                    BaseDal.DeleteAndMoveIntoHty(deliveryOrders, OperateTypeEnum.人工完成);
                }
                content.OK();
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
    }
}