From b736d2069a049a0465a3a8814ab72afabb6a187c Mon Sep 17 00:00:00 2001
From: pan <antony1029@163.com>
Date: 星期二, 25 十一月 2025 11:10:40 +0800
Subject: [PATCH] 提交

---
 项目代码/WMS无仓储版/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundOrderDetailService.cs |  176 +++++++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 123 insertions(+), 53 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/OutboundOrderDetailService.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/OutboundOrderDetailService.cs"
index e856df7..4459d4c 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/OutboundOrderDetailService.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/OutboundOrderDetailService.cs"
@@ -49,7 +49,8 @@
         /// <summary>
         /// 鍒嗛厤鍑哄簱搴撳瓨  鎸夊厛杩涘厛鍑哄師鍒欏垎閰�
         /// </summary>
-        public (List<Dt_StockInfo>, List<Dt_OutboundOrderDetail>, List<Dt_OutStockLockInfo>, List<Dt_LocationInfo>) AssignStockOutbound(List<Dt_OutboundOrderDetail> outboundOrderDetails)
+        public (List<Dt_StockInfo>, List<Dt_OutboundOrderDetail>, List<Dt_OutStockLockInfo>, List<Dt_LocationInfo>)
+       AssignStockOutbound(List<Dt_OutboundOrderDetail> outboundOrderDetails)
         {
             if (!outboundOrderDetails.Any())
             {
@@ -61,21 +62,22 @@
                 throw new Exception("璇峰嬁鍚屾椂鎿嶄綔澶氫釜鍗曟嵁鏄庣粏");
             }
 
+            var orderId = outboundOrderDetails.First().OrderId;
             var outboundOrder = _outboundOrderService.Db.Queryable<Dt_OutboundOrder>()
-                .First(x => x.Id == outboundOrderDetails.First().OrderId);
+                .First(x => x.Id == orderId);
 
             List<Dt_StockInfo> outStocks = new List<Dt_StockInfo>();
             List<Dt_OutStockLockInfo> outStockLockInfos = new List<Dt_OutStockLockInfo>();
             List<Dt_LocationInfo> locationInfos = new List<Dt_LocationInfo>();
 
-            // 鎸夌墿鏂欍�佹壒娆°�佷緵搴斿晢鍒嗙粍
+            // 鎸夌墿鏂欏拰鎵规鍒嗙粍澶勭悊
             var groupDetails = outboundOrderDetails
                 .GroupBy(x => new { x.MaterielCode, x.BatchNo, x.SupplyCode })
                 .Select(x => new
                 {
-                    x.Key.MaterielCode,
-                    x.Key.BatchNo,
-                    x.Key.SupplyCode,
+                    MaterielCode = x.Key.MaterielCode,
+                    BatchNo = x.Key.BatchNo,
+                    SupplyCode = x.Key.SupplyCode,
                     Details = x.ToList(),
                     TotalNeedQuantity = x.Sum(v => v.OrderQuantity - v.OverOutQuantity - v.LockQuantity - v.MoveQty)
                 })
@@ -86,101 +88,169 @@
             {
                 var needQuantity = item.TotalNeedQuantity;
 
-                // 鑾峰彇鍙敤搴撳瓨锛堝凡鎸夊厛杩涘厛鍑烘帓搴忥級
-                List<Dt_StockInfo> stockInfos = _stockService.StockInfoService.GetUseableStocks(
-                    item.MaterielCode, item.BatchNo, item.SupplyCode);
+                // 鑾峰彇鍙敤搴撳瓨锛堟寜鍏堣繘鍏堝嚭鎺掑簭锛�
+                List<Dt_StockInfo> stockInfos = _stockService.StockInfoService.GetUseableStocks(item.MaterielCode, item.BatchNo, item.SupplyCode);
 
                 if (!stockInfos.Any())
                 {
                     throw new Exception($"鐗╂枡[{item.MaterielCode}]鎵规[{item.BatchNo}]鏈壘鍒板彲鍒嗛厤搴撳瓨");
                 }
 
-                // 鍒嗛厤搴撳瓨
-                var (autoAssignStocks, stockAllocations) = _stockService.StockInfoService.GetOutboundStocks(
-                    stockInfos, item.MaterielCode, needQuantity, out decimal residueQuantity);
+                // 鍒嗛厤搴撳瓨锛堟寜鍏堣繘鍏堝嚭锛�
+                var (autoAssignStocks, stockAllocations) = _stockService.StockInfoService.GetOutboundStocks(stockInfos, item.MaterielCode, needQuantity, out decimal residueQuantity);
 
-                // 妫�鏌ュ垎閰嶇粨鏋�
-                if (residueQuantity > 0)
+                // 妫�鏌ュ垎閰嶇粨鏋滐細濡傛灉瀹屽叏鏃犳硶鍒嗛厤锛堝垎閰嶆暟閲忎负0锛夛紝鍒欐姤閿�
+                decimal allocatedQuantity = needQuantity - residueQuantity;
+                if (allocatedQuantity <= 0)
                 {
-                    var allocatedQuantity = needQuantity - residueQuantity;
-                    throw new Exception($"鐗╂枡[{item.MaterielCode}]搴撳瓨涓嶈冻锛岄渶瑕亄needQuantity}锛屽疄闄呭垎閰峽allocatedQuantity}");
+                    throw new Exception($"鐗╂枡[{item.MaterielCode}]鎵规[{item.BatchNo}]搴撳瓨涓嶈冻锛岄渶瑕亄needQuantity}锛屼絾鏃犳硶鍒嗛厤浠讳綍搴撳瓨");
                 }
 
                 outStocks.AddRange(autoAssignStocks);
 
-                // 鎸夊厛杩涘厛鍑哄垎閰嶉攣瀹氭暟閲�
-                DistributeLockQuantityByFIFO(item.Details, autoAssignStocks, stockAllocations,
-                    outStockLockInfos, outboundOrder);
+                // 鎸夊厛杩涘厛鍑哄師鍒欏垎閰嶉攣瀹氭暟閲忓埌鍚勪釜鏄庣粏
+                var distributionResult = DistributeLockQuantityByFIFO(item.Details, autoAssignStocks, stockAllocations, outStockLockInfos, outboundOrder);
+
+                if (!distributionResult.success)
+                {
+                    throw new Exception(distributionResult.message);
+                }
+
+                // 鏇存柊鍑哄簱鍗曟槑缁嗙姸鎬�
+                UpdateOrderDetailStatus(item.Details, allocatedQuantity, needQuantity);
             }
 
-            locationInfos.AddRange(_locationInfoService.GetLocationInfos(
-                outStocks.Select(x => x.LocationCode).Distinct().ToList()));
+            if (outStocks.Any())
+            {
+                locationInfos.AddRange(_locationInfoService.GetLocationInfos(
+                    outStocks.Select(x => x.LocationCode).Distinct().ToList()));
+            }
 
             return (outStocks, outboundOrderDetails, outStockLockInfos, locationInfos);
         }
