From f93bae5f52585a5b838e65af44c1f239988355ec Mon Sep 17 00:00:00 2001 From: SoulStar Date: Tue, 3 Sep 2024 23:32:28 +0800 Subject: [PATCH 1/3] =?UTF-8?q?add=20-=20=E6=B7=BB=E5=8A=A0=E4=B8=9A?= =?UTF-8?q?=E5=8A=A1=E7=B1=BB=EF=BC=8C=E6=97=A0=E6=B5=8B=E8=AF=95=E7=9B=B2?= =?UTF-8?q?=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Admin.Core.Api/Admin.Core.Api.xml | 111 +++++++- .../Business/MaterialController.cs | 5 +- .../PLTBusiness/Entity/MaterialData.cs | 63 +++++ .../PLTBusiness/Entity/ResponseRoot.cs | 24 ++ .../PLTBusiness/Entity/TokenData.cs | 20 ++ Admin.Core.Api/PLTBusiness/HttpService.cs | 252 ++++++++++++++++++ Admin.Core.Api/PLTBusiness/XlBusiness.cs | 51 ++++ Admin.Core.Api/Program.cs | 3 + Admin.Core.Api/Startup.cs | 9 +- .../IService_New/Ixl_materialServices.cs | 16 +- .../Service_New/xl_materialServices.cs | 75 +++++- 11 files changed, 619 insertions(+), 10 deletions(-) create mode 100644 Admin.Core.Api/PLTBusiness/Entity/MaterialData.cs create mode 100644 Admin.Core.Api/PLTBusiness/Entity/ResponseRoot.cs create mode 100644 Admin.Core.Api/PLTBusiness/Entity/TokenData.cs create mode 100644 Admin.Core.Api/PLTBusiness/HttpService.cs create mode 100644 Admin.Core.Api/PLTBusiness/XlBusiness.cs diff --git a/Admin.Core.Api/Admin.Core.Api.xml b/Admin.Core.Api/Admin.Core.Api.xml index 3ba0122..806a89e 100644 --- a/Admin.Core.Api/Admin.Core.Api.xml +++ b/Admin.Core.Api/Admin.Core.Api.xml @@ -325,7 +325,7 @@ - + 小料同步MES物料信息 @@ -1718,6 +1718,115 @@ DeleteSubscriptionFiles + + + 总页数 + + + + + 页码 + + + + + 数据对象 + + + + + 总记录数 + + + + + 每页记录数 + + + + + 尾页编号 + + + + + 起始页编号 + + + + + 物料名称 + + + + + 物料状态(0禁用, 1启用) + + + + + 物料编号 + + + + + 物料创建时间 + + + + + 状态码 + + + + + 消息提示 + + + + + 结果对象 + + + + + Token(后续请求需要携带这个Token) + + + + + Token失效时间 + + + + + 发起POST异步请求 + + 请求地址 + POST提交的内容 + POST内容的媒体类型,如:application/xml、application/json + HTTP响应上的content-type内容头的值,如:application/xml、application/json、application/text、application/x-www-form-urlencoded等 + 请求头信息 + 请求超时时间,单位秒 + 返回string + + + + 发起GET异步请求 + + 返回类型 + 请求地址 + 请求头信息 + 请求超时时间,单位秒 + 返回string + + + + 获取请求的主机名 + + + + Program diff --git a/Admin.Core.Api/Controllers/Business/MaterialController.cs b/Admin.Core.Api/Controllers/Business/MaterialController.cs index b936c58..eb5401c 100644 --- a/Admin.Core.Api/Controllers/Business/MaterialController.cs +++ b/Admin.Core.Api/Controllers/Business/MaterialController.cs @@ -45,7 +45,6 @@ namespace Admin.Core.Api _xl_materialService = xl_materialServices; _binServices = binServices; _xlPlanServices = xlPlanServices; - _xlPlanServices = xlPlanServices; } #endregion @@ -247,11 +246,11 @@ namespace Admin.Core.Api /// /// [HttpGet] - public async Task> xlDownLoadMateralInfo(string materialCode, string materialName) + public async Task> XlDownLoadMateralInfo(string materialCode, string materialName, int isEnable, DateTime createDateTime) { try { - var result = await _xl_materialService.xlInsertMaterial(materialCode, materialName); + var result = await _xl_materialService.xlInsertMaterial(materialCode, materialName, isEnable, createDateTime); if (result) { return Success(result); diff --git a/Admin.Core.Api/PLTBusiness/Entity/MaterialData.cs b/Admin.Core.Api/PLTBusiness/Entity/MaterialData.cs new file mode 100644 index 0000000..17cd9e6 --- /dev/null +++ b/Admin.Core.Api/PLTBusiness/Entity/MaterialData.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using SqlSugar; + + +namespace Admin.Core.Api.PLTBusiness.Entity +{ + public class MaterialData + { + /// + /// 总页数 + /// + public int pageCount { get; set; } + /// + /// 页码 + /// + public int pageIndex { get; set; } + /// + /// 数据对象 + /// + public List recordList { get; set; } + /// + /// 总记录数 + /// + public int recordCount { get; set; } + /// + /// 每页记录数 + /// + public int pageSize { get; set; } + /// + /// 尾页编号 + /// + public int endPageIndex { get; set; } + /// + /// 起始页编号 + /// + public int beginPageIndex { get; set; } + } + + public class MaterialRecordListItem + { + /// + /// 物料名称 + /// + public string name { get; set; } + /// + /// 物料状态(0禁用, 1启用) + /// + public int status { get; set; } + /// + /// 物料编号 + /// + public string code { get; set; } + /// + /// 物料创建时间 + /// + public string createTime { get; set; } + + } + +} \ No newline at end of file diff --git a/Admin.Core.Api/PLTBusiness/Entity/ResponseRoot.cs b/Admin.Core.Api/PLTBusiness/Entity/ResponseRoot.cs new file mode 100644 index 0000000..deb0d60 --- /dev/null +++ b/Admin.Core.Api/PLTBusiness/Entity/ResponseRoot.cs @@ -0,0 +1,24 @@ +using System; +using System.Linq; +using System.Text; +using SqlSugar; + + +namespace Admin.Core.Api.PLTBusiness.Entity +{ + public class ResponseRoot + { + /// + /// 状态码 + /// + public int code { get; set; } + /// + /// 消息提示 + /// + public string message { get; set; } + /// + /// 结果对象 + /// + public T Data { get; set; } + } +} \ No newline at end of file diff --git a/Admin.Core.Api/PLTBusiness/Entity/TokenData.cs b/Admin.Core.Api/PLTBusiness/Entity/TokenData.cs new file mode 100644 index 0000000..f9be6eb --- /dev/null +++ b/Admin.Core.Api/PLTBusiness/Entity/TokenData.cs @@ -0,0 +1,20 @@ +using System; +using System.Linq; +using System.Text; +using SqlSugar; + + +namespace Admin.Core.Api.PLTBusiness.Entity +{ + public class TokenData + { + /// + /// Token(后续请求需要携带这个Token) + /// + public string token { get; set; } + /// + /// Token失效时间 + /// + public int expiration { get; set; } + } +} \ No newline at end of file diff --git a/Admin.Core.Api/PLTBusiness/HttpService.cs b/Admin.Core.Api/PLTBusiness/HttpService.cs new file mode 100644 index 0000000..271f77e --- /dev/null +++ b/Admin.Core.Api/PLTBusiness/HttpService.cs @@ -0,0 +1,252 @@ +using Admin.Core.Api.PLTBusiness.Entity; +using Admin.Core.IService; +using Admin.Core.Service; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using RabbitMQ.Client; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Security.Policy; +using System.Text; +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; + +namespace Admin.Core.Api.PLTBusiness +{ + + public class HttpService : BackgroundService + { + private readonly IServiceScopeFactory _scopeFactory; + + private string _token; + + private int _tokenExpirationTime; + + private Dictionary _tokenDictionary = new Dictionary() + { + {"authCode", "QT_XXX"}, + {"appid", "q7cloud_XXX"}, + {"secret", "f434eab8b9652524dc719e33f4ff255a45570XXX"} + }; + + private string _host; + + private readonly IHttpClientFactory _httpClientFactory; + + public HttpService(IHttpClientFactory httpClientFactory, IServiceScopeFactory scopeFactory) + { + _httpClientFactory = httpClientFactory; + _scopeFactory = scopeFactory; + } + + protected async override Task ExecuteAsync(CancellationToken stoppingToken) + { + using var scope = _scopeFactory.CreateScope(); + var services = scope.ServiceProvider; + var xlBusiness = services.GetService(); + + while (!stoppingToken.IsCancellationRequested) + { + + //执行任务 + Console.WriteLine($"{DateTime.Now}"); + + await xlBusiness.InsertNewMaterialData(null); + + await Task.Delay(1000*10); + + } + } + + private async void GetToken() + { + try + { + string body = JsonSerializer.Serialize(_tokenDictionary); + string tokenJson = await PostAsync(_host + "/qtn/api/auth/token", body, "application/json", "application/json", null, 10); + ResponseRoot responseRoot = JsonSerializer.Deserialize>(tokenJson); + if (responseRoot == null) + { + Console.WriteLine("Token获取失败!"); + return; + } + if(responseRoot.code != 200) + { + Console.WriteLine("ERROR 状态码:" + responseRoot.code); + return; + } + _token = responseRoot.Data.token; + _tokenExpirationTime = responseRoot.Data.expiration; + } + catch (Exception ex) + { + Console.WriteLine("ERROR: " + ex.ToString()); + } + } + /// + /// 发起POST异步请求 + /// + /// 请求地址 + /// POST提交的内容 + /// POST内容的媒体类型,如:application/xml、application/json + /// HTTP响应上的content-type内容头的值,如:application/xml、application/json、application/text、application/x-www-form-urlencoded等 + /// 请求头信息 + /// 请求超时时间,单位秒 + /// 返回string + private async Task PostAsync(string url, string body, + string bodyMediaType = null, + string responseContentType = null, + Dictionary headers = null, + int timeOut = 30) + { + try + { + var hostName = GetHostName(url); + using (HttpClient client = _httpClientFactory.CreateClient(hostName)) + { + client.Timeout = TimeSpan.FromSeconds(timeOut); + if (headers?.Count > 0) + { + foreach (string key in headers.Keys) + { + client.DefaultRequestHeaders.Add(key, headers[key]); + } + } + StringContent content = new StringContent(body, System.Text.Encoding.UTF8, mediaType: bodyMediaType); + if (!string.IsNullOrWhiteSpace(responseContentType)) + { + content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse(responseContentType); + } + using (HttpResponseMessage response = await client.PostAsync(url, content)) + { + if (response.IsSuccessStatusCode) + { + string responseString = await response.Content.ReadAsStringAsync(); + return responseString; + } + else + { + return string.Empty; + } + } + } + } + catch (Exception ex) + { + Console.WriteLine($"ERROR: {ex.ToString()} Time: {DateTime.Now}"); + return string.Empty; + } + + } + + + /// + /// 发起GET异步请求 + /// + /// 返回类型 + /// 请求地址 + /// 请求头信息 + /// 请求超时时间,单位秒 + /// 返回string + private async Task GetAsync(string url, Dictionary headers = null, int timeOut = 30) + { + try + { + var hostName = GetHostName(url); + using (HttpClient client = _httpClientFactory.CreateClient(hostName)) + { + client.Timeout = TimeSpan.FromSeconds(timeOut); + if (headers?.Count > 0) + { + foreach (string key in headers.Keys) + { + client.DefaultRequestHeaders.Add(key, headers[key]); + } + } + using (HttpResponseMessage response = await client.GetAsync(url)) + { + if (response.IsSuccessStatusCode) + { + string responseString = await response.Content.ReadAsStringAsync(); + return responseString; + } + else + { + return string.Empty; + } + } + } + } + catch (Exception ex) + { + Console.WriteLine($"ERROR: {ex.ToString()} Time: {DateTime.Now}"); + return string.Empty; + } + } + + + + private async Task Post(string url, Dictionary headers = null) + { + var hostName = GetHostName(url); + // 创建 HttpClient 实例 + var client = _httpClientFactory.CreateClient(hostName); + + // 定义要发送的对象 + //var data = new + //{ + // Name = "John Doe", + // Age = 30, + // Email = "john.doe@example.com" + //}; + + // 序列化对象为 JSON + var json = JsonSerializer.Serialize(headers); + + // 创建 HttpContent,设置内容为 JSON 格式 + var content = new StringContent(json, Encoding.UTF8, "application/json"); + + // 发送 POST 请求 + var response = await client.PostAsync(url, content); + + // 检查响应状态码 + if (response.IsSuccessStatusCode) + { + // 读取响应内容 + var responseBody = await response.Content.ReadAsStringAsync(); + Console.WriteLine($"Response: {responseBody}"); + } + else + { + Console.WriteLine($"Error: {response.StatusCode}"); + } + } + + + #region 私有函数 + + /// + /// 获取请求的主机名 + /// + /// + /// + private static string GetHostName(string url) + { + if (!string.IsNullOrWhiteSpace(url)) + { + return url.Replace("https://", "").Replace("http://", "").Split('/')[0]; + } + else + { + return "AnyHost"; + } + } + + #endregion + + + } +} diff --git a/Admin.Core.Api/PLTBusiness/XlBusiness.cs b/Admin.Core.Api/PLTBusiness/XlBusiness.cs new file mode 100644 index 0000000..95ec0f8 --- /dev/null +++ b/Admin.Core.Api/PLTBusiness/XlBusiness.cs @@ -0,0 +1,51 @@ +using Admin.Core.Api.PLTBusiness.Entity; +using Admin.Core.IService; +using Admin.Core.Model; +using Admin.Core.Service; +using Microsoft.Extensions.Hosting; +using RabbitMQ.Client; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Security.Policy; +using System.Text; +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; + +namespace Admin.Core.Api.PLTBusiness +{ + + public class XlBusiness + { + private readonly Ixl_materialServices _xl_materialService; + + public XlBusiness(Ixl_materialServices xl_materialService) + { + _xl_materialService = xl_materialService; + } + + public async Task InsertNewMaterialData(List list) + { + List mlist = new List(); + mlist = await _xl_materialService.xlMaterialList(); + bool state = await _xl_materialService.xlUpdateMaterial("4cbdaa3913a54092bff72e09f13e633f" + , "123412421567" + , "更新213214测试测试" + , 1 + , DateTime.Now); + //foreach (var mesItem in list) + //{ + // foreach(var mItem in mlist) + // { + + // } + //} + + return true; + } + + + } +} diff --git a/Admin.Core.Api/Program.cs b/Admin.Core.Api/Program.cs index 18820c6..e806108 100644 --- a/Admin.Core.Api/Program.cs +++ b/Admin.Core.Api/Program.cs @@ -28,6 +28,8 @@ namespace Admin.Core.Api // web Ӧóֹ߳, ֱرա // 쳣鿴 Log ļµ쳣־ .Run(); + + } /// @@ -58,5 +60,6 @@ namespace Admin.Core.Api // Ĭlog4net.confg builder.AddLog4Net(Path.Combine(Directory.GetCurrentDirectory(), "Log4net.config")); }); + } } diff --git a/Admin.Core.Api/Startup.cs b/Admin.Core.Api/Startup.cs index 04e8b69..5bd9af4 100644 --- a/Admin.Core.Api/Startup.cs +++ b/Admin.Core.Api/Startup.cs @@ -1,3 +1,4 @@ +using Admin.Core.Api.PLTBusiness; using Admin.Core.Common; using Admin.Core.Extensions; using Admin.Core.IService.ISys; @@ -23,6 +24,7 @@ using System.IO; using System.Linq; using System.Reflection; using System.Text; +using TouchSocket.Http; namespace Admin.Core.Api { @@ -103,11 +105,16 @@ namespace Admin.Core.Api options.Cookie.HttpOnly = true; options.Cookie.IsEssential = true; }); - + //עhttp + services.AddHttpClient(); // services.AddJobSetup(); //PLC services.AddPlcSetup(); + //ӷ + services.AddHostedService(); + services.AddScoped(); + //Redis //services.AddRedisCacheSetup(); //Redis diff --git a/Admin.Core.IService/IService_New/Ixl_materialServices.cs b/Admin.Core.IService/IService_New/Ixl_materialServices.cs index 9c70dfc..dae081c 100644 --- a/Admin.Core.IService/IService_New/Ixl_materialServices.cs +++ b/Admin.Core.IService/IService_New/Ixl_materialServices.cs @@ -3,6 +3,7 @@ using Admin.Core.IService; using Admin.Core.Model; using Admin.Core.Model.ViewModels; using System; +using System.Collections.Generic; using System.Threading.Tasks; namespace Admin.Core.IService @@ -31,6 +32,19 @@ namespace Admin.Core.IService Task> MaterialVerification(string barCode); - Task xlInsertMaterial(string materialCode, string materialName); + Task xlInsertMaterial( + string materialCode, + string materialName, + int isEnable, + DateTime createDatetime); + + Task xlUpdateMaterial( + string id, + string materialCode, + string materialName, + int isEnable, + DateTime createDatetime); + + Task> xlMaterialList(); } } \ No newline at end of file diff --git a/Admin.Core.Service/Service_New/xl_materialServices.cs b/Admin.Core.Service/Service_New/xl_materialServices.cs index 86f4626..40b3bbc 100644 --- a/Admin.Core.Service/Service_New/xl_materialServices.cs +++ b/Admin.Core.Service/Service_New/xl_materialServices.cs @@ -633,6 +633,7 @@ namespace Admin.Core.Service #endregion #region 料框防错验证——只验证单料 + public async Task> MaterialVerification(string barCode) { MessageModel messageModel = new MessageModel(); @@ -684,15 +685,20 @@ namespace Admin.Core.Service } #endregion + /// - /// 小料上位机同步插入物料信息 + /// 小料上位机插入物料信息 /// /// /// /// /// /// - public async Task xlInsertMaterial(string materialCode, string materialName) + public async Task xlInsertMaterial( + string materialCode, + string materialName, + int isEnable, + DateTime createDatetime) { try { @@ -701,13 +707,13 @@ namespace Admin.Core.Service ID = Guid.NewGuid().ToString("N"), Material_code = materialCode, Material_name = materialName, - CreateDateTime = DateTime.Now, + CreateDateTime = createDatetime, Remark = null, Batch_number = null, Barcode1 = null, Barcode2 = null, Barcode3 = null, - IsEnable = "是" + IsEnable = isEnable == 1 ? "是" : "否" }; var result = await _xl_materialRepository.Add(xl_m); @@ -726,6 +732,67 @@ namespace Admin.Core.Service } } + /// + /// 小料上位机更新物料信息 + /// + /// + /// + /// + /// + /// + public async Task xlUpdateMaterial( + string id, + string materialCode, + string materialName, + int isEnable, + DateTime createDatetime) + { + try + { + xl_material xl_m = new xl_material() + { + ID = id, + Material_code = materialCode, + Material_name = materialName, + CreateDateTime = createDatetime, + Remark = null, + Batch_number = null, + Barcode1 = null, + Barcode2 = null, + Barcode3 = null, + IsEnable = isEnable == 1 ? "是" : "否" + }; + + bool result = await _xl_materialRepository.UpdateAsync(xl_m, $"ID = '{id}'"); + return result; + } + catch (Exception e) + { + return false; + } + + } + + /// + /// 获取小料上位机物料信息 + /// + /// + /// + /// + /// + /// + public async Task> xlMaterialList() + { + try + { + return await _xl_materialRepository.QueryAsync(); + } + catch (Exception ex) + { + return null; + } + } + } } \ No newline at end of file From f7a39845b18ec85d69f05db5615cb821a6ab3ec7 Mon Sep 17 00:00:00 2001 From: SoulStar Date: Wed, 4 Sep 2024 14:27:42 +0800 Subject: [PATCH 2/3] =?UTF-8?q?add=20-=20=E7=89=A9=E6=96=99=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E5=90=8C=E6=AD=A5=E5=8A=9F=E8=83=BD=E5=AE=8C=E6=88=90?= =?UTF-8?q?=EF=BC=8C=E5=BE=85=E6=B5=8B=E8=AF=95=EF=BC=8C=E9=85=8D=E6=96=B9?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E5=BE=85=E5=AE=8C=E5=96=84=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Admin.Core.Api/Admin.Core.Api.xml | 21 ++ .../Business/MaterialController.cs | 2 +- Admin.Core.Api/Log4net.config | 14 +- .../PLTBusiness/Entity/MaterialData.cs | 4 + Admin.Core.Api/PLTBusiness/HttpService.cs | 200 +++++++++++++++--- Admin.Core.Api/PLTBusiness/XlBusiness.cs | 57 +++-- .../IService_New/Ixl_materialServices.cs | 30 +++ Admin.Core.PlcServer/Impl/SiemensPlc.cs | 2 +- .../Service_New/xl_materialServices.cs | 27 ++- 9 files changed, 303 insertions(+), 54 deletions(-) diff --git a/Admin.Core.Api/Admin.Core.Api.xml b/Admin.Core.Api/Admin.Core.Api.xml index 806a89e..5a5eb24 100644 --- a/Admin.Core.Api/Admin.Core.Api.xml +++ b/Admin.Core.Api/Admin.Core.Api.xml @@ -1753,6 +1753,11 @@ 起始页编号 + + + 物料id + + 物料名称 @@ -1798,6 +1803,22 @@ Token失效时间 + + + 分页拉取所有Mes物料信息 + + + + + + 获取Token + + + + + 刷新Token + + 发起POST异步请求 diff --git a/Admin.Core.Api/Controllers/Business/MaterialController.cs b/Admin.Core.Api/Controllers/Business/MaterialController.cs index eb5401c..c788cf4 100644 --- a/Admin.Core.Api/Controllers/Business/MaterialController.cs +++ b/Admin.Core.Api/Controllers/Business/MaterialController.cs @@ -250,7 +250,7 @@ namespace Admin.Core.Api { try { - var result = await _xl_materialService.xlInsertMaterial(materialCode, materialName, isEnable, createDateTime); + var result = await _xl_materialService.xlInsertMaterial(Guid.NewGuid().ToString("N"), materialCode, materialName, isEnable, createDateTime); if (result) { return Success(result); diff --git a/Admin.Core.Api/Log4net.config b/Admin.Core.Api/Log4net.config index d7e7064..627f877 100644 --- a/Admin.Core.Api/Log4net.config +++ b/Admin.Core.Api/Log4net.config @@ -6,7 +6,7 @@ - + @@ -26,8 +26,6 @@ - - @@ -52,6 +50,15 @@ + + + + + + + + + @@ -61,5 +68,6 @@ + \ No newline at end of file diff --git a/Admin.Core.Api/PLTBusiness/Entity/MaterialData.cs b/Admin.Core.Api/PLTBusiness/Entity/MaterialData.cs index 17cd9e6..05ffac0 100644 --- a/Admin.Core.Api/PLTBusiness/Entity/MaterialData.cs +++ b/Admin.Core.Api/PLTBusiness/Entity/MaterialData.cs @@ -41,6 +41,10 @@ namespace Admin.Core.Api.PLTBusiness.Entity public class MaterialRecordListItem { + /// + /// 物料id + /// + public int id { get; set; } /// /// 物料名称 /// diff --git a/Admin.Core.Api/PLTBusiness/HttpService.cs b/Admin.Core.Api/PLTBusiness/HttpService.cs index 271f77e..9016d90 100644 --- a/Admin.Core.Api/PLTBusiness/HttpService.cs +++ b/Admin.Core.Api/PLTBusiness/HttpService.cs @@ -1,8 +1,12 @@ using Admin.Core.Api.PLTBusiness.Entity; +using Admin.Core.Common; using Admin.Core.IService; +using Admin.Core.PlcServer; using Admin.Core.Service; +using log4net; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using NPOI.HSSF.Record; using RabbitMQ.Client; using System; using System.Collections.Generic; @@ -19,22 +23,17 @@ namespace Admin.Core.Api.PLTBusiness public class HttpService : BackgroundService { - private readonly IServiceScopeFactory _scopeFactory; + private readonly log4net.ILog log = LogManager.GetLogger(typeof(HttpService)); - private string _token; + private readonly IServiceScopeFactory _scopeFactory; - private int _tokenExpirationTime; + private readonly IHttpClientFactory _httpClientFactory; - private Dictionary _tokenDictionary = new Dictionary() - { - {"authCode", "QT_XXX"}, - {"appid", "q7cloud_XXX"}, - {"secret", "f434eab8b9652524dc719e33f4ff255a45570XXX"} - }; + private readonly string _host; - private string _host; + private string _token; - private readonly IHttpClientFactory _httpClientFactory; + private int _tokenExpirationTime = 0; public HttpService(IHttpClientFactory httpClientFactory, IServiceScopeFactory scopeFactory) { @@ -44,25 +43,128 @@ namespace Admin.Core.Api.PLTBusiness protected async override Task ExecuteAsync(CancellationToken stoppingToken) { + int second = 1000 * 10; using var scope = _scopeFactory.CreateScope(); var services = scope.ServiceProvider; var xlBusiness = services.GetService(); - while (!stoppingToken.IsCancellationRequested) { //执行任务 - Console.WriteLine($"{DateTime.Now}"); + //log.Info($"{DateTime.Now}"); + + if (_tokenExpirationTime == 0) + { + if (await GetToken()) + { + log.Info("Token Get! Value:" + _token); + } + else + { + log.Error("Token Get Falied"); + } + + } + else if (_tokenExpirationTime <= 86400) + { + RefreshToken(); + } + else + { + _tokenExpirationTime = _tokenExpirationTime - (second / 1000); + } - await xlBusiness.InsertNewMaterialData(null); + try + { + if (_token.IsNotEmptyOrNull()) + { + await xlBusiness.InsertNewMaterialData(await GetMaterialDataPageList()); + } + else + { + log.Error("No Token!!!"); + } + } + catch (Exception ex) + { + log.Error("Error: " + ex); + } + + await Task.Delay(second); + } - await Task.Delay(1000*10); + } + + #region Mes信息拉取 + /// + /// 分页拉取所有Mes物料信息 + /// + /// + private async Task> GetMaterialDataPageList() + { + int pageSize = 200; + int pageIndex = 0; + int pageCount = 1; + string orgCode = "Mesnac"; + List allMaterials = new List(); + Dictionary _pageInfo = new Dictionary() + { + {"pageIndex", 200}, + {"pageSize", 0}, + {"orgCode", "Mesnac"}, + }; + try + { + do + { + string body = JsonSerializer.Serialize(_pageInfo); + string materialJson = await PostAsync(_host + "/qtn/api/q7/com/inventory/page", + body, "application/json", "application/json", + new Dictionary { { "token", _token } }, 10); + ResponseRoot responseRoot = JsonSerializer.Deserialize>(materialJson); + if (responseRoot == null) + { + log.Error("物料信息拉取失败!"); + return null; + } + if (responseRoot.code != 200) + { + log.Error("ERROR 状态码:" + responseRoot.code); + return null; + } + pageCount = responseRoot.Data.pageCount; + pageIndex = responseRoot.Data.pageIndex; + allMaterials.AddRange(responseRoot.Data.recordList); + pageIndex = pageIndex + 1; + } + while (pageIndex < pageSize); } + catch (Exception ex) + { + log.Error("Mes物料信息拉取失败 Error: " + ex); + return null; + } + + return allMaterials; } - private async void GetToken() + #endregion + + #region Token获取 + + /// + /// 获取Token + /// + private async Task GetToken() { + Dictionary _tokenDictionary = new Dictionary() + { + {"authCode", "QT_XXX"}, + {"appid", "q7cloud_XXX"}, + {"secret", "f434eab8b9652524dc719e33f4ff255a45570XXX"} + }; + try { string body = JsonSerializer.Serialize(_tokenDictionary); @@ -70,22 +172,63 @@ namespace Admin.Core.Api.PLTBusiness ResponseRoot responseRoot = JsonSerializer.Deserialize>(tokenJson); if (responseRoot == null) { - Console.WriteLine("Token获取失败!"); - return; + log.Error("Token获取失败!"); + return false; + } + if (responseRoot.code != 200) + { + log.Error("ERROR 状态码:" + responseRoot.code); + return false; + } + _token = responseRoot.Data.token; + _tokenExpirationTime = responseRoot.Data.expiration; + log.Info("Token获取成功:" + _token + "超时时间:" + _tokenExpirationTime); + return true; + } + catch (Exception ex) + { + log.Error("Token获取失败 ERROR: " + ex.ToString()); + return false; + } + } + + /// + /// 刷新Token + /// + private async Task RefreshToken() + { + try + { + string tokenJson = await PostAsync(_host + "/qtn/api/auth/token/refresh", "" + , "application/json", "application/x-www-form-urlencoded" + , new Dictionary() { { "token", _token } }, 10); + ResponseRoot responseRoot = JsonSerializer.Deserialize>(tokenJson); + if (responseRoot == null) + { + log.Error("Token更新失败!"); + return false; } - if(responseRoot.code != 200) + if (responseRoot.code != 200) { - Console.WriteLine("ERROR 状态码:" + responseRoot.code); - return; + log.Error("ERROR 状态码:" + responseRoot.code); + return false; } _token = responseRoot.Data.token; _tokenExpirationTime = responseRoot.Data.expiration; + log.Info("Token更新成功:" + _token + "超时时间:" + _tokenExpirationTime); + return true; } catch (Exception ex) { - Console.WriteLine("ERROR: " + ex.ToString()); + log.Error("Token更新失败 ERROR: " + ex.ToString()); + return false; } } + + #endregion + + #region HTTP操作 + /// /// 发起POST异步请求 /// @@ -136,8 +279,8 @@ namespace Admin.Core.Api.PLTBusiness } catch (Exception ex) { - Console.WriteLine($"ERROR: {ex.ToString()} Time: {DateTime.Now}"); - return string.Empty; + log.Error($"Post请求失败 URL:{url} ERROR: {ex.ToString()} Time: {DateTime.Now}"); + throw; } } @@ -182,8 +325,8 @@ namespace Admin.Core.Api.PLTBusiness } catch (Exception ex) { - Console.WriteLine($"ERROR: {ex.ToString()} Time: {DateTime.Now}"); - return string.Empty; + log.Error($"Get请求失败 URL:{url} ERROR: {ex.ToString()} Time: {DateTime.Now}"); + throw; } } @@ -217,14 +360,15 @@ namespace Admin.Core.Api.PLTBusiness { // 读取响应内容 var responseBody = await response.Content.ReadAsStringAsync(); - Console.WriteLine($"Response: {responseBody}"); + log.Info($"Response: {responseBody}"); } else { - Console.WriteLine($"Error: {response.StatusCode}"); + log.Error($"Error: {response.StatusCode}"); } } + #endregion #region 私有函数 diff --git a/Admin.Core.Api/PLTBusiness/XlBusiness.cs b/Admin.Core.Api/PLTBusiness/XlBusiness.cs index 95ec0f8..67ef605 100644 --- a/Admin.Core.Api/PLTBusiness/XlBusiness.cs +++ b/Admin.Core.Api/PLTBusiness/XlBusiness.cs @@ -26,26 +26,51 @@ namespace Admin.Core.Api.PLTBusiness _xl_materialService = xl_materialService; } + /// + /// 向物料数据库中动态同步mes数据,没有就插入,不同就修改 + /// + /// + /// public async Task InsertNewMaterialData(List list) { - List mlist = new List(); - mlist = await _xl_materialService.xlMaterialList(); - bool state = await _xl_materialService.xlUpdateMaterial("4cbdaa3913a54092bff72e09f13e633f" - , "123412421567" - , "更新213214测试测试" - , 1 - , DateTime.Now); - //foreach (var mesItem in list) - //{ - // foreach(var mItem in mlist) - // { + try + { + foreach (var mesMaterial in list) + { + // Step 3: 在B表中查询是否存在相同ID的记录 + var material = await _xl_materialService.SelectXlMaterialById(mesMaterial.id.ToString()); - // } - //} - - return true; + if (material == null) + { + // Step 4: 如果B表中不存在该物料,插入新物料 + await _xl_materialService.xlInsertMaterial( + mesMaterial.id.ToString(), + mesMaterial.code, mesMaterial.name, mesMaterial.status, + Convert.ToDateTime(mesMaterial.createTime)); + } + else + { + // Step 5: 如果B表中存在该物料且有更新,更新物料信息 + if (material.Material_code != mesMaterial.code || + material.Material_name != mesMaterial.name || + material.IsEnable != (mesMaterial.status == 1 ? "是" : "否") || + material.CreateDateTime != Convert.ToDateTime(mesMaterial.createTime)) + { + await _xl_materialService.xlUpdateMaterial( + mesMaterial.id.ToString(), + mesMaterial.code, mesMaterial.name, mesMaterial.status, + Convert.ToDateTime(mesMaterial.createTime)); + } + } + } + return true; + } + catch (Exception ex) + { + throw; + } } - + } } diff --git a/Admin.Core.IService/IService_New/Ixl_materialServices.cs b/Admin.Core.IService/IService_New/Ixl_materialServices.cs index dae081c..c559437 100644 --- a/Admin.Core.IService/IService_New/Ixl_materialServices.cs +++ b/Admin.Core.IService/IService_New/Ixl_materialServices.cs @@ -32,12 +32,31 @@ namespace Admin.Core.IService Task> MaterialVerification(string barCode); + /// + /// 物料插入 + /// + /// + /// + /// + /// + /// + /// Task xlInsertMaterial( + string id, string materialCode, string materialName, int isEnable, DateTime createDatetime); + /// + /// 物料更新 + /// + /// + /// + /// + /// + /// + /// Task xlUpdateMaterial( string id, string materialCode, @@ -45,6 +64,17 @@ namespace Admin.Core.IService int isEnable, DateTime createDatetime); + /// + /// 物料查询 + /// + /// Task> xlMaterialList(); + + /// + /// 根据ID查询 + /// + /// + /// + Task SelectXlMaterialById(string id); } } \ No newline at end of file diff --git a/Admin.Core.PlcServer/Impl/SiemensPlc.cs b/Admin.Core.PlcServer/Impl/SiemensPlc.cs index 393ce05..042b6bf 100644 --- a/Admin.Core.PlcServer/Impl/SiemensPlc.cs +++ b/Admin.Core.PlcServer/Impl/SiemensPlc.cs @@ -507,7 +507,7 @@ namespace Admin.Core.PlcServer { siemensS7Net.ReceiveTimeOut = 2000; OperateResult read = await siemensS7Net.ReadBoolAsync(address); - log.Info($"心跳方法是否成功:{read.IsSuccess};反馈:{read.Message}"); + //log.Info($"心跳方法是否成功:{read.IsSuccess};反馈:{read.Message}"); if (read.IsSuccess) { IsConnected = true; diff --git a/Admin.Core.Service/Service_New/xl_materialServices.cs b/Admin.Core.Service/Service_New/xl_materialServices.cs index 40b3bbc..ec54597 100644 --- a/Admin.Core.Service/Service_New/xl_materialServices.cs +++ b/Admin.Core.Service/Service_New/xl_materialServices.cs @@ -695,6 +695,7 @@ namespace Admin.Core.Service /// /// public async Task xlInsertMaterial( + string id, string materialCode, string materialName, int isEnable, @@ -704,7 +705,7 @@ namespace Admin.Core.Service { xl_material xl_m = new xl_material() { - ID = Guid.NewGuid().ToString("N"), + ID = id, Material_code = materialCode, Material_name = materialName, CreateDateTime = createDatetime, @@ -728,7 +729,7 @@ namespace Admin.Core.Service } catch (Exception ex) { - return false; + throw; } } @@ -768,7 +769,7 @@ namespace Admin.Core.Service } catch (Exception e) { - return false; + throw; } } @@ -789,10 +790,26 @@ namespace Admin.Core.Service } catch (Exception ex) { - return null; + throw; } } - + /// + /// 根据ID查询物料信息 + /// + /// + /// + /// + public async Task SelectXlMaterialById(string id) + { + try + { + return await _xl_materialRepository.QueryByIdAsync(id); + } + catch (Exception ex) + { + throw; + } + } } } \ No newline at end of file From ce44035e7fa50b0dd2a6582d586601b1d6ef7d2d Mon Sep 17 00:00:00 2001 From: SoulStar Date: Thu, 31 Oct 2024 16:25:02 +0800 Subject: [PATCH 3/3] =?UTF-8?q?add=20-=20=E6=B7=BB=E5=8A=A0=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=EF=BC=8C=E7=89=A9=E6=96=99=E5=90=8C=E6=AD=A5=E5=87=86?= =?UTF-8?q?=E5=A4=87=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Admin.Core.Api/Admin.Core.Api.xml | 101 ++++++- .../Business/MaterialController.cs | 9 +- .../Business/WarehouseController.cs | 69 ++++- Admin.Core.Api/PLTBusiness/Entity/BomData.cs | 67 +++++ .../PLTBusiness/Entity/LRWeighData.cs | 24 ++ Admin.Core.Api/PLTBusiness/HttpService.cs | 66 +++- Admin.Core.Api/Startup.cs | 6 +- .../IService_New/IHw_BarrelServices.cs | 22 ++ .../IService_New/ILR_weighServices.cs | 8 +- .../IService_New/Ixl_recipeServices.cs | 28 ++ Admin.Core.Model/Model_New/xl_recipe.cs | 284 +++++++++--------- .../Service_New/Hw_BarrelServices.cs | 36 +++ .../Service_New/Hw_WarehouseServices.cs | 4 +- .../Service_New/LR_weighServices.cs | 18 ++ .../Service_New/xl_recipeServices.cs | 71 +++++ 15 files changed, 647 insertions(+), 166 deletions(-) create mode 100644 Admin.Core.Api/PLTBusiness/Entity/BomData.cs create mode 100644 Admin.Core.Api/PLTBusiness/Entity/LRWeighData.cs create mode 100644 Admin.Core.IService/IService_New/IHw_BarrelServices.cs create mode 100644 Admin.Core.Service/Service_New/Hw_BarrelServices.cs diff --git a/Admin.Core.Api/Admin.Core.Api.xml b/Admin.Core.Api/Admin.Core.Api.xml index 5a5eb24..10d1833 100644 --- a/Admin.Core.Api/Admin.Core.Api.xml +++ b/Admin.Core.Api/Admin.Core.Api.xml @@ -390,12 +390,24 @@ 投料仓服务 - + + + 料筒服务类 + + + + + 称重服务类 + + + 构造方法 + + @@ -442,6 +454,13 @@ + + + 根据罐号查询称量列表 + + + + Controller代码生成 @@ -1718,6 +1737,66 @@ DeleteSubscriptionFiles + + + 总页数 + + + + + 页码 + + + + + 数据对象 + + + + + 总记录数 + + + + + 每页记录数 + + + + + 尾页编号 + + + + + 起始页编号 + + + + + 物料id + + + + + 物料名称 + + + + + 物料状态(0禁用, 1启用) + + + + + 物料编号 + + + + + 物料创建时间 + + 总页数 @@ -1803,12 +1882,25 @@ Token失效时间 + + + 循环执行的服务 + + + + 分页拉取所有Mes物料信息 + + + 分页拉取所有MesBom配方信息 + + + 获取Token @@ -1848,6 +1940,13 @@ + + + 向物料数据库中动态同步mes数据,没有就插入,不同就修改 + + + + Program diff --git a/Admin.Core.Api/Controllers/Business/MaterialController.cs b/Admin.Core.Api/Controllers/Business/MaterialController.cs index c788cf4..442e820 100644 --- a/Admin.Core.Api/Controllers/Business/MaterialController.cs +++ b/Admin.Core.Api/Controllers/Business/MaterialController.cs @@ -253,14 +253,17 @@ namespace Admin.Core.Api var result = await _xl_materialService.xlInsertMaterial(Guid.NewGuid().ToString("N"), materialCode, materialName, isEnable, createDateTime); if (result) { - return Success(result); + return Success(result, "成功"); + } + else + { + return Failed("执行失败!"); } } catch (Exception ex) { - + return Failed("执行失败!"); } - return Failed("执行失败!"); } } } \ No newline at end of file diff --git a/Admin.Core.Api/Controllers/Business/WarehouseController.cs b/Admin.Core.Api/Controllers/Business/WarehouseController.cs index a7d1dee..3437715 100644 --- a/Admin.Core.Api/Controllers/Business/WarehouseController.cs +++ b/Admin.Core.Api/Controllers/Business/WarehouseController.cs @@ -15,6 +15,8 @@ using Admin.Core.Model.ViewModels; using Microsoft.AspNetCore.Mvc.ViewEngines; using System.Collections; using NPOI.SS.Formula.Functions; +using System.Numerics; +using Admin.Core.IService.IService_New; namespace Admin.Core.Api.Controllers.Business { @@ -38,16 +40,30 @@ namespace Admin.Core.Api.Controllers.Business /// private readonly IHw_WarehouseServices _hw_WarehouseService; + /// + /// 料筒服务类 + /// + private readonly IHw_BarrelServices _hw_BarrelService; + + /// + /// 称重服务类 + /// + private readonly ILR_weighServices _lr_weighService; + #region 构造方法 /// /// 构造方法 /// /// /// - public WarehouseController(IHw_WarehouseServices hw_WarehouseServices, ISysUserService sysUserService) : base(sysUserService) + /// + /// + public WarehouseController(IHw_WarehouseServices hw_WarehouseServices, ISysUserService sysUserService, IHw_BarrelServices hwBarrelService, ILR_weighServices lrweighService) : base(sysUserService) { _sysUserService = sysUserService; _hw_WarehouseService = hw_WarehouseServices; + _hw_BarrelService = hwBarrelService; + _lr_weighService = lrweighService; } #endregion @@ -61,13 +77,13 @@ namespace Admin.Core.Api.Controllers.Business /// 料桶Id /// [HttpGet] - public async Task> GetByBarrelInfo(string kettleBarCode, string planId,string code) + public async Task> GetByBarrelInfo(string kettleBarCode, string planId, string code) { if (!code.IsNotEmptyOrNull()) { return Failed("传入参数为空!"); } - var query = await _hw_WarehouseService.GetXlInfo(kettleBarCode.Trim(),planId.Trim(),code.Trim()); + var query = await _hw_WarehouseService.GetXlInfo(kettleBarCode.Trim(), planId.Trim(), code.Trim()); if (query == null) { return Failed("查询失败!"); @@ -84,7 +100,7 @@ namespace Admin.Core.Api.Controllers.Business /// 计划Id /// [HttpGet] - public async Task> GetByCode(string code,string planId) + public async Task> GetByCode(string code, string planId) { if (!code.IsNotEmptyOrNull()) @@ -96,11 +112,11 @@ namespace Admin.Core.Api.Controllers.Business return Failed("传入计划ID参数为空!"); } var query = await _hw_WarehouseService.QueryByCode(code.Trim(), planId); - - if (query==null) + + if (query == null) { return Failed("查询失败!"); - } + } return Success(query); } #endregion @@ -121,7 +137,7 @@ namespace Admin.Core.Api.Controllers.Business var result = await _hw_WarehouseService.UpdatePlcState(view); if (result) { - return Success(result); + return Success(result); } return Failed("执行失败!"); } @@ -162,7 +178,7 @@ namespace Admin.Core.Api.Controllers.Business ////产品名称 //var ss3 = "D1-813"; - + } catch (Exception ex) { @@ -179,13 +195,13 @@ namespace Admin.Core.Api.Controllers.Business /// /// [HttpGet] - public async Task> ExecPlcState(string code,int state) + public async Task> ExecPlcState(string code, int state) { if (!code.IsNotEmptyOrNull() || !state.IsNotEmptyOrNull()) { return Failed("传入参数为空!"); } - var result = await _hw_WarehouseService.UpdatePlcState(code,state); + var result = await _hw_WarehouseService.UpdatePlcState(code, state); if (result) { return Success(result); @@ -208,7 +224,7 @@ namespace Admin.Core.Api.Controllers.Business var result = await _hw_WarehouseService.GetPlanWeightInfo(); if (result != null) { - return Success(result,"成功"); + return Success(result, "成功"); } return Failed>("执行失败!"); } @@ -234,7 +250,34 @@ namespace Admin.Core.Api.Controllers.Business messageModel.success = true; messageModel.data = list; return Success(list); - } + } #endregion + + /// + /// 根据罐号查询称量列表 + /// + /// + /// + [HttpGet] + public async Task>> GetWeightInfoByBarrelID(string barrelBarcode) + { + if (!barrelBarcode.IsNotEmptyOrNull()) + { + return Failed>("传入参数为空!"); + } + MessageModel> messageModel = new MessageModel>(); + string planID = _hw_BarrelService.GetPlanIdByBarrelID(barrelBarcode.Trim()); + if (string.IsNullOrEmpty(planID)) + { + return Failed>("查询不到罐号对应的计划ID!"); + } + List list = _lr_weighService.GetRecentWeightList(planID); + if (list != null) + { + return Success(list); + } + return Failed>("执行失败!"); + + } } } \ No newline at end of file diff --git a/Admin.Core.Api/PLTBusiness/Entity/BomData.cs b/Admin.Core.Api/PLTBusiness/Entity/BomData.cs new file mode 100644 index 0000000..3a9e5b0 --- /dev/null +++ b/Admin.Core.Api/PLTBusiness/Entity/BomData.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using SqlSugar; + + +namespace Admin.Core.Api.PLTBusiness.Entity +{ + public class BomData + { + /// + /// 总页数 + /// + public int pageCount { get; set; } + /// + /// 页码 + /// + public int pageIndex { get; set; } + /// + /// 数据对象 + /// + public List recordList { get; set; } + /// + /// 总记录数 + /// + public int recordCount { get; set; } + /// + /// 每页记录数 + /// + public int pageSize { get; set; } + /// + /// 尾页编号 + /// + public int endPageIndex { get; set; } + /// + /// 起始页编号 + /// + public int beginPageIndex { get; set; } + } + + public class BomRecordListItem + { + /// + /// 物料id + /// + public int id { get; set; } + /// + /// 物料名称 + /// + public string name { get; set; } + /// + /// 物料状态(0禁用, 1启用) + /// + public int status { get; set; } + /// + /// 物料编号 + /// + public string code { get; set; } + /// + /// 物料创建时间 + /// + public string createTime { get; set; } + + } + +} \ No newline at end of file diff --git a/Admin.Core.Api/PLTBusiness/Entity/LRWeighData.cs b/Admin.Core.Api/PLTBusiness/Entity/LRWeighData.cs new file mode 100644 index 0000000..d0a3f18 --- /dev/null +++ b/Admin.Core.Api/PLTBusiness/Entity/LRWeighData.cs @@ -0,0 +1,24 @@ +using System; + +namespace Admin.Core.Api.PLTBusiness.Entity +{ + public class LRWeighData + { + public string BinSerial { get; set; } + + + public string SetWeight { get; set; } + + + public string RealWeight { get; set; } + + + public string MainID { get; set; } + + + public DateTime EndTime { get; set; } + + + + } +} diff --git a/Admin.Core.Api/PLTBusiness/HttpService.cs b/Admin.Core.Api/PLTBusiness/HttpService.cs index 9016d90..50df55a 100644 --- a/Admin.Core.Api/PLTBusiness/HttpService.cs +++ b/Admin.Core.Api/PLTBusiness/HttpService.cs @@ -29,18 +29,26 @@ namespace Admin.Core.Api.PLTBusiness private readonly IHttpClientFactory _httpClientFactory; - private readonly string _host; + private readonly Ixl_recipeServices _xlrecipeServices; + + private readonly string _host = "http://192.168.111.141:8090"; private string _token; private int _tokenExpirationTime = 0; - public HttpService(IHttpClientFactory httpClientFactory, IServiceScopeFactory scopeFactory) + public HttpService(IHttpClientFactory httpClientFactory, IServiceScopeFactory scopeFactory, Ixl_recipeServices xlrecipeServices) { _httpClientFactory = httpClientFactory; _scopeFactory = scopeFactory; + _xlrecipeServices = xlrecipeServices; } + /// + /// 循环执行的服务 + /// + /// + /// protected async override Task ExecuteAsync(CancellationToken stoppingToken) { int second = 1000 * 10; @@ -76,9 +84,11 @@ namespace Admin.Core.Api.PLTBusiness try { + //物料信息同步 if (_token.IsNotEmptyOrNull()) { await xlBusiness.InsertNewMaterialData(await GetMaterialDataPageList()); + //await _xlrecipeServices.xlUpdateOrAdd(); } else { @@ -149,6 +159,58 @@ namespace Admin.Core.Api.PLTBusiness return allMaterials; } + /// + /// 分页拉取所有MesBom配方信息 + /// + /// + private async Task> GetRecipeDataPageList() + { + int pageSize = 200; + int pageIndex = 0; + int pageCount = 1; + string orgCode = "Mesnac"; + List allBoms = new List(); + Dictionary _pageInfo = new Dictionary() + { + {"pageIndex", 200}, + {"pageSize", 0}, + {"orgCode", "Mesnac"}, + }; + try + { + do + { + string body = JsonSerializer.Serialize(_pageInfo); + string bomJson = await PostAsync(_host + "/qtn/api/q7/com/bom/page", + body, "application/json", "application/json", + new Dictionary { { "token", _token } }, 10); + ResponseRoot responseRoot = JsonSerializer.Deserialize>(bomJson); + if (responseRoot == null) + { + log.Error("Bom信息拉取失败!"); + return null; + } + if (responseRoot.code != 200) + { + log.Error("ERROR 状态码:" + responseRoot.code); + return null; + } + pageCount = responseRoot.Data.pageCount; + pageIndex = responseRoot.Data.pageIndex; + allBoms.AddRange(responseRoot.Data.recordList); + pageIndex = pageIndex + 1; + } + while (pageIndex < pageSize); + } + catch (Exception ex) + { + log.Error("MesBom信息拉取失败 Error: " + ex); + return null; + } + + return allBoms; + } + #endregion #region Token获取 diff --git a/Admin.Core.Api/Startup.cs b/Admin.Core.Api/Startup.cs index 5bd9af4..e3d7e2f 100644 --- a/Admin.Core.Api/Startup.cs +++ b/Admin.Core.Api/Startup.cs @@ -42,7 +42,7 @@ namespace Admin.Core.Api { Configuration = configuration; Env = env; - + } /// @@ -112,8 +112,8 @@ namespace Admin.Core.Api //PLC services.AddPlcSetup(); //ӷ - services.AddHostedService(); - services.AddScoped(); + //services.AddHostedService(); + //services.AddScoped(); //Redis //services.AddRedisCacheSetup(); diff --git a/Admin.Core.IService/IService_New/IHw_BarrelServices.cs b/Admin.Core.IService/IService_New/IHw_BarrelServices.cs new file mode 100644 index 0000000..c640c97 --- /dev/null +++ b/Admin.Core.IService/IService_New/IHw_BarrelServices.cs @@ -0,0 +1,22 @@ +using Admin.Core.Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Admin.Core.IService.IService_New +{ + /// + /// 料筒服务类 + /// + public interface IHw_BarrelServices : IBaseServices + { + /// + /// 根据罐号获取料罐ID + /// + /// + /// + string GetPlanIdByBarrelID(string barrelBarcode); + } +} diff --git a/Admin.Core.IService/IService_New/ILR_weighServices.cs b/Admin.Core.IService/IService_New/ILR_weighServices.cs index d55505f..e3e0fcc 100644 --- a/Admin.Core.IService/IService_New/ILR_weighServices.cs +++ b/Admin.Core.IService/IService_New/ILR_weighServices.cs @@ -1,5 +1,6 @@ using Admin.Core.IService; using Admin.Core.Model; +using System.Collections.Generic; namespace Admin.Core.IService { @@ -8,5 +9,10 @@ namespace Admin.Core.IService /// public interface ILR_weighServices :IBaseServices { - } + /// + /// 通过计划号获取最近一次的称量列表 + /// + /// + List GetRecentWeightList(string planCode); + } } \ No newline at end of file diff --git a/Admin.Core.IService/IService_New/Ixl_recipeServices.cs b/Admin.Core.IService/IService_New/Ixl_recipeServices.cs index 0b8e2d9..da6f8a1 100644 --- a/Admin.Core.IService/IService_New/Ixl_recipeServices.cs +++ b/Admin.Core.IService/IService_New/Ixl_recipeServices.cs @@ -1,5 +1,7 @@ using Admin.Core.IService; using Admin.Core.Model; +using System.Collections.Generic; +using System.Threading.Tasks; namespace Admin.Core.IService { @@ -8,5 +10,31 @@ namespace Admin.Core.IService /// public interface Ixl_recipeServices :IBaseServices { + /// + /// 新增配方信息 + /// + /// + /// + Task xlInsertRecipe(xl_recipe recipe); + + /// + /// 更新配方信息 + /// + /// + /// + Task xlUpdateRecipe(xl_recipe recipe); + + /// + /// 获取配方列表 + /// + /// + Task> xlGetAllRecipes(); + + /// + /// 如果有记录就更新,没有记录就新增 + /// + /// + /// + Task xlUpdateOrAdd(xl_recipe recipe); } } \ No newline at end of file diff --git a/Admin.Core.Model/Model_New/xl_recipe.cs b/Admin.Core.Model/Model_New/xl_recipe.cs index c877469..f7c5c12 100644 --- a/Admin.Core.Model/Model_New/xl_recipe.cs +++ b/Admin.Core.Model/Model_New/xl_recipe.cs @@ -9,149 +9,149 @@ namespace Admin.Core.Model /// ///配方信息 /// - [SugarTable( "xl_recipe", "cwss_xl")] + [SugarTable("xl_recipe", "cwss_xl")] public class xl_recipe { - public xl_recipe() - { - } - /// - /// Desc: - /// Default: - /// Nullable:False - /// -public string ID { get; set; } - /// - /// Desc:机台号 - /// Default: - /// Nullable:True - /// -public string Equip_Code { get; set; } - /// - /// Desc:配方编码 - /// Default: - /// Nullable:True - /// -public string Recipe_Code { get; set; } - /// - /// Desc:配方版本号 - /// Default: - /// Nullable:True - /// -public string Version { get; set; } - /// - /// Desc:配方别名 - /// Default: - /// Nullable:True - /// -public string Recipe_Name { get; set; } - /// - /// Desc:配方版本号 - /// Default: - /// Nullable:True - /// -public string Mixer_line { get; set; } - /// - /// Desc:配方类型 - /// Default: - /// Nullable:True - /// -public int? Recipe_type { get; set; } - /// - /// Desc:配方习惯 - /// Default: - /// Nullable:True - /// -public int? Recipe_Used { get; set; } - /// - /// Desc:配方验证 - /// Default:0 - /// Nullable:True - /// -public int? Recipe_Verify { get; set; } - /// - /// Desc:总计重量 - /// Default: - /// Nullable:True - /// -public decimal? Total_Weight { get; set; } - /// - /// Desc:总计误差 - /// Default: - /// Nullable:True - /// -public decimal? Total_Error { get; set; } - /// - /// Desc:结束时间 - /// Default: - /// Nullable:True - /// -public string End_datetime { get; set; } - /// - /// Desc:备注 - /// Default: - /// Nullable:True - /// -public string Remark { get; set; } - /// - /// Desc:附件用户 - /// Default: - /// Nullable:True - /// -public int? Attach_User { get; set; } - /// - /// Desc:分组袋 - /// Default: - /// Nullable:True - /// -public int? GroupBags { get; set; } - /// - /// Desc: - /// Default: - /// Nullable:True - /// -public int? IF_FLAG { get; set; } - /// - /// Desc:有效性 - /// Default: - /// Nullable:True - /// -public int? Validity { get; set; } - /// - /// Desc:有效天数 - /// Default: - /// Nullable:True - /// -public int? ValidDays { get; set; } - /// - /// Desc: - /// Default: - /// Nullable:True - /// -public string UpdateByID { get; set; } - /// - /// Desc: - /// Default: - /// Nullable:True - /// -public string UpdateBy { get; set; } - /// - /// Desc: - /// Default: - /// Nullable:True - /// -public string CreatorID { get; set; } - /// - /// Desc: - /// Default: - /// Nullable:True - /// -public string Creator { get; set; } - /// - /// Desc: - /// Default: - /// Nullable:True - /// -public DateTime? CreateDateTime { get; set; } + public xl_recipe() + { + } + /// + /// Desc: + /// Default: + /// Nullable:False + /// + public string ID { get; set; } + /// + /// Desc:机台号 + /// Default: + /// Nullable:True + /// + public string Equip_Code { get; set; } + /// + /// Desc:配方编码 + /// Default: + /// Nullable:True + /// + public string Recipe_Code { get; set; } + /// + /// Desc:配方版本号 + /// Default: + /// Nullable:True + /// + public string Version { get; set; } + /// + /// Desc:配方别名 + /// Default: + /// Nullable:True + /// + public string Recipe_Name { get; set; } + /// + /// Desc:配方版本号 + /// Default: + /// Nullable:True + /// + public string Mixer_line { get; set; } + /// + /// Desc:配方类型 + /// Default: + /// Nullable:True + /// + public int? Recipe_type { get; set; } + /// + /// Desc:配方习惯 + /// Default: + /// Nullable:True + /// + public int? Recipe_Used { get; set; } + /// + /// Desc:配方验证 + /// Default:0 + /// Nullable:True + /// + public int? Recipe_Verify { get; set; } + /// + /// Desc:总计重量 + /// Default: + /// Nullable:True + /// + public decimal? Total_Weight { get; set; } + /// + /// Desc:总计误差 + /// Default: + /// Nullable:True + /// + public decimal? Total_Error { get; set; } + /// + /// Desc:结束时间 + /// Default: + /// Nullable:True + /// + public string End_datetime { get; set; } + /// + /// Desc:备注 + /// Default: + /// Nullable:True + /// + public string Remark { get; set; } + /// + /// Desc:附件用户 + /// Default: + /// Nullable:True + /// + public int? Attach_User { get; set; } + /// + /// Desc:分组袋 + /// Default: + /// Nullable:True + /// + public int? GroupBags { get; set; } + /// + /// Desc: + /// Default: + /// Nullable:True + /// + public int? IF_FLAG { get; set; } + /// + /// Desc:有效性 + /// Default: + /// Nullable:True + /// + public int? Validity { get; set; } + /// + /// Desc:有效天数 + /// Default: + /// Nullable:True + /// + public int? ValidDays { get; set; } + /// + /// Desc: + /// Default: + /// Nullable:True + /// + public string UpdateByID { get; set; } + /// + /// Desc: + /// Default: + /// Nullable:True + /// + public string UpdateBy { get; set; } + /// + /// Desc: + /// Default: + /// Nullable:True + /// + public string CreatorID { get; set; } + /// + /// Desc: + /// Default: + /// Nullable:True + /// + public string Creator { get; set; } + /// + /// Desc: + /// Default: + /// Nullable:True + /// + public DateTime? CreateDateTime { get; set; } } } \ No newline at end of file diff --git a/Admin.Core.Service/Service_New/Hw_BarrelServices.cs b/Admin.Core.Service/Service_New/Hw_BarrelServices.cs new file mode 100644 index 0000000..c1644d7 --- /dev/null +++ b/Admin.Core.Service/Service_New/Hw_BarrelServices.cs @@ -0,0 +1,36 @@ +using Admin.Core.IRepository; +using Admin.Core.IService; +using Admin.Core.IService.IService_New; +using Admin.Core.Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Admin.Core.Service.Service_New +{ + public class Hw_BarrelServices : BaseServices, IHw_BarrelServices + { + + private readonly IBaseRepository _dal; + public Hw_BarrelServices(IBaseRepository dal) + { + this._dal = dal; + base.BaseDal = dal; + } + + public string GetPlanIdByBarrelID(string barrelBarcode) + { + try + { + string planId = _dal.Query(x => x.BarCode == barrelBarcode).FirstOrDefault().PlanId.ToString(); + return planId; + } + catch + { + throw; + } + } + } +} diff --git a/Admin.Core.Service/Service_New/Hw_WarehouseServices.cs b/Admin.Core.Service/Service_New/Hw_WarehouseServices.cs index 6915a25..e833458 100644 --- a/Admin.Core.Service/Service_New/Hw_WarehouseServices.cs +++ b/Admin.Core.Service/Service_New/Hw_WarehouseServices.cs @@ -36,7 +36,9 @@ namespace Admin.Core.Service public Hw_WarehouseServices(IBaseRepository dal, IHw_WarehouseRepository wareHouse, IHw_WareHouse_SubRepository wareHouse_Sub, IHw_FeedReportRepository feed, IHw_BarrelRepository barrel, Ixl_materialRepository materialRepository, - Ixl_recipeRepository recipeRepository, ILR_weighRepository lrWeighRepository, Ixl_planRepository planRepository, IXLPlan_weightRepository IXLPlan_weightRepository, IXLPlan_weightRepository xLPlan_weightRepository) + Ixl_recipeRepository recipeRepository, ILR_weighRepository lrWeighRepository, + Ixl_planRepository planRepository, IXLPlan_weightRepository IXLPlan_weightRepository, + IXLPlan_weightRepository xLPlan_weightRepository) { this._dal = dal; base.BaseDal = dal; diff --git a/Admin.Core.Service/Service_New/LR_weighServices.cs b/Admin.Core.Service/Service_New/LR_weighServices.cs index 62767e6..bfdd653 100644 --- a/Admin.Core.Service/Service_New/LR_weighServices.cs +++ b/Admin.Core.Service/Service_New/LR_weighServices.cs @@ -2,6 +2,9 @@ using Admin.Core.Service; using Admin.Core.IService; using Admin.Core.Model; +using System.Collections.Generic; +using System.Linq; +using System; namespace Admin.Core.Service { @@ -13,5 +16,20 @@ namespace Admin.Core.Service this._dal = dal; base.BaseDal = dal; } + + public List GetRecentWeightList(string planCode) + { + try + { + List list = _dal.Query(x => x.Plan_ID == planCode).ToList(); + int maxMainID = list.Max(x => x.MainId); + List groupList = list.Where(x => x.MainId == maxMainID).ToList(); + return groupList; + } + catch + { + throw; + } + } } } \ No newline at end of file diff --git a/Admin.Core.Service/Service_New/xl_recipeServices.cs b/Admin.Core.Service/Service_New/xl_recipeServices.cs index d888f81..c613aa9 100644 --- a/Admin.Core.Service/Service_New/xl_recipeServices.cs +++ b/Admin.Core.Service/Service_New/xl_recipeServices.cs @@ -2,6 +2,13 @@ using Admin.Core.Service; using Admin.Core.IService; using Admin.Core.Model; +using System.Threading.Tasks; +using System.Collections.Generic; +using System; +using Serilog; +using NPOI.SS.Formula.Functions; +using System.Linq; +using Microsoft.Extensions.Logging; namespace Admin.Core.Service { @@ -13,5 +20,69 @@ namespace Admin.Core.Service this._dal = dal; base.BaseDal = dal; } + + public async Task> xlGetAllRecipes() + { + try + { + return await _dal.QueryAsync(); + } + catch(Exception ex) + { + throw; + } + } + + public async Task xlInsertRecipe(xl_recipe recipe) + { + try + { + if(await _dal.AddAsync(recipe) == 1) + { + return true; + } + else + { + return false; + } + } + catch(Exception ex) + { + throw; + } + } + + public async Task xlUpdateRecipe(xl_recipe recipe) + { + try + { + return await _dal.UpdateAsync(recipe); + } + catch(Exception ex) + { + throw; + } + } + + public async Task xlUpdateOrAdd(xl_recipe recipe) + { + try + { + var record = await _dal.QueryAsync(x => x.Recipe_Code == recipe.Recipe_Code); + if(record == null || record.Count == 0) + { + recipe.ID = record[0].ID; + return await xlUpdateRecipe(recipe); + } + else + { + return await xlInsertRecipe(recipe); + } + } + catch (Exception ex) + { + throw; + } + } } } \ No newline at end of file