pan
2025-11-28 cf3050083e157819b94781d0445547ffc73e21f2
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
using Microsoft.Extensions.Logging;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_BasicService;
using WIDESEA_Common.OrderEnum;
using WIDESEA_Common.StockEnum;
using WIDESEA_Core;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.BaseServices;
using WIDESEA_IAllocateService;
using WIDESEA_IBasicService;
using WIDESEA_IOutboundService;
using WIDESEA_IStockService;
using WIDESEA_Model.Models;
using WIDESEA_Model.Models.Basic;
using WIDESEA_Model.Models.Outbound;
 
namespace WIDESEA_OutboundService
{
    public  class OutboundBatchPickingService : ServiceBase<Dt_PickingRecord, IRepository<Dt_PickingRecord>>
    {
 
 
        private readonly IUnitOfWorkManage _unitOfWorkManage;
        public IRepository<Dt_PickingRecord> Repository => BaseDal;
 
        private readonly IStockInfoService _stockInfoService;
        private readonly IStockService _stockService;
        private readonly IOutStockLockInfoService _outStockLockInfoService;
        private readonly IStockInfoDetailService _stockInfoDetailService;
        private readonly ILocationInfoService _locationInfoService;
        private readonly IOutboundOrderDetailService _outboundOrderDetailService;
        private readonly IOutboundOrderService _outboundOrderService;
        private readonly ISplitPackageService _splitPackageService;
        private readonly IRepository<Dt_Task> _taskRepository;
        private readonly IESSApiService _eSSApiService;
        private readonly IInvokeMESService _invokeMESService;
        private readonly IDailySequenceService _dailySequenceService;
        private readonly IAllocateService _allocateService;
        private readonly IRepository<Dt_OutboundBatch> _outboundBatchRepository;
        private readonly ILogger<OutboundPickingService> _logger;
 
        private Dictionary<string, string> stations = new Dictionary<string, string>
        {
            {"2-1","2-9" },
            {"3-1","3-9" },
 
        };
 
        private Dictionary<string, string> movestations = new Dictionary<string, string>
        {
            {"2-1","2-5" },
            {"3-1","3-5" },
 
        };
 
        public OutboundBatchPickingService(IRepository<Dt_PickingRecord> BaseDal, IUnitOfWorkManage unitOfWorkManage, IStockInfoService stockInfoService, IStockService stockService,
            IOutStockLockInfoService outStockLockInfoService, IStockInfoDetailService stockInfoDetailService, ILocationInfoService locationInfoService,
            IOutboundOrderDetailService outboundOrderDetailService, ISplitPackageService splitPackageService, IOutboundOrderService outboundOrderService,
            IRepository<Dt_Task> taskRepository, IESSApiService eSSApiService, ILogger<OutboundPickingService> logger, IInvokeMESService invokeMESService, IDailySequenceService dailySequenceService, IAllocateService allocateService) : base(BaseDal)
        {
            _unitOfWorkManage = unitOfWorkManage;
            _stockInfoService = stockInfoService;
            _stockService = stockService;
            _outStockLockInfoService = outStockLockInfoService;
            _stockInfoDetailService = stockInfoDetailService;
            _locationInfoService = locationInfoService;
            _outboundOrderDetailService = outboundOrderDetailService;
            _splitPackageService = splitPackageService;
            _outboundOrderService = outboundOrderService;
            _taskRepository = taskRepository;
            _eSSApiService = eSSApiService;
            _logger = logger;
            _invokeMESService = invokeMESService;
            _dailySequenceService = dailySequenceService;
            _allocateService = allocateService;
        }
 
 
        #region 分批分拣
 
