From 1a1bc81ac1d9ad656e15b409122e23f7f3763293 Mon Sep 17 00:00:00 2001
From: pan <antony1029@163.com>
Date: 星期四, 27 十一月 2025 08:27:41 +0800
Subject: [PATCH] 提交

---
 项目代码/WMS无仓储版/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundPickingService.cs |  637 +++++++++++++++++++++++++++++++++++++++++----------------
 1 files changed, 460 insertions(+), 177 deletions(-)

diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundPickingService.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundPickingService.cs"
index 14ac400..d9a755c 100644
--- "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundPickingService.cs"
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundPickingService.cs"
@@ -20,9 +20,11 @@
 using WIDESEA_Core.BaseServices;
 using WIDESEA_Core.Enums;
 using WIDESEA_Core.Helper;
+using WIDESEA_DTO.Allocate;
 using WIDESEA_DTO.Basic;
 using WIDESEA_DTO.Inbound;
 using WIDESEA_DTO.Outbound;
+using WIDESEA_IAllocateService;
 using WIDESEA_IBasicService;
 using WIDESEA_IOutboundService;
 using WIDESEA_IStockService;
@@ -50,6 +52,7 @@
         private readonly IESSApiService _eSSApiService;
         private readonly IInvokeMESService _invokeMESService;
         private readonly IDailySequenceService _dailySequenceService;
+        private readonly IAllocateService _allocateService;
 
         private readonly ILogger<OutboundPickingService> _logger;
 
@@ -70,7 +73,7 @@
         public OutboundPickingService(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) : base(BaseDal)
+            IRepository<Dt_Task> taskRepository, IESSApiService eSSApiService, ILogger<OutboundPickingService> logger, IInvokeMESService invokeMESService, IDailySequenceService dailySequenceService, IAllocateService allocateService) : base(BaseDal)
         {
             _unitOfWorkManage = unitOfWorkManage;
             _stockInfoService = stockInfoService;
@@ -86,6 +89,7 @@
             _logger = logger;
             _invokeMESService = invokeMESService;
             _dailySequenceService = dailySequenceService;
+            _allocateService = allocateService;
         }
 
 
@@ -187,10 +191,12 @@
                     return WebResponseContent.Instance.Error(overPickingValidation.ErrorMessage);
                 }
 
-                //  鎵ц鍒嗘嫞閫昏緫
+                // 鎵ц鍒嗘嫞閫昏緫锛堝彧澶勭悊搴撳瓨鍜岄攣瀹氾紝涓嶅鐞嗚鍗曪級
                 var pickingResult = await ExecutePickingLogic(lockInfo, orderDetail, stockDetail, orderNo, palletCode, barcode, actualQty);
 
-          
+                // 缁熶竴鏇存柊璁㈠崟鏁版嵁锛堟墍鏈夊垎鏀兘鍦ㄨ繖閲屾洿鏂帮級
+                await UpdateOrderRelatedData(orderDetail.Id, pickingResult.ActualPickedQty, orderNo);
+
                 // 璁板綍鎿嶄綔鍘嗗彶
                 await RecordPickingHistory(pickingResult, orderNo, palletCode);
 
@@ -205,6 +211,7 @@
                 return WebResponseContent.Instance.Error($"鎷i�夌‘璁ゅけ璐ワ細{ex.Message}");
             }
         }
+
         /// <summary>
         /// 鍙栨秷鎷i��
         /// </summary>
@@ -234,7 +241,7 @@
 
                 _unitOfWorkManage.CommitTran();
 
-                return WebResponseContent.Instance.OK($"鍙栨秷鍒嗘嫞鎴愬姛锛屾仮澶嶆暟閲忥細{pickingRecord.PickQuantity}");
+                return WebResponseContent.Instance.OK($"鍙栨秷鍒嗘嫞鎴愬姛");
             }
             catch (Exception ex)
             {
@@ -290,14 +297,15 @@
 
                 _unitOfWorkManage.CommitTran();
 
-              
+
                 // 鍒涘缓鍥炲簱浠诲姟
-                await CreateReturnTaskAndHandleESS(orderNo, palletCode, task, TaskTypeEnum.InPick);
+                await CreateReturnTaskAndHandleESS(orderNo, palletCode, task, TaskTypeEnum.InPick, task.PalletType);
 
                 // 鏇存柊璁㈠崟鐘舵�侊紙涓嶈Е鍙慚ES鍥炰紶锛�
                 await UpdateOrderStatusForReturn(orderNo);
 
-                return WebResponseContent.Instance.OK($"鍥炲簱鎿嶄綔鎴愬姛锛屽叡鍥炲簱鏁伴噺锛歿statusAnalysis.TotalReturnQty}");
+                return WebResponseContent.Instance.OK($"鍥炲簱鎿嶄綔鎴愬姛");
+                //return WebResponseContent.Instance.OK($"鍥炲簱鎿嶄綔鎴愬姛锛屽叡鍥炲簱鏁伴噺锛歿statusAnalysis.TotalReturnQty}");
             }
             catch (Exception ex)
             {
@@ -550,8 +558,8 @@
 
 
         private async Task<PickingResult> ExecutePickingLogic(
-    Dt_OutStockLockInfo lockInfo, Dt_OutboundOrderDetail orderDetail, Dt_StockInfoDetail stockDetail,
-    string orderNo, string palletCode, string barcode, decimal actualQty)
+         Dt_OutStockLockInfo lockInfo, Dt_OutboundOrderDetail orderDetail, Dt_StockInfoDetail stockDetail,
+         string orderNo, string palletCode, string barcode, decimal actualQty)
         {
             decimal stockQuantity = stockDetail.StockQuantity;
             var result = new PickingResult
@@ -562,35 +570,29 @@
                 ActualPickedQty = actualQty
             };
 
-            decimal finalPickedQty = actualQty;
-
             if (actualQty < stockQuantity)
             {
-                // 鎷嗗寘鍦烘櫙
                 await HandleSplitPacking(lockInfo, stockDetail, actualQty, stockQuantity, result);
-                finalPickedQty = actualQty;
+                // 鎷嗗寘鍦烘櫙杩斿洖瀹為檯鎷i�夋暟閲�
+                result.ActualPickedQty = actualQty;
             }
             else if (actualQty == stockQuantity)
             {
-                // 鏁村寘鎷i��
                 await HandleFullPicking(lockInfo, stockDetail, actualQty, result);
-                finalPickedQty = actualQty;
+                // 鏁村寘鎷i�夎繑鍥炲疄闄呮嫞閫夋暟閲�
+                result.ActualPickedQty = actualQty;
             }
             else
             {
-                // 閮ㄥ垎鎷i�夛紙搴撳瓨涓嶈冻锛�
                 await HandlePartialPicking(lockInfo, stockDetail, actualQty, stockQuantity, result);
-                finalPickedQty = result.ActualPickedQty; // 鍙兘琚皟鏁�
+                // 閮ㄥ垎鎷i�夎繑鍥炶皟鏁村悗鐨勬暟閲�
+                result.ActualPickedQty = result.ActualPickedQty; // 宸茬粡鍦ㄦ柟娉曞唴璋冩暣
             }
-
-            // 缁熶竴鏇存柊璁㈠崟鏁版嵁锛堟墍鏈夊垎鏀兘浠庤繖閲屾洿鏂帮級
-            await UpdateOrderRelatedData(lockInfo.OrderDetailId, finalPickedQty, orderNo);
 
             return result;
         }
