diff --git a/BasicData/Tnb.BasicData.Entities/Dto/ThirdResult.cs b/BasicData/Tnb.BasicData.Entities/Dto/ThirdResult.cs new file mode 100644 index 00000000..ba0ee32c --- /dev/null +++ b/BasicData/Tnb.BasicData.Entities/Dto/ThirdResult.cs @@ -0,0 +1,13 @@ +namespace Tnb.BasicData.Entities +{ + public class ThirdResult + { + public int Code { get; set; } + public string msgResult { get; set; } + } + + // public enum ThirdResultCode + // { + // OK = 200 + // } +} \ No newline at end of file diff --git a/BasicData/Tnb.BasicData.Entities/Entity/ThirdWebapiRecord.cs b/BasicData/Tnb.BasicData.Entities/Entity/ThirdWebapiRecord.cs new file mode 100644 index 00000000..9dba0ee4 --- /dev/null +++ b/BasicData/Tnb.BasicData.Entities/Entity/ThirdWebapiRecord.cs @@ -0,0 +1,78 @@ +using JNPF.Common.Contracts; +using SqlSugar; + +namespace Tnb.BasicData.Entities +{ + /// + /// 工位资料 + /// + [SugarTable("third_webapi_record")] + public class ThirdWebapiRecord : BaseEntity + { + /// + /// 第三方名 + /// + public string third_name { get; set; } + + /// + /// 名称 + /// + public string name { get; set; } + + /// + /// 请求方式 + /// + public string method { get; set; } + + /// + /// 地址 + /// + public string url { get; set; } + + /// + /// 请求数据 + /// + public string request_data { get; set; } + + /// + /// 接收数据 + /// + public string response_data { get; set; } + + /// + /// 返回码 + /// + public int response_code { get; set; } + + /// + /// 响应时长 + /// + public long response_time { get; set; } + + /// + /// 是否发送 0 否 1是 + /// + public int is_send { get; set; } + + /// + /// 状态 0 失败 1 成功 + /// + public int status { get; set; } + + /// + /// 发送成功时类型 自动 手动 + /// + public string send_type { get; set; } + + /// + /// 创建时间 + /// + public DateTime create_time { get; set; } + + /// + /// 最后发送时间 + /// + public DateTime? last_send_time { get; set; } + + } +} \ No newline at end of file diff --git a/BasicData/Tnb.BasicData/ThirdApiRecordService.cs b/BasicData/Tnb.BasicData/ThirdApiRecordService.cs new file mode 100644 index 00000000..33ecc31a --- /dev/null +++ b/BasicData/Tnb.BasicData/ThirdApiRecordService.cs @@ -0,0 +1,80 @@ +using System.Diagnostics; +using JNPF.Common.Core.Manager; +using JNPF.DependencyInjection; +using JNPF.DynamicApiController; +using JNPF.Extras.CollectiveOAuth.Utils; +using JNPF.Systems.Interfaces.Permission; +using Microsoft.AspNetCore.Mvc; +using Newtonsoft.Json; +using SqlSugar; +using Tnb.BasicData.Entities; + +namespace Tnb.BasicData +{ + /// + /// 第三方接口发送记录 + /// + [ApiDescriptionSettings(Tag = ModuleConst.Tag, Area = ModuleConst.Area, Order = 700)] + [Route("api/[area]/[controller]/[action]")] + public class ThirdApiRecordService : IDynamicApiController, ITransient + { + private readonly ISqlSugarRepository _repository; + private readonly IUserManager _userManager; + + public ThirdApiRecordService( + ISqlSugarRepository repository, + IUserManager userManager + ) + { + _repository = repository; + _userManager = userManager; + } + + [HttpGet] + public async Task Send(string id) + { + var db = _repository.AsSugarClient(); + string response = ""; + ThirdWebapiRecord record = await _repository.GetSingleAsync(x=>x.id==id); + DateTime now = DateTime.Now; + Stopwatch stopwatch = Stopwatch.StartNew(); + switch (record.method.ToUpper()) + { + case "GET": + response = HttpUtils.RequestGet(record.url); + break; + case "POST": + response = HttpUtils.RequestPost(record.url, record.request_data); + break; + } + + stopwatch.Stop(); + var elapsedMilliseconds = stopwatch.ElapsedMilliseconds; + ThirdResult thirdResult = JsonConvert.DeserializeObject(response); + if (thirdResult.Code == 200) + { + await db.Updateable() + .SetColumns(x => x.response_data == response) + .SetColumns(x => x.response_code == thirdResult.Code) + .SetColumns(x => x.last_send_time == now) + .SetColumns(x => x.response_time == elapsedMilliseconds) + .SetColumns(x => x.send_type == "手动") + .SetColumns(x => x.status == 1) + .Where(x=>x.id==record.id) + .ExecuteCommandAsync(); + } + else + { + await db.Updateable() + .SetColumns(x => x.response_data == response) + .SetColumns(x => x.response_code == thirdResult.Code) + .SetColumns(x => x.last_send_time == now) + .SetColumns(x => x.response_time == elapsedMilliseconds) + .Where(x=>x.id==record.id) + .ExecuteCommandAsync(); + } + + return thirdResult.Code == 200 ? "成功" : "失败"; + } + } +} \ No newline at end of file diff --git a/ProductionMgr/Tnb.ProductionMgr/TimeWorkService.cs b/ProductionMgr/Tnb.ProductionMgr/TimeWorkService.cs index 85420290..48f2024d 100644 --- a/ProductionMgr/Tnb.ProductionMgr/TimeWorkService.cs +++ b/ProductionMgr/Tnb.ProductionMgr/TimeWorkService.cs @@ -1,3 +1,4 @@ +using System.Diagnostics; using Aop.Api.Domain; using JNPF; using JNPF.Common.Core.Manager; @@ -432,6 +433,62 @@ namespace Tnb.ProductionMgr await _prdMoTaskService.PrdReport(input); return "true"; } + + [HttpGet] + [AllowAnonymous] + public async Task SendThirdApi(string thirdNmaes) + { + string[] thirdNameArr = thirdNmaes.Split("-"); + List records = await _db.Queryable().Where(x => thirdNameArr.Contains(x.third_name) && x.status == 0 && x.is_send == 1).ToListAsync(); + DateTime now = DateTime.Now; + Stopwatch stopwatch = null; + string response = ""; + var elapsedMilliseconds = 0l; + ThirdResult thirdResult = null; + foreach (var record in records) + { + now = DateTime.Now; + stopwatch = Stopwatch.StartNew(); + switch (record.method.ToUpper()) + { + case "GET": + response = HttpUtils.RequestGet(record.url); + break; + case "POST": + response = HttpUtils.RequestPost(record.url, record.request_data); + break; + } + + stopwatch.Stop(); + elapsedMilliseconds = stopwatch.ElapsedMilliseconds; + thirdResult = JsonConvert.DeserializeObject(response); + if (thirdResult.Code == 200) + { + await _db.Updateable() + .SetColumns(x => x.response_data == response) + .SetColumns(x => x.response_code == thirdResult.Code) + .SetColumns(x => x.last_send_time == now) + .SetColumns(x => x.response_time == elapsedMilliseconds) + .SetColumns(x => x.send_type == "自动") + .SetColumns(x => x.status == 1) + .Where(x=>x.id==record.id) + .ExecuteCommandAsync(); + } + else + { + await _db.Updateable() + .SetColumns(x => x.response_data == response) + .SetColumns(x => x.response_code == thirdResult.Code) + .SetColumns(x => x.last_send_time == now) + .SetColumns(x => x.response_time == elapsedMilliseconds) + .Where(x=>x.id==record.id) + .ExecuteCommandAsync(); + } + + } + + return "true"; + } } } \ No newline at end of file diff --git a/system/Tnb.Systems/Common/TestService.cs b/system/Tnb.Systems/Common/TestService.cs index 68d6783c..344ffb29 100644 --- a/system/Tnb.Systems/Common/TestService.cs +++ b/system/Tnb.Systems/Common/TestService.cs @@ -82,5 +82,12 @@ public class TestService : IDynamicApiController, ITransient user.Account = "2312321"; } + + [HttpGet] + [AllowAnonymous] + public string test1() + { + return "测试成功"; + } }