1
yangpeixing
2 天以前 aa4aa67abfdf69e30d5076451716e5aba11d5ec0
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MailKit.Search;
using WIDESEA_Common;
using WIDESEA_Common.OrderEnum;
using WIDESEA_Core;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_DTO.Stock;
using WIDESEA_Model.Models;
 
namespace WIDESEA_TaskInfoService
{
    public partial class TaskService
    {
        /// <summary>
        /// 成品出库
        /// </summary>
        /// <param name="keys"></param>
        /// <returns></returns>
        public WebResponseContent WMSGenerateNewOutboundTask(int[] keys)
        {
            try
            {
                List<Dt_Task> tasks = new List<Dt_Task>();
                List<Dt_LocationInfo> locationInfos = new List<Dt_LocationInfo>();
                List<Dt_OutStockLockInfo> outStockLockInfos = new List<Dt_OutStockLockInfo>();
 
                List<Dt_NewOutboundOrderDetail> outboundOrderDetails = _outboundService.NewOutboundOrderDetailService.Repository.QueryData(x => keys.Contains(x.Id));
                if (outboundOrderDetails == null || outboundOrderDetails.Count == 0)
                {
                    throw new Exception("未找到出库单明细信息");
                }
                if (outboundOrderDetails.FirstOrDefault(x => x.OrderDetailStatus > OrderDetailStatusEnum.New.ObjToInt() && x.OrderDetailStatus != OrderDetailStatusEnum.AssignOverPartial.ObjToInt()) != null)
                {
                    throw new Exception("所选出库单明细存在出库中或已完成");
                }
                if (outboundOrderDetails.FirstOrDefault()?.LPNNo == null || outboundOrderDetails.FirstOrDefault()?.LPNNo == "")
                {
                    outboundOrderDetails.Clear();
                    try
                    {
                        List<StockSelectViewDTO> stockSelectViews = new List<StockSelectViewDTO>();
                        List<Dt_StockInfo> stockInfos = new List<Dt_StockInfo>();
                        foreach (int key in keys)
                        {
 
                            (List<Dt_Task>, List<Dt_StockInfo>?, List<Dt_NewOutboundOrderDetail>?, List<Dt_OutStockLockInfo>?, List<Dt_LocationInfo>?) result = NEWOutboundTaskDataHandle(keys);
                            if (result.Item2 != null && result.Item2.Count > 0)
                            {
                                stockInfos.AddRange(result.Item2);
                            }
                            if (result.Item3 != null && result.Item3.Count > 0)
                            {
                                outboundOrderDetails.AddRange(result.Item3);
                            }
                            if (result.Item4 != null && result.Item4.Count > 0)
                            {
                                outStockLockInfos.AddRange(result.Item4);
                            }
                            if (result.Item5 != null && result.Item5.Count > 0)
                            {
                                locationInfos.AddRange(result.Item5.DistinctBy(x => x.LocationCode));
                            }
                            if (result.Item1 != null && result.Item1.Count > 0)
                            {
                                tasks.AddRange(result.Item1);
                            }
                            break;
                        }
 
                        WebResponseContent content = NEWGenerateOutboundTaskDataUpdate(tasks, stockInfos, outboundOrderDetails, outStockLockInfos, locationInfos);
                        return content;
                    }
                    catch (Exception ex)
                    {
                        _unitOfWorkManage.RollbackTran();
                        return WebResponseContent.Instance.Error(ex.Message);
                    }
                }
                else
                {
                    var lpngroup = outboundOrderDetails.GroupBy(x => x.LPNNo);
 
 
                    List<Dt_NewOutboundOrderDetail> newOutboundOrderDetails = new List<Dt_NewOutboundOrderDetail>();
                    List<Dt_StockInfo> stockInfos = new List<Dt_StockInfo>();
 
                    foreach (var lpngroups in lpngroup)
                    {
                        string lpn = lpngroups.Key;
                        List<Dt_NewOutboundOrderDetail> detailsForThisLPN = _outboundService.NewOutboundOrderDetailService.Repository.QueryData(x => x.LPNNo == lpn && x.OrderDetailStatus == OrderDetailStatusEnum.New.ObjToInt());
                        if (detailsForThisLPN.Count == 0) continue;
                        newOutboundOrderDetails.AddRange(detailsForThisLPN);
                        Dt_StockInfo? stockInfo = _stockService.StockInfoService.Repository.QueryFirst(x => x.PalletCode == lpn && x.LocationCode != null);
                        if (stockInfo == null)
                        {
                            throw new Exception($"未找到库存信息,托盘号{lpn}");
                        }
                        stockInfos.Add(stockInfo);
 
                    }
 
                    if (stockInfos == null || stockInfos.Count == 0)
                    {
                        throw new Exception("未找到库存信息");
                    }
                    List<int> orderId = newOutboundOrderDetails.Select(x => x.OrderId).Distinct().ToList();
                    List<Dt_NewOutboundOrder> outboundOrders = _outboundService.NewOutboundOrderService.Repository.QueryData(x => orderId.Contains(x.Id));
                    _unitOfWorkManage.BeginTran();
                    var Newtasks = GetTasks(stockInfos, TaskTypeEnum.OutProduct);
                    List<Dt_StockInfoDetail> stockInfoDetails = _stockService.StockInfoDetailService.Repository.QueryData(x => stockInfos.Select(x => x.Id).Contains(x.StockId));
                    List<string> palletcodelpn = new List<string>();
                    foreach (var item in newOutboundOrderDetails)
                    {
                        Dt_StockInfo? stockInfo = stockInfos.FirstOrDefault(x => x.PalletCode == item.LPNNo);
                        if (stockInfo == null) continue;
                        Dt_StockInfoDetail? stockInfoDetail = stockInfoDetails.FirstOrDefault(x => x.StockId == stockInfo.Id && x.BatchNo == item.BatchNo);
                        if (stockInfoDetail == null) continue;
                        stockInfoDetail.OutboundQuantity += item.OrderQuantity;
                        item.LockQuantity += item.OrderQuantity;
                        item.OrderDetailStatus = OrderDetailStatusEnum.Outbound.ObjToInt();
                        var outboundOrder = outboundOrders.FirstOrDefault(x => x.Id == item.OrderId);
                        Dt_OutStockLockInfo outStockLockInfo = _outboundService.OutboundStockLockInfoService.GetOutStockLockInfo(outboundOrder, item, stockInfo, stockInfoDetail, item.OrderQuantity);
                        outStockLockInfo.Status = OutStockStatus.出库中.ObjToInt();
                        item.OrderDetailStatus = OrderDetailStatusEnum.Outbound.ObjToInt();
                        outboundOrder.OrderStatus = OutboundStatusEnum.出库中.ObjToInt();
                        stockInfo.StockStatus = StockStatusEmun.出库锁定.ObjToInt();
                        var task = Newtasks.FirstOrDefault(x => x.PalletCode == item.LPNNo);
                        if (task != null && !palletcodelpn.Contains(item.LPNNo))
                        {
                            if (outboundOrder.OrderType == 1)
                            {
                                task.TaskType = TaskTypeEnum.OutProduct.ObjToInt();
                            }
                            else if (outboundOrder.OrderType == 2)
                            {
                                task.TaskType = TaskTypeEnum.OutAllocate.ObjToInt();
                            }
                            task.OrderNo = outboundOrder.OrderNo;
                            tasks.Add(task);
                            palletcodelpn.Add(task.PalletCode);
                        }
                        outStockLockInfo.TaskNum = task.TaskNum;
                        outStockLockInfos.Add(outStockLockInfo);
                    }
                    locationInfos.AddRange(_basicService.LocationInfoService.Repository.GetLocationInfos(stockInfos.Select(x => x.LocationCode).ToList()));
 
 
 
                    BaseDal.AddData(tasks);
                    _stockService.StockInfoService.Repository.UpdateData(stockInfos);
                    _stockService.StockInfoDetailService.Repository.UpdateData(stockInfoDetails);
                    _outboundService.NewOutboundOrderDetailService.Repository.UpdateData(newOutboundOrderDetails);
                    _outboundService.NewOutboundOrderService.Repository.UpdateData(outboundOrders);
                    _outboundService.OutboundStockLockInfoService.Repository.AddData(outStockLockInfos);
                    _recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(locationInfos, LocationStatusEnum.Lock.ObjToInt(), StockChangeType.Outbound.ObjToInt(), "", tasks?.Select(x => x.TaskNum).ToList());
                    _basicService.LocationInfoService.Repository.UpdateLocationStatus(locationInfos, LocationStatusEnum.Lock);
 
                    _unitOfWorkManage.CommitTran();
 
                }
                //将任务推送到WCS
                return PushTasksWCS(tasks);
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
 
        /// <summary>
        /// 合托出库
        /// </summary>
        /// <param name="keys"></param>
        /// <returns></returns>
        public WebResponseContent WMSGenerateSTOutboundTask(int[] keys)
        {
            try
            {
                List<Dt_Task> tasks = new List<Dt_Task>();
                List<Dt_LocationInfo> locationInfos = new List<Dt_LocationInfo>();
                List<Dt_OutStockLockInfo> outStockLockInfos = new List<Dt_OutStockLockInfo>();
 
                List<Dt_StockInfo> stockInfos = _stockService.StockInfoService.Repository.QueryData(x => keys.Contains(x.Id) && x.StockStatus == StockStatusEmun.入库完成.ObjToInt());
                if (stockInfos == null || stockInfos.Count == 0)
                {
                    throw new Exception("未找到库存信息,或库存状态不为入库完成");
                }
                _unitOfWorkManage.BeginTran();
                var Newtasks = GetTasks(stockInfos, TaskTypeEnum.OutSyncretism);
                List<Dt_StockInfoDetail> stockInfoDetails = _stockService.StockInfoDetailService.Repository.QueryData(x => stockInfos.Select(x => x.Id).Contains(x.StockId));
                foreach (var item in stockInfos)
                {
                    Dt_StockInfo? stockInfo = stockInfos.FirstOrDefault(x => x.PalletCode == item.PalletCode);
                    if (stockInfo == null) continue;
 
                    stockInfo.StockStatus = StockStatusEmun.出库锁定.ObjToInt();
                    var task = Newtasks.FirstOrDefault(x => x.PalletCode == item.PalletCode);
                    tasks.Add(task);
                }
                locationInfos.AddRange(_basicService.LocationInfoService.Repository.GetLocationInfos(stockInfos.Select(x => x.LocationCode).ToList()));
 
 
 
                BaseDal.AddData(tasks);
                _stockService.StockInfoService.Repository.UpdateData(stockInfos);
                _stockService.StockInfoDetailService.Repository.UpdateData(stockInfoDetails);
                _recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(locationInfos, LocationStatusEnum.Lock.ObjToInt(), StockChangeType.Outbound.ObjToInt(), "", tasks?.Select(x => x.TaskNum).ToList());
                _basicService.LocationInfoService.Repository.UpdateLocationStatus(locationInfos, LocationStatusEnum.Lock);
 
                _unitOfWorkManage.CommitTran();
                //将任务推送到WCS
                return PushTasksWCS(tasks);
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
 
        /// <summary>
        /// SMOM盘点出库
        /// </summary>
        /// <param name="keys"></param>
        /// <returns></returns>
        public WebResponseContent SMOMGenerateSTOutboundTask(int[] keys)
        {
            try
            {
                List<Dt_Task> tasks = new List<Dt_Task>();
                List<Dt_LocationInfo> locationInfos = new List<Dt_LocationInfo>();
                List<Dt_OutStockLockInfo> outStockLockInfos = new List<Dt_OutStockLockInfo>();
 
                List<Dt_TakeStockOrderCP> takeStockOrderCPs = _takeStockOrderCPRepository.QueryData(x => keys.Contains(x.Id));
                if (takeStockOrderCPs == null || takeStockOrderCPs.Count == 0)
                {
                    throw new Exception("未找到库存信息,或库存状态不为入库完成");
                }
                if (takeStockOrderCPs.FirstOrDefault(x => x.TakeStockStatus > TakeStockDetailStatusEnum.未盘点.ObjToInt()) != null)
                {
                    throw new Exception("所选出库单明细存在出库中或已完成");
                }
                List<Dt_StockInfo> stockInfos = new List<Dt_StockInfo>();
                _unitOfWorkManage.BeginTran();
                foreach (var takeStock in takeStockOrderCPs)
                {
                    takeStock.TakeStockStatus = TakeStockStatusEnum.盘点中.ObjToInt();
                    List<Dt_TakeStockOrderDetailCP> takeStockOrderDetailCPs = _takeStockOrderDetailCPRepository.QueryData(x => x.TakeStockId == takeStock.Id);
                    foreach (var item in takeStockOrderDetailCPs)
                    {
                        item.TakeDetalStatus = TakeStockDetailStatusEnum.盘点出库中.ObjToInt();
                        _takeStockOrderDetailCPRepository.UpdateData(item);
                    }
                    var lpngroup = takeStockOrderDetailCPs.GroupBy(x => x.TakePalletCode);
                    foreach (var lpngroups in lpngroup)
                    {
                        string lpn = lpngroups.Key;
                        Dt_StockInfo? stockInfo = _stockService.StockInfoService.Repository.QueryFirst(x => x.PalletCode == lpn);
                        if (stockInfo == null)
                        {
                            throw new Exception($"未找到库存信息,托盘号{lpn}");
                        }
                        stockInfos.Add(stockInfo);
                    }
                    if (stockInfos == null || stockInfos.Count == 0)
                    {
                        throw new Exception("未找到库存信息");
                    }
                }
                string orderNo = takeStockOrderCPs.FirstOrDefault().OrderNo;
                var Newtasks = GetTask(stockInfos, TaskTypeEnum.OutInventory, orderNo);
                List<Dt_StockInfoDetail> stockInfoDetails = _stockService.StockInfoDetailService.Repository.QueryData(x => stockInfos.Select(x => x.Id).Contains(x.StockId));
                foreach (var item in stockInfos)
                {
                    Dt_StockInfo? stockInfo = stockInfos.FirstOrDefault(x => x.PalletCode == item.PalletCode);
                    if (stockInfo == null) continue;
 
                    stockInfo.StockStatus = StockStatusEmun.出库锁定.ObjToInt();
                    var task = Newtasks.FirstOrDefault(x => x.PalletCode == item.PalletCode);
                    tasks.Add(task);
                }
                locationInfos.AddRange(_basicService.LocationInfoService.Repository.GetLocationInfos(stockInfos.Select(x => x.LocationCode).ToList()));
 
 
 
                BaseDal.AddData(tasks);
                _stockService.StockInfoService.Repository.UpdateData(stockInfos);
                _stockService.StockInfoDetailService.Repository.UpdateData(stockInfoDetails);
                _takeStockOrderCPRepository.UpdateData(takeStockOrderCPs);
                _recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(locationInfos, LocationStatusEnum.Lock.ObjToInt(), StockChangeType.Outbound.ObjToInt(), "", tasks?.Select(x => x.TaskNum).ToList());
                _basicService.LocationInfoService.Repository.UpdateLocationStatus(locationInfos, LocationStatusEnum.Lock);
 
                _unitOfWorkManage.CommitTran();
                //将任务推送到WCS
                return PushTasksWCS(tasks);
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
 
        /// <summary>
        /// SMOM盘点后库存调整
        /// </summary>
        /// <param name="houseAdjustment"></param>
        /// <returns></returns>
        public WebResponseContent SMOMAdjustment(HouseAdjustment houseAdjustment)
        {
            WebResponseContent responseContent = new WebResponseContent();
            try
            {
                if (houseAdjustment.DetailList.Count == 0)
                {
                    return WebResponseContent.Instance.Error("无明细信息");
                }
                List<Dt_StockInfoDetail> stockdetail = new List<Dt_StockInfoDetail>();
                foreach (var item in houseAdjustment.DetailList)
                {
                    Dt_StockInfo stockInfo = _stockRepository.StockInfoRepository.QueryFirst(x => x.PalletCode == item.LPNNo);
                    if (stockInfo == null)
                    {
                        return WebResponseContent.Instance.Error("未找到此托盘库存");
                    }
                    var dt_StockInfo = BaseDal.Db.Queryable<Dt_StockInfoDetail>().LeftJoin<Dt_StockInfo>((stockdetail, stock) => stockdetail.StockId == stock.Id)
                         .Where((stockdetail, stock) => stockdetail.MaterielCode == item.MaterielCode && stockdetail.BatchNo == item.BatchNo && stock.PalletCode == item.LPNNo).Select((stockdetail, stock) => stockdetail).First();
                    if (dt_StockInfo != null)
                    {
                        dt_StockInfo.StockQuantity = item.OrderQuantity;
                        stockdetail.Add(dt_StockInfo);
                    }
                    else
                    {
                        Dt_StockInfoDetail stockInfoDetail = new Dt_StockInfoDetail()
                        {
                            StockId = stockInfo.Id,
                            MaterielCode = item.MaterielCode,
                            MaterielName = item.MaterielName,
                            OrderNo = dt_StockInfo.OrderNo,
                            BatchNo = item.BatchNo,
                            SupplierBatch = dt_StockInfo.SupplierBatch,
                            LinId = item.LinId,
                            StockQuantity = item.OrderQuantity,
                            Status = (int)StockStatusEmun.组盘暂存,
                            Creater = "WMS",
                            CreateDate = DateTime.Now,
                            Id = item.LinId.ObjToInt(),
                            MaterieSpec = dt_StockInfo.MaterieSpec,
                            OrinalLocation = dt_StockInfo.OrinalLocation
                        };
                        stockdetail.Add(stockInfoDetail);
                    }
 
                }
                _unitOfWorkManage.BeginTran();
                _stockInfoDetailRepository.UpdateData(stockdetail);
                _unitOfWorkManage.CommitTran();
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error($"Error: {ex.Message}");
            }
            return WebResponseContent.Instance.OK();
        }
 
        /// <summary>
        /// SMOM发货后成品箱库存调整
        /// </summary>
        /// <param name="houseStockDetail"></param>
        /// <returns></returns>
        public WebResponseContent SMOMStock(HouseStockDetail houseStockDetail)
        {
            WebResponseContent responseContent = new WebResponseContent();
            try
            {
                if (houseStockDetail.DetailList.Count == 0)
                {
                    return WebResponseContent.Instance.Error("无明细信息");
                }
                List<Dt_StockInfoDetail> stockdetail = new List<Dt_StockInfoDetail>();
                var allBoxCodes = new List<string>();
                _unitOfWorkManage.BeginTran();
                foreach (var item in houseStockDetail.DetailList)
                {
                    // 检查明细列表是否存在且有效
                    if (item.LabelList == null || item.LabelList.Count == 0)
                    {
                        return WebResponseContent.Instance.Error($"托盘{item.LPNNo}无箱码明细信息");
                    }
                    allBoxCodes.AddRange(item.LabelList);
 
                    //Dt_StockInfo stockInfo = _stockRepository.StockInfoRepository.QueryFirst(x => x.PalletCode == item.LPNNo);
                    //if (stockInfo == null)
                    //{
                    //    return WebResponseContent.Instance.Error("未找到此托盘库存");
                    //}
 
                    var stockdetailbox = BaseDal.Db.Queryable<Dt_StockInfo>().LeftJoin<Dt_StockInfoDetail>((s, d) => s.Id == d.StockId).LeftJoin<Dt_StockInfoDetailCP>((s, d, cp) => d.Id == cp.StockDetailId).Where((s, d, cp) => allBoxCodes.Contains(cp.BoxCode)).Select((s, d, cp) => cp.BoxCode)
                        .Distinct()
                        .ToList();
 
                    var stockInfoDetailCPList = _stockInfoDetailCPRepository.QueryData(x => stockdetailbox.Contains(x.BoxCode)).ToList();
                    if (stockInfoDetailCPList != null)
                    {
                        _stockInfoDetailCPRepository.DeleteAndMoveIntoHty(stockInfoDetailCPList, App.User.UserId == 0 ? OperateType.自动完成 : OperateType.人工完成);
 
                        for (int i = 0; i < stockInfoDetailCPList.Count(); i++)
                        {
                            Dt_StockInfo dt_StockInfo = BaseDal.Db.Queryable<Dt_StockInfo>().Where(x => x.PalletCode == item.LPNNo).Includes(x => x.Details).First();
                            if (dt_StockInfo == null)
                            {
                                return WebResponseContent.Instance.Error($"托盘{item.LPNNo}无箱码明细信息");
                            }
                            List<Dt_StockInfoDetail> stockdetails = dt_StockInfo.Details;
 
                            var matchedDetail = stockdetails.FirstOrDefault(d => d.MaterielCode.Contains(stockInfoDetailCPList[i].PartNum) && d.BatchNo.Contains(item.BatchNo));
                            if (matchedDetail != null)
                            {
                                matchedDetail.StockQuantity -= (decimal)stockInfoDetailCPList[i].QtyOfpcs;
                                matchedDetail.OutboundQuantity += (decimal)stockInfoDetailCPList[i].QtyOfpcs;
                                _stockInfoDetailRepository.UpdateData(matchedDetail);
 
                                if (matchedDetail.StockQuantity == 0)
                                {
                                    _stockService.StockInfoDetailService.Repository.DeleteAndMoveIntoHty(matchedDetail, App.User.UserId == 0 ? OperateType.自动完成 : OperateType.人工完成);
                                }
                            }
                            else
                            {
                                return WebResponseContent.Instance.Error($"托盘{item.LPNNo}中未找到物料{stockInfoDetailCPList[i].PartNum}和批次{item.BatchNo}的明细");
                            }
                            if (dt_StockInfo.Details.Sum(x => x.StockQuantity) == 0)
                            {
                                _stockService.StockInfoService.Repository.DeleteAndMoveIntoHty(dt_StockInfo, App.User.UserId == 0 ? OperateType.自动完成 : OperateType.人工完成);
                            }
                        }
                    }
 
                }
                _unitOfWorkManage.CommitTran();
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error($"Error: {ex.Message}");
            }
            return WebResponseContent.Instance.OK();
        }
 
 
        public (List<Dt_Task>, List<Dt_StockInfo>?, List<Dt_NewOutboundOrderDetail>?, List<Dt_OutStockLockInfo>?, List<Dt_LocationInfo>?) NEWOutboundTaskDataHandle(int[] keys)
        {
            List<Dt_Task> tasks = new List<Dt_Task>();
            List<Dt_NewOutboundOrderDetail> outboundOrderDetails = _outboundService.NewOutboundOrderDetailService.Repository.QueryData(x => keys.Contains(x.Id));
 
            if (outboundOrderDetails == null || outboundOrderDetails.Count == 0)
            {
                throw new Exception("未找到出库单明细信息");
            }
            if (outboundOrderDetails.FirstOrDefault(x => x.OrderDetailStatus > OrderDetailStatusEnum.New.ObjToInt() && x.OrderDetailStatus != OrderDetailStatusEnum.AssignOverPartial.ObjToInt()) != null)
            {
                throw new Exception("所选出库单明细存在出库中或已完成");
            }
            List<Dt_StockInfo>? stockInfos = null;
            List<Dt_NewOutboundOrderDetail>? orderDetails = null;
            List<Dt_OutStockLockInfo>? outStockLockInfos = null;
            List<Dt_LocationInfo>? locationInfos = null;
            //if (outboundOrderDetail.OrderDetailStatus == OrderDetailStatusEnum.New.ObjToInt())
            {
                Dt_NewOutboundOrder dt_OutboundOrder = _outboundService.NewOutboundOrderService.Repository.QueryData(x => x.Id == outboundOrderDetails.First().OrderId).First();
                (List<Dt_StockInfo>, List<Dt_NewOutboundOrderDetail>, List<Dt_OutStockLockInfo>, List<Dt_LocationInfo>) result = new();
 
                result = _outboundService.NewOutboundOrderDetailService.CPAssignStockOutbound(outboundOrderDetails);
 
                if (result.Item1 != null && result.Item1.Count > 0)
                {
                    Dt_NewOutboundOrder outboundOrder = _outboundService.NewOutboundOrderService.Repository.QueryFirst(x => x.Id == outboundOrderDetails.FirstOrDefault().OrderId);
                    TaskTypeEnum typeEnum = outboundOrder.OrderType switch
                    {
                        (int)OrderTypeEnum.生产出库单 => TaskTypeEnum.Outbound,
                        (int)OrderTypeEnum.调拨出库单 => TaskTypeEnum.OutAllocate,
                        (int)OrderTypeEnum.反拣出库单 => TaskTypeEnum.ReverseOut,
                        _ => new TaskTypeEnum()
                    };
                    tasks = GetTasks(result.Item1, typeEnum);
                    tasks.ForEach(x =>
                    {
                        x.OrderNo = outboundOrder.OrderNo;
                    });
                    result.Item2.ForEach(x =>
                    {
                        foreach (var item in result.Item2)
                        {
                            if (item.LockQuantity > 0)
                            {
                                item.OrderDetailStatus = OrderDetailStatusEnum.Outbound.ObjToInt();
                            }
                        }
 
                    });
                    result.Item3.ForEach(x =>
                    {
                        x.Status = OutStockStatus.出库中.ObjToInt();
                    });
 
                    stockInfos = result.Item1;
                    orderDetails = result.Item2;
                    outStockLockInfos = result.Item3;
                    locationInfos = result.Item4;
                }
                else
                {
                    throw new Exception("无库存");
                }
            }
            //else
            //{
            //    List<Dt_OutStockLockInfo> stockLockInfos = _outboundService.OutboundStockLockInfoService.GetByOrderDetailId(outboundOrderDetail.OrderId, OutLockStockStatusEnum.已分配);
            //    if (stockLockInfos != null && stockLockInfos.Count > 0)
            //    {
            //        List<Dt_StockInfo> stocks = _stockService.StockInfoService.Repository.GetStockInfosByPalletCodes(stockLockInfos.Select(x => x.PalletCode).Distinct().ToList());
            //        tasks = GetTasks(stocks);
            //    }
            //}
 
            return (tasks, stockInfos, orderDetails, outStockLockInfos, locationInfos);
        }
 
 
        /// <summary>
        /// 生成出库任务后数据更新到数据库
        /// </summary>
        /// <param name="tasks"></param>
        /// <param name="stockInfos"></param>
        /// <param name="outboundOrderDetails"></param>
        /// <param name="outStockLockInfos"></param>
        /// <param name="locationInfos"></param>
        /// <returns></returns>
        public WebResponseContent NEWGenerateOutboundTaskDataUpdate(List<Dt_Task> tasks, List<Dt_StockInfo>? stockInfos = null, List<Dt_NewOutboundOrderDetail>? outboundOrderDetails = null, List<Dt_OutStockLockInfo>? outStockLockInfos = null, List<Dt_LocationInfo>? locationInfos = null)
        {
            try
            {
                WebResponseContent content = new WebResponseContent();
                _unitOfWorkManage.BeginTran();
 
                BaseDal.AddData(tasks);
                //判断移库
                //content = RelocationTasks(tasks.OrderBy(x => x.Depth).ToList());
                //if (content.Status)
                //{
                //    _unitOfWorkManage.CommitTran();
                //}
                //else
                //{
                //    _unitOfWorkManage.RollbackTran();
                //    return content;
                //}
                //BaseDal.AddData(tasks);
 
                if (stockInfos != null && outboundOrderDetails != null && outStockLockInfos != null && locationInfos != null)
                {
                    Dt_NewOutboundOrder outboundOrder = _outboundService.NewOutboundOrderService.Repository.QueryFirst(x => x.Id == outboundOrderDetails.FirstOrDefault().OrderId);
                    outboundOrder.OrderStatus = OutboundStatusEnum.出库中.ObjToInt();
                    _outboundService.NewOutboundOrderService.Repository.UpdateData(outboundOrder);
                    content = _outboundService.NewOutboundOrderDetailService.CPLockOutboundStockDataUpdate(stockInfos, outboundOrderDetails, outStockLockInfos, locationInfos, tasks: tasks);
 
                    if (!content.Status)
                    {
                        _unitOfWorkManage.RollbackTran();
                        return content;
                    }
                }
                else if (outboundOrderDetails != null && outboundOrderDetails.Count > 0)
                {
                    outboundOrderDetails.ForEach(x =>
                    {
                        x.OrderDetailStatus = OrderDetailStatusEnum.Outbound.ObjToInt();
                    });
 
                    _outboundService.NewOutboundOrderDetailService.Repository.UpdateData(outboundOrderDetails);
                }
                _unitOfWorkManage.CommitTran();
                //将任务推送到WCS
 
                if (tasks.FirstOrDefault().Roadway.Contains("SC01_BC"))
                {
                    PushTasksToWCS(tasks);
                }
                else
                {
                    PushTasksWCS(tasks);
                }
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
 
        }
 
        public List<Dt_Task> GetTask(List<Dt_StockInfo> stockInfos, TaskTypeEnum taskType, string orderNo)
        {
            List<Dt_Task> tasks = new List<Dt_Task>();
            for (int i = 0; i < stockInfos.Count; i++)
            {
                Dt_StockInfo stockInfo = stockInfos[i];
 
                if (stockInfo != null)
                {
                    Dt_LocationInfo locationInfo = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == stockInfo.LocationCode);
                    if (locationInfo == null)
                    {
                        throw new Exception("未找到出库库位");
                    }
                    Dt_Task dt_Task = BaseDal.QueryFirst(x => x.SourceAddress == locationInfo.LocationCode || x.TargetAddress == locationInfo.LocationCode);
                    if (dt_Task != null)
                    {
                        throw new Exception("该货位已存在起点任务或终点任务");
                    }
                    Dt_Task dt_Task1 = BaseDal.QueryFirst(x => x.PalletCode == stockInfo.PalletCode);
                    if (dt_Task1 != null)
                    {
                        throw new Exception("该托盘已存在任务");
                    }
                    Dt_RoadwayInfo roadwayInfo = _basicService.RoadwayInfoService.Repository.QueryFirst(x => x.RoadwayNo == locationInfo.RoadwayNo);
                    Dt_StockInfoDetail stockInfoDetail = _stockService.StockInfoDetailService.Repository.QueryFirst(x => x.StockId == stockInfo.Id);
                    if (roadwayInfo != null)
                    {
                        if (roadwayInfo.RoadwayNo.Contains("CP"))
                        {
                            Dt_Task task = new()
                            {
                                PalletType = stockInfo.PalletType,
                                OrderNo = orderNo,
                                CurrentAddress = locationInfo.LocationCode,
                                Grade = 0,
                                Creater = "WMS",
                                PalletCode = stockInfo.PalletCode,
                                NextAddress = roadwayInfo.OutSCStationCode,
                                Roadway = locationInfo.RoadwayNo,
                                SourceAddress = locationInfo.LocationCode,
                                TargetAddress = "",
                                TaskStatus = OutTaskStatusEnum.OutNew.ObjToInt(),
                                TaskType = taskType.ObjToInt(),
                                Depth = locationInfo.Depth,
                                WarehouseId = stockInfo.WarehouseId,
                                TaskNum = BaseDal.GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
                                MaterielCode = stockInfoDetail.MaterielCode,
                                Quantity = (float)stockInfoDetail.StockQuantity
                            };
                            tasks.Add(task);
                        }
                        else
                        {
                            Dt_Task task = new()
                            {
                                PalletType = stockInfo.PalletType,
                                OrderNo = stockInfoDetail.OrderNo,
                                CurrentAddress = locationInfo.LocationCode,
                                Grade = 0,
                                Creater = "WMS",
                                PalletCode = stockInfo.PalletCode,
                                NextAddress = roadwayInfo.OutSCStationCode,
                                Roadway = locationInfo.RoadwayNo,
                                SourceAddress = locationInfo.LocationCode,
                                TargetAddress = "",
                                TaskStatus = OutTaskStatusEnum.OutNew.ObjToInt(),
                                TaskType = taskType.ObjToInt(),
                                Depth = locationInfo.Depth,
                                WarehouseId = stockInfo.WarehouseId,
                                TaskNum = BaseDal.GetTaskNum(nameof(SequenceEnum.SeqTaskNum))
                            };
                            if (taskType != TaskTypeEnum.OutEmpty)
                            {
                                task.MaterielCode = stockInfo.Details?.Where(x => x.StockId == stockInfo.Id).FirstOrDefault()?.MaterielCode;
                                task.Quantity = (float)stockInfo.Details?.Where(x => x.StockId == stockInfo.Id).Sum(x => x.StockQuantity);
                            }
                            tasks.Add(task);
                        }
 
                    }
                }
 
            }
            return tasks;
        }
    }
}