¶Ô±ÈÐÂÎļþ |
| | |
| | | using 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; |
| | | } |
| | | } |
| | | } |
| | | } |