-
         private async Task HandleSplitPacking(Dt_OutStockLockInfo lockInfo, Dt_StockInfoDetail stockDetail,
-            decimal actualQty, decimal stockQuantity, PickingResult result)
+       decimal actualQty, decimal stockQuantity, PickingResult result)
         {
             decimal remainingStockQty = stockQuantity - actualQty;
 
@@ -599,10 +601,10 @@
             stockDetail.OutboundQuantity = remainingStockQty;
             await _stockInfoDetailService.Db.Updateable(stockDetail).ExecuteCommandAsync();
 
-            //鐢熸垚鏂版潯鐮�
+            // 鐢熸垚鏂版潯鐮�
             string newBarcode = await GenerateNewBarcode();
 
-            //鍒涘缓鏂伴攣瀹氫俊鎭�
+            // 鍒涘缓鏂伴攣瀹氫俊鎭�
             var newLockInfo = await CreateSplitLockInfo(lockInfo, actualQty, newBarcode);
 
             // 璁板綍鎷嗗寘鍘嗗彶
@@ -619,10 +621,10 @@
             result.FinalBarcode = newBarcode;
             result.SplitResults.AddRange(CreateSplitResults(lockInfo, actualQty, remainingStockQty, newBarcode, stockDetail.Barcode));
 
-         
-            _logger.LogInformation($"鎷嗗寘鍒嗘嫞鏇存柊璁㈠崟鏄庣粏 - OrderDetailId: {lockInfo.OrderDetailId}, 鍒嗘嫞鏁伴噺: {actualQty}");
-        }
 
+
+            _logger.LogInformation($"鎷嗗寘鍒嗘嫞瀹屾垚 - OrderDetailId: {lockInfo.OrderDetailId}, 鍒嗘嫞鏁伴噺: {actualQty}");
+        }
         private async Task HandleFullPicking(Dt_OutStockLockInfo lockInfo, Dt_StockInfoDetail stockDetail,
             decimal actualQty, PickingResult result)
         {
@@ -740,7 +742,73 @@
         #endregion
 
         #region 鍙栨秷鍒嗘嫞绉佹湁鏂规硶
+        private async Task<ValidationResult<bool>> ValidateDataConsistencyBeforeCancel(CancelPickingContext context)
+        {
+            try
+            {
+                // 1. 楠岃瘉璁㈠崟鏄庣粏鏁版嵁
+                var currentOrderDetail = await _outboundOrderDetailService.Db.Queryable<Dt_OutboundOrderDetail>()
+                    .FirstAsync(x => x.Id == context.OrderDetail.Id);
 
+                if (currentOrderDetail.OverOutQuantity < context.PickingRecord.PickQuantity)
+                    return ValidationResult<bool>.Error($"璁㈠崟鏄庣粏宸插嚭搴撴暟閲�({currentOrderDetail.OverOutQuantity})灏忎簬鍙栨秷鏁伴噺({context.PickingRecord.PickQuantity})");
+
+                if (currentOrderDetail.PickedQty < context.PickingRecord.PickQuantity)
+                    return ValidationResult<bool>.Error($"璁㈠崟鏄庣粏宸叉嫞閫夋暟閲�({currentOrderDetail.PickedQty})灏忎簬鍙栨秷鏁伴噺({context.PickingRecord.PickQuantity})");
+
+                // 2. 楠岃瘉閿佸畾淇℃伅鏁版嵁
+                var currentLockInfo = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
+                    .FirstAsync(x => x.Id == context.LockInfo.Id);
+
+                if (currentLockInfo.PickedQty < context.PickingRecord.PickQuantity)
+                    return ValidationResult<bool>.Error($"閿佸畾淇℃伅宸叉嫞閫夋暟閲�({currentLockInfo.PickedQty})灏忎簬鍙栨秷鏁伴噺({context.PickingRecord.PickQuantity})");
+
+                // 3. 楠岃瘉搴撳瓨鏁版嵁
+                var currentStockDetail = await _stockInfoDetailService.Db.Queryable<Dt_StockInfoDetail>()
+                    .FirstAsync(x => x.Barcode == context.PickingRecord.Barcode && x.StockId == context.PickingRecord.StockId);
+
+                if (currentStockDetail == null)
+                    return ValidationResult<bool>.Error($"鏈壘鍒板搴旂殑搴撳瓨鏄庣粏璁板綍");
+
+                if (currentStockDetail.Status == StockStatusEmun.鍏ュ簱纭.ObjToInt() ||
+                    currentStockDetail.Status == StockStatusEmun.鍏ュ簱瀹屾垚.ObjToInt())
+                    return ValidationResult<bool>.Error($"鏉$爜{context.PickingRecord.Barcode}宸茬粡鍥炲簱锛屾棤娉曞彇娑堝垎鎷�");
+
+                // 4. 楠岃瘉鐘舵�佹祦杞殑鍚堟硶鎬�
+                if (!await CanCancelPicking(currentLockInfo, currentStockDetail))
+                    return ValidationResult<bool>.Error($"褰撳墠鐘舵�佷笉鍏佽鍙栨秷鍒嗘嫞");
+
+                return ValidationResult<bool>.Success(true);
+            }
+            catch (Exception ex)
+            {
+                _logger.LogError($"鍙栨秷鍒嗘嫞鏁版嵁涓�鑷存�ч獙璇佸け璐�: {ex.Message}");
+                return ValidationResult<bool>.Error($"鏁版嵁楠岃瘉澶辫触: {ex.Message}");
+            }
+        }
+
+        private async Task<bool> CanCancelPicking(Dt_OutStockLockInfo lockInfo, Dt_StockInfoDetail stockDetail)
+        {
+            // 閿佸畾淇℃伅鐘舵�佹鏌�
+            if (lockInfo.Status != (int)OutLockStockStatusEnum.鎷i�夊畬鎴�)
+                return false;
+
+            // 搴撳瓨鐘舵�佹鏌�
+            if (stockDetail.Status == StockStatusEmun.鍑哄簱瀹屾垚.ObjToInt())
+                return false;
+
+            // 濡傛灉鏄媶鍖呰褰曪紝杩橀渶瑕佹鏌ョ埗閿佸畾淇℃伅鐘舵��
+            if (lockInfo.IsSplitted == 1 && lockInfo.ParentLockId.HasValue)
+            {
+                var parentLock = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
+                    .FirstAsync(x => x.Id == lockInfo.ParentLockId.Value);
+
+                if (parentLock == null || parentLock.Status == (int)OutLockStockStatusEnum.鍥炲簱涓�)
+                    return false;
+            }
+
+            return true;
+        }
         private async Task<ValidationResult<(Dt_PickingRecord, Dt_OutStockLockInfo, Dt_OutboundOrderDetail)>> ValidateCancelRequest(string orderNo, string palletCode, string barcode)
         {
             // 鍩虹鍙傛暟楠岃瘉
@@ -848,30 +916,25 @@
                    stockDetail.Status == StockStatusEmun.鍏ュ簱瀹屾垚.ObjToInt();
         }
         private async Task ExecuteCancelLogic(Dt_OutStockLockInfo lockInfo, Dt_PickingRecord pickingRecord,
-            Dt_OutboundOrderDetail orderDetail, string orderNo)
+        Dt_OutboundOrderDetail orderDetail, string orderNo)
         {
             decimal cancelQty = pickingRecord.PickQuantity;
 
-            var currentStockDetail = await _stockInfoDetailService.Db.Queryable<Dt_StockInfoDetail>()
-        .Where(it => it.Barcode == pickingRecord.Barcode && it.StockId == pickingRecord.StockId)
-        .FirstAsync();
-
-            if (currentStockDetail != null &&
-                (currentStockDetail.Status == StockStatusEmun.鍏ュ簱纭.ObjToInt() ||
-                 currentStockDetail.Status == StockStatusEmun.鍏ュ簱瀹屾垚.ObjToInt()))
+            // 1. 鏁版嵁涓�鑷存�ч獙璇�
+            var context = new CancelPickingContext
             {
-                throw new Exception($"鏉$爜{pickingRecord.Barcode}宸茬粡鍥炲簱锛屾棤娉曞彇娑堝垎鎷�");
-            }
-            //   妫�鏌ュ彇娑堝悗鏁伴噺涓嶄細涓鸿礋鏁�
-            decimal newOverOutQuantity = orderDetail.OverOutQuantity - cancelQty;
-            decimal newPickedQty = orderDetail.PickedQty - cancelQty;
+                LockInfo = lockInfo,
+                PickingRecord = pickingRecord,
+                OrderDetail = orderDetail,
+                OrderNo = orderNo,
+                CancelQuantity = cancelQty
+            };
 
