647556386
2025-10-18 d01658c63cd541fe4ea5cec5c4bd7f23b9408cdb
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
using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Core;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_DTO.ERP;
using WIDESEA_DTO.Stock;
using WIDESEA_IBasicRepository;
using WIDESEA_IBasicService;
using WIDESEA_IOutboundRepository;
using WIDESEA_IOutboundService;
using WIDESEA_IRecordService;
using WIDESEA_IStockRepository;
using WIDESEA_IStockService;
using WIDESEA_Model.Models;
using WIDESEA_StockRepository;
 
namespace WIDESEA_OutboundService
{
 
    
    public partial class OutboundOrderDetailService : ServiceBase<Dt_OutboundOrderDetail, IOutboundOrderDetailRepository>, IOutboundOrderDetailService
    {
 
        public WebResponseContent LockOutboundStock(int orderDetailId)
        {
            Dt_OutboundOrderDetail outboundOrderDetail = BaseDal.QueryFirst(x => x.Id == orderDetailId);
            (bool, string) result = CheckDeital(outboundOrderDetail);
            if (!result.Item1) return WebResponseContent.Instance.Error(result.Item2);
 
            decimal needQuantity = outboundOrderDetail.OrderQuantity - outboundOrderDetail.LockQuantity;
 
            List<Dt_StockInfo> outStock = new List<Dt_StockInfo>();
 
            List<Dt_StockInfo> stockInfos = _stockService.StockInfoService.GetUseableStocks(outboundOrderDetail.MaterielCode);
            decimal stockTotalQuantity = stockInfos.Select(x => x.Details.Sum(v => v.StockQuantity - v.OutboundQuantity)).Sum(x => x);
            if (stockTotalQuantity >= needQuantity)//库存够
            {
                int index = 0;
                while (needQuantity > 0)
                {
                    Dt_StockInfo stockInfo = stockInfos[index];
                    decimal useableStockQuantity = stockInfo.Details.Where(x => x.MaterielCode == outboundOrderDetail.MaterielCode).Sum(x => x.StockQuantity - x.OutboundQuantity);
                    if (useableStockQuantity < needQuantity)
                    {
                        outboundOrderDetail.LockQuantity += useableStockQuantity;
                        stockInfo.Details.ForEach(x => x.OutboundQuantity = x.StockQuantity);
                        needQuantity -= useableStockQuantity;
                    }
                    else
                    {
                        outboundOrderDetail.LockQuantity += needQuantity;
                        stockInfo.Details.ForEach(x =>
                        {
                            if (x.StockQuantity > x.OutboundQuantity && x.MaterielCode == outboundOrderDetail.MaterielCode)
                            {
                                if (x.StockQuantity - x.OutboundQuantity >= needQuantity)
                                {
                                    x.OutboundQuantity += needQuantity;
                                    needQuantity = 0;
                                }
                                else
                                {
                                    needQuantity -= (x.StockQuantity - x.OutboundQuantity);
                                    x.OutboundQuantity = x.StockQuantity;
                                }
                            }
                        });
                    }
                    outStock.Add(stockInfo);
                    index++;
                }
                return WebResponseContent.Instance.OK(data: new { stockInfos, outboundOrderDetail });
            }
            else
            {
                for (int i = 0; i < stockInfos.Count; i++)
                {
                    Dt_StockInfo stockInfo = stockInfos[i];
                    decimal useableStockQuantity = stockInfo.Details.Where(x => x.MaterielCode == outboundOrderDetail.MaterielCode).Sum(x => x.StockQuantity - x.OutboundQuantity);
                    if (useableStockQuantity < needQuantity)
                    {
                        outboundOrderDetail.LockQuantity += useableStockQuantity;
                        stockInfo.Details.ForEach(x => x.OutboundQuantity = x.StockQuantity);
                        needQuantity -= useableStockQuantity;
                    }
                    else
                    {
                        outboundOrderDetail.LockQuantity += needQuantity;
                        stockInfo.Details.ForEach(x =>
                        {
                            if (x.StockQuantity > x.OutboundQuantity && x.MaterielCode == outboundOrderDetail.MaterielCode)
                            {
                                if (x.StockQuantity - x.OutboundQuantity >= needQuantity)
                                {
                                    x.OutboundQuantity += needQuantity;
                                    needQuantity = 0;
                                }
                                else
                                {
                                    needQuantity -= (x.StockQuantity - x.OutboundQuantity);
                                    x.OutboundQuantity = x.StockQuantity;
                                }
                            }
                        });
                    }
                    outStock.Add(stockInfo);
                }
                return WebResponseContent.Instance.OK(data: new { stockInfos, outboundOrderDetail });
            }
        }
 
