From 07774c70a65bed6bacf936bdd66f756ba9c7f266 Mon Sep 17 00:00:00 2001
From: pan <antony1029@163.com>
Date: 星期二, 02 十二月 2025 10:15:48 +0800
Subject: [PATCH] 提交

---
 项目代码/WMS无仓储版/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundOrderDetailService.cs |  242 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 235 insertions(+), 7 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 121a74e..d222228 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"
@@ -66,6 +66,11 @@
             var outboundOrder = _outboundOrderService.Db.Queryable<Dt_OutboundOrder>()
                 .First(x => x.Id == orderId);
 
+            if (!CanReassignOrder(outboundOrder))
+            {
+                throw new Exception($"璁㈠崟{outboundOrder.OrderNo}鐘舵�佷笉鍏佽閲嶆柊鍒嗛厤搴撳瓨");
+            }
+
             List<Dt_StockInfo> outStocks = new List<Dt_StockInfo>();
             List<Dt_OutStockLockInfo> outStockLockInfos = new List<Dt_OutStockLockInfo>();
             List<Dt_LocationInfo> locationInfos = new List<Dt_LocationInfo>();
@@ -79,7 +84,7 @@
                     BatchNo = x.Key.BatchNo,
                     SupplyCode = x.Key.SupplyCode,
                     Details = x.ToList(),
-                    TotalNeedQuantity = x.Sum(v => v.OrderQuantity - v.OverOutQuantity - v.LockQuantity - v.MoveQty)
+                    TotalNeedQuantity = CalculateReassignNeedQuantity(x.ToList())
                 })
                 .Where(x => x.TotalNeedQuantity > 0)
                 .ToList();
@@ -93,7 +98,7 @@
 
                 if (!stockInfos.Any())
                 {
-                    throw new Exception($"鐗╂枡[{item.MaterielCode}]鎵规[{item.BatchNo}]鏈壘鍒板彲鍒嗛厤搴撳瓨");
+                    throw new Exception($"鐗╂枡[{item.MaterielCode}]鎵规[{item.BatchNo}]渚涘簲鍟哰{item.SupplyCode}]鏈壘鍒板彲鍒嗛厤搴撳瓨");
                 }
 
                 // 鍒嗛厤搴撳瓨锛堟寜鍏堣繘鍏堝嚭锛�
@@ -127,6 +132,39 @@
             }
 
             return (outStocks, outboundOrderDetails, outStockLockInfos, locationInfos);
+        }
+
+        /// <summary>
+        /// 妫�鏌ヨ鍗曟槸鍚﹀厑璁搁噸鏂板垎閰�
+        /// </summary>
+        private bool CanReassignOrder(Dt_OutboundOrder outboundOrder)
+        {
+            // 鍏佽閲嶆柊鍒嗛厤鐨勭姸鎬�
+            var allowedStatus = new[] { OutOrderStatusEnum.鏈紑濮�, OutOrderStatusEnum.鍑哄簱涓�, OutOrderStatusEnum.閮ㄥ垎瀹屾垚 };
+
+            return allowedStatus.Contains((OutOrderStatusEnum)outboundOrder.OrderStatus);
+        }
+
+        /// <summary>
+        /// 閲嶆柊璁$畻闇�姹傛暟閲忥紙鑰冭檻閲嶆柊鍒嗛厤鐨勫満鏅級
+        /// </summary>
+        private decimal CalculateReassignNeedQuantity(List<Dt_OutboundOrderDetail> details)
+        {
+            decimal totalNeed = 0;
+
+            foreach (var detail in details)
+            {
+                // 鍏抽敭淇敼锛氶噸鏂板垎閰嶆椂锛屽彧鑰冭檻鏈嚭搴撶殑閮ㄥ垎锛屽拷鐣ラ攣瀹氭暟閲�
+                // 鍥犱负閿佸畾鏁伴噺鍦ㄥ洖搴撴椂宸茬粡琚噸缃�
+                decimal remainingNeed = detail.OrderQuantity - detail.OverOutQuantity - detail.MoveQty;
+
+                if (remainingNeed > 0)
+                {
+                    totalNeed += remainingNeed;
+                }
+            }
+
+            return totalNeed;
         }
         private (bool success, string message) DistributeLockQuantityByFIFO(
            List<Dt_OutboundOrderDetail> details,
@@ -226,6 +264,190 @@
 
             return (true, "鍒嗛厤鎴愬姛");
         }