-       
-        /// <summary>
-        /// 鎸夊厛杩涘厛鍑哄師鍒欏垎閰嶉攣瀹氭暟閲忓埌鍚勪釜鏄庣粏  
-        /// </summary>
-        private void DistributeLockQuantityByFIFO(List<Dt_OutboundOrderDetail> details,List<Dt_StockInfo> assignStocks,Dictionary<int, decimal> stockAllocations,List<Dt_OutStockLockInfo> outStockLockInfos,
-    Dt_OutboundOrder outboundOrder)
+        private (bool success, string message) DistributeLockQuantityByFIFO(
+           List<Dt_OutboundOrderDetail> details,
+           List<Dt_StockInfo> assignStocks,
+           Dictionary<int, decimal> stockAllocations,
+           List<Dt_OutStockLockInfo> outStockLockInfos,
+           Dt_OutboundOrder outboundOrder)
         {
-            var sortedStocks = assignStocks.OrderBy(x => x.CreateDate).ToList();
-                    
-            var totalNeedQuantity = details.Sum(d => d.OrderQuantity - d.OverOutQuantity - d.LockQuantity - d.MoveQty);
+            // 鎸夊厛杩涘厛鍑烘帓搴忓嚭搴撳崟鏄庣粏锛圛d灏忕殑浼樺厛锛�
+            var sortedDetails = details
+                .Where(d => d.OrderQuantity - d.OverOutQuantity - d.LockQuantity - d.MoveQty > 0)
+                .OrderBy(x => x.Id)
+                .ToList();
+
+            if (!sortedDetails.Any())
+                return (true, "鏃犻渶鍒嗛厤");
+
+            // 鑾峰彇鎵�鏈夊垎閰嶄簡搴撳瓨鐨勬槑缁嗭紝鎸夊厛杩涘厛鍑烘帓搴�
+            var allocatedStockDetails = assignStocks
+                .SelectMany(x => x.Details)
+                .Where(x => stockAllocations.ContainsKey(x.Id) && stockAllocations[x.Id] > 0)
+                .OrderBy(x => x.CreateDate)
+                .ThenBy(x => x.StockId)
+                .ToList();
+
+            if (!allocatedStockDetails.Any())
+            {
+                return (false, "娌℃湁鍙垎閰嶇殑搴撳瓨鏄庣粏");
+            }
+
+            decimal totalNeedQuantity = sortedDetails.Sum(d =>
+                d.OrderQuantity - d.OverOutQuantity - d.LockQuantity - d.MoveQty);
             decimal allocatedQuantity = 0;
 
-            foreach (var stock in sortedStocks)
+            // 涓烘瘡涓簱瀛樻槑缁嗗垱寤哄垎閰嶈褰�
+            foreach (var stockDetail in allocatedStockDetails)
             {
-                if (allocatedQuantity >= totalNeedQuantity) break;
-
-                if (!stockAllocations.TryGetValue(stock.Id, out decimal stockAllocation) || stockAllocation <= 0)
+                if (!stockAllocations.TryGetValue(stockDetail.Id, out decimal allocatedQuantityForStock))
                     continue;
 
-                var sortedDetails = details
-                    .Where(d => d.OrderQuantity - d.OverOutQuantity - d.LockQuantity - d.MoveQty > 0)
-                    .OrderBy(x => x.Id)
-                    .ToList();
+                if (allocatedQuantityForStock <= 0) continue;
 
+                var stockInfo = assignStocks.First(x => x.Id == stockDetail.StockId);
+                decimal remainingAllocate = allocatedQuantityForStock;
+
+                // 鎸夐『搴忓垎閰嶇粰鍚勪釜鍑哄簱鍗曟槑缁�
                 foreach (var detail in sortedDetails)
                 {
-                    if (stockAllocation <= 0) break;
+                    if (remainingAllocate <= 0) break;
 
+                    // 璁$畻杩欎釜鏄庣粏杩橀渶瑕佸垎閰嶇殑鏁伴噺
                     var detailNeed = detail.OrderQuantity - detail.OverOutQuantity - detail.LockQuantity - detail.MoveQty;
                     if (detailNeed <= 0) continue;
 
-                    var assignQuantity = Math.Min(stockAllocation, detailNeed);
+                    // 鍒嗛厤鏁伴噺
+                    var assignQuantity = Math.Min(remainingAllocate, detailNeed);
 
-                    // 浣跨敤搴撳瓨涓殑鏈夋晥鏉$爜
-                    var barcode = stock.Details
-                        .Where(d => !string.IsNullOrEmpty(d.Barcode))
-                        .Select(d => d.Barcode)
-                        .FirstOrDefault();
-
-                    if (string.IsNullOrEmpty(barcode))
+                    // 楠岃瘉鏉$爜鏄惁瀛樺湪
+                    if (string.IsNullOrEmpty(stockDetail.Barcode))
                     {
-                        throw new Exception($"搴撳瓨ID[{stock.Id}]鐨勬潯鐮佷负绌�");
+                        return (false, $"搴撳瓨鏄庣粏ID[{stockDetail.Id}]鐨勬潯鐮佷负绌�");
                     }
 
+                    // 鍒涘缓鍑哄簱閿佸畾淇℃伅
                     var lockInfo = _outStockLockInfoService.GetOutStockLockInfo(
-                        outboundOrder, detail, stock, assignQuantity, barcode);
+                        outboundOrder, detail, stockInfo, assignQuantity, stockDetail.Barcode);
                     outStockLockInfos.Add(lockInfo);
 
+                    // 鏇存柊鏄庣粏鐨勯攣瀹氭暟閲�
                     detail.LockQuantity += assignQuantity;
-                    stockAllocation -= assignQuantity;
+                    remainingAllocate -= assignQuantity;
                     allocatedQuantity += assignQuantity;
+                }
 
-                    if (allocatedQuantity >= totalNeedQuantity) break;
+                // 濡傛灉杩樻湁鍓╀綑鍒嗛厤鏁伴噺锛岃褰曡鍛�
+                if (remainingAllocate > 0)
+                {
+                    _logger.LogWarning($"搴撳瓨鍒嗛厤鍚庝粛鏈夊墿浣欐暟閲忔湭鍒嗛厤: {remainingAllocate}, 鏉$爜: {stockDetail.Barcode}");
                 }
             }
 
