diff --git a/PARR.EsppApi/EsppApiService.cs b/PARR.EsppApi/EsppApiService.cs index af773801..198ccdf5 100644 --- a/PARR.EsppApi/EsppApiService.cs +++ b/PARR.EsppApi/EsppApiService.cs @@ -1,7 +1,6 @@ using Microsoft.Extensions.Logging; using PARR.EsppApi.Models; using PARR.EsppApi.Models.Query; -using PARR.EsppApi.Services; using PARR.EsppApi.Settings; namespace PARR.EsppApi @@ -10,26 +9,37 @@ namespace PARR.EsppApi { private readonly ILogger logger; private readonly EsppOrderSettings esppOrderSettings; - private readonly Espp espp; + private readonly EsppHttpService esppHttpService; - public EsppApiService(ILogger logger, EsppOrderSettings esppOrderSettings, Espp espp) + public EsppApiService(ILogger logger, EsppOrderSettings esppOrderSettings, EsppHttpService esppHttpService) { this.logger = logger; this.esppOrderSettings = esppOrderSettings; - this.espp = espp; + this.esppHttpService = esppHttpService; } public async Task>> FindOrdersAsync(FindOrdersQuery query) { - logger.LogInformation("Привет!"); + var conditions = new List(); - // var url = esppOrderSettings.Url; - var aaa = await espp.Get(); - logger.LogInformation($"asdasd: {aaa}"); + if (!string.IsNullOrEmpty(query.ShortDescriptionContains)) + conditions.Add($"title like \"*{query.ShortDescriptionContains}*\""); + if (!string.IsNullOrEmpty(query.TemplateNameContains)) + conditions.Add($"templateName like \"*{query.TemplateNameContains}*\""); + if (query.DateOpen.HasValue) + conditions.Add($"open.time>='{query.DateOpen.Value.Day}/{query.DateOpen.Value.Month}/{query.DateOpen.Value.Year} 00:00:00'"); + if (query.DateEnd.HasValue) + conditions.Add($"expirationDat<='{query.DateEnd.Value.Day}/{query.DateEnd.Value.Month}/{query.DateEnd.Value.Year} 00:00:00'"); - var esppOrders = new List(); + if (conditions.Count < 1) + { + logger.LogDebug("Пришёл пустой запрос в EsppHttpService.FindOrdersAsync"); + return new EsppResultBase>(null, false); + } - return new EsppResultBase>(esppOrders, true); + var result = await esppHttpService.SendAsync>($"{{\"VIEW_QUERY\":\"{string.Join(" and ", conditions)}\"}}"); + + return result; } } diff --git a/PARR.EsppApi/Models/EsppOrder.cs b/PARR.EsppApi/Models/EsppOrder.cs index b86a6d83..c589af1c 100644 --- a/PARR.EsppApi/Models/EsppOrder.cs +++ b/PARR.EsppApi/Models/EsppOrder.cs @@ -1,12 +1,23 @@ -namespace PARR.EsppApi.Models +using System.ComponentModel.DataAnnotations.Schema; +using System.Text.Json.Serialization; + +namespace PARR.EsppApi.Models { /// /// Модель наряда из еспп /// public class EsppOrder { + [JsonPropertyName("recordid")] public string Number { get; set; } = string.Empty; - + [JsonPropertyName("assignment")] public string? WorkGroup { get; set; } + [JsonPropertyName("status")] + public string Status { get; set; } = string.Empty; + [JsonPropertyName("title")] + public string ShortName { get; set; } = string.Empty; + [JsonPropertyName("templateName")] + public string TemplateName { get; set; } = string.Empty; } } + diff --git a/PARR.EsppApi/Models/Query/FindOrdersQuery.cs b/PARR.EsppApi/Models/Query/FindOrdersQuery.cs index 518fe358..4105f791 100644 --- a/PARR.EsppApi/Models/Query/FindOrdersQuery.cs +++ b/PARR.EsppApi/Models/Query/FindOrdersQuery.cs @@ -12,9 +12,19 @@ /// public string? ShortDescriptionContains { get; set; } + /// + /// Шаблон, содержит значение + /// + public string? TemplateNameContains { get; set; } + /// /// Дата - "ПО" /// public DateTime? DateEnd { get; set; } + + /// + /// Дата - "Открытия" + /// + public DateTime? DateOpen { get; set; } } } diff --git a/PARR.EsppApi/ParrEsppApiInstaller.cs b/PARR.EsppApi/ParrEsppApiInstaller.cs index cf7006a5..1d06db3a 100644 --- a/PARR.EsppApi/ParrEsppApiInstaller.cs +++ b/PARR.EsppApi/ParrEsppApiInstaller.cs @@ -1,6 +1,5 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using PARR.EsppApi.Services; using PARR.EsppApi.Settings; namespace PARR.EsppApi @@ -17,8 +16,12 @@ namespace PARR.EsppApi services.AddTransient(); - services.AddHttpClient(); - //add other services + services.AddHttpClient(client => + { + client.BaseAddress = new Uri(esppSettings.Url); + client.DefaultRequestHeaders.Add("EsppUser", esppSettings.UserName); + client.DefaultRequestHeaders.Add("EsppPassword", esppSettings.Password); + }); } } diff --git a/PARR.EsppApi/Services/Espp.cs b/PARR.EsppApi/Services/Espp.cs deleted file mode 100644 index ab30e83e..00000000 --- a/PARR.EsppApi/Services/Espp.cs +++ /dev/null @@ -1,27 +0,0 @@ -namespace PARR.EsppApi.Services -{ - internal class Espp - { - private readonly HttpClient httpClient; - - public Espp(HttpClient httpClient) - { - this.httpClient = httpClient; - } - - public async Task Get() - { - //var req = new HttpRequestMessage(HttpMethod.Get, "https://www.google.com"); - - //if (req == null) - // return; - - var response = await httpClient.GetAsync("http://dvocor.dvgd.rzd"); - var content = await response.Content.ReadAsStringAsync(); - - - - return content; - } - } -} diff --git a/PARR.EsppApi/Services/EsppHttpService.cs b/PARR.EsppApi/Services/EsppHttpService.cs new file mode 100644 index 00000000..74fc7593 --- /dev/null +++ b/PARR.EsppApi/Services/EsppHttpService.cs @@ -0,0 +1,54 @@ +using Microsoft.Extensions.Logging; +using PARR.EsppApi.Models; +using System; +using System.Net.Http.Json; +using System.Text; +using System.Text.Json; + +namespace PARR.EsppApi +{ + internal class EsppHttpService + { + private readonly ILogger logger; + private readonly HttpClient httpClient; + + public EsppHttpService(ILogger logger,HttpClient httpClient) + { + this.logger = logger; + this.httpClient = httpClient; + } + + public async Task> SendAsync(string query) where T : class + { + var request = new HttpRequestMessage(HttpMethod.Post, ""); + request.Content = new StringContent(query); + + try + { + logger.LogDebug($"Отправляю запрос в АСУ ЕСПП({query})"); + var response = await httpClient.SendAsync(request); + + if (!response.IsSuccessStatusCode) + { + logger.LogError($"Ошибка при получении данных из АСУ ЕСПП, StatusCode:{response.StatusCode}"); + + } + + var responseString = await response.Content.ReadAsStringAsync(); + + logger.LogDebug($"Получил данные, строка: {responseString}"); + + var obj = JsonSerializer.Deserialize(responseString); + + return new EsppResultBase(obj, true); + + } + catch (Exception ex) + { + logger.LogError(ex, "Ошибка при получении данных из АСУ ЕСПП"); + + return new EsppResultBase(null, false, ex); + } + } + } +} diff --git a/PARR.EsppApi/Settings/EsppOrderSettings.cs b/PARR.EsppApi/Settings/EsppOrderSettings.cs index 26296223..48d1cf71 100644 --- a/PARR.EsppApi/Settings/EsppOrderSettings.cs +++ b/PARR.EsppApi/Settings/EsppOrderSettings.cs @@ -3,8 +3,8 @@ internal class EsppOrderSettings { public string Url { get; set; } = string.Empty; + public string UserName { get; set; } = string.Empty; + public string Password { get; set; } = string.Empty; - //login, pass - //todo: add othe } } diff --git a/PARR.Test/PARR.Test.csproj b/PARR.Test/PARR.Test.csproj index dc88257b..b041f721 100644 --- a/PARR.Test/PARR.Test.csproj +++ b/PARR.Test/PARR.Test.csproj @@ -9,6 +9,10 @@ + + + + diff --git a/PARR.Test/Program.cs b/PARR.Test/Program.cs index 6afaab79..78fca86f 100644 --- a/PARR.Test/Program.cs +++ b/PARR.Test/Program.cs @@ -1,5 +1,6 @@ using PARR.EsppApi; using PARR.Test; +using Serilog; IHost host = Host.CreateDefaultBuilder(args) .ConfigureServices((hostContext, services) => @@ -8,6 +9,12 @@ IHost host = Host.CreateDefaultBuilder(args) services.AddHostedService(); }) + .UseSerilog((hostContext, services, config) => + { + config + .WriteTo.Console() + .ReadFrom.Configuration(hostContext.Configuration); + }) .Build(); host.Run(); diff --git a/PARR.Test/Worker.cs b/PARR.Test/Worker.cs index 6efad3e8..5b310150 100644 --- a/PARR.Test/Worker.cs +++ b/PARR.Test/Worker.cs @@ -28,8 +28,7 @@ namespace PARR.Test private async Task TestEsppApi() { - // var orders = await esppApiService.FindOrdersByPrefixAsync("ПАРР__"); - var result = await esppApiService.FindOrdersAsync(new FindOrdersQuery { }); + var result = await esppApiService.FindOrdersAsync(new FindOrdersQuery { ShortDescriptionContains = "описание", DateOpen = new DateTime(2023, 10, 10) }); if (result.IsSuccess) { diff --git a/PARR.Test/appsettings.Development.json b/PARR.Test/appsettings.Development.json index b2dcdb67..2a5e9169 100644 --- a/PARR.Test/appsettings.Development.json +++ b/PARR.Test/appsettings.Development.json @@ -4,5 +4,29 @@ "Default": "Information", "Microsoft.Hosting.Lifetime": "Information" } + }, + "Serilog": { + "MinimumLevel": { + "Default": "Debug", + "Override": { + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Debug" + } + }, + "WriteTo": [ + { + "Name": "File", + "Args": { + "path": "log/log-.txt", + "rollingInterval": "Day" + } + } + ] + }, + + "EsppOrderSettings": { + "Url": "http://rzd-espp-t-rpa-app-1.gvc.oao.rzd:8080/espp_api/OperationExecutor/", + "UserName": "Auto-PTK-DVS", + "Password": "123456789" } -} +} \ No newline at end of file diff --git a/PARR.Test/appsettings.json b/PARR.Test/appsettings.json index 337c5fd0..558b1f03 100644 --- a/PARR.Test/appsettings.json +++ b/PARR.Test/appsettings.json @@ -5,7 +5,27 @@ "Microsoft.Hosting.Lifetime": "Information" } }, + "Serilog": { + "MinimumLevel": { + "Default": "Debug", + "Override": { + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Debug" + } + }, + "WriteTo": [ + { + "Name": "File", + "Args": { + "path": "log/log-.txt", + "rollingInterval": "Day" + } + } + ] + }, "EsppOrderSettings": { - "Url": "http://dafsdf" + "Url": "http://rzd-espp-t-rpa-app-1.gvc.oao.rzd:8080/espp_api/OperationExecutor/", + "UserName": "Auto-PTK-DVS", + "Password": "123456789" } }