From 213c9c2a2c24eecbb9d6716f190cc265101a642e Mon Sep 17 00:00:00 2001
From: pan <antony1029@163.com>
Date: 星期一, 01 十二月 2025 11:51:02 +0800
Subject: [PATCH] 提交

---
 项目代码/WMS无仓储版/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundBatchPickingService.cs | 1237 ++++++++++++++++++++++++++++++++++++++++++++++++----------
 项目代码/WMS无仓储版/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundPickingService.cs      |    8 
 项目代码/WMS无仓储版/WIDESEA_WMSServer/WIDESEA_Model/Models/Outbound/Dt_OutboundLockInfo.cs   |    2 
 3 files changed, 1,030 insertions(+), 217 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_Model/Models/Outbound/Dt_OutboundLockInfo.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_Model/Models/Outbound/Dt_OutboundLockInfo.cs"
index 50a2684..de23f20 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_Model/Models/Outbound/Dt_OutboundLockInfo.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_Model/Models/Outbound/Dt_OutboundLockInfo.cs"
@@ -141,7 +141,7 @@
         [Navigate(NavigateType.OneToOne, nameof(StockInfo))]//涓�瀵逛竴 SchoolId鏄疭tudentA绫婚噷闈㈢殑
         public Dt_StockInfo StockInfo { get; set; } //涓嶈兘璧嬪�煎彧鑳芥槸null
 
-
+        public int IsUnallocated { get; set; } = 0;
         public string FactoryArea { get; set; }
 
         [SqlSugar.SugarColumn(IsIgnore = true)]
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/OutboundBatchPickingService.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/OutboundBatchPickingService.cs"
index 97e3106..990d58a 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/OutboundBatchPickingService.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/OutboundBatchPickingService.cs"
@@ -395,6 +395,26 @@
                     $"鎵樼洏杩樻湁{unfinishedCount}鏉℃湭瀹屾垚璁板綍锛屽墿浣欐暟閲弡unfinishedQty}锛屼笉鑳藉彇璧扮┖绠�");
             }
 
+            // 妫�鏌ユ墭鐩樹笂鏄惁杩樻湁搴撳瓨璐х墿
+            var stockInfo = await _stockInfoService.Db.Queryable<Dt_StockInfo>()
+                .FirstAsync(x => x.PalletCode == palletCode);
+
+            if (stockInfo != null)
+            {
+                var remainingStock = await _stockInfoDetailService.Db.Queryable<Dt_StockInfoDetail>()
+                    .Where(x => x.StockId == stockInfo.Id &&
+                               x.Status == (int)StockStatusEmun.鍑哄簱閿佸畾 &&
+                               x.StockQuantity > 0)
+                    .ToListAsync();
+
+                if (remainingStock.Any())
+                {
+                    var remainingQty = remainingStock.Sum(x => x.StockQuantity);
+                    return ValidationResult<List<Dt_OutStockLockInfo>>.Error(
+                        $"鎵樼洏涓婅繕鏈夊簱瀛樿揣鐗╋紝鏁伴噺{remainingQty}锛屼笉鑳藉彇璧扮┖绠�");
+                }
+            }
+
             // 鑾峰彇宸插畬鎴愮殑閿佸畾璁板綍
             var completedLocks = lockInfos.Where(x =>
                 x.Status == (int)OutLockStockStatusEnum.鎷i�夊畬鎴�).ToList();
@@ -513,8 +533,8 @@
             }
         };
         }
-      
-    
+
+
 
         #region 鎵嬪姩鎷嗗寘   
 
@@ -597,21 +617,21 @@
 
             _logger.LogInformation($"鎵惧埌搴撳瓨淇℃伅 - 搴撳瓨鏁伴噺: {stockDetail.StockQuantity}, 鍑哄簱鏁伴噺: {stockDetail.OutboundQuantity}");
 
-            // 閲嶈淇锛氶獙璇佹媶鍖呮暟閲忎笉鑳藉ぇ浜庡簱瀛樻暟閲�
+            // 楠岃瘉鎷嗗寘鏁伴噺涓嶈兘澶т簬搴撳瓨鏁伴噺
             if (stockDetail.StockQuantity < splitQuantity)
             {
                 _logger.LogWarning($"鎷嗗寘鏁伴噺澶т簬搴撳瓨鏁伴噺 - 鎷嗗寘鏁伴噺: {splitQuantity}, 搴撳瓨鏁伴噺: {stockDetail.StockQuantity}");
                 return ValidationResult<(Dt_OutStockLockInfo, Dt_StockInfoDetail)>.Error($"鎷嗗寘鏁伴噺涓嶈兘澶т簬搴撳瓨鏁伴噺锛屽綋鍓嶅簱瀛橈細{stockDetail.StockQuantity}");
             }
 
-            // 閲嶈淇锛氶獙璇佹媶鍖呮暟閲忎笉鑳藉ぇ浜庨攣瀹氫俊鎭殑鍒嗛厤鏁伴噺
+            // 楠岃瘉鎷嗗寘鏁伴噺涓嶈兘澶т簬閿佸畾淇℃伅鐨勫垎閰嶆暟閲�
             if (lockInfo.AssignQuantity < splitQuantity)
             {
                 _logger.LogWarning($"鎷嗗寘鏁伴噺澶т簬鍒嗛厤鏁伴噺 - 鎷嗗寘鏁伴噺: {splitQuantity}, 鍒嗛厤鏁伴噺: {lockInfo.AssignQuantity}");
                 return ValidationResult<(Dt_OutStockLockInfo, Dt_StockInfoDetail)>.Error($"鎷嗗寘鏁伴噺涓嶈兘澶т簬鍒嗛厤鏁伴噺锛屽綋鍓嶅垎閰嶆暟閲忥細{lockInfo.AssignQuantity}");
             }
 
-            // 閲嶈淇锛氶獙璇佹媶鍖呮暟閲忎笉鑳藉ぇ浜庨攣瀹氫俊鎭殑鏈嫞閫夋暟閲�
+            // 楠岃瘉鎷嗗寘鏁伴噺涓嶈兘澶т簬閿佸畾淇℃伅鐨勬湭鎷i�夋暟閲�
             decimal remainingToPick = lockInfo.AssignQuantity - lockInfo.PickedQty;
             if (remainingToPick < splitQuantity)
             {
@@ -619,14 +639,14 @@
                 return ValidationResult<(Dt_OutStockLockInfo, Dt_StockInfoDetail)>.Error($"鎷嗗寘鏁伴噺涓嶈兘澶т簬鏈嫞閫夋暟閲忥紝褰撳墠鏈嫞閫夛細{remainingToPick}");
             }
 
-            // 閲嶈淇锛氶獙璇佹媶鍖呭悗鍘熼攣瀹氫俊鎭殑鍒嗛厤鏁伴噺涓嶄細涓鸿礋鏁�
+            // 楠岃瘉鎷嗗寘鍚庡師閿佸畾淇℃伅鐨勫垎閰嶆暟閲忎笉浼氫负璐熸暟
             if (lockInfo.AssignQuantity - splitQuantity < 0)
             {
                 _logger.LogWarning($"鎷嗗寘鍚庡垎閰嶆暟閲忎负璐熸暟 - 褰撳墠鍒嗛厤鏁伴噺: {lockInfo.AssignQuantity}, 鎷嗗寘鏁伴噺: {splitQuantity}");
                 return ValidationResult<(Dt_OutStockLockInfo, Dt_StockInfoDetail)>.Error($"鎷嗗寘鍚庡垎閰嶆暟閲忎笉鑳戒负璐熸暟");
             }
 
-            // 閲嶈淇锛氶獙璇佽鍗曟槑缁嗙殑鍒嗛厤鏁伴噺鏄惁瓒冲
+            // 楠岃瘉璁㈠崟鏄庣粏鐨勫垎閰嶆暟閲忔槸鍚﹁冻澶�
             // 娉ㄦ剰锛氭墜鍔ㄦ媶鍖呬笉浼氭敼鍙樿鍗曟槑缁嗙殑鍒嗛厤鏁伴噺锛屽洜涓烘�诲垎閰嶆暟閲忎笉鍙�
             // 鍙槸浠庝竴涓攣瀹氫俊鎭浆绉诲埌鍙︿竴涓攣瀹氫俊鎭�
             decimal totalLockAssignQuantity = await GetTotalLockAssignQuantity(orderDetail.Id);
