From c84db706e8c8d82a96bb4b4c18c243a42b2976c1 Mon Sep 17 00:00:00 2001
From: pan <antony1029@163.com>
Date: 星期三, 19 十一月 2025 19:44:30 +0800
Subject: [PATCH] 提交

---
 项目代码/WMS无仓储版/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundOrderDetailService.cs |  222 +++++++++++++++++++++----------------------------------
 1 files changed, 85 insertions(+), 137 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 fdf2e9c..3fa61d7 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"
@@ -1,8 +1,12 @@
-锘縰sing WIDESEA_Common.LocationEnum;
+锘縰sing Microsoft.Extensions.Logging;
+using SqlSugar;
+using WIDESEA_Common.LocationEnum;
 using WIDESEA_Common.StockEnum;
 using WIDESEA_Core;
 using WIDESEA_Core.BaseRepository;
 using WIDESEA_Core.BaseServices;
+using WIDESEA_Core.Helper;
+using WIDESEA_DTO.Stock;
 using WIDESEA_IBasicService;
 using WIDESEA_IOutboundService;
 using WIDESEA_IRecordService;
@@ -24,8 +28,8 @@
         private readonly IRecordService _recordService;
         private readonly IOutboundOrderService _outboundOrderService;
         private readonly ILocationStatusChangeRecordService _locationStatusChangeRecordService;
-
-        public OutboundOrderDetailService(IRepository<Dt_OutboundOrderDetail> BaseDal, IUnitOfWorkManage unitOfWorkManage, IStockService stockService, IOutStockLockInfoService outStockLockInfoService, IBasicService basicService, IRecordService recordService, ILocationInfoService locationInfoService, ILocationStatusChangeRecordService locationStatusChangeRecordService, IOutboundOrderService outboundOrderService) : base(BaseDal)
+        private readonly ILogger<OutboundOrderDetailService> _logger;
+        public OutboundOrderDetailService(IRepository<Dt_OutboundOrderDetail> BaseDal, IUnitOfWorkManage unitOfWorkManage, IStockService stockService, IOutStockLockInfoService outStockLockInfoService, IBasicService basicService, IRecordService recordService, ILocationInfoService locationInfoService, ILocationStatusChangeRecordService locationStatusChangeRecordService, IOutboundOrderService outboundOrderService, ILogger<OutboundOrderDetailService> logger) : base(BaseDal)
         {
             _unitOfWorkManage = unitOfWorkManage;
             _stockService = stockService;
@@ -35,6 +39,7 @@
             _locationInfoService = locationInfoService;
             _locationStatusChangeRecordService = locationStatusChangeRecordService;
             _outboundOrderService = outboundOrderService;
+            _logger = logger;
         }
 
 
@@ -62,13 +67,14 @@
 
             // 鎸夌墿鏂欏拰鎵规鍒嗙粍澶勭悊
             var groupDetails = outboundOrderDetails
-                .GroupBy(x => new { x.MaterielCode, x.BatchNo })
+                .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 = x.Sum(v => v.OrderQuantity - v.OverOutQuantity - v.LockQuantity)
+                    TotalNeedQuantity = x.Sum(v => v.OrderQuantity - v.OverOutQuantity - v.LockQuantity-v.MoveQty)
                 })
                 .Where(x => x.TotalNeedQuantity > 0)
                 .ToList();
@@ -78,17 +84,17 @@
                 var needQuantity = item.TotalNeedQuantity;
 
                 // 鑾峰彇鍙敤搴撳瓨锛堟寜鍏堣繘鍏堝嚭鎺掑簭锛�
-                List<Dt_StockInfo> stockInfos = _stockService.StockInfoService.GetUseableStocks(item.MaterielCode, item.BatchNo);
+                List<Dt_StockInfo> stockInfos = _stockService.StockInfoService.GetUseableStocks(item.MaterielCode, item.BatchNo,item.SupplyCode);
                 if (!stockInfos.Any())
                 {
                     throw new Exception($"鐗╂枡[{item.MaterielCode}]鎵规[{item.BatchNo}]鏈壘鍒板彲鍒嗛厤搴撳瓨");
                 }
 
                 // 鍒嗛厤搴撳瓨锛堟寜鍏堣繘鍏堝嚭锛�
-                List<Dt_StockInfo> autoAssignStocks = _stockService.StockInfoService.GetOutboundStocks(
+                var (autoAssignStocks, stockAllocations) = _stockService.StockInfoService.GetOutboundStocks(
                     stockInfos, item.MaterielCode, needQuantity, out decimal residueQuantity);
 
-                if (residueQuantity > 0)
+                if (residueQuantity > 0 && residueQuantity == needQuantity)
                 {
                     throw new Exception($"鐗╂枡[{item.MaterielCode}]搴撳瓨涓嶈冻锛岄渶瑕亄needQuantity}锛屽彲鐢▄needQuantity - residueQuantity}");
                 }
@@ -96,7 +102,7 @@
                 outStocks.AddRange(autoAssignStocks);
 
                 // 鎸夊厛杩涘厛鍑哄師鍒欏垎閰嶉攣瀹氭暟閲忓埌鍚勪釜鏄庣粏
-                DistributeLockQuantityByFIFO(item.Details, autoAssignStocks, outStockLockInfos, outboundOrder);
+                DistributeLockQuantityByFIFO(item.Details, autoAssignStocks, stockAllocations, outStockLockInfos, outboundOrder);
             }
 
             locationInfos.AddRange(_locationInfoService.GetLocationInfos(outStocks.Select(x => x.LocationCode).Distinct().ToList()));
@@ -105,52 +111,59 @@
         }
 
         /// <summary>
-        /// 鎸夊厛杩涘厛鍑哄師鍒欏垎閰嶉攣瀹氭暟閲忓埌鍚勪釜鏄庣粏
+        /// 鎸夊厛杩涘厛鍑哄師鍒欏垎閰嶉攣瀹氭暟閲忓埌鍚勪釜鏄庣粏  
         /// </summary>
         private void DistributeLockQuantityByFIFO(
             List<Dt_OutboundOrderDetail> details,
             List<Dt_StockInfo> assignStocks,
+            Dictionary<int, decimal> stockAllocations,
             List<Dt_OutStockLockInfo> outStockLockInfos,
             Dt_OutboundOrder outboundOrder)
         {
             // 鎸夊厛杩涘厛鍑烘帓搴忓嚭搴撳崟鏄庣粏锛堝亣璁惧厛鍒涘缓鐨勬槑缁嗛渶瑕佷紭鍏堟弧瓒筹級
             var sortedDetails = details
-                .OrderBy(x => x.Id) // 鎸塈D鎺掑簭锛屽亣璁惧厛鍒涘缓鐨処D灏�
+                .Where(d => d.OrderQuantity - d.OverOutQuantity - d.LockQuantity -d.MoveQty> 0) // 鍙鐞嗚繕闇�瑕佸垎閰嶇殑鏁伴噺
+                .OrderBy(x => x.Id)
                 .ToList();
 
-            // 鎸夊厛杩涘厛鍑烘帓搴忓簱瀛橈紙鐢熶骇鏃ユ湡鏈�鏃╃殑浼樺厛锛�
-            var sortedStockDetails = assignStocks
+            if (!sortedDetails.Any()) return;
+
+            // 鑾峰彇鎵�鏈夊垎閰嶄簡搴撳瓨鐨勬槑缁嗭紝鎸夊厛杩涘厛鍑烘帓搴�
+            var allocatedStockDetails = assignStocks
                 .SelectMany(x => x.Details)
-                .Where(x => details.Any(d => d.MaterielCode == x.MaterielCode))
-                .OrderBy(x => x.ProductionDate)
+                .Where(x => stockAllocations.ContainsKey(x.Id))
+                .OrderBy(x => x.CreateDate)
+                .ThenBy(x => x.StockId)
                 .ToList();
 
             // 涓烘瘡涓簱瀛樻槑缁嗗垱寤哄垎閰嶈褰�
-            foreach (var stockDetail in sortedStockDetails)
+            foreach (var stockDetail in allocatedStockDetails)
             {
-                var stockInfo = assignStocks.First(x => x.Id == stockDetail.StockId);
-                var allocatedQuantity = stockDetail.OutboundQuantity; // 杩欎釜搴撳瓨鏄庣粏鍒嗛厤鐨勬暟閲�
+                if (!stockAllocations.TryGetValue(stockDetail.Id, out decimal allocatedQuantity))
+                    continue;
 
                 if (allocatedQuantity <= 0) continue;
 
-                // 鎸夐『搴忓垎閰嶇粰鍚勪釜鍑哄簱鍗曟槑缁�
+                var stockInfo = assignStocks.First(x => x.Id == stockDetail.StockId);
                 decimal remainingAllocate = allocatedQuantity;
 
+                // 鎸夐『搴忓垎閰嶇粰鍚勪釜鍑哄簱鍗曟槑缁�
                 foreach (var detail in sortedDetails)
                 {
                     if (remainingAllocate <= 0) break;
 
                     // 璁$畻杩欎釜鏄庣粏杩橀渶瑕佸垎閰嶇殑鏁伴噺
-                    var alreadyAssigned = outStockLockInfos
-                        .Where(x => x.OrderDetailId == detail.Id && x.StockId == stockInfo.Id)
-                        .Sum(x => x.AssignQuantity);
-
-                    var detailNeed = detail.OrderQuantity - detail.OverOutQuantity - detail.LockQuantity - alreadyAssigned;
-
+                    var detailNeed = detail.OrderQuantity - detail.OverOutQuantity - detail.LockQuantity;
                     if (detailNeed <= 0) continue;
 
                     // 鍒嗛厤鏁伴噺
                     var assignQuantity = Math.Min(remainingAllocate, detailNeed);
+
+                    // 楠岃瘉鏉$爜鏄惁瀛樺湪
+                    if (string.IsNullOrEmpty(stockDetail.Barcode))
+                    {
+                        throw new Exception($"搴撳瓨鏄庣粏ID[{stockDetail.Id}]鐨勬潯鐮佷负绌�");
+                    }
 
                     // 鍒涘缓鍑哄簱閿佸畾淇℃伅
                     var lockInfo = _outStockLockInfoService.GetOutStockLockInfo(
@@ -162,131 +175,36 @@
                     remainingAllocate -= assignQuantity;
                 }
 
-                // 濡傛灉杩樻湁鍓╀綑鍒嗛厤鏁伴噺锛岃鏄庨�昏緫鏈夎
+                // 濡傛灉杩樻湁鍓╀綑鍒嗛厤鏁伴噺锛岄噸鏂板垎閰嶆垨璁板綍璀﹀憡
                 if (remainingAllocate > 0)
                 {
-                    throw new Exception($"搴撳瓨鍒嗛厤閫昏緫閿欒锛屽墿浣欐湭鍒嗛厤鏁伴噺锛歿remainingAllocate}");
-                }
-            }
-        }
-
- 
-/// <summary>
-    /// 鍒涘缓鍑哄簱閿佸畾淇℃伅
-    /// </summary>
-    private void CreateOutStockLockInfos(Dt_OutboundOrder outboundOrder,List<Dt_OutboundOrderDetail> details,
-        List<Dt_StockInfo> assignStocks,List<Dt_OutStockLockInfo> outStockLockInfos)
-        {
-            foreach (var stock in assignStocks)
-            {
-                // 鑾峰彇璇ュ簱瀛樹腑鐩稿叧鐗╂枡鐨勫彲鐢ㄦ潯鐮佷俊鎭�
-                var stockDetails = stock.Details
-                    .Where(x => details.Any(d => d.MaterielCode == x.MaterielCode) &&
-                               x.StockQuantity > x.OutboundQuantity)
-                    .OrderBy(x => x.ProductionDate) // 鍏堣繘鍏堝嚭
-                    .ToList();
-                if (!stockDetails.Any()) continue;
-
-                var stockAssignQuantity = stockDetails.Sum(x => x.OutboundQuantity);
-
-
-                // 鏌ユ壘杩欎釜搴撳瓨宸茬粡鍒嗛厤鐨勬暟閲�
-                var existingAssign = outStockLockInfos
-                    .Where(x => x.StockId == stock.Id &&
-                               details.Any(d => d.Id == x.OrderDetailId))
-                    .Sum(x => x.AssignQuantity);
-
-                var availableAssign = stockAssignQuantity - existingAssign;
-
-                if (availableAssign <= 0) continue;
-
-                // 鎸夊厛杩涘厛鍑哄師鍒欏垎閰嶆潯鐮�
-                var barcodeAllocation = AllocateBarcodes(stockDetails, availableAssign);
-
-
-                // 鍒嗛厤缁欏悇涓槑缁�
-                foreach (var detail in details.Where(d => d.LockQuantity > 0))
-                {
-                    var alreadyAssigned = outStockLockInfos
-                        .Where(x => x.OrderDetailId == detail.Id && x.StockId == stock.Id)
-                        .Sum(x => x.AssignQuantity);
-
-                    var canAssign = Math.Min(detail.LockQuantity - alreadyAssigned, availableAssign);
-
-                    if (canAssign > 0)
+                    // 閲嶆柊鍒嗛厤缁欏叾浠栭渶瑕佸垎閰嶇殑鏄庣粏
+                    foreach (var detail in sortedDetails)
                     {
-                        // 涓鸿繖涓垎閰嶇‘瀹氭潯鐮�
-                        var (barcode, barcodeQuantity) = GetBarcodeForAllocation(barcodeAllocation, canAssign);
+                        if (remainingAllocate <= 0) break;
+
+                        var detailNeed = detail.OrderQuantity - detail.OverOutQuantity - detail.LockQuantity;
+                        if (detailNeed <= 0) continue;
+
+                        var assignQuantity = Math.Min(remainingAllocate, detailNeed);
 
                         var lockInfo = _outStockLockInfoService.GetOutStockLockInfo(
-                            outboundOrder, detail, stock, canAssign, barcode,null);
+                            outboundOrder, detail, stockInfo, assignQuantity, stockDetail.Barcode);
                         outStockLockInfos.Add(lockInfo);
 
-                        availableAssign -= canAssign;
-
-                        // 鏇存柊鏉$爜鍒嗛厤璁板綍
-                        UpdateBarcodeAllocation(barcodeAllocation, barcode, barcodeQuantity);
+                        detail.LockQuantity += assignQuantity;
+                        remainingAllocate -= assignQuantity;
                     }
 
-                    if (availableAssign <= 0) break;
+                    // 濡傛灉杩樻湁鍓╀綑锛岃褰曡鍛婁絾涓嶆姏鍑哄紓甯�
+                    if (remainingAllocate > 0)
+                    {
+                        _logger.LogWarning($"搴撳瓨鍒嗛厤鍚庝粛鏈夊墿浣欐暟閲忔湭鍒嗛厤: {remainingAllocate}, 鏉$爜: {stockDetail.Barcode}");
+                    }
                 }
             }
         }
 
-        /// <summary>
-        /// 鎸夊厛杩涘厛鍑哄師鍒欏垎閰嶆潯鐮�
-        /// </summary>
-        private Dictionary<string, decimal> AllocateBarcodes(List<Dt_StockInfoDetail> stockDetails, decimal totalQuantity)
-        {
-            var allocation = new Dictionary<string, decimal>();
-            decimal remainingQuantity = totalQuantity;
-
-            foreach (var detail in stockDetails.OrderBy(x => x.ProductionDate))
-            {
-                if (remainingQuantity <= 0) break;
-
-                decimal available = detail.StockQuantity - detail.OutboundQuantity;
-                decimal allocate = Math.Min(available, remainingQuantity);
-
-                allocation[detail.Barcode] = allocate;
-                remainingQuantity -= allocate;
-            }
-
-            return allocation;
-        }
-
-        /// <summary>
-        /// 涓哄垎閰嶈幏鍙栧悎閫傜殑鏉$爜
-        /// </summary>
-        private (string barcode, decimal quantity) GetBarcodeForAllocation(Dictionary<string, decimal> barcodeAllocation, decimal requiredQuantity)
-        {
-            foreach (var (barcode, quantity) in barcodeAllocation)
-            {
-                if (quantity >= requiredQuantity)
-                {
-                    return (barcode, requiredQuantity);
-                }
-            }
-
-            // 濡傛灉鍗曚釜鏉$爜鏁伴噺涓嶈冻锛屼娇鐢ㄧ涓�涓潯鐮�
-            var first = barcodeAllocation.First();
-            return (first.Key, Math.Min(first.Value, requiredQuantity));
-        }
-
-        /// <summary>
-        /// 鏇存柊鏉$爜鍒嗛厤璁板綍
-        /// </summary>
-        private void UpdateBarcodeAllocation(Dictionary<string, decimal> barcodeAllocation, string barcode, decimal usedQuantity)
-        {
-            if (barcodeAllocation.ContainsKey(barcode))
-            {
-                barcodeAllocation[barcode] -= usedQuantity;
-                if (barcodeAllocation[barcode] <= 0)
-                {
-                    barcodeAllocation.Remove(barcode);
-                }
-            }
-        }
         /// <summary>
         /// 鍑哄簱搴撳瓨鍒嗛厤鍚庯紝鏇存柊鏁版嵁搴撴暟鎹�
         /// </summary>
@@ -341,5 +259,35 @@
             }
         }
 
+
+
+        public override PageGridData<Dt_OutboundOrderDetail> GetPageData(PageDataOptions options)
+        {
+            //var  pageGridData = base.GetPageData(options);
+
+            ISugarQueryable<Dt_OutboundOrderDetail> sugarQueryable1 = BaseDal.Db.Queryable<Dt_OutboundOrderDetail>();
+            if (!string.IsNullOrEmpty(options.Wheres))
+            {
+
+                List<SearchParameters> searchParametersList = options.Wheres.DeserializeObject<List<SearchParameters>>();
+                int totalCount = 0;
+                if (searchParametersList.Count > 0)
+                {
+                    {
+                        SearchParameters? searchParameters = searchParametersList.FirstOrDefault(x => x.Name == nameof(Dt_InboundOrderDetail.OrderId).FirstLetterToLower());
+                        if (searchParameters != null)
+                        {
+                            sugarQueryable1 = sugarQueryable1.Where(x => x.OrderId== searchParameters.Value.ObjToInt());
+                            var  dataList = sugarQueryable1.ToPageList(options.Page, options.Rows, ref totalCount);
+                            return new PageGridData<Dt_OutboundOrderDetail>(totalCount, dataList);
+                        }
+                    }
+
+                   
+                }
+            }
+            return new PageGridData<Dt_OutboundOrderDetail> ();
+        }
+
     }
 }

--
Gitblit v1.9.3