From 2a78fec08d73995a10d0c1c97690eb3deabf2b8a Mon Sep 17 00:00:00 2001
From: 覃楚胧 <qinchulong@hnkhzn.com>
Date: 星期五, 12 十二月 2025 16:18:23 +0800
Subject: [PATCH] 数据库字典

---
 项目代码/WMS无仓储版/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundOrderDetailService.cs |  128 ++++++++++++++++++++++++------------------
 1 files changed, 73 insertions(+), 55 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 f78db64..664827c 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"
@@ -78,13 +78,14 @@
 
             // 鎸夌墿鏂欏拰鎵规鍒嗙粍澶勭悊
             var groupDetails = outboundOrderDetails
-                .GroupBy(x => new { x.MaterielCode, x.BatchNo, x.SupplyCode })
+                .GroupBy(x => new { x.MaterielCode, x.BatchNo, x.SupplyCode,x.WarehouseCode })
                 .Select(x => new
                 {
                     MaterielCode = x.Key.MaterielCode,
                     BatchNo = x.Key.BatchNo,
                     SupplyCode = x.Key.SupplyCode,
                     Details = x.ToList(),
+                    WarehouseCode=x.Key.WarehouseCode,
                     TotalNeedQuantity = CalculateReassignNeedQuantity(x.ToList())
                 })
                 .Where(x => x.TotalNeedQuantity > 0)
@@ -93,9 +94,9 @@
             foreach (var item in groupDetails)
             {
                 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,item.WarehouseCode, outboundOrder.FactoryArea);
 
                 if (!stockInfos.Any())
                 {
@@ -311,12 +312,13 @@
 
             // 鎸夌墿鏂欏拰鎵规鍒嗙粍澶勭悊锛堣繖閲屽彧鏈変竴涓槑缁嗭級
             var groupDetails = new List<Dt_OutboundOrderDetail> { orderDetail }
-                .GroupBy(x => new { x.MaterielCode, x.BatchNo, x.SupplyCode })
+                .GroupBy(x => new { x.MaterielCode, x.BatchNo, x.SupplyCode ,x.WarehouseCode})
                 .Select(x => new
                 {
                     MaterielCode = x.Key.MaterielCode,
                     BatchNo = x.Key.BatchNo,
                     SupplyCode = x.Key.SupplyCode,
+                    WarehouseCode=x.Key.WarehouseCode,
                     Details = x.ToList(),
                     TotalNeedQuantity = batchQuantity  // 浣跨敤鍒嗘壒鏁伴噺
                 })
@@ -328,7 +330,7 @@
                 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,item.WarehouseCode,outboundOrder.FactoryArea);
                 if (!stockInfos.Any())
                 {
                     throw new Exception($"鐗╂枡[{item.MaterielCode}]鎵规[{item.BatchNo}]鏈壘鍒板彲鍒嗛厤搴撳瓨");
@@ -636,24 +638,25 @@
                 if (userSelection == null) continue;
 
                 // 璁$畻璇ユ墭鐩樺疄闄呭彲鐢ㄦ暟閲�
-                var availableQuantity = CalculateAvailableQuantity(stock, outboundOrderDetail.MaterielCode,
-                    outboundOrderDetail.BatchNo, outboundOrderDetail.SupplyCode);
+                var availableQuantity = CalculateAvailableQuantityByBarcode(stock, outboundOrderDetail.MaterielCode,
+           outboundOrderDetail.BatchNo, outboundOrderDetail.SupplyCode, userSelection.Barcode);
 
                 // 纭畾鍒嗛厤鏁伴噺锛氬彇鐢ㄦ埛閫夋嫨鏁伴噺銆佸彲鐢ㄦ暟閲忓拰鍓╀綑闇�姹傜殑鏈�灏忓��
-                var assignQuantity = Math.Min(Math.Min(userSelection.UseableQuantity, availableQuantity),remainingNeedQuantity);
+                var assignQuantity = Math.Min(Math.Min(userSelection.UseableQuantity, availableQuantity), remainingNeedQuantity);
 
                 if (assignQuantity <= 0) continue;
 
-                // 鎵ц鍒嗛厤
-                var (actualAssigned, barcode) = AssignStockQuantity(stock, outboundOrderDetail, assignQuantity);
+                // 鎵ц鍒嗛厤锛屼娇鐢ㄧ敤鎴烽�夋嫨鐨勬潯鐮�
+                var actualAssigned = AssignStockQuantity(stock, outboundOrderDetail, assignQuantity, userSelection.Barcode);
+
                 if (actualAssigned > 0)
                 {
                     outStocks.Add(stock);
                     totalAssignedFromUserSelection += actualAssigned;
                     remainingNeedQuantity -= actualAssigned;
 
-                    // 鍒涘缓閿佸畾璁板綍
-                    var lockInfo = CreateOutStockLockInfo(outboundOrder, outboundOrderDetail, stock, actualAssigned, barcode);
+                    // 鍒涘缓閿佸畾璁板綍锛屼娇鐢ㄧ敤鎴烽�夋嫨鐨勬潯鐮�
+                    var lockInfo = CreateOutStockLockInfo(outboundOrder, outboundOrderDetail, stock, actualAssigned, userSelection.Barcode);
                     outStockLockInfos.Add(lockInfo);
                 }
             }