-            if (newOverOutQuantity < 0 || newPickedQty < 0)
-            {
-                throw new Exception($"鍙栨秷鍒嗘嫞灏嗗鑷存暟鎹紓甯革細宸插嚭搴搟newOverOutQuantity}锛屽凡鎷i�墈newPickedQty}");
-            }
+            var validationResult = await ValidateDataConsistencyBeforeCancel(context);
+            if (!validationResult.IsValid)
+                throw new Exception(validationResult.ErrorMessage);
 
-            //  澶勭悊涓嶅悓绫诲瀷鐨勫彇娑�
+            // 2. 澶勭悊涓嶅悓绫诲瀷鐨勫彇娑�
             if (lockInfo.IsSplitted == 1 && lockInfo.ParentLockId.HasValue)
             {
                 await HandleSplitBarcodeCancel(lockInfo, pickingRecord, cancelQty);
@@ -881,16 +944,23 @@
                 await HandleNormalBarcodeCancel(lockInfo, pickingRecord, cancelQty);
             }
 
-            // 鏇存柊璁㈠崟鏄庣粏
+            //  鏇存柊璁㈠崟鏄庣粏
             await UpdateOrderDetailOnCancel(pickingRecord.OrderDetailId, cancelQty);
 
             //  鍒犻櫎鎷i�夎褰�
-            await Db.Deleteable<Dt_PickingRecord>()
+            var deleteResult = await Db.Deleteable<Dt_PickingRecord>()
                 .Where(x => x.Id == pickingRecord.Id)
                 .ExecuteCommandAsync();
 
-            //  閲嶆柊妫�鏌ヨ鍗曠姸鎬�
+            if (deleteResult <= 0)
+                throw new Exception("鍒犻櫎鎷i�夎褰曞け璐�");
+
+            _logger.LogInformation($"鍒犻櫎鎷i�夎褰� - 璁板綍ID: {pickingRecord.Id}, 鏉$爜: {pickingRecord.Barcode}");
+
+            // 閲嶆柊妫�鏌ヨ鍗曠姸鎬�
             await UpdateOrderStatusForReturn(orderNo);
+
+
         }
 
         private async Task HandleSplitBarcodeCancel(Dt_OutStockLockInfo lockInfo, Dt_PickingRecord pickingRecord, decimal cancelQty)
@@ -903,61 +973,85 @@
             if (parentLockInfo == null)
                 throw new Exception("鏈壘鍒扮埗閿佸畾淇℃伅锛屾棤娉曞彇娑堟媶鍖呭垎鎷�");
 
+            // 妫�鏌ョ埗鏉$爜鍜屾媶鍖呮潯鐮佺殑鐘舵��
             if (await IsLockInfoReturned(parentLockInfo))
-            {
                 throw new Exception($"鐖舵潯鐮亄parentLockInfo.CurrentBarcode}宸茬粡鍥炲簱锛屾棤娉曞彇娑堟媶鍖呭垎鎷�");
-            }
+
             if (await IsLockInfoReturned(lockInfo))
-            {
                 throw new Exception($"鎷嗗寘鏉$爜{lockInfo.CurrentBarcode}宸茬粡鍥炲簱锛屾棤娉曞彇娑堟媶鍖呭垎鎷�");
-            }
+
             // 鎭㈠鐖堕攣瀹氫俊鎭殑鍒嗛厤鏁伴噺
             parentLockInfo.AssignQuantity += cancelQty;
+            parentLockInfo.Status = (int)OutLockStockStatusEnum.鍑哄簱涓�; // 鎭㈠涓哄嚭搴撲腑鐘舵��
             await _outStockLockInfoService.Db.Updateable(parentLockInfo).ExecuteCommandAsync();
 
-            // 鎭㈠搴撳瓨
-            var stockDetail = await _stockInfoDetailService.Db.Queryable<Dt_StockInfoDetail>()
+            // 鎭㈠鐖舵潯鐮佸簱瀛�
+            var parentStockDetail = await _stockInfoDetailService.Db.Queryable<Dt_StockInfoDetail>()
                 .Where(x => x.Barcode == parentLockInfo.CurrentBarcode && x.StockId == parentLockInfo.StockId)
                 .FirstAsync();
 
-            if (stockDetail != null)
+            if (parentStockDetail != null)
             {
-                stockDetail.StockQuantity += cancelQty;
-                stockDetail.OutboundQuantity = stockDetail.StockQuantity;
-                stockDetail.Status = StockStatusEmun.鍑哄簱閿佸畾.ObjToInt();
-                await _stockInfoDetailService.Db.Updateable(stockDetail).ExecuteCommandAsync();
+                parentStockDetail.StockQuantity += cancelQty;
+                parentStockDetail.OutboundQuantity = parentStockDetail.StockQuantity;
+                parentStockDetail.Status = StockStatusEmun.鍑哄簱閿佸畾.ObjToInt();
+                await _stockInfoDetailService.Db.Updateable(parentStockDetail).ExecuteCommandAsync();
+
+                _logger.LogInformation($"鎭㈠鐖舵潯鐮佸簱瀛� - 鏉$爜: {parentStockDetail.Barcode}, 鎭㈠鏁伴噺: {cancelQty}, 鏂板簱瀛�: {parentStockDetail.StockQuantity}");
+            }
+
+            // 澶勭悊鎷嗗寘浜х敓鐨勬柊鏉$爜搴撳瓨
+            var splitStockDetail = await _stockInfoDetailService.Db.Queryable<Dt_StockInfoDetail>()
+                .Where(x => x.Barcode == lockInfo.CurrentBarcode && x.StockId == lockInfo.StockId)
+                .FirstAsync();
+
+            if (splitStockDetail != null)
+            {
+                // 鍒犻櫎鎷嗗寘浜х敓鐨勬柊鏉$爜搴撳瓨璁板綍
+                await _stockInfoDetailService.Db.Deleteable(splitStockDetail).ExecuteCommandAsync();
+                _logger.LogInformation($"鍒犻櫎鎷嗗寘鏂版潯鐮佸簱瀛� - 鏉$爜: {splitStockDetail.Barcode}");
             }
 
             // 鏇存柊鎷嗗寘璁板綍鐘舵��
-            await _splitPackageService.Db.Updateable<Dt_SplitPackageRecord>()
+            var updateCount = await _splitPackageService.Db.Updateable<Dt_SplitPackageRecord>()
                 .SetColumns(x => new Dt_SplitPackageRecord
                 {
                     Status = (int)SplitPackageStatusEnum.宸叉挙閿�,
                     IsReverted = true,
+                    Operator = App.User.UserName,
+                    RevertTime = DateTime.Now
                 })
                 .Where(x => x.NewBarcode == lockInfo.CurrentBarcode && !x.IsReverted)
                 .ExecuteCommandAsync();
+
+            _logger.LogInformation($"鏇存柊鎷嗗寘璁板綍鐘舵�� - 鏇存柊璁板綍鏁�: {updateCount}");
 
             // 鍒犻櫎鎷嗗寘浜х敓鐨勯攣瀹氫俊鎭�
             await _outStockLockInfoService.Db.Deleteable<Dt_OutStockLockInfo>()
                 .Where(x => x.Id == lockInfo.Id)
                 .ExecuteCommandAsync();
 
-            await UpdateOrderDetailOnCancel(pickingRecord.OrderDetailId, cancelQty);
+            _logger.LogInformation($"鍒犻櫎鎷嗗寘閿佸畾淇℃伅 - 閿佸畾ID: {lockInfo.Id}, 鏉$爜: {lockInfo.CurrentBarcode}");
         }
-
         private async Task HandleNormalBarcodeCancel(Dt_OutStockLockInfo lockInfo, Dt_PickingRecord pickingRecord, decimal cancelQty)
         {
             if (await IsLockInfoReturned(lockInfo))
-            {
                 throw new Exception($"鏉$爜{lockInfo.CurrentBarcode}宸茬粡鍥炲簱锛屾棤娉曞彇娑堝垎鎷�");
-            }
+
             // 鎭㈠閿佸畾淇℃伅
             lockInfo.PickedQty -= cancelQty;
             if (lockInfo.PickedQty < 0) lockInfo.PickedQty = 0;
 
-            lockInfo.Status = (int)OutLockStockStatusEnum.鍑哄簱涓�;
+            // 鍙湁褰撴嫞閫夋暟閲忓畬鍏ㄥ彇娑堟椂鎵嶆仮澶嶇姸鎬�
+            if (lockInfo.PickedQty == 0)
+            {
+                lockInfo.Status = (int)OutLockStockStatusEnum.鍑哄簱涓�;
+            }
+
+            lockInfo.Operator = App.User.UserName;
             await _outStockLockInfoService.Db.Updateable(lockInfo).ExecuteCommandAsync();
+
+            _logger.LogInformation($"鎭㈠閿佸畾淇℃伅 - 閿佸畾ID: {lockInfo.Id}, 鎵e噺鎷i�夋暟閲�: {cancelQty}, 鏂板凡鎷i�夋暟閲�: {lockInfo.PickedQty}");
 
             // 鎭㈠搴撳瓨
             var stockDetail = await _stockInfoDetailService.Db.Queryable<Dt_StockInfoDetail>()
@@ -968,36 +1062,57 @@
             {
                 stockDetail.StockQuantity += cancelQty;
                 stockDetail.OutboundQuantity = stockDetail.StockQuantity;
-                stockDetail.Status = StockStatusEmun.鍑哄簱閿佸畾.ObjToInt();
+
+                // 鎭㈠搴撳瓨鐘舵��
+                if (stockDetail.Status == StockStatusEmun.鍑哄簱瀹屾垚.ObjToInt())
+                {
+                    stockDetail.Status = StockStatusEmun.鍑哄簱閿佸畾.ObjToInt();
+                }
+
                 await _stockInfoDetailService.Db.Updateable(stockDetail).ExecuteCommandAsync();
+
+                _logger.LogInformation($"鎭㈠搴撳瓨 - 鏉$爜: {stockDetail.Barcode}, 鎭㈠鏁伴噺: {cancelQty}, " +
+                                      $"鏂板簱瀛�: {stockDetail.StockQuantity}, 鏂扮姸鎬�: {stockDetail.Status}");
+            }
+            else
+            {
+                _logger.LogWarning($"鏈壘鍒板簱瀛樿褰� - 鏉$爜: {pickingRecord.Barcode}, 搴撳瓨ID: {pickingRecord.StockId}");
             }
         }
-
         private async Task UpdateOrderDetailOnCancel(int orderDetailId, decimal cancelQty)
         {
-            // 鑾峰彇鏈�鏂扮殑璁㈠崟鏄庣粏鏁版嵁
+            // 鑾峰彇鏈�鏂扮殑璁㈠崟鏄庣粏鏁版嵁锛堝甫閿侊級
             var currentOrderDetail = await _outboundOrderDetailService.Db.Queryable<Dt_OutboundOrderDetail>()
+                .With(SqlWith.RowLock)
                 .FirstAsync(x => x.Id == orderDetailId);
 
             decimal newOverOutQuantity = currentOrderDetail.OverOutQuantity - cancelQty;
             decimal newPickedQty = currentOrderDetail.PickedQty - cancelQty;
 
-            // 妫�鏌ュ彇娑堝悗鏁伴噺涓嶄細涓鸿礋鏁�
-            if (newOverOutQuantity < 0 || newPickedQty < 0)
-            {
-                throw new Exception($"鍙栨秷鍒嗘嫞灏嗗鑷村凡鍑哄簱鏁伴噺({newOverOutQuantity})鎴栧凡鎷i�夋暟閲�({newPickedQty})涓鸿礋鏁�");
-            }
+            // 涓ユ牸妫�鏌ュ彇娑堝悗鏁伴噺涓嶄細涓鸿礋鏁�
+            if (newOverOutQuantity < 0)
+                throw new Exception($"鍙栨秷鍒嗘嫞灏嗗鑷村凡鍑哄簱鏁伴噺({newOverOutQuantity})涓鸿礋鏁�");
 
-            await _outboundOrderDetailService.Db.Updateable<Dt_OutboundOrderDetail>()
+            if (newPickedQty < 0)
+                throw new Exception($"鍙栨秷鍒嗘嫞灏嗗鑷村凡鎷i�夋暟閲�({newPickedQty})涓鸿礋鏁�");
+
+            // 鏇存柊璁㈠崟鏄庣粏
+            var updateResult = await _outboundOrderDetailService.Db.Updateable<Dt_OutboundOrderDetail>()
                 .SetColumns(it => new Dt_OutboundOrderDetail
                 {
                     PickedQty = newPickedQty,
-                    OverOutQuantity = newOverOutQuantity,
+                    OverOutQuantity = newOverOutQuantity
                 })
                 .Where(it => it.Id == orderDetailId)
                 .ExecuteCommandAsync();
-        }
 
+            if (updateResult <= 0)
+                throw new Exception("鏇存柊璁㈠崟鏄庣粏澶辫触");
+
+            _logger.LogInformation($"鏇存柊璁㈠崟鏄庣粏 - OrderDetailId: {orderDetailId}, " +
+                                  $"鎵e噺宸插嚭搴�: {cancelQty}, 鏂板凡鍑哄簱: {newOverOutQuantity}, " +
+                                  $"鎵e噺宸叉嫞閫�: {cancelQty}, 鏂板凡鎷i��: {newPickedQty}");
+        }
         #endregion
 
         #region 鍥炲簱鎿嶄綔绉佹湁鏂规硶
@@ -1040,7 +1155,7 @@
 
             return task;
         }
