feat(api,dal,esppSync,esppTemplateSync,esppScheduleSync): Реализован сервис ShortcodesService подменяющий динамические составляющие в полях шаблонов и расписаний. В API контроллер выдачи заданий роботу теперь выдаёт значение полй в новом виде, реализуя Shortcodes.

This commit is contained in:
Mikhail Kuznetsov
2025-08-06 10:33:55 +10:00
parent 46a96c666e
commit f351a8863a
11 changed files with 106 additions and 58 deletions

View File

@@ -16,7 +16,7 @@ namespace PARR.EsppSync
/// <typeparam name="EsppObject"></typeparam>
/// <param name="templateName"></param>
/// <returns></returns>
public delegate Task<EsppObject> ConvertDbObjToComparisonObjHandlerDelegate<EsppObject>(Template template) where EsppObject : class, IEsppObject;
public delegate EsppObject ConvertDbObjToComparisonObjHandlerDelegate<EsppObject>(Template template) where EsppObject : class, IEsppObject;
public interface ISyncService<EsppObject> where EsppObject : class, IEsppObject
{

View File

@@ -2,23 +2,31 @@
using Microsoft.Extensions.Logging;
using PARR.Constants;
using PARR.DAL.Contracts;
using PARR.DAL.DomainServices;
using PARR.DAL.Models;
using PARR.DAL.Services.Interfaces;
using System.Reflection;
namespace PARR.EsppSync
{
internal class SyncService<EsppObject> : ISyncService<EsppObject> where EsppObject : class, IEsppObject
{
private readonly ILogger<SyncService<EsppObject>> logger;
private readonly IServiceProvider serviceProvider;
private readonly ITemplateService templateService;
private readonly IRobotConfigurationService robotConfigurationService;
private readonly IShortcodesService shortcodesService;
public SyncService(
ILogger<SyncService<EsppObject>> logger,
IServiceProvider serviceProvider
ITemplateService templateService,
IRobotConfigurationService robotConfigurationService,
IShortcodesService shortcodesService
)
{
this.logger = logger;
this.serviceProvider = serviceProvider;
this.templateService = templateService;
this.robotConfigurationService = robotConfigurationService;
this.shortcodesService = shortcodesService;
}
@@ -44,11 +52,8 @@ namespace PARR.EsppSync
return;
}
using (var scope = serviceProvider.CreateScope())
try
{
var templateService = GetServiceInScope<ITemplateService>(scope);
var robotConfigurationService = GetServiceInScope<IRobotConfigurationService>(scope);
var template = await templateService.GetTemplateByNameAsync(esppObject.TemplateName);
//todo: существует в ЕСПП но отсутствует в ПАРР. Может его деактивировать или еще что-то сделать. Пока просто пропустим
@@ -62,26 +67,36 @@ namespace PARR.EsppSync
{
var dbObjectInEsppObject = converterToEsppObject.Invoke(template);
//Проверяем наличие Shortcode в полях объекта из БД
var properties = dbObjectInEsppObject.GetType().GetProperties();
foreach (PropertyInfo property in properties)
{
var value = property.GetValue(dbObjectInEsppObject)?.ToString();
if (value != null && shortcodesService.isAnyShortcodes(value))
property.SetValue(dbObjectInEsppObject, await shortcodesService.ApplyShortcodesAsync(value, template.UnitId, template.JobId));
}
bool isChanged = false;
//TODO: FIX ME Please, BRO
//bool isChanged;
// если в БД isActive == false, то синхронизировать только по полям из IEsppObject
//if (dbObjectInEsppObject.IsActive == false)
//{
// logger.LogDebug($"Объект деактивирован в ПАРР. Сравниваем только обязательные поля. {esppObject.TemplateName}");
// var lightDbObj = new EsppLightObject(dbObjectInEsppObject);
// var lightEsppObject = new EsppLightObject(esppObject);
// isChanged = IsChanged(lightEsppObject, lightDbObj);
//}
//else
//{
// logger.LogDebug($"Объект активирован в ПАРР. Сравниваем все поля. {esppObject.TemplateName}");
// isChanged = IsChanged(esppObject, dbObjectInEsppObject);
//}
if (dbObjectInEsppObject.IsActive == false)
{
logger.LogDebug($"Объект деактивирован в ПАРР. Сравниваем только обязательные поля. {esppObject.TemplateName}");
var lightDbObj = new EsppLightObject(dbObjectInEsppObject);
var lightEsppObject = new EsppLightObject(esppObject);
isChanged = IsChanged(lightEsppObject, lightDbObj);
}
else
{
logger.LogDebug($"Объект активирован в ПАРР. Сравниваем все поля. {esppObject.TemplateName}");
isChanged = IsChanged(esppObject, dbObjectInEsppObject);
}
if (isChanged)
{
@@ -134,6 +149,10 @@ namespace PARR.EsppSync
}
}
}
catch (Exception ex)
{
logger.LogError(ex, $"Ошибка синхронизации объекта АСУ ЕСПП {esppObject.TemplateName}");
}
}