@@ -668,47 +671,7 @@
           
             if (remainingNeedQuantity > 0)
             {
-                //// 鍙�夛細杩欓噷鍙互鏍规嵁涓氬姟闇�姹傚喅瀹氭槸鍚﹀厑璁歌嚜鍔ㄥ垎閰�
-                //// 濡傛灉瑕佹眰涓ユ牸鎸夌敤鎴烽�夋嫨鍑哄簱锛屽彲浠ユ姏鍑哄紓甯�
-                //// throw new Exception($"鐢ㄦ埛閫夋嫨鐨勫簱瀛樻暟閲忎笉瓒筹紝杩橀渶{remainingNeedQuantity}锛岃閲嶆柊閫夋嫨");
-
-                //// 濡傛灉闇�瑕佽嚜鍔ㄥ垎閰嶏紝璁板綍鏃ュ織
-                //_logger.LogInformation($"鐢ㄦ埛閫夋嫨鐨勫簱瀛樻暟閲忎笉瓒筹紝杩橀渶{remainingNeedQuantity}锛岀郴缁熷皢鑷姩鍒嗛厤");
-
-                //List<Dt_StockInfo> autoStocks = _stockService.StockInfoService.GetUseableStocks(
-                //    outboundOrderDetail.MaterielCode,
-                //    outboundOrderDetail.BatchNo,
-                //    "");
-
-                //// 鎺掗櫎鐢ㄦ埛宸查�夋嫨涓斿凡鍒嗛厤鐨勬墭鐩�
-                //var assignedPalletCodes = outStocks.Select(x => x.PalletCode).ToList();
-                //autoStocks = autoStocks
-                //    .Where(x => !assignedPalletCodes.Contains(x.PalletCode))
-                //    .ToList();
-
-                //var (autoAssignStocks, stockAllocations) = _stockService.StockInfoService.GetOutboundStocks(
-                //    autoStocks,
-                //    outboundOrderDetail.MaterielCode,
-                //    remainingNeedQuantity,
-                //    out decimal residueQuantity);
-
-                //if (autoAssignStocks != null && autoAssignStocks.Any())
-                //{
-                //    outStocks.AddRange(autoAssignStocks);
-
-                //    // 涓鸿嚜鍔ㄥ垎閰嶇殑搴撳瓨鍒涘缓閿佸畾璁板綍
-                //    var autoLockInfos = CreateLockInfosForAutoAssign(outboundOrder, outboundOrderDetail, autoAssignStocks, stockAllocations);
-                //    outStockLockInfos.AddRange(autoLockInfos);
-
-                //    // 鏇存柊宸插垎閰嶆暟閲�
-                //    decimal autoAssignedQuantity = remainingNeedQuantity - residueQuantity;
-                //    totalAssignedFromUserSelection += autoAssignedQuantity;
-                //    remainingNeedQuantity = residueQuantity; // 鏇存柊鍓╀綑闇�姹�
-                //}
-                //else if (remainingNeedQuantity > 0)
-                //{
-                //    _logger.LogWarning($"鑷姩鍒嗛厤澶辫触锛屽墿浣欓渶姹倇remainingNeedQuantity}鏃犳硶婊¤冻");
-                //}
+                
             }
 
             // 鏇存柊閿佸畾鏁伴噺
