add - 添加业务类,无测试盲写

dep_wangsr
SoulStar 2 weeks ago
parent 6034832914
commit f93bae5f52

@ -325,7 +325,7 @@
</summary>
<returns></returns>
</member>
<member name="M:Admin.Core.Api.xl_materialController.xlDownLoadMateralInfo(System.String,System.String)">
<member name="M:Admin.Core.Api.xl_materialController.XlDownLoadMateralInfo(System.String,System.String,System.Int32,System.DateTime)">
<summary>
小料同步MES物料信息
</summary>
@ -1718,6 +1718,115 @@
DeleteSubscriptionFiles
</summary>
</member>
<member name="P:Admin.Core.Api.PLTBusiness.Entity.MaterialData.pageCount">
<summary>
总页数
</summary>
</member>
<member name="P:Admin.Core.Api.PLTBusiness.Entity.MaterialData.pageIndex">
<summary>
页码
</summary>
</member>
<member name="P:Admin.Core.Api.PLTBusiness.Entity.MaterialData.recordList">
<summary>
数据对象
</summary>
</member>
<member name="P:Admin.Core.Api.PLTBusiness.Entity.MaterialData.recordCount">
<summary>
总记录数
</summary>
</member>
<member name="P:Admin.Core.Api.PLTBusiness.Entity.MaterialData.pageSize">
<summary>
每页记录数
</summary>
</member>
<member name="P:Admin.Core.Api.PLTBusiness.Entity.MaterialData.endPageIndex">
<summary>
尾页编号
</summary>
</member>
<member name="P:Admin.Core.Api.PLTBusiness.Entity.MaterialData.beginPageIndex">
<summary>
起始页编号
</summary>
</member>
<member name="P:Admin.Core.Api.PLTBusiness.Entity.MaterialRecordListItem.name">
<summary>
物料名称
</summary>
</member>
<member name="P:Admin.Core.Api.PLTBusiness.Entity.MaterialRecordListItem.status">
<summary>
物料状态0禁用 1启用
</summary>
</member>
<member name="P:Admin.Core.Api.PLTBusiness.Entity.MaterialRecordListItem.code">
<summary>
物料编号
</summary>
</member>
<member name="P:Admin.Core.Api.PLTBusiness.Entity.MaterialRecordListItem.createTime">
<summary>
物料创建时间
</summary>
</member>
<member name="P:Admin.Core.Api.PLTBusiness.Entity.ResponseRoot`1.code">
<summary>
状态码
</summary>
</member>
<member name="P:Admin.Core.Api.PLTBusiness.Entity.ResponseRoot`1.message">
<summary>
消息提示
</summary>
</member>
<member name="P:Admin.Core.Api.PLTBusiness.Entity.ResponseRoot`1.Data">
<summary>
结果对象
</summary>
</member>
<member name="P:Admin.Core.Api.PLTBusiness.Entity.TokenData.token">
<summary>
Token(后续请求需要携带这个Token)
</summary>
</member>
<member name="P:Admin.Core.Api.PLTBusiness.Entity.TokenData.expiration">
<summary>
Token失效时间
</summary>
</member>
<member name="M:Admin.Core.Api.PLTBusiness.HttpService.PostAsync(System.String,System.String,System.String,System.String,System.Collections.Generic.Dictionary{System.String,System.String},System.Int32)">
<summary>
发起POST异步请求
</summary>
<param name="url">请求地址</param>
<param name="body">POST提交的内容</param>
<param name="bodyMediaType">POST内容的媒体类型application/xml、application/json</param>
<param name="responseContentType">HTTP响应上的content-type内容头的值,如application/xml、application/json、application/text、application/x-www-form-urlencoded等</param>
<param name="headers">请求头信息</param>
<param name="timeOut">请求超时时间,单位秒</param>
<returns>返回string</returns>
</member>
<member name="M:Admin.Core.Api.PLTBusiness.HttpService.GetAsync(System.String,System.Collections.Generic.Dictionary{System.String,System.String},System.Int32)">
<summary>
发起GET异步请求
</summary>
<typeparam name="T">返回类型</typeparam>
<param name="url">请求地址</param>
<param name="headers">请求头信息</param>
<param name="timeOut">请求超时时间,单位秒</param>
<returns>返回string</returns>
</member>
<member name="M:Admin.Core.Api.PLTBusiness.HttpService.GetHostName(System.String)">
<summary>
获取请求的主机名
</summary>
<param name="url"></param>
<returns></returns>
</member>
<member name="T:Admin.Core.Api.Program">
<summary>
Program

@ -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
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task<MessageModel<bool>> xlDownLoadMateralInfo(string materialCode, string materialName)
public async Task<MessageModel<bool>> 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);

@ -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
{
/// <summary>
/// 总页数
/// </summary>
public int pageCount { get; set; }
/// <summary>
/// 页码
/// </summary>
public int pageIndex { get; set; }
/// <summary>
/// 数据对象
/// </summary>
public List<MaterialRecordListItem> recordList { get; set; }
/// <summary>
/// 总记录数
/// </summary>
public int recordCount { get; set; }
/// <summary>
/// 每页记录数
/// </summary>
public int pageSize { get; set; }
/// <summary>
/// 尾页编号
/// </summary>
public int endPageIndex { get; set; }
/// <summary>
/// 起始页编号
/// </summary>
public int beginPageIndex { get; set; }
}
public class MaterialRecordListItem
{
/// <summary>
/// 物料名称
/// </summary>
public string name { get; set; }
/// <summary>
/// 物料状态0禁用 1启用
/// </summary>
public int status { get; set; }
/// <summary>
/// 物料编号
/// </summary>
public string code { get; set; }
/// <summary>
/// 物料创建时间
/// </summary>
public string createTime { get; set; }
}
}