- 
+
         private async Task<decimal> CalculateSplitReturnQuantity(List<Dt_SplitPackageRecord> splitRecords, int stockId)
         {
             decimal totalQty = 0;
@@ -1048,11 +1163,14 @@
 
             foreach (var splitRecord in splitRecords)
             {
+                if (splitRecord.Status != (int)SplitPackageStatusEnum.宸叉挙閿�)
+                    continue;
                 // 妫�鏌ュ師鏉$爜
                 if (!processedBarcodes.Contains(splitRecord.OriginalBarcode))
                 {
                     var originalStock = await _stockInfoDetailService.Db.Queryable<Dt_StockInfoDetail>()
-                        .Where(it => it.Barcode == splitRecord.OriginalBarcode && it.StockId == stockId)
+                        .Where(it => it.Barcode == splitRecord.OriginalBarcode && it.StockId == stockId &&
+                           it.Status != StockStatusEmun.鍑哄簱瀹屾垚.ObjToInt())
                         .FirstAsync();
 
                     if (originalStock != null && originalStock.StockQuantity > 0)
@@ -1066,7 +1184,7 @@
                 if (!processedBarcodes.Contains(splitRecord.NewBarcode))
                 {
                     var newStock = await _stockInfoDetailService.Db.Queryable<Dt_StockInfoDetail>()
-                        .Where(it => it.Barcode == splitRecord.NewBarcode && it.StockId == stockId)
+                        .Where(it => it.Barcode == splitRecord.NewBarcode && it.StockId == stockId && it.Status != StockStatusEmun.鍑哄簱瀹屾垚.ObjToInt())
                         .FirstAsync();
 
                     if (newStock != null && newStock.StockQuantity > 0)
@@ -1114,11 +1232,13 @@
                 else
                 {
                     locationtype = stockInfo.LocationType;
+                    _stockInfoService.DeleteData(stockInfo);
                 }
 
                 var targetAddress = originalTask.TargetAddress;
 
                 await CleanupZeroStockData(stockInfoId);
+
 
                 var emptystockInfo = new Dt_StockInfo() { PalletType = PalletTypeEnum.Empty.ObjToInt(), StockStatus = StockStatusEmun.缁勭洏鏆傚瓨.ObjToInt(), PalletCode = palletCode, LocationType = locationtype };
                 emptystockInfo.Details = new List<Dt_StockInfoDetail>();
@@ -1126,7 +1246,7 @@
                 //绌烘墭鐩樺浣曞鐞�  杩樻湁涓�涓嚭搴撲换鍔¤澶勭悊銆�
                 originalTask.PalletType = PalletTypeEnum.Empty.ObjToInt();
 
-                await CreateReturnTaskAndHandleESS(orderNo, palletCode, originalTask, TaskTypeEnum.InEmpty);
+                await CreateReturnTaskAndHandleESS(orderNo, palletCode, originalTask, TaskTypeEnum.InEmpty, PalletTypeEnum.Empty.ObjToInt());
 
             }
             catch (Exception ex)
@@ -1146,19 +1266,41 @@
             {
                 await HandleRemainingLocksReturn(analysis.RemainingLocks, stockInfo.Id);
 
-               // await UpdateOrderDetailsOnReturn(analysis.RemainingLocks);
-            }                
+                // await UpdateOrderDetailsOnReturn(analysis.RemainingLocks);
+            }
 
             // 澶勭悊鎵樼洏涓婂叾浠栧簱瀛樿揣鐗�
             if (analysis.HasPalletStockGoods)
             {
-                await HandlePalletStockGoodsReturn(analysis.PalletStockGoods);
+                var validStockGoods = analysis.PalletStockGoods
+            .Where(x => x.Status != StockStatusEmun.鍑哄簱瀹屾垚.ObjToInt())
+            .ToList();
+
+                if (validStockGoods.Any())
+                {
+                    await HandlePalletStockGoodsReturn(analysis.PalletStockGoods);
+                }
+                else
+                {
+                    _logger.LogInformation("娌℃湁鏈夋晥鐨勫簱瀛樿揣鐗╅渶瑕佸洖搴�");
+                }
             }
 
             // 澶勭悊鎷嗗寘璁板綍
             if (analysis.HasSplitRecords)
             {
-                await HandleSplitRecordsReturn(analysis.SplitRecords, orderNo, palletCode);
+                var validSplitRecords = analysis.SplitRecords
+            .Where(x => x.Status != (int)SplitPackageStatusEnum.宸叉嫞閫�)
+            .ToList();
+
+                if (validSplitRecords.Any())
+                {
+                    await HandleSplitRecordsReturn(analysis.SplitRecords, orderNo, palletCode);
+                }
+                else
+                {
+                    _logger.LogInformation("娌℃湁鏈夋晥鐨勬媶鍖呰褰曢渶瑕佸鐞�");
+                }
             }
 
             // 鏇存柊搴撳瓨涓昏〃鐘舵��
@@ -1182,7 +1324,13 @@
             foreach (var lockInfo in remainingLocks)
             {
                 decimal returnQty = lockInfo.AssignQuantity - lockInfo.PickedQty;
+                if (returnQty <= 0)
+                {
+                    _logger.LogWarning($"閿佸畾璁板綍{lockInfo.Id}鏃犻渶鍥炲簱锛屽垎閰嶆暟閲�: {lockInfo.AssignQuantity}, 宸叉嫞閫�: {lockInfo.PickedQty}");
+                    continue;
+                }
 
+                _logger.LogInformation($"澶勭悊閿佸畾璁板綍鍥炲簱 - 閿佸畾ID: {lockInfo.Id}, 鏉$爜: {lockInfo.CurrentBarcode}, 鍥炲簱鏁伴噺: {returnQty}");
                 // 鏌ユ壘瀵瑰簲鐨勫簱瀛樻槑缁�
                 var stockDetail = await _stockInfoDetailService.Db.Queryable<Dt_StockInfoDetail>()
                     .Where(it => it.Barcode == lockInfo.CurrentBarcode && it.StockId == lockInfo.StockId)
@@ -1198,23 +1346,23 @@
                 else
                 {
                     // 鍒涘缓鏂扮殑搴撳瓨璁板綍
-                    var newStockDetail = new Dt_StockInfoDetail
-                    {
-                        StockId = lockInfo.StockId,
-                        MaterielCode = lockInfo.MaterielCode,
-                        MaterielName = lockInfo.MaterielName,
-                        OrderNo = lockInfo.OrderNo,
-                        BatchNo = lockInfo.BatchNo,
-                        StockQuantity = returnQty,
-                        OutboundQuantity = 0,
-                        Barcode = lockInfo.CurrentBarcode,
-                        InboundOrderRowNo = "",
-                        Status = StockStatusEmun.鍏ュ簱纭.ObjToInt(),
-                        SupplyCode = lockInfo.SupplyCode,
-                        WarehouseCode = lockInfo.WarehouseCode,
-                        Unit = lockInfo.Unit,
-                    };
-                    await _stockInfoDetailService.Db.Insertable(newStockDetail).ExecuteCommandAsync();
+                    //var newStockDetail = new Dt_StockInfoDetail
+                    //{
+                    //    StockId = lockInfo.StockId,
+                    //    MaterielCode = lockInfo.MaterielCode,
+                    //    MaterielName = lockInfo.MaterielName,
+                    //    OrderNo = lockInfo.OrderNo,
+                    //    BatchNo = lockInfo.BatchNo,
+                    //    StockQuantity = returnQty,
+                    //    OutboundQuantity = 0,
+                    //    Barcode = lockInfo.CurrentBarcode,
+                    //    InboundOrderRowNo = "",
+                    //    Status = StockStatusEmun.鍏ュ簱纭.ObjToInt(),
+                    //    SupplyCode = lockInfo.SupplyCode,
+                    //    WarehouseCode = lockInfo.WarehouseCode,
+                    //    Unit = lockInfo.Unit,
+                    //};
+                    //await _stockInfoDetailService.Db.Insertable(newStockDetail).ExecuteCommandAsync();
                 }
             }
         }
@@ -1258,23 +1406,41 @@
             {
                 _logger.LogInformation($"寰呭洖搴撹揣鐗� - 鏉$爜: {stockGood.Barcode}, 鏁伴噺: {stockGood.StockQuantity}, 褰撳墠鐘舵��: {stockGood.Status}");
 
-                // 鎭㈠搴撳瓨鐘舵��
-                stockGood.OutboundQuantity = 0;
-                stockGood.Status = StockStatusEmun.鍏ュ簱纭.ObjToInt();
+                if (stockGood.Status != StockStatusEmun.鍑哄簱瀹屾垚.ObjToInt())
+                {
+                    stockGood.OutboundQuantity = 0;
+                    stockGood.Status = StockStatusEmun.鍏ュ簱纭.ObjToInt();
+                    await _stockInfoDetailService.Db.Updateable(stockGood).ExecuteCommandAsync();
 
-                await _stockInfoDetailService.Db.Updateable(stockGood).ExecuteCommandAsync();
+                    _logger.LogInformation($"搴撳瓨璐х墿鍥炲簱瀹屾垚 - 鏉$爜: {stockGood.Barcode}, 鏂扮姸鎬�: {stockGood.Status}");
+                }
+                else
+                {
+                    _logger.LogWarning($"璺宠繃宸插嚭搴撳畬鎴愮殑璐х墿 - 鏉$爜: {stockGood.Barcode}");
+                }
             }
         }
 
         private async Task HandleSplitRecordsReturn(List<Dt_SplitPackageRecord> splitRecords, string orderNo, string palletCode)
         {
+            var validRecords = splitRecords.Where(x => x.Status != (int)SplitPackageStatusEnum.宸叉嫞閫�).ToList();
+
+            if (!validRecords.Any())
+            {
+                _logger.LogInformation("娌℃湁闇�瑕佸洖搴撶殑鎷嗗寘璁板綍");
+                return;
+            }
+
+            _logger.LogInformation($"鏇存柊{validRecords.Count}鏉℃媶鍖呰褰曠姸鎬佷负宸插洖搴�");
+
             // 鏇存柊鎷嗗寘璁板綍鐘舵��
             await _splitPackageService.Db.Updateable<Dt_SplitPackageRecord>()
                 .SetColumns(x => new Dt_SplitPackageRecord
                 {
                     Status = (int)SplitPackageStatusEnum.宸插洖搴�,
+                    Operator = App.User.UserName
                 })
-                .Where(x => x.OrderNo == orderNo && x.PalletCode == palletCode && !x.IsReverted)
+                .Where(x => validRecords.Select(r => r.Id).Contains(x.Id))
                 .ExecuteCommandAsync();
         }
 
