From fbddf8270dffec0192fa40c8b78a714e72f64c6f Mon Sep 17 00:00:00 2001
From: tjx <t2856754968@163.com>
Date: 星期三, 10 十二月 2025 18:44:35 +0800
Subject: [PATCH] 11

---
 StandardPda/MES.Service/service/Warehouse/MesInvItemStocksManager.cs |  391 ++++++++++++++++++++++++-------------------------------
 1 files changed, 169 insertions(+), 222 deletions(-)

diff --git a/StandardPda/MES.Service/service/Warehouse/MesInvItemStocksManager.cs b/StandardPda/MES.Service/service/Warehouse/MesInvItemStocksManager.cs
index 634aaab..10d370f 100644
--- a/StandardPda/MES.Service/service/Warehouse/MesInvItemStocksManager.cs
+++ b/StandardPda/MES.Service/service/Warehouse/MesInvItemStocksManager.cs
@@ -28,21 +28,21 @@
             throw new Exception("姣忛〉鏉℃暟蹇呴』涓�10銆�20鎴�50");
         }
 
-        // 1. 浼樺寲锛氫娇鐢ㄥ師鐢烻QL鑾峰彇姣忎釜鏉$爜鐨勬渶鏂拌褰曪紙閬垮厤鍏ㄨ〃鍔犺浇鍒板唴瀛橈級
-        // 浣跨敤绐楀彛鍑芥暟ROW_NUMBER鍦ㄦ暟鎹簱灞傞潰瀹屾垚鍒嗙粍鍙栨渶鏂拌褰�
-        var sql = @"
-            SELECT ITEM_BARCODE, PalletCode, Code, Id
-            FROM (SELECT ITEM_BARCODE,
-                         PalletCode,
-                         Code,
-                         Id,
-                         ROW_NUMBER() OVER (PARTITION BY ITEM_BARCODE ORDER BY Id DESC) as rn
-                  FROM XB_RACKING_TASK_SYXT_LOG
-                  WHERE ITEM_BARCODE IS NOT NULL
-                    AND Code != '500') t
-            WHERE rn = 1";
-        
-        var rackingTaskData = Db.Ado.SqlQuery<RackingTaskInfo>(sql);
+        // 1. 鏌ヨXB_RACKING_TASK_SYXT_LOG涓璉temBarcode鍜孭alletCode鐨勬槧灏勫叧绯�
+        var allRackingTaskData = Db.Queryable<XbRackingTaskSyxtLog>()
+            .Where(x => !string.IsNullOrEmpty(x.ItemBarcode))
+            .Select(x => new { x.ItemBarcode, x.PalletCode, x.Code, x.Id })
+            .ToList();
+
+        // ReturnableStockDto澧炲姞涓�涓簱瀛樼姸鎬佺殑瀛楁
+        // 褰揳llRackingTaskData涓殑Code涓簄ull鏃讹紝搴撳瓨鐘舵�佷负绔嬪簱鍏ュ簱涓�
+        // 褰揳llRackingTaskData涓殑Code涓�200鏃讹紝搴撳瓨鐘舵�佷负宸茬粡鍦ㄧ珛搴撳唴
+        // 瀵规瘡涓潯鐮侊紝鍙栨渶鏂癐D鐨勮褰曪紝骞惰繃婊ゆ帀Code涓�500锛堣〃绀哄け璐ワ級鐨勮褰�
+        var rackingTaskData = allRackingTaskData
+            .GroupBy(x => x.ItemBarcode) // 鎸夋潯鐮佸垎缁�
+            .Select(g => g.OrderByDescending(x => x.Id).First()) // 鍙栨瘡涓潯鐮佹渶鏂扮殑涓�鏉¤褰�
+            .Where(x => x.Code != "500") // 杩囨护鎺塁ode涓�500鐨勫け璐ヨ褰�
+            .ToList();
 
         if (rackingTaskData == null || !rackingTaskData.Any())
         {
@@ -63,182 +63,162 @@
         var distinctBarcodes = rackingTaskData.Select(x => x.ItemBarcode)
             .Distinct().ToList();
 
-        // 1.2 鎻愬彇鎼滅储鏉′欢锛堟彁鍗囦綔鐢ㄥ煙锛�
+        // 2. 鏋勫缓鏌ヨ鏉′欢
+        var query = Db.Queryable<MesInvItemStocks>()
+            .LeftJoin<MesItems>((stock, item) => stock.ItemId == item.Id)
+            .LeftJoin<MesDepots>((stock, item, depot) =>
+                stock.DepotsCode == depot.DepotCode)
+            .LeftJoin<Organize>((stock, item, depot, org) =>
+                item.UseOrg == org.Id.ToString())
+            .LeftJoin<MesUnit>((stock, item, depot, org, unit) =>
+                item.ItemUnit == unit.Id.ToString())
+            .Where((stock, item, depot, org, unit) =>
+                (distinctBarcodes.Contains(stock.ItemBarcode) ||
+                 distinctBarcodes.Contains(stock.StackCode)) &&
+                stock.Quantity > 0);
+
+        // 3. 搴旂敤鎼滅储鏉′欢
         var conditions = searchDto.Conditions;
-
-        // 2. 浼樺寲锛氬垎鎵瑰鐞嗘潯鐮佸垪琛紙閬垮厤IN鏌ヨ杩囧ぇ锛�
-        const int batchSize = 500; // 姣忔壒澶勭悊500鏉�
-        var allQueryResults = new List<StockQueryResult>();
-        
-        for (int i = 0; i < distinctBarcodes.Count; i += batchSize)
+        if (conditions != null)
         {
-            var batchBarcodes = distinctBarcodes.Skip(i).Take(batchSize).ToList();
-            
-            // 2.1 鏋勫缓鏌ヨ鏉′欢
-            var query = Db.Queryable<MesInvItemStocks>()
-                .LeftJoin<MesItems>((stock, item) => stock.ItemId == item.Id)
-                .LeftJoin<MesDepots>((stock, item, depot) =>
-                    stock.DepotsCode == depot.DepotCode)
-                .LeftJoin<Organize>((stock, item, depot, org) =>
-                    item.UseOrg == org.Id.ToString())
-                .LeftJoin<MesUnit>((stock, item, depot, org, unit) =>
-                    item.ItemUnit == unit.Id.ToString())
-                .Where((stock, item, depot, org, unit) =>
-                    (batchBarcodes.Contains(stock.ItemBarcode) ||
-                     batchBarcodes.Contains(stock.StackCode)) &&
-                    stock.Quantity > 0);
-
-            // 2.2 搴旂敤鎼滅储鏉′欢锛堝湪鏁版嵁搴撳眰闈㈣繃婊わ級
-            if (conditions != null)
+            //褰揷onditions.IqcStatus涓�1鏃舵煡璇㈢殑鍊煎氨瑕佹槸鐗归噰鐩存帴浣跨敤锛屽凡妫�锛屽厤妫�锛�1鐨勫��
+            // 绮剧‘鍖归厤鏉′欢
+            if (!string.IsNullOrEmpty(conditions.IqcStatus))
             {
-                //褰揷onditions.IqcStatus涓�1鏃舵煡璇㈢殑鍊煎氨瑕佹槸鐗归噰鐩存帴浣跨敤锛屽凡妫�锛屽厤妫�锛�1鐨勫��
-                // 绮剧‘鍖归厤鏉′欢
-                if (!string.IsNullOrEmpty(conditions.IqcStatus))
+                if (conditions.IqcStatus == "1")
                 {
-                    if (conditions.IqcStatus == "1")
-                    {
-                        // 褰揑qcStatus涓�"1"鏃讹紝鏌ヨ鐗归噰鐩存帴浣跨敤銆佸凡妫�銆佸厤妫�鐘舵��
-                        query = query.Where((stock, item, depot, org, unit) =>
-                            stock.IqcStatus == "鐗归噰鐩存帴浣跨敤" ||
-                            stock.IqcStatus == "宸叉" ||
-                            stock.IqcStatus == "鍏嶆");
-                    }
-                    else
-                    {
-                        // 鍏朵粬鎯呭喌鎸夊師鍊煎尮閰�
-                        query = query.Where((stock, item, depot, org, unit) =>
-                            stock.IqcStatus == conditions.IqcStatus);
-                    }
-                }
-
-                if (conditions.Quantity.HasValue)
-                {
+                    // 褰揑qcStatus涓�"1"鏃讹紝鏌ヨ鐗归噰鐩存帴浣跨敤銆佸凡妫�銆佸厤妫�鐘舵��
                     query = query.Where((stock, item, depot, org, unit) =>
-                        stock.Quantity == conditions.Quantity.Value);
+                        stock.IqcStatus == "鐗归噰鐩存帴浣跨敤" ||
+                        stock.IqcStatus == "宸叉" ||
+                        stock.IqcStatus == "鍏嶆");
                 }
-
-                // 妯$硦鍖归厤鏉′欢
-
-                if (!string.IsNullOrEmpty(conditions.DepotName))
+                else
                 {
+                    // 鍏朵粬鎯呭喌鎸夊師鍊煎尮閰�
                     query = query.Where((stock, item, depot, org, unit) =>
-                        depot.DepotName != null &&
-                        depot.DepotName.Contains(conditions.DepotName));
-                }
-
-                if (!string.IsNullOrEmpty(conditions.DepotSectionsCode))
-                {
-                    query = query.Where((stock, item, depot, org, unit) =>
-                        stock.DepotSectionsCode != null &&
-                        stock.DepotSectionsCode.Contains(conditions
-                            .DepotSectionsCode));
-                }
-
-                if (!string.IsNullOrEmpty(conditions.ItemNo))
-                {
-                    query = query.Where((stock, item, depot, org, unit) =>
-                        item.ItemNo != null &&
-                        item.ItemNo.Contains(conditions.ItemNo));
-                }
-
-                if (!string.IsNullOrEmpty(conditions.ItemName))
-                {
-                    query = query.Where((stock, item, depot, org, unit) =>
-                        item.ItemName != null &&
-                        item.ItemName.Contains(conditions.ItemName));
-                }
-
-                if (!string.IsNullOrEmpty(conditions.ItemModel))
-                {
-                    query = query.Where((stock, item, depot, org, unit) =>
-                        item.ItemModel != null &&
-                        item.ItemModel.Contains(conditions.ItemModel));
-                }
-
-                if (!string.IsNullOrEmpty(conditions.ItemUnitName))
-                {
-                    query = query.Where((stock, item, depot, org, unit) =>
-                        unit.Fname != null &&
-                        unit.Fname.Contains(conditions.ItemUnitName));
-                }
-
-                if (!string.IsNullOrEmpty(conditions.OrgCode))
-                {
-                    query = query.Where((stock, item, depot, org, unit) =>
-                        org.Fnumber != null &&
-                        org.Fnumber.Contains(conditions.OrgCode));
-                }
-
-                if (!string.IsNullOrEmpty(conditions.OrgName))
-                {
-                    query = query.Where((stock, item, depot, org, unit) =>
-                        org.Fname != null &&
-                        org.Fname.Contains(conditions.OrgName));
-                }
-
-                if (!string.IsNullOrEmpty(conditions.ItemBarcode))
-                {
-                    query = query.Where((stock, item, depot, org, unit) =>
-                        (stock.ItemBarcode != null &&
-                         stock.ItemBarcode.Contains(conditions.ItemBarcode)) ||
-                        (stock.StackCode != null &&
-                         stock.StackCode.Contains(conditions.ItemBarcode)));
-                }
-
-                // 鏃ユ湡鑼冨洿鏉′欢
-                if (!string.IsNullOrEmpty(conditions.IndepDateStart))
-                {
-                    if (DateTime.TryParse(conditions.IndepDateStart,
-                            out var startDate))
-                    {
-                        query = query.Where((stock, item, depot, org, unit) =>
-                            stock.IndepDate >= startDate);
-                    }
-                }
-
-                if (!string.IsNullOrEmpty(conditions.IndepDateEnd))
-                {
-                    if (DateTime.TryParse(conditions.IndepDateEnd, out var endDate))
-                    {
-                        query = query.Where((stock, item, depot, org, unit) =>
-                            stock.IndepDate <= endDate);
-                    }
+                        stock.IqcStatus == conditions.IqcStatus);
                 }
             }
 
