2025-12-19 19:15:58 +10:00
using Microsoft.EntityFrameworkCore ;
2025-08-19 11:13:56 +10:00
using PARR.BLL.Domain.Mq ;
using PARR.BLL.Services.Interfaces ;
using PARR.Common.Domain ;
using PARR.Constants ;
2025-12-23 18:27:31 +10:00
using PARR.DAL.DomainServices.Shortcodes ;
using PARR.DAL.DomainServices.Shortcodes.Models ;
2025-08-19 11:13:56 +10:00
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
}
2025-12-19 19:15:58 +10:00
2025-08-19 11:13:56 +10:00
public async Task GenerateTemplateAsync ( string msg )
{
logger . LogInformation ( $"Получили запрос: {msg}" ) ;
2025-12-19 19:15:58 +10:00
var query = transformService . GetModelFromJson < TemplateGeneratorMq > ( msg ) ;
2025-08-19 11:13:56 +10:00
if ( query = = null )
{
2025-12-19 19:15:58 +10:00
logger . LogError ( "Н е удалось десериализовать запрос: {Message}" , msg ) ;
2025-08-19 11:13:56 +10:00
return ;
}
2025-12-19 19:15:58 +10:00
// Проверяем всё
if ( ! await validatorService . IsValidAsync ( query . JobId , query . UnitId , query . Index , query . UnitsInTemplate ) )
2025-08-21 11:28:39 +10:00
{
2025-12-19 19:15:58 +10:00
logger . LogError ( $"Параметры запроса не прошли валидацию: JobId={query.JobId}, UnitId={query.UnitId}, Index={query.Index}, UnitsInTemplateCount={query.UnitsInTemplate?.Count ?? 0}" ) ;
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-12-23 18:27:31 +10:00
var templateForShortcodes = new TemplateForShortcodes
{
Id = Guid . Empty , // шаблон ещё не создан
Index = query . Index ,
JobId = query . JobId ,
UnitId = query . UnitId ,
Job = job = = null ? null : new JobForShortcodes
{
Group = job . Group = = null ? null : new JobGroupForShortcodes
{
GroupingUnitFieldId = job . Group . GroupingUnitFieldId ,
GroupType = job . Group . GroupType = = null ? null : new JobGroupTypeForShortcodes
{
Code = job . Group . GroupType . Code
} ,
GroupName = job . Group . GroupName
} ,
Tnk = job . Tnk = = null ? null : new TnkForShortcodes
{
Name = job . Tnk . Name ,
ShortName = job . Tnk . ShortName
} ,
WorkName = job . WorkName ,
Name = job . Name
} ,
UnitsInTemplate = query . UnitsInTemplate ? . Select ( guid = > new UnitInTemplateForShortcodes { UnitId = guid } ) . ToList ( ) ? ? new List < UnitInTemplateForShortcodes > ( )
} ;
var templateName = await shortcodesService . ApplyShortcodesAsync ( job ! . TemplateNameMask ! , templateForShortcodes ) ;
2025-08-19 11:13:56 +10:00
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
StatusTypeId = TemplateStatusTypeEnum . Used ,
2025-08-19 11:13:56 +10:00
InitiatorComment = query . HistoryInitiator ? . InitiatorComment ,
2025-12-19 19:15:58 +10:00
InitiatorParrComponentId = query . HistoryInitiator ? . InitiatorParrComponentId ,
Index = query . Index
2025-08-19 11:13:56 +10:00
} ;
2025-12-19 19:15:58 +10:00
// Устанавливаем UnitsInTemplate
if ( query . UnitsInTemplate ! = null & & query . UnitsInTemplate . Any ( ) )
{
template . UnitsInTemplate = query . UnitsInTemplate . Select ( unitId = > new UnitsInTemplate { UnitId = unitId } ) . ToList ( ) ;
}
else
{
// Если список пуст, все равно инициализируем коллекцию, чтобы избежать NullReferenceException при сохранении (если это не nullable)
template . UnitsInTemplate = new List < UnitsInTemplate > ( ) ;
}
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-12-19 19:15:58 +10:00
logger . LogInformation ( "Создан шаблон: Id={TemplateId}, Name={Name}, Job={JobId}, Unit={UnitId}, Index={Index}, UnitsInTemplateCount={UnitsCount}" ,
template . Id , template . Name , query . JobId , query . UnitId , query . Index , template . UnitsInTemplate . Count ) ;
2025-08-19 11:13:56 +10:00
}
else
{
2025-12-19 19:15:58 +10:00
logger . LogError ( "Ошибка создания шаблона: Name={Name}, JobId={JobId}, UnitId={UnitId}, Index={Index}" , templateName , query . JobId , query . UnitId , query . Index ) ;
2025-08-19 11:13:56 +10:00
}
}
}
2025-12-19 19:15:58 +10:00
}