@@ -661,14 +681,7 @@
         {
             _logger.LogInformation($"寮�濮嬫墽琛屾墜鍔ㄦ媶鍖呴�昏緫 - 鍘熸潯鐮�: {stockDetail.Barcode}, 鎷嗗寘鏁伴噺: {splitQuantity}");
 
-            // 閲嶈淇锛氳幏鍙栬鍗曟槑缁嗗苟楠岃瘉鍒嗛厤鏁伴噺
-            var orderDetail = await _outboundOrderDetailService.Db.Queryable<Dt_OutboundOrderDetail>()
-                .FirstAsync(x => x.Id == lockInfo.OrderDetailId);
-
-            if (orderDetail == null)
-                throw new InvalidOperationException("鏈壘鍒拌鍗曟槑缁�");
-
-            // 楠岃瘉鎷嗗寘鏁伴噺涓嶄細瀵艰嚧鍒嗛厤鏁伴噺涓鸿礋鏁�
+            // 楠岃瘉鎷嗗寘鏁伴噺
             if (lockInfo.AssignQuantity < splitQuantity)
             {
                 throw new InvalidOperationException($"鎷嗗寘鏁伴噺瓒呰繃閿佸畾淇℃伅鍒嗛厤鏁伴噺锛屾媶鍖呮暟閲�: {splitQuantity}, 鍒嗛厤鏁伴噺: {lockInfo.AssignQuantity}");
@@ -677,15 +690,10 @@
             // 鐢熸垚鏂版潯鐮�
             string newBarcode = await GenerateNewBarcode();
 
-            // 璁板綍鎷嗗寘鍓嶇殑鍒嗛厤鏁伴噺
-            decimal originalAssignQtyBefore = lockInfo.AssignQuantity;
-            decimal originalOrderQtyBefore = lockInfo.OrderQuantity;
-            decimal remainQty = originalAssignQtyBefore - splitQuantity;
-            if (remainQty < 0)
-            {
-                throw new InvalidOperationException($"鎷嗗寘鍚庡墿浣欐暟閲忎负璐熸暟锛屽師鍒嗛厤鏁伴噺: {originalAssignQtyBefore}, 鎷嗗寘鏁伴噺: {splitQuantity}");
-            }
-            //  鍒涘缓鏂板簱瀛樻槑缁�
+            // 璁$畻鍓╀綑鏁伴噺
+            decimal remainQty = lockInfo.AssignQuantity - splitQuantity;
+
+            // 鍒涘缓鏂板簱瀛樻槑缁�
             var newStockDetail = new Dt_StockInfoDetail
             {
                 StockId = stockDetail.StockId,
@@ -704,24 +712,17 @@
                 InboundOrderRowNo = stockDetail.InboundOrderRowNo,
             };
             await _stockInfoDetailService.Db.Insertable(newStockDetail).ExecuteCommandAsync();
-            _logger.LogInformation($"鍒涘缓鏂板簱瀛樻槑缁� - 鏉$爜: {newBarcode}, 搴撳瓨鏁伴噺: {splitQuantity}");
 
-            //  鏇存柊鍘熷簱瀛樻槑缁�
-            decimal originalStockQtyBefore = stockDetail.StockQuantity;
+            // 鏇存柊鍘熷簱瀛樻槑缁�
             stockDetail.StockQuantity -= splitQuantity;
-            if (stockDetail.StockQuantity < 0)
-            {
-                stockDetail.StockQuantity = 0;
-                _logger.LogWarning($"鍘熷簱瀛樻暟閲忓嚭鐜拌礋鏁帮紝閲嶇疆涓�0");
-            }
+            if (stockDetail.StockQuantity < 0) stockDetail.StockQuantity = 0;
             await _stockInfoDetailService.Db.Updateable(stockDetail).ExecuteCommandAsync();
-            _logger.LogInformation($"鏇存柊鍘熷簱瀛樻槑缁� - 搴撳瓨鏁伴噺浠� {originalStockQtyBefore} 鍑忓皯鍒� {stockDetail.StockQuantity}");
 
-            //   鍒涘缓鏂伴攣瀹氫俊鎭�
+            // 鍒涘缓鏂伴攣瀹氫俊鎭�
             var newLockInfo = new Dt_OutStockLockInfo
             {
                 OrderNo = lockInfo.OrderNo,
-                OrderDetailId = lockInfo.OrderDetailId,
+                OrderDetailId = lockInfo.OrderDetailId, // 缁戝畾鍒板悓涓�涓鍗曟槑缁�
                 OutboundBatchNo = lockInfo.OutboundBatchNo,
                 MaterielCode = lockInfo.MaterielCode,
                 MaterielName = lockInfo.MaterielName,
@@ -748,31 +749,64 @@
             };
 
             await _outStockLockInfoService.Db.Insertable(newLockInfo).ExecuteCommandAsync();
-            _logger.LogInformation($"鍒涘缓鏂伴攣瀹氫俊鎭� - 鏉$爜: {newBarcode}, 鍒嗛厤鏁伴噺: {splitQuantity}");
 
-            //  鏇存柊鍘熼攣瀹氫俊鎭�
+            // 鏇存柊鍘熼攣瀹氫俊鎭�
             lockInfo.AssignQuantity = remainQty;
             lockInfo.OrderQuantity = remainQty;
-
-            _logger.LogInformation($"鏇存柊鍘熼攣瀹氫俊鎭� - 鍒嗛厤鏁伴噺浠� {originalAssignQtyBefore} 鍑忓皯鍒� {lockInfo.AssignQuantity}");
-            _logger.LogInformation($"鏇存柊鍘熼攣瀹氫俊鎭� - 璁㈠崟鏁伴噺浠� {originalOrderQtyBefore} 鍑忓皯鍒� {lockInfo.OrderQuantity}");
-
             await _outStockLockInfoService.Db.Updateable(lockInfo).ExecuteCommandAsync();
 
-          
-            _logger.LogInformation($"璁㈠崟鏄庣粏鍒嗛厤鏁伴噺淇濇寔涓嶅彉 - 宸插垎閰嶆暟閲�: {orderDetail.AllocatedQuantity}");
+            // 閲嶈锛氭墜鍔ㄦ媶鍖呬笉鏀瑰彉璁㈠崟鏄庣粏鐨勬�诲垎閰嶆暟閲�
+            // 鍥犱负鍒嗛厤鏁伴噺鍙槸浠庝竴涓攣瀹氫俊鎭浆绉诲埌鍙︿竴涓攣瀹氫俊鎭�
+            _logger.LogInformation($"鎵嬪姩鎷嗗寘 - 璁㈠崟鏄庣粏鎬诲垎閰嶆暟閲忎繚鎸佷笉鍙�");
 
-            //   璁板綍鎷嗗寘鍘嗗彶
+            // 璁板綍鎷嗗寘鍘嗗彶
             await RecordSplitHistory(lockInfo, stockDetail, splitQuantity, newBarcode, false);
 
-            //  鍒涘缓鎷嗗寘缁撴灉鍒楄〃
+            // 鍒涘缓鎷嗗寘缁撴灉鍒楄〃
             var splitResults = CreateSplitResults(lockInfo, splitQuantity, remainQty, newBarcode, stockDetail.Barcode);
 
             _logger.LogInformation($"鎵嬪姩鎷嗗寘閫昏緫鎵ц瀹屾垚");
 
             return splitResults;
         }
- 
+        /// <summary>
+        /// 楠岃瘉鎷嗗寘鍚庢暟鎹竴鑷存��
+        /// </summary>
+        private async Task ValidateDataConsistencyAfterSplit(long orderDetailId, decimal expectedAllocatedQty, decimal expectedLockQty)
+        {
+            // 閲嶆柊鑾峰彇璁㈠崟鏄庣粏鏁版嵁
+            var orderDetail = await _outboundOrderDetailService.Db.Queryable<Dt_OutboundOrderDetail>()
+                .FirstAsync(x => x.Id == orderDetailId);
+
+            if (orderDetail == null)
+                return;
+
+            // 璁$畻鎵�鏈夐攣瀹氫俊鎭殑鎬诲垎閰嶆暟閲�
+            var allLocks = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
+                .Where(x => x.OrderDetailId == orderDetailId)
+                .ToListAsync();
+
+            decimal totalLockAssignQty = allLocks.Sum(x => x.AssignQuantity);
+
+            _logger.LogInformation($"鏁版嵁涓�鑷存�ч獙璇� - 璁㈠崟鏄庣粏鍒嗛厤鏁伴噺: {orderDetail.AllocatedQuantity}, 閿佸畾淇℃伅鎬诲垎閰嶆暟閲�: {totalLockAssignQty}");
+
+            // 濡傛灉鏁版嵁涓嶄竴鑷达紝璁板綍璀﹀憡
+            if (Math.Abs(orderDetail.AllocatedQuantity - totalLockAssignQty) > 0.01m)
+            {
+                _logger.LogWarning($"鏁版嵁涓嶄竴鑷� - 璁㈠崟鏄庣粏鍒嗛厤鏁伴噺: {orderDetail.AllocatedQuantity}, 閿佸畾淇℃伅鎬诲垎閰嶆暟閲�: {totalLockAssignQty}");
+            }
+
+            // 楠岃瘉鍒嗛厤鏁伴噺娌℃湁寮傚父鍙樺寲
+            if (Math.Abs(orderDetail.AllocatedQuantity - expectedAllocatedQty) > 0.01m)
+            {
+                _logger.LogWarning($"鍒嗛厤鏁伴噺寮傚父鍙樺寲 - 鏈熸湜: {expectedAllocatedQty}, 瀹為檯: {orderDetail.AllocatedQuantity}");
+            }
+
+            if (Math.Abs(orderDetail.LockQuantity - expectedLockQty) > 0.01m)
+            {
+                _logger.LogWarning($"閿佸畾鏁伴噺寮傚父鍙樺寲 - 鏈熸湜: {expectedLockQty}, 瀹為檯: {orderDetail.LockQuantity}");
+            }
+        }
         #endregion
 
 
@@ -833,16 +867,37 @@
         {
             _logger.LogInformation($"寮�濮嬫墽琛屽彇娑堟媶鍖呴�昏緫 - 鍘熸潯鐮�: {splitRecord.OriginalBarcode}, 鏂版潯鐮�: {splitRecord.NewBarcode}, 鎷嗗寘鏁伴噺: {splitRecord.SplitQty}");
 
+            // 鑾峰彇璁㈠崟鏄庣粏
+            var orderDetail = await _outboundOrderDetailService.Db.Queryable<Dt_OutboundOrderDetail>()
+                .FirstAsync(x => x.Id == originalLockInfo.OrderDetailId);
+
+            if (orderDetail == null)
+                throw new InvalidOperationException("鏈壘鍒拌鍗曟槑缁�");
+
+            // 璁板綍鍙栨秷鎷嗗寘鍓嶇殑鍏抽敭鏁版嵁
+            decimal originalOrderDetailAllocatedQty = orderDetail.AllocatedQuantity;
+            decimal originalOrderDetailLockQty = orderDetail.LockQuantity;
+
+            _logger.LogInformation($"鍙栨秷鎷嗗寘鍓嶆暟鎹� - 璁㈠崟鏄庣粏鍒嗛厤鏁伴噺: {originalOrderDetailAllocatedQty}, 閿佸畾鏁伴噺: {originalOrderDetailLockQty}");
+
             // 鎭㈠鍘熼攣瀹氫俊鎭�
             decimal originalAssignQtyBefore = originalLockInfo.AssignQuantity;
             decimal originalOrderQtyBefore = originalLockInfo.OrderQuantity;
 
- 
-            originalLockInfo.AssignQuantity += splitRecord.SplitQty;
-            originalLockInfo.OrderQuantity += splitRecord.SplitQty;
+            // 鏍规嵁鎷嗗寘绫诲瀷鍐冲畾濡備綍鎭㈠
+            if (splitRecord.IsAutoSplit)
+            {
+                // 鑷姩鎷嗗寘锛氬師閿佸畾淇℃伅淇濇寔涓嶅彉锛屽彧闇�瑕佸垹闄ゆ柊閿佸畾淇℃伅
+                _logger.LogInformation($"鍙栨秷鑷姩鎷嗗寘 - 鍘熼攣瀹氫俊鎭繚鎸佷笉鍙�");
+            }
+            else
+            {
+                // 鎵嬪姩鎷嗗寘锛氭仮澶嶅師閿佸畾淇℃伅鐨勫垎閰嶆暟閲�
+                originalLockInfo.AssignQuantity += splitRecord.SplitQty;
+                originalLockInfo.OrderQuantity += splitRecord.SplitQty;
 
-            _logger.LogInformation($"鎭㈠鍘熼攣瀹氫俊鎭� - 鍒嗛厤鏁伴噺浠� {originalAssignQtyBefore} 澧炲姞鍒� {originalLockInfo.AssignQuantity}");
-            _logger.LogInformation($"鎭㈠鍘熼攣瀹氫俊鎭� - 璁㈠崟鏁伴噺浠� {originalOrderQtyBefore} 澧炲姞鍒� {originalLockInfo.OrderQuantity}");
+                _logger.LogInformation($"鍙栨秷鎵嬪姩鎷嗗寘 - 鎭㈠鍘熼攣瀹氫俊鎭垎閰嶆暟閲忎粠 {originalAssignQtyBefore} 澧炲姞鍒� {originalLockInfo.AssignQuantity}");
+            }
 
             // 濡傛灉鍘熼攣瀹氫俊鎭殑鐘舵�佹槸鎷i�夊畬鎴愶紝闇�瑕侀噸鏂拌缃负鍑哄簱涓�
             if (originalLockInfo.Status == (int)OutLockStockStatusEnum.鎷i�夊畬鎴�)
@@ -859,34 +914,67 @@
 
             if (originalStock != null)
             {
-                decimal originalStockQtyBefore = originalStock.StockQuantity;
-                originalStock.StockQuantity += splitRecord.SplitQty;
-
-                _logger.LogInformation($"鎭㈠鍘熷簱瀛樻槑缁� - 搴撳瓨鏁伴噺浠� {originalStockQtyBefore} 澧炲姞鍒� {originalStock.StockQuantity}");
-
-                // 濡傛灉鍘熷簱瀛樼姸鎬佹槸鍑哄簱瀹屾垚锛岄渶瑕侀噸鏂拌缃负鍑哄簱閿佸畾
-                if (originalStock.Status == (int)StockStatusEmun.鍑哄簱瀹屾垚)
+                if (splitRecord.IsAutoSplit)
                 {
-                    originalStock.Status = (int)StockStatusEmun.鍑哄簱閿佸畾;
-                    _logger.LogInformation($"鍘熷簱瀛樼姸鎬佷粠鍑哄簱瀹屾垚鎭㈠涓哄嚭搴撻攣瀹�");
+                    // 鑷姩鎷嗗寘锛氬師搴撳瓨鏄庣粏淇濇寔涓嶅彉
+                    _logger.LogInformation($"鍙栨秷鑷姩鎷嗗寘 - 鍘熷簱瀛樻槑缁嗕繚鎸佷笉鍙�");
                 }
+                else
+                {
+                    // 鎵嬪姩鎷嗗寘锛氭仮澶嶅師搴撳瓨鏁伴噺
+                    decimal originalStockQtyBefore = originalStock.StockQuantity;
+                    originalStock.StockQuantity += splitRecord.SplitQty;
 
-                await _stockInfoDetailService.Db.Updateable(originalStock).ExecuteCommandAsync();
+                    _logger.LogInformation($"鍙栨秷鎵嬪姩鎷嗗寘 - 鎭㈠鍘熷簱瀛樻槑缁嗘暟閲忎粠 {originalStockQtyBefore} 澧炲姞鍒� {originalStock.StockQuantity}");
+
+                    // 濡傛灉鍘熷簱瀛樼姸鎬佹槸鍑哄簱瀹屾垚锛岄渶瑕侀噸鏂拌缃负鍑哄簱閿佸畾
+                    if (originalStock.Status == (int)StockStatusEmun.鍑哄簱瀹屾垚)
+                    {
+                        originalStock.Status = (int)StockStatusEmun.鍑哄簱閿佸畾;
+                        _logger.LogInformation($"鍘熷簱瀛樼姸鎬佷粠鍑哄簱瀹屾垚鎭㈠涓哄嚭搴撻攣瀹�");
+                    }
+
+                    await _stockInfoDetailService.Db.Updateable(originalStock).ExecuteCommandAsync();
+                }
             }
 
-            if (newLockInfo != null)
-            {
-                // 鍒犻櫎鏂伴攣瀹氫俊鎭�
-                _logger.LogInformation($"鍒犻櫎鏂伴攣瀹氫俊鎭� - 鏉$爜: {newLockInfo?.CurrentBarcode}, 鍒嗛厤鏁伴噺: {newLockInfo?.AssignQuantity}");
-                await _outStockLockInfoService.Db.Deleteable<Dt_OutStockLockInfo>()
-                    .Where(x => x.Id == newLockInfo.Id)
-                    .ExecuteCommandAsync();
+            // 鍒犻櫎鏂伴攣瀹氫俊鎭�
+            _logger.LogInformation($"鍒犻櫎鏂伴攣瀹氫俊鎭� - 鏉$爜: {newLockInfo.CurrentBarcode}, 鍒嗛厤鏁伴噺: {newLockInfo.AssignQuantity}");
+            await _outStockLockInfoService.Db.Deleteable<Dt_OutStockLockInfo>()
+                .Where(x => x.Id == newLockInfo.Id)
+                .ExecuteCommandAsync();
 
-                //  鍒犻櫎鏂板簱瀛樻槑缁�
-                _logger.LogInformation($"鍒犻櫎鏂板簱瀛樻槑缁� - 鏉$爜: {newStockDetail.Barcode}, 搴撳瓨鏁伴噺: {newStockDetail.StockQuantity}");
-                await _stockInfoDetailService.Db.Deleteable<Dt_StockInfoDetail>()
-                    .Where(x => x.Barcode == newLockInfo.CurrentBarcode)
-                    .ExecuteCommandAsync();
+            // 鍒犻櫎鏂板簱瀛樻槑缁�
+            _logger.LogInformation($"鍒犻櫎鏂板簱瀛樻槑缁� - 鏉$爜: {newStockDetail.Barcode}, 搴撳瓨鏁伴噺: {newStockDetail.StockQuantity}");
+            await _stockInfoDetailService.Db.Deleteable<Dt_StockInfoDetail>()
+                .Where(x => x.Barcode == newLockInfo.CurrentBarcode)
+                .ExecuteCommandAsync();
+
+            // 濡傛灉鏄嚜鍔ㄦ媶鍖咃紝闇�瑕佸噺灏戣鍗曟槑缁嗙殑鍒嗛厤鏁伴噺鍜岄攣瀹氭暟閲�
+            if (splitRecord.IsAutoSplit)
+            {
+                decimal originalAllocatedBefore = orderDetail.AllocatedQuantity;
+                decimal originalLockBefore = orderDetail.LockQuantity;
+
+                orderDetail.AllocatedQuantity -= splitRecord.SplitQty;
+                orderDetail.LockQuantity -= splitRecord.SplitQty;
+
+                // 杈圭晫妫�鏌ワ細纭繚鏁伴噺涓嶄細涓鸿礋鏁�
+                if (orderDetail.AllocatedQuantity < 0)
+                {
+                    _logger.LogWarning($"鍒嗛厤鏁伴噺鍑虹幇璐熸暟锛岄噸缃负0銆傚師鍊�: {orderDetail.AllocatedQuantity + splitRecord.SplitQty}, 鍑忓皯: {splitRecord.SplitQty}");
+                    orderDetail.AllocatedQuantity = 0;
+                }
+                if (orderDetail.LockQuantity < 0)
+                {
+                    _logger.LogWarning($"閿佸畾鏁伴噺鍑虹幇璐熸暟锛岄噸缃负0銆傚師鍊�: {orderDetail.LockQuantity + splitRecord.SplitQty}, 鍑忓皯: {splitRecord.SplitQty}");
+                    orderDetail.LockQuantity = 0;
+                }
+
+                await _outboundOrderDetailService.Db.Updateable(orderDetail).ExecuteCommandAsync();
+
+                _logger.LogInformation($"鍙栨秷鑷姩鎷嗗寘鍑忓皯璁㈠崟鏄庣粏鏁伴噺 - 鍒嗛厤鏁伴噺浠� {originalAllocatedBefore} 鍑忓皯鍒� {orderDetail.AllocatedQuantity}");
+                _logger.LogInformation($"鍙栨秷鑷姩鎷嗗寘鍑忓皯璁㈠崟鏄庣粏鏁伴噺 - 閿佸畾鏁伴噺浠� {originalLockBefore} 鍑忓皯鍒� {orderDetail.LockQuantity}");
             }
 
             // 鏍囪鎷嗗寘璁板綍涓哄凡鎾ら攢
@@ -896,13 +984,96 @@
             await _splitPackageService.Db.Updateable(splitRecord).ExecuteCommandAsync();
             _logger.LogInformation($"鏍囪鎷嗗寘璁板綍涓哄凡鎾ら攢");
 
+            // 楠岃瘉鍙栨秷鎷嗗寘鍚庢暟鎹竴鑷存��
+            await ValidateDataConsistencyAfterCancelSplit(orderDetail.Id, originalOrderDetailAllocatedQty, originalOrderDetailLockQty, splitRecord.IsAutoSplit, splitRecord.SplitQty);
+
             // 妫�鏌ュ苟鏇存柊鎵规鍜岃鍗曠姸鎬�
             await CheckAndUpdateBatchStatus(originalLockInfo.BatchNo);
             await CheckAndUpdateOrderStatus(originalLockInfo.OrderNo);
 
             _logger.LogInformation($"鍙栨秷鎷嗗寘閫昏緫鎵ц瀹屾垚");
         }
+        /// <summary>
+        /// 楠岃瘉鍙栨秷鎷嗗寘鍚庢暟鎹竴鑷存�� - 鏈�鏂扮増鏈�
+        /// </summary>
+        private async Task ValidateDataConsistencyAfterCancelSplit(long orderDetailId, decimal originalAllocatedQty, decimal originalLockQty, bool isAutoSplit, decimal splitQuantity)
+        {
+            // 閲嶆柊鑾峰彇璁㈠崟鏄庣粏鏁版嵁
+            var orderDetail = await _outboundOrderDetailService.Db.Queryable<Dt_OutboundOrderDetail>()
+                .FirstAsync(x => x.Id == orderDetailId);
 
+            if (orderDetail == null)
+                return;
+
+            // 璁$畻鎵�鏈夐攣瀹氫俊鎭殑鎬诲垎閰嶆暟閲�
+            var allLocks = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
+                .Where(x => x.OrderDetailId == orderDetailId)
+                .ToListAsync();
+
+            decimal totalLockAssignQty = allLocks.Sum(x => x.AssignQuantity);
+
+            _logger.LogInformation($"鍙栨秷鎷嗗寘鍚庢暟鎹竴鑷存�ч獙璇� - 璁㈠崟鏄庣粏鍒嗛厤鏁伴噺: {orderDetail.AllocatedQuantity}, 閿佸畾淇℃伅鎬诲垎閰嶆暟閲�: {totalLockAssignQty}");
+
+            // 鏍规嵁鎷嗗寘绫诲瀷璁$畻鏈熸湜鍊�
+            decimal expectedAllocatedQty;
+            decimal expectedLockQty;
+
+            if (isAutoSplit)
+            {
+                // 鑷姩鎷嗗寘鍙栨秷锛氬垎閰嶆暟閲忓簲璇ュ噺灏戞媶鍖呮暟閲�
+                expectedAllocatedQty = originalAllocatedQty - splitQuantity;
+                expectedLockQty = originalLockQty - splitQuantity;
+
+                _logger.LogInformation($"鍙栨秷鑷姩鎷嗗寘鏈熸湜鍊� - 鍒嗛厤鏁伴噺: {expectedAllocatedQty}, 閿佸畾鏁伴噺: {expectedLockQty}");
+            }
+            else
+            {
+                // 鎵嬪姩鎷嗗寘鍙栨秷锛氬垎閰嶆暟閲忓簲璇ヤ繚鎸佷笉鍙�
+                expectedAllocatedQty = originalAllocatedQty;
+                expectedLockQty = originalLockQty;
+
+                _logger.LogInformation($"鍙栨秷鎵嬪姩鎷嗗寘鏈熸湜鍊� - 鍒嗛厤鏁伴噺: {expectedAllocatedQty}, 閿佸畾鏁伴噺: {expectedLockQty}");
+            }
+
+            // 杈圭晫妫�鏌ワ細纭繚鏈熸湜鍊间笉涓鸿礋鏁�
+            if (expectedAllocatedQty < 0)
+            {
+                _logger.LogWarning($"鏈熸湜鍒嗛厤鏁伴噺涓鸿礋鏁帮紝閲嶇疆涓�0銆傝绠楀��: {expectedAllocatedQty}");
+                expectedAllocatedQty = 0;
+            }
+
+            if (expectedLockQty < 0)
+            {
+                _logger.LogWarning($"鏈熸湜閿佸畾鏁伴噺涓鸿礋鏁帮紝閲嶇疆涓�0銆傝绠楀��: {expectedLockQty}");
+                expectedLockQty = 0;
+            }
+
+            // 楠岃瘉鍒嗛厤鏁伴噺
+            if (Math.Abs(orderDetail.AllocatedQuantity - expectedAllocatedQty) > 0.01m)
+            {
+                _logger.LogWarning($"鍙栨秷鎷嗗寘鍚庡垎閰嶆暟閲忓紓甯� - 鏈熸湜: {expectedAllocatedQty}, 瀹為檯: {orderDetail.AllocatedQuantity}");
+            }
+
+            // 楠岃瘉閿佸畾鏁伴噺
+            if (Math.Abs(orderDetail.LockQuantity - expectedLockQty) > 0.01m)
+            {
+                _logger.LogWarning($"鍙栨秷鎷嗗寘鍚庨攣瀹氭暟閲忓紓甯� - 鏈熸湜: {expectedLockQty}, 瀹為檯: {orderDetail.LockQuantity}");
+            }
+
+            // 楠岃瘉璁㈠崟鏄庣粏鍒嗛厤鏁伴噺涓庨攣瀹氫俊鎭�诲垎閰嶆暟閲忕殑涓�鑷存��
+            if (Math.Abs(orderDetail.AllocatedQuantity - totalLockAssignQty) > 0.01m)
+            {
+                _logger.LogWarning($"鍙栨秷鎷嗗寘鍚庢暟鎹笉涓�鑷� - 璁㈠崟鏄庣粏鍒嗛厤鏁伴噺: {orderDetail.AllocatedQuantity}, 閿佸畾淇℃伅鎬诲垎閰嶆暟閲�: {totalLockAssignQty}");
+            }
+
+            // 璁板綍璇︾粏鐨勪竴鑷存�ф姤鍛�
+            _logger.LogInformation($"鍙栨秷鎷嗗寘鏁版嵁涓�鑷存�ф姤鍛� - " +
+                                  $"璁㈠崟鏄庣粏鍒嗛厤鏁伴噺: {orderDetail.AllocatedQuantity}, " +
+                                  $"璁㈠崟鏄庣粏閿佸畾鏁伴噺: {orderDetail.LockQuantity}, " +
+                                  $"閿佸畾淇℃伅鎬诲垎閰嶆暟閲�: {totalLockAssignQty}, " +
+                                  $"鎷嗗寘绫诲瀷: {(isAutoSplit ? "鑷姩" : "鎵嬪姩")}, " +
+                                  $"鎷嗗寘鏁伴噺: {splitQuantity}");
+        }
         /// <summary>
         /// 楠岃瘉鍙栨秷鎷嗗寘璇锋眰 
         /// </summary>
@@ -935,7 +1106,7 @@
 
             _logger.LogInformation($"鎵惧埌鏂伴攣瀹氫俊鎭� - 鐘舵��: {newLockInfo.Status}, 宸叉嫞閫�: {newLockInfo.PickedQty}, 鍒嗛厤鏁伴噺: {newLockInfo.AssignQuantity}");
 
-            // 閲嶈淇锛氭鏌ユ柊鏉$爜鏄惁宸茶鍒嗘嫞
+            // 妫�鏌ユ柊鏉$爜鏄惁宸茶鍒嗘嫞
             var newBarcodePickingRecords = await Db.Queryable<Dt_PickingRecord>()
                 .Where(x => x.Barcode == newBarcode && x.OrderNo == orderNo && !x.IsCancelled)
                 .ToListAsync();
@@ -948,7 +1119,7 @@
                     $"鏂版潯鐮佸凡琚垎鎷o紙宸叉嫞閫夋暟閲忥細{totalPickedQty}锛夛紝璇峰厛鍙栨秷鍒嗘嫞锛岀劧鍚庡啀鍙栨秷鎷嗗寘");
             }
 