-            // 2.3 鎵ц褰撳墠鎵规鏌ヨ
-            var batchQueryResult = query
-                .OrderByDescending((stock, item, depot, org, unit) =>
-                    stock.IndepDate)
-                .Select((stock, item, depot, org, unit) => new StockQueryResult
-                {
-                    IqcStatus = stock.IqcStatus,
-                    DepotCode = stock.DepotsCode,
-                    DepotName = depot.DepotName,
-                    DepotSectionsCode = stock.DepotSectionsCode,
-                    ItemNo = item.ItemNo,
-                    ItemName = item.ItemName,
-                    ItemModel = item.ItemModel,
-                    Quantity = stock.Quantity,
-                    ItemUnit = item.ItemUnit,
-                    ItemUnitName = unit.Fname,
-                    IndepDate = stock.IndepDate,
-                    OrgCode = org.Fnumber,
-                    OrgName = org.Fname,
-                    ItemBarcode = stock.ItemBarcode,
-                    StockStackCode = stock.StackCode
-                })
-                .ToList();
-            
-            allQueryResults.AddRange(batchQueryResult);
-        }
-        
-        // 3. 鍚堝苟鎵�鏈夋壒娆$殑鏌ヨ缁撴灉
-        var queryResult = allQueryResults;
+            if (conditions.Quantity.HasValue)
+            {
+                query = query.Where((stock, item, depot, org, unit) =>
+                    stock.Quantity == conditions.Quantity.Value);
+            }
 
