From 18d2f40bbf40469b033de6f07ca8a8e3bc191132 Mon Sep 17 00:00:00 2001
From: dengjunjie <dengjunjie@hnkhzn.com>
Date: 星期六, 26 四月 2025 19:21:40 +0800
Subject: [PATCH] 优化代码

---
 项目代码/WMS/WIDESEA_WMSServer/WIDESEA_OutboundService/Base/OutboundOrderDetailService.cs |  121 +++++++++++++++++++++++++++++-----------
 1 files changed, 87 insertions(+), 34 deletions(-)

diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_OutboundService/Base/OutboundOrderDetailService.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_OutboundService/Base/OutboundOrderDetailService.cs"
index 2226aea..74ccbd6 100644
--- "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_OutboundService/Base/OutboundOrderDetailService.cs"
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_OutboundService/Base/OutboundOrderDetailService.cs"
@@ -1,5 +1,6 @@
 锘縰sing OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
 using System;
+using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 using System.Reflection;
@@ -66,45 +67,97 @@
             List<string> LocationCodes = new List<string>();
             List<Dt_OutStockLockInfo> outStockLockInfos = new List<Dt_OutStockLockInfo>();
             List<Dt_LocationInfo> locationInfos = new List<Dt_LocationInfo>();
+            List<int> keys = new List<int>();
             foreach (var item in outboundOrderDetails)
             {
-                var StockSelectViews = BaseDal.QueryTabs<Dt_StockInfo, Dt_StockInfoDetail, StockSelectViewDTO>((a, b) => a.Id == b.StockId, (a, b) => new StockSelectViewDTO
-                {
-                    LocationCode = a.LocationCode,
-                    MaterielCode = b.MaterielCode,
-                    MaterielName = b.MaterielName,
-                    PalletCode = a.PalletCode,
-                    UseableQuantity = b.StockQuantity,
-                    BatchNo = b.BatchNo,
-                    InDate = a.InDate
-                }, a => a.StockStatus == StockStatusEmun.宸插叆搴�.ObjToInt(), b => b.Status == StockStatusEmun.宸插叆搴�.ObjToInt() && b.MaterielCode == item.MaterielCode, x => true).GroupBy(x => x.LocationCode).Select(x => new StockSelectViewDTO
-                {
-                    LocationCode = x.FirstOrDefault()?.LocationCode ?? "",
-                    MaterielCode = x.FirstOrDefault()?.MaterielCode ?? "",
-                    MaterielName = x.FirstOrDefault()?.MaterielName ?? "",
-                    //PalletCode = string.Join(",", x.Select(x => x.PalletCode).ToList()),
-                    PalletCode = x.Select(x => x.PalletCode).ToList().Aggregate((i, j) => i + "," + j),
-                    UseableQuantity = x.Sum(x => x.UseableQuantity),
-                    BatchNo = x.FirstOrDefault()?.BatchNo ?? "",
-                    InDate = x.FirstOrDefault()?.InDate
-                }).ToList();
+                var StockSelectViews = _stockService.StockInfoService.GetStockSelectViews(item.MaterielCode);
                 if (StockSelectViews.Count < 1) throw new Exception($"鏈壘鍒板彲鍒嗛厤搴撳瓨");
-                StockSelectViewDTO? stockSelectView = StockSelectViews.Where(x => x.UseableQuantity == item.OrderQuantity)?.OrderBy(x => x.InDate).First();
-                if (stockSelectView == null)
+                StockSelectViews = StockSelectViews.Where(x => !keys.Contains(x.Id)).OrderBy(x => x.InDate).ToList();
+                if (StockSelectViews.Count < 1) throw new Exception($"鍙垎閰嶅簱瀛樹笉瓒�");
+                foreach (var StockSelectView in StockSelectViews.GroupBy(x => x.LocationCode))
                 {
-                    throw new Exception($"鏈壘鍒板彲鍒嗛厤搴撳瓨");
+                    if (item.OrderQuantity - item.LockQuantity <= 0) break;
+                    decimal StockQuantity = StockSelectView.Sum(x => x.UseableQuantity);
+                    if (StockQuantity <= item.OrderQuantity - item.LockQuantity)
+                    {
+                        foreach (var Stock in StockSelectView.OrderByDescending(x => x.InDate))
+                        {
+                            keys.Add(Stock.Id);
+                            item.LockQuantity += Stock.UseableQuantity;
+                            item.OrderDetailStatus = OrderDetailStatusEnum.Outbound.ObjToInt();
+                            List<Dt_StockInfo> dt_StockInfos = _stockService.StockInfoService.Repository.QueryData(x => Stock.PalletCode.Contains(x.PalletCode));
+                            dt_StockInfos.ForEach(x =>
+                            {
+                                x.StockStatus = StockStatusEmun.鍑哄簱涓�.ObjToInt();
+                            });
+                            outboundOrder.OrderStatus = OutboundStatusEnum.鍑哄簱涓�.ObjToInt();
+                            outStockLockInfos.AddRange(_outStockLockInfoService.GetOutStockLockInfo(outboundOrder, item, dt_StockInfos, BatchNo: Stock.BatchNo));
+                            outStocks.AddRange(dt_StockInfos);
+                        }
+                        LocationCodes.Add(StockSelectView.Key);
+                    }
+                    else
+                    {
+                        foreach (var Stock in StockSelectView.OrderByDescending(x => x.InDate))
+                        {
+                            if (item.OrderQuantity - item.LockQuantity - Stock.UseableQuantity >= 0)
+                            {
+                                keys.Add(Stock.Id);
+                                item.LockQuantity += Stock.UseableQuantity;
+                                item.OrderDetailStatus = OrderDetailStatusEnum.Outbound.ObjToInt();
+                                List<Dt_StockInfo> StockInfos = _stockService.StockInfoService.Repository.QueryData(x => Stock.PalletCode.Contains(x.PalletCode));
+                                StockInfos.ForEach(x =>
+                                {
+                                    x.StockStatus = StockStatusEmun.鍑哄簱涓�.ObjToInt();
+                                });
+                                outboundOrder.OrderStatus = OutboundStatusEnum.鍑哄簱涓�.ObjToInt();
+                                outStockLockInfos.AddRange(_outStockLockInfoService.GetOutStockLockInfo(outboundOrder, item, StockInfos, BatchNo: Stock.BatchNo));
+                                outStocks.AddRange(StockInfos);
+                                LocationCodes.Add(Stock.LocationCode);
+                            }
+                        }
+                    }
                 }
-                item.LockQuantity = stockSelectView.UseableQuantity;
-                item.OrderDetailStatus = OrderDetailStatusEnum.Outbound.ObjToInt();
-                List<Dt_StockInfo> dt_StockInfos = _stockService.StockInfoService.Repository.QueryData(x => x.LocationCode == stockSelectView.LocationCode);
-                dt_StockInfos.ForEach(x =>
-                {
-                    x.StockStatus = StockStatusEmun.鍑哄簱涓�.ObjToInt();
-                });
-                outboundOrder.OrderStatus = OutboundStatusEnum.鍑哄簱涓�.ObjToInt();
-                outStockLockInfos.AddRange(_outStockLockInfoService.GetOutStockLockInfo(outboundOrder, item, dt_StockInfos, BatchNo: stockSelectView.BatchNo));
-                outStocks.AddRange(dt_StockInfos);
-                LocationCodes.Add(stockSelectView.LocationCode);
+                #region MyRegion
+                //StockSelectViewDTO? stockSelectView = StockSelectViews.Where(x => x.UseableQuantity == item.OrderQuantity).FirstOrDefault();
+                //if (stockSelectView == null)
+                //{
+                //    foreach (var StockSelectView in StockSelectViews)
+                //    {
+                //        if (item.OrderQuantity - item.LockQuantity - StockSelectView.UseableQuantity >= 0)
+                //        {
+                //            keys.Add(StockSelectView.Id);
+                //            item.LockQuantity += StockSelectView.UseableQuantity;
+                //            item.OrderDetailStatus = OrderDetailStatusEnum.Outbound.ObjToInt();
+                //            List<Dt_StockInfo> StockInfos = _stockService.StockInfoService.Repository.QueryData(x => StockSelectView.PalletCode.Contains(x.PalletCode));
+                //            StockInfos.ForEach(x =>
+                //            {
+                //                x.StockStatus = StockStatusEmun.鍑哄簱涓�.ObjToInt();
+                //            });
+                //            outboundOrder.OrderStatus = OutboundStatusEnum.鍑哄簱涓�.ObjToInt();
+                //            outStockLockInfos.AddRange(_outStockLockInfoService.GetOutStockLockInfo(outboundOrder, item, StockInfos, BatchNo: StockSelectView.BatchNo));
+                //            outStocks.AddRange(StockInfos);
+                //            LocationCodes.Add(StockSelectView.LocationCode);
+                //        }
+                //    }
+                //    if (keys.Count < 1) throw new Exception($"鏈壘鍒板尮閰嶅嚭搴撳崟鏁伴噺搴撳瓨");
+                //}
+                //else
+                //{
+                //    keys.Add(stockSelectView.Id);
+                //    item.LockQuantity = stockSelectView.UseableQuantity;
+                //    item.OrderDetailStatus = OrderDetailStatusEnum.Outbound.ObjToInt();
+                //    List<Dt_StockInfo> dt_StockInfos = _stockService.StockInfoService.Repository.QueryData(x => stockSelectView.PalletCode.Contains(x.PalletCode));
+                //    dt_StockInfos.ForEach(x =>
+                //    {
+                //        x.StockStatus = StockStatusEmun.鍑哄簱涓�.ObjToInt();
+                //    });
+                //    outboundOrder.OrderStatus = OutboundStatusEnum.鍑哄簱涓�.ObjToInt();
+                //    outStockLockInfos.AddRange(_outStockLockInfoService.GetOutStockLockInfo(outboundOrder, item, dt_StockInfos, BatchNo: stockSelectView.BatchNo));
+                //    outStocks.AddRange(dt_StockInfos);
+                //    LocationCodes.Add(stockSelectView.LocationCode);
+                //} 
+                #endregion
             }
             locationInfos.AddRange(_basicService.LocationInfoService.Repository.GetLocationInfos(LocationCodes));
             locationInfos.ForEach(x =>

--
Gitblit v1.9.3