From c4c8b748f008b4152e5a284422ab8fa96146cf51 Mon Sep 17 00:00:00 2001 From: Mikhail Kuznetsov Date: Fri, 27 Oct 2023 16:39:59 +1000 Subject: [PATCH] =?UTF-8?q?feat(esppApi):=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D1=8B=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D1=8B?= =?UTF-8?q?=20=D0=B2=D0=B7=D1=8F=D1=82=D0=B8=D1=8F=20=D0=B2=20=D1=80=D0=B0?= =?UTF-8?q?=D0=B1=D0=BE=D1=82=D1=83,=20=D0=B2=D1=8B=D0=BF=D0=BE=D0=BB?= =?UTF-8?q?=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B8=20=D1=81=D0=BF=D0=B8?= =?UTF-8?q?=D1=81=D0=B0=D0=BD=D0=B8=D1=8F=20=D1=82=D1=80=D1=83=D0=B4=D0=BE?= =?UTF-8?q?=D0=B7=D0=B0=D1=82=D1=80=D0=B0=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PARR.EsppApi/EsppApiService.cs | 48 ++++++++++++++++++-- PARR.EsppApi/IEsppApiService.cs | 24 ++++++++-- PARR.EsppApi/Models/EsppOrder.cs | 1 - PARR.EsppApi/Models/EsppResponse.cs | 14 ++++++ PARR.EsppApi/Models/Query/AddMtnkQuery.cs | 27 +++++++++++ PARR.EsppApi/Models/Query/FindOrdersQuery.cs | 4 -- PARR.EsppApi/Services/EsppHttpService.cs | 10 +--- PARR.EsppApi/Settings/EsppOrderSettings.cs | 4 ++ PARR.Test/Worker.cs | 44 ++++++++++++++---- PARR.Test/appsettings.json | 1 + 10 files changed, 148 insertions(+), 29 deletions(-) create mode 100644 PARR.EsppApi/Models/EsppResponse.cs create mode 100644 PARR.EsppApi/Models/Query/AddMtnkQuery.cs diff --git a/PARR.EsppApi/EsppApiService.cs b/PARR.EsppApi/EsppApiService.cs index 603e00ff..262abf82 100644 --- a/PARR.EsppApi/EsppApiService.cs +++ b/PARR.EsppApi/EsppApiService.cs @@ -18,6 +18,35 @@ namespace PARR.EsppApi this.esppHttpService = esppHttpService; } + public Task> SetStatusInWorkAsync(string recordId)//TODO упростить можно без RecordIdQuery + { + return SetStatusAsync(recordId, "2-В работе"); + } + + public Task> SetStatusIsDone(string recordId)//TODO упростить можно без RecordIdQuery + { + + return SetStatusAsync(recordId, "4-Выполнен"); + } + + private async Task> SetStatusAsync(string recordId, string newStatusStr) + { + //TODO assignee сразу назначаем принудительно, чтобы было понятно кто менял объект + var result = await esppHttpService.SendAsync($"{{\"recordid\":\"{recordId}\",\"status\":\"{newStatusStr}\", \"assignee\":\"{esppOrderSettings.AccountName}\"}}"); + + if (result.Data != null && result.Data.ErrorCode != "0") + return new EsppResultBase(null, false, new Exception(result.Data.Message)); + + return result; + } + + public async Task> FindOrderByRecordIdAsync(string recordId) + { + var result = await esppHttpService.SendAsync($"\r\n{{\"recordid\":\"{recordId}\"}}\r\n");//TODO упростить можно без RecordIdQuery + + return result; + } + public async Task>> FindOrdersAsync(FindOrdersQuery query) { DateTime? generateDateStartWOTimeZone = query.GenerateDateStart.HasValue ? query.GenerateDateStart.Value.AddHours(esppOrderSettings.EsppUserTimeZone) : null; @@ -29,16 +58,16 @@ namespace PARR.EsppApi conditions.Add($"description like \"*{query.DescriptionContains}*\""); //if (!string.IsNullOrEmpty(query.TemplateNameContains)) // conditions.Add($"templateName like \"*{query.TemplateNameContains}*\""); - + if (generateDateStartWOTimeZone.HasValue) conditions.Add($"open.time>='{generateDateStartWOTimeZone.Value.Day}/{generateDateStartWOTimeZone.Value.Month}/{generateDateStartWOTimeZone.Value.Year}" + $" {generateDateStartWOTimeZone.Value.Hour}:{generateDateStartWOTimeZone.Value.Minute}:{generateDateStartWOTimeZone.Value.Second}'"); - + if (generateDateEndWOTimeZone.HasValue) conditions.Add($"open.time<='{generateDateEndWOTimeZone.Value.Day}/{generateDateEndWOTimeZone.Value.Month}/{generateDateEndWOTimeZone.Value.Year}" + $" {generateDateEndWOTimeZone.Value.Hour}:{generateDateEndWOTimeZone.Value.Minute}:{generateDateEndWOTimeZone.Value.Second}'"); - if (conditions.Count < 1) + if (!conditions.Any()) { logger.LogDebug("Пришёл пустой запрос в EsppHttpService.FindOrdersAsync"); return new EsppResultBase>(null, false); @@ -49,5 +78,18 @@ namespace PARR.EsppApi return result; } + + public async Task> AddMTnk(AddMtnkQuery mtnk) + { + var result = await esppHttpService.SendAsync( + $"{{\"recordid\":\"{mtnk.RecordId}\"," + + $"\"joboperation\":\"{mtnk.JobOperation}\",\"time\":\"{mtnk.Time}\",\"workscope\":\"{mtnk.Workspace}\"" + + $"}}"); + + if (result.Data != null && result.Data.ErrorCode != "0") + return new EsppResultBase(null, false, new Exception(result.Data.Message)); + + return result; + } } } diff --git a/PARR.EsppApi/IEsppApiService.cs b/PARR.EsppApi/IEsppApiService.cs index f4aef925..819017c3 100644 --- a/PARR.EsppApi/IEsppApiService.cs +++ b/PARR.EsppApi/IEsppApiService.cs @@ -10,10 +10,26 @@ namespace PARR.EsppApi /// /// Task>> FindOrdersAsync(FindOrdersQuery query); + /// + /// Поиск наряда по номеру + /// + /// + Task> FindOrderByRecordIdAsync(string recordId); + /// + /// Взять наряд в работу + /// + /// + Task> SetStatusInWorkAsync(string recordId); + /// + /// Установить статус наряда как выполнен + /// + /// + Task> SetStatusIsDone(string recordId); + /// + /// Списание трудозатрат по модульным ТНК + /// + /// + Task> AddMTnk(AddMtnkQuery mtnk); - //взять в работу - //выполнить + списать трудозатраты - //найти по номеру - } } diff --git a/PARR.EsppApi/Models/EsppOrder.cs b/PARR.EsppApi/Models/EsppOrder.cs index 1e1401c6..aee98eca 100644 --- a/PARR.EsppApi/Models/EsppOrder.cs +++ b/PARR.EsppApi/Models/EsppOrder.cs @@ -37,7 +37,6 @@ namespace PARR.EsppApi.Models return new DateTimeOffset(utc, new TimeSpan(0)); } } - } } diff --git a/PARR.EsppApi/Models/EsppResponse.cs b/PARR.EsppApi/Models/EsppResponse.cs new file mode 100644 index 00000000..b99fb7ea --- /dev/null +++ b/PARR.EsppApi/Models/EsppResponse.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PARR.EsppApi.Models +{ + public class EsppResponse + { + public string Message { get; set; } = string.Empty; + public string ErrorCode { get; set; } = string.Empty; + } +} diff --git a/PARR.EsppApi/Models/Query/AddMtnkQuery.cs b/PARR.EsppApi/Models/Query/AddMtnkQuery.cs new file mode 100644 index 00000000..ff787dd9 --- /dev/null +++ b/PARR.EsppApi/Models/Query/AddMtnkQuery.cs @@ -0,0 +1,27 @@ +namespace PARR.EsppApi.Models.Query +{ + public class AddMtnkQuery + { + /// + /// номер наряда + /// + public required string RecordId { get; set; } + + /// + /// операция для списания, должна совпадать по тексту с АСУ ЕСПП + /// + public required string JobOperation { get; set; } + + /// + /// время в формате чч:мм + /// + public required string Time { get; set; } + + /// + /// объём работ + /// + public int Workspace { get; set; } = 1; + + + } +} diff --git a/PARR.EsppApi/Models/Query/FindOrdersQuery.cs b/PARR.EsppApi/Models/Query/FindOrdersQuery.cs index 1e3262fd..38f50efe 100644 --- a/PARR.EsppApi/Models/Query/FindOrdersQuery.cs +++ b/PARR.EsppApi/Models/Query/FindOrdersQuery.cs @@ -5,15 +5,11 @@ /// public class FindOrdersQuery { - - /// /// Подробное описание, содержит значение /// public string? DescriptionContains { get; set; } - private DateTime? _generateDateStart; - /// /// Дата сначала которой выбирать созданные наряды, UTC /// diff --git a/PARR.EsppApi/Services/EsppHttpService.cs b/PARR.EsppApi/Services/EsppHttpService.cs index 74fc7593..546846c0 100644 --- a/PARR.EsppApi/Services/EsppHttpService.cs +++ b/PARR.EsppApi/Services/EsppHttpService.cs @@ -1,8 +1,5 @@ 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 @@ -12,7 +9,7 @@ namespace PARR.EsppApi private readonly ILogger logger; private readonly HttpClient httpClient; - public EsppHttpService(ILogger logger,HttpClient httpClient) + public EsppHttpService(ILogger logger, HttpClient httpClient) { this.logger = logger; this.httpClient = httpClient; @@ -29,19 +26,16 @@ namespace PARR.EsppApi 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); + return new EsppResultBase(obj, true); } catch (Exception ex) { diff --git a/PARR.EsppApi/Settings/EsppOrderSettings.cs b/PARR.EsppApi/Settings/EsppOrderSettings.cs index 9c2eeba9..e066f94d 100644 --- a/PARR.EsppApi/Settings/EsppOrderSettings.cs +++ b/PARR.EsppApi/Settings/EsppOrderSettings.cs @@ -5,7 +5,11 @@ namespace PARR.EsppApi.Settings internal class EsppOrderSettings { public string Url { get; set; } = string.Empty; + public string UserName { get; set; } = string.Empty; + + public string AccountName { get; set; } = string.Empty; + public string Password { get; set; } = string.Empty; /// diff --git a/PARR.Test/Worker.cs b/PARR.Test/Worker.cs index 7e856722..b4547b8e 100644 --- a/PARR.Test/Worker.cs +++ b/PARR.Test/Worker.cs @@ -1,6 +1,5 @@ using PARR.EsppApi; using PARR.EsppApi.Models.Query; -using System.ComponentModel.Design.Serialization; namespace PARR.Test { @@ -17,11 +16,6 @@ namespace PARR.Test protected override async Task ExecuteAsync(CancellationToken stoppingToken) { - //while (!stoppingToken.IsCancellationRequested) - //{ - // _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); - // await Task.Delay(1000, stoppingToken); - //} await TestEsppApi(); } @@ -29,11 +23,43 @@ namespace PARR.Test private async Task TestEsppApi() { - var result = await esppApiService.FindOrdersAsync(new FindOrdersQuery { DescriptionContains = "Полное", GenerateDateStart = new DateTime(2023, 10, 10) , GenerateDateEnd = new DateTime(2023, 10, 24) }); + //var result = await esppApiService.FindOrdersAsync(new FindOrdersQuery { DescriptionContains = "Полное", GenerateDateStart = new DateTime(2023, 10, 10) , GenerateDateEnd = new DateTime(2023, 10, 24) }); + var recordId = "НАР23-00049787"; - if (result.IsSuccess) + var result = await esppApiService.FindOrderByRecordIdAsync(recordId); + + if (result.IsSuccess && (result.Data != null && result.Data.Number.Any())) { - var orders = result.Data; + //var orders = result.Data; + + var inWorkResult = await esppApiService.SetStatusInWorkAsync(recordId); + if (!inWorkResult.IsSuccess) + { + _logger.LogError(inWorkResult.Exception, $"Не удалось взять в работу наряда{recordId}"); + return; + } + + var tnk = new AddMtnkQuery { RecordId = recordId, JobOperation = "Не учтена в перечне ТНК", Time = "00:05" }; + + var addMtnkResult = await esppApiService.AddMTnk(tnk); + + if (!addMtnkResult.IsSuccess) + { + _logger.LogError(addMtnkResult.Exception, $"Не удалось списать трудозатраты наряда {recordId}"); + return; + } + + + var resFinish = await esppApiService.SetStatusIsDone(recordId); + if (!resFinish.IsSuccess) + { + _logger.LogError(resFinish.Exception, $"Не удалось обновить статус наряда {recordId}"); + return; + } + } + else + { + _logger.LogWarning($"не найден наряд {recordId}"); } } } diff --git a/PARR.Test/appsettings.json b/PARR.Test/appsettings.json index 4ee316b6..f3e5285d 100644 --- a/PARR.Test/appsettings.json +++ b/PARR.Test/appsettings.json @@ -26,6 +26,7 @@ "EsppOrderSettings": { "Url": "http://rzd-espp-t-rpa-app-1.gvc.oao.rzd:8080/espp_api/OperationExecutor/", "UserName": "Auto-PTK-DVS", + "AccountName": "АВТО ТЕХНОЛОГ ПТК-ДВС (AUTO-PTK-DVS)", "Password": "123456789", "EsppUserTimeZone": 10 }