2023-09-21 15:54:46 +10:00
|
|
|
|
using AutoMapper;
|
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2023-09-01 16:19:32 +10:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2023-09-14 17:00:56 +10:00
|
|
|
|
using PARR.DAL.Contracts;
|
2023-09-01 12:40:41 +10:00
|
|
|
|
using PARR.DAL.Models;
|
2023-09-01 16:19:32 +10:00
|
|
|
|
using PARR.DAL.Services.Interfaces;
|
2023-09-21 15:54:46 +10:00
|
|
|
|
using PARR.EsppTemplateSync.Domain;
|
2023-09-20 10:11:00 +10:00
|
|
|
|
using PARR.EsppTemplateSync.Settings;
|
2023-09-01 11:47:39 +10:00
|
|
|
|
|
|
|
|
|
|
namespace PARR.EsppTemplateSync.Services
|
2023-08-31 12:04:05 +10:00
|
|
|
|
{
|
|
|
|
|
|
internal class Manager : IManager
|
|
|
|
|
|
{
|
2023-09-01 11:47:39 +10:00
|
|
|
|
private readonly ILogger<Manager> logger;
|
2023-09-01 16:19:32 +10:00
|
|
|
|
private readonly IServiceProvider serviceProvider;
|
2023-09-01 11:47:39 +10:00
|
|
|
|
private readonly IParserService parserService;
|
2023-09-20 10:11:00 +10:00
|
|
|
|
private readonly GlobalSettings globalSettings;
|
2023-09-21 15:54:46 +10:00
|
|
|
|
private readonly IMapper mapper;
|
2023-10-06 17:09:25 +10:00
|
|
|
|
private readonly IRobotConfigurationService robotConfigurationService;
|
2023-09-01 11:47:39 +10:00
|
|
|
|
|
2023-09-01 16:19:32 +10:00
|
|
|
|
public Manager(
|
|
|
|
|
|
ILogger<Manager> logger,
|
|
|
|
|
|
IServiceProvider serviceProvider,
|
2023-09-20 10:11:00 +10:00
|
|
|
|
IParserService parserService,
|
2023-09-21 15:54:46 +10:00
|
|
|
|
GlobalSettings globalSettings,
|
2023-10-06 17:09:25 +10:00
|
|
|
|
IMapper mapper,
|
|
|
|
|
|
IRobotConfigurationService robotConfigurationService
|
2023-09-01 16:19:32 +10:00
|
|
|
|
)
|
2023-09-01 11:47:39 +10:00
|
|
|
|
{
|
|
|
|
|
|
this.logger = logger;
|
2023-09-01 16:19:32 +10:00
|
|
|
|
this.serviceProvider = serviceProvider;
|
2023-09-01 11:47:39 +10:00
|
|
|
|
this.parserService = parserService;
|
2023-09-20 10:11:00 +10:00
|
|
|
|
this.globalSettings = globalSettings;
|
2023-09-21 15:54:46 +10:00
|
|
|
|
this.mapper = mapper;
|
2023-10-06 17:09:25 +10:00
|
|
|
|
this.robotConfigurationService = robotConfigurationService;
|
2023-09-01 11:47:39 +10:00
|
|
|
|
}
|
2023-09-04 12:17:50 +10:00
|
|
|
|
|
2023-08-31 12:04:05 +10:00
|
|
|
|
public Task<bool> ManageFileAsync(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-29 15:01:34 +10:00
|
|
|
|
public async Task ManageStringAsync(string str)
|
2023-08-31 12:04:05 +10:00
|
|
|
|
{
|
2023-09-29 15:01:34 +10:00
|
|
|
|
|
2023-09-04 12:17:50 +10:00
|
|
|
|
// тут обязательно нужна проверка, если str не того формата, возникает exception в ParseStringAsync
|
2023-09-29 15:01:34 +10:00
|
|
|
|
if (string.IsNullOrEmpty(str))
|
|
|
|
|
|
return;
|
2023-09-04 16:26:43 +10:00
|
|
|
|
|
2023-09-21 13:41:55 +10:00
|
|
|
|
var esppTemplate = parserService.ParseString(str);
|
2023-09-04 16:26:43 +10:00
|
|
|
|
|
2023-09-21 13:41:55 +10:00
|
|
|
|
if (esppTemplate == null)
|
2023-09-14 17:00:56 +10:00
|
|
|
|
{
|
2023-09-29 15:01:34 +10:00
|
|
|
|
return;
|
2023-09-04 16:26:43 +10:00
|
|
|
|
}
|
2023-09-14 17:00:56 +10:00
|
|
|
|
else
|
|
|
|
|
|
{
|
2023-09-04 16:26:43 +10:00
|
|
|
|
//если шаблон не аквтивен, пропускаем
|
2023-09-21 15:54:46 +10:00
|
|
|
|
//if (!esppTemplate.IsActive)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// return isSuccess;
|
|
|
|
|
|
//}
|
2023-09-14 17:00:56 +10:00
|
|
|
|
|
2023-09-21 13:41:55 +10:00
|
|
|
|
await SyncTemplateAsync(esppTemplate);
|
|
|
|
|
|
|
2023-09-04 16:26:43 +10:00
|
|
|
|
}
|
2023-09-01 12:40:41 +10:00
|
|
|
|
|
2023-09-29 15:01:34 +10:00
|
|
|
|
return;
|
2023-08-31 12:04:05 +10:00
|
|
|
|
}
|
2023-09-01 12:40:41 +10:00
|
|
|
|
|
2023-09-21 13:41:55 +10:00
|
|
|
|
|
2023-09-21 15:54:46 +10:00
|
|
|
|
private async Task SyncTemplateAsync(EsppTemplate esppTemplate)
|
2023-09-21 13:41:55 +10:00
|
|
|
|
{
|
2023-09-04 12:17:50 +10:00
|
|
|
|
using (var scope = serviceProvider.CreateScope())
|
|
|
|
|
|
{
|
|
|
|
|
|
var services = scope.ServiceProvider;
|
2023-09-15 10:17:17 +10:00
|
|
|
|
|
2023-09-04 12:17:50 +10:00
|
|
|
|
var templateService = services.GetService<ITemplateService>();
|
|
|
|
|
|
|
|
|
|
|
|
if (templateService == null)
|
|
|
|
|
|
{
|
2023-09-15 10:17:17 +10:00
|
|
|
|
throw new Exception($"Не найден сервис: {nameof(ITemplateService)}");
|
|
|
|
|
|
// return;
|
2023-09-04 12:17:50 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-21 15:54:46 +10:00
|
|
|
|
Template? template = await templateService.GetTemplateByNameAsync(esppTemplate.Name);
|
2023-09-04 16:26:43 +10:00
|
|
|
|
//существует в ЕСПП но отсутствует в ПАРР.
|
|
|
|
|
|
//TODO деактивируем и видимо ещё что-то нужно
|
|
|
|
|
|
if (template == null)
|
|
|
|
|
|
{
|
2023-09-15 10:17:17 +10:00
|
|
|
|
// создает запись-шаблон из ЕСПП в ПАРР. Возможно избыточно так как эталон в ПАРР
|
|
|
|
|
|
////esppTemplate.StatusCode = "Deactivating";
|
|
|
|
|
|
////---
|
|
|
|
|
|
//esppTemplate.StatusCode = (int)StatusTemplateEnum.Ok;
|
|
|
|
|
|
//esppTemplate.IsActive = false;
|
2023-09-14 15:07:19 +10:00
|
|
|
|
//if (!await templateService.CreateAsync(esppTemplate) || !await templateService.CommitAsync())
|
|
|
|
|
|
//{
|
|
|
|
|
|
// logger.LogError($"Не удалось создать запись Template {esppTemplate.Name}({esppTemplate.ShortDescription})");
|
|
|
|
|
|
//}
|
|
|
|
|
|
//else
|
|
|
|
|
|
// logger.LogInformation($"----- Создана запись Template {esppTemplate.Name}({esppTemplate.ShortDescription}) -----");
|
2023-09-15 10:17:17 +10:00
|
|
|
|
//---
|
2023-09-21 15:54:46 +10:00
|
|
|
|
logger.LogWarning($"----- Найден шаблон ЕСПП незарегистрированный в ПАРР {esppTemplate.Name}({esppTemplate.ShortDescription}) -----");
|
2023-09-21 13:41:55 +10:00
|
|
|
|
return;
|
2023-09-04 16:26:43 +10:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2023-09-21 15:54:46 +10:00
|
|
|
|
var mappedTemplate = mapper.Map<EsppTemplate>(template);
|
2023-10-04 14:08:52 +10:00
|
|
|
|
if (mappedTemplate == null)
|
2023-09-21 15:54:46 +10:00
|
|
|
|
return;
|
|
|
|
|
|
var isChanged = IsChanged(mappedTemplate, esppTemplate);
|
2023-09-04 16:26:43 +10:00
|
|
|
|
|
2023-09-21 13:41:55 +10:00
|
|
|
|
if (isChanged)
|
2023-09-04 16:26:43 +10:00
|
|
|
|
{
|
2023-10-06 17:09:25 +10:00
|
|
|
|
SetUpdateStatus(template);
|
2023-09-14 15:07:19 +10:00
|
|
|
|
|
2023-09-21 13:41:55 +10:00
|
|
|
|
if (!await templateService.CommitAsync())
|
2023-09-04 16:26:43 +10:00
|
|
|
|
{
|
2023-09-21 13:41:55 +10:00
|
|
|
|
logger.LogError($"Не удалось изменить запись Template {template.Name}");
|
2023-09-04 16:26:43 +10:00
|
|
|
|
}
|
2023-09-21 13:41:55 +10:00
|
|
|
|
else
|
|
|
|
|
|
logger.LogInformation($"----- Установлен принудительный статус обновления Template {template.Name} -----");
|
2023-10-04 14:08:52 +10:00
|
|
|
|
} //надо ли проверять если не изменился, но был статус Updating не понятно. Доверяем роботу пока, что после окончания работ от точно сообщит
|
2023-09-21 13:41:55 +10:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-09-04 16:26:43 +10:00
|
|
|
|
|
2023-09-21 13:41:55 +10:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2023-09-14 17:00:56 +10:00
|
|
|
|
|
2023-10-06 17:09:25 +10:00
|
|
|
|
private void SetUpdateStatus(Template template)
|
2023-09-21 13:41:55 +10:00
|
|
|
|
{
|
2023-10-06 17:09:25 +10:00
|
|
|
|
var robotConfig = robotConfigurationService.GetFromTemplateByRobotCode(RobotsEnum.TemplateOrder, ref template);
|
|
|
|
|
|
robotConfigurationService.ChangeTaskStatus(TaskStatusEnum.Updating, ref robotConfig);
|
2023-09-21 13:41:55 +10:00
|
|
|
|
}
|
2023-09-14 17:00:56 +10:00
|
|
|
|
|
2023-09-21 15:54:46 +10:00
|
|
|
|
private bool IsChanged(EsppTemplate template, EsppTemplate esppTemplate)
|
2023-09-21 13:41:55 +10:00
|
|
|
|
{
|
|
|
|
|
|
foreach (var prop in template.GetType().GetProperties())
|
|
|
|
|
|
{
|
|
|
|
|
|
if (prop == null) continue;
|
2023-09-15 10:17:17 +10:00
|
|
|
|
|
2023-09-21 13:41:55 +10:00
|
|
|
|
if (globalSettings.IgnoreTemplateFields != null && globalSettings.IgnoreTemplateFields.Contains(prop.Name)) continue;
|
2023-09-04 12:17:50 +10:00
|
|
|
|
|
2023-09-21 13:41:55 +10:00
|
|
|
|
var parrValue = template.GetType().GetProperty(prop.Name)?.GetValue(template, null);
|
|
|
|
|
|
var esppValue = esppTemplate.GetType().GetProperty(prop.Name)?.GetValue(esppTemplate, null);
|
|
|
|
|
|
|
|
|
|
|
|
if (parrValue == null || esppValue == null)
|
|
|
|
|
|
continue;
|
2023-09-01 16:19:32 +10:00
|
|
|
|
|
2023-09-21 15:54:46 +10:00
|
|
|
|
if (parrValue!.ToString()!.ToLower() != esppValue!.ToString()!.ToLower())
|
|
|
|
|
|
return true;
|
2023-09-21 13:41:55 +10:00
|
|
|
|
|
|
|
|
|
|
}
|
2023-10-04 14:08:52 +10:00
|
|
|
|
return false;
|
2023-09-01 12:40:41 +10:00
|
|
|
|
}
|
2023-08-31 12:04:05 +10:00
|
|
|
|
}
|
|
|
|
|
|
}
|