        /// <summary>
        /// 分批分拣确认
        /// </summary>
        public async Task<WebResponseContent> ConfirmBatchPicking(string orderNo, string batchNo, string palletCode, string barcode, decimal actualPickedQty)
        {
            try
            {
                _unitOfWorkManage.BeginTran();
 
                // 1. 验证分拣请求
                var validationResult = await ValidateBatchPickingRequest(orderNo, batchNo, palletCode, barcode, actualPickedQty);
                if (!validationResult.IsValid)
                    return WebResponseContent.Instance.Error(validationResult.ErrorMessage);
 
                var (lockInfo, orderDetail, stockDetail) = validationResult.Data;
 
                // 2. 执行分拣逻辑
                var pickingResult = await ExecuteBatchPickingLogic(lockInfo, orderDetail, stockDetail, actualPickedQty);
 
                // 3. 更新批次完成数量
                await UpdateBatchCompletedQuantity(batchNo, actualPickedQty);
 
                // 4. 更新订单相关数据
                await UpdateOrderRelatedData(orderDetail.Id, actualPickedQty, orderNo);
 
                // 5. 记录拣选历史
                await RecordPickingHistory(pickingResult, orderNo, palletCode, batchNo);
 
                _unitOfWorkManage.CommitTran();
 
                return WebResponseContent.Instance.OK("分批分拣成功");
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                _logger.LogError($"分批分拣失败 - OrderNo: {orderNo}, BatchNo: {batchNo}, Error: {ex.Message}");
                return WebResponseContent.Instance.Error($"分批分拣失败:{ex.Message}");
            }
        }
 
        private async Task<ValidationResult<(Dt_OutStockLockInfo, Dt_OutboundOrderDetail, Dt_StockInfoDetail)>> ValidateBatchPickingRequest(
            string orderNo, string batchNo, string palletCode, string barcode, decimal actualPickedQty)
        {
            // 查找批次锁定信息
            var lockInfo = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
                .Where(x => x.OrderNo == orderNo &&
                           x.BatchNo == batchNo &&
                           x.PalletCode == palletCode &&
                           x.CurrentBarcode == barcode &&
                           x.Status == (int)OutLockStockStatusEnum.出库中)
                .FirstAsync();
 
            if (lockInfo == null)
                return ValidationResult<(Dt_OutStockLockInfo, Dt_OutboundOrderDetail, Dt_StockInfoDetail)>.Error("未找到有效的批次锁定信息");
 
            if (actualPickedQty <= 0 || actualPickedQty > lockInfo.AssignQuantity - lockInfo.PickedQty)
                return ValidationResult<(Dt_OutStockLockInfo, Dt_OutboundOrderDetail, Dt_StockInfoDetail)>.Error("分拣数量无效");
 
            // 获取订单明细和库存明细
            var orderDetail = await _outboundOrderDetailService.Db.Queryable<Dt_OutboundOrderDetail>()
                .FirstAsync(x => x.Id == lockInfo.OrderDetailId);
 
            var stockDetail = await _stockInfoDetailService.Db.Queryable<Dt_StockInfoDetail>()
                .FirstAsync(x => x.Barcode == barcode && x.StockId == lockInfo.StockId);
 
            return ValidationResult<(Dt_OutStockLockInfo, Dt_OutboundOrderDetail, Dt_StockInfoDetail)>.Success((lockInfo, orderDetail, stockDetail));
        }
 
        private async Task<PickingResult> ExecuteBatchPickingLogic(
            Dt_OutStockLockInfo lockInfo, Dt_OutboundOrderDetail orderDetail,
            Dt_StockInfoDetail stockDetail, decimal actualPickedQty)
        {
            // 更新锁定信息
            lockInfo.PickedQty += actualPickedQty;
            if (lockInfo.PickedQty >= lockInfo.AssignQuantity)
            {
                lockInfo.Status = (int)OutLockStockStatusEnum.拣选完成;
            }
            await _outStockLockInfoService.Db.Updateable(lockInfo).ExecuteCommandAsync();
 
            // 更新库存
            stockDetail.StockQuantity -= actualPickedQty;
            stockDetail.OutboundQuantity += actualPickedQty;
 
            if (stockDetail.StockQuantity <= 0)
            {
                stockDetail.Status = (int)StockStatusEmun.出库完成;
            }
            await _stockInfoDetailService.Db.Updateable(stockDetail).ExecuteCommandAsync();
 
            return new PickingResult
            {
                FinalLockInfo = lockInfo,
                ActualPickedQty = actualPickedQty
            };
        }
 
