yanjinhui
2025-11-19 02e4718a9a3f03e2385d4e26ffbedf1ceb89908e
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
using HslCommunication;
using MailKit.Search;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using WIDESEA_Common;
using WIDESEA_Common.LocationEnum;
using WIDESEA_Common.MaterielEnum;
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.SquareCabin;
using WIDESEA_IBasicService;
using WIDESEA_ISquareCabinServices;
using WIDESEA_IWMsInfoServices;
using WIDESEA_Model.Models;
 
using static System.Net.WebRequestMethods;
using static WIDESEA_DTO.SquareCabin.OrderDto;
using static WIDESEA_DTO.SquareCabin.TowcsDto;
 
 
namespace WIDESEA_SquareCabinServices
{
    public partial class CabinOrderServices : ServiceBase<Dt_CabinOrder, IRepository<Dt_CabinOrder>>, ICabinOrderServices
    {
        private readonly IMedicineGoodsServices _medicineGoodsServices;
        private readonly IUnitOfWorkManage _unitOfWorkManage;
        private readonly IInventory_BatchServices _inventory_BatchServices;
        private readonly IInventoryInfoService _inventoryInfoService;
        private readonly ICabinOrderDetailServices _cabinOrderDetailServices;
        private readonly ISupplyTaskService _supplyTaskService;
        private readonly ISupplyTaskHtyService _supplyTaskHtyService;
        private readonly ILocationInfoService _locationInfoService;
        private readonly IMaterielInfoService _materielInfoService;
        private readonly IMessageInfoService _messageInfoService;
        public IRepository<Dt_CabinOrder> Repository => BaseDal;
 
        public CabinOrderServices(IRepository<Dt_CabinOrder> BaseDal, IMedicineGoodsServices medicineGoodsServices, IUnitOfWorkManage unitOfWorkManage, IInventory_BatchServices inventory_BatchServices, IInventoryInfoService inventoryInfoService, ICabinOrderDetailServices cabinOrderDetailServices, ICabinOrderHtyServices cabinOrderHtyServices, ICabinOrderDetailHtyServices cabinOrderDetailHtyServices, ISupplyTaskService supplyTaskService, ISupplyTaskHtyService supplyTaskHtyService, IMessageInfoService messageInfoService, IMaterielInfoService materielInfoService, ILocationInfoService locationInfoService) : base(BaseDal)
        {
            _medicineGoodsServices = medicineGoodsServices;
            _unitOfWorkManage = unitOfWorkManage;
            _inventory_BatchServices = inventory_BatchServices;
            _inventoryInfoService = inventoryInfoService;
            _cabinOrderDetailServices = cabinOrderDetailServices;
            _supplyTaskService = supplyTaskService;
            _supplyTaskHtyService = supplyTaskHtyService;
            _messageInfoService = messageInfoService;
            _materielInfoService = materielInfoService;
            _locationInfoService = locationInfoService;
        }
 
 
 
        /// <summary>
        /// pda查询入库单信息
        /// </summary>
        /// <param name="saveModel"></param>
        /// <returns></returns>
        public WebResponseContent GetCabinOrders(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_CabinOrder> dt_ReceiveOrders = new List<Dt_CabinOrder>();
                if (string.IsNullOrEmpty(orderNo))
                {
                    dt_ReceiveOrders = Db.Queryable<Dt_CabinOrder>().Where(x => (x.OdrderStatus == "新建" || x.OdrderStatus == "开始") && x.Warehouse_no == warehouseCode).Includes(x => x.Details).OrderByDescending(x => x.CreateDate).ToPageList(pageNo, 5);
                }
                else
                {
                    dt_ReceiveOrders = Db.Queryable<Dt_CabinOrder>().Where(x => (x.Order_no.Contains(orderNo) || x.Supplier_no.Contains(orderNo)) && (x.OdrderStatus == "新建" || x.OdrderStatus == "开始") && 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>
        /// pda查看入库详情表
        /// </summary>
        /// <param name="pageNo"></param>
        /// <param name="orderNo"></param>
        /// <returns></returns>
        public WebResponseContent GetCabinOrderDetail(int pageNo, string orderNo)
        {
            WebResponseContent content = new WebResponseContent();
            Dt_CabinOrder cabinOrder = Db.Queryable<Dt_CabinOrder>().Includes(x => x.Details).First(x => x.Order_no == orderNo);
            List<Dt_CabinOrderDetail> cabinOrderDetails = cabinOrder.Details.Where(x => x.Status == 2 && x.OrderDetailStatus != "已完成").ToList();
            content.OK(data: cabinOrderDetails);
            return content;
        }
 
 
        /// <summary>
        /// Pad入库完成 
        /// </summary>
        /// <param name="saveModel"></param>
        /// <returns></returns>
        public WebResponseContent FeedbackIn([FromBody] SaveModel saveModel)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                var LocationCode = saveModel.MainData["LocationCode"].ToString();
                var orderNo = saveModel.MainData["orderNo"].ToString();
                var batchNo = saveModel.MainData["batchNo"].ToString();
                var id = saveModel.MainData["id"].ObjToInt();
                var materielCode = saveModel.MainData["materielCode"].ToString();
                var Inqty = saveModel.MainData["Inqty"].ObjToInt();
                var warehouseCode = saveModel.MainData["warehouseCode"].ToString();
                Dt_CabinOrder cabinOrder = BaseDal.Db.Queryable<Dt_CabinOrder>().Where(x => x.Order_no == orderNo && x.Warehouse_no == warehouseCode).Includes(x => x.Details).First();
                if (cabinOrder == null || cabinOrder.OdrderStatus == "已完成")
                    return WebResponseContent.Instance.Error($"入库单已完成");
                //Dt_CabinOrderDetail cabinOrderDetail = cabinOrder.Details.Where(x => x.Goods_no == materielCode && x.Batch_num == batchNo && x.Status == 2).FirstOrDefault();
                Dt_CabinOrderDetail? cabinOrderDetail = cabinOrder.Details.Where(x => x.Id == id).FirstOrDefault();
                if (cabinOrderDetail == null || cabinOrderDetail.OrderDetailStatus == "已完成")
                    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_Inqty += Inqty;
                if (cabinOrderDetail.Order_Inqty > cabinOrderDetail.Order_qty)
                    return WebResponseContent.Instance.Error($"入库数量不可超出单据数量");
                //大件库整箱校验
                if (warehouseCode == WarehouseEnum.大件库.ObjToInt().ToString("000")&& (materielInfo.MaterielSourceType==MaterielSourceTypeEnum.SelfMadePart))
                { 
                    var BoxRule = materielInfo.BoxQty;
                    if (BoxRule > 0 && Inqty % BoxRule != 0)
                    {
                        return WebResponseContent.Instance.Error($"大件库只允许存放整箱货物!当前入库数量 {Convert.ToInt32(Inqty)} 不是箱规 {Convert.ToInt32(BoxRule)} 的整数倍");
                    }
                }
 
                #region 处理入库单,货位,库存,库存批次信息
                _unitOfWorkManage.BeginTran();
 
                #region 入库单
                cabinOrder.OdrderStatus = "开始";
                cabinOrderDetail.OrderDetailStatus = "开始";
                if (cabinOrderDetail.Order_Inqty == cabinOrderDetail.Order_qty)
                {
                    cabinOrderDetail.OrderDetailStatus = "已完成";
                }
                _cabinOrderDetailServices.Repository.UpdateData(cabinOrderDetail);
                var cabinOrder1 = BaseDal.Db.Queryable<Dt_CabinOrder>().Where(x => x.Order_no == cabinOrder.Order_no && x.Warehouse_no == warehouseCode).Includes(x => x.Details).First();
                if (!cabinOrder1.Details.Where(x => x.OrderDetailStatus != "已完成").Any()) cabinOrder.OdrderStatus = "已完成";
                Repository.UpdateData(cabinOrder);
                #endregion
 
                #region 货位
                var location = _locationInfoService.Repository.QueryFirst(x => x.LocationCode == LocationCode);
                if (location == null) return WebResponseContent.Instance.Error($"请维护货位编号【{LocationCode}】的货位信息");
              
                //if (location.EnableStatus == EnableStatusEnum.Disable.ObjToInt())
                //    return WebResponseContent.Instance.Error($"货位编号【{LocationCode}】已禁用,请恢复正常再使用");
                if (location.WarehouseCode != cabinOrderDetail.Reservoirarea)
                    return WebResponseContent.Instance.Error($"货位编号【{LocationCode}】所属库房与当前入库单所属库房不匹配");
                if (location.LocationStatus == LocationStatusEnum.Free.ObjToInt())
                {
                    location.LocationStatus = LocationStatusEnum.InStock.ObjToInt();
                    _locationInfoService.UpdateData(location);
                }
                #endregion
 
                #region 库存
                Dt_InventoryInfo inventoryInfo = _inventoryInfoService.Repository.QueryFirst(x => x.BatchNo == cabinOrderDetail.Batch_num && x.MaterielCode == cabinOrderDetail.Goods_no && x.LocationCode == LocationCode);
                if (inventoryInfo != null)
                {
                    inventoryInfo.StockQuantity += Inqty;
                    inventoryInfo.AvailableQuantity += Inqty;
                    inventoryInfo.InDate = DateTime.Now;
                    _inventoryInfoService.UpdateData(inventoryInfo);
                }
                else
                {
                    inventoryInfo = new Dt_InventoryInfo()
                    {
                        LocationCode = LocationCode,
                        MaterielCode = materielInfo.MaterielCode,
                        MaterielName = materielInfo.MaterielName,
                        MaterielSpec = materielInfo.MaterielSpec,
                        OutboundQuantity = 0,
                        StockQuantity = Inqty,
                        AvailableQuantity = Inqty,
                        StockStatus = StockStatusEmun.入库完成.ObjToInt(),
                        ValidityPeriod = cabinOrderDetail.Exp_date,
                        WarehouseCode = cabinOrderDetail.Reservoirarea,
                        SupplyQuantity = 0,
                        InDate = DateTime.Now,
                        BatchNo = cabinOrderDetail.Batch_num,
                        Creater = App.User.UserName,
                        CreateDate = DateTime.Now,
                    };
                    switch (inventoryInfo.WarehouseCode)
                    {
                        case "001":
                            inventoryInfo.Remark = "智能立库";
                            break;
                        case "002":
                            inventoryInfo.Remark = "大件库";
                            break;
                        case "003":
                            inventoryInfo.Remark = "麻精库";
                            break;
                        case "010":
                            inventoryInfo.Remark = "冷冻库";
                            break;
                    }
                    _inventoryInfoService.AddData(inventoryInfo);
                }
                #endregion
 
                #region 任务记录
                #region MyRegion
                //Dt_SupplyTask supplyTask = new Dt_SupplyTask()
                //{
                //    WarehouseCode = cabinOrderDetail.Reservoirarea,
                //    TaskNum = cabinOrderDetail.Id,
                //    TaskStatus = SupplyStatusEnum.InFinish.ObjToInt(),
                //    BatchNo = inventoryInfo.BatchNo,
                //    MaterielName = inventoryInfo.MaterielName,
                //    MaterielCode = inventoryInfo.MaterielCode,
                //    MaterielSpec = inventoryInfo.MaterielSpec,
                //    TaskType = TaskTypeEnum.InPick.ObjToInt(),
                //    CreateDate = DateTime.Now,
                //    Creater = App.User.UserName,
                //    LocationCode = location.LocationCode,
                //    OrderNo = cabinOrder.Order_no,
                //    StockQuantity = Inqty,
                //    SupplyQuantity = 0,
                //    Remark = "入库"
                //};
                //_supplyTaskService.AddData(supplyTask); 
                #endregion
                Dt_SupplyTask_Hty supplyTask_Hty = new Dt_SupplyTask_Hty()
                {
                    WarehouseCode = cabinOrderDetail.Reservoirarea,
                    TaskNum = cabinOrderDetail.Id,
                    OperateType = OperateTypeEnum.人工完成.ToString(),
                    InsertTime = DateTime.Now,
                    TaskStatus = SupplyStatusEnum.InFinish.ObjToInt(),
                    BatchNo = inventoryInfo.BatchNo,
                    MaterielName = inventoryInfo.MaterielName,
                    MaterielCode = inventoryInfo.MaterielCode,
                    MaterielSpec = inventoryInfo.MaterielSpec,
                    TaskType = cabinOrder.Order_type == "1" ? TaskTypeEnum.In.ObjToInt() : TaskTypeEnum.OutReturn.ObjToInt(),
                    CreateDate = DateTime.Now,
                    Creater = App.User.UserName,
                    LocationCode = location.LocationCode,
                    OrderNo = cabinOrder.Order_no,
                    StockQuantity = Inqty,
                    SupplyQuantity = 0,
                    Remark = "入库"
                };
                _supplyTaskHtyService.AddData(supplyTask_Hty);
                #endregion
 
                #region 库存批次
                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 += Inqty;
                    inventory_Batch.AvailableQuantity += Inqty;
                    _inventory_BatchServices.UpdateData(inventory_Batch);
                }
                else
                {
                    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.StockQuantity,
                        ValidityPeriod = inventoryInfo.ValidityPeriod.ObjToDate(),
                        SupplyQuantity = inventoryInfo.SupplyQuantity,
                    };
                    _inventory_BatchServices.AddData(inventory_Batch);
                }
                #endregion
 
                _unitOfWorkManage.CommitTran();
                #endregion
 
                content.OK(cabinOrderDetail.Order_Inqty.ToString());
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                content.Error(ex.Message);
            }
            return content;
        }
 
        #region 创建入库单
        /// <summary>
        /// 创建入库单,返回一个入库单集合到data
        /// </summary>
        public WebResponseContent CreateInboundOrder(UpstreamOrderInfo order)
        {
            WebResponseContent webResponseContent = new WebResponseContent();
            try
            {
                string WareCodeMJ = WarehouseEnum.麻精库.ObjToInt().ToString("000");
                string WareCodeLD = WarehouseEnum.冷冻库.ObjToInt().ToString("000");
                #region 检查是否有未完成的盘点任务
                var dt_bath = _inventory_BatchServices.Repository.QueryData(x => order.details.Select(d => d.goods_no).Contains(x.MaterielCode)).ToList();
                if (dt_bath.Count>=1)
                {
                    foreach (var item in dt_bath)
                    {
                        if (item.SupplyQuantity > 0) throw new Exception($"入库单【{order.order_no}】物料编号【{dt_bath[0].MaterielCode}】存在未完成的盘盈入库任务,请处理完成后再进行正常入库操作");
                    }
                } 
                #endregion
                List<Dt_CabinOrder> dt_CabinOrders = new List<Dt_CabinOrder>();
                #region 特殊药品入特殊库房
                if (order.warehouse_no == WareCodeMJ || order.warehouse_no == WareCodeLD)
                {
                    var entityOrder = new Dt_CabinOrder
                    {
                        Order_no = order.order_no,
                        Order_type = order.order_type,
                        Supplier_no = order.supplier_no,
                        Account_tiem = order.account_time,
                        OdrderStatus = "新建",
                        Supplier_name = order.supplier_name,
                        Warehouse_no = order.warehouse_no,
                        Details = order.details.Select(d => new Dt_CabinOrderDetail
                        {
                            Reservoirarea = order.warehouse_no,
                            Goods_no = d.goods_no,
                            Order_qty = Math.Abs(d.order_qty),
                            Batch_num = d.batch_num,
                            Exp_date = d.exp_date,
                            OrderDetailStatus = "新建",
                            Status = 2, //如果是001房那么就是未同步状态,如果不是001房那么就是无需同步状态
                        }).ToList()
                    };
                    dt_CabinOrders.Add(entityOrder);
                    BaseDal.Db.InsertNav(dt_CabinOrders).Include(x => x.Details).ExecuteCommand();
                    //webResponseContent.OK(data: dt_CabinOrders);
                }
                #endregion
                else
                {
                    #region 创建入库单头表、大件库和立库
                    var entityOrder = new Dt_CabinOrder//大件库订单
                    {
                        Order_no = order.order_no,
                        Order_type = order.order_type,
                        Supplier_no = order.supplier_no,
                        Account_tiem = order.account_time,
                        OdrderStatus = "新建",
                        Supplier_name = order.supplier_name,
                        Warehouse_no = WarehouseEnum.大件库.ObjToInt().ToString("000"),
                        Details = new List<Dt_CabinOrderDetail>()
                    };
                    var entityOrderLK = new Dt_CabinOrder//立库订单
                    {
                        Order_no = order.order_no,
                        Order_type = order.order_type,
                        Supplier_no = order.supplier_no,
                        Account_tiem = order.account_time,
                        OdrderStatus = "新建",
                        Supplier_name = order.supplier_name,
                        Warehouse_no = WarehouseEnum.立库.ObjToInt().ToString("000"),
                        Details = new List<Dt_CabinOrderDetail>()
                    };
                    #endregion
 
                    List<Dt_MaterielInfo> materielInfos = _materielInfoService.Repository.QueryData(x => order.details.Select(x => x.goods_no).Contains(x.MaterielCode)).ToList();
                    List<Dt_MaterielInfo> materielInfosUp = new List<Dt_MaterielInfo>();
                    foreach (var item in order.details)
                    {
                        // 将上游入库数量转为正数
                        item.order_qty = Math.Abs(item.order_qty);
                        #region 根据物料编码查询物料信息
                        Dt_MaterielInfo? materielInfo = materielInfos.Where(x => x.MaterielCode == item.goods_no).FirstOrDefault();
                        if (materielInfo == null) throw new Exception($"未找到药品编码【{item.goods_no}】的信息");
                        if (!Enum.IsDefined(typeof(MaterielSourceTypeEnum), materielInfo.MaterielSourceType))
                            throw new Exception($"请设置药品编号【{item.goods_no}】的属性分类");
                        #endregion
                        #region 大件
                        if (materielInfo.MaterielSourceType == MaterielSourceTypeEnum.PurchasePart)//如果物料是大件
                        {
                            Dt_CabinOrderDetail orderDetail = new Dt_CabinOrderDetail()
                            {
                                Reservoirarea = entityOrder.Warehouse_no,
                                Goods_no = item.goods_no,
                                Order_qty = item.order_qty,
                                Batch_num = item.batch_num,
                                Exp_date = item.exp_date,
                                OrderDetailStatus = "新建",
                                Status = 2
                            };
                            entityOrder.Details.Add(orderDetail);
                        }
                        #endregion
                        else
                        {
                            if (materielInfo.BoxQty < 1) throw new Exception($"请设置药品编号【{item.goods_no}】的箱规数量");
                            if (materielInfo.MinQty < 1) throw new Exception($"请设置药品编号【{item.goods_no}】的立库最低库存数");
                            Dt_CabinOrderDetail orderDetail = null;
                            var ys = item.order_qty % materielInfo.BoxQty; //不能整除箱规的散件数 
                            var xs = (int)(item.order_qty / materielInfo.BoxQty);//保留整数
                            #region 判断是否有散件
                            if (ys > 0)
                            {
                                orderDetail = new Dt_CabinOrderDetail()
                                {
                                    Reservoirarea = entityOrderLK.Warehouse_no,
                                    Goods_no = item.goods_no,
                                    Order_qty = ys,
                                    Batch_num = item.batch_num,
                                    Exp_date = item.exp_date,
                                    OrderDetailStatus = "新建",
                                    Status = 0
                                };
                                materielInfo.Business_qty += ys;
                            }
                            #endregion
 
                            #region 判断立库库存是否大于立库最低库存数
                            while (materielInfo.Business_qty < materielInfo.MinQty && xs > 0) //当业务数量和整箱数都大于0的时候才会停止循环
                            {
                                xs--;
                                if (orderDetail == null)
                                {
                                    orderDetail = new Dt_CabinOrderDetail()
                                    {
                                        Reservoirarea = entityOrderLK.Warehouse_no,
                                        Goods_no = item.goods_no,
                                        Order_qty = materielInfo.BoxQty,
                                        Batch_num = item.batch_num,
                                        Exp_date = item.exp_date,
                                        OrderDetailStatus = "新建",
                                        Status = 0
                                    };
                                    materielInfo.Business_qty += materielInfo.BoxQty;
                                }
                                else
                                {
                                    orderDetail.Order_qty += materielInfo.BoxQty;
                                    materielInfo.Business_qty += materielInfo.BoxQty;
                                }
                            }
 
                            if (orderDetail != null) entityOrderLK.Details.Add(orderDetail);
                            #endregion
 
                            #region 剩余整件入平库
                            if (xs > 0)
                            {
                                orderDetail = new Dt_CabinOrderDetail()
                                {
                                    Reservoirarea = entityOrder.Warehouse_no,
                                    Goods_no = item.goods_no,
                                    Order_qty = materielInfo.BoxQty * xs,
                                    Batch_num = item.batch_num,
                                    Exp_date = item.exp_date,
                                    OrderDetailStatus = "新建",
                                    Status = 2
                                };
                                entityOrder.Details.Add(orderDetail);
                            }
                            #endregion
                            materielInfosUp.Add(materielInfo);
                        }
                        //_materielInfoService.UpdateData(materielInfo);
                    }
                    try
                    {
                        _unitOfWorkManage.BeginTran();
                        if (materielInfosUp.Count > 0) _materielInfoService.UpdateData(materielInfosUp);
                        if (entityOrder.Details.Count > 0) dt_CabinOrders.Add(entityOrder);
                        if (entityOrderLK.Details.Count > 0) dt_CabinOrders.Add(entityOrderLK);
                        if (dt_CabinOrders.Count > 0) BaseDal.Db.InsertNav(dt_CabinOrders).Include(x => x.Details).ExecuteCommand();
                        _unitOfWorkManage.CommitTran();
                    }
                    catch (Exception)
                    {
                        _unitOfWorkManage.RollbackTran();
                    }
                    //webResponseContent.OK(data: dt_CabinOrders);
                }
            }
            catch (Exception ex)
            {
                _messageInfoService.AddMessageInfo(MessageGroupByEnum.InOrderAlarm, (order.order_type == "2" ? "出库退货" : "正常入库") + $":单号【{order.order_no}】", ex.Message);
                webResponseContent.Error(ex.Message);
            }
            return webResponseContent;
        }
        #endregion
 
        #region 创建盘盈入库单
        //public WebResponseContent CreateCheckInOrder(UpstreamOrderInfo 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();
        //                if (inventory_Batch.SupplyQuantity != item.order_qty)
        //                {
        //                    _messageInfoService.AddMessageInfo(MessageGroupByEnum.InOrderAlarm, "ERP报报溢入库错误", $"盘盈入库单【{order.order_no}】物料编号【{item.goods_no}】物料批次【{item.batch_num}】的盘盈数量有误");
        //                    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.SupplyQuantity != 0)
        //                    {
        //                        Dt_SupplyTask_Hty supplyTask_Hty = new Dt_SupplyTask_Hty()
        //                        {
        //                            WarehouseCode = inventoryInfo.WarehouseCode,
        //                            OperateType = OperateTypeEnum.自动完成.ToString(),
        //                            InsertTime = DateTime.Now,
        //                            TaskStatus = SupplyStatusEnum.InFinish.ObjToInt(),
        //                            BatchNo = inventoryInfo.BatchNo,
        //                            MaterielName = inventoryInfo.MaterielName,
        //                            MaterielCode = inventoryInfo.MaterielCode,
        //                            MaterielSpec = inventoryInfo.MaterielSpec,
        //                            TaskType = TaskTypeEnum.ChenckIn.ObjToInt(),
        //                            CreateDate = DateTime.Now,
        //                            Creater = App.User.UserName,
        //                            LocationCode = inventoryInfo.LocationCode,
        //                            OrderNo = order.order_no,
        //                            StockQuantity = 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();
        //                }
        //                infosUp.AddRange(inventoryInfos);
        //                inventory_Batch.StockQuantity += inventory_Batch.SupplyQuantity;
        //                inventory_Batch.AvailableQuantity = inventory_Batch.StockQuantity;
        //                inventory_Batch.SupplyQuantity = 0;
        //                batchesUp.Add(inventory_Batch);
        //                //_inventoryInfoService.UpdateData(inventoryInfos);
        //                //_inventory_BatchServices.UpdateData(inventory_Batch);
        //            }
        //            #endregion
        //            #region 创建盘点单
        //            var entityOrder = new Dt_CabinOrder
        //            {
        //                Order_no = order.order_no,
        //                Order_type = order.order_type,
        //                Supplier_no = order.supplier_no,
        //                Account_tiem = order.account_time,
        //                OdrderStatus = "已完成",
        //                Supplier_name = order.supplier_name,
        //                Warehouse_no = order.warehouse_no,
        //                Details = order.details.Select(d => new Dt_CabinOrderDetail
        //                {
        //                    Goods_no = d.goods_no,
        //                    Order_qty = Math.Abs(d.order_qty),
        //                    Order_Inqty = Math.Abs(d.order_qty),
        //                    Batch_num = d.batch_num,
        //                    Exp_date = d.exp_date,
        //                    Reservoirarea = order.warehouse_no,
        //                    OrderDetailStatus = "已完成",
        //                    Status = 2,
        //                }).ToList()
        //            };
        //            _unitOfWorkManage.BeginTran();
        //            if (supplyTask_Hties.Count > 0) _supplyTaskHtyService.AddData(supplyTask_Hties);
        //            if (infosUp.Count > 0) _inventoryInfoService.UpdateData(infosUp);
        //            if (batchesUp.Count > 0) _inventory_BatchServices.UpdateData(batchesUp);
        //            BaseDal.Db.InsertNav(entityOrder).Include(it => it.Details).ExecuteCommand();
        //            _unitOfWorkManage.CommitTran();
        //            //Repository.AddData(entityOrder);
        //            #endregion
        //        }
        //        #endregion
        //        else
        //        {
        //            List<Dt_CabinOrder> cabinOrdersAdd = new List<Dt_CabinOrder>();
        //            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)).ToList();
        //            List<Dt_InventoryInfo> _InventoryInfos = _inventoryInfoService.Repository.QueryData(x => codes.Contains(x.MaterielCode)).ToList();
        //            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)
        //                {
        //                    _messageInfoService.AddMessageInfo(MessageGroupByEnum.InOrderAlarm, "ERP报报溢入库错误", $"盘盈入库单【{order.order_no}】物料编号【{item.goods_no}】物料批次【{item.batch_num}】的盘盈数量有误");
        //                    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).FirstOrDefault(); //修改这里有可能只有大件库才会存在差异
        //                //var LkQty = Math.Abs(inventoryLK.SupplyQuantity);
        //                var LkQty = inventoryLK?.SupplyQuantity ?? 0;
 
        //                //获取大件库盘点差异数
        //                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);
        //                if (LkQty + DJQty != Qty)
        //                {
        //                    _messageInfoService.AddMessageInfo(MessageGroupByEnum.InOrderAlarm, "ERP报报溢入库错误", $"盘盈入库单【{order.order_no}】物料编号【{item.goods_no}】物料批次【{item.batch_num}】的盘盈数量有误");
        //                    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.InFinish.ObjToInt(),
        //                                BatchNo = inventoryInfo.BatchNo,
        //                                MaterielName = inventoryInfo.MaterielName,
        //                                MaterielCode = inventoryInfo.MaterielCode,
        //                                MaterielSpec = inventoryInfo.MaterielSpec,
        //                                TaskType = TaskTypeEnum.ChenckIn.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);
        //                    inventory_Batch.StockQuantity += inventory_Batch.SupplyQuantity;
        //                    inventory_Batch.AvailableQuantity = inventory_Batch.StockQuantity;
        //                    inventory_Batch.SupplyQuantity = 0;
        //                    //_inventory_BatchServices.UpdateData(inventory_Batch);
        //                    batchesUp.Add(inventory_Batch);
        //                    #endregion
 
        //                    #region 创建大件库盘点单
        //                    var entityOrder = new Dt_CabinOrder
        //                    {
        //                        Order_no = order.order_no,
        //                        Order_type = order.order_type,
        //                        Supplier_no = order.supplier_no,
        //                        Account_tiem = order.account_time,
        //                        OdrderStatus = "已完成",
        //                        Supplier_name = order.supplier_name,
        //                        Warehouse_no = WareCodeDJ,
        //                        Details = order.details.Select(d => new Dt_CabinOrderDetail
        //                        {
        //                            Goods_no = d.goods_no,
        //                            Order_qty = Math.Abs(d.order_qty),
        //                            Order_Inqty = Math.Abs(d.order_qty),
        //                            Batch_num = d.batch_num,
        //                            Exp_date = d.exp_date,
        //                            Reservoirarea = WareCodeDJ,
        //                            OrderDetailStatus = "已完成",
        //                            Status = 2,
        //                        }).ToList()
        //                    };
        //                    //Repository.AddData(entityOrder);
        //                    //Db.InsertNav(entityOrder).Include(it => it.Details).ExecuteCommand();
        //                    cabinOrdersAdd.Add(entityOrder);
        //                    #endregion
        //                }
        //                else // LkQty != 0 表示立库有差异
        //                {
        //                    #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.InFinish.ObjToInt(),
        //                                BatchNo = inventoryInfo.BatchNo,
        //                                MaterielName = inventoryInfo.MaterielName,
        //                                MaterielCode = inventoryInfo.MaterielCode,
        //                                MaterielSpec = inventoryInfo.MaterielSpec,
        //                                TaskType = TaskTypeEnum.ChenckIn.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_CabinOrder
        //                        {
        //                            Order_no = order.order_no,
        //                            Order_type = order.order_type,
        //                            Supplier_no = order.supplier_no,
        //                            Account_tiem = order.account_time,
        //                            OdrderStatus = "已完成",
        //                            Supplier_name = order.supplier_name,
        //                            Warehouse_no = WareCodeDJ,
        //                            Details = order.details.Select(d => new Dt_CabinOrderDetail
        //                            {
        //                                Goods_no = d.goods_no,
        //                                Order_qty = DJQty,
        //                                Order_Inqty = DJQty,
        //                                Batch_num = d.batch_num,
        //                                Exp_date = d.exp_date,
        //                                Reservoirarea = WareCodeDJ,
        //                                OrderDetailStatus = "已完成",
        //                                Status = 2,
        //                            }).ToList()
        //                        };
        //                        //Repository.AddData(cabinOrder);
        //                        //Db.InsertNav(cabinOrder).Include(it => it.Details).ExecuteCommand();
        //                        cabinOrdersAdd.Add(cabinOrder);
        //                    }
        //                    #endregion
 
        //                    #region 创建立库盘点单
        //                    var entityOrder = new Dt_CabinOrder
        //                    {
        //                        Order_no = order.order_no,
        //                        Order_type = order.order_type,
        //                        Supplier_no = order.supplier_no,
        //                        Account_tiem = order.account_time,
        //                        OdrderStatus = "新建",
        //                        Supplier_name = order.supplier_name,
        //                        Warehouse_no = WareCodeLK,
        //                        Details = order.details.Select(d => new Dt_CabinOrderDetail
        //                        {
        //                            Goods_no = d.goods_no,
        //                            Order_qty = Math.Abs(LkQty),
        //                            Batch_num = d.batch_num,
        //                            Exp_date = d.exp_date,
        //                            Reservoirarea = WareCodeLK,
        //                            OrderDetailStatus = "新建",
        //                            Status = 0,
        //                        }).ToList()
        //                    };
        //                    //Repository.AddData(entityOrder);
        //                    //Db.InsertNav(entityOrder).Include(it => it.Details).ExecuteCommand();
        //                    cabinOrdersAdd.Add(entityOrder);
        //                    #endregion
        //                }
        //            }
 
        //            _unitOfWorkManage.BeginTran();
        //            if (supplyTask_Hties.Count > 1) _supplyTaskHtyService.AddData(supplyTask_Hties);
 
        //            if (infosUp.Count > 0) _inventoryInfoService.UpdateData(infosUp);
        //            if (batchesUp.Count > 0) _inventory_BatchServices.UpdateData(batchesUp);
        //            if (cabinOrdersAdd.Count > 0) BaseDal.Db.InsertNav(cabinOrdersAdd).Include(it => it.Details).ExecuteCommand();
        //            _unitOfWorkManage.CommitTran();
        //            // 添加事务提交后的检查
        //            Console.WriteLine($"事务提交完成,准备返回。cabinOrdersAdd数量: {cabinOrdersAdd.Count}");
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        _unitOfWorkManage.RollbackTran();
        //        //_messageInfoService.AddMessageInfo(MessageGroupByEnum.InOrderAlarm, "创建盘盈入库单", ex.Message);
        //        //return content.Error(ex.Message);
        //        throw ex;
        //    }
        //    //return content;
        //    return content.OK($"盘盈入库单 {order.order_no} 创建成功");
        //}
        #endregion
        #region 创建盘盈入库单(修改原版因为前面订单创建失败导致后面也创建不了问题)
        public WebResponseContent CreateCheckInOrder(UpstreamOrderInfo 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();
                        if (inventory_Batch.SupplyQuantity != item.order_qty)
                        {
                            _messageInfoService.AddMessageInfo(MessageGroupByEnum.InOrderAlarm, "ERP报报溢入库错误", $"盘盈入库单【{order.order_no}】物料编号【{item.goods_no}】物料批次【{item.batch_num}】的盘盈数量有误");
                            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.SupplyQuantity != 0)
                            {
                                Dt_SupplyTask_Hty supplyTask_Hty = new Dt_SupplyTask_Hty()
                                {
                                    WarehouseCode = inventoryInfo.WarehouseCode,
                                    OperateType = OperateTypeEnum.自动完成.ToString(),
                                    InsertTime = DateTime.Now,
                                    TaskStatus = SupplyStatusEnum.InFinish.ObjToInt(),
                                    BatchNo = inventoryInfo.BatchNo,
                                    MaterielName = inventoryInfo.MaterielName,
                                    MaterielCode = inventoryInfo.MaterielCode,
                                    MaterielSpec = inventoryInfo.MaterielSpec,
                                    TaskType = TaskTypeEnum.ChenckIn.ObjToInt(),
                                    CreateDate = DateTime.Now,
                                    Creater = App.User.UserName,
                                    LocationCode = inventoryInfo.LocationCode,
                                    OrderNo = order.order_no,
                                    StockQuantity = 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();
                        }
                        infosUp.AddRange(inventoryInfos);
                        inventory_Batch.StockQuantity += inventory_Batch.SupplyQuantity;
                        inventory_Batch.AvailableQuantity = inventory_Batch.StockQuantity;
                        inventory_Batch.SupplyQuantity = 0;
                        batchesUp.Add(inventory_Batch);
                        //_inventoryInfoService.UpdateData(inventoryInfos);
                        //_inventory_BatchServices.UpdateData(inventory_Batch);
                    }
                    #endregion
                    #region 创建盘点单
                    var entityOrder = new Dt_CabinOrder
                    {
                        Order_no = order.order_no,
                        Order_type = order.order_type,
                        Supplier_no = order.supplier_no,
                        Account_tiem = order.account_time,
                        OdrderStatus = "已完成",
                        Supplier_name = order.supplier_name,
                        Warehouse_no = order.warehouse_no,
                        Details = order.details.Select(d => new Dt_CabinOrderDetail
                        {
                            Goods_no = d.goods_no,
                            Order_qty = Math.Abs(d.order_qty),
                            Order_Inqty = Math.Abs(d.order_qty),
                            Batch_num = d.batch_num,
                            Exp_date = d.exp_date,
                            Reservoirarea = order.warehouse_no,
                            OrderDetailStatus = "已完成",
                            Status = 2,
                        }).ToList()
                    };
                    using (var scop = new TransactionScope(TransactionScopeOption.RequiresNew))
                    {
                        if (supplyTask_Hties.Count > 0) _supplyTaskHtyService.AddData(supplyTask_Hties);
                        if (infosUp.Count > 0) _inventoryInfoService.UpdateData(infosUp);
                        if (batchesUp.Count > 0) _inventory_BatchServices.UpdateData(batchesUp);
                        BaseDal.Db.InsertNav(entityOrder).Include(it => it.Details).ExecuteCommand();
                        scop.Complete();
                    }
                    #endregion
                }
                #endregion
                else
                {
                    List<Dt_CabinOrder> cabinOrdersAdd = new List<Dt_CabinOrder>();
                    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)).ToList();
                    List<Dt_InventoryInfo> _InventoryInfos = _inventoryInfoService.Repository.QueryData(x => codes.Contains(x.MaterielCode)).ToList();
                    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)
                        {
                            _messageInfoService.AddMessageInfo(MessageGroupByEnum.InOrderAlarm, "ERP报报溢入库错误", $"盘盈入库单【{order.order_no}】物料编号【{item.goods_no}】物料批次【{item.batch_num}】的盘盈数量有误");
                            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).FirstOrDefault(); //修改这里有可能只有大件库才会存在差异
                        //var LkQty = Math.Abs(inventoryLK.SupplyQuantity);
                        var LkQty = inventoryLK?.SupplyQuantity ?? 0;
 
                        //获取大件库盘点差异数
                        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);
                        if (LkQty + DJQty != Qty)
                        {
                            _messageInfoService.AddMessageInfo(MessageGroupByEnum.InOrderAlarm, "ERP报报溢入库错误", $"盘盈入库单【{order.order_no}】物料编号【{item.goods_no}】物料批次【{item.batch_num}】的盘盈数量有误");
                            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.InFinish.ObjToInt(),
                                        BatchNo = inventoryInfo.BatchNo,
                                        MaterielName = inventoryInfo.MaterielName,
                                        MaterielCode = inventoryInfo.MaterielCode,
                                        MaterielSpec = inventoryInfo.MaterielSpec,
                                        TaskType = TaskTypeEnum.ChenckIn.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);
                            inventory_Batch.StockQuantity += inventory_Batch.SupplyQuantity;
                            inventory_Batch.AvailableQuantity = inventory_Batch.StockQuantity;
                            inventory_Batch.SupplyQuantity = 0;
                            //_inventory_BatchServices.UpdateData(inventory_Batch);
                            batchesUp.Add(inventory_Batch);
                            #endregion
 
                            #region 创建大件库盘点单
                            var entityOrder = new Dt_CabinOrder
                            {
                                Order_no = order.order_no,
                                Order_type = order.order_type,
                                Supplier_no = order.supplier_no,
                                Account_tiem = order.account_time,
                                OdrderStatus = "已完成",
                                Supplier_name = order.supplier_name,
                                Warehouse_no = WareCodeDJ,
                                Details = order.details.Select(d => new Dt_CabinOrderDetail
                                {
                                    Goods_no = d.goods_no,
                                    Order_qty = Math.Abs(d.order_qty),
                                    Order_Inqty = Math.Abs(d.order_qty),
                                    Batch_num = d.batch_num,
                                    Exp_date = d.exp_date,
                                    Reservoirarea = WareCodeDJ,
                                    OrderDetailStatus = "已完成",
                                    Status = 2,
                                }).ToList()
                            };
                            //Repository.AddData(entityOrder);
                            //Db.InsertNav(entityOrder).Include(it => it.Details).ExecuteCommand();
                            cabinOrdersAdd.Add(entityOrder);
                            #endregion
                        }
                        else // LkQty != 0 表示立库有差异
                        {
                            #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.InFinish.ObjToInt(),
                                        BatchNo = inventoryInfo.BatchNo,
                                        MaterielName = inventoryInfo.MaterielName,
                                        MaterielCode = inventoryInfo.MaterielCode,
                                        MaterielSpec = inventoryInfo.MaterielSpec,
                                        TaskType = TaskTypeEnum.ChenckIn.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_CabinOrder
                                {
                                    Order_no = order.order_no,
                                    Order_type = order.order_type,
                                    Supplier_no = order.supplier_no,
                                    Account_tiem = order.account_time,
                                    OdrderStatus = "已完成",
                                    Supplier_name = order.supplier_name,
                                    Warehouse_no = WareCodeDJ,
                                    Details = order.details.Select(d => new Dt_CabinOrderDetail
                                    {
                                        Goods_no = d.goods_no,
                                        Order_qty = DJQty,
                                        Order_Inqty = DJQty,
                                        Batch_num = d.batch_num,
                                        Exp_date = d.exp_date,
                                        Reservoirarea = WareCodeDJ,
                                        OrderDetailStatus = "已完成",
                                        Status = 2,
                                    }).ToList()
                                };
                                //Repository.AddData(cabinOrder);
                                //Db.InsertNav(cabinOrder).Include(it => it.Details).ExecuteCommand();
                                cabinOrdersAdd.Add(cabinOrder);
                            }
                            #endregion
 
                            #region 创建立库盘点单
                            var entityOrder = new Dt_CabinOrder
                            {
                                Order_no = order.order_no,
                                Order_type = order.order_type,
                                Supplier_no = order.supplier_no,
                                Account_tiem = order.account_time,
                                OdrderStatus = "新建",
                                Supplier_name = order.supplier_name,
                                Warehouse_no = WareCodeLK,
                                Details = order.details.Select(d => new Dt_CabinOrderDetail
                                {
                                    Goods_no = d.goods_no,
                                    Order_qty = Math.Abs(LkQty),
                                    Batch_num = d.batch_num,
                                    Exp_date = d.exp_date,
                                    Reservoirarea = WareCodeLK,
                                    OrderDetailStatus = "新建",
                                    Status = 0,
                                }).ToList()
                            };
                            //Repository.AddData(entityOrder);
                            //Db.InsertNav(entityOrder).Include(it => it.Details).ExecuteCommand();
                            cabinOrdersAdd.Add(entityOrder);
                            #endregion
                        }
                    }
                    using (var scop = new TransactionScope(TransactionScopeOption.RequiresNew))
                    {
                        if (supplyTask_Hties.Count > 0) _supplyTaskHtyService.AddData(supplyTask_Hties);
                        if (infosUp.Count > 0) _inventoryInfoService.UpdateData(infosUp);
                        if (batchesUp.Count > 0) _inventory_BatchServices.UpdateData(batchesUp);
                        if (cabinOrdersAdd.Count > 0) BaseDal.Db.InsertNav(cabinOrdersAdd).Include(it => it.Details).ExecuteCommand();
                        scop.Complete();
                    }
                }
            }
            catch (Exception ex)
            {
                //throw ex;
                return content.Error(ex.Message);
            }
            //return content;
            return content.OK($"盘盈入库单创建成功");
        }
        #endregion
 
 
 
        /// <summary>
        /// 人工入库完成
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public WebResponseContent FinishInOrder(int key)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                Dt_CabinOrder cabinOrder = BaseDal.QueryFirst(x => x.Id == key);
                List<Dt_CabinOrder> cabinOrders = Db.Queryable<Dt_CabinOrder>().Where(x => x.Order_no == cabinOrder.Order_no).Includes(x => x.Details).ToList();//找出所有出库单号相同的出库单
 
                List<Dt_CabinOrderDetail> cabinOrderDetails = new List<Dt_CabinOrderDetail>();
                foreach (var item in cabinOrders)
                {
                    if (item.Details != null) cabinOrderDetails.AddRange(item.Details);
 
                    item.Modifier = App.User.UserName;
                    item.ModifyDate = DateTime.Now;
                    item.Details = null;
                }
                if (cabinOrder.Order_type == InOrderTypeEnum.Allocat.ObjToInt().ToString())
                {
                    _cabinOrderDetailServices.Repository.DeleteAndMoveIntoHty(cabinOrderDetails, OperateTypeEnum.人工完成);
                    BaseDal.DeleteAndMoveIntoHty(cabinOrders, OperateTypeEnum.人工完成);
                }
                else
                {
                    var url = "http://121.37.118.63:80/GYZ2/95fck/outOrderOk";
                    if (cabinOrder.Order_type == "2") url = "http://121.37.118.63:80/GYZ2/95fck/outOrderOk";
                    var requestDate = new
                    {
                        order_no = cabinOrder.Order_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);
 
                    _cabinOrderDetailServices.Repository.DeleteAndMoveIntoHty(cabinOrderDetails, OperateTypeEnum.人工完成);
                    BaseDal.DeleteAndMoveIntoHty(cabinOrders, OperateTypeEnum.人工完成);
                }
                content.OK();
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
    }
}