2025-08-19 11:13:56 +10:00
using Microsoft.EntityFrameworkCore ;
using PARR.BLL.Domain.Mq ;
using PARR.BLL.Services.Interfaces ;
using PARR.Common.Domain ;
using PARR.Constants ;
using PARR.DAL.DomainServices.Interfaces ;
using PARR.DAL.Models ;
using PARR.DAL.Models.Job ;
using PARR.DAL.Models.Unit ;
using PARR.DAL.Services.Interfaces ;
using PARR.DAL.Services.Interfaces.Job ;
2025-11-20 16:21:03 +10:00
using PARR.DAL.TransformServices ;
2025-08-19 11:13:56 +10:00
using PARR.TemplateGeneratorWorker.Services ;
namespace PARR.TemplateGeneratorWorker
{
internal class TemplateGenerator : ITemplateGenerator
{
private readonly ILogger < TemplateGenerator > logger ;
private readonly ITransformService transformService ;
private readonly IValidatorService validatorService ;
private readonly IJobService jobService ;
private readonly IShortcodesService shortcodesService ;
private readonly ITemplateService templateService ;
2025-11-20 16:21:03 +10:00
private readonly IEsppScheduleTransformService esppScheduleTransformService ;
2025-08-19 11:13:56 +10:00
public TemplateGenerator (
ILogger < TemplateGenerator > logger ,
ITransformService transformService ,
IValidatorService validatorService ,
IJobService jobService ,
IShortcodesService shortcodesService ,
2025-11-20 16:21:03 +10:00
ITemplateService templateService ,
IEsppScheduleTransformService esppScheduleTransformService
2025-08-19 11:13:56 +10:00
)
{
this . logger = logger ;
this . transformService = transformService ;
this . validatorService = validatorService ;
this . jobService = jobService ;
this . shortcodesService = shortcodesService ;
this . templateService = templateService ;
2025-11-20 16:21:03 +10:00
this . esppScheduleTransformService = esppScheduleTransformService ;
2025-08-19 11:13:56 +10:00
}
public async Task GenerateTemplateAsync ( string msg )
{
logger . LogInformation ( $"Получили запрос: {msg}" ) ;
var query = transformService . GetModelFromJson < TemplateGeneratorWorkerMq > ( msg ) ;
if ( query = = null )
return ;
2025-08-21 11:28:39 +10:00
2025-08-19 11:13:56 +10:00
if ( ! await validatorService . IsValidAsync ( query . JobId , query . UnitId ) )
{
logger . LogError ( $"Некорректные параметры регламентной работы или Unit {nameof(Job)}: {query.JobId}, {nameof(Unit)}: {query.UnitId}" ) ;
return ;
}
2025-11-20 16:21:03 +10:00
var existing = await templateService
. Get ( )
. AsNoTracking ( )
. FirstOrDefaultAsync ( t = > t . JobId = = query . JobId & & t . UnitId = = query . UnitId ) ;
if ( existing ! = null )
2025-08-21 11:28:39 +10:00
{
2025-11-20 16:21:03 +10:00
logger . LogWarning ( "Шаблон уже существует (Job={JobId}, Unit={UnitId}) → пропускаем Create" , query . JobId , query . UnitId ) ;
2025-08-21 11:28:39 +10:00
return ;
}
2025-08-19 11:13:56 +10:00
var job = await jobService
. Get ( ) . AsNoTracking ( )
2025-11-20 16:21:03 +10:00
. Include ( t = > t . Group )
2025-08-19 11:13:56 +10:00
. FirstOrDefaultAsync ( t = > t . Id = = query . JobId ) ;
2025-11-20 16:21:03 +10:00
if ( job = = null )
{
logger . LogError ( "Job не найден: {JobId}" , query . JobId ) ;
return ;
}
2025-08-21 11:28:39 +10:00
2025-11-20 16:21:03 +10:00
if ( job . Group = = null )
{
logger . LogError ( "Job {JobId} не привязан к JobGroup" , query . JobId ) ;
return ;
}
2025-08-21 11:28:39 +10:00
2025-08-19 11:13:56 +10:00
var templateName = await shortcodesService . ApplyShortcodesAsync ( job ! . TemplateNameMask ! , query . UnitId , query . JobId ) ;
2025-11-20 16:21:03 +10:00
var nextRun = await esppScheduleTransformService . GetNextDateAsync ( job . GroupId , job . Group ! . ReferenceDate ) ;
2025-08-19 11:13:56 +10:00
var template = new Template
{
Id = Guid . NewGuid ( ) ,
2025-11-28 15:32:06 +10:00
Name = templateName . ToUpper ( ) ,
2025-08-19 11:13:56 +10:00
UnitId = query . UnitId ,
JobId = query . JobId ,
IsActiveTemplate = query . IsActiveTemplate ? ? false ,
IsActiveSchedule = query . IsActiveSchedule ? ? false ,
2025-11-20 16:21:03 +10:00
NextRun = nextRun ,
2025-12-01 15:27:18 +10:00
//IsUnused = false,
StatusTypeId = TemplateStatusTypeEnum . Used ,
2025-08-19 11:13:56 +10:00
InitiatorComment = query . HistoryInitiator ? . InitiatorComment ,
InitiatorParrComponentId = query . HistoryInitiator ? . InitiatorParrComponentId
} ;
2025-11-20 16:23:38 +10:00
if ( await templateService . CreateAsync ( template ) & & await templateService . CommitAsync ( new HistoryInitiator { InitiatorComment = "Запрос на генерацию с тестового шаблона" , InitiatorParrComponentId = ParrComponentsEnum . TemplateTaskGenerator } ) )
2025-08-19 11:13:56 +10:00
{
2025-11-20 16:21:03 +10:00
logger . LogInformation ( "Создан шаблон: Id={TemplateId}, Name={Name}, Job={JobId}, Unit={UnitId}" ,
2025-11-20 16:23:38 +10:00
template . Id , template . Name , query . JobId , query . UnitId ) ;
2025-08-19 11:13:56 +10:00
}
else
{
2025-11-20 16:21:03 +10:00
logger . LogError ( "Ошибка создания шаблона: Name={Name}" , templateName ) ;
2025-08-19 11:13:56 +10:00
}
}
}
}