feat(esppOrderManager): доработка логики. EsppApi - запросы переделаны под модели.
This commit is contained in:
28
PARR.EsppApi/Constants/EsppOperationsEnum.cs
Normal file
28
PARR.EsppApi/Constants/EsppOperationsEnum.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
namespace PARR.EsppApi.Constants
|
||||
{
|
||||
/// <summary>
|
||||
/// Операции в ЕСПП
|
||||
/// </summary>
|
||||
internal enum EsppOperationsEnum
|
||||
{
|
||||
/// <summary>
|
||||
/// Списать трудозатраты (ТНК)
|
||||
/// </summary>
|
||||
AddMTNK,
|
||||
|
||||
/// <summary>
|
||||
/// Получить список нарядов
|
||||
/// </summary>
|
||||
GetTaskList,
|
||||
|
||||
/// <summary>
|
||||
/// Получить наряд
|
||||
/// </summary>
|
||||
GetTask,
|
||||
|
||||
/// <summary>
|
||||
/// Сохранить наряд
|
||||
/// </summary>
|
||||
SaveTask
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,10 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PARR.EsppApi.Constants;
|
||||
using PARR.EsppApi.Models;
|
||||
using PARR.EsppApi.Models.Query;
|
||||
using PARR.EsppApi.Requests;
|
||||
using PARR.EsppApi.Settings;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace PARR.EsppApi
|
||||
{
|
||||
@@ -18,27 +21,40 @@ namespace PARR.EsppApi
|
||||
this.esppHttpService = esppHttpService;
|
||||
}
|
||||
|
||||
|
||||
public Task<EsppResultBase<EsppResponse>> SetStatusInWorkAsync(string recordId, string resultMsg)//TODO упростить можно без RecordIdQuery
|
||||
{
|
||||
return SetStatusAsync(recordId, "2-В работе", resultMsg);
|
||||
}
|
||||
|
||||
|
||||
public Task<EsppResultBase<EsppResponse>> SetStatusIsDoneAsync(string recordId, string resultMsg)//TODO упростить можно без RecordIdQuery
|
||||
{
|
||||
return SetStatusAsync(recordId, "4-Выполнен", resultMsg);
|
||||
}
|
||||
|
||||
|
||||
private async Task<EsppResultBase<EsppResponse>> SetStatusAsync(string recordId, string newStatusStr, string resultMsg)
|
||||
{
|
||||
//TODO assignee сразу назначаем принудительно, чтобы было понятно кто менял объект
|
||||
var query = $"<ROOT operation=\"SaveTask\" returnFormat=\"json\">{{" +
|
||||
$"\"recordid\":\"{recordId}\"," +
|
||||
$"\"status\":\"{newStatusStr}\"," +
|
||||
$"\"assignee\":\"{esppOrderSettings.AccountName}\"," +
|
||||
$"\"resolution\":\"{resultMsg}\"" +
|
||||
$"}}</ROOT>";
|
||||
//var query = $"<ROOT operation=\"SaveTask\" returnFormat=\"json\">{{" +
|
||||
// $"\"recordid\":\"{recordId}\"," +
|
||||
// $"\"status\":\"{newStatusStr}\"," +
|
||||
// $"\"assignee\":\"{esppOrderSettings.AccountName}\"," +
|
||||
// $"\"resolution\":\"{resultMsg}\"" +
|
||||
// $"}}</ROOT>";
|
||||
|
||||
var result = await esppHttpService.SendAsync<EsppResponse>(query);
|
||||
//var result = await esppHttpService.SendAsync<EsppResponse>(QuerySanitize(query));
|
||||
|
||||
var request = new SetStatusRequest
|
||||
{
|
||||
recordid = recordId,
|
||||
status = newStatusStr,
|
||||
assignee = esppOrderSettings.AccountName,
|
||||
resolution = resultMsg
|
||||
};
|
||||
|
||||
var result = await esppHttpService.SendAsync<EsppResponse>(GenerateRequest(EsppOperationsEnum.SaveTask, request));
|
||||
|
||||
if (result.Data != null && result.Data.ErrorCode != "0")
|
||||
return new EsppResultBase<EsppResponse>(null, false, new Exception(result.Data.Message));
|
||||
@@ -46,13 +62,21 @@ namespace PARR.EsppApi
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public async Task<EsppResultBase<EsppOrder>> FindOrderByRecordIdAsync(string recordId)
|
||||
{
|
||||
var result = await esppHttpService.SendAsync<EsppOrder>($"<ROOT operation=\"GetTask\" returnFormat=\"json\">\r\n{{\"recordid\":\"{recordId}\"}}\r\n</ROOT>");//TODO упростить можно без RecordIdQuery
|
||||
//TODO упростить можно без RecordIdQuery
|
||||
//var query = $"<ROOT operation=\"GetTask\" returnFormat=\"json\">\r\n{{\"recordid\":\"{recordId}\"}}\r\n</ROOT>";
|
||||
|
||||
//var result = await esppHttpService.SendAsync<EsppOrder>(QuerySanitize(query));
|
||||
|
||||
var request = new GetTaskRequest { recordid = recordId };
|
||||
var result = await esppHttpService.SendAsync<EsppOrder>(GenerateRequest(EsppOperationsEnum.GetTask, request));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public async Task<EsppResultBase<IEnumerable<EsppOrder>>> FindOrdersAsync(FindOrdersQuery query)
|
||||
{
|
||||
DateTime? generateDateStartWOTimeZone = query.GenerateDateStart.HasValue ? query.GenerateDateStart.Value.AddHours(esppOrderSettings.EsppUserTimeZone) : null;
|
||||
@@ -79,23 +103,79 @@ namespace PARR.EsppApi
|
||||
return new EsppResultBase<IEnumerable<EsppOrder>>(null, false);
|
||||
}
|
||||
|
||||
var result = await esppHttpService.SendAsync<IEnumerable<EsppOrder>>($"<ROOT operation=\"GetTaskList\" returnFormat=\"json\">{{\"VIEW_QUERY\":\"{string.Join(" and ", conditions)}\"}}</ROOT>");
|
||||
//var request = $"<ROOT operation=\"GetTaskList\" returnFormat=\"json\">{{\"VIEW_QUERY\":\"{string.Join(" and ", conditions)}\"}}</ROOT>";
|
||||
//var result = await esppHttpService.SendAsync<IEnumerable<EsppOrder>>(QuerySanitize(request));
|
||||
|
||||
var request = new GetTaskListRequest { VIEW_QUERY = string.Join(" and ", conditions) };
|
||||
var result = await esppHttpService.SendAsync<IEnumerable<EsppOrder>>(GenerateRequest(EsppOperationsEnum.GetTaskList, request));
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public async Task<EsppResultBase<EsppResponse>> AddMTnkAsync(AddMtnkQuery mtnk)
|
||||
{
|
||||
var result = await esppHttpService.SendAsync<EsppResponse>(
|
||||
$"<ROOT operation=\"AddMTNK\" returnFormat=\"json\">{{\"recordid\":\"{mtnk.RecordId}\"," +
|
||||
$"\"joboperation\":\"{mtnk.JobOperation}\",\"time\":\"{mtnk.Time}\",\"workscope\":\"{mtnk.Workspace}\"" +
|
||||
$"}}</ROOT>");
|
||||
//var query = $"<ROOT operation=\"AddMTNK\" returnFormat=\"json\">{{\"recordid\":\"{mtnk.RecordId}\"," +
|
||||
// $"\"joboperation\":\"{mtnk.JobOperation}\",\"time\":\"{mtnk.Time}\",\"workscope\":\"{mtnk.Workspace}\"" +
|
||||
// $"}}</ROOT>";
|
||||
|
||||
//var result = await esppHttpService.SendAsync<EsppResponse>(QuerySanitize(query));
|
||||
|
||||
var request = new AddMTNKRequest
|
||||
{
|
||||
recordid = mtnk.RecordId,
|
||||
joboperation = mtnk.JobOperation,
|
||||
time = mtnk.Time,
|
||||
workscope = mtnk.Workspace.ToString()
|
||||
};
|
||||
var result = await esppHttpService.SendAsync<EsppResponse>(GenerateRequest(EsppOperationsEnum.AddMTNK, request));
|
||||
|
||||
if (result.Data != null && result.Data.ErrorCode != "0")
|
||||
return new EsppResultBase<EsppResponse>(null, false, new Exception(result.Data.Message));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Убирает из запроса запрещенные символы
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private string QuerySanitize(string str)
|
||||
{
|
||||
//str = str.Replace("\r\n", "<br/>");
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Формирует запрос в ЕСПП из строки
|
||||
/// </summary>
|
||||
/// <param name="operation"></param>
|
||||
/// <param name="query"></param>
|
||||
/// <returns></returns>
|
||||
private string GenerateRequest(EsppOperationsEnum operation, string query)
|
||||
{
|
||||
var baseRequest = $"<ROOT operation=\"operationValue\" returnFormat=\"json\">requestValue</ROOT>";
|
||||
|
||||
return baseRequest.Replace("operationValue", operation.ToString()).Replace("requestValue", query);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Формирует запрос в ЕСПП из jsonQuery
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="operation"></param>
|
||||
/// <param name="jsonQuery"></param>
|
||||
/// <returns></returns>
|
||||
private string GenerateRequest<T>(EsppOperationsEnum operation, T jsonQuery)
|
||||
{
|
||||
var strQuery = JsonSerializer.Serialize(jsonQuery);
|
||||
|
||||
return GenerateRequest(operation, strQuery);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
17
PARR.EsppApi/Requests/AddMTNKRequest.cs
Normal file
17
PARR.EsppApi/Requests/AddMTNKRequest.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
namespace PARR.EsppApi.Requests
|
||||
{
|
||||
/// <summary>
|
||||
/// Списать трудозатраты
|
||||
/// </summary>
|
||||
internal class AddMTNKRequest
|
||||
{
|
||||
public required string recordid { get; set; }
|
||||
|
||||
public required string joboperation { get; set; }
|
||||
|
||||
public required string time { get; set; }
|
||||
|
||||
public required string workscope { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
10
PARR.EsppApi/Requests/GetTaskListRequest.cs
Normal file
10
PARR.EsppApi/Requests/GetTaskListRequest.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace PARR.EsppApi.Requests
|
||||
{
|
||||
/// <summary>
|
||||
/// Получить список нарядов
|
||||
/// </summary>
|
||||
internal class GetTaskListRequest
|
||||
{
|
||||
public required string VIEW_QUERY { get; set; }
|
||||
}
|
||||
}
|
||||
10
PARR.EsppApi/Requests/GetTaskRequest.cs
Normal file
10
PARR.EsppApi/Requests/GetTaskRequest.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace PARR.EsppApi.Requests
|
||||
{
|
||||
/// <summary>
|
||||
/// Получить наряд по номеру
|
||||
/// </summary>
|
||||
internal class GetTaskRequest
|
||||
{
|
||||
public required string recordid { get; set; }
|
||||
}
|
||||
}
|
||||
16
PARR.EsppApi/Requests/SetStatusRequest.cs
Normal file
16
PARR.EsppApi/Requests/SetStatusRequest.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
namespace PARR.EsppApi.Requests
|
||||
{
|
||||
/// <summary>
|
||||
/// Изменить статус наряда
|
||||
/// </summary>
|
||||
internal class SetStatusRequest
|
||||
{
|
||||
public required string recordid { get; set; }
|
||||
|
||||
public required string status { get; set; }
|
||||
|
||||
public required string assignee { get; set; }
|
||||
|
||||
public required string resolution { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -30,15 +30,12 @@ namespace PARR.EsppApi
|
||||
|
||||
var responseString = await response.Content.ReadAsStringAsync();
|
||||
|
||||
if (responseString.Contains("ErrorCode") && responseString.Contains("Message"))
|
||||
{
|
||||
logger.LogError(new Exception(responseString), $"Ошибка при выполнении запроса в ЕСПП. Тело запроса: {query}");
|
||||
|
||||
return new EsppResultBase<T>(null, false, new Exception($"Espp error: {responseString}"));
|
||||
}
|
||||
|
||||
logger.LogDebug($"Получил данные, строка: {responseString}");
|
||||
|
||||
var errors = CheckErrors<T>(responseString, query);
|
||||
if (errors != null)
|
||||
return errors;
|
||||
|
||||
var obj = JsonSerializer.Deserialize<T>(responseString);
|
||||
|
||||
|
||||
@@ -51,5 +48,40 @@ namespace PARR.EsppApi
|
||||
return new EsppResultBase<T>(null, false, ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Проверка респонса на ошибки. В ЕСПП полный вест кост кастом
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
private EsppResultBase<T>? CheckErrors<T>(string response, string query) where T : class
|
||||
{
|
||||
//Если вернет "ErrorCode":"0" - то все успешно прошло, если вернет другой ErrorCode, то не успешно
|
||||
//Не всегда возвращается ErrorCode, если получали например наряд, он вернет тело наряда
|
||||
if (response.Contains("ErrorCode") && response.Contains("Message"))
|
||||
{
|
||||
if (!response.Contains("\"ErrorCode\":\"0\""))
|
||||
{
|
||||
logger.LogError(new Exception(response), $"Ошибка при выполнении запроса в ЕСПП. Тело запроса: {query}");
|
||||
|
||||
return new EsppResultBase<T>(null, false, new Exception($"Espp error: {response}"));
|
||||
}
|
||||
|
||||
// ахахах! Может быть "ErrorCode":"0", и "Message":"Успешно", но в "MessageESPP":"err - Операция 'Прочее(работы)' не найденаЗапись \"Трудозатраты и Время в пути\" добавлена."
|
||||
// "Message":"Успешно","ErrorCode":"0","MessageESPP":"err - Операция 'Прочее(работы)' не найденаЗапись \"Трудозатраты и Время в пути\" добавлена."
|
||||
// т е это ошибка. рука лицо и привет разрабам ЕСПП
|
||||
// делаем тупо, может стоит переделать
|
||||
if (response.Contains("\"MessageESPP\":\"err"))
|
||||
{
|
||||
logger.LogError(new Exception(response), $"Ошибка при выполнении запроса в ЕСПП. Тело запроса: {query}");
|
||||
|
||||
return new EsppResultBase<T>(null, false, new Exception($"Espp error: {response}"));
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user