-        // 4. 浼樺寲锛氭瀯寤烘潯鐮佹槧灏勫瓧鍏革紙鎻愰珮鏌ユ壘鏁堢巼锛�
-        var rackingTaskDict = rackingTaskData
-            .ToDictionary(x => x.ItemBarcode, x => x);
+            // 妯$硦鍖归厤鏉′欢
+
+            if (!string.IsNullOrEmpty(conditions.DepotName))
+            {
+                query = query.Where((stock, item, depot, org, unit) =>
+                    depot.DepotName != null &&
+                    depot.DepotName.Contains(conditions.DepotName));
+            }
+
+            if (!string.IsNullOrEmpty(conditions.DepotSectionsCode))
+            {
+                query = query.Where((stock, item, depot, org, unit) =>
+                    stock.DepotSectionsCode != null &&
+                    stock.DepotSectionsCode.Contains(conditions
+                        .DepotSectionsCode));
+            }
+
+            if (!string.IsNullOrEmpty(conditions.ItemNo))
+            {
+                query = query.Where((stock, item, depot, org, unit) =>
+                    item.ItemNo != null &&
+                    item.ItemNo.Contains(conditions.ItemNo));
+            }
+
+            if (!string.IsNullOrEmpty(conditions.ItemName))
+            {
+                query = query.Where((stock, item, depot, org, unit) =>
+                    item.ItemName != null &&
+                    item.ItemName.Contains(conditions.ItemName));
+            }
+
+            if (!string.IsNullOrEmpty(conditions.ItemModel))
+            {
+                query = query.Where((stock, item, depot, org, unit) =>
+                    item.ItemModel != null &&
+                    item.ItemModel.Contains(conditions.ItemModel));
+            }
+
+            if (!string.IsNullOrEmpty(conditions.ItemUnitName))
+            {
+                query = query.Where((stock, item, depot, org, unit) =>
+                    unit.Fname != null &&
+                    unit.Fname.Contains(conditions.ItemUnitName));
+            }
+
+            if (!string.IsNullOrEmpty(conditions.OrgCode))
+            {
+                query = query.Where((stock, item, depot, org, unit) =>
+                    org.Fnumber != null &&
+                    org.Fnumber.Contains(conditions.OrgCode));
+            }
+
+            if (!string.IsNullOrEmpty(conditions.OrgName))
+            {
+                query = query.Where((stock, item, depot, org, unit) =>
+                    org.Fname != null &&
+                    org.Fname.Contains(conditions.OrgName));
+            }
+
+            if (!string.IsNullOrEmpty(conditions.ItemBarcode))
+            {
+                query = query.Where((stock, item, depot, org, unit) =>
+                    (stock.ItemBarcode != null &&
+                     stock.ItemBarcode.Contains(conditions.ItemBarcode)) ||
+                    (stock.StackCode != null &&
+                     stock.StackCode.Contains(conditions.ItemBarcode)));
+            }
+
+            // 鏃ユ湡鑼冨洿鏉′欢
+            if (!string.IsNullOrEmpty(conditions.IndepDateStart))
+            {
+                if (DateTime.TryParse(conditions.IndepDateStart,
+                        out var startDate))
+                {
+                    query = query.Where((stock, item, depot, org, unit) =>
+                        stock.IndepDate >= startDate);
+                }
+            }
+
+            if (!string.IsNullOrEmpty(conditions.IndepDateEnd))
+            {
+                if (DateTime.TryParse(conditions.IndepDateEnd, out var endDate))
+                {
+                    query = query.Where((stock, item, depot, org, unit) =>
+                        stock.IndepDate <= endDate);
+                }
+            }
+        }
+
+        // 4. 鏌ヨ鎵�鏈夌鍚堟潯浠剁殑鏁版嵁锛堜笉鍒嗛〉锛�
+        var queryResult = query
+            .OrderByDescending((stock, item, depot, org, unit) =>
+                stock.IndepDate)
+            .Select((stock, item, depot, org, unit) => new
+            {
+                stock.IqcStatus,
+                DepotCode = stock.DepotsCode,
+                depot.DepotName,
+                stock.DepotSectionsCode,
+                item.ItemNo,
+                item.ItemName,
+                item.ItemModel,
+                stock.Quantity,
+                item.ItemUnit,
+                ItemUnitName = unit.Fname,
+                stock.IndepDate,
+                OrgCode = org.Fnumber,
+                OrgName = org.Fname,
+                stock.ItemBarcode,
+                StockStackCode = stock.StackCode
+            })
+            .ToList();
 
         // 5. 鍦ㄥ唴瀛樹腑杞崲涓篋TO锛屽叧鑱擯alletCode骞惰祴鍊糏temBarcode锛堜紭鍏堜娇鐢ㄧ鏉$爜StockStackCode锛�
         var tempDataList = queryResult.Select(x =>
@@ -247,12 +227,12 @@
             var barcodeToMatch = !string.IsNullOrEmpty(x.StockStackCode)
                 ? x.StockStackCode
                 : x.ItemBarcode;
-            
-            // 浣跨敤瀛楀吀鏌ユ壘锛屾�ц兘鏇翠紭
-            rackingTaskDict.TryGetValue(barcodeToMatch, out var rackingTask);
+            var rackingTask = rackingTaskData
+                .Where(r => r.ItemBarcode == barcodeToMatch)
+                .FirstOrDefault();
 
             // 鏍规嵁Code鍊肩‘瀹氬簱瀛樼姸鎬�: null涓虹珛搴撳叆搴撲腑(0), 200涓哄凡鍦ㄧ珛搴撳唴(1)
-            string? stockStatus = "杩涘叆绔嬪簱鐨勮矾涓�"; // 榛樿涓虹珛搴撳叆搴撲腑
+            string stockStatus = "杩涘叆绔嬪簱鐨勮矾涓�"; // 榛樿涓虹珛搴撳叆搴撲腑
             if (rackingTask?.Code != null)
             {
                 stockStatus = rackingTask.Code == "200" ? "宸插湪绔嬪簱涓�" : "杩涘叆绔嬪簱鐨勮矾涓�";
@@ -371,39 +351,6 @@
     }
 
     /// <summary>
-    ///     杈呭姪绫伙細绔嬪簱浠诲姟淇℃伅
-    /// </summary>
-    private class RackingTaskInfo
-    {
-        public string? ItemBarcode { get; set; }
-        public string? PalletCode { get; set; }
-        public string? Code { get; set; }
-        public decimal Id { get; set; }
-    }
-
-    /// <summary>
-    ///     杈呭姪绫伙細搴撳瓨鏌ヨ涓棿缁撴灉
-    /// </summary>
-    private class StockQueryResult
-    {
-        public string? IqcStatus { get; set; }
-        public string? DepotCode { get; set; }
-        public string? DepotName { get; set; }
-        public string? DepotSectionsCode { get; set; }
-        public string? ItemNo { get; set; }
-        public string? ItemName { get; set; }
-        public string? ItemModel { get; set; }
-        public decimal? Quantity { get; set; }
-        public string? ItemUnit { get; set; }
-        public string? ItemUnitName { get; set; }
-        public DateTime? IndepDate { get; set; }
-        public string? OrgCode { get; set; }
-        public string? OrgName { get; set; }
-        public string? ItemBarcode { get; set; }
-        public string? StockStackCode { get; set; }
-    }
-
-    /// <summary>
     ///     鏌ヨ鍙互閫�璐х殑鐗╂枡(鏃х増鏈�,淇濈暀鍏煎)
     /// </summary>
     /// <returns>鍙��璐х墿鏂欏簱瀛樺垪琛�</returns>
@@ -473,7 +420,7 @@
                 .FirstOrDefault();
 
             // 鏍规嵁Code鍊肩‘瀹氬簱瀛樼姸鎬�: null涓虹珛搴撳叆搴撲腑(0), 200涓哄凡鍦ㄧ珛搴撳唴(1)
-            string? stockStatus = "0"; // 榛樿涓虹珛搴撳叆搴撲腑
+            string stockStatus = "0"; // 榛樿涓虹珛搴撳叆搴撲腑
             if (rackingTask?.Code != null)
             {
                 stockStatus =
@@ -542,7 +489,7 @@
                 }
 
                 decimal messageId = 0;
-                string? taskCode = "";
+                string taskCode = "";
 
                 // 鏍规嵁鏉$爜鏌ヨXB_RACKING_TASK_SYXT_LOG琛紝鏌ヨmax(PALLETCODE)鍜屽搴旂殑widthType
                 var rackingTaskInfo = Db.Queryable<XbRackingTaskSyxtLog>()
@@ -647,8 +594,8 @@
                         throw new Exception($"瑙f瀽鍝嶅簲澶辫触: {responseStr}");
                     }
 
