diff --git a/WarehouseMgr/Tnb.WarehouseMgr/DeviceProviderService.cs b/WarehouseMgr/Tnb.WarehouseMgr/DeviceProviderService.cs new file mode 100644 index 00000000..4eb9b83c --- /dev/null +++ b/WarehouseMgr/Tnb.WarehouseMgr/DeviceProviderService.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tnb.WarehouseMgr +{ + /// + /// Wms设备接口提供程序服务类 + /// + public class DeviceProviderService : BaseWareHouseService + { + } +} diff --git a/WarehouseMgr/Tnb.WarehouseMgr/WareHouseService.cs b/WarehouseMgr/Tnb.WarehouseMgr/WareHouseService.cs index c0bef6f3..a8a61455 100644 --- a/WarehouseMgr/Tnb.WarehouseMgr/WareHouseService.cs +++ b/WarehouseMgr/Tnb.WarehouseMgr/WareHouseService.cs @@ -698,7 +698,7 @@ namespace Tnb.WarehouseMgr List results = new() { startObj }; if (points?.Count > 0) { - points.Where(it => result.Contains(it.id)); + //points.Where(it => result.Contains(it.id)); foreach (var i in result) { WmsPointH? point = points?.Find(x => x.id == i); diff --git a/WarehouseMgr/Tnb.WarehouseMgr/WmsSetSortingService.cs b/WarehouseMgr/Tnb.WarehouseMgr/WmsSetSortingService.cs index 3c4b2012..7a34163e 100644 --- a/WarehouseMgr/Tnb.WarehouseMgr/WmsSetSortingService.cs +++ b/WarehouseMgr/Tnb.WarehouseMgr/WmsSetSortingService.cs @@ -77,19 +77,18 @@ namespace Tnb.WarehouseMgr List carryCodes = new(); List carryIds = new(); - - foreach (var os in setSortingDList) { - var OutStockStrategyInput = new OutStockStrategyQuery { - carry_id = carryMat.carry_id, - material_id = os.material_id, - warehouse_id = os.warehouse_id, - code_batch = os.code_batch, + var OutStockStrategyInput = new OutStockStrategyQuery + { + carry_id = carryMat.carry_id, + material_id = os.material_id, + warehouse_id = os.warehouse_id, + code_batch = os.code_batch, }; var outStkCarrys = await _wareHouseService.OutStockStrategy(OutStockStrategyInput); var carryCodesPart = await _db.Queryable().InnerJoin((a, b) => a.id == b.carry_id).InnerJoin((a, b, c) => a.location_id == c.id) - .Where((a, b) => outStkCarrys.Select(x=>x.id).Contains(b.carry_id)) + .Where((a, b) => outStkCarrys.Select(x => x.id).Contains(b.carry_id)) .Select() .ToListAsync(); diff --git a/common/Tnb.Common/Utils/HttpClientHelper.cs b/common/Tnb.Common/Utils/HttpClientHelper.cs new file mode 100644 index 00000000..274ece91 --- /dev/null +++ b/common/Tnb.Common/Utils/HttpClientHelper.cs @@ -0,0 +1,244 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Reflection; +using System.Security.Policy; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using JNPF.Logging; +using Microsoft.AspNetCore.WebUtilities; +using Newtonsoft.Json; + +namespace Tnb.Common.Utils +{ + public class ApiException : Exception + { + public int StatusCode { get; set; } + + public string Content { get; set; } + } + /// + /// Http请求帮助类 + /// added by ly on 20230801 + /// + public class HttpClientHelper + { + private static readonly HttpClient HttpClient = new HttpClient(); + private static Dictionary HttpClientDict = new Dictionary(StringComparer.OrdinalIgnoreCase); + public static string Url { get; private set; } + + public static async Task HttpPostAsync(string url, string clientKey, string postData = null, Dictionary headers = null, string contentType = "application/json", int timeOut = 30) + { + using var client = HttpClientDict[clientKey]; + if (headers != null) + { + foreach (var header in headers) + client.DefaultRequestHeaders.Add(header.Key, header.Value); + } + using HttpContent httpContent = new StringContent(postData, Encoding.UTF8); + if (contentType != null) + httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType); + + var response = await client.PostAsync(url, httpContent); + return await response.Content.ReadAsStringAsync(); + } + + + /// + /// 发起POST异步请求 + /// + /// + /// + /// application/xml、application/json、application/text、application/x-www-form-urlencoded + /// 填充消息头 + /// + public static async Task HttpPostAsync(string url, string postData = null, Dictionary headers = null, string contentType = "application/json", int timeOut = 30) + { + postData ??= ""; + HttpResponseMessage response = null; + HttpClient client = null; + try + { + + if (client == null) + client = new HttpClient(); + + //HttpClient.Timeout = new TimeSpan(0, 0, timeOut); + if (headers != null) + { + foreach (var header in headers) + client.DefaultRequestHeaders.Add(header.Key, header.Value); + } + //var request = new HttpRequestMessage(HttpMethod.Post, url); + //request.Headers.Accept.Clear(); + //request.Headers. + + + using HttpContent httpContent = new StringContent(postData, Encoding.UTF8); + if (contentType != null) + httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType); + + response = await client.PostAsync(url, httpContent); + } + catch (Exception ex) + { + client = new HttpClient(); + } + + return await response.Content.ReadAsStringAsync(); + } + + public static async Task PostFormDataAsync(string url, Dictionary pars) + { + var respJson = ""; + try + { + var content = new FormUrlEncodedContent(pars); + var respBody = await HttpClient.PostAsync(url, content); + respJson = await respBody.Content.ReadAsStringAsync(); + } + catch (Exception ex) + { + Log.Error("调用PostFormDataAsync时出错", ex); + } + return respJson; + } + + + public static async Task GetAsync(string url, Dictionary headers = null, Dictionary pars = null) + { + var reqUri = url; + if (pars?.Count > 0) + { + reqUri = QueryHelpers.AddQueryString(url, pars); + } + if (headers?.Count > 0) + { + foreach (var (k, v) in headers) + { + HttpClient.DefaultRequestHeaders.Add(k, v); + } + } + HttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + var respBody = await HttpClient.GetAsync(reqUri); + var result = await respBody.Content.ReadAsStringAsync(); + return result; + } + + + private static async Task PostStreamAsync(object content, CancellationToken cancellationToken) + { + using (var client = new HttpClient()) + using (var request = new HttpRequestMessage(HttpMethod.Post, Url)) + using (var httpContent = CreateHttpContent(content)) + { + request.Content = httpContent; + + using (var response = await client + .SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken) + .ConfigureAwait(false)) + { + response.EnsureSuccessStatusCode(); + } + } + } + + + private static HttpContent CreateHttpContent(object content) + { + HttpContent httpContent = null; + + if (content != null) + { + var ms = new MemoryStream(); + SerializeJsonIntoStream(content, ms); + ms.Seek(0, SeekOrigin.Begin); + httpContent = new StreamContent(ms); + httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); + } + + return httpContent; + } + + + public static void SerializeJsonIntoStream(object value, Stream stream) + { + using (var sw = new StreamWriter(stream, new UTF8Encoding(false), 1024, true)) + using (var jtw = new JsonTextWriter(sw) { Formatting = Formatting.None }) + { + var js = new JsonSerializer(); + js.Serialize(jtw, value); + jtw.Flush(); + } + } + + + private static T DeserializeJsonFromStream(Stream stream) + { + if (stream == null || stream.CanRead == false) + return default(T); + + using (var sr = new StreamReader(stream)) + using (var jtr = new JsonTextReader(sr)) + { + var js = new JsonSerializer(); + var searchResult = js.Deserialize(jtr); + return searchResult; + } + } + private static async Task StreamToStringAsync(Stream stream) + { + string content = null; + + if (stream != null) + using (var sr = new StreamReader(stream)) + content = await sr.ReadToEndAsync(); + + return content; + } + + private static async Task> DeserializeFromStreamCallAsync(CancellationToken cancellationToken) + { + using (var client = new HttpClient()) + using (var request = new HttpRequestMessage(HttpMethod.Get, Url)) + using (var response = await client.SendAsync(request, cancellationToken)) + { + var stream = await response.Content.ReadAsStreamAsync(); + + if (response.IsSuccessStatusCode) + return DeserializeJsonFromStream>(stream); + + var content = await StreamToStringAsync(stream); + throw new ApiException + { + StatusCode = (int)response.StatusCode, + Content = content + }; + } + } + + private static async Task> DeserializeOptimizedFromStreamCallAsync(CancellationToken cancellationToken) + { + using (var client = new HttpClient()) + using (var request = new HttpRequestMessage(HttpMethod.Get, Url)) + using (var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken)) + { + var stream = await response.Content.ReadAsStreamAsync(); + + if (response.IsSuccessStatusCode) + return DeserializeJsonFromStream>(stream); + + var content = await StreamToStringAsync(stream); + throw new ApiException + { + StatusCode = (int)response.StatusCode, + Content = content + }; + } + } + + } +}