using Microsoft.Extensions.Logging; using PARR.DAL.Models; using System.Globalization; namespace PARR.EsppTemplateSync.Services { internal class ParserService : IParserService { private readonly ILogger logger; public ParserService(ILogger logger) { this.logger = logger; } public async Task ParseFileAsync(string path) { //TODO: bool isSuccess = true; return isSuccess; } public Template? ParseString(string str) { //TODO: а если формат нет тот? Все будет ок? //TODO: var splittedContent = str.Split(";"); if (splittedContent.Length != 8) return null; var ek = splittedContent[0].Trim().Substring(1, splittedContent[0].Length - 2); var isActive = splittedContent[1].Trim().Substring(1, splittedContent[1].Length - 2); var workGroup = splittedContent[2].Trim().Substring(1, splittedContent[2].Length - 2); var shortDescription = splittedContent[3].Trim().Substring(1, splittedContent[3].Length - 2); var category = splittedContent[4].Trim().Substring(1, splittedContent[4].Length - 2); var ekDateCreatedStr = splittedContent[5].Trim().Substring(1, splittedContent[5].Length - 2); var process = splittedContent[6].Trim().Substring(1, splittedContent[6].Length - 2); var subProcess = splittedContent[7].Trim().Substring(1, splittedContent[7].Length - 2); //logger.LogInformation($"Получено сообщение: {ek}"); var template = new Template { Id = Guid.NewGuid(), DateCreated = DateTime.UtcNow, EK = ek, isActive = bool.Parse(isActive), WorkGroup = workGroup, ShortDescription = shortDescription, Category = category, //todo: EKDateCreated EKDateCreated = ConvertDateFromStr(ekDateCreatedStr), //EKDateCreated = null, Process = process, SubProcess = subProcess, Status = "Checking" }; //bool isSuccess = true; return template; } private DateTimeOffset? ConvertDateFromStr(string dateString) { var format = "dd/MM/yy HH:mm:ss"; var provider = CultureInfo.CurrentCulture; try { var result = DateTimeOffset.ParseExact(dateString, format, provider).ToOffset(new TimeSpan(0)); logger.LogInformation($"{dateString} было преобразовано в {result.ToString()}."); return result; } catch (FormatException) { logger.LogWarning($"{dateString} имеет некоррктный формат для преобразование в DateTimeOffset"); } return null; } } }