Files
parr_api/PARR.EsppTemplateSync/Services/ParserService.cs

84 lines
3.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.DAL.Models;
using System.Globalization;
namespace PARR.EsppTemplateSync.Services
{
internal class ParserService : IParserService
{
private readonly ILogger<ParserService> logger;
public ParserService(ILogger<ParserService> logger)
{
this.logger = logger;
}
public async Task<bool> 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;
}
}
}