        private async Task UpdateBatchCompletedQuantity(string batchNo, decimal pickedQty)
        {
            await _outboundBatchRepository.Db.Updateable<Dt_OutboundBatch>()
                .SetColumns(x => x.CompletedQuantity == x.CompletedQuantity + pickedQty)
                .Where(x => x.BatchNo == batchNo)
                .ExecuteCommandAsync();
 
            // 检查批次是否完成
            var batch = await _outboundBatchRepository.Db.Queryable<Dt_OutboundBatch>()
                .FirstAsync(x => x.BatchNo == batchNo);
 
            if (batch.CompletedQuantity >= batch.BatchQuantity)
            {
                batch.BatchStatus = (int)BatchStatusEnum.已完成;
                await _outboundBatchRepository.Db.Updateable(batch).ExecuteCommandAsync();
            }
        }
 
        #endregion
 
        #region 手动拆包
 
        /// <summary>
        /// 手动拆包
        /// </summary>
        public async Task<WebResponseContent> ManualSplitPackage(string orderNo, string batchNo, string originalBarcode, decimal splitQuantity)
        {
            try
            {
                _unitOfWorkManage.BeginTran();
 
                // 1. 验证拆包请求
                var validationResult = await ValidateManualSplitRequest(orderNo, batchNo, originalBarcode, splitQuantity);
                if (!validationResult.IsValid)
                    return WebResponseContent.Instance.Error(validationResult.ErrorMessage);
 
                var (lockInfo, stockDetail) = validationResult.Data;
 
                // 2. 执行拆包逻辑
                var splitResult = await ExecuteManualSplit(lockInfo, stockDetail, splitQuantity, batchNo);
 
                _unitOfWorkManage.CommitTran();
 
                return WebResponseContent.Instance.OK("手动拆包成功", new { NewBarcode = splitResult.NewBarcode });
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                _logger.LogError($"手动拆包失败 - OrderNo: {orderNo}, BatchNo: {batchNo}, Barcode: {originalBarcode}, Error: {ex.Message}");
                return WebResponseContent.Instance.Error($"手动拆包失败:{ex.Message}");
            }
        }
 
        private async Task<ValidationResult<(Dt_OutStockLockInfo, Dt_StockInfoDetail)>> ValidateManualSplitRequest(
            string orderNo, string batchNo, string originalBarcode, decimal splitQuantity)
        {
            var lockInfo = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
                .Where(x => x.OrderNo == orderNo &&
                           x.BatchNo == batchNo &&
                           x.CurrentBarcode == originalBarcode &&
                           x.Status == (int)OutLockStockStatusEnum.出库中)
                .FirstAsync();
 
            if (lockInfo == null)
                return ValidationResult<(Dt_OutStockLockInfo, Dt_StockInfoDetail)>.Error("未找到有效的锁定信息");
 
            var stockDetail = await _stockInfoDetailService.Db.Queryable<Dt_StockInfoDetail>()
                .FirstAsync(x => x.Barcode == originalBarcode && x.StockId == lockInfo.StockId);
 
            if (stockDetail.StockQuantity < splitQuantity)
                return ValidationResult<(Dt_OutStockLockInfo, Dt_StockInfoDetail)>.Error("拆包数量不能大于库存数量");
 
            if (lockInfo.AssignQuantity - lockInfo.PickedQty < splitQuantity)
                return ValidationResult<(Dt_OutStockLockInfo, Dt_StockInfoDetail)>.Error("拆包数量不能大于未拣选数量");
 
            return ValidationResult<(Dt_OutStockLockInfo, Dt_StockInfoDetail)>.Success((lockInfo, stockDetail));
        }
 