        private (bool, string) CheckDeital(Dt_OutboundOrderDetail outboundOrderDetail)
        {
            if (outboundOrderDetail == null)
            {
                return (false, "未找到出库单明细信息");
            }
            if (outboundOrderDetail.OrderDetailStatus != OrderDetailStatusEnum.New.ObjToInt())
            {
                return (false, "该明细不可操作");
            }
            return (true, "成功");
        }
 
        public (List<Dt_StockInfo>, Dt_OutboundOrderDetail, List<Dt_OutStockLockInfo>, List<Dt_LocationInfo>) AssignStockOutbound(Dt_OutboundOrderDetail outboundOrderDetail, List<StockSelectViewDTO> stockSelectViews)
        {
            #region
            (bool, string) checkResult = CheckSelectStockDeital(outboundOrderDetail, stockSelectViews);
            if (!checkResult.Item1) throw new Exception(checkResult.Item2);
 
            Dt_OutboundOrder outboundOrder = _outboundRepository.OutboundOrderRepository.QueryFirst(x => x.Id == outboundOrderDetail.OrderId);
            decimal originalNeedQuantity = outboundOrderDetail.OrderQuantity - outboundOrderDetail.LockQuantity;
 
            decimal needQuantity = originalNeedQuantity;
 
            List<Dt_StockInfo> outStocks = _stockService.StockInfoService.Repository.GetStockInfosByPalletCode(stockSelectViews.Select(x => x.PalletCode).ToList());
 
            //List<Dt_StockInfo> stockInfos = _stockService.StockInfoService.GetUseableStocks(outboundOrder.MaterialCode, "", outboundOrder.WarehouseId);
            if (!outStocks.Any())
            {
                throw new Exception($"未找到可分配库存");
            }
            decimal assignQuantity = 0;
            outStocks.ForEach(x =>
            {
                x.Details.ForEach(v =>
                {
                    assignQuantity += v.StockQuantity - v.OutboundQuantity;
                });
            });
 
            outboundOrderDetail.LockQuantity += assignQuantity;
            outStocks.ForEach(x =>
            {
                x.Details.ForEach(v =>
                {
                    v.OutboundQuantity = v.StockQuantity;
                });
            });
            needQuantity -= assignQuantity;
            if (outboundOrderDetail.OrderQuantity > outboundOrderDetail.LockQuantity)
            {
                List<Dt_StockInfo> stockInfos = _stockService.StockInfoService.GetUseableStocks(outboundOrderDetail.MaterielCode);
                stockInfos = stockInfos.Where(x => !stockSelectViews.Select(v => v.PalletCode).Contains(x.PalletCode)).ToList();
                List<Dt_StockInfo> autoAssignStocks = _stockService.StockInfoService.GetOutboundStocks(stockInfos, outboundOrderDetail.MaterielCode, needQuantity, out decimal residueQuantity);
                outboundOrderDetail.LockQuantity += needQuantity - residueQuantity;
                outStocks.AddRange(autoAssignStocks);
                outboundOrderDetail.OrderDetailStatus = OrderDetailStatusEnum.AssignOver.ObjToInt();
                if (residueQuantity > 0)
                {
                    outboundOrderDetail.OrderDetailStatus = OrderDetailStatusEnum.AssignOverPartial.ObjToInt();
                }
            }
 
            List<Dt_OutStockLockInfo> outStockLockInfos = _outStockLockInfoService.GetOutStockLockInfos(outboundOrder, outboundOrderDetail, outStocks);
 
            List<Dt_LocationInfo> locationInfos = _basicService.LocationInfoService.Repository.GetLocationInfos(outStocks.Select(x => x.LocationCode).ToList());
 
            #endregion
            return (outStocks, outboundOrderDetail, outStockLockInfos, locationInfos);
        }
 