-                    var code = responseJson?.Code?.ToString()();
-                    var jsonMessage = responseJson?.JsonMessage?.ToString()();
+                    var code = responseJson?.Code?.ToString();
+                    var jsonMessage = responseJson?.JsonMessage?.ToString();
 
                     // 7. 鏍规嵁Code鍒ゆ柇鎴愬姛鎴栧け璐�
                     if (code == "200")
@@ -688,7 +635,7 @@
                             ? "浠诲姟璇锋眰澶辫触"
                             : jsonMessage;
                         var failureMessage =
-                            (string?)("绔嬪簱浠诲姟涓嬪彂澶辫触: " + errorMessage);
+                            (string)("绔嬪簱浠诲姟涓嬪彂澶辫触: " + errorMessage);
 
                         Db.Updateable<MessageCenter>()
                             .SetColumns(it => it.Result == 0)
@@ -707,7 +654,7 @@
                     if (messageId > 0)
                     {
                         var exceptionMessage =
-                            (string?)("绔嬪簱浠诲姟寮傚父: " + ex.Message);
+                            (string)("绔嬪簱浠诲姟寮傚父: " + ex.Message);
                         Db.Updateable<MessageCenter>()
                             .SetColumns(it => it.Result == 0)
                             .SetColumns(it =>

--
Gitblit v1.9.3