        private async Task<SplitResultDto> ExecuteManualSplit(Dt_OutStockLockInfo lockInfo, Dt_StockInfoDetail stockDetail,
            decimal splitQuantity, string batchNo)
        {
            // 生成新条码
            string newBarcode = await GenerateNewBarcode();
 
            // 创建新库存明细
            var newStockDetail = new Dt_StockInfoDetail
            {
                StockId = stockDetail.StockId,
                MaterielCode = stockDetail.MaterielCode,
                OrderNo = stockDetail.OrderNo,
                BatchNo = stockDetail.BatchNo,
                StockQuantity = splitQuantity,
                OutboundQuantity = 0,
                Barcode = newBarcode,
                Status = (int)StockStatusEmun.出库锁定,
                SupplyCode = stockDetail.SupplyCode,
                Unit = stockDetail.Unit
            };
            await _stockInfoDetailService.Db.Insertable(newStockDetail).ExecuteCommandAsync();
 
            // 更新原库存明细
            stockDetail.StockQuantity -= splitQuantity;
            await _stockInfoDetailService.Db.Updateable(stockDetail).ExecuteCommandAsync();
 
            // 创建新锁定信息
            var newLockInfo = new Dt_OutStockLockInfo
            {
                OrderNo = lockInfo.OrderNo,
                OrderDetailId = lockInfo.OrderDetailId,
                BatchNo = batchNo,
                MaterielCode = lockInfo.MaterielCode,
                StockId = lockInfo.StockId,
                OrderQuantity = splitQuantity,
                AssignQuantity = splitQuantity,
                PickedQty = 0,
                LocationCode = lockInfo.LocationCode,
                PalletCode = lockInfo.PalletCode,
                Status = (int)OutLockStockStatusEnum.出库中,
                CurrentBarcode = newBarcode,
                Operator = App.User.UserName, 
            };
            await _outStockLockInfoService.Db.Insertable(newLockInfo).ExecuteCommandAsync();
 
            // 更新原锁定信息
            lockInfo.AssignQuantity -= splitQuantity;
            await _outStockLockInfoService.Db.Updateable(lockInfo).ExecuteCommandAsync();
 
            // 记录拆包历史
            await RecordSplitHistory(lockInfo, stockDetail, splitQuantity, newBarcode);
 
            return new SplitResultDto { NewBarcode = newBarcode };
        }
 
        #endregion
 
        #region 取消拆包
 
        /// <summary>
        /// 取消拆包
        /// </summary>
        public async Task<WebResponseContent> CancelSplitPackage(string orderNo, string batchNo, string newBarcode)
        {
            try
            {
                _unitOfWorkManage.BeginTran();
 
                // 查找拆包记录和新锁定信息
                var splitRecord = await _splitPackageService.Db.Queryable<Dt_SplitPackageRecord>()
                    .Where(x => x.NewBarcode == newBarcode && x.OrderNo == orderNo && !x.IsReverted)
                    .FirstAsync();
 
                if (splitRecord == null)
                    return WebResponseContent.Instance.Error("未找到拆包记录");
 
                var newLockInfo = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
                    .Where(x => x.CurrentBarcode == newBarcode && x.BatchNo == batchNo)
                    .FirstAsync();
 
                if (newLockInfo == null)
                    return WebResponseContent.Instance.Error("未找到新锁定信息");
 
                // 恢复原库存
                var originalStock = await _stockInfoDetailService.Db.Queryable<Dt_StockInfoDetail>()
                    .FirstAsync(x => x.Barcode == splitRecord.OriginalBarcode && x.StockId == splitRecord.StockId);
 
                originalStock.StockQuantity += splitRecord.SplitQty;
                await _stockInfoDetailService.Db.Updateable(originalStock).ExecuteCommandAsync();
 
                // 恢复原锁定信息
                var originalLockInfo = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
                    .FirstAsync(x => x.Id == splitRecord.OutStockLockInfoId);
 
                originalLockInfo.AssignQuantity += splitRecord.SplitQty;
                await _outStockLockInfoService.Db.Updateable(originalLockInfo).ExecuteCommandAsync();
 
                // 删除新库存明细
                await _stockInfoDetailService.Db.Deleteable<Dt_StockInfoDetail>()
                    .Where(x => x.Barcode == newBarcode)
                    .ExecuteCommandAsync();
 
                // 删除新锁定信息
                await _outStockLockInfoService.Db.Deleteable<Dt_OutStockLockInfo>()
                    .Where(x => x.Id == newLockInfo.Id)
                    .ExecuteCommandAsync();
 
                // 标记拆包记录为已撤销
                splitRecord.IsReverted = true;
                splitRecord.RevertTime = DateTime.Now;
                splitRecord.Operator = App.User.UserName;
                await _splitPackageService.Db.Updateable(splitRecord).ExecuteCommandAsync();
 
                _unitOfWorkManage.CommitTran();
 
                return WebResponseContent.Instance.OK("取消拆包成功");
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                _logger.LogError($"取消拆包失败 - OrderNo: {orderNo}, BatchNo: {batchNo}, Barcode: {newBarcode}, Error: {ex.Message}");
                return WebResponseContent.Instance.Error($"取消拆包失败:{ex.Message}");
            }
        }
 