-            // 閲嶈淇锛氭鏌ュ師鏉$爜鏄惁宸茶鍒嗘嫞
+            // 妫�鏌ュ師鏉$爜鏄惁宸茶鍒嗘嫞
             var originalBarcodePickingRecords = await Db.Queryable<Dt_PickingRecord>()
                 .Where(x => x.Barcode == splitRecord.OriginalBarcode && x.OrderNo == orderNo && !x.IsCancelled)
                 .ToListAsync();
@@ -1366,7 +1537,7 @@
 
         #endregion
 
-         #region 缁熶竴鍥炲簱閫昏緫
+        #region 缁熶竴鍥炲簱閫昏緫
         private async Task<Dt_Task> GetCurrentTask(string orderNo, string palletCode)
         {
             // 鍏堝皾璇曢�氳繃璁㈠崟鍙峰拰鎵樼洏鍙锋煡鎵句换鍔�
@@ -1522,50 +1693,34 @@
                     return WebResponseContent.Instance.Error("璁㈠崟鍙峰拰鎵樼洏鐮佷笉鑳戒负绌�");
 
                 // 鑾峰彇搴撳瓨淇℃伅
-                var stockInfo = await _stockInfoService.Db.Queryable<Dt_StockInfo>().FirstAsync(x => x.PalletCode == palletCode);
+                var stockInfo = await _stockInfoService.Db.Queryable<Dt_StockInfo>()
+                    .FirstAsync(x => x.PalletCode == palletCode);
                 if (stockInfo == null)
                     return WebResponseContent.Instance.Error($"鏈壘鍒版墭鐩� {palletCode} 瀵瑰簲鐨勫簱瀛樹俊鎭�");
 
-                var task = await GetCurrentTask(orderNo, palletCode);
-                if (task == null)
-                    return WebResponseContent.Instance.Error("鏈壘鍒板搴旂殑浠诲姟淇℃伅");
-
                 // 鍒嗘瀽鎵樼洏鐘舵��
-                var statusAnalysis = await AnalyzePalletStatus(orderNo, palletCode, stockInfo.Id);
+                var statusAnalysis = await AnalyzePalletStatusForReturn(orderNo, palletCode, stockInfo.Id);
+
                 if (!statusAnalysis.HasItemsToReturn)
-                    return await HandleNoReturnItems(orderNo, palletCode, task, stockInfo.Id);
+                    return await HandleEmptyPalletReturn(orderNo, palletCode, stockInfo);
 
-                // 妫�鏌ユ槸鍚︽湁杩涜涓殑浠诲姟
-                if (statusAnalysis.HasActiveTasks)
-                {
-                    return WebResponseContent.Instance.Error($"鎵樼洏 {palletCode} 鏈夎繘琛屼腑鐨勪换鍔★紝涓嶈兘鎵ц鍥炲簱鎿嶄綔");
-                }
+                _logger.LogInformation($"寮�濮嬪洖搴撴搷浣� - 璁㈠崟: {orderNo}, 鎵樼洏: {palletCode}, 鍥炲簱鏁伴噺: {statusAnalysis.TotalReturnQty}");
 
-                // 鎵ц鍥炲簱鎿嶄綔
-                await ExecuteReturnDataOperations(orderNo, palletCode, stockInfo, task, statusAnalysis);
+                // 鎵ц鍥炲簱鏁版嵁鎿嶄綔
+                await ExecuteReturnDataOperations(statusAnalysis);
 
-                // 閲婃斁鎵�鏈夐攣瀹氫互渚块噸鏂板垎閰�
-                await ReleaseAllLocksForReallocation(orderNo, palletCode, statusAnalysis);
+                // 鏇存柊璁㈠崟鐘舵��
+                await UpdateOrderStatusAfterReturn(orderNo);
 
                 _unitOfWorkManage.CommitTran();
 
-                // 鏀堕泦鎵�鏈夐渶瑕佸洖搴撶殑鏉$爜
-                var returnBarcodes = await CollectReturnBarcodes(statusAnalysis);
-
-                // 鍒涘缓ESS鍥炲簱浠诲姟
-                if (returnBarcodes.Any())
-                {
-                    _logger.LogInformation($"鍒涘缓鍥炲簱AGV浠诲姟 - 璁㈠崟: {orderNo}, 鎵樼洏: {palletCode}, 鏉$爜鏁伴噺: {returnBarcodes.Count}, 鍘熷洜: {returnReason}");
-                    await CreateReturnTaskAndHandleESS(orderNo, palletCode, task, TaskTypeEnum.InPick, stockInfo.PalletType);
-                }
-
-                // 鏇存柊璁㈠崟鐘舵�侊紙涓嶈Е鍙慚ES鍥炰紶锛�
-                await UpdateOrderStatusForReturn(orderNo);
+                // 鍒涘缓鍥炲簱浠诲姟锛圓GV锛�
+                await CreateReturnTask(orderNo, palletCode, stockInfo);
 
                 return WebResponseContent.Instance.OK($"鍥炲簱鎿嶄綔鎴愬姛锛屽叡鍥炲簱鏁伴噺锛歿statusAnalysis.TotalReturnQty}", new
                 {
                     ReturnQuantity = statusAnalysis.TotalReturnQty,
-                    ReturnBarcodes = returnBarcodes,
+                    ReturnBarcodes = statusAnalysis.AllBarcodes,
                     Reason = returnReason,
                     PalletCode = palletCode,
                     OrderNo = orderNo
@@ -1578,32 +1733,638 @@
                 return WebResponseContent.Instance.Error($"鍥炲簱鎿嶄綔澶辫触: {ex.Message}");
             }
         }
-        private async Task ExecuteReturnDataOperations(string orderNo, string palletCode, Dt_StockInfo stockInfo,Dt_Task task, PalletStatusAnalysis statusAnalysis)
+        /// <summary>
+        /// 鎵ц鍥炲簱鏁版嵁鎿嶄綔
+        /// 纭繚涓嶄細灏嗙敓鎴愮殑鏉$爜鏁伴噺閿欒缁戝畾鍒伴攣瀹氭暟閲�
+        /// </summary>
+        private async Task ExecuteReturnDataOperations(PalletStatusAnalysis statusAnalysis)
         {
-            _logger.LogInformation($"寮�濮嬫墽琛屽洖搴撴暟鎹搷浣� - 璁㈠崟: {orderNo}, 鎵樼洏: {palletCode}");
+            _logger.LogInformation($"寮�濮嬫墽琛屽洖搴撴暟鎹搷浣� - 璁㈠崟: {statusAnalysis.OrderNo}, 鎵樼洏: {statusAnalysis.PalletCode}");
 
-            // 1. 澶勭悊鏈垎鎷g殑閿佸畾璁板綍
-            if (statusAnalysis.HasRemainingLocks)
+            try
             {
-                _logger.LogInformation($"澶勭悊 {statusAnalysis.RemainingLocks.Count} 鏉℃湭鍒嗘嫞閿佸畾璁板綍");
-                await HandleRemainingLocksReturn(statusAnalysis.RemainingLocks);
+                // 1. 澶勭悊宸插垎閰嶇殑鏈垎鎷i攣瀹氳褰�
+                if (statusAnalysis.HasRemainingLocks)
+                {
+                    _logger.LogInformation($"澶勭悊 {statusAnalysis.RemainingLocks.Count} 鏉″凡鍒嗛厤鏈垎鎷i攣瀹氳褰�");
+                    await HandleAllocatedLocksReturn(statusAnalysis.RemainingLocks);
+                }
+
+                // 2. 澶勭悊鏈垎閰嶇殑閿佸畾璁板綍锛堝鑷姩鎷嗗寘浜х敓鐨勶級
+                if (statusAnalysis.HasUnallocatedLocks)
+                {
+                    _logger.LogInformation($"澶勭悊 {statusAnalysis.UnallocatedLocks.Count} 鏉℃湭鍒嗛厤閿佸畾璁板綍");
+                    await HandleUnallocatedLocksReturn(statusAnalysis.UnallocatedLocks);
+                }
+
+                // 3. 澶勭悊鏈垎閰嶇殑搴撳瓨璐х墿
+                if (statusAnalysis.HasPalletStockGoods)
+                {
+                    _logger.LogInformation($"澶勭悊 {statusAnalysis.PalletStockGoods.Count} 涓湭鍒嗛厤搴撳瓨璐х墿");
+                    await HandleUnallocatedStockReturn(statusAnalysis.PalletStockGoods);
+                }
+
+                _logger.LogInformation($"鍥炲簱鏁版嵁鎿嶄綔瀹屾垚 - 鎬诲洖搴撴暟閲�: {statusAnalysis.TotalReturnQty}");
+            }
+            catch (Exception ex)
+            {
+                _logger.LogError($"鍥炲簱鏁版嵁鎿嶄綔澶辫触 - 璁㈠崟: {statusAnalysis.OrderNo}, 鎵樼洏: {statusAnalysis.PalletCode}, Error: {ex.Message}");
+                throw;
+            }
+        }
+        // <summary>
+        /// 澶勭悊鏈垎閰嶇殑閿佸畾璁板綍鍥炲簱
+        /// 涓嶉渶瑕佸噺灏戣鍗曟槑缁嗙殑鍒嗛厤鏁伴噺
+        /// </summary>
+        private async Task HandleUnallocatedLocksReturn(List<Dt_OutStockLockInfo> unallocatedLocks)
+        {
+            _logger.LogInformation($"寮�濮嬪鐞嗘湭鍒嗛厤閿佸畾璁板綍鍥炲簱 - 鍏� {unallocatedLocks.Count} 鏉¤褰�");
+
+            foreach (var lockInfo in unallocatedLocks)
+            {
+                // 璁$畻鍥炲簱鏁伴噺锛堟湭鎷i�夌殑閮ㄥ垎锛�
+                decimal returnQty = lockInfo.AssignQuantity - lockInfo.PickedQty;
+
+                if (returnQty <= 0)
+                {
+                    _logger.LogInformation($"璺宠繃鏈垎閰嶉攣瀹氳褰� - 閿佸畾ID: {lockInfo.Id}, 宸叉嫞閫夊畬鎴愭垨鏃犻渶鍥炲簱");
+                    continue;
+                }
+
+                _logger.LogInformation($"澶勭悊鏈垎閰嶉攣瀹氳褰曞洖搴� - 閿佸畾ID: {lockInfo.Id}, 鏉$爜: {lockInfo.CurrentBarcode}, 鍥炲簱鏁伴噺: {returnQty}");
+
+                // 鎭㈠搴撳瓨鐘舵��
+                await RestoreStockForLockInfo(lockInfo, returnQty);
+
+                // 鏇存柊閿佸畾璁板綍鐘舵�佷负宸插洖搴�
+                lockInfo.Status = (int)OutLockStockStatusEnum.宸插洖搴�;
+                lockInfo.Operator = App.User.UserName; 
+
+                await _outStockLockInfoService.Db.Updateable(lockInfo).ExecuteCommandAsync();
+
+                _logger.LogInformation($"鏇存柊鏈垎閰嶉攣瀹氱姸鎬� - 閿佸畾ID: {lockInfo.Id}, 鐘舵��: 鍑哄簱涓� -> 宸插洖搴�");
+
+                // 閲嶈锛氭湭鍒嗛厤閿佸畾璁板綍涓嶉渶瑕佸噺灏戣鍗曟槑缁嗙殑鍒嗛厤鏁伴噺
+                _logger.LogInformation($"鏈垎閰嶉攣瀹氳褰曞洖搴撳畬鎴� - 閿佸畾ID: {lockInfo.Id}, 鍥炲簱鏁伴噺: {returnQty}, 鏃犻渶鏇存柊璁㈠崟鏄庣粏");
             }
 
-            // 2. 澶勭悊鎵樼洏涓婄殑搴撳瓨璐х墿
-            if (statusAnalysis.HasPalletStockGoods)
+            _logger.LogInformation($"鏈垎閰嶉攣瀹氳褰曞洖搴撳鐞嗗畬鎴� - 鍏卞鐞� {unallocatedLocks.Count} 鏉¤褰�");
+        }
+        private async Task HandleAllocatedLocksReturn(List<Dt_OutStockLockInfo> allocatedLocks)
+        {
+            _logger.LogInformation($"寮�濮嬪鐞嗗凡鍒嗛厤閿佸畾璁板綍鍥炲簱 - 鍏� {allocatedLocks.Count} 鏉¤褰�");
+
+            // 鎸夎鍗曟槑缁嗗垎缁勫鐞�
+            var orderDetailGroups = allocatedLocks.GroupBy(x => x.OrderDetailId);
+
+            foreach (var group in orderDetailGroups)
             {
-                _logger.LogInformation($"澶勭悊 {statusAnalysis.PalletStockGoods.Count} 涓簱瀛樿揣鐗�");
-                await HandlePalletStockGoodsReturn(statusAnalysis.PalletStockGoods, stockInfo.Id);
+                var orderDetailId = group.Key;
+                var groupLocks = group.ToList();
+
+                _logger.LogInformation($"澶勭悊璁㈠崟鏄庣粏 {orderDetailId} 鐨� {groupLocks.Count} 鏉¢攣瀹氳褰�");
+
+                // 鑾峰彇璁㈠崟鏄庣粏
+                var orderDetail = await _outboundOrderDetailService.Db.Queryable<Dt_OutboundOrderDetail>()
+                    .FirstAsync(x => x.Id == orderDetailId);
+
+                if (orderDetail == null)
+                {
+                    _logger.LogWarning($"鏈壘鍒拌鍗曟槑缁� - OrderDetailId: {orderDetailId}");
+                    continue;
+                }
+
+                decimal totalReturnQtyForDetail = 0;
+
+                foreach (var lockInfo in groupLocks)
+                {
+                    // 璁$畻鍥炲簱鏁伴噺锛堟湭鎷i�夌殑閮ㄥ垎锛�
+                    decimal returnQty = lockInfo.AssignQuantity - lockInfo.PickedQty;
+
+                    if (returnQty <= 0)
+                    {
+                        _logger.LogInformation($"璺宠繃閿佸畾璁板綍 - 閿佸畾ID: {lockInfo.Id}, 宸叉嫞閫夊畬鎴愭垨鏃犻渶鍥炲簱");
+                        continue;
+                    }
+
+                    _logger.LogInformation($"澶勭悊宸插垎閰嶉攣瀹氳褰曞洖搴� - 閿佸畾ID: {lockInfo.Id}, 鏉$爜: {lockInfo.CurrentBarcode}, 鍥炲簱鏁伴噺: {returnQty}");
+
+                    // 鎭㈠搴撳瓨鐘舵��
+                    await RestoreStockForLockInfo(lockInfo, returnQty);
+
+                    // 鏇存柊閿佸畾璁板綍鐘舵�佷负宸插洖搴�
+                    lockInfo.Status = (int)OutLockStockStatusEnum.宸插洖搴�;
+                    lockInfo.Operator = App.User.UserName; 
+
+                    await _outStockLockInfoService.Db.Updateable(lockInfo).ExecuteCommandAsync();
+
+                    _logger.LogInformation($"鏇存柊宸插垎閰嶉攣瀹氱姸鎬� - 閿佸畾ID: {lockInfo.Id}, 鐘舵��: 鍑哄簱涓� -> 宸插洖搴�");
+
+                    totalReturnQtyForDetail += returnQty;
+                }
+
+                // 鍑忓皯璁㈠崟鏄庣粏鐨勫垎閰嶆暟閲�
+                if (totalReturnQtyForDetail > 0)
+                {
+                    await ReduceOrderDetailAllocation(orderDetail, totalReturnQtyForDetail);
+                }
             }
 
-            // 3. 澶勭悊鎷嗗寘璁板綍
-            if (statusAnalysis.HasSplitRecords)
+            _logger.LogInformation($"宸插垎閰嶉攣瀹氳褰曞洖搴撳鐞嗗畬鎴� - 鍏卞鐞� {allocatedLocks.Count} 鏉¤褰�");
+        }
+        /// <summary>
+        /// 鎭㈠閿佸畾璁板綍瀵瑰簲鐨勫簱瀛�
+        /// </summary>
+        private async Task RestoreStockForLockInfo(Dt_OutStockLockInfo lockInfo, decimal returnQty)
+        {
+            var stockDetail = await _stockInfoDetailService.Db.Queryable<Dt_StockInfoDetail>()
+                .FirstAsync(x => x.Barcode == lockInfo.CurrentBarcode && x.StockId == lockInfo.StockId);
+
+            if (stockDetail != null)
             {
-                _logger.LogInformation($"澶勭悊 {statusAnalysis.SplitRecords.Count} 鏉℃媶鍖呰褰�");
-                await HandleSplitRecordsReturn(statusAnalysis.SplitRecords, stockInfo.Id);
+                // 璁板綍鎭㈠鍓嶇殑搴撳瓨鐘舵��
+                decimal originalStockQty = stockDetail.StockQuantity;
+                decimal originalOutboundQty = stockDetail.OutboundQuantity;
+
+                // 鎭㈠搴撳瓨鏁伴噺锛氬嚭搴撴暟閲忓噺灏戯紝搴撳瓨鏁伴噺澧炲姞
+                stockDetail.OutboundQuantity -= returnQty;
+                stockDetail.StockQuantity += returnQty;
+
+                // 纭繚鏁伴噺涓嶄細涓鸿礋鏁�
+                if (stockDetail.OutboundQuantity < 0)
+                {
+                    _logger.LogWarning($"鍑哄簱鏁伴噺鍑虹幇璐熸暟锛岄噸缃负0銆傚師鍊�: {stockDetail.OutboundQuantity + returnQty}");
+                    stockDetail.OutboundQuantity = 0;
+                }
+
+                // 鎭㈠搴撳瓨鐘舵�佷负鍙敤鐘舵��
+                if (stockDetail.Status == (int)StockStatusEmun.鍑哄簱閿佸畾)
+                {
+                    stockDetail.Status = (int)StockStatusEmun.鍏ュ簱瀹屾垚;
+                    _logger.LogInformation($"搴撳瓨鐘舵�佹洿鏂颁负鍏ュ簱瀹屾垚 - 鏉$爜: {stockDetail.Barcode}");
+                }
+
+                await _stockInfoDetailService.Db.Updateable(stockDetail).ExecuteCommandAsync();
+
+                _logger.LogInformation($"鎭㈠搴撳瓨鐘舵�� - 鏉$爜: {stockDetail.Barcode}, " +
+                                     $"搴撳瓨鏁伴噺: {originalStockQty} -> {stockDetail.StockQuantity}, " +
+                                     $"鍑哄簱鏁伴噺: {originalOutboundQty} -> {stockDetail.OutboundQuantity}");
+            }
+            else
+            {
+                _logger.LogWarning($"鏈壘鍒板搴旂殑搴撳瓨淇℃伅 - 鏉$爜: {lockInfo.CurrentBarcode}, StockId: {lockInfo.StockId}");
+            }
+        }
+        /// <summary>
+        /// 鍒涘缓鍥炲簱浠诲姟
+        /// </summary>
+        private async Task CreateReturnTask(string orderNo, string palletCode, Dt_StockInfo stockInfo)
+        {
+            // 鑾峰彇褰撳墠浠诲姟淇℃伅
+            var currentTask = await _taskRepository.Db.Queryable<Dt_Task>()
+                .Where(x => x.OrderNo == orderNo && x.PalletCode == palletCode)
+                .FirstAsync();
+
+            if (currentTask != null)
+            {
+                // 鍒嗛厤鏂拌揣浣�
+                var newLocation = _locationInfoService.AssignLocation(stockInfo.LocationType);
+
+                var returnTask = new Dt_Task()
+                {
+                    CurrentAddress = stations[currentTask.TargetAddress],
+                    Grade = 0,
+                    PalletCode = palletCode,
+                    NextAddress = "",
+                    OrderNo = orderNo,
+                    Roadway = newLocation.RoadwayNo,
+                    SourceAddress = stations[currentTask.TargetAddress],
+                    TargetAddress = newLocation.LocationCode,
+                    TaskStatus = TaskStatusEnum.New.ObjToInt(),
+                    TaskType = TaskTypeEnum.InPick.ObjToInt(),
+                    PalletType = stockInfo.PalletType,
+                    WarehouseId = currentTask.WarehouseId
+                };
+
+                await _taskRepository.Db.Insertable(returnTask).ExecuteCommandAsync();
+
+                // 鍙戦�丒SS鍛戒护
+                await SendESSCommands(palletCode, currentTask.TargetAddress, returnTask);
+
+                _logger.LogInformation($"鍒涘缓鍥炲簱浠诲姟鎴愬姛 - 璁㈠崟: {orderNo}, 鎵樼洏: {palletCode}");
+            }
+        }
+        /// <summary>
+        /// 鏇存柊鍥炲簱鍚庣殑璁㈠崟鐘舵��
+        /// </summary>
+        private async Task UpdateOrderStatusAfterReturn(string orderNo)
+        {
+            // 妫�鏌ヨ鍗曟槸鍚﹁繕鏈夋湭瀹屾垚鐨勯攣瀹氳褰�
+            var activeLocks = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
+                .Where(x => x.OrderNo == orderNo &&
+                           (x.Status == (int)OutLockStockStatusEnum.鍑哄簱涓� ||
+                            x.Status == (int)OutLockStockStatusEnum.鍥炲簱涓�))
+                .ToListAsync();
+
+            if (!activeLocks.Any())
+            {
+                // 鎵�鏈夐攣瀹氳褰曢兘宸插畬鎴愭垨宸插洖搴擄紝鏇存柊璁㈠崟鐘舵��
+                await _outboundOrderService.Db.Updateable<Dt_OutboundOrder>()
+                    .SetColumns(x => new Dt_OutboundOrder
+                    {
+                        OrderStatus = (int)OutOrderStatusEnum.鍑哄簱瀹屾垚,
+                    })
+                    .Where(x => x.OrderNo == orderNo)
+                    .ExecuteCommandAsync();
+
+                _logger.LogInformation($"鏇存柊璁㈠崟鐘舵�佷负鍑哄簱瀹屾垚 - 璁㈠崟: {orderNo}");
+            }
+        }
+
+        /// <summary>
+        /// 澶勭悊绌烘墭鐩樺洖搴�
+        /// </summary>
+        private async Task<WebResponseContent> HandleEmptyPalletReturn(string orderNo, string palletCode, Dt_StockInfo stockInfo)
+        {
+            _logger.LogInformation($"澶勭悊绌烘墭鐩樺洖搴� - 璁㈠崟: {orderNo}, 鎵樼洏: {palletCode}");
+
+            try
+            {
+                // 娓呯悊闆跺簱瀛樻暟鎹�
+                await CleanupZeroStockData(stockInfo.Id);
+
+                // 鍒涘缓绌烘墭鐩樺簱瀛樿褰�
+                var emptyStockInfo = new Dt_StockInfo()
+                {
+                    PalletType = PalletTypeEnum.Empty.ObjToInt(),
+                    StockStatus = StockStatusEmun.缁勭洏鏆傚瓨.ObjToInt(),
+                    PalletCode = palletCode,
+                    LocationType = stockInfo.LocationType
+                };
+                emptyStockInfo.Details = new List<Dt_StockInfoDetail>();
+                _stockInfoService.AddMaterielGroup(emptyStockInfo);
+
+                // 鍒涘缓绌烘墭鐩樺洖搴撲换鍔�
+                await CreateReturnTask(orderNo, palletCode, emptyStockInfo);
+
+                return WebResponseContent.Instance.OK("绌烘墭鐩樺洖搴撴垚鍔�");
+            }
+            catch (Exception ex)
+            {
+                _logger.LogError($"绌烘墭鐩樺洖搴撳け璐�: {ex.Message}");
+                return WebResponseContent.Instance.Error($"绌烘墭鐩樺洖搴撳け璐�: {ex.Message}");
+            }
+        }
+        /// <summary>
+        /// 鍒嗘瀽鎵樼洏鐘舵�佺敤浜庡洖搴�
+        /// 纭繚涓嶄細閿欒璇嗗埆闇�瑕佸洖搴撶殑鐗╁搧
+        /// </summary>
+        private async Task<PalletStatusAnalysis> AnalyzePalletStatusForReturn(string orderNo, string palletCode, int stockId)
+        {
+            var result = new PalletStatusAnalysis
+            {
+                OrderNo = orderNo,
+                PalletCode = palletCode,
+                StockId = stockId
+            };
+
+            // 1. 鍒嗘瀽鏈垎鎷g殑閿佸畾璁板綍锛堢姸鎬佷负鍑哄簱涓級
+            var unfinishedLocks = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
+                .Where(x => x.OrderNo == orderNo &&
+                           x.PalletCode == palletCode &&
+                           x.Status == (int)OutLockStockStatusEnum.鍑哄簱涓�)
+                .ToListAsync();
+
+            if (unfinishedLocks.Any())
+            {
+                // 閲嶈锛氬尯鍒嗗凡鍒嗛厤鍜屾湭鍒嗛厤鐨勯攣瀹氳褰�
+                var allocatedLocks = unfinishedLocks.Where(x => x.IsUnallocated != 1 && x.OrderDetailId > 0).ToList();
+                var unallocatedLocks = unfinishedLocks.Where(x => x.IsUnallocated == 1 || x.OrderDetailId == 0).ToList();
+
+                // 澶勭悊宸插垎閰嶇殑閿佸畾璁板綍
+                if (allocatedLocks.Any())
+                {
+                    result.HasRemainingLocks = true;
+                    result.RemainingLocks = allocatedLocks;
+                    result.RemainingLocksReturnQty = allocatedLocks.Sum(x => x.AssignQuantity - x.PickedQty);
+
+                    foreach (var lockInfo in allocatedLocks)
+                    {
+                        if (!string.IsNullOrEmpty(lockInfo.CurrentBarcode))
+                        {
+                            result.AllBarcodes.Add(lockInfo.CurrentBarcode);
+                        }
+                    }
+
+                    _logger.LogInformation($"鍙戠幇{allocatedLocks.Count}鏉″凡鍒嗛厤鏈垎鎷i攣瀹氳褰曪紝鎬绘暟閲�: {result.RemainingLocksReturnQty}");
+                }
+
+                // 澶勭悊鏈垎閰嶇殑閿佸畾璁板綍锛堝鑷姩鎷嗗寘浜х敓鐨勶級
+                if (unallocatedLocks.Any())
+                {
+                    result.HasUnallocatedLocks = true;
+                    result.UnallocatedLocks = unallocatedLocks;
+                    result.UnallocatedLocksReturnQty = unallocatedLocks.Sum(x => x.AssignQuantity - x.PickedQty);
+
+                    foreach (var lockInfo in unallocatedLocks)
+                    {
+                        if (!string.IsNullOrEmpty(lockInfo.CurrentBarcode))
+                        {
+                            result.AllBarcodes.Add(lockInfo.CurrentBarcode);
+                        }
+                    }
+
+                    _logger.LogInformation($"鍙戠幇{unallocatedLocks.Count}鏉℃湭鍒嗛厤閿佸畾璁板綍锛屾�绘暟閲�: {result.UnallocatedLocksReturnQty}");
+                }
             }
 
-            _logger.LogInformation($"鍥炲簱鏁版嵁鎿嶄綔瀹屾垚 - 鎬诲洖搴撴暟閲�: {statusAnalysis.TotalReturnQty}");
+            // 2. 鍒嗘瀽鎵樼洏涓婄殑鍓╀綑搴撳瓨璐х墿锛堢姸鎬佷负鍑哄簱閿佸畾浣嗘湭鍒嗛厤锛�
+            var palletStockGoods = await _stockInfoDetailService.Db.Queryable<Dt_StockInfoDetail>()
+                .Where(x => x.StockId == stockId &&
+                           x.Status == (int)StockStatusEmun.鍑哄簱閿佸畾 &&
+                           x.StockQuantity > 0)
+                .ToListAsync();
+
+            // 杩囨护鎺夊凡缁忚閿佸畾璁板綍鍗犵敤鐨勫簱瀛�
+            var lockedBarcodes = unfinishedLocks.Select(x => x.CurrentBarcode).ToList();
+            var unlockedStockGoods = palletStockGoods.Where(x => !lockedBarcodes.Contains(x.Barcode)).ToList();
+
+            // 杩涗竴姝ヨ繃婊わ細妫�鏌ヨ繖浜涘簱瀛樻槸鍚︽湁鍏宠仈鐨勯攣瀹氳褰�
+            var trulyUnallocatedGoods = new List<Dt_StockInfoDetail>();
+            foreach (var stock in unlockedStockGoods)
+            {
+                var hasLock = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
+                    .Where(x => x.CurrentBarcode == stock.Barcode &&
+                               x.Status == (int)OutLockStockStatusEnum.鍑哄簱涓�)
+                    .AnyAsync();
+
+                if (!hasLock)
+                {
+                    trulyUnallocatedGoods.Add(stock);
+                }
+            }
+
+            if (trulyUnallocatedGoods.Any())
+            {
+                result.HasPalletStockGoods = true;
+                result.PalletStockGoods = trulyUnallocatedGoods;
+                result.PalletStockReturnQty = trulyUnallocatedGoods.Sum(x => x.StockQuantity);
+
+                foreach (var stock in trulyUnallocatedGoods)
+                {
+                    result.AllBarcodes.Add(stock.Barcode);
+                }
+
+                _logger.LogInformation($"鍙戠幇{trulyUnallocatedGoods.Count}涓湡姝f湭鍒嗛厤搴撳瓨璐х墿锛屾�绘暟閲�: {result.PalletStockReturnQty}");
+            }
+
+            // 3. 璁$畻鎬诲洖搴撴暟閲�
+            result.TotalReturnQty = result.RemainingLocksReturnQty + result.UnallocatedLocksReturnQty + result.PalletStockReturnQty;
+            result.HasItemsToReturn = result.TotalReturnQty > 0;
+            result.IsEmptyPallet = !result.HasItemsToReturn;
+
+            _logger.LogInformation($"鎵樼洏鐘舵�佸垎鏋愬畬鎴� - 璁㈠崟: {orderNo}, 鎵樼洏: {palletCode}, 鎬诲洖搴撴暟閲�: {result.TotalReturnQty}");
+
+            return result;
+        }
+        /// <summary>
+        /// 澶勭悊鏈垎鎷g殑閿佸畾璁板綍鍥炲簱
+        /// 纭繚涓嶄細閿欒缁戝畾鏉$爜鏁伴噺鍒伴攣瀹氭暟閲�
+        /// </summary>
+        private async Task HandleRemainingLocksReturn(List<Dt_OutStockLockInfo> remainingLocks)
+        {
+            _logger.LogInformation($"寮�濮嬪鐞嗘湭鍒嗘嫞閿佸畾璁板綍鍥炲簱 - 鍏� {remainingLocks.Count} 鏉¤褰�");
+
+            // 鎸夎鍗曟槑缁嗗垎缁勫鐞嗭紝纭繚璁㈠崟鏄庣粏鏁版嵁鐨勪竴鑷存��
+            var orderDetailGroups = remainingLocks.GroupBy(x => x.OrderDetailId);
+
+            foreach (var group in orderDetailGroups)
+            {
+                var orderDetailId = group.Key;
+                var groupLocks = group.ToList();
+
+                _logger.LogInformation($"澶勭悊璁㈠崟鏄庣粏 {orderDetailId} 鐨� {groupLocks.Count} 鏉¢攣瀹氳褰�");
+
+                // 鑾峰彇璁㈠崟鏄庣粏
+                var orderDetail = await _outboundOrderDetailService.Db.Queryable<Dt_OutboundOrderDetail>()
+                    .FirstAsync(x => x.Id == orderDetailId);
+
+                if (orderDetail == null)
+                {
+                    _logger.LogWarning($"鏈壘鍒拌鍗曟槑缁� - OrderDetailId: {orderDetailId}");
+                    continue;
+                }
+
+                decimal totalReturnQtyForDetail = 0;
+
+                foreach (var lockInfo in groupLocks)
+                {
+                    // 鍙鐞嗙姸鎬佷负鍑哄簱涓殑閿佸畾璁板綍
+                    if (lockInfo.Status != (int)OutLockStockStatusEnum.鍑哄簱涓�)
+                    {
+                        _logger.LogInformation($"璺宠繃闈炲嚭搴撲腑鐘舵�佺殑閿佸畾璁板綍 - 閿佸畾ID: {lockInfo.Id}, 鐘舵��: {lockInfo.Status}");
+                        continue;
+                    }
+
+                    // 璁$畻鍥炲簱鏁伴噺锛堟湭鎷i�夌殑閮ㄥ垎锛�
+                    decimal returnQty = lockInfo.AssignQuantity - lockInfo.PickedQty;
+
+                    if (returnQty <= 0)
+                    {
+                        _logger.LogInformation($"璺宠繃閿佸畾璁板綍 - 閿佸畾ID: {lockInfo.Id}, 宸叉嫞閫夊畬鎴愭垨鏃犻渶鍥炲簱");
+                        continue;
+                    }
+
+                    _logger.LogInformation($"澶勭悊閿佸畾璁板綍鍥炲簱 - 閿佸畾ID: {lockInfo.Id}, 鏉$爜: {lockInfo.CurrentBarcode}, 鍥炲簱鏁伴噺: {returnQty}");
+
+                    // 鎭㈠搴撳瓨鐘舵��
+                    var stockDetail = await _stockInfoDetailService.Db.Queryable<Dt_StockInfoDetail>()
+                        .FirstAsync(x => x.Barcode == lockInfo.CurrentBarcode && x.StockId == lockInfo.StockId);
+
+                    if (stockDetail != null)
+                    {
+                        // 璁板綍鎭㈠鍓嶇殑搴撳瓨鐘舵��
+                        decimal originalStockQty = stockDetail.StockQuantity;
+                        decimal originalOutboundQty = stockDetail.OutboundQuantity;
+
+                        // 鍙仮澶嶅疄闄呯殑搴撳瓨鏁伴噺锛屼笉鍒涘缓鏂扮殑鏉$爜鎴栫粦瀹�
+                        // 鎭㈠搴撳瓨鏁伴噺锛氬嚭搴撴暟閲忓噺灏戯紝搴撳瓨鏁伴噺澧炲姞
+                        stockDetail.OutboundQuantity -= returnQty;
+                        stockDetail.StockQuantity += returnQty;
+
+                        // 纭繚鏁伴噺涓嶄細涓鸿礋鏁�
+                        if (stockDetail.OutboundQuantity < 0)
+                        {
+                            _logger.LogWarning($"鍑哄簱鏁伴噺鍑虹幇璐熸暟锛岄噸缃负0銆傚師鍊�: {stockDetail.OutboundQuantity + returnQty}");
+                            stockDetail.OutboundQuantity = 0;
+                        }
+
+                        // 鎭㈠搴撳瓨鐘舵�佷负鍙敤鐘舵��
+                        if (stockDetail.Status == (int)StockStatusEmun.鍑哄簱閿佸畾)
+                        {
+                            stockDetail.Status = (int)StockStatusEmun.鍏ュ簱瀹屾垚;
+                            _logger.LogInformation($"搴撳瓨鐘舵�佹洿鏂颁负鍏ュ簱瀹屾垚 - 鏉$爜: {stockDetail.Barcode}");
+                        }
+
+                        await _stockInfoDetailService.Db.Updateable(stockDetail).ExecuteCommandAsync();
+
+                        _logger.LogInformation($"鎭㈠搴撳瓨鐘舵�� - 鏉$爜: {stockDetail.Barcode}, " +
+                                             $"搴撳瓨鏁伴噺: {originalStockQty} -> {stockDetail.StockQuantity}, " +
+                                             $"鍑哄簱鏁伴噺: {originalOutboundQty} -> {stockDetail.OutboundQuantity}");
+                    }
+                    else
+                    {
+                        _logger.LogWarning($"鏈壘鍒板搴旂殑搴撳瓨淇℃伅 - 鏉$爜: {lockInfo.CurrentBarcode}, StockId: {lockInfo.StockId}");
+                        // 閲嶈锛氬鏋滄壘涓嶅埌搴撳瓨淇℃伅锛岃烦杩囨閿佸畾璁板綍锛岄伩鍏嶆暟鎹笉涓�鑷�
+                        continue;
+                    }
+
+                    // 鏇存柊閿佸畾璁板綍鐘舵�佷负宸插洖搴擄紝浣嗕笉淇敼鍒嗛厤鏁伴噺
+                    // 鍒嗛厤鏁伴噺鍦ㄨ鍗曟槑缁嗗眰闈㈢粺涓�澶勭悊
+                    var originalStatus = lockInfo.Status;
+                    lockInfo.Status = (int)OutLockStockStatusEnum.宸插洖搴�;
+                    lockInfo.Operator = App.User.UserName; 
+
+                    await _outStockLockInfoService.Db.Updateable(lockInfo).ExecuteCommandAsync();
+
+                    _logger.LogInformation($"鏇存柊閿佸畾鐘舵�� - 閿佸畾ID: {lockInfo.Id}, 鐘舵��: {originalStatus} -> {lockInfo.Status}");
+
+                    totalReturnQtyForDetail += returnQty;
+
+                    _logger.LogInformation($"閿佸畾璁板綍鍥炲簱瀹屾垚 - 閿佸畾ID: {lockInfo.Id}, 鍥炲簱鏁伴噺: {returnQty}");
+                }
+
+                // 鍑忓皯璁㈠崟鏄庣粏鐨勫垎閰嶆暟閲�
+                if (totalReturnQtyForDetail > 0)
+                {
+                    await ReduceOrderDetailAllocation(orderDetail, totalReturnQtyForDetail);
+                }
+            }
+
+            _logger.LogInformation($"鏈垎鎷i攣瀹氳褰曞洖搴撳鐞嗗畬鎴� - 鍏卞鐞� {remainingLocks.Count} 鏉¤褰�");
+        }
+        /// <summary>
+        /// 澶勭悊鏈垎閰嶇殑搴撳瓨璐х墿鍥炲簱
+        /// 纭繚涓嶄細鍒涘缓鏂扮殑閿佸畾璁板綍
+        /// </summary>
+        private async Task HandleUnallocatedStockReturn(List<Dt_StockInfoDetail> stockGoods)
+        {
+            _logger.LogInformation($"寮�濮嬪鐞嗘湭鍒嗛厤搴撳瓨鍥炲簱 - 鍏� {stockGoods.Count} 涓揣鐗�");
+
+            foreach (var stockDetail in stockGoods)
+            {
+                if (stockDetail.StockQuantity <= 0)
+                {
+                    _logger.LogInformation($"璺宠繃闆跺簱瀛樿揣鐗� - 鏉$爜: {stockDetail.Barcode}");
+                    continue;
+                }
+
+                _logger.LogInformation($"澶勭悊鏈垎閰嶅簱瀛樺洖搴� - 鏉$爜: {stockDetail.Barcode}, 鏁伴噺: {stockDetail.StockQuantity}");
+
+                // 妫�鏌ユ槸鍚﹀凡缁忔湁瀵瑰簲鐨勯攣瀹氳褰�
+                var existingLock = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
+                    .Where(x => x.CurrentBarcode == stockDetail.Barcode &&
+                               x.Status == (int)OutLockStockStatusEnum.鍑哄簱涓�)
+                    .FirstAsync();
+
+                if (existingLock != null)
+                {
+                    _logger.LogWarning($"搴撳瓨鏉$爜 {stockDetail.Barcode} 宸叉湁閿佸畾璁板綍锛岃烦杩囩洿鎺ュ洖搴撳鐞�");
+                    continue;
+                }
+
+                // 璁板綍鎭㈠鍓嶇殑鐘舵��
+                var originalStatus = stockDetail.Status;
+
+                // 鐩存帴鎭㈠搴撳瓨鐘舵�佷负鍙敤鐘舵�侊紝涓嶅垱寤轰换浣曢攣瀹氳褰�
+                stockDetail.Status = (int)StockStatusEmun.鍏ュ簱瀹屾垚;
+
+                await _stockInfoDetailService.Db.Updateable(stockDetail).ExecuteCommandAsync();
+
+                _logger.LogInformation($"鏈垎閰嶅簱瀛樺洖搴撳畬鎴� - 鏉$爜: {stockDetail.Barcode}, 鐘舵��: {originalStatus} -> {stockDetail.Status}");
+            }
+
+            _logger.LogInformation($"鏈垎閰嶅簱瀛樺洖搴撳鐞嗗畬鎴� - 鍏卞鐞� {stockGoods.Count} 涓揣鐗�");
+        }
+        /// <summary>
+        /// 鍑忓皯璁㈠崟鏄庣粏鐨勫垎閰嶆暟閲�
+        /// 纭繚鍒嗛厤鏁伴噺鐨勫噺灏戞槸鍑嗙‘鐨�
+        /// </summary>
+        private async Task ReduceOrderDetailAllocation(Dt_OutboundOrderDetail orderDetail, decimal reduceQty)
+        {
+            if (orderDetail == null)
+                return;
+
+            decimal originalAllocated = orderDetail.AllocatedQuantity;
+            decimal originalLock = orderDetail.LockQuantity;
+
+            // 楠岃瘉鍑忓皯鏁伴噺涓嶄細瀵艰嚧璐熸暟
+            if (orderDetail.AllocatedQuantity < reduceQty)
+            {
+                _logger.LogWarning($"鍒嗛厤鏁伴噺涓嶈冻锛岃皟鏁村噺灏戞暟閲� - 鍘熻鍒掑噺灏�: {reduceQty}, 瀹為檯鍙敤: {orderDetail.AllocatedQuantity}");
+                reduceQty = orderDetail.AllocatedQuantity;
+            }
+
+            // 鍑忓皯鍒嗛厤鏁伴噺鍜岄攣瀹氭暟閲�
+            orderDetail.AllocatedQuantity -= reduceQty;
+            orderDetail.LockQuantity -= reduceQty;
+
+            // 纭繚鏁伴噺涓嶄細涓鸿礋鏁�
+            if (orderDetail.AllocatedQuantity < 0)
+            {
+                _logger.LogWarning($"鍒嗛厤鏁伴噺鍑虹幇璐熸暟锛岄噸缃负0銆傚師鍊�: {orderDetail.AllocatedQuantity + reduceQty}, 鍑忓皯: {reduceQty}");
+                orderDetail.AllocatedQuantity = 0;
+            }
+
+            if (orderDetail.LockQuantity < 0)
+            {
+                _logger.LogWarning($"閿佸畾鏁伴噺鍑虹幇璐熸暟锛岄噸缃负0銆傚師鍊�: {orderDetail.LockQuantity + reduceQty}, 鍑忓皯: {reduceQty}");
+                orderDetail.LockQuantity = 0;
+            }
+
+            // 鏇存柊鎵规鍒嗛厤鐘舵��
+            await UpdateBatchAllocateStatus(orderDetail);
+
+            await _outboundOrderDetailService.Db.Updateable(orderDetail).ExecuteCommandAsync();
+
+            _logger.LogInformation($"鍑忓皯璁㈠崟鏄庣粏鍒嗛厤 - OrderDetailId: {orderDetail.Id}, " +
+                                 $"鍒嗛厤鏁伴噺: {originalAllocated} -> {orderDetail.AllocatedQuantity}, " +
+                                 $"閿佸畾鏁伴噺: {originalLock} -> {orderDetail.LockQuantity}, " +
+                                 $"鍑忓皯鏁伴噺: {reduceQty}");
+
+            // 楠岃瘉鏁版嵁涓�鑷存��
+            await ValidateOrderDetailConsistency(orderDetail.Id);
+        }
+        /// <summary>
+        /// 楠岃瘉璁㈠崟鏄庣粏鏁版嵁涓�鑷存��
+        /// </summary>
+        private async Task ValidateOrderDetailConsistency(long orderDetailId)
+        {
+            var orderDetail = await _outboundOrderDetailService.Db.Queryable<Dt_OutboundOrderDetail>()
+                .FirstAsync(x => x.Id == orderDetailId);
+
+            if (orderDetail == null)
+                return;
+
+            // 璁$畻鎵�鏈夌浉鍏抽攣瀹氳褰曠殑鎬诲垎閰嶆暟閲�
+            var relatedLocks = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
+                .Where(x => x.OrderDetailId == orderDetailId &&
+                           x.Status != (int)OutLockStockStatusEnum.宸插洖搴�)
+                .ToListAsync();
+
+            decimal totalLockAssignQty = relatedLocks.Sum(x => x.AssignQuantity);
+
+            // 楠岃瘉璁㈠崟鏄庣粏鍒嗛厤鏁伴噺涓庨攣瀹氳褰曟�诲垎閰嶆暟閲忕殑涓�鑷存��
+            if (Math.Abs(orderDetail.AllocatedQuantity - totalLockAssignQty) > 0.01m)
+            {
+                _logger.LogWarning($"鏁版嵁涓嶄竴鑷磋鍛� - OrderDetailId: {orderDetailId}, " +
+                                  $"璁㈠崟鏄庣粏鍒嗛厤鏁伴噺: {orderDetail.AllocatedQuantity}, " +
+                                  $"閿佸畾璁板綍鎬诲垎閰嶆暟閲�: {totalLockAssignQty}");
+            }
+            else
+            {
+                _logger.LogInformation($"鏁版嵁涓�鑷存�ч獙璇侀�氳繃 - OrderDetailId: {orderDetailId}");
+            }
         }
         /// <summary>
         /// 鍒嗘壒鍥炲簱 - 璋冪敤缁熶竴鍥炲簱鏂规硶
@@ -1784,51 +2545,12 @@
             }
 
             _logger.LogInformation($"鍥炲簱鎿嶄綔瀹屾垚 - 鎬诲洖搴撴暟閲�: {statusAnalysis.TotalReturnQty}");
