Files
parr_api/PARR.EsppApi/EsppApiService.cs

102 lines
5.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.Extensions.Logging;
using PARR.EsppApi.Models;
using PARR.EsppApi.Models.Query;
using PARR.EsppApi.Settings;
namespace PARR.EsppApi
{
internal class EsppApiService : IEsppApiService
{
private readonly ILogger<EsppApiService> logger;
private readonly EsppOrderSettings esppOrderSettings;
private readonly EsppHttpService esppHttpService;
public EsppApiService(ILogger<EsppApiService> logger, EsppOrderSettings esppOrderSettings, EsppHttpService esppHttpService)
{
this.logger = logger;
this.esppOrderSettings = esppOrderSettings;
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 result = await esppHttpService.SendAsync<EsppResponse>(query);
if (result.Data != null && result.Data.ErrorCode != "0")
return new EsppResultBase<EsppResponse>(null, false, new Exception(result.Data.Message));
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
return result;
}
public async Task<EsppResultBase<IEnumerable<EsppOrder>>> FindOrdersAsync(FindOrdersQuery query)
{
DateTime? generateDateStartWOTimeZone = query.GenerateDateStart.HasValue ? query.GenerateDateStart.Value.AddHours(esppOrderSettings.EsppUserTimeZone) : null;
DateTime? generateDateEndWOTimeZone = query.GenerateDateEnd.HasValue ? query.GenerateDateEnd.Value.AddHours(esppOrderSettings.EsppUserTimeZone) : null;
var conditions = new List<string>();
if (!string.IsNullOrEmpty(query.DescriptionContains))
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.Any())
{
logger.LogDebug("Пришёл пустой запрос в EsppHttpService.FindOrdersAsync");
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>");
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>");
if (result.Data != null && result.Data.ErrorCode != "0")
return new EsppResultBase<EsppResponse>(null, false, new Exception(result.Data.Message));
return result;
}
}
}