        #endregion
 
        #region 分批回库
 
        /// <summary>
        /// 分批回库 - 释放未拣选的库存
        /// </summary>
        public async Task<WebResponseContent> BatchReturnStock(string orderNo, string batchNo)
        {
            try
            {
                _unitOfWorkManage.BeginTran();
 
                // 1. 查找批次未完成的锁定记录
                var unfinishedLocks = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
                    .Where(x => x.OrderNo == orderNo &&
                               x.BatchNo == batchNo &&
                               x.Status == (int)OutLockStockStatusEnum.出库中)
                    .ToListAsync();
 
                if (!unfinishedLocks.Any())
                    return WebResponseContent.Instance.Error("该批次没有未完成的锁定记录");
 
                // 2. 释放库存和锁定记录
                foreach (var lockInfo in unfinishedLocks)
                {
                    await ReleaseLockAndStock(lockInfo);
                }
 
                // 3. 更新批次状态
                await UpdateBatchStatusForReturn(batchNo);
 
                // 4. 更新订单明细的已分配数量
                await UpdateOrderDetailAfterReturn(unfinishedLocks);
 
                _unitOfWorkManage.CommitTran();
 
                return WebResponseContent.Instance.OK("分批回库成功");
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                _logger.LogError($"分批回库失败 - OrderNo: {orderNo}, BatchNo: {batchNo}, Error: {ex.Message}");
                return WebResponseContent.Instance.Error($"分批回库失败:{ex.Message}");
            }
        }
 
        private async Task ReleaseLockAndStock(Dt_OutStockLockInfo lockInfo)
        {
            // 恢复库存状态
            var stockDetail = await _stockInfoDetailService.Db.Queryable<Dt_StockInfoDetail>()
                .FirstAsync(x => x.Barcode == lockInfo.CurrentBarcode && x.StockId == lockInfo.StockId);
 
            if (stockDetail != null)
            {
                stockDetail.Status = (int)StockStatusEmun.入库完成;
                await _stockInfoDetailService.Db.Updateable(stockDetail).ExecuteCommandAsync();
            }
 
            // 更新锁定记录状态为回库
            lockInfo.Status = (int)OutLockStockStatusEnum.回库中;
            await _outStockLockInfoService.Db.Updateable(lockInfo).ExecuteCommandAsync();
        }
 
        private async Task UpdateBatchStatusForReturn(string batchNo)
        {
            await _outboundBatchRepository.Db.Updateable<Dt_OutboundBatch>()
                .SetColumns(x => new Dt_OutboundBatch
                {
                    BatchStatus = (int)BatchStatusEnum.已回库,
                    Operator = App.User.UserName
                })
                .Where(x => x.BatchNo == batchNo)
                .ExecuteCommandAsync();
        }
 
