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

125 lines
5.0 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.DependencyInjection;
using Microsoft.Extensions.Logging;
using PARR.DAL.Models;
using PARR.DAL.Services.Interfaces;
using System.Collections;
using System.Globalization;
using System.Reflection;
namespace PARR.EsppTemplateSync.Services
{
internal class Manager : IManager
{
private readonly ILogger<Manager> logger;
private readonly IServiceProvider serviceProvider;
private readonly IParserService parserService;
public Manager(
ILogger<Manager> logger,
IServiceProvider serviceProvider,
IParserService parserService
)
{
this.logger = logger;
this.serviceProvider = serviceProvider;
this.parserService = parserService;
}
public Task<bool> ManageFileAsync(string path)
{
throw new NotImplementedException();
}
public async Task<bool> ManageStringAsync(string str)
{
bool isSuccess = true;
////TODO:
// тут обязательно нужна проверка, если str не того формата, возникает exception в ParseStringAsync
if (str == null || str.Trim().Length == 0)
return false;
var template = parserService.ParseString(str);
if (template == null) {
isSuccess = false;
logger.LogError($"Не удалось распознать строку({str}) в Template");
}
else {
//если шаблон не аквтивен, пропускаем
if (!template.isActive)
{
return isSuccess;
}
await CheckChangesAsync(template);
}
//todo: only true?????
return isSuccess;
}
private async Task CheckChangesAsync(Template esppTemplate)
{
// --- test ---
using (var scope = serviceProvider.CreateScope())
{
var services = scope.ServiceProvider;
var templateService = services.GetService<ITemplateService>();
if (templateService == null)
{
return;
}
var template = await templateService.GetTemplateByNameAsync(esppTemplate.Name);
//существует в ЕСПП но отсутствует в ПАРР.
//TODO деактивируем и видимо ещё что-то нужно
if (template == null)
{
esppTemplate.Status="Deactivating";
esppTemplate.isActive = false;
if (!await templateService.CreateAsync(esppTemplate) || !await templateService.CommitAsync())
{
logger.LogError($"Не удалось создать запись Template {esppTemplate.Name}({esppTemplate.ShortDescription})");
}
else
logger.LogInformation($"----- Создана запись Template {esppTemplate.Name}({esppTemplate.ShortDescription}) -----");
//TODO написать проверку успешности деактивации и удаление строки из таблицы Templates
}
else
{
var isChanged = false;
foreach (var prop in template.GetType().GetProperties())
{
if (prop.Name == "Id" ||
prop.Name == "Status" ||
prop.Name == "DateCreated" ||
prop.Name == "DateModified"
) continue;
if (!template.GetType().GetProperty(prop.Name).GetValue(template,null).Equals(
esppTemplate.GetType().GetProperty(prop.Name).GetValue(esppTemplate, null))
)
{
isChanged = true; break;
}
}
if (isChanged)
{
logger.LogInformation($"----- Запись в ЕСПП отличается от Template {esppTemplate.EK}({esppTemplate.ShortDescription}) -----");
template.Status = "Updating";
if(!await templateService.CommitAsync())
logger.LogError($"Не удалось обновить запись Template {esppTemplate.EK}({esppTemplate.ShortDescription})");
else
logger.LogInformation($"----- Требуется обновление шаблона в ЕСПП роботом {esppTemplate.EK}({esppTemplate.ShortDescription}) -----");
}
}
}
}
}
}