        public WebResponseContent LockOutboundStock(int orderDetailId, List<StockSelectViewDTO> stockSelectViews)
        {
            try
            {
                Dt_OutboundOrderDetail outboundOrderDetail = BaseDal.QueryFirst(x => x.Id == orderDetailId);
                (bool, string) checkResult = CheckSelectStockDeital(outboundOrderDetail, stockSelectViews);
                if (!checkResult.Item1) throw new Exception(checkResult.Item2);
 
                (List<Dt_StockInfo>, Dt_OutboundOrderDetail, List<Dt_OutStockLockInfo>, List<Dt_LocationInfo>) result = AssignStockOutbound(outboundOrderDetail, stockSelectViews);
 
                _unitOfWorkManage.BeginTran();
                WebResponseContent content = LockOutboundStockDataUpdate(result.Item1, new List<Dt_OutboundOrderDetail> { result.Item2 }, result.Item3, result.Item4);
                if (content.Status)
                {
                    _unitOfWorkManage.CommitTran();
                }
                else
                {
                    _unitOfWorkManage.RollbackTran();
                }
                return content;
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
 
        public WebResponseContent LockOutboundStock(int[] keys)
        {
            try
            {
                List<StockSelectViewDTO> stockSelectViews = new List<StockSelectViewDTO>();
                List<Dt_StockInfo> stockInfos = new List<Dt_StockInfo>();
                List<Dt_OutboundOrderDetail> outboundOrderDetails = new List<Dt_OutboundOrderDetail>();
                List<Dt_OutStockLockInfo> outStockLockInfos = new List<Dt_OutStockLockInfo>();
                List<Dt_LocationInfo> locationInfos = new List<Dt_LocationInfo>();
                foreach (var item in keys)
                {
                    Dt_OutboundOrderDetail outboundOrderDetail = BaseDal.QueryFirst(x => x.Id == item);
                    (bool, string) checkResult = CheckSelectStockDeital(outboundOrderDetail, stockSelectViews);
                    if (!checkResult.Item1) throw new Exception(checkResult.Item2);
 
                    (List<Dt_StockInfo>, Dt_OutboundOrderDetail, List<Dt_OutStockLockInfo>, List<Dt_LocationInfo>) result = AssignStockOutbound(outboundOrderDetail, stockSelectViews);
                    if (result.Item1.Count > 0)
                    {
                        stockInfos.AddRange(result.Item1);
                        outboundOrderDetails.Add(result.Item2);
                        outStockLockInfos.AddRange(result.Item3);
                        locationInfos.AddRange(result.Item4);
                    }
                }
 
                _unitOfWorkManage.BeginTran();
                WebResponseContent content = LockOutboundStockDataUpdate(stockInfos, outboundOrderDetails, outStockLockInfos, locationInfos);
                if (content.Status)
                {
                    _unitOfWorkManage.CommitTran();
                }
                else
                {
                    _unitOfWorkManage.RollbackTran();
                }
                return content;
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
 
        public WebResponseContent LockOutboundStockDataUpdate(List<Dt_StockInfo> stockInfos, List<Dt_OutboundOrderDetail> outboundOrderDetails, List<Dt_OutStockLockInfo> outStockLockInfos, List<Dt_LocationInfo> locationInfos, LocationStatusEnum locationStatus = LocationStatusEnum.Lock, List<Dt_Task>? tasks = null)
        {
            try
            {
                _stockService.StockInfoService.Repository.UpdateData(stockInfos);
                List<Dt_StockInfoDetail> stockInfoDetails = new List<Dt_StockInfoDetail>();
                foreach (var item in stockInfos)
                {
                    stockInfoDetails.AddRange(item.Details);
                }
                _stockService.StockInfoDetailService.Repository.UpdateData(stockInfoDetails);
                BaseDal.UpdateData(outboundOrderDetails);
 
                List<Dt_OutStockLockInfo> addOutStockLockInfos = outStockLockInfos.Where(x => x.Id == 0).ToList();
                if (addOutStockLockInfos != null && addOutStockLockInfos.Any())
                {
                    if (tasks != null)
                    {
                        addOutStockLockInfos.ForEach(x =>
                        {
                            x.TaskNum = tasks.FirstOrDefault(v => v.PalletCode == x.PalletCode)?.TaskNum;
                        });
                    }
 
                    _outStockLockInfoService.Repository.AddData(addOutStockLockInfos);
                }
                List<Dt_OutStockLockInfo> updateOutStockLockInfos = outStockLockInfos.Where(x => x.Id > 0).ToList();
                if (updateOutStockLockInfos != null && updateOutStockLockInfos.Any())
                {
                    _outStockLockInfoService.Repository.UpdateData(updateOutStockLockInfos);
                }
 
                _recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(locationInfos, locationStatus.ObjToInt(), StockChangeType.Outbound.ObjToInt(), "", tasks?.Select(x => x.TaskNum).ToList());
                _basicService.LocationInfoService.Repository.UpdateLocationStatus(locationInfos, locationStatus);
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
 
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="outboundOrderDetails"></param>
        /// <returns></returns>
        public (List<Dt_StockInfo>, List<Dt_OutboundOrderDetail>, List<Dt_OutStockLockInfo>, List<Dt_LocationInfo>) AssignStockOutbound(List<Dt_OutboundOrderDetail> outboundOrderDetails)
        {
            if (!outboundOrderDetails.Any())
            {
                throw new Exception($"未找到出库单明细信息");
            }
 
            if (outboundOrderDetails.GroupBy(x => x.OrderId).Count() > 1)
            {
                throw new Exception($"请勿同时操作多个单据明细");
            }
            Dt_OutboundOrder outboundOrder = _outboundRepository.OutboundOrderRepository.QueryFirst(x => x.Id == outboundOrderDetails.FirstOrDefault().OrderId);
            List<Dt_StockInfo> outStocks = new List<Dt_StockInfo>();
            List<Dt_OutboundOrderDetail> groupDetails = outboundOrderDetails.GroupBy(x => new { x.MaterielCode, x.BatchNo , x.LocationName}).Select(x => new Dt_OutboundOrderDetail { OrderQuantity = x.Sum(v => v.OrderQuantity) - x.Sum(v => v.LockQuantity), MaterielCode = x.Key.MaterielCode, BatchNo = x.Key.BatchNo,LocationName = x.Key.LocationName}).ToList();
            List<Dt_Warehouse> warehouse = new List<Dt_Warehouse>();
            if (outboundOrder.OutWareHouse == "SC01_BC")
            {
                warehouse = _basicService.WarehouseService.Repository.QueryData(x => x.WarehouseDes == outboundOrder.OutWareHouse || x.WarehouseDes =="SC02_BC");
            }
            else
            {
                warehouse = _basicService.WarehouseService.Repository.QueryData(x => x.WarehouseDes == outboundOrder.OutWareHouse);
            }
            List<Dt_OutStockLockInfo> outStockLockInfos = new List<Dt_OutStockLockInfo>();
            List<Dt_LocationInfo> locationInfos = new List<Dt_LocationInfo>();
            foreach (var item in groupDetails)
            {
                decimal originalNeedQuantity = item.OrderQuantity;
 
                decimal needQuantity = originalNeedQuantity;
 
                List<Dt_StockInfo> stockInfos = new List<Dt_StockInfo>();
                ///出库指定库位出库判断
                if (item.LocationName != null && item.LocationName != "")
                {
                   stockInfos = _stockService.StockInfoService.GetUseableStocks(item.MaterielCode, item.BatchNo, warehouse).Where(x=>x.LocationCode == item.LocationName).ToList();
                }
                else
                {
                   stockInfos = _stockService.StockInfoService.GetUseableStocks(item.MaterielCode, item.BatchNo, warehouse);
                }
                    
                if (!stockInfos.Any())
                {
                    throw new Exception($"未找到可分配库存");
                }
                List<Dt_StockInfo> autoAssignStocks = _stockService.StockInfoService.GetOutboundStocks(stockInfos, item.MaterielCode, needQuantity, out decimal residueQuantity);
 
                item.LockQuantity += needQuantity - residueQuantity;
                outStocks.AddRange(autoAssignStocks);
                decimal assignQuantity = needQuantity - residueQuantity;
 
                List<Dt_OutboundOrderDetail> details = outboundOrderDetails.Where(x => !string.IsNullOrEmpty(x.BatchNo) ? x.BatchNo == item.BatchNo : true && x.MaterielCode == item.MaterielCode).ToList();
 
                for (int i = 0; i < details.Count; i++)
                {
                    decimal orderQuantity = details[i].OrderQuantity;
                    for (int j = 0; j < autoAssignStocks.Count; j++)
                    {
                        decimal detailAssignQuantity = outStockLockInfos.Where(x => !string.IsNullOrEmpty(x.BatchNo) ? x.BatchNo == item.BatchNo : true && x.MaterielCode == item.MaterielCode && x.OrderDetailId == details[i].Id).Sum(x => x.AssignQuantity);//出库订单明细已分配数量
 
                        decimal palletAssignQuantity = outStockLockInfos.Where(x => x.BatchNo == item.BatchNo && x.MaterielCode == item.MaterielCode && x.PalletCode == autoAssignStocks[j].PalletCode).Sum(x => x.AssignQuantity);//出库详情已分配数量
                        if (string.IsNullOrEmpty(item.BatchNo))
                        {
                            palletAssignQuantity = outStockLockInfos.Where(x => x.MaterielCode == item.MaterielCode && x.PalletCode == autoAssignStocks[j].PalletCode).Sum(x => x.AssignQuantity);//出库详情已分配数量
                        }
                        decimal palletOutboundQuantity = autoAssignStocks[j].Details.Sum(x => x.OutboundQuantity);
                        if (palletAssignQuantity < palletOutboundQuantity)//如果出库详情已分配数量小于托盘已分配数量,则可以继续添加该托盘出库信息
                        {
                            decimal orderDetailNeedQuantity = details[i].OrderQuantity - detailAssignQuantity;
                            if (orderDetailNeedQuantity > autoAssignStocks[j].Details.Sum(x => x.OutboundQuantity) - palletAssignQuantity)
                            {
                                details[i].LockQuantity += autoAssignStocks[j].Details.Sum(x => x.OutboundQuantity) - palletAssignQuantity;
                                Dt_OutStockLockInfo outStockLockInfo = _outStockLockInfoService.GetOutStockLockInfo(outboundOrder, details[i], autoAssignStocks[j], autoAssignStocks[j].Details.Sum(x => x.OutboundQuantity) - palletAssignQuantity);
                                outStockLockInfos.Add(outStockLockInfo);
                            }
                            else
                            {
                                Dt_OutStockLockInfo outStockLockInfo = _outStockLockInfoService.GetOutStockLockInfo(outboundOrder, details[i], autoAssignStocks[j], details[i].OrderQuantity - details[i].LockQuantity);
                                outStockLockInfos.Add(outStockLockInfo);
                                details[i].LockQuantity = details[i].OrderQuantity;
                                break;
                            }
 
                        }
                    }
                }
 
                locationInfos.AddRange(_basicService.LocationInfoService.Repository.GetLocationInfos(outStocks.Select(x => x.LocationCode).ToList()));
            }
 
            return (outStocks, outboundOrderDetails, outStockLockInfos, locationInfos);
        }
        private (bool, string) CheckSelectStockDeital(Dt_OutboundOrderDetail outboundOrderDetail, List<StockSelectViewDTO> stockSelectViews)
        {
            if (outboundOrderDetail == null)
            {
                return (false, "未找到出库单明细信息");
            }
            if (outboundOrderDetail.OrderDetailStatus != OrderDetailStatusEnum.New.ObjToInt() && outboundOrderDetail.OrderDetailStatus != OrderDetailStatusEnum.AssignOverPartial.ObjToInt())
            {
                return (false, "该明细不可操作");
            }
            if (stockSelectViews.Sum(x => x.UseableQuantity) > outboundOrderDetail.OrderQuantity - outboundOrderDetail.LockQuantity)
            {
                return (false, "选择数量超出单据数量");
            }
            return (true, "成功");
        }
 
        public WebResponseContent RevokeLockOutboundStock(int orderDetailId)
        {
            Dt_OutboundOrderDetail outboundOrderDetail = BaseDal.QueryFirst(x => x.Id == orderDetailId);
            (bool, string) result = CheckRevoke(outboundOrderDetail);
            if (!result.Item1) return WebResponseContent.Instance.Error(result.Item2);
 
            List<Dt_OutStockLockInfo> outStockLockInfos = _outStockLockInfoService.GetByOrderDetailId(orderDetailId, OutStockStatus.已分配);
            if (outStockLockInfos.Count > 0)
            {
                List<Dt_StockInfo> stocks = _stockService.StockInfoService.Repository.GetStockInfosByPalletCodes(outStockLockInfos.Select(x => x.PalletCode).ToList());
                if (stocks.Count > 0)
                {
                    stocks.ForEach(x =>
                    {
                        x.Details.ForEach(v =>
                        {
                            v.OutboundQuantity = 0;
                        });
                    });
 
                    outboundOrderDetail.OrderDetailStatus = OrderDetailStatusEnum.New.ObjToInt();
                    outboundOrderDetail.LockQuantity = outboundOrderDetail.OverOutQuantity;
 
                    outStockLockInfos.ForEach(x =>
                    {
                        x.Status = OutStockStatus.撤销.ObjToInt();
                    });
 
                    List<Dt_LocationInfo> locationInfos = _basicService.LocationInfoService.Repository.GetLocationInfos(stocks.Select(x => x.LocationCode).ToList());
 
                    return LockOutboundStockDataUpdate(stocks, new List<Dt_OutboundOrderDetail> { outboundOrderDetail }, outStockLockInfos, locationInfos, LocationStatusEnum.InStock);
                }
                return WebResponseContent.Instance.Error("未找到库存信息");
            }
            return WebResponseContent.Instance.Error("未找到已分配的出库信息");
 
        }
 
        private (bool, string) CheckRevoke(Dt_OutboundOrderDetail outboundOrderDetail)
        {
            if (outboundOrderDetail == null)
            {
                return (false, "未找到出库单明细信息");
            }
            if (outboundOrderDetail.OrderDetailStatus != OrderDetailStatusEnum.New.ObjToInt() && outboundOrderDetail.OrderDetailStatus != OrderDetailStatusEnum.AssignOverPartial.ObjToInt() && outboundOrderDetail.OrderDetailStatus != OrderDetailStatusEnum.AssignOver.ObjToInt())
            {
                return (false, "该明细不可操作");
            }
            return (true, "成功");
        }
 
 
        public string ToCancelOutFeedbackERP = WIDESEA_Core.Helper.AppSettings.Configuration["CancelOutFeedbackERP"];
        public WebResponseContent CancelOutFeedbackERP(int[] keys)
        {
            List<Dt_OutboundOrderDetail> outboundOrderDetails = _outboundRepository.OutboundOrderDetailRepository.QueryData(x => keys.Contains(x.Id));
 
            if (outboundOrderDetails == null || outboundOrderDetails.Count == 0)
            {
                return WebResponseContent.Instance.Error("未找到出库单明细信息");
            }
            if (outboundOrderDetails.FirstOrDefault(x => x.OrderDetailStatus > OrderDetailStatusEnum.New.ObjToInt() && x.OrderDetailStatus != OrderDetailStatusEnum.AssignOverPartial.ObjToInt()) != null)
            {
                return WebResponseContent.Instance.Error("所选出库单明细存在出库中或已完成");
            }
            Dt_OutboundOrder outboundOrder = _outboundRepository.OutboundOrderRepository.QueryFirst(x => x.Id == outboundOrderDetails[0].OrderId);
            if(outboundOrder == null)
            {
                return WebResponseContent.Instance.Error("未通过该明细找到出库单信息");
            }
            if (!outboundOrder.System.Equals("ERP")) 
            {
                return WebResponseContent.Instance.Error("该出库单据非ERP推送,无法取消");
            }
            string ids ="";
            foreach (var item in outboundOrderDetails)
            {
                ids = item.LinId;
                var postContent = new MultipartFormDataContent();
                postContent.Headers.Add("ContentType", $"multipart/form-data");
                postContent.Add(new StringContent(ids), "ids");
                string result = string.Empty;
                HttpClient client = null;
                try
                {
                    using (client = new HttpClient())
                    {
                        HttpResponseMessage response = client.PostAsync(ToCancelOutFeedbackERP, postContent)
                            .ConfigureAwait(false).GetAwaiter().GetResult();
 
                        // 确保响应成功
                        response.EnsureSuccessStatusCode();
                        result = response.Content.ReadAsStringAsync()
                            .ConfigureAwait(false).GetAwaiter().GetResult();
                    }
                    _unitOfWorkManage.BeginTran();
                    _outboundRepository.OutboundOrderDetailRepository.DeleteAndMoveIntoHty(outboundOrderDetails, OperateType.人工取消);
 
                    //检查该主订单是否还有剩余明细
                    int remainingDetailsCount = _outboundRepository.OutboundOrderDetailRepository
                        .Db.Queryable<Dt_OutboundOrderDetail>()
                        .Where(d => d.OrderId == outboundOrder.Id)
                        .Count();
 
                    // 如果没有剩余明细,再删除主订单
                    if (remainingDetailsCount == 0)
                    {
                        _outboundRepository.OutboundOrderRepository.DeleteAndMoveIntoHty(outboundOrder, OperateType.人工取消);
                    }
                    _unitOfWorkManage.CommitTran();
                    // 假设ERP返回成功时返回成功响应
                    return WebResponseContent.Instance.OK(result);
 
                }
                catch (HttpRequestException ex)
                {
                    _unitOfWorkManage.RollbackTran();
                    // 处理HTTP请求相关异常
                    return WebResponseContent.Instance.Error($"HTTP请求错误: {ex.Message}");
                    
                }
                catch (Exception ex)
                {
                    _unitOfWorkManage.RollbackTran();
                    // 处理其他异常
                    return WebResponseContent.Instance.Error($"处理失败: {ex.Message}");
                }
 
            }
            return WebResponseContent.Instance.OK();
        }
 
    }
}