        private async Task UpdateOrderDetailAfterReturn(List<Dt_OutStockLockInfo> returnedLocks)
        {
            var orderDetailGroups = returnedLocks.GroupBy(x => x.OrderDetailId);
 
            foreach (var group in orderDetailGroups)
            {
                var orderDetailId = group.Key;
                var returnedQty = group.Sum(x => x.AssignQuantity - x.PickedQty);
 
                await _outboundOrderDetailService.Db.Updateable<Dt_OutboundOrderDetail>()
                    .SetColumns(x => x.AllocatedQuantity == x.AllocatedQuantity - returnedQty)
                    .Where(x => x.Id == orderDetailId)
                    .ExecuteCommandAsync();
            }
        }
 
        #endregion
 
        #region 辅助方法
 
        private async Task<string> GenerateNewBarcode()
        {
            var seq = await _dailySequenceService.GetNextSequenceAsync();
            return "WSLOT" + DateTime.Now.ToString("yyyyMMdd") + seq.ToString()?.PadLeft(5, '0');
        }
 
        private async Task RecordSplitHistory(Dt_OutStockLockInfo lockInfo, Dt_StockInfoDetail stockDetail,decimal splitQty, string newBarcode)
        {
            var splitHistory = new Dt_SplitPackageRecord
            {
                OrderNo = lockInfo.OrderNo,
                OutStockLockInfoId = lockInfo.Id,
                StockId = stockDetail.StockId,
                Operator = App.User.UserName,
                OriginalBarcode = stockDetail.Barcode,
                NewBarcode = newBarcode,
                SplitQty = splitQty,
                SplitTime = DateTime.Now,
                Status = (int)SplitPackageStatusEnum.已拆包
            };
 
            await _splitPackageService.Db.Insertable(splitHistory).ExecuteCommandAsync();
        }
 
        private async Task RecordPickingHistory(PickingResult result, string orderNo, string palletCode, string batchNo)
        {
            var pickingRecord = new Dt_PickingRecord
            {
                OrderNo = orderNo,
               // BatchNo = batchNo,
                OrderDetailId = result.FinalLockInfo.OrderDetailId,
                PalletCode = palletCode,
                Barcode = result.FinalLockInfo.CurrentBarcode,
                MaterielCode = result.FinalLockInfo.MaterielCode,
                PickQuantity = result.ActualPickedQty,
                PickTime = DateTime.Now,
                Operator = App.User.UserName,
                OutStockLockId = result.FinalLockInfo.Id
            };
 
            await Db.Insertable(pickingRecord).ExecuteCommandAsync();
        }
 
        private async Task UpdateOrderRelatedData(int orderDetailId, decimal pickedQty, string orderNo)
        {
            // 更新订单明细的已出库数量
            await _outboundOrderDetailService.Db.Updateable<Dt_OutboundOrderDetail>()
                .SetColumns(x => new Dt_OutboundOrderDetail
                {
                    OverOutQuantity = x.OverOutQuantity + pickedQty,
                    AllocatedQuantity = x.AllocatedQuantity - pickedQty
                })
                .Where(x => x.Id == orderDetailId)
                .ExecuteCommandAsync();
 
            // 检查订单状态
            await CheckAndUpdateOrderStatus(orderNo);
        }
 
        private async Task CheckAndUpdateOrderStatus(string orderNo)
        {
            var orderDetails = await _outboundOrderDetailService.Db.Queryable<Dt_OutboundOrderDetail>()
                  .LeftJoin<Dt_OutboundOrder>((o, item) => o.OrderId == item.Id)
                  .Where((o, item) => item.OrderNo == orderNo)
                  .Select((o, item) => o)
                  .ToListAsync();
 
 
 
            bool allCompleted = orderDetails.All(x => x.OverOutQuantity >= x.NeedOutQuantity);
 
            if (allCompleted)
            {
                await _outboundOrderService.Db.Updateable<Dt_OutboundOrder>()
                    .SetColumns(x => new Dt_OutboundOrder { OrderStatus = (int)OutOrderStatusEnum.出库完成 })
                    .Where(x => x.OrderNo == orderNo)
                    .ExecuteCommandAsync();
            }
        }
 
        #endregion
    }
 
    
    // 支持类
    public class SplitResultDto
    {
        public string NewBarcode { get; set; }
    }
}