@ -0,0 +1,24 @@
using System;
using System.Linq;
using System.Text;
using SqlSugar;
namespace Admin.Core.Api.PLTBusiness.Entity
{
public class ResponseRoot<T>
{
/// <summary>
/// 状态码
/// </summary>
public int code { get; set; }
/// <summary>
/// 消息提示
/// </summary>
public string message { get; set; }
/// <summary>
/// 结果对象
/// </summary>
public T Data { get; set; }
}
}

@ -0,0 +1,20 @@
using System;
using System.Linq;
using System.Text;
using SqlSugar;
namespace Admin.Core.Api.PLTBusiness.Entity
{
public class TokenData
{
/// <summary>
/// Token(后续请求需要携带这个Token)
/// </summary>
public string token { get; set; }
/// <summary>
/// Token失效时间
/// </summary>
public int expiration { get; set; }
}
}

@ -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<string, string> _tokenDictionary = new Dictionary<string, string>()
{
{"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<XlBusiness>();
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<TokenData> responseRoot = JsonSerializer.Deserialize<ResponseRoot<TokenData>>(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());
}
}
/// <summary>
/// 发起POST异步请求
/// </summary>
/// <param name="url">请求地址</param>
/// <param name="body">POST提交的内容</param>
/// <param name="bodyMediaType">POST内容的媒体类型application/xml、application/json</param>
/// <param name="responseContentType">HTTP响应上的content-type内容头的值,如application/xml、application/json、application/text、application/x-www-form-urlencoded等</param>
/// <param name="headers">请求头信息</param>
/// <param name="timeOut">请求超时时间,单位秒</param>
/// <returns>返回string</returns>
private async Task<string> PostAsync(string url, string body,
string bodyMediaType = null,
string responseContentType = null,
Dictionary<string, string> 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;
}
}
/// <summary>
/// 发起GET异步请求
/// </summary>
/// <typeparam name="T">返回类型</typeparam>
/// <param name="url">请求地址</param>
/// <param name="headers">请求头信息</param>
/// <param name="timeOut">请求超时时间,单位秒</param>
/// <returns>返回string</returns>
private async Task<string> GetAsync(string url, Dictionary<string, string> 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<string, string> 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 私有函数
/// <summary>
/// 获取请求的主机名
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
private static string GetHostName(string url)
{
if (!string.IsNullOrWhiteSpace(url))
{
return url.Replace("https://", "").Replace("http://", "").Split('/')[0];
}
else
{
return "AnyHost";
}
}
#endregion
}
}

@ -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<bool> InsertNewMaterialData(List<MaterialRecordListItem> list)
{
List<xl_material> mlist = new List<xl_material>();
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;
}
}
}

@ -28,6 +28,8 @@ namespace Admin.Core.Api
// 运行 web 应用程序并阻止调用线程, 直到主机关闭。
// ※※※※ 有异常,查看 Log 文件夹下的异常日志 ※※※※
.Run();
}
/// <summary>
@ -58,5 +60,6 @@ namespace Admin.Core.Api
// 默认log4net.confg
builder.AddLog4Net(Path.Combine(Directory.GetCurrentDirectory(), "Log4net.config"));
});
}
}

@ -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<PLTBusiness.HttpService>();
services.AddScoped<PLTBusiness.XlBusiness>();
//Redis»º´æ
//services.AddRedisCacheSetup();
//Redis¶ÓÁÐ

@ -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<MessageModel<bool>> MaterialVerification(string barCode);
Task<bool> xlInsertMaterial(string materialCode, string materialName);
Task<bool> xlInsertMaterial(
string materialCode,
string materialName,
int isEnable,
DateTime createDatetime);
Task<bool> xlUpdateMaterial(
string id,
string materialCode,
string materialName,
int isEnable,
DateTime createDatetime);
Task<List<xl_material>> xlMaterialList();
}
}

@ -633,6 +633,7 @@ namespace Admin.Core.Service
#endregion
#region 料框防错验证——只验证单料
public async Task<MessageModel<bool>> MaterialVerification(string barCode)
{
MessageModel<bool> messageModel = new MessageModel<bool>();
@ -684,15 +685,20 @@ namespace Admin.Core.Service
}
#endregion
/// <summary>
/// 小料上位机同步插入物料信息
/// 小料上位机插入物料信息
/// </summary>
/// <param name="planDate"></param>
/// <param name="recipeGUID"></param>
/// <param name="batch"></param>
/// <param name="productName"></param>
/// <returns></returns>
public async Task<bool> xlInsertMaterial(string materialCode, string materialName)
public async Task<bool> 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
}
}
/// <summary>
/// 小料上位机更新物料信息
/// </summary>
/// <param name="planDate"></param>
/// <param name="recipeGUID"></param>
/// <param name="batch"></param>
/// <param name="productName"></param>
/// <returns></returns>
public async Task<bool> 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;
}
}
/// <summary>
/// 获取小料上位机物料信息
/// </summary>
/// <param name="planDate"></param>
/// <param name="recipeGUID"></param>
/// <param name="batch"></param>
/// <param name="productName"></param>
/// <returns></returns>
public async Task<List<xl_material>> xlMaterialList()
{
try
{
return await _xl_materialRepository.QueryAsync();
}
catch (Exception ex)
{
return null;
}
}
}
}
Loading…
Cancel
Save