-            // 楠岃瘉鏄惁瀹屽叏鍒嗛厤
+            // 楠岃瘉鏄惁鑷冲皯鍒嗛厤浜嗕竴閮ㄥ垎
+            if (allocatedQuantity <= 0)
+            {
+                return (false, "搴撳瓨鍒嗛厤澶辫触锛屾棤娉曞垎閰嶄换浣曟暟閲�");
+            }
+
+            // 璁板綍鍒嗛厤缁撴灉
             if (allocatedQuantity < totalNeedQuantity)
             {
-                _logger.LogWarning($"搴撳瓨鍒嗛厤涓嶅畬鍏紝闇�瑕亄totalNeedQuantity}锛屽疄闄呭垎閰峽allocatedQuantity}");
+                _logger.LogWarning($"搴撳瓨閮ㄥ垎鍒嗛厤锛岄渶瑕亄totalNeedQuantity}锛屽疄闄呭垎閰峽allocatedQuantity}");
             }
+            else
+            {
+                _logger.LogInformation($"搴撳瓨瀹屽叏鍒嗛厤锛屽垎閰嶆暟閲弡allocatedQuantity}");
+            }
+
+            return (true, "鍒嗛厤鎴愬姛");
         }
 
+        private void UpdateOrderDetailStatus(List<Dt_OutboundOrderDetail> details,
+    decimal allocatedQuantity, decimal needQuantity)
+        {
+            foreach (var detail in details)
+            {
+                var detailNeed = detail.OrderQuantity - detail.LockQuantity - detail.OverOutQuantity - detail.MoveQty;
+
+                if (detailNeed <= 0)
+                {
+                    // 璇ユ槑缁嗗凡瀹屽叏鍒嗛厤
+                    detail.OrderDetailStatus = OrderDetailStatusEnum.AssignOver.ObjToInt();
+                }
+                else if (allocatedQuantity < needQuantity)
+                {
+                    // 鏁翠綋閮ㄥ垎鍒嗛厤锛岃鏄庣粏鍙兘杩樻湁闇�姹�
+                    detail.OrderDetailStatus = OrderDetailStatusEnum.AssignOverPartial.ObjToInt();
+                }
+                else
+                {
+                    // 鏁翠綋瀹屽叏鍒嗛厤
+                    detail.OrderDetailStatus = OrderDetailStatusEnum.AssignOver.ObjToInt();
+                }
+            }
+        }
         /// <summary>
         /// 鍑哄簱搴撳瓨鍒嗛厤鍚庯紝鏇存柊鏁版嵁搴撴暟鎹�
         /// </summary>

--
Gitblit v1.9.3