From a91fdba94705dd25cecccc7a4a5dcc2b3f2c09a6 Mon Sep 17 00:00:00 2001
From: llj <132905093+newwwwwwtree@users.noreply.github.com>
Date: 星期一, 22 十二月 2025 16:16:00 +0800
Subject: [PATCH] Merge branch 'master' of http://101.37.171.70:10101/r/MES-WEB-API

---
 WebAPI/Controllers/MateOutController.cs |  383 +++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 357 insertions(+), 26 deletions(-)

diff --git a/WebAPI/Controllers/MateOutController.cs b/WebAPI/Controllers/MateOutController.cs
index a6c7077..517102f 100644
--- a/WebAPI/Controllers/MateOutController.cs
+++ b/WebAPI/Controllers/MateOutController.cs
@@ -1074,40 +1074,175 @@
         {
             try
             {
-
                 SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                 List<object> columnNameList = new List<object>();
-                DataSet ds = oCN.RunProcReturn("exec h_p_KF_ICInventoryByMaterIDList1 '" + HBarCode + "'," + sHWHID + "," + sHSPID+","+ HOWNERID+",'"+ sWhere+"'", "h_p_KF_ICInventoryByMaterIDList");
-                //娣诲姞鍒楀悕
-                foreach (DataColumn col in ds.Tables[0].Columns)
+                string Ret = "";
+
+                if (oSystemParameter.ShowBill(ref Ret))
                 {
-                    Type dataType = col.DataType;
-                    string ColmString = "{\"ColmCols\":\"" + col.ColumnName + "\",\"ColmType\":\"" + dataType.Name + "\"}";
-                    columnNameList.Add(JsonConvert.DeserializeObject(ColmString));//鑾峰彇鍒癉ataColumn鍒楀璞$殑鍒楀悕
+                    if (oSystemParameter.omodel.WMS_CloudMode == "Y")
+                    {
+                        DataSet ds = oCN.RunProcReturn("exec h_p_KF_ICInventoryByMaterIDList1 '" + HBarCode + "'," + sHWHID + "," + sHSPID + "," + HOWNERID + ",'" + sWhere + "'", "h_p_KF_ICInventoryByMaterIDList");
+
+                        //娣诲姞鍒楀悕
+                        foreach (DataColumn col in ds.Tables[0].Columns)
+                        {
+                            Type dataType = col.DataType;
+                            string ColmString = "{\"ColmCols\":\"" + col.ColumnName + "\",\"ColmType\":\"" + dataType.Name + "\"}";
+                            columnNameList.Add(JsonConvert.DeserializeObject(ColmString));//鑾峰彇鍒癉ataColumn鍒楀璞$殑鍒楀悕
+                        }
+
+                        if (ds == null || ds.Tables[0].Rows.Count <= 0)
+                        {
+                            objJsonResult.code = "0";
+                            objJsonResult.count = 0;
+                            objJsonResult.Message = "娌℃湁杩斿洖浠讳綍璁板綍锛�";
+                            objJsonResult.data = null;
+                            return objJsonResult;
+                        }
+                        else
+                        {
+                            objJsonResult.code = "1";
+                            objJsonResult.count = 1;
+                            objJsonResult.Message = "鑾峰彇淇℃伅鎴愬姛锛�";
+                            objJsonResult.data = ds.Tables[0];
+                            objJsonResult.list = columnNameList;
+                            return objJsonResult;
+                        }
+                    }
+                    else
+                    {
+                        //浠庨厤缃枃浠惰幏鍙� CLOUD缃戝潃銆佽处濂椾俊鎭�佺櫥褰曠敤鎴枫�佺櫥褰曞瘑鐮�
+                        if (!Pub_Class.ClsPub.GetCLOUDLoginInfo(ref Pub_Class.ClsPub.sExeReturnInfo))
+                        {
+                            objJsonResult.code = "0";
+                            objJsonResult.count = 0;
+                            objJsonResult.Message = "ERP搴撳瓨鏌ヨ锛屽悓姝ヨ幏鍙栫櫥褰曡处鍙峰瘑鐮佸け璐ワ紒";
+                            objJsonResult.data = null;
+                            return objJsonResult;
+                        }
+
+                        var loginRet = InvokeHelper.Login();
+                        var isSuccess = JObject.Parse(loginRet)["LoginResultType"].Value<int>();
+                        if (isSuccess == 0)
+                        {
+                            objJsonResult.code = "0";
+                            objJsonResult.count = 0;
+                            objJsonResult.Message = "鐧诲綍閲戣澏澶辫触锛�";
+                            objJsonResult.data = null;
+                            return objJsonResult;
+                        }
+                        else
+                        {
+                            string queryJson = BuildDynamicQueryJson(HBarCode, sHWHID, HOWNERID);
+
+                            string result = InvokeHelper.ExecuteBillQuery("STK_Inventory", queryJson);//鏌ヨ
+                            var dataArray = JArray.Parse(result);
+
+                            if (dataArray == null || dataArray.Count == 0)
+                            {
+                                objJsonResult.code = "1";
+                                objJsonResult.count = 0;
+                                objJsonResult.Message = "鏌ヨ鎴愬姛锛屾棤鏁版嵁锛�";
+                                objJsonResult.data = null;
+                                return objJsonResult;
+                            }
+
+                            // 绗竴姝ワ細鏀堕泦鎵�鏈夐渶瑕佹煡璇㈢殑ID
+                            var ids = CollectIdsFromDataArray(dataArray);
+
+                            // 绗簩姝ワ細鎵归噺鏌ヨ鏁版嵁搴撲腑鐨勫熀纭�淇℃伅锛堝悓姝ユ柟寮忥級
+                            var stockDict = BatchQuery(oCN, "Gy_Warehouse", "HName", ids.StockIds);
+                            var locationDict = BatchQuery(oCN, "Gy_StockPlace", "HName", ids.LocationIds);
+                            var materialDict = BatchQuery(oCN, "Gy_Material", "HNumber", ids.MaterialIds);
+                            var orgDict = BatchQuery(oCN, "Xt_ORGANIZATIONS", "HName", ids.OwnerIds);
+                            var propDict = BatchQuery(oCN, "Gy_Property", "HName", ids.PropIds);
+
+                            // 绗笁姝ワ細鎵归噺鏌ヨ鎵规淇℃伅锛堣皟鐢ㄩ噾铦舵帴鍙o級
+                            var batchDict = BatchQueryBatchInfo(ids.BatchIds);
+
+                            var finalResults = new List<Dictionary<string, object>>();
+
+                            // 绗洓姝ワ細澶勭悊鏁版嵁
+                            foreach (JArray item in dataArray)
+                            {
+                                if (item.Count >= 11)
+                                {
+                                    int HMaterID = item[0]?.Value<int>() ?? 0;
+                                    int HStockId = item[3]?.Value<int>() ?? 0;
+                                    int HLocationId = item[5]?.Value<int>() ?? 0;
+                                    int FAuxPropId = item[8]?.Value<int>() ?? 0;
+                                    int ownerId = item[11]?.Value<int>() ?? 0;
+                                    int FLot = item[6]?.Value<int>() ?? 0; // 鎵规ID
+
+                                    // 鍒涘缓缁撴灉瀛楀吀锛屼娇鐢ㄤ腑鏂囧瓧娈靛悕
+                                    var resultDict = new Dictionary<string, object>
+                                    {
+                                        ["鐗╂枡鍚嶇О"] = item[1].Value<string>(),
+                                        ["瑙勬牸鍨嬪彿"] = item[2].Value<string>(),
+                                        ["鏁伴噺"] = item[4].Value<decimal>(),
+                                      
+                                        ["鐢熶骇鏃ユ湡"] = DBUtility.ClsPub.isDate(item[8]?.Value<string>()),
+                                        ["鏈夋晥鏈熻嚦"] = DBUtility.ClsPub.isDate(item[9]?.Value<string>())
+                                    };
+
+                                    // 浠庡瓧鍏歌幏鍙栦粨搴撳悕绉�
+                                    if (HStockId > 0 && stockDict.TryGetValue(HStockId, out string stockName))
+                                        resultDict["浠撳簱"] = stockName;
+                                    else
+                                        resultDict["浠撳簱"] = "";
+
+                                    // 浠庡瓧鍏歌幏鍙栦粨浣嶅悕绉�
+                                    if (HLocationId > 0 && locationDict.TryGetValue(HLocationId, out string locationName))
+                                        resultDict["浠撲綅"] = locationName;
+                                    else
+                                        resultDict["浠撲綅"] = "";
+
+                                    // 浠庡瓧鍏歌幏鍙栫墿鏂欎唬鐮�
+                                    if (HMaterID > 0 && materialDict.TryGetValue(HMaterID, out string materialCode))
+                                        resultDict["鐗╂枡浠g爜"] = materialCode;
+                                    else
+                                        resultDict["鐗╂枡浠g爜"] = "";
+
+                                    // 浠庡瓧鍏歌幏鍙栫粍缁囧悕绉�
+                                    if (ownerId > 0 && orgDict.TryGetValue(ownerId, out string orgName))
+                                        resultDict["缁勭粐"] = orgName;
+                                    else
+                                        resultDict["缁勭粐"] = "";
+
+                                    // 浠庡瓧鍏歌幏鍙栬緟鍔╁睘鎬у悕绉�
+                                    if (FAuxPropId > 0 && propDict.TryGetValue(FAuxPropId, out string propName))
+                                        resultDict["杈呭姪灞炴��"] = propName;
+                                    else
+                                        resultDict["杈呭姪灞炴��"] = "";
+
+                                    // 浠庡瓧鍏歌幏鍙栨壒娆″悕绉�
+                                    if (FLot > 0 && batchDict.TryGetValue(FLot, out string batchName))
+                                        resultDict["鎵规"] = batchName;
+                                    else
+                                        resultDict["鎵规"] = "";
+
+                                    finalResults.Add(resultDict);
+                                }
+                            }
+
+                            objJsonResult.code = "1";
+                            objJsonResult.count = finalResults.Count;
+                            objJsonResult.Message = "鏌ヨ鎴愬姛锛�";
+                            objJsonResult.data = finalResults;
+                            return objJsonResult;
+                        }
+                    }
                 }
 
-                if (ds == null || ds.Tables[0].Rows.Count <= 0)
-                {
-                    objJsonResult.code = "0";
-                    objJsonResult.count = 0;
-                    objJsonResult.Message = "娌℃湁杩斿洖浠讳綍璁板綍锛�";
-                    objJsonResult.data = null;
-                    return objJsonResult;
-                    //DBUtility.ClsPub.MessageBeep((int)DBUtility.ClsPub.BeepType.Warning);
-                }
-                else
-                {
-                    objJsonResult.code = "1";
-                    objJsonResult.count = 1;
-                    objJsonResult.Message = "鑾峰彇淇℃伅鎴愬姛锛�";
-                    objJsonResult.data = ds.Tables[0];
-                    objJsonResult.list = columnNameList;
-                    return objJsonResult;
-                }
+                objJsonResult.code = "1";
+                objJsonResult.count = 1;
+                objJsonResult.Message = "娌℃湁杩斿洖浠讳綍璁板綍锛�";
+                objJsonResult.data = null;
+                return objJsonResult;
             }
             catch (Exception e)
             {
-
                 objJsonResult.code = "0";
                 objJsonResult.count = 0;
                 objJsonResult.Message = "娌℃湁杩斿洖浠讳綍璁板綍锛�" + e.ToString();
@@ -1116,6 +1251,202 @@
             }
         }
 