@@ -1293,7 +1459,7 @@
         /// <param name="originalTask"></param>
         /// <param name="analysis"></param>
         /// <returns></returns>
-        private async Task CreateReturnTaskAndHandleESS(string orderNo, string palletCode, Dt_Task originalTask, TaskTypeEnum taskTypeEnum)
+        private async Task CreateReturnTaskAndHandleESS(string orderNo, string palletCode, Dt_Task originalTask, TaskTypeEnum taskTypeEnum, int palletType)
         {
             var firstLocation = await _locationInfoService.Db.Queryable<Dt_LocationInfo>()
                 .FirstAsync(x => x.LocationCode == originalTask.SourceAddress);
@@ -1307,13 +1473,14 @@
                 Grade = 0,
                 PalletCode = palletCode,
                 NextAddress = "",
-                OrderNo = originalTask.OrderNo,
+                // OrderNo = originalTask.OrderNo,
+                OrderNo = orderNo,
                 Roadway = newLocation.RoadwayNo,
                 SourceAddress = stations[originalTask.TargetAddress],
                 TargetAddress = newLocation.LocationCode,
                 TaskStatus = TaskStatusEnum.New.ObjToInt(),
                 TaskType = taskTypeEnum.ObjToInt(),
-                PalletType = originalTask.PalletType,
+                PalletType = palletType,
                 WarehouseId = originalTask.WarehouseId
 
             };
@@ -1349,15 +1516,15 @@
                     containerCode = palletCode
                 });
 
-                if (moveResult)
+                //if (moveResult)
+                //{
+                // 2. 鍒涘缓鍥炲簱浠诲姟
+                var essTask = new TaskModel()
                 {
-                    // 2. 鍒涘缓鍥炲簱浠诲姟
-                    var essTask = new TaskModel()
-                    {
-                        taskType = "putaway",
-                        taskGroupCode = "",
-                        groupPriority = 0,
-                        tasks = new List<TasksType>{  new() {
+                    taskType = "putaway",
+                    taskGroupCode = "",
+                    groupPriority = 0,
+                    tasks = new List<TasksType>{  new() {
                             taskCode = returnTask.TaskNum.ToString(),
                             taskPriority = 0,
                             taskDescribe = new TaskDescribeType
@@ -1371,11 +1538,11 @@
                                 storageTag = ""
                             }
                         } }
-                    };
+                };
 
-                    var resultTask = await _eSSApiService.CreateTaskAsync(essTask);
-                    _logger.LogInformation($"ReturnRemaining 鍒涘缓浠诲姟鎴愬姛: {resultTask}");
-                }
+                var resultTask = await _eSSApiService.CreateTaskAsync(essTask);
+                _logger.LogInformation($"ReturnRemaining 鍒涘缓浠诲姟鎴愬姛: {resultTask}");
+                //}
             }
             catch (Exception ex)
             {
@@ -1418,15 +1585,19 @@
                 if (outboundOrder.OrderStatus != newStatus)
                 {
                     await _outboundOrderService.Db.Updateable<Dt_OutboundOrder>()
-                        .SetColumns(x => x.OrderStatus == newStatus)
+                        .SetColumns(x => new Dt_OutboundOrder
+                        {
+                            OrderStatus = newStatus,
+                            Operator = App.User.UserName,
+                        })
                         .Where(x => x.OrderNo == orderNo)
                         .ExecuteCommandAsync();
 
                     // 鍙湁姝e父鍒嗘嫞瀹屾垚鏃舵墠鍚慚ES鍙嶉
-                    if (allCompleted && newStatus == (int)OutOrderStatusEnum.鍑哄簱瀹屾垚)
-                    {
-                        await HandleOrderCompletion(outboundOrder, orderNo);
-                    }
+                    //if (allCompleted && newStatus == (int)OutOrderStatusEnum.鍑哄簱瀹屾垚)
+                    //{
+                    //    await HandleOrderCompletion(outboundOrder, orderNo);
+                    //}
                 }
             }
             catch (Exception ex)
@@ -1435,7 +1606,7 @@
             }
         }
 
-        
+
 
         private async Task UpdateOrderStatusForReturn(string orderNo)
         {
@@ -1467,7 +1638,11 @@
                 if (outboundOrder.OrderStatus != newStatus)
                 {
                     await _outboundOrderService.Db.Updateable<Dt_OutboundOrder>()
-                        .SetColumns(x => x.OrderStatus == newStatus)
+                          .SetColumns(x => new Dt_OutboundOrder
+                          {
+                              OrderStatus = newStatus,
+                              Operator = App.User.UserName,
+                          })
                         .Where(x => x.OrderNo == orderNo)
                         .ExecuteCommandAsync();
 
@@ -1483,73 +1658,141 @@
         private async Task HandleOrderCompletion(Dt_OutboundOrder outboundOrder, string orderNo)
         {
             // 璋冩嫧鍑哄簱鍜岄噸妫�鍑哄簱涓嶉渶瑕佸弽棣圡ES
-            if (outboundOrder.OrderType == OutOrderTypeEnum.Allocate.ObjToInt() ||
-                outboundOrder.OrderType == OutOrderTypeEnum.ReCheck.ObjToInt())
+            if (outboundOrder.OrderType == OutOrderTypeEnum.Allocate.ObjToInt())
             {
-                return;
-            }
-
-            try
-            {
-                var feedmodel = new FeedbackOutboundRequestModel
+                var allocate = _allocateService.Repository.QueryData(x => x.UpperOrderNo == outboundOrder.UpperOrderNo).First();
+                var feedmodel = new AllocateDto
                 {
-                    reqCode = Guid.NewGuid().ToString(),
-                    reqTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
-                    business_type = outboundOrder.BusinessType,
-                    factoryArea = outboundOrder.FactoryArea,
-                    operationType = 1,
+                    ReqCode = Guid.NewGuid().ToString(),
+                    ReqTime = DateTime.Now.ToString(),
+                    BusinessType = "3",
+                  
+                    FactoryArea = outboundOrder.FactoryArea,
+                    OperationType = 1,
                     Operator = App.User.UserName,
-                    orderNo = outboundOrder.UpperOrderNo,
-                    documentsNO = outboundOrder.OrderNo,
-                    status = outboundOrder.OrderStatus,
-                    details = new List<FeedbackOutboundDetailsModel>()
-                };
+                    OrderNo = outboundOrder.UpperOrderNo,
+                   // documentsNO = outboundOrder.OrderNo,
+                   // status = outboundOrder.OrderStatus,
+                    fromWarehouse = allocate?.FromWarehouse ?? "",
+                    toWarehouse = allocate?.ToWarehouse ?? "",
+                    Details = new List<AllocateDtoDetail>()
 
+                };
                 // 鍙幏鍙栧凡鎷i�夊畬鎴愮殑閿佸畾璁板綍
                 var lists = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
                     .Where(x => x.OrderNo == orderNo && x.Status == (int)OutLockStockStatusEnum.鎷i�夊畬鎴�)
                     .ToListAsync();
 
                 var groupedData = lists.GroupBy(item => new { item.MaterielCode, item.lineNo, item.Unit, item.WarehouseCode })
-                   .Select(group => new FeedbackOutboundDetailsModel
+                   .Select(group => new AllocateDtoDetail
                    {
-                       materialCode = group.Key.MaterielCode,
-                       lineNo = group.Key.lineNo,
-                       warehouseCode = group.Key.WarehouseCode,
-                       qty = group.Sum(x => x.PickedQty),
-                       currentDeliveryQty = group.Sum(x => x.PickedQty),
-                       unit = group.Key.Unit,
-                       barcodes = group.Select(row => new WIDESEA_DTO.Outbound.BarcodesModel
+                       MaterialCode = group.Key.MaterielCode,
+                       LineNo = group.Key.lineNo,
+                       WarehouseCode = group.Key.WarehouseCode,
+                       Qty = group.Sum(x => x.PickedQty),
+                      
+                       Unit = group.Key.Unit,
+                       Barcodes = group.Select(row => new BarcodeInfo
                        {
-                           barcode = row.CurrentBarcode,
-                           supplyCode = row.SupplyCode,
-                           batchNo = row.BatchNo,
-                           unit = row.Unit,
-                           qty = row.PickedQty
+                           Barcode = row.CurrentBarcode,
+                           SupplyCode = row.SupplyCode,
+                           BatchNo = row.BatchNo,
+                           Unit = row.Unit,
+                           Qty = row.PickedQty
                        }).ToList()
+
+                  
                    }).ToList();
+                feedmodel.Details = groupedData;
 
-                feedmodel.details = groupedData;
-
-                var result = await _invokeMESService.FeedbackOutbound(feedmodel);
+                var result = await _invokeMESService.FeedbackAllocate(feedmodel);
                 if (result != null && result.code == 200)
-                {
+                {                    
                     await _outboundOrderDetailService.Db.Updateable<Dt_OutboundOrderDetail>()
-                        .SetColumns(x => x.ReturnToMESStatus == 1)
-                        .Where(x => x.OrderId == outboundOrder.Id)
-                        .ExecuteCommandAsync();
+                           .SetColumns(x => x.ReturnToMESStatus == 1)
+                           .Where(x => x.OrderId == outboundOrder.Id).ExecuteCommandAsync();
 
                     await _outboundOrderService.Db.Updateable<Dt_OutboundOrder>()
-                        .SetColumns(x => x.ReturnToMESStatus == 1)
-                        .Where(x => x.OrderNo == orderNo)
-                        .ExecuteCommandAsync();
+                          .SetColumns(x => new Dt_OutboundOrder
+                          {
+                              ReturnToMESStatus = 1,
+                              Operator = App.User.UserName,
+                          }).Where(x => x.OrderNo == orderNo).ExecuteCommandAsync();
                 }
-
-                _logger.LogError($"FeedbackOutbound鎴愬姛 - OrderNo: {orderNo}, {JsonSerializer.Serialize(result)}");
             }
-            catch (Exception ex)
+            else if (outboundOrder.OrderType == OutOrderTypeEnum.ReCheck.ObjToInt())
             {
-                _logger.LogError($"FeedbackOutbound澶辫触 - OrderNo: {orderNo}, Error: {ex.Message}");
+
+            }
+            else
+            {
+                try
+                {
+                    var feedmodel = new FeedbackOutboundRequestModel
+                    {
+                        reqCode = Guid.NewGuid().ToString(),
+                        reqTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
+                        business_type = outboundOrder.BusinessType,
+                        factoryArea = outboundOrder.FactoryArea,
+                        operationType = 1,
+                        Operator = App.User.UserName,
+                        orderNo = outboundOrder.UpperOrderNo,
+                        documentsNO = outboundOrder.OrderNo,
+                        status = outboundOrder.OrderStatus,
+                        details = new List<FeedbackOutboundDetailsModel>()
+                    };
+
+                    // 鍙幏鍙栧凡鎷i�夊畬鎴愮殑閿佸畾璁板綍
+                    var lists = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
+                        .Where(x => x.OrderNo == orderNo && x.Status == (int)OutLockStockStatusEnum.鎷i�夊畬鎴�)
+                        .ToListAsync();
+
+                    var groupedData = lists.GroupBy(item => new { item.MaterielCode, item.lineNo, item.Unit, item.WarehouseCode })
+                       .Select(group => new FeedbackOutboundDetailsModel
+                       {
+                           materialCode = group.Key.MaterielCode,
+                           lineNo = group.Key.lineNo,
+                           warehouseCode = group.Key.WarehouseCode,
+                           qty = group.Sum(x => x.PickedQty),
+                           currentDeliveryQty = group.Sum(x => x.PickedQty),
+                           unit = group.Key.Unit,
+                           barcodes = group.Select(row => new WIDESEA_DTO.Outbound.BarcodesModel
+                           {
+                               barcode = row.CurrentBarcode,
+                               supplyCode = row.SupplyCode,
+                               batchNo = row.BatchNo,
+                               unit = row.Unit,
+                               qty = row.PickedQty
+                           }).ToList()
+                       }).ToList();
+
+                    feedmodel.details = groupedData;
+
+                    var result = await _invokeMESService.FeedbackOutbound(feedmodel);
+                    if (result != null && result.code == 200)
+                    {
+                        await _outboundOrderDetailService.Db.Updateable<Dt_OutboundOrderDetail>()
+                            .SetColumns(x => x.ReturnToMESStatus == 1)
+                            .Where(x => x.OrderId == outboundOrder.Id)
+                            .ExecuteCommandAsync();
+
+                        await _outboundOrderService.Db.Updateable<Dt_OutboundOrder>()
+                              .SetColumns(x => new Dt_OutboundOrder
+                              {
+                                  ReturnToMESStatus = 1,
+                                  Operator = App.User.UserName,
+                              })
+
+                            .Where(x => x.OrderNo == orderNo)
+                            .ExecuteCommandAsync();
+                    }
+
+                    _logger.LogError($"FeedbackOutbound鎴愬姛 - OrderNo: {orderNo}, {JsonSerializer.Serialize(result)}");
+                }
+                catch (Exception ex)
+                {
+                    _logger.LogError($"FeedbackOutbound澶辫触 - OrderNo: {orderNo}, Error: {ex.Message}");
+                }
             }
         }
 
@@ -1682,6 +1925,7 @@
             if (outboundOrder != null && allCompleted && outboundOrder.OrderStatus != (int)OutOrderStatusEnum.鍑哄簱瀹屾垚)
             {
                 outboundOrder.OrderStatus = (int)OutOrderStatusEnum.鍑哄簱瀹屾垚;
+                outboundOrder.Operator = App.User.UserName;
                 await _outboundOrderService.Db.Updateable(outboundOrder).ExecuteCommandAsync();
 
                 _logger.LogInformation($"璁㈠崟 {orderNo} 宸叉爣璁颁负鍑哄簱瀹屾垚");
@@ -1731,7 +1975,7 @@
                 StockId = stockId
             };
 
-            // 1. 鍒嗘瀽鏈垎鎷g殑鍑哄簱閿佸畾璁板綍
+            // 鍒嗘瀽鏈垎鎷g殑鍑哄簱閿佸畾璁板綍
             var remainingLocks = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
                 .Where(it => it.OrderNo == orderNo &&
                            it.PalletCode == palletCode &&
@@ -1743,9 +1987,10 @@
                 result.HasRemainingLocks = true;
                 result.RemainingLocks = remainingLocks;
                 result.RemainingLocksReturnQty = remainingLocks.Sum(x => x.AssignQuantity - x.PickedQty);
+                _logger.LogInformation($"鍙戠幇{remainingLocks.Count}鏉℃湭鍒嗘嫞閿佸畾璁板綍锛屾�绘暟閲�: {result.RemainingLocksReturnQty}");
             }
 
-            // 2. 鍒嗘瀽鎵樼洏涓婄殑搴撳瓨璐х墿
+            // 鍒嗘瀽鎵樼洏涓婄殑搴撳瓨璐х墿
             var palletStockGoods = await _stockInfoDetailService.Db.Queryable<Dt_StockInfoDetail>()
                 .Where(it => it.StockId == stockId &&
                      (it.Status == StockStatusEmun.鍏ュ簱纭.ObjToInt() ||
@@ -1759,13 +2004,21 @@
                 result.HasPalletStockGoods = true;
                 result.PalletStockGoods = palletStockGoods;
                 result.PalletStockReturnQty = palletStockGoods.Sum(x => x.StockQuantity);
+                _logger.LogInformation($"鍙戠幇{palletStockGoods.Count}涓簱瀛樿揣鐗╋紝鎬绘暟閲�: {result.PalletStockReturnQty}");
+
+                // 璁板綍璇︾粏鐘舵�佸垎甯�
+                var statusGroups = palletStockGoods.GroupBy(x => x.Status);
+                foreach (var group in statusGroups)
+                {
+                    _logger.LogInformation($"搴撳瓨鐘舵�亄group.Key}: {group.Count()}涓揣鐗╋紝鏁伴噺: {group.Sum(x => x.StockQuantity)}");
+                }
             }
 
-            // 3. 鍒嗘瀽鎷嗗寘璁板綍
+            //鍒嗘瀽鎷嗗寘璁板綍
             var splitRecords = await _splitPackageService.Db.Queryable<Dt_SplitPackageRecord>()
                 .Where(it => it.OrderNo == orderNo &&
                            it.PalletCode == palletCode &&
-                           !it.IsReverted &&
+                           !it.IsReverted && it.Status != (int)SplitPackageStatusEnum.宸叉嫞閫� &&
                            it.Status != (int)SplitPackageStatusEnum.宸插洖搴�)
                 .ToListAsync();
 
@@ -1774,6 +2027,8 @@
                 result.HasSplitRecords = true;
                 result.SplitRecords = splitRecords;
                 result.SplitReturnQty = await CalculateSplitReturnQuantity(splitRecords, stockId);
+
+                _logger.LogInformation($"鍙戠幇{splitRecords.Count}鏉℃湭鎷i�夋媶鍖呰褰曪紝鎬绘暟閲�: {result.SplitReturnQty}");
             }
 
             // 4. 璁$畻鎬诲洖搴撴暟閲忓拰绌烘墭鐩樼姸鎬�
@@ -1787,6 +2042,10 @@
                            x.PalletCode == palletCode &&
                            x.TaskStatus == (int)TaskStatusEnum.New)
                 .AnyAsync();
+
+            _logger.LogInformation($"鎵樼洏鐘舵�佸垎鏋愬畬鎴� - 璁㈠崟: {orderNo}, 鎵樼洏: {palletCode}, " +
+                                  $"鎬诲洖搴撴暟閲�: {result.TotalReturnQty}, 鏄惁绌烘墭鐩�: {result.IsEmptyPallet}, " +
+                                  $"鏈夎繘琛屼腑浠诲姟: {result.HasActiveTasks}");
 
             return result;
         }
@@ -2094,5 +2353,29 @@
         public bool CanReturn => HasItemsToReturn && !HasActiveTasks;
         public bool CanRemove => IsEmptyPallet && !HasActiveTasks;
     }
+    public class PickingContext
+    {
+        public string OrderNo { get; set; }
+        public string PalletCode { get; set; }
+        public string Barcode { get; set; }
+        public string Operator { get; set; }
+        public Dt_OutStockLockInfo LockInfo { get; set; }
+        public Dt_OutboundOrderDetail OrderDetail { get; set; }
+        public Dt_StockInfoDetail StockDetail { get; set; }
+        public decimal ActualQuantity { get; set; }
+        public string AdjustedReason { get; set; }
+    }
+    public class CancelPickingContext
+    {
+        public string OrderNo { get; set; }
+        public string PalletCode { get; set; }
+        public string Barcode { get; set; }
+        public string Operator { get; set; }
+
+        public decimal CancelQuantity { get; set; }
+        public Dt_PickingRecord PickingRecord { get; set; }
+        public Dt_OutStockLockInfo LockInfo { get; set; }
+        public Dt_OutboundOrderDetail OrderDetail { get; set; }
+    }
     #endregion
 }

--
Gitblit v1.9.3