@@ -721,7 +684,25 @@
 
             return (outStocks, outboundOrderDetail, outStockLockInfos, locationInfos);
         }
+        private decimal CalculateAvailableQuantityByBarcode(Dt_StockInfo stock, string materielCode, string batchNo, string supplyCode, string barcode)
+        {
+            var query = stock.Details.AsQueryable()
+                .Where(d => d.MaterielCode == materielCode &&
+                           (d.StockQuantity - d.OutboundQuantity) > 0 &&
+                           d.Barcode == barcode);
 
+            if (!string.IsNullOrEmpty(batchNo))
+            {
+                query = query.Where(x => x.BatchNo == batchNo);
+            }
+
+            if (!string.IsNullOrEmpty(supplyCode))
+            {
+                query = query.Where(d => d.SupplyCode == supplyCode);
+            }
+
+            return query.Sum(d => d.StockQuantity - d.OutboundQuantity);
+        }
         // 杈呭姪鏂规硶
         private decimal CalculateAvailableQuantity(Dt_StockInfo stock, string materielCode, string batchNo, string supplyCode)
         {
@@ -732,6 +713,43 @@
                 .ToList();
 
             return relevantDetails.Sum(d => d.StockQuantity - d.OutboundQuantity);
+        }
+
+        private decimal AssignStockQuantity(Dt_StockInfo stock, Dt_OutboundOrderDetail detail, decimal assignQuantity, string barcode)
+        {
+            decimal remainingAssign = assignQuantity;
+
+            // 鎸夊厛杩涘厛鍑哄垎閰嶆寚瀹氭潯鐮佺殑搴撳瓨鏄庣粏
+            var query = stock.Details.AsQueryable()
+                .Where(d => d.MaterielCode == detail.MaterielCode &&
+                           (d.StockQuantity - d.OutboundQuantity) > 0 &&
+                           d.Barcode == barcode); // 鍙垎閰嶆寚瀹氭潯鐮�
+           
+            query = query.Where(x => x.WarehouseCode == detail.WarehouseCode);
+
+            if (!string.IsNullOrEmpty(detail.BatchNo))
+            {
+                query = query.Where(x => x.BatchNo == detail.BatchNo);
+            }
+            // 濡傛灉鍑哄簱鍗曟湁渚涘簲鍟嗚姹傦紝鎸変緵搴斿晢杩囨护
+            if (!string.IsNullOrEmpty(detail.SupplyCode))
+            {
+                query = query.Where(d => d.SupplyCode == detail.SupplyCode);
+            }
+            var sortedDetails = query.ToList().OrderBy(d => d.CreateDate);
+
+            foreach (var stockDetail in sortedDetails)
+            {
+                if (remainingAssign <= 0) break;
+
+                var available = stockDetail.StockQuantity - stockDetail.OutboundQuantity;
+                var assign = Math.Min(available, remainingAssign);
+
+                stockDetail.OutboundQuantity += assign;
+                remainingAssign -= assign;
+            }
+
+            return assignQuantity - remainingAssign; // 杩斿洖瀹為檯鍒嗛厤鏁伴噺
         }
 
         private (decimal assignedQuantity, string barcode) AssignStockQuantity(Dt_StockInfo stock, Dt_OutboundOrderDetail detail, decimal assignQuantity)
@@ -843,8 +861,8 @@
                     return (false, $"鎵樼洏[{selection.PalletCode}]涓嶅瓨鍦�");
                 }
 
-                var available = CalculateAvailableQuantity(stock, outboundOrderDetail.MaterielCode,
-                    outboundOrderDetail.BatchNo, outboundOrderDetail.SupplyCode);
+                var available = CalculateAvailableQuantityByBarcode(stock, outboundOrderDetail.MaterielCode,
+                    outboundOrderDetail.BatchNo, outboundOrderDetail.SupplyCode,selection.Barcode);
 
                 if (available <= 0)
                 {

--
Gitblit v1.9.3