+        #region 閲戣澏鍙婃椂搴撳瓨鏌ヨ 浣跨敤鏂规硶
+
+        // 杈呭姪绫伙細鐢ㄤ簬鏀堕泦鎵�鏈夐渶瑕佹煡璇㈢殑ID
+        private class CollectedIds
+        {
+            public HashSet<int> StockIds { get; set; } = new HashSet<int>();
+            public HashSet<int> LocationIds { get; set; } = new HashSet<int>();
+            public HashSet<int> MaterialIds { get; set; } = new HashSet<int>();
+            public HashSet<int> OwnerIds { get; set; } = new HashSet<int>();
+            public HashSet<int> PropIds { get; set; } = new HashSet<int>();
+            public HashSet<int> BatchIds { get; set; } = new HashSet<int>(); // 鎵规ID
+        }
+
+        // 鏀堕泦鎵�鏈夐渶瑕佹煡璇㈢殑ID
+        private CollectedIds CollectIdsFromDataArray(JArray dataArray)
+        {
+            var ids = new CollectedIds();
+
+            foreach (JArray item in dataArray)
+            {
+                if (item.Count >= 11)
+                {
+                    int HMaterID = item[0]?.Value<int>() ?? 0;
+                    int HStockId = item[3]?.Value<int>() ?? 0;
+                    int HLocationId = item[5]?.Value<int>() ?? 0;
+                    int FAuxPropId = item[8]?.Value<int>() ?? 0;
+                    int ownerId = item[11]?.Value<int>() ?? 0;
+                    int FLot = item[6]?.Value<int>() ?? 0; // 鎵规ID
+
+                    if (HStockId > 0) ids.StockIds.Add(HStockId);
+                    if (HLocationId > 0) ids.LocationIds.Add(HLocationId);
+                    if (HMaterID > 0) ids.MaterialIds.Add(HMaterID);
+                    if (ownerId > 0) ids.OwnerIds.Add(ownerId);
+                    if (FAuxPropId > 0) ids.PropIds.Add(FAuxPropId);
+                    if (FLot > 0) ids.BatchIds.Add(FLot); // 鏀堕泦鎵规ID
+                }
+            }
+
+            return ids;
+        }
+
+        // 鎵归噺鏌ヨ鏂规硶锛堝悓姝ョ増鏈級
+        private Dictionary<int, string> BatchQuery(SQLHelper.ClsCN oCN, string tableName, string nameColumn, HashSet<int> ids)
+        {
+            if (ids == null || ids.Count == 0)
+                return new Dictionary<int, string>();
+
+            string idList = string.Join(",", ids);
+            string query = $"select Hitemid, {nameColumn} from {tableName} with (nolock) where Hitemid in ({idList})";
+
+            DataSet ds = oCN.RunProcReturn(query, tableName);
+            var dict = new Dictionary<int, string>();
+
+            if (ds != null && ds.Tables.Count > 0)
+            {
+                foreach (DataRow row in ds.Tables[0].Rows)
+                {
+                    int id = Convert.ToInt32(row["Hitemid"]);
+                    string name = row[nameColumn].ToString();
+                    dict[id] = name;
+                }
+            }
+
+            return dict;
+        }
+
+        // 鎵归噺鏌ヨ鎵规淇℃伅锛堣皟鐢ㄩ噾铦舵帴鍙o級
+        private Dictionary<int, string> BatchQueryBatchInfo(HashSet<int> batchIds)
+        {
+            var batchDict = new Dictionary<int, string>();
+
+            if (batchIds == null || batchIds.Count == 0)
+                return batchDict;
+
+            // 濡傛灉闇�瑕佸苟琛屾煡璇紝鍙互浣跨敤 Parallel.ForEach
+            // 浣嗘敞鎰忥細濡傛灉鎺ュ彛涓嶆敮鎸侀珮骞跺彂锛屽彲浠ユ敼涓洪『搴忔煡璇�
+            foreach (int batchId in batchIds)
+            {
+                try
+                {
+                    // 鏋勫缓鏌ヨ鍙傛暟
+                    var queryJson = $"{{\"Id\": {batchId}, \"IsSortBySeq\": \"false\"}}";
+
+                    // 璋冪敤鎵规鏌ヨ鎺ュ彛
+                    string result = InvokeHelper.View("BD_BatchMainFile", queryJson);
+
+                    // 瑙f瀽杩斿洖鐨凧SON
+                    var jsonObj = JObject.Parse(result);
+
+                    // 鑾峰彇鎵规鍚嶇О锛堜粠Name鏁扮粍鐨勭涓�涓厓绱犵殑Value锛�
+                    var nameArray = jsonObj["Result"]?["Result"]?["Name"] as JArray;
+                    if (nameArray != null && nameArray.Count > 0)
+                    {
+                        string batchName = nameArray[0]["Value"]?.ToString();
+                        if (!string.IsNullOrEmpty(batchName))
+                        {
+                            batchDict[batchId] = batchName;
+                        }
+                        else
+                        {
+                            // 濡傛灉Name鏁扮粍涓病鏈夊�硷紝灏濊瘯浠嶯umber瀛楁鑾峰彇
+                            string number = jsonObj["Result"]?["Result"]?["Number"]?.ToString();
+                            if (!string.IsNullOrEmpty(number))
+                            {
+                                batchDict[batchId] = number;
+                            }
+                        }
+                    }
+                }
+                catch (Exception ex)
+                {
+                    // 璁板綍閿欒鏃ュ織锛屼絾缁х画澶勭悊鍏朵粬鎵规
+                    Console.WriteLine($"鏌ヨ鎵规淇℃伅澶辫触锛屾壒娆D: {batchId}, 閿欒: {ex.Message}");
+                    // 濡傛灉鏌ヨ澶辫触锛屽彲浠ュ皢鎵规ID浣滀负榛樿鍊�
+                    batchDict[batchId] = batchId.ToString();
+                }
+            }
+
+            return batchDict;
+        }
+
+        //JSON瀛楃涓插弬鏁�
+        public static string BuildDynamicQueryJson(string materialNumber = null, long FHStockId=0, long HOWNERID = 0)
+        {
+            // 鍒涘缓鍔ㄦ�佽繃婊ゆ潯浠跺垪琛�
+            var filterList = new List<object>();            
+            if (HOWNERID!= 100038)
+            {
+                // 濡傛灉鏈夌墿鏂欑紪鐮侊紝娣诲姞鐗╂枡鏉′欢
+                if (!string.IsNullOrWhiteSpace(materialNumber))
+                {
+                    filterList.Add(new
+                    {
+                        Left = "",
+                        FieldName = "FHMaterID.FNumber",
+                        Compare = "67",  // 绛変簬
+                        Value = materialNumber,
+                        Right = "",
+                        Logic = filterList.Count > 0 ? 1 : 0  // 濡傛灉鏈夊涓潯浠讹紝Logic=1琛ㄧずOR
+                    });
+                }
+                // 濡傛灉鏈変粨搴撳悕绉帮紝娣诲姞浠撳簱鏉′欢
+                if (FHStockId != 0)
+                {
+                    filterList.Add(new
+                    {
+                        Left = "",
+                        FieldName = "FHStockId",
+                        Compare = "67",  // 鍖呭惈
+                        Value = FHStockId,
+                        Right = "",
+                        Logic = filterList.Count > 0 ? 1 : 0  // 濡傛灉鏈夊涓潯浠讹紝Logic=1琛ㄧずOR
+                    });
+                }
+                // 濡傛灉鏈夌粍缁嘔D锛屾坊鍔犵粍缁囨潯浠�
+                if (HOWNERID != 0)
+                {
+                    filterList.Add(new
+                    {
+                        Left = "",
+                        FieldName = "FStockOrgId",
+                        Compare = "67",  // 鍖呭惈
+                        Value = HOWNERID,  // 淇濇寔涔嬪墠鐨勭被鍨嬭浆鎹慨澶�
+                        Right = "",
+                        Logic = filterList.Count > 0 ? 1 : 0  // 濡傛灉鏈夊涓潯浠讹紝Logic=1琛ㄧずOR
+                    });
+                }
+
+                // 鏋勫缓瀹屾暣鐨勬煡璇㈠弬鏁�
+                var queryParams = new
+                {
+                    FormId = "STK_Inventory",
+                    FieldKeys = "FMaterialId,FMaterialName,FModel,FStockUnitId,FBaseQty,FStockId,FStockLocId,FLot,FAuxPropId,FProduceDate,FExpiryDate,FStockOrgId",
+                    FilterString = filterList
+                };
+                return JsonConvert.SerializeObject(queryParams);
+            }
+            else
+            {
+
+                //47 涓嶆敮鎸佷笂闈㈢殑鍔ㄦ�佽繃婊ゆ潯浠� 鏋勫缓瀹屾暣鐨勬煡璇㈠弬鏁�
+                var queryParams1 = new
+                {
+                    FormId = "STK_Inventory",
+                    FieldKeys = "FMaterialId,FMaterialName,FModel,FStockUnitId,FBaseQty,FStockId,FStockLocId,FLot,FAuxPropId,FProduceDate,FExpiryDate,FStockOrgId",
+                    FilterString = ""
+                };
+                return JsonConvert.SerializeObject(queryParams1);
+            }
+            
+
+           
+        }
+
+        #endregion
+
         //鍒锋柊琛ㄤ綋 杞﹂棿瀹氫綅  杩斿洖杞﹂棿鍗虫椂搴撳瓨搴撳瓨淇℃伅缃戦〉PDA鐗�
         [Route("KF_ICInventory_WorkShopByMaterID/GetWorkShopICInventory")]
         [HttpGet]

--
Gitblit v1.9.1