+
+
+        /// <summary>
+        /// 涓哄垎鎵瑰垎閰嶅簱瀛�
+        /// </summary>
+        public async Task<(List<Dt_StockInfo>, List<Dt_OutboundOrderDetail>, List<Dt_OutStockLockInfo>, List<Dt_LocationInfo>)>
+            AssignStockForBatch(Dt_OutboundOrderDetail orderDetail, decimal batchQuantity, string batchNo)
+        {
+            if (orderDetail == null)
+            {
+                throw new Exception("鏈壘鍒板嚭搴撳崟鏄庣粏淇℃伅");
+            }
+
+            var outboundOrder = await _outboundOrderService.Db.Queryable<Dt_OutboundOrder>()
+                .FirstAsync(x => x.Id == orderDetail.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 = new List<Dt_OutboundOrderDetail> { orderDetail }
+                .GroupBy(x => new { x.MaterielCode, x.BatchNo, x.SupplyCode })
+                .Select(x => new
+                {
+                    MaterielCode = x.Key.MaterielCode,
+                    BatchNo = x.Key.BatchNo,
+                    SupplyCode = x.Key.SupplyCode,
+                    Details = x.ToList(),
+                    TotalNeedQuantity = batchQuantity  // 浣跨敤鍒嗘壒鏁伴噺
+                })
+                .Where(x => x.TotalNeedQuantity > 0)
+                .ToList();
+
+            foreach (var item in groupDetails)
+            {
+                var needQuantity = item.TotalNeedQuantity;
+
+                // 鑾峰彇鍙敤搴撳瓨锛堟寜鍏堣繘鍏堝嚭鎺掑簭锛�
+                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);
+
+                // 妫�鏌ュ垎閰嶇粨鏋�
+                decimal allocatedQuantity = needQuantity - residueQuantity;
+                if (allocatedQuantity <= 0)
+                {
+                    throw new Exception($"鐗╂枡[{item.MaterielCode}]鎵规[{item.BatchNo}]搴撳瓨涓嶈冻锛岄渶瑕亄needQuantity}锛屼絾鏃犳硶鍒嗛厤浠讳綍搴撳瓨");
+                }
+
+                outStocks.AddRange(autoAssignStocks);
+
+                // 鎸夊厛杩涘厛鍑哄師鍒欏垎閰嶉攣瀹氭暟閲忓埌鍚勪釜鏄庣粏
+                var distributionResult = DistributeLockQuantityByFIFO(item.Details, autoAssignStocks, stockAllocations, outStockLockInfos, outboundOrder, batchNo);
+
+                if (!distributionResult.success)
+                {
+                    throw new Exception(distributionResult.message);
+                }
+
+                // 鏇存柊鍑哄簱鍗曟槑缁嗙姸鎬�
+                UpdateOrderDetailStatus(item.Details, allocatedQuantity, needQuantity);
+            }
+
+            if (outStocks.Any())
+            {
+                locationInfos.AddRange(_locationInfoService.GetLocationInfos(
+                  outStocks.Select(x => x.LocationCode).Distinct().ToList()));
+
+
+            }
+
+            return (outStocks, groupDetails.SelectMany(x => x.Details).ToList(), outStockLockInfos, locationInfos);
+        }
+
+        /// <summary>
+        /// 鎸夊厛杩涘厛鍑哄師鍒欏垎閰嶉攣瀹氭暟閲�
+        /// </summary>
+        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,
+            string batchNo)
+        {
+            var sortedDetails = details
+                .Where(d => d.OrderQuantity - d.OverOutQuantity - d.AllocatedQuantity > 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.AllocatedQuantity);
+            decimal allocatedQuantity = 0;
+
+            // 涓烘瘡涓簱瀛樻槑缁嗗垱寤哄垎閰嶈褰�
+            foreach (var stockDetail in allocatedStockDetails)
+            {
+                if (!stockAllocations.TryGetValue(stockDetail.Id, out decimal allocatedQuantityForStock))
+                    continue;
+
+                if (allocatedQuantityForStock <= 0) continue;
+
+                var stockInfo = assignStocks.First(x => x.Id == stockDetail.StockId);
+                decimal remainingAllocate = allocatedQuantityForStock;
+
+                // 鎸夐『搴忓垎閰嶇粰鍚勪釜鍑哄簱鍗曟槑缁�
+                foreach (var detail in sortedDetails)
+                {
+                    if (remainingAllocate <= 0) break;
+
+                    // 璁$畻杩欎釜鏄庣粏杩橀渶瑕佸垎閰嶇殑鏁伴噺
+                    var detailNeed = detail.OrderQuantity - detail.OverOutQuantity - detail.AllocatedQuantity;
+                    if (detailNeed <= 0) continue;
+
+                    // 鍒嗛厤鏁伴噺
+                    var assignQuantity = Math.Min(remainingAllocate, detailNeed);
+
+                    // 楠岃瘉鏉$爜鏄惁瀛樺湪
+                    if (string.IsNullOrEmpty(stockDetail.Barcode))
+                    {
+                        return (false, $"搴撳瓨鏄庣粏ID[{stockDetail.Id}]鐨勬潯鐮佷负绌�");
+                    }
+
+
+                    // 鍒涘缓鍑哄簱閿佸畾淇℃伅
+                    var lockInfo = _outStockLockInfoService.GetOutStockLockInfo(
+                        outboundOrder, detail, stockInfo, assignQuantity, stockDetail.Barcode, batchNo);
+                    outStockLockInfos.Add(lockInfo);
+
+                    // 鏇存柊鏄庣粏鐨勫凡鍒嗛厤鏁伴噺
+                    detail.AllocatedQuantity += assignQuantity;
+                    detail.LockQuantity = detail.AllocatedQuantity;
+                    remainingAllocate -= assignQuantity;
+                    allocatedQuantity += assignQuantity;
+                }
+
+                // 濡傛灉杩樻湁鍓╀綑鍒嗛厤鏁伴噺锛岃褰曡鍛�
+                if (remainingAllocate > 0)
+                {
+                    _logger.LogWarning($"搴撳瓨鍒嗛厤鍚庝粛鏈夊墿浣欐暟閲忔湭鍒嗛厤: {remainingAllocate}, 鏉$爜: {stockDetail.Barcode}");
+                }
+            }
+
+            // 楠岃瘉鏄惁鑷冲皯鍒嗛厤浜嗕竴閮ㄥ垎
+            if (allocatedQuantity <= 0)
+            {
+                return (false, "搴撳瓨鍒嗛厤澶辫触锛屾棤娉曞垎閰嶄换浣曟暟閲�");
+            }
+
+            // 璁板綍鍒嗛厤缁撴灉
+            if (allocatedQuantity < totalNeedQuantity)
+            {
+                _logger.LogWarning($"搴撳瓨閮ㄥ垎鍒嗛厤锛岄渶瑕亄totalNeedQuantity}锛屽疄闄呭垎閰峽allocatedQuantity}");
+            }
+            else
+            {
+                _logger.LogInformation($"搴撳瓨瀹屽叏鍒嗛厤锛屽垎閰嶆暟閲弡allocatedQuantity}");
+            }
+
+            return (true, "鍒嗛厤鎴愬姛");
+        }
+
 
         private void UpdateOrderDetailStatus(List<Dt_OutboundOrderDetail> details,
     decimal allocatedQuantity, decimal needQuantity)
@@ -460,12 +682,18 @@
             decimal remainingAssign = assignQuantity;
 
             // 鎸夊厛杩涘厛鍑哄垎閰嶅簱瀛樻槑缁�
-            var sortedDetails = stock.Details
+            var sorteds = stock.Details
                 .Where(d => d.MaterielCode == detail.MaterielCode &&
-                           d.BatchNo == detail.BatchNo &&
-                           (d.StockQuantity - d.OutboundQuantity) > 0)
-                .OrderBy(d => d.CreateDate)
-                .ToList();
+                           (d.StockQuantity - d.OutboundQuantity) > 0);
+               // .OrderBy(d => d.CreateDate);             
+
+            if (!string.IsNullOrEmpty(detail.BatchNo))
+            {
+                sorteds= stock.Details.Where(x => x.BatchNo == detail.BatchNo);
+            }
+
+            var sortedDetails= sorteds.ToList().OrderBy(d => d.CreateDate);
+
 
             foreach (var stockDetail in sortedDetails)
             {

--
Gitblit v1.9.3