From 23302eec1c9f2a8e1d8b41f7001950e046208cd6 Mon Sep 17 00:00:00 2001
From: zjh <2207896513@qq.com>
Date: 星期四, 21 八月 2025 21:56:22 +0800
Subject: [PATCH] 即时库存获取代码提交

---
 StandardPda/MESApplication/appsettings.json                         |   12 +
 StandardPda/MES.Service/Dto/service/WhqohDto.cs                     |    9 +
 StandardPda/MES.Service/Dto/service/WhqohList.cs                    |   32 ++++++
 StandardPda/MESApplication/Controllers/Warehouse/WhqohController.cs |   71 ++++++++++++++
 StandardPda/MES.Service/service/Warehouse/whqohDtoManager.cs        |  168 +++++++++++++++++++++++++++++++++
 5 files changed, 288 insertions(+), 4 deletions(-)

diff --git a/StandardPda/MES.Service/Dto/service/WhqohDto.cs b/StandardPda/MES.Service/Dto/service/WhqohDto.cs
new file mode 100644
index 0000000..f9cee95
--- /dev/null
+++ b/StandardPda/MES.Service/Dto/service/WhqohDto.cs
@@ -0,0 +1,9 @@
+锘縩amespace MES.Service.Dto.service
+{
+    public class WhqohDto
+    {
+
+        //public List<WhqohList> items { get; set; }
+        public string itemId { get; set; }
+    }
+}
diff --git a/StandardPda/MES.Service/Dto/service/WhqohList.cs b/StandardPda/MES.Service/Dto/service/WhqohList.cs
new file mode 100644
index 0000000..5d74807
--- /dev/null
+++ b/StandardPda/MES.Service/Dto/service/WhqohList.cs
@@ -0,0 +1,32 @@
+锘縰sing System.Text.Json.Serialization;
+
+namespace MES.Service.Dto.service
+{
+    public class WhqohList
+    {
+        /// <summary>
+        /// 鐗╂枡ID
+        /// </summary>
+        [JsonPropertyName("ItemId")]
+        public string ItemId { get; set; }
+
+        ///// <summary>
+        ///// 浠撳簱ID
+        ///// </summary>
+        //[JsonPropertyName("WhId")]
+        //public string WhId { get; set; }
+
+        ///// <summary>
+        ///// 椤圭洰
+        ///// </summary>
+        //[JsonPropertyName("Project")]
+        //public string Project { get; set; }
+
+        ///// <summary>
+        ///// 鎵瑰彿
+        ///// </summary>
+        //[JsonPropertyName("LotCode")]
+        //public string LotCode { get; set; }
+
+    }
+}
diff --git a/StandardPda/MES.Service/service/Warehouse/whqohDtoManager.cs b/StandardPda/MES.Service/service/Warehouse/whqohDtoManager.cs
new file mode 100644
index 0000000..980a4c7
--- /dev/null
+++ b/StandardPda/MES.Service/service/Warehouse/whqohDtoManager.cs
@@ -0,0 +1,168 @@
+锘縰sing Masuit.Tools;
+using MES.Service.DB;
+using MES.Service.Dto.service;
+using MES.Service.Modes;
+using Newtonsoft.Json;
+using Oracle.ManagedDataAccess.Client;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Microsoft.Extensions.Configuration;
+using System.Net.Http;
+using System.Globalization;
+using System.IO;
+
+namespace MES.Service.service.Warehouse
+{
+    #region 鎺ュ彛杩斿洖鏁版嵁缁撴瀯瀹氫箟
+    public class WhqohResponse
+    {
+        public Header Header { get; set; }
+        public List<BodyItem> Body { get; set; }
+    }
+
+    public class Header
+    {
+        public int Code { get; set; }
+        public bool Success { get; set; }
+        public string Message { get; set; }
+    }
+
+    public class BodyItem
+    {
+        public string ItemId { get; set; }
+        public List<Detail> Detail { get; set; }
+    }
+
+    public class Detail
+    {
+        public string WhId { get; set; }
+        public string UomId { get; set; }
+        public string CurrentQuantity { get; set; }
+        public string AvailableQuantity { get; set; }
+        public string Project { get; set; }
+        public string LotCode { get; set; }
+    }
+    #endregion
+
+    public class whqohDtoManager
+    {
+        private static IConfiguration configuration = new ConfigurationBuilder()
+            .SetBasePath(Directory.GetCurrentDirectory())
+            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
+            .Build();
+
+        // Oracle 鏁版嵁搴撹繛鎺ュ瓧绗︿覆
+        private static readonly string connectionString = configuration["AppSettings:DataBaseConn"];
+        // 鎺ュ彛鍦板潃
+        private static readonly string apiUrl = "http://" + configuration["AppSettings:U9IP"] + "/api/getWhqoh";
+
+        public static async Task<bool> GetWhqoh(WhqohDto mesItemQtRKDto)
+        {
+            // 1. 璇锋眰鎺ュ彛
+            var requestBody = new[]
+            {
+                new { ItemId = mesItemQtRKDto.itemId, WhId = "", Project = "", LotCode = "" }
+            };
+
+            string jsonRequest = JsonConvert.SerializeObject(requestBody);
+
+            using var http = new HttpClient();
+            var response = await http.PostAsync(apiUrl,
+                new StringContent(jsonRequest, Encoding.UTF8, "application/json"));
+
+            string responseStr = await response.Content.ReadAsStringAsync();
+
+            // 2. 鍙嶅簭鍒楀寲鎺ュ彛杩斿洖 JSON
+            var result = JsonConvert.DeserializeObject<WhqohResponse>(responseStr);
+
+            if (result?.Header.Success != true || result.Body == null)
+            {
+                Console.WriteLine("鎺ュ彛杩斿洖澶辫触: " + result?.Header?.Message);
+                return false;
+            }
+
+            // 3. 鎵佸钩鍖栧苟鍘婚噸
+            var records = new HashSet<(string ItemId, string WhId, string UomId, string Project, string LotCode)>();
+            var distinctList = new List<(string ItemId, string WhId, string UomId, decimal CurrentQty, decimal AvailableQty, string Project, string LotCode)>();
+
+            foreach (var body in result.Body)
+            {
+                foreach (var detail in body.Detail)
+                {
+                    var key = (body.ItemId, detail.WhId, detail.UomId, detail.Project ?? "", detail.LotCode ?? "");
+                    if (records.Add(key)) // 鍘婚噸
+                    {
+                        distinctList.Add((
+                            body.ItemId,
+                            detail.WhId,
+                            detail.UomId,
+                            decimal.Parse(detail.CurrentQuantity, CultureInfo.InvariantCulture),
+                            decimal.Parse(detail.AvailableQuantity, CultureInfo.InvariantCulture),
+                            detail.Project ?? "",
+                            detail.LotCode ?? ""
+                        ));
+                    }
+                }
+            }
+
+            // 4. 鍏堝垹鍚庢彃
+            using var conn = new OracleConnection(connectionString);
+            await conn.OpenAsync();
+            using var tran = conn.BeginTransaction();
+
+            try
+            {
+                string deleteSql = "";
+                // 鍒犻櫎鎵�鏈夋棫鏁版嵁锛堟寜闇�姹傝皟鏁磋寖鍥达紝鍙互鍔� WHERE锛�
+                if (mesItemQtRKDto.itemId=="" || mesItemQtRKDto.itemId==null)
+                {
+                     deleteSql = "DELETE FROM WHQOH_DETAIL";
+                }
+                else
+                {
+                    deleteSql = "DELETE FROM WHQOH_DETAIL where ITEM_ID='"+ mesItemQtRKDto.itemId + "'";
+                }
+                
+                using (var delCmd = new OracleCommand(deleteSql, conn))
+                {
+                    delCmd.Transaction = tran;
+                    await delCmd.ExecuteNonQueryAsync();
+                }
+
+                // 鎻掑叆鏂版暟鎹�
+                string insertSql = @"
+INSERT INTO WHQOH_DETAIL
+(ID, ITEM_ID, WH_ID, UOM_ID, CURRENT_QUANTITY, AVAILABLE_QUANTITY, PROJECT_CODE, LOT_CODE)
+VALUES
+(SEQ_WHQOH_DETAIL.NEXTVAL, :ItemId, :WhId, :UomId, :CurrentQty, :AvailableQty, :Project, :LotCode)";
+
+                using var cmd = new OracleCommand(insertSql, conn);
+                cmd.Transaction = tran;
+                cmd.ArrayBindCount = distinctList.Count;
+
+                cmd.Parameters.Add(":ItemId", OracleDbType.Varchar2, distinctList.Select(x => x.ItemId).ToArray(), System.Data.ParameterDirection.Input);
+                cmd.Parameters.Add(":WhId", OracleDbType.Varchar2, distinctList.Select(x => x.WhId).ToArray(), System.Data.ParameterDirection.Input);
+                cmd.Parameters.Add(":UomId", OracleDbType.Varchar2, distinctList.Select(x => x.UomId).ToArray(), System.Data.ParameterDirection.Input);
+                cmd.Parameters.Add(":CurrentQty", OracleDbType.Decimal, distinctList.Select(x => x.CurrentQty).ToArray(), System.Data.ParameterDirection.Input);
+                cmd.Parameters.Add(":AvailableQty", OracleDbType.Decimal, distinctList.Select(x => x.AvailableQty).ToArray(), System.Data.ParameterDirection.Input);
+                cmd.Parameters.Add(":Project", OracleDbType.Varchar2, distinctList.Select(x => x.Project).ToArray(), System.Data.ParameterDirection.Input);
+                cmd.Parameters.Add(":LotCode", OracleDbType.Varchar2, distinctList.Select(x => x.LotCode).ToArray(), System.Data.ParameterDirection.Input);
+
+                int affected = await cmd.ExecuteNonQueryAsync();
+                tran.Commit();
+
+                Console.WriteLine($"鎵归噺澶勭悊鎴愬姛锛屽叡 {affected} 鏉℃暟鎹鎻掑叆銆�");
+                return true;
+            }
+            catch (Exception ex)
+            {
+                tran.Rollback();
+                Console.WriteLine("鎵归噺澶勭悊澶辫触: " + ex.Message);
+                return false;
+            }
+        }
+    }
+}
diff --git a/StandardPda/MESApplication/Controllers/Warehouse/WhqohController.cs b/StandardPda/MESApplication/Controllers/Warehouse/WhqohController.cs
new file mode 100644
index 0000000..0247c64
--- /dev/null
+++ b/StandardPda/MESApplication/Controllers/Warehouse/WhqohController.cs
@@ -0,0 +1,71 @@
+锘縰sing MES.Service.Dto.service;
+using MES.Service.Modes;
+using MES.Service.service;
+using MES.Service.service.Warehouse;
+using MES.Service.util;
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc;
+using System.Dynamic;
+
+namespace MESApplication.Controllers.Warehouse
+{
+    [Route("api/[controller]")]
+    [ApiController]
+       public class WhqohController : ControllerBase
+    {
+        private readonly whqohDtoManager m = new();
+
+        private readonly MessageCenterManager _manager = new();
+
+        private readonly string METHOD = "POST";
+
+        private readonly string TableName = "Whqoh";
+
+        private readonly string URL = "http://localhost:10054/api/Whqoh/";
+
+        /// <summary>
+        ///     鍒锋柊搴撳瓨
+        /// </summary>
+        /// <returns></returns>
+        [HttpPost("getWhqoh")]
+        public async Task<ResponseResult> getWhqoh(WhqohDto whqohDto)
+        {
+            var entity = new MessageCenter();
+            entity.TableName = TableName;
+            entity.Url = URL + "getWhqoh";
+            entity.Method = METHOD;
+            entity.Data = Newtonsoft.Json.JsonConvert.SerializeObject(whqohDto);
+            entity.Status = 1;
+            entity.CreateBy = "PL017";
+            try
+            {
+                dynamic resultInfos = new ExpandoObject();
+                bool result = await whqohDtoManager.GetWhqoh(whqohDto);
+                resultInfos.tbBillList = result;
+
+                entity.Result = 0;
+                //if (result) entity.Result = 1;
+                //entity.DealWith = 1;
+                _manager.save(entity);
+
+                return new ResponseResult
+                {
+                    status = 0,
+                    message = "OK",
+                    data = resultInfos
+                };
+            }
+            catch (Exception ex)
+            {
+                entity.Result = 0;
+                entity.DealWith = 0;
+                entity.ResultData = ex.Message;
+                _manager.save(entity);
+
+                return ResponseResult.ResponseError(ex);
+            }
+        }
+
+     
+    }
+}
diff --git a/StandardPda/MESApplication/appsettings.json b/StandardPda/MESApplication/appsettings.json
index 7af20e8..235b707 100644
--- a/StandardPda/MESApplication/appsettings.json
+++ b/StandardPda/MESApplication/appsettings.json
@@ -11,11 +11,15 @@
     "TestErpUrl": "http://192.168.11.120:8098/WebService1.asmx/mesToErpinfo",
     "ProductionErpUrl": "http://192.168.11.120:8098/WebService1.asmx/mesToErpinfoFormal",
     //姝e紡搴�
-    //"DataBaseConn": "Data Source = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.104 )(PORT = 1521))(CONNECT_DATA = (SERVICE_NAME = ORCL))); Persist Security Info=True;User ID = zmz_test; Password=zmztest"
+    "DataBaseConn": "Data Source = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.104 )(PORT = 1521))(CONNECT_DATA = (SERVICE_NAME = ORCL))); Persist Security Info=True;User ID = zmz_prd; Password=zmzprd",
     //娴嬭瘯搴擄紙鍙戝竷鏃惰鍒囨崲姝e紡搴擄紝鎻愪氦浠g爜鍕挎彁浜ゆ鏂囦欢锛�
-    "DataBaseConn": "Data Source = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.104 )(PORT = 1521))(CONNECT_DATA = (SERVICE_NAME = ORCL))); Persist Security Info=True;User ID = zmz_test; Password=zmztest",
-    //鎼哄浜戠幆澧冨垏鎹細0娴嬭瘯锛�1姝e紡
-    "XKYamgylchedd": "0"
+    //"DataBaseConn": "Data Source = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.104 )(PORT = 1521))(CONNECT_DATA = (SERVICE_NAME = ORCL))); Persist Security Info=True;User ID = zmz_test; Password=zmztest",
+    ////鎼哄浜戠幆澧冨垏鎹細0娴嬭瘯锛�1姝e紡
+    "XKYamgylchedd": "0",
+    "U9IP": "192.168.1.66:45795" //U9鏈嶅姟鍣↖P鍦板潃,绉戞妧姝e紡
+    //"U9IP": "192.168.1.66:45796" //U9鏈嶅姟鍣↖P鍦板潃,绉戞妧娴嬭瘯
+    //"U9IP": "192.168.1.66:45798" //U9鏈嶅姟鍣↖P鍦板潃,灏忔姝e紡
+    //"U9IP": "192.168.1.66:45797" //U9鏈嶅姟鍣↖P鍦板潃,灏忔娴嬭瘯
 
   }
 }

--
Gitblit v1.9.3