-        }  
+        }
 
         /// <summary>
         /// 澶勭悊鏈垎鎷g殑閿佸畾璁板綍鍥炲簱
         /// </summary>
-        private async Task HandleRemainingLocksReturn(List<Dt_OutStockLockInfo> remainingLocks)
-        {
-            foreach (var lockInfo in remainingLocks)
-            {
-                // 璁$畻鍥炲簱鏁伴噺锛堟湭鎷i�夌殑閮ㄥ垎锛�
-                decimal returnQty = lockInfo.AssignQuantity - lockInfo.PickedQty;
-
-                if (returnQty > 0)
-                {
-                    // 鎭㈠搴撳瓨鐘舵��
-                    var stockDetail = await _stockInfoDetailService.Db.Queryable<Dt_StockInfoDetail>()
-                        .FirstAsync(x => x.Barcode == lockInfo.CurrentBarcode && x.StockId == lockInfo.StockId);
-
-                    if (stockDetail != null)
-                    {
-                        stockDetail.StockQuantity += returnQty;
-                        stockDetail.OutboundQuantity -= returnQty;
-
-                        // 纭繚鍑哄簱鏁伴噺涓嶄細涓鸿礋鏁�
-                        if (stockDetail.OutboundQuantity < 0)
-                        {
-                            stockDetail.OutboundQuantity = 0;
-                        }
-
-                        // 鍥炲簱鍚庡簱瀛樼姸鎬佹仮澶嶄负鍏ュ簱瀹屾垚锛堝彲鐢ㄧ姸鎬侊級
-                        stockDetail.Status = (int)StockStatusEmun.鍏ュ簱瀹屾垚;
-                        await _stockInfoDetailService.Db.Updateable(stockDetail).ExecuteCommandAsync();
-
-                        _logger.LogInformation($"鎭㈠搴撳瓨 - 鏉$爜: {stockDetail.Barcode}, 鏁伴噺: {returnQty}, 鏂板簱瀛�: {stockDetail.StockQuantity}");
-                    }
-
-                    // 鏇存柊閿佸畾璁板綍鐘舵�佷负宸插洖搴�
-                    lockInfo.Status = (int)OutLockStockStatusEnum.宸插洖搴�;
-                    lockInfo.Operator = App.User.UserName;
-                    await _outStockLockInfoService.Db.Updateable(lockInfo).ExecuteCommandAsync();
-
-                    _logger.LogInformation($"鏇存柊閿佸畾鐘舵�� - 閿佸畾ID: {lockInfo.Id}, 鍥炲簱鏁伴噺: {returnQty}");
-                }
-            }
-        }
+  
 
         /// <summary>
         /// 澶勭悊鎵樼洏涓婄殑搴撳瓨璐х墿鍥炲簱
@@ -1884,42 +2606,61 @@
             }
         }
 
-        /// <summary>
-        /// 澶勭悊鍗曚釜閿佸畾璁板綍鍥炲簱
-        /// </summary>
         private async Task HandleSingleLockReturn(Dt_OutStockLockInfo lockInfo)
         {
             decimal returnQty = lockInfo.AssignQuantity - lockInfo.PickedQty;
 
-            if (returnQty > 0)
+            if (returnQty <= 0)
             {
-                // 鎭㈠搴撳瓨
-                var stockDetail = await _stockInfoDetailService.Db.Queryable<Dt_StockInfoDetail>()
-                    .FirstAsync(x => x.Barcode == lockInfo.CurrentBarcode && x.StockId == lockInfo.StockId);
+                _logger.LogInformation($"璺宠繃閿佸畾璁板綍 - 閿佸畾ID: {lockInfo.Id}, 宸叉嫞閫夊畬鎴愭垨鏃犻渶鍥炲簱");
+                return;
+            }
 
-                if (stockDetail != null)
+            _logger.LogInformation($"澶勭悊鎷嗗寘鐩稿叧閿佸畾璁板綍 - 閿佸畾ID: {lockInfo.Id}, 鏉$爜: {lockInfo.CurrentBarcode}, 鍥炲簱鏁伴噺: {returnQty}");
+
+            // 鎭㈠搴撳瓨
+            var stockDetail = await _stockInfoDetailService.Db.Queryable<Dt_StockInfoDetail>()
+                .FirstAsync(x => x.Barcode == lockInfo.CurrentBarcode && x.StockId == lockInfo.StockId);
+
+            if (stockDetail != null)
+            {
+                decimal originalStockQty = stockDetail.StockQuantity;
+                decimal originalOutboundQty = stockDetail.OutboundQuantity;
+
+                stockDetail.StockQuantity += returnQty;
+                stockDetail.OutboundQuantity -= returnQty;
+
+                if (stockDetail.OutboundQuantity < 0)
                 {
-                    stockDetail.StockQuantity += returnQty;
-                    stockDetail.OutboundQuantity -= returnQty;
-
-                    if (stockDetail.OutboundQuantity < 0)
-                    {
-                        stockDetail.OutboundQuantity = 0;
-                    }
-
-                    stockDetail.Status = (int)StockStatusEmun.鍏ュ簱瀹屾垚;
-                    await _stockInfoDetailService.Db.Updateable(stockDetail).ExecuteCommandAsync();
+                    stockDetail.OutboundQuantity = 0;
                 }
 
-                // 鏇存柊閿佸畾鐘舵��
-                lockInfo.Status = (int)OutLockStockStatusEnum.宸插洖搴�;
-                lockInfo.Operator = App.User.UserName;
-                await _outStockLockInfoService.Db.Updateable(lockInfo).ExecuteCommandAsync();
+                stockDetail.Status = (int)StockStatusEmun.鍏ュ簱瀹屾垚;
+                await _stockInfoDetailService.Db.Updateable(stockDetail).ExecuteCommandAsync();
 
-                _logger.LogInformation($"澶勭悊鎷嗗寘鐩稿叧閿佸畾 - 鏉$爜: {lockInfo.CurrentBarcode}, 鍥炲簱鏁伴噺: {returnQty}");
+                _logger.LogInformation($"鎭㈠鎷嗗寘鐩稿叧搴撳瓨 - 鏉$爜: {stockDetail.Barcode}, " +
+                                     $"搴撳瓨鏁伴噺: {originalStockQty} -> {stockDetail.StockQuantity}");
             }
-        }
 
+            // 鏇存柊閿佸畾鐘舵��
+            lockInfo.Status = (int)OutLockStockStatusEnum.宸插洖搴�;
+            lockInfo.Operator = App.User.UserName;
+            await _outStockLockInfoService.Db.Updateable(lockInfo).ExecuteCommandAsync();
+
+            // 鍑忓皯璁㈠崟鏄庣粏鐨勫垎閰嶆暟閲�
+            if (lockInfo.OrderDetailId > 0)
+            {
+                var orderDetail = await _outboundOrderDetailService.Db.Queryable<Dt_OutboundOrderDetail>()
+                    .FirstAsync(x => x.Id == lockInfo.OrderDetailId);
+
+                if (orderDetail != null)
+                {
+                    await ReduceOrderDetailAllocation(orderDetail, returnQty);
+                }
+            }
+
+            _logger.LogInformation($"鎷嗗寘鐩稿叧閿佸畾璁板綍鍥炲簱瀹屾垚 - 閿佸畾ID: {lockInfo.Id}, 鍥炲簱鏁伴噺: {returnQty}");
+        }
         /// <summary>
         /// 閲婃斁鎵�鏈夐攣瀹氫互渚块噸鏂板垎閰�
         /// </summary>
@@ -2017,9 +2758,8 @@
             {
                 // 1. 鍒犻櫎搴撳瓨鏁伴噺涓�0鐨勬槑缁嗚褰�
                 var deleteDetailCount = await _stockInfoDetailService.Db.Deleteable<Dt_StockInfoDetail>()
-                    .Where(x => x.StockId == stockId && x.StockQuantity == 0 && (x.Status == StockStatusEmun.鍑哄簱瀹屾垚.ObjToInt() || x.Status ==
-                                          StockStatusEmun.鍏ュ簱瀹屾垚.ObjToInt()))
-                    .ExecuteCommandAsync();
+            .Where(x => x.StockId == stockId && x.StockQuantity == 0)
+            .ExecuteCommandAsync();
 
                 await _stockInfoService.Db.Deleteable<Dt_StockInfo>()
                    .Where(x => x.Id == stockId).ExecuteCommandAsync();
@@ -2086,6 +2826,11 @@
             if (lockInfo == null)
                 return ValidationResult<(Dt_OutStockLockInfo, Dt_OutboundOrderDetail, Dt_StockInfoDetail, Dt_OutboundBatch)>.Error("鏈壘鍒版湁鏁堢殑閿佸畾淇℃伅");
 
+            if (lockInfo.IsUnallocated == 1 || lockInfo.OrderDetailId == 0)
+            {
+                return ValidationResult<(Dt_OutStockLockInfo, Dt_OutboundOrderDetail, Dt_StockInfoDetail, Dt_OutboundBatch)>.Error("璇ユ潯鐮佹槸鏈垎閰嶉攣瀹氳褰曪紝涓嶈兘鐩存帴鍒嗘嫞");
+            }
+
             _logger.LogInformation($"鎵惧埌閿佸畾淇℃伅 - 鍒嗛厤鏁伴噺: {lockInfo.AssignQuantity}, 宸叉嫞閫�: {lockInfo.PickedQty}");
 
             // 妫�鏌ユ槸鍚﹀凡缁忓垎鎷e畬鎴�
@@ -2101,21 +2846,32 @@
 
             _logger.LogInformation($"鎵惧埌璁㈠崟鏄庣粏 - 宸插垎閰嶆暟閲�: {orderDetail.AllocatedQuantity}, 閿佸畾鏁伴噺: {orderDetail.LockQuantity}");
 
-            // 閲嶈淇锛氭鏌ヨ鍗曟槑缁嗙殑宸插垎閰嶆暟閲忔槸鍚﹁冻澶�
+            // 妫�鏌ヨ鍗曟槑缁嗙殑宸插垎閰嶆暟閲忔槸鍚﹁冻澶�
             decimal remainingToPick = lockInfo.AssignQuantity - lockInfo.PickedQty;
-            if (orderDetail.AllocatedQuantity < remainingToPick)
+
+            // 杩欓噷搴旇妫�鏌ラ攣瀹氫俊鎭殑鍒嗛厤鏁伴噺锛岃�屼笉鏄鍗曟槑缁嗙殑鍒嗛厤鏁伴噺
+            // 鍥犱负鎷嗗寘鍚庯紝閿佸畾淇℃伅鐨勫垎閰嶆暟閲忓彲鑳藉皬浜庤鍗曟槑缁嗙殑鍒嗛厤鏁伴噺
+            if (lockInfo.AssignQuantity < remainingToPick)
             {
-                _logger.LogWarning($"璁㈠崟鏄庣粏宸插垎閰嶆暟閲忎笉瓒� - 闇�瑕佹嫞閫�: {remainingToPick}, 鍙敤鍒嗛厤鏁伴噺: {orderDetail.AllocatedQuantity}");
+                _logger.LogWarning($"閿佸畾淇℃伅鍒嗛厤鏁伴噺涓嶈冻 - 闇�瑕佹嫞閫�: {remainingToPick}, 閿佸畾淇℃伅鍒嗛厤鏁伴噺: {lockInfo.AssignQuantity}");
                 return ValidationResult<(Dt_OutStockLockInfo, Dt_OutboundOrderDetail, Dt_StockInfoDetail, Dt_OutboundBatch)>.Error(
-                    $"璁㈠崟鏄庣粏鍒嗛厤鏁伴噺涓嶈冻锛岄渶瑕佹嫞閫夛細{remainingToPick}锛屽彲鐢ㄥ垎閰嶆暟閲忥細{orderDetail.AllocatedQuantity}");
+                    $"閿佸畾淇℃伅鍒嗛厤鏁伴噺涓嶈冻锛岄渶瑕佹嫞閫夛細{remainingToPick}锛岄攣瀹氫俊鎭垎閰嶆暟閲忥細{lockInfo.AssignQuantity}");
             }
 
-            // 閲嶈淇锛氭鏌ラ攣瀹氭暟閲忔槸鍚﹁冻澶�
+            // 妫�鏌ラ攣瀹氭暟閲忔槸鍚﹁冻澶�
             if (orderDetail.LockQuantity < remainingToPick)
             {
                 _logger.LogWarning($"璁㈠崟鏄庣粏閿佸畾鏁伴噺涓嶈冻 - 闇�瑕佹嫞閫�: {remainingToPick}, 鍙敤閿佸畾鏁伴噺: {orderDetail.LockQuantity}");
                 return ValidationResult<(Dt_OutStockLockInfo, Dt_OutboundOrderDetail, Dt_StockInfoDetail, Dt_OutboundBatch)>.Error(
                     $"璁㈠崟鏄庣粏閿佸畾鏁伴噺涓嶈冻锛岄渶瑕佹嫞閫夛細{remainingToPick}锛屽彲鐢ㄩ攣瀹氭暟閲忥細{orderDetail.LockQuantity}");
+            }
+
+            // 妫�鏌ヨ鍗曟槑缁嗗垎閰嶆暟閲忔槸鍚︿负璐熸暟
+            if (orderDetail.AllocatedQuantity < 0)
+            {
+                _logger.LogError($"璁㈠崟鏄庣粏鍒嗛厤鏁伴噺涓鸿礋鏁� - 褰撳墠鍊�: {orderDetail.AllocatedQuantity}");
+                return ValidationResult<(Dt_OutStockLockInfo, Dt_OutboundOrderDetail, Dt_StockInfoDetail, Dt_OutboundBatch)>.Error(
+                    $"璁㈠崟鏄庣粏鍒嗛厤鏁伴噺寮傚父锛堣礋鏁帮級锛岃鑱旂郴绠$悊鍛樺鐞�");
             }
 
             var stockDetail = await _stockInfoDetailService.Db.Queryable<Dt_StockInfoDetail>()
@@ -2151,6 +2907,11 @@
         /// </summary>
         private async Task<List<SplitResult>> CheckAndAutoSplitIfNeeded(Dt_OutStockLockInfo lockInfo, Dt_StockInfoDetail stockDetail, string palletCode)
         {
+            if (lockInfo.IsUnallocated == 1 || lockInfo.OrderDetailId == 0)
+            {
+                _logger.LogInformation($"璺宠繃鏈垎閰嶉攣瀹氳褰曠殑鑷姩鎷嗗寘妫�鏌� - 閿佸畾ID: {lockInfo.Id}");
+                return null;
+            }
             // 妫�鏌ユ槸鍚﹂渶瑕佽嚜鍔ㄦ媶鍖呯殑鏉′欢锛�
             // 1. 搴撳瓨鏁伴噺澶т簬鍒嗛厤鏁伴噺
             // 2. 閿佸畾淇℃伅鐘舵�佷负鍑哄簱涓�
@@ -2172,21 +2933,32 @@
         }
         /// <summary>
         /// 鎵ц鑷姩鎷嗗寘閫昏緫
+        /// 纭繚鑷姩鎷嗗寘涓嶄細褰卞搷鍥炲簱閫昏緫
         /// </summary>
         private async Task<List<SplitResult>> ExecuteAutoSplitLogic(Dt_OutStockLockInfo lockInfo, Dt_StockInfoDetail stockDetail,
-            decimal splitQuantity, string palletCode)
+     decimal splitQuantity, string palletCode)
         {
             _logger.LogInformation($"寮�濮嬫墽琛岃嚜鍔ㄦ媶鍖呴�昏緫 - 鍘熸潯鐮�: {stockDetail.Barcode}, 鎷嗗寘鏁伴噺: {splitQuantity}");
+
+            // 楠岃瘉鎷嗗寘鏁伴噺鍚堢悊鎬�
+            if (splitQuantity <= 0)
+            {
+                throw new InvalidOperationException($"鎷嗗寘鏁伴噺蹇呴』澶т簬0锛屽綋鍓嶅��: {splitQuantity}");
+            }
+
+            if (stockDetail.StockQuantity < lockInfo.AssignQuantity + splitQuantity)
+            {
+                throw new InvalidOperationException($"搴撳瓨鏁伴噺涓嶈冻浠ヨ繘琛岃嚜鍔ㄦ媶鍖�");
+            }
 
             // 鐢熸垚鏂版潯鐮�
             string newBarcode = await GenerateNewBarcode();
 
-            // 閲嶈淇锛氳褰曟媶鍖呭墠鐨勫垎閰嶆暟閲�
-            decimal originalAssignQtyBefore = lockInfo.AssignQuantity;
-            decimal originalOrderQtyBefore = lockInfo.OrderQuantity;
-            decimal remainQty = originalAssignQtyBefore; // 鑷姩鎷嗗寘鍚庡師鏉$爜鍒嗛厤鏁伴噺淇濇寔涓嶅彉
+            // 璁板綍鎷嗗寘鍓嶇殑鍒嗛厤鏁伴噺
+            decimal originalAssignQty = lockInfo.AssignQuantity;
+            decimal remainQty = originalAssignQty; // 鍘熼攣瀹氫俊鎭垎閰嶆暟閲忎繚鎸佷笉鍙�
 
-            // 鍒涘缓鏂板簱瀛樻槑缁嗭紙鍓╀綑閮ㄥ垎锛�
+            // 鍒涘缓鏂板簱瀛樻槑缁嗭紙澶氫綑閮ㄥ垎锛�
             var newStockDetail = new Dt_StockInfoDetail
             {
                 StockId = stockDetail.StockId,
@@ -2207,17 +2979,11 @@
             await _stockInfoDetailService.Db.Insertable(newStockDetail).ExecuteCommandAsync();
             _logger.LogInformation($"鍒涘缓鏂板簱瀛樻槑缁� - 鏉$爜: {newBarcode}, 搴撳瓨鏁伴噺: {splitQuantity}");
 
-            // 鏇存柊鍘熷簱瀛樻槑缁�
-            // 閲嶈淇锛氳嚜鍔ㄦ媶鍖呮椂锛屽師搴撳瓨鏁伴噺淇濇寔涓嶅彉锛屽洜涓烘垜浠鍒嗘嫞鐨勫氨鏄垎閰嶆暟閲�
-            // stockDetail.StockQuantity 淇濇寔涓嶅彉
-
-            _logger.LogInformation($"鍘熷簱瀛樻槑缁嗕繚鎸佷笉鍙� - 搴撳瓨鏁伴噺: {stockDetail.StockQuantity}");
-
-            // 鍒涘缓鏂伴攣瀹氫俊鎭紙鍓╀綑閮ㄥ垎锛�
+            // 鍒涘缓鏂伴攣瀹氫俊鎭紙澶氫綑閮ㄥ垎锛�- 鏍囪涓烘湭鍒嗛厤
             var newLockInfo = new Dt_OutStockLockInfo
             {
                 OrderNo = lockInfo.OrderNo,
-                OrderDetailId = lockInfo.OrderDetailId,
+                OrderDetailId = 0, // 閲嶈锛氫笉缁戝畾鍒板叿浣撹鍗曟槑缁�
                 OutboundBatchNo = lockInfo.OutboundBatchNo,
                 MaterielCode = lockInfo.MaterielCode,
                 MaterielName = lockInfo.MaterielName,
@@ -2241,19 +3007,19 @@
                 WarehouseCode = lockInfo.WarehouseCode,
                 BarcodeQty = lockInfo.BarcodeQty,
                 BarcodeUnit = lockInfo.BarcodeUnit,
+                IsUnallocated = 1 // 鏍囪涓烘湭鍒嗛厤
             };
 
             await _outStockLockInfoService.Db.Insertable(newLockInfo).ExecuteCommandAsync();
-            _logger.LogInformation($"鍒涘缓鏂伴攣瀹氫俊鎭� - 鏉$爜: {newBarcode}, 鍒嗛厤鏁伴噺: {splitQuantity}");
+            _logger.LogInformation($"鍒涘缓鏂伴攣瀹氫俊鎭� - 鏉$爜: {newBarcode}, 鍒嗛厤鏁伴噺: {splitQuantity}, 鏍囪涓烘湭鍒嗛厤");
 
-            // 鑷姩鎷嗗寘鏃讹紝鍘熼攣瀹氫俊鎭殑鍒嗛厤鏁伴噺淇濇寔涓嶅彉
-            // 鍥犱负鑷姩鎷嗗寘鍚庯紝鍘熸潯鐮佷粛鐒堕渶瑕佽鍒嗘嫞锛屽垎閰嶆暟閲忎笉搴旇鏀瑰彉
-            _logger.LogInformation($"鍘熼攣瀹氫俊鎭繚鎸佷笉鍙� - 鍒嗛厤鏁伴噺: {lockInfo.AssignQuantity}, 璁㈠崟鏁伴噺: {lockInfo.OrderQuantity}");
+            // 鑷姩鎷嗗寘涓嶆敼鍙樿鍗曟槑缁嗙殑鍒嗛厤鏁伴噺
+            _logger.LogInformation($"鑷姩鎷嗗寘 - 璁㈠崟鏄庣粏鍒嗛厤鏁伴噺淇濇寔涓嶅彉");
 
             // 璁板綍鎷嗗寘鍘嗗彶
             await RecordSplitHistory(lockInfo, stockDetail, splitQuantity, newBarcode, true, stockDetail.StockQuantity);
 
-            //  鍒涘缓鎷嗗寘缁撴灉鍒楄〃
+            // 鍒涘缓鎷嗗寘缁撴灉鍒楄〃
             var splitResults = CreateSplitResults(lockInfo, splitQuantity, remainQty, newBarcode, stockDetail.Barcode);
 
             _logger.LogInformation($"鑷姩鎷嗗寘閫昏緫鎵ц瀹屾垚");
@@ -2261,7 +3027,46 @@
             return splitResults;
         }
 
+        /// <summary>
+        /// 楠岃瘉鑷姩鎷嗗寘鍚庢暟鎹竴鑷存��
+        /// </summary>
+        private async Task ValidateDataConsistencyAfterAutoSplit(long orderDetailId, decimal originalAllocatedQty, decimal originalLockQty, decimal splitQuantity)
+        {
+            // 閲嶆柊鑾峰彇璁㈠崟鏄庣粏鏁版嵁
+            var orderDetail = await _outboundOrderDetailService.Db.Queryable<Dt_OutboundOrderDetail>()
+                .FirstAsync(x => x.Id == orderDetailId);
 
+            if (orderDetail == null)
+                return;
+
+            // 璁$畻鎵�鏈夐攣瀹氫俊鎭殑鎬诲垎閰嶆暟閲�
+            var allLocks = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
+                .Where(x => x.OrderDetailId == orderDetailId)
+                .ToListAsync();
+
+            decimal totalLockAssignQty = allLocks.Sum(x => x.AssignQuantity);
+
+            _logger.LogInformation($"鑷姩鎷嗗寘鍚庢暟鎹竴鑷存�ч獙璇� - 璁㈠崟鏄庣粏鍒嗛厤鏁伴噺: {orderDetail.AllocatedQuantity}, 閿佸畾淇℃伅鎬诲垎閰嶆暟閲�: {totalLockAssignQty}");
+
+            // 楠岃瘉鑷姩鎷嗗寘鍚庣殑鏁版嵁涓�鑷存��
+            decimal expectedAllocatedQty = originalAllocatedQty + splitQuantity;
+            decimal expectedLockQty = originalLockQty + splitQuantity;
+
+            if (Math.Abs(orderDetail.AllocatedQuantity - expectedAllocatedQty) > 0.01m)
+            {
+                _logger.LogWarning($"鑷姩鎷嗗寘鍚庡垎閰嶆暟閲忓紓甯� - 鏈熸湜: {expectedAllocatedQty}, 瀹為檯: {orderDetail.AllocatedQuantity}");
+            }
+
+            if (Math.Abs(orderDetail.LockQuantity - expectedLockQty) > 0.01m)
+            {
+                _logger.LogWarning($"鑷姩鎷嗗寘鍚庨攣瀹氭暟閲忓紓甯� - 鏈熸湜: {expectedLockQty}, 瀹為檯: {orderDetail.LockQuantity}");
+            }
+
+            if (Math.Abs(orderDetail.AllocatedQuantity - totalLockAssignQty) > 0.01m)
+            {
+                _logger.LogWarning($"鑷姩鎷嗗寘鍚庢暟鎹笉涓�鑷� - 璁㈠崟鏄庣粏鍒嗛厤鏁伴噺: {orderDetail.AllocatedQuantity}, 閿佸畾淇℃伅鎬诲垎閰嶆暟閲�: {totalLockAssignQty}");
+            }
+        }
         #endregion
 
         #region 鏍稿績閫昏緫鏂规硶
@@ -2351,7 +3156,7 @@
             decimal originalPickedQty = lockInfo.PickedQty;
             decimal originalAssignQty = lockInfo.AssignQuantity; // 璁板綍鍘熷鍒嗛厤鏁伴噺
 
-            // 閲嶈淇锛氬彧鎭㈠宸叉嫞閫夋暟閲忥紝涓嶄慨鏀瑰垎閰嶆暟閲�
+            // 鍙仮澶嶅凡鎷i�夋暟閲忥紝涓嶄慨鏀瑰垎閰嶆暟閲�
             lockInfo.PickedQty -= pickingRecord.PickQuantity;
 
             // 纭繚宸叉嫞閫夋暟閲忎笉浼氫负璐熸暟
@@ -2434,7 +3239,7 @@
                 throw new InvalidOperationException("鏈壘鍒拌鍗曟槑缁�");
 
             orderDetail = latestOrderDetail;
-            // 閲嶈淇锛氶獙璇佸垎閰嶆暟閲忎笉浼氬彉鎴愯礋鏁�
+            // 楠岃瘉鍒嗛厤鏁伴噺涓嶄細鍙樻垚璐熸暟
             if (orderDetail.AllocatedQuantity < pickedQty)
             {
                 decimal actualPickedQty = orderDetail.AllocatedQuantity;
@@ -2529,9 +3334,9 @@
                 decimal originalAllocatedQty = orderDetail.AllocatedQuantity;
                 decimal originalLockQty = orderDetail.LockQuantity;
 
-                // 閲嶈淇锛氬彧鎭㈠鐩稿叧鏁伴噺锛屽垎閰嶆暟閲忎繚鎸佷笉鍙�
+                // 鍙仮澶嶇浉鍏虫暟閲忥紝鍒嗛厤鏁伴噺淇濇寔涓嶅彉
                 orderDetail.OverOutQuantity -= pickingRecord.PickQuantity;
-                orderDetail.AllocatedQuantity += pickingRecord.PickQuantity;   
+                orderDetail.AllocatedQuantity += pickingRecord.PickQuantity;
                 orderDetail.LockQuantity += pickingRecord.PickQuantity;
                 if (orderDetail.OverOutQuantity < 0) orderDetail.OverOutQuantity = 0;
                 if (orderDetail.AllocatedQuantity < 0) orderDetail.AllocatedQuantity = 0;
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 196b146..448d71e 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"
@@ -3159,12 +3159,20 @@
         public bool HasSplitRecords { get; set; }
         public decimal RemainingLocksReturnQty { get; set; }
         public decimal PalletStockReturnQty { get; set; }
+
         public decimal SplitReturnQty { get; set; }
         public decimal TotalReturnQty { get; set; }
+
+        // 鏈垎閰嶇殑閿佸畾璁板綍锛堝鑷姩鎷嗗寘浜х敓鐨勶級
+        public bool HasUnallocatedLocks { get; set; }
+        public List<Dt_OutStockLockInfo> UnallocatedLocks { get; set; } = new List<Dt_OutStockLockInfo>();
+        public decimal UnallocatedLocksReturnQty { get; set; }
+
         public List<Dt_OutStockLockInfo> RemainingLocks { get; set; } = new List<Dt_OutStockLockInfo>();
         public List<Dt_StockInfoDetail> PalletStockGoods { get; set; } = new List<Dt_StockInfoDetail>();
         public List<Dt_SplitPackageRecord> SplitRecords { get; set; } = new List<Dt_SplitPackageRecord>();
 
+        public List<string> AllBarcodes { get; set; } = new List<string>();
         // 绌烘墭鐩樼浉鍏冲睘鎬�
         public bool IsEmptyPallet { get; set; }
         public bool HasActiveTasks { get; set; }

--
Gitblit v1.9.3