2023-09-01 11:47:39 +10:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2023-09-29 15:19:05 +10:00
|
|
|
|
using PARR.DAL.Contracts;
|
2023-09-21 15:54:46 +10:00
|
|
|
|
using PARR.EsppTemplateSync.Domain;
|
2023-09-20 17:07:17 +10:00
|
|
|
|
using PARR.EsppTemplateSync.Settings;
|
2023-09-01 12:40:41 +10:00
|
|
|
|
using System.Globalization;
|
2023-09-01 11:47:39 +10:00
|
|
|
|
|
|
|
|
|
|
namespace PARR.EsppTemplateSync.Services
|
2023-08-25 15:32:06 +10:00
|
|
|
|
{
|
|
|
|
|
|
internal class ParserService : IParserService
|
|
|
|
|
|
{
|
2023-09-01 11:47:39 +10:00
|
|
|
|
private readonly ILogger<ParserService> logger;
|
2023-09-20 17:07:17 +10:00
|
|
|
|
private readonly GlobalSettings globalSettings;
|
2023-09-29 15:19:05 +10:00
|
|
|
|
private readonly SettingsFromDb settingsFromDb;
|
2023-09-01 11:47:39 +10:00
|
|
|
|
|
2023-09-20 17:07:17 +10:00
|
|
|
|
public ParserService(
|
|
|
|
|
|
ILogger<ParserService> logger,
|
2023-09-29 15:19:05 +10:00
|
|
|
|
GlobalSettings globalSettings,
|
|
|
|
|
|
SettingsFromDb settingsFromDb
|
2023-09-20 17:07:17 +10:00
|
|
|
|
)
|
2023-09-01 11:47:39 +10:00
|
|
|
|
{
|
|
|
|
|
|
this.logger = logger;
|
2023-09-20 17:07:17 +10:00
|
|
|
|
this.globalSettings = globalSettings;
|
2023-09-29 15:19:05 +10:00
|
|
|
|
this.settingsFromDb = settingsFromDb;
|
2023-09-01 11:47:39 +10:00
|
|
|
|
}
|
2023-08-31 12:04:05 +10:00
|
|
|
|
public async Task<bool> ParseFileAsync(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
//TODO:
|
|
|
|
|
|
|
|
|
|
|
|
bool isSuccess = true;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return isSuccess;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-21 15:54:46 +10:00
|
|
|
|
public EsppTemplate? ParseString(string str)
|
2023-08-25 15:32:06 +10:00
|
|
|
|
{
|
2023-09-29 15:01:34 +10:00
|
|
|
|
// TODO: а если формат нет тот? Все будет ок?
|
2023-09-20 17:07:17 +10:00
|
|
|
|
var splittedContent = str.Split(globalSettings.ParsingSeparator);
|
2023-09-13 14:44:21 +10:00
|
|
|
|
if (splittedContent.Length != 17)
|
|
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//var name = splittedContent[0].Trim().Substring(1, splittedContent[0].Length - 2);
|
|
|
|
|
|
var name = splittedContent[0].Trim();
|
2023-09-29 15:01:34 +10:00
|
|
|
|
|
|
|
|
|
|
CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
|
2023-09-29 15:19:05 +10:00
|
|
|
|
if (!string.IsNullOrEmpty(settingsFromDb.TemplatePrefixName)
|
|
|
|
|
|
&& !name.StartsWith(settingsFromDb.TemplatePrefixName, true, currentCulture)
|
2023-09-29 15:01:34 +10:00
|
|
|
|
)
|
|
|
|
|
|
{
|
2023-09-29 15:19:05 +10:00
|
|
|
|
logger.LogWarning($"Имя шаблона не соответствует обязательному префиксу({settingsFromDb.TemplatePrefixName}). Шаблон {name} игнорирован");
|
2023-09-29 15:01:34 +10:00
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!bool.TryParse(splittedContent[1].Trim(), out var isActive))
|
|
|
|
|
|
isActive = false;
|
2023-09-13 14:44:21 +10:00
|
|
|
|
var workGroup = splittedContent[2].Trim();
|
|
|
|
|
|
var shortDescription = splittedContent[3].Trim();
|
|
|
|
|
|
var category = splittedContent[4].Trim();
|
|
|
|
|
|
var responseArea = splittedContent[5].Trim();
|
|
|
|
|
|
var duration = splittedContent[6].Trim();
|
|
|
|
|
|
var ek = splittedContent[7].Trim();
|
|
|
|
|
|
var initiator = splittedContent[8].Trim();
|
2023-09-04 16:26:43 +10:00
|
|
|
|
|
2023-09-13 14:44:21 +10:00
|
|
|
|
var fullDescription = splittedContent[9].Trim();
|
|
|
|
|
|
var closingCode = splittedContent[10].Trim();
|
|
|
|
|
|
var solution = splittedContent[11].Trim();
|
|
|
|
|
|
var process = splittedContent[12].Trim();
|
|
|
|
|
|
var subProcess = splittedContent[13].Trim();
|
|
|
|
|
|
var tnk = splittedContent[14].Trim();
|
|
|
|
|
|
var work = splittedContent[15].Trim();
|
|
|
|
|
|
var worker = splittedContent[16].Trim();
|
2023-08-25 15:32:06 +10:00
|
|
|
|
|
2023-09-21 15:54:46 +10:00
|
|
|
|
var template = new EsppTemplate
|
2023-09-01 12:40:41 +10:00
|
|
|
|
{
|
2023-09-13 14:44:21 +10:00
|
|
|
|
Name = name,
|
2023-09-29 15:01:34 +10:00
|
|
|
|
IsActive = isActive,
|
2023-09-21 15:54:46 +10:00
|
|
|
|
WorkGroup = workGroup,
|
|
|
|
|
|
ShortDescription = shortDescription,
|
|
|
|
|
|
//Category = category,
|
|
|
|
|
|
ResponseArea = responseArea,
|
|
|
|
|
|
Duration = duration,
|
|
|
|
|
|
EK = ek,
|
|
|
|
|
|
//Initiator = initiator,
|
|
|
|
|
|
FullDescription = fullDescription,
|
|
|
|
|
|
//ClosingCode = closingCode,
|
|
|
|
|
|
Solution = solution,
|
|
|
|
|
|
Process = process,
|
|
|
|
|
|
SubProcess = subProcess,
|
|
|
|
|
|
TNK = tnk,
|
|
|
|
|
|
Work = work,
|
|
|
|
|
|
//Worker = worker
|
|
|
|
|
|
|
2023-09-01 12:40:41 +10:00
|
|
|
|
};
|
2023-08-25 15:32:06 +10:00
|
|
|
|
|
2023-09-01 12:40:41 +10:00
|
|
|
|
return template;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-29 15:01:34 +10:00
|
|
|
|
|
|
|
|
|
|
|
2023-09-01 12:40:41 +10:00
|
|
|
|
private DateTimeOffset? ConvertDateFromStr(string dateString)
|
|
|
|
|
|
{
|
2023-09-04 16:26:43 +10:00
|
|
|
|
var format = "dd/MM/yy HH:mm:ss";
|
2023-09-01 12:40:41 +10:00
|
|
|
|
var provider = CultureInfo.CurrentCulture;
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2023-09-04 16:26:43 +10:00
|
|
|
|
var result = DateTimeOffset.ParseExact(dateString, format, provider).ToOffset(new TimeSpan(0));
|
2023-09-01 16:19:32 +10:00
|
|
|
|
logger.LogInformation($"{dateString} было преобразовано в {result.ToString()}.");
|
2023-09-01 12:40:41 +10:00
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (FormatException)
|
|
|
|
|
|
{
|
2023-09-01 16:19:32 +10:00
|
|
|
|
logger.LogWarning($"{dateString} имеет некоррктный формат для преобразование в DateTimeOffset");
|
2023-09-01 12:40:41 +10:00
|
|
|
|
}
|
|
|
|
|
|
return null;
|
2023-08-25 15:32:06 +10:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|