1
dengjunjie
2025-01-09 be0856c1bf533ed6001ff786908ae98e4b853eb5
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
using AutoMapper;
using HslCommunication.WebSocket;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common.CommonEnum;
using WIDESEA_Common.OrderEnum;
using WIDESEA_Common.StockEnum;
using WIDESEA_Common.WareHouseEnum;
using WIDESEA_Core;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.CodeConfigEnum;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_Core.Utilities;
using WIDESEA_DTO;
using WIDESEA_DTO.Basic;
using WIDESEA_DTO.ERP;
using WIDESEA_DTO.Inbound;
using WIDESEA_DTO.MES;
using WIDESEA_External.ERPService;
using WIDESEA_External.Model;
using WIDESEA_IBasicRepository;
using WIDESEA_IBasicService;
using WIDESEA_IInboundRepository;
using WIDESEA_IInboundService;
using WIDESEA_IRecordService;
using WIDESEA_IStockRepository;
using WIDESEA_IStockService;
using WIDESEA_ITaskInfoRepository;
using WIDESEA_ITaskInfoService;
using WIDESEA_Model.Models;
using WIDESEA_StockRepository;
 
namespace WIDESEA_InboundService
{
    public class InboundOrderService : ServiceBase<Dt_InboundOrder, IInboundOrderRepository>, IInboundOrderService
    {
        private readonly IMapper _mapper;
        private readonly IUnitOfWorkManage _unitOfWorkManage;
        private readonly IInboundRepository _inboundRepository;
        private readonly IBasicRepository _basicRepository;
        private readonly IStockRepository _stockRepository;
        private readonly IRecordService _recordService;
        private readonly IInvokeERPService _invokeERPService;
        private readonly IWarehouseService _warehouseService;
        private readonly IPalletTypeInfoRepository _palletTypeInfoRepository;
 
        public IInboundOrderRepository Repository => BaseDal;
 
        public InboundOrderService(IInboundOrderRepository BaseDal, IMapper mapper, IUnitOfWorkManage unitOfWorkManage, IInboundRepository inboundRepository, IBasicRepository basicRepository, IStockRepository stockRepository, IRecordService recordService, IInvokeERPService invokeERPService, IWarehouseService warehouseService, IPalletTypeInfoRepository palletTypeInfoRepository) : base(BaseDal)
        {
            _mapper = mapper;
            _unitOfWorkManage = unitOfWorkManage;
            _inboundRepository = inboundRepository;
            _basicRepository = basicRepository;
            _stockRepository = stockRepository;
            _recordService = recordService;
            _invokeERPService = invokeERPService;
            _warehouseService = warehouseService;
            _palletTypeInfoRepository = palletTypeInfoRepository;
        }
        /// <summary>
        /// 其他入库单创建
        /// </summary>
        /// <returns></returns>
        public WebResponseContent ReceiveWarehousingOrder(ErpInOrderDTO erpInOrder)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                //获取仓库信息
                Dt_Warehouse warehouse = _warehouseService.Repository.QueryFirst(x => x.WarehouseCode == erpInOrder.WaId);
                if (warehouse == null)
                {
                    return content.Error("未找到仓库信息");
                }
                //获取是否存在订单
                Dt_InboundOrder inboundOrder = BaseDal.QueryFirst(x => x.InboundOrderNo == erpInOrder.OrderNo);
                if (erpInOrder.Way == 1)
                {
                    if (inboundOrder != null)
                    {
 
                    }
                }
                else if (erpInOrder.Way == 2)
                {
 
                }
                else
                {
 
                }
                return content.OK();
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
        public WebResponseContent GetInboundOrders(SaveModel saveModel)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                int pageNo = saveModel.MainData["pageNo"].ObjToInt();
                string? orderNo = saveModel.MainData["orderNo"].ToString();
                int warehouseId = saveModel.MainData["warehouseId"].ObjToInt();
                List<Dt_InboundOrder> dt_ReceiveOrders = new List<Dt_InboundOrder>();
                if (string.IsNullOrEmpty(orderNo))
                {
                    dt_ReceiveOrders = Db.Queryable<Dt_InboundOrder>().Where(x => x.OrderStatus < InOrderStatusEnum.入库完成.ObjToInt() && x.WarehouseId == warehouseId).ToPageList(pageNo, 5);
                }
                else
                {
                    dt_ReceiveOrders = Db.Queryable<Dt_InboundOrder>().Where(x => (x.InboundOrderNo.Contains(orderNo)) && x.OrderStatus < InOrderStatusEnum.入库完成.ObjToInt() && x.WarehouseId == warehouseId).ToPageList(pageNo, 5);
                }
 
                content.OK(data: dt_ReceiveOrders);
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
        /// <summary>
        /// 单个物料码组盘 适用测试架
        /// </summary>
        /// <param name="inboundOrderId"></param>
        /// <param name="palletCode"></param>
        /// <param name="serNum"></param>
        /// <returns></returns>
        public WebResponseContent MaterielGroup(int inboundOrderId, string palletCode, string serNum)
        {
            try
            {
                Dt_InboundOrder inboundOrder = BaseDal.Db.Queryable<Dt_InboundOrder>().Where(x => x.Id == inboundOrderId).Includes(x => x.Details).First();
                if (inboundOrder == null)
                {
                    return WebResponseContent.Instance.Error($"未找到入库单信息");
                }
                if (inboundOrder.OrderStatus >= InOrderStatusEnum.入库完成.ObjToInt())
                {
                    return WebResponseContent.Instance.Error($"入库单已结束");
                }
                if (inboundOrder.Details.Count <= 0)
                {
                    return WebResponseContent.Instance.Error($"未找到入库单明细信息");
                }
                MatSerNumAnalysisModel model = CodeAnalysisHelper.CodeAnalysis<MatSerNumAnalysisModel>(AnalysisCodeEnum.MatSerNumAnalysis, serNum);
                Dt_MaterielInfo materielInfo = _basicRepository.MaterielInfoRepository.QueryFirst(x => x.MaterielCode == model.MaterielCode);
                if (materielInfo == null)
                {
                    return WebResponseContent.Instance.Error($"未找到该物料的信息");
                }
                if (materielInfo.WarehouseId != inboundOrder.WarehouseId)
                {
                    return WebResponseContent.Instance.Error($"该物料不属于本仓");
                }
                Dt_ReceiveOrder receiveOrder = _inboundRepository.ReceiveOrderRepository.Db.Queryable<Dt_ReceiveOrder>().Where(x => x.ReceiveOrderNo == inboundOrder.UpperOrderNo).Includes(x => x.Details).First();
                if (receiveOrder == null)
                {
                    return WebResponseContent.Instance.Error($"未找到对应的收货单");
                }
                if (receiveOrder.Details.Count <= 0)
                {
                    return WebResponseContent.Instance.Error($"未找到对应的收货单明细");
                }
                List<string> purchaseOrderNos = receiveOrder.Details.Select(x => x.PurchaseOrderNo).ToList();
                if (!purchaseOrderNos.Contains(model.PurchaseOrderNo))
                {
                    return WebResponseContent.Instance.Error($"未找到对应收货单明细中采购单");
                }
                Dt_ReceiveOrderDetail? receiveOrderDetail = receiveOrder.Details.FirstOrDefault(x => x.MaterielCode == model.MaterielCode);
                if (receiveOrderDetail == null)
                {
                    return WebResponseContent.Instance.Error($"未在收货单明细中找到该物料信息");
                }
                Dt_InboundOrderDetail? inboundOrderDetail = inboundOrder.Details.FirstOrDefault(x => x.MaterielCode == model.MaterielCode);
                if (inboundOrderDetail == null)
                {
                    return WebResponseContent.Instance.Error($"未在入库单明细中找到该物料信息");
                }
                if (inboundOrderDetail.OrderDetailStatus > OrderDetailStatusEnum.GroupAndInbound.ObjToInt())
                {
                    return WebResponseContent.Instance.Error($"对应入库单明细已结束");
                }
                float inboundDetailSum = inboundOrderDetail.OverInQuantity + inboundOrderDetail.ReceiptQuantity;
                if (inboundDetailSum == inboundOrderDetail.OrderQuantity)
                {
                    return WebResponseContent.Instance.Error($"该物料入库单明细已全部组盘完成");
                }
                if (model.Quantity > receiveOrderDetail.ReceivedQuantity || (inboundDetailSum + model.Quantity) > receiveOrderDetail.ReceivedQuantity)
                {
                    return WebResponseContent.Instance.Error($"与该收货单明细收货数量不一致");
                }
                Dt_StockInfo oldStockinfo = _stockRepository.StockInfoRepository.QueryFirst(x => x.PalletCode == palletCode);
                if (oldStockinfo != null)
                {
                    return WebResponseContent.Instance.Error($"托盘重复");
                }
                Dt_StockInfoDetail stockInfoDetail = new Dt_StockInfoDetail()
                {
                    BatchNo = model.LotNo,
                    MaterielCode = materielInfo.MaterielCode,
                    MaterielName = materielInfo.MaterielName,
                    OrderNo = inboundOrder.InboundOrderNo,
                    SerialNumber = model.SerialNumber,
                    StockQuantity = model.Quantity,
                    OutboundQuantity = 0,
                    Unit = materielInfo.MaterielUnit,
                    Status = StockStatusEmun.组盘暂存.ObjToInt(),
                    ProductionDate = model.ProductionDate,
                    EffectiveDate = model.EffectiveDate,
                    InboundOrderRowNo = inboundOrderDetail.RowNo,
                };
 
                Dt_StockInfo stockInfo = new Dt_StockInfo()
                {
                    PalletCode = palletCode,
                    StockStatus = StockStatusEmun.组盘暂存.ObjToInt(),
                    WarehouseId = inboundOrder.WarehouseId,
                    Details = new List<Dt_StockInfoDetail> { stockInfoDetail }
                };
                inboundOrderDetail.ReceiptQuantity += model.Quantity;
                _unitOfWorkManage.BeginTran();
                if (inboundOrderDetail.OrderDetailStatus == OrderDetailStatusEnum.New.ObjToInt())
                {
                    inboundOrderDetail.OrderDetailStatus = OrderDetailStatusEnum.GroupAndInbound.ObjToInt();
                }
                _inboundRepository.InboundOrderDetailRepository.UpdateData(inboundOrderDetail);
                if (inboundOrder.OrderStatus == InOrderStatusEnum.未开始.ObjToInt())
                {
                    inboundOrder.OrderStatus = InOrderStatusEnum.入库中.ObjToInt();
                    BaseDal.UpdateData(inboundOrder);
                }
                _stockRepository.StockInfoRepository.Db.InsertNav(stockInfo).Include(x => x.Details).ExecuteCommand();
                _unitOfWorkManage.CommitTran();
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
 
 
        /// <summary>
        /// 入平库,直接添加库存
        /// </summary>
        /// <param name="warehouseId"></param>
        /// <param name="serNums"></param>
        /// <returns></returns>
        public WebResponseContent InPinKu(string inboundOrderId, int warehouseId, List<string> serNums)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                Dt_Warehouse warehouse = _warehouseService.Repository.QueryFirst(x => x.WarehouseId == warehouseId);
                if (warehouse == null)
                {
                    return WebResponseContent.Instance.Error($"未找到该仓库信息");
                }
                Dt_InboundOrder inboundOrder = BaseDal.Db.Queryable<Dt_InboundOrder>().Where(x => x.InboundOrderNo == inboundOrderId && x.WarehouseId == warehouse.WarehouseId).Includes(x => x.Details).First();
                if (inboundOrder == null)
                {
                    return WebResponseContent.Instance.Error($"未找到入库单信息");
                }
                if (inboundOrder.Details == null || inboundOrder.Details.Count <= 0)
                {
                    return WebResponseContent.Instance.Error($"未找到入库单明细信息");
                }
 
                //入库明细原完成数量
                int oldCount = inboundOrder.Details.Where(x => x.OrderDetailStatus == OrderDetailStatusEnum.Over.ObjToInt()).ToList().Count;
 
                Dt_ReceiveOrder receiveOrder = _inboundRepository.ReceiveOrderRepository.Db.Queryable<Dt_ReceiveOrder>().Where(x => x.ReceiveOrderNo == inboundOrder.UpperOrderNo).Includes(x => x.Details).First();
                if (receiveOrder == null)
                {
                    return WebResponseContent.Instance.Error($"未找到对应的收货单");
                }
                if (receiveOrder.Details == null || receiveOrder.Details.Count <= 0)
                {
                    return WebResponseContent.Instance.Error($"未找到对应的收货单明细");
                }
 
                List<string> purchaseOrderNos = receiveOrder.Details.Select(x => x.PurchaseOrderNo).ToList();
 
                List<MatSerNumAnalysisModel> models = CodeAnalysisHelper.CodeAnalysis<MatSerNumAnalysisModel>(AnalysisCodeEnum.MatSerNumAnalysis, serNums);
 
                if (models.Select(x => x.MaterielCode).Distinct().Count() > 1)
                {
                    return WebResponseContent.Instance.Error($"物料不可混放");
                }
 
                string materielCode = models.FirstOrDefault()?.MaterielCode ?? "";
                Dt_MaterielInfo materielInfo = _basicRepository.MaterielInfoRepository.QueryFirst(x => x.MaterielCode == materielCode);
                if (materielInfo == null)
                {
                    return WebResponseContent.Instance.Error($"未找到该物料的信息");
                }
 
                List<Dt_InboundOrderDetail> inboundOrderDetails = inboundOrder.Details.Where(x => x.MaterielCode == materielCode).ToList();
 
                if (inboundOrderDetails == null || inboundOrderDetails.Count <= 0)
                {
                    return WebResponseContent.Instance.Error($"未在入库单明细中找到该物料信息");
                }
                foreach (var model in models)
                {
                    if (!inboundOrderDetails.Any(x => x.BatchNo == model.LotNo))
                        return WebResponseContent.Instance.Error($"未在入库单明细中找到该物料批次【{model.LotNo}】");
                }
                if (receiveOrder.Details.FirstOrDefault(x => x.MaterielCode == materielCode) == null)
                {
                    return WebResponseContent.Instance.Error($"未在收货单明细中找到该物料信息");
                }
                float beforeQuantity = 0;
                Dt_StockInfo stockInfo = new Dt_StockInfo()
                {
                    PalletCode = DateTime.Now.ToString("yyyyMMddHHmmss"),
                    StockStatus = StockStatusEmun.入库完成.ObjToInt(),
                    WarehouseId = inboundOrder.WarehouseId,
                    PalletType = PalletTypeEnum.SmallPallet.ObjToInt(),
                    LocationCode = "平库位",
                    Details = new List<Dt_StockInfoDetail>()
                };
 
                List<Dt_StockInfoDetail> stockInfoDetails = new List<Dt_StockInfoDetail>();
                List<int> detailKeys = new List<int>();
                foreach (var model in models)
                {
                    if (purchaseOrderNos.FirstOrDefault(x => x == model.PurchaseOrderNo) == null)
                    {
                        return WebResponseContent.Instance.Error($"未在收货单明细中找到该采购单");
                    }
 
                    Dt_InboundOrderDetail? notGroupDetail = inboundOrderDetails.Where(x => x.OrderDetailStatus == OrderDetailStatusEnum.New.ObjToInt() && x.OrderQuantity == model.Quantity && !detailKeys.Contains(x.Id) && x.OrderQuantity > x.ReceiptQuantity).FirstOrDefault();
 
                    if (notGroupDetail == null)
                    {
                        return WebResponseContent.Instance.Error($"该物料在该入库单中已全部组盘完成");
                    }
                    detailKeys.Add(notGroupDetail.Id);
 
                    Dt_StockInfoDetail stockInfoDetail = new Dt_StockInfoDetail()
                    {
                        BatchNo = model.LotNo,
                        MaterielCode = materielInfo.MaterielCode,
                        MaterielName = materielInfo.MaterielName,
                        OrderNo = inboundOrder.InboundOrderNo,
                        SerialNumber = model.SerialNumber,
                        StockQuantity = model.Quantity,
                        OutboundQuantity = 0,
                        Unit = materielInfo.MaterielUnit,
                        Status = StockStatusEmun.入库完成.ObjToInt(),
                        ProductionDate = model.ProductionDate,
                        EffectiveDate = model.EffectiveDate,
                        InboundOrderRowNo = notGroupDetail.RowNo,
                    };
 
                    if (stockInfo.Id > 0)
                    {
                        stockInfoDetail.StockId = stockInfo.Id;
                    }
                    stockInfo.Details.Add(stockInfoDetail);
 
                    stockInfoDetails.Add(stockInfoDetail);
 
                    notGroupDetail.ReceiptQuantity = model.Quantity;
                    notGroupDetail.OverInQuantity = model.Quantity;
                    notGroupDetail.OrderDetailStatus = OrderDetailStatusEnum.Over.ObjToInt();
                }
 
                //入库明细新增完成数量
                int newCount = inboundOrderDetails.Where(x => x.OrderDetailStatus == OrderDetailStatusEnum.Over.ObjToInt()).ToList().Count;
                if (inboundOrder.Details.Count == (newCount + oldCount))
                {
                    inboundOrder.OrderStatus = InOrderStatusEnum.入库完成.ObjToInt();
                }
                else
                    inboundOrder.OrderStatus = InOrderStatusEnum.入库中.ObjToInt();
                float totalQuantity = stockInfo.Details.Sum(x => x.StockQuantity);
 
                _unitOfWorkManage.BeginTran();
                if (stockInfo.Id == 0)
                {
                    _stockRepository.StockInfoRepository.Db.InsertNav(stockInfo).Include(x => x.Details).ExecuteCommand();
                }
                else
                {
                    _stockRepository.StockInfoRepository.Db.UpdateNav(stockInfo).Include(x => x.Details, new UpdateNavOptions() { OneToManyInsertOrUpdate = true }).ExecuteCommand();
                }
                _inboundRepository.InboundOrderDetailRepository.UpdateData(inboundOrderDetails);
                _inboundRepository.InboundOrderRepository.UpdateData(inboundOrder);
                _recordService.StockQuantityChangeRecordService.AddStockChangeRecord(stockInfo, stockInfoDetails, beforeQuantity, totalQuantity, StockChangeTypeEnum.Inbound);
                _unitOfWorkManage.CommitTran();
                content.OK();
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                content.Error(ex.Message);
            }
            return content;
        }
 
        /// <summary>
        /// 组盘
        /// </summary>
        /// <param name="saveModel"></param>
        /// <returns></returns>
        public WebResponseContent MaterielGroup(SaveModel saveModel)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                var orderNo = saveModel.MainData["orderNo"].ToString();
                var palletCode = saveModel.MainData["palletCode"].ToString();
                var warehouseId = saveModel.MainData["warehouseId"].ObjToInt();
                var Initiallife = saveModel.MainData["initiallife"].ObjToInt();
                var serNums = saveModel.DelKeys.Select(x => x.ToString()).ToList();
                Dt_Warehouse warehouse = _warehouseService.Repository.QueryFirst(x => x.WarehouseId == warehouseId);
                if (warehouse == null)
                {
                    return WebResponseContent.Instance.Error($"未找到该仓库信息");
                }
 
                Dt_InboundOrder inboundOrder = BaseDal.Db.Queryable<Dt_InboundOrder>().Where(x => x.InboundOrderNo == orderNo && x.WarehouseId == warehouse.WarehouseId).Includes(x => x.Details).First();
                if (inboundOrder == null)
                {
                    return WebResponseContent.Instance.Error($"未找到入库单信息");
                }
                if (inboundOrder.Details == null || inboundOrder.Details.Count <= 0)
                {
                    return WebResponseContent.Instance.Error($"未找到入库单明细信息");
                }
 
                Dt_ReceiveOrder receiveOrder = _inboundRepository.ReceiveOrderRepository.Db.Queryable<Dt_ReceiveOrder>().Where(x => x.ReceiveOrderNo == inboundOrder.UpperOrderNo).Includes(x => x.Details).First();
                if (receiveOrder == null)
                {
                    return WebResponseContent.Instance.Error($"未找到对应的收货单");
                }
                if (receiveOrder.Details == null || receiveOrder.Details.Count <= 0)
                {
                    return WebResponseContent.Instance.Error($"未找到对应的收货单明细");
                }
 
                List<string> purchaseOrderNos = receiveOrder.Details.Select(x => x.PurchaseOrderNo).ToList();
 
                List<MatSerNumAnalysisModel> models = CodeAnalysisHelper.CodeAnalysis<MatSerNumAnalysisModel>(AnalysisCodeEnum.MatSerNumAnalysis, serNums);
 
                if (models.Select(x => x.MaterielCode).Distinct().Count() > 1)
                {
                    return WebResponseContent.Instance.Error($"物料不可混放");
                }
 
                string materielCode = models.FirstOrDefault()?.MaterielCode ?? "";
                Dt_MaterielInfo materielInfo = _basicRepository.MaterielInfoRepository.QueryFirst(x => x.MaterielCode == materielCode);
                if (materielInfo == null)
                {
                    return WebResponseContent.Instance.Error($"未找到该物料的信息");
                }
 
                List<Dt_InboundOrderDetail> inboundOrderDetails = inboundOrder.Details.Where(x => x.MaterielCode == materielCode).ToList();
 
                if (inboundOrderDetails == null || inboundOrderDetails.Count <= 0)
                {
                    return WebResponseContent.Instance.Error($"未在入库单明细中找到该物料信息");
                }
                if (receiveOrder.Details.FirstOrDefault(x => x.MaterielCode == materielCode) == null)
                {
                    return WebResponseContent.Instance.Error($"未在收货单明细中找到该物料信息");
                }
 
                float beforeQuantity = 0;
 
                Dt_StockInfo stockInfo = _stockRepository.StockInfoRepository.Db.Queryable<Dt_StockInfo>().Where(x => x.PalletCode == palletCode).Includes(x => x.Details).First();
                if (stockInfo == null)
                {
                    stockInfo = new Dt_StockInfo()
                    {
                        PalletCode = palletCode,
                        StockStatus = StockStatusEmun.组盘暂存.ObjToInt(),
                        WarehouseId = inboundOrder.WarehouseId,
                        PalletType = GetPalletType(warehouse, palletCode, materielInfo),
                        Details = new List<Dt_StockInfoDetail>()
                    };
                }
                else
                {
                    if (stockInfo.StockStatus != StockStatusEmun.组盘暂存.ObjToInt())
                    {
                        return WebResponseContent.Instance.Error($"托盘号重复");
                    }
                    beforeQuantity = stockInfo.Details.Sum(x => x.StockQuantity);
                }
 
                if (warehouse.WarehouseCode == WarehouseEnum.HA64.ToString())
                {
                    stockInfo.Remark = Initiallife.ToString();
                    string batchNo = models.FirstOrDefault()?.LotNo ?? "";
                    Dt_StockInfoDetail existDetail = _stockRepository.StockInfoDetailRepository.QueryFirst(x => x.BatchNo == batchNo);
                    if (existDetail != null)
                    {
                        return WebResponseContent.Instance.Error($"{batchNo}测试架已存在");
                    }
                    if (models.Count >= 2)
                    {
                        return WebResponseContent.Instance.Error($"组盘明细不唯一");
                    }
                    if (palletCode.Substring(0, 1) == "6")
                    {
                        stockInfo.PalletType = PalletTypeEnum.MediumPallet.ObjToInt();
                    }
                    else
                    {
                        stockInfo.PalletType = PalletTypeEnum.LargestPallet.ObjToInt();
                    }
                }
 
                List<Dt_StockInfoDetail> stockInfoDetails = new List<Dt_StockInfoDetail>();
                List<int> detailKeys = new List<int>();
                foreach (var model in models)
                {
                    if (purchaseOrderNos.FirstOrDefault(x => x == model.PurchaseOrderNo) == null)
                    {
                        return WebResponseContent.Instance.Error($"未在收货单明细中找到该采购单");
                    }
 
                    Dt_InboundOrderDetail? notGroupDetail = inboundOrderDetails.Where(x => x.OrderDetailStatus == OrderDetailStatusEnum.New.ObjToInt() && x.OrderQuantity == model.Quantity && !detailKeys.Contains(x.Id) && x.OrderQuantity > x.ReceiptQuantity).FirstOrDefault();
 
                    if (notGroupDetail == null)
                    {
                        return WebResponseContent.Instance.Error($"该物料在该入库单中已全部组盘完成");
                    }
                    detailKeys.Add(notGroupDetail.Id);
                    Dt_StockInfoDetail stockInfoDetail = new Dt_StockInfoDetail()
                    {
                        BatchNo = model.LotNo,
                        MaterielCode = materielInfo.MaterielCode,
                        MaterielName = materielInfo.MaterielName,
                        OrderNo = inboundOrder.InboundOrderNo,
                        SerialNumber = model.SerialNumber,
                        StockQuantity = model.Quantity,
                        OutboundQuantity = 0,
                        Unit = materielInfo.MaterielUnit,
                        Status = StockStatusEmun.组盘暂存.ObjToInt(),
                        ProductionDate = model.ProductionDate,
                        EffectiveDate = model.EffectiveDate,
                        InboundOrderRowNo = notGroupDetail.RowNo,
                    };
 
                    if (stockInfo.Id > 0)
                    {
                        stockInfoDetail.StockId = stockInfo.Id;
                    }
                    stockInfo.Details.Add(stockInfoDetail);
 
                    stockInfoDetails.Add(stockInfoDetail);
 
                    notGroupDetail.ReceiptQuantity = model.Quantity;
                    notGroupDetail.OrderDetailStatus = OrderDetailStatusEnum.GroupAndInbound.ObjToInt();
                }
 
                float totalQuantity = stockInfo.Details.Sum(x => x.StockQuantity);
 
                inboundOrder.OrderStatus = InOrderStatusEnum.入库中.ObjToInt();
 
                _unitOfWorkManage.BeginTran();
                if (stockInfo.Id == 0)
                {
                    _stockRepository.StockInfoRepository.Db.InsertNav(stockInfo).Include(x => x.Details).ExecuteCommand();
                }
                else
                {
                    _stockRepository.StockInfoRepository.Db.UpdateNav(stockInfo).Include(x => x.Details, new UpdateNavOptions() { OneToManyInsertOrUpdate = true }).ExecuteCommand();
                }
                _inboundRepository.InboundOrderDetailRepository.UpdateData(inboundOrderDetails);
                _inboundRepository.InboundOrderRepository.UpdateData(inboundOrder);
                _recordService.StockQuantityChangeRecordService.AddStockChangeRecord(stockInfo, stockInfoDetails, beforeQuantity, totalQuantity, StockChangeTypeEnum.MaterielGroup);
                _unitOfWorkManage.CommitTran();
                content.OK();
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                content.Error(ex.Message);
            }
            return content;
        }
 
 
        public WebResponseContent MaterielGroup(string palletCode, int Initiallife, int warehouseId, List<string> serNums)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                Dt_Warehouse warehouse = _warehouseService.Repository.QueryFirst(x => x.WarehouseId == warehouseId);
                if (warehouse == null)
                {
                    return WebResponseContent.Instance.Error($"未找到该仓库信息");
                }
 
                List<MatSerNumAnalysisModel> models = CodeAnalysisHelper.CodeAnalysis<MatSerNumAnalysisModel>(AnalysisCodeEnum.MatSerNumAnalysis, serNums);
 
                if (models.Select(x => x.MaterielCode).Distinct().Count() > 1)
                {
                    return WebResponseContent.Instance.Error($"物料不可混放");
                }
 
                string materielCode = models.FirstOrDefault()?.MaterielCode ?? "";
                Dt_MaterielInfo materielInfo = _basicRepository.MaterielInfoRepository.QueryFirst(x => x.MaterielCode == materielCode);
                if (materielInfo == null)
                {
                    return WebResponseContent.Instance.Error($"未找到该物料的信息");
                }
 
                float beforeQuantity = 0;
 
                Dt_StockInfo stockInfo = _stockRepository.StockInfoRepository.Db.Queryable<Dt_StockInfo>().Where(x => x.PalletCode == palletCode).Includes(x => x.Details).First();
 
                if (stockInfo == null)
                {
                    stockInfo = new Dt_StockInfo()
                    {
                        PalletCode = palletCode,
                        StockStatus = StockStatusEmun.手动组盘暂存.ObjToInt(),
                        WarehouseId = warehouse.WarehouseId,
                        PalletType = GetPalletType(warehouse, palletCode, materielInfo),
                        Details = new List<Dt_StockInfoDetail>()
                    };
                }
                else
                {
                    if (stockInfo.StockStatus != StockStatusEmun.组盘暂存.ObjToInt() && stockInfo.StockStatus != StockStatusEmun.手动组盘暂存.ObjToInt())
                    {
                        return WebResponseContent.Instance.Error($"托盘号重复");
                    }
                    beforeQuantity = stockInfo.Details.Sum(x => x.StockQuantity);
                }
 
                if (warehouse.WarehouseCode == WarehouseEnum.HA64.ToString())
                {
                    string batchNo = models.FirstOrDefault()?.LotNo ?? "";
                    Dt_StockInfoDetail existDetail = _stockRepository.StockInfoDetailRepository.QueryFirst(x => x.BatchNo == batchNo);
                    if (existDetail != null)
                    {
                        return WebResponseContent.Instance.Error($"{batchNo}测试架已存在");
                    }
                    if (models.Count >= 2)
                    {
                        return WebResponseContent.Instance.Error($"组盘明细不唯一");
                    }
                    stockInfo.Remark = Initiallife.ToString();
                    if (palletCode.Substring(0, 1) == "6")
                    {
                        stockInfo.PalletType = PalletTypeEnum.MediumPallet.ObjToInt();
                    }
                    else
                    {
                        stockInfo.PalletType = PalletTypeEnum.LargestPallet.ObjToInt();
                    }
                }
 
                List<Dt_StockInfoDetail> stockInfoDetails = new List<Dt_StockInfoDetail>();
                List<int> detailKeys = new List<int>();
                foreach (var model in models)
                {
                    Dt_StockInfoDetail stockInfoDetail = new Dt_StockInfoDetail()
                    {
                        BatchNo = model.LotNo,
                        MaterielCode = materielInfo.MaterielCode,
                        MaterielName = materielInfo.MaterielName,
                        OrderNo = "",
                        SerialNumber = model.SerialNumber,
                        StockQuantity = model.Quantity,
                        OutboundQuantity = 0,
                        Unit = materielInfo.MaterielUnit,
                        Status = StockStatusEmun.组盘暂存.ObjToInt(),
                        ProductionDate = model.ProductionDate,
                        EffectiveDate = model.EffectiveDate,
                        InboundOrderRowNo = 0,
                    };
 
                    if (stockInfo.Id > 0)
                    {
                        stockInfoDetail.StockId = stockInfo.Id;
                    }
                    stockInfo.Details.Add(stockInfoDetail);
 
                    stockInfoDetails.Add(stockInfoDetail);
                }
 
                float totalQuantity = stockInfo.Details.Sum(x => x.StockQuantity);
 
                _unitOfWorkManage.BeginTran();
                if (stockInfo.Id == 0)
                {
                    _stockRepository.StockInfoRepository.Db.InsertNav(stockInfo).Include(x => x.Details).ExecuteCommand();
                }
                else
                {
                    _stockRepository.StockInfoRepository.Db.UpdateNav(stockInfo).Include(x => x.Details, new UpdateNavOptions() { OneToManyInsertOrUpdate = true }).ExecuteCommand();
                }
                _recordService.StockQuantityChangeRecordService.AddStockChangeRecord(stockInfo, stockInfoDetails, beforeQuantity, totalQuantity, StockChangeTypeEnum.MaterielGroup);
                _unitOfWorkManage.CommitTran();
                content.OK();
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                content.Error(ex.Message);
            }
            return content;
        }
 
        /// <summary>
        /// 入库完成上报ERP
        /// </summary>
        /// <returns></returns>
        public WebResponseContent FeedbackInboundOrder(Dt_StockInfo stockInfo, Dt_InboundOrder inboundOrder, List<ERPInboundDetailModel> detailModels)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                if (inboundOrder.Details.Count == 0)
                {
                    return WebResponseContent.Instance.Error($"未找到入库单明细信息");
                }
                if (inboundOrder.OrderStatus == InOrderStatusEnum.入库完成.ObjToInt())
                {
                    return WebResponseContent.Instance.Error($"入库单据已完成");
                }
 
                Dt_Warehouse warehouse = _warehouseService.Repository.QueryFirst(x => x.WarehouseId == stockInfo.WarehouseId);
                if (warehouse == null)
                {
                    return WebResponseContent.Instance.Error($"未找到仓库信息");
                }
 
                ERPInboundModel model = new ERPInboundModel()
                {
                    Code = inboundOrder.InboundOrderNo,
                    CreatorCode = inboundOrder.Creater,//测试
                    EntDate = inboundOrder.CreateDate.ToString("yyyy-MM-dd HH:mm:ss"),
                    StockDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                    SuppliersId = inboundOrder.SupplierId,
                    Type = "S",
                    UniqueTag = inboundOrder.Id.ToString(),
                    WarehouseCode = warehouse.WarehouseCode,
                    Way = 1,
                    Details = detailModels
                };
                //推送ERP 测试注释
                _invokeERPService.InvokeInboundOrderApi(model);
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
 
        /// <summary>
        /// 入库完成回传到ERP
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public WebResponseContent FeedbackInboundOrder(int id)
        {
            try
            {
                Dt_InboundOrder inboundOrder = Db.Queryable<Dt_InboundOrder>().Where(x => x.Id == id).Includes(x => x.Details).First();
                if (inboundOrder == null)
                {
                    return WebResponseContent.Instance.Error($"未找到入库单信息");
                }
                if (inboundOrder.Details == null || inboundOrder.Details.Count == 0)
                {
                    return WebResponseContent.Instance.Error($"未找到入库单明细信息");
                }
                if (inboundOrder.OrderStatus == InOrderStatusEnum.入库完成.ObjToInt())
                {
                    return WebResponseContent.Instance.Error($"入库单据已完成");
                }
 
                List<Dt_StockInfo> stockInfos = Db.Queryable<Dt_StockInfo>().Where(x => x.Details.Any(v => v.OrderNo == inboundOrder.InboundOrderNo)).Includes(x => x.Details).ToList();
                List<Dt_Warehouse> warehouses = Db.Queryable<Dt_Warehouse>().ToList();
 
                List<ERPInboundDetailModel> detailModels = new List<ERPInboundDetailModel>();
                foreach (var item in inboundOrder.Details)
                {
                    Dt_StockInfo? stockInfo = stockInfos.FirstOrDefault(x => x.Details.Any(v => v.InboundOrderRowNo == item.RowNo && v.MaterielCode == item.MaterielCode));
                    if (stockInfo == null)
                    {
                        return WebResponseContent.Instance.Error($"未找到库存信息");
                    }
 
                    Dt_Warehouse? warehouse = warehouses.FirstOrDefault(x => x.WarehouseId == stockInfo.WarehouseId);
                    if (warehouse == null)
                    {
                        return WebResponseContent.Instance.Error($"未找到仓库信息");
                    }
 
                    ERPInboundDetailModel detailModel = new ERPInboundDetailModel()
                    {
                        ExpiryDate = stockInfo.Details.FirstOrDefault()?.EffectiveDate ?? "",
                        LocationCode = warehouse.WarehouseCode,
                        MaterialsCode = item.MaterielCode,
                        MfgDate = stockInfo.Details.FirstOrDefault()?.ProductionDate ?? "",
                        QtyCustoms = "0",
                        Quantity = stockInfo.Details.Sum(x => x.StockQuantity).ToString(),
                        Rack = stockInfo.LocationCode,
                        ReceiptCode = inboundOrder.UpperOrderNo,
                        ReceiptSerNo = item.RowNo.ToString()
                    };
                    detailModels.Add(detailModel);
                }
                Dt_Warehouse? warehouse2 = warehouses.FirstOrDefault(x => x.WarehouseId == inboundOrder.WarehouseId);
                if (warehouse2 == null)
                {
                    return WebResponseContent.Instance.Error($"未找到仓库信息");
                }
                ERPInboundModel model = new ERPInboundModel()
                {
                    Code = inboundOrder.InboundOrderNo,
                    CreatorCode = inboundOrder.Creater,//测试
                    EntDate = inboundOrder.CreateDate.ToString("yyyy-MM-dd HH:mm:ss"),
                    StockDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                    SuppliersId = inboundOrder.SupplierId,
                    Type = "S",
                    UniqueTag = inboundOrder.Id.ToString(),
                    WarehouseCode = warehouse2.WarehouseCode,
                    Way = 1,
                    Details = detailModels
                };
                //测试注释
                _invokeERPService.InvokeInboundOrderApi(model);
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
 
        public int GetPalletType(Dt_Warehouse warehouse, string palletCode, Dt_MaterielInfo materielInfo)
        {
            if (warehouse.WarehouseCode == WarehouseEnum.HA64.ToString())
            {
                if (palletCode.Substring(0, 1) == "6")
                {
                    return PalletTypeEnum.MediumPallet.ObjToInt();
                }
                else
                {
                    return PalletTypeEnum.LargestPallet.ObjToInt();
                }
            }
            else if (warehouse.WarehouseCode == WarehouseEnum.HA152.ObjToString())
            {
                Dt_PalletTypeInfo palletTypeInfo = _palletTypeInfoRepository.QueryFirst(x => x.CodeStartStr == palletCode.Substring(0, 2));
                if (palletTypeInfo == null)
                {
                    throw new Exception($"托盘号错误");
                }
                return palletTypeInfo.PalletType;
            }
            return -1;
        }
    }
}