2025-12-19 19:15:58 +10:00
using Microsoft.EntityFrameworkCore ;
2026-04-14 16:27:27 +10:00
using PARR.Core.Common.Interfaces ;
2026-04-30 10:35:31 +10:00
using PARR.Core.Repositories.Interfaces ;
2026-07-03 11:17:48 +10:00
using PARR.Core.Repositories.Interfaces.JobRepositories ;
2026-05-04 11:55:07 +10:00
using PARR.Core.Services.NextRunServices ;
using PARR.Core.Services.Shortcodes ;
2026-05-14 16:47:27 +10:00
using PARR.Domain.Common.Rabbit.Messages.TemplateMatching ;
2026-04-30 10:35:31 +10:00
using PARR.Domain.Entities ;
2026-04-13 16:58:30 +10:00
using PARR.Domain.Entities.Base.History ;
2026-07-03 11:17:48 +10:00
using PARR.Domain.Entities.JobEntities ;
2026-04-13 16:58:30 +10:00
using PARR.Domain.Enums ;
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 ;
2026-04-30 10:35:31 +10:00
private readonly IJobRepository jobService ;
2025-08-19 11:13:56 +10:00
private readonly IShortcodesService shortcodesService ;
2026-04-30 10:35:31 +10:00
private readonly ITemplateRepository templateService ;
2026-03-05 10:19:54 +10:00
private readonly INextRunService nextRunService ;
2026-02-26 14:42:53 +10:00
2025-08-19 11:13:56 +10:00
public TemplateGenerator (
ILogger < TemplateGenerator > logger ,
ITransformService transformService ,
IValidatorService validatorService ,
2026-04-30 10:35:31 +10:00
IJobRepository jobService ,
2025-08-19 11:13:56 +10:00
IShortcodesService shortcodesService ,
2026-04-30 10:35:31 +10:00
ITemplateRepository templateService ,
2026-03-05 10:19:54 +10:00
INextRunService nextRunService
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 ;
2026-01-22 12:06:57 +10:00
this . nextRunService = nextRunService ;
2025-08-19 11:13:56 +10:00
}
2025-12-19 19:15:58 +10:00
2026-05-14 18:06:19 +10:00
2025-08-19 11:13:56 +10:00
public async Task GenerateTemplateAsync ( string msg )
{
logger . LogInformation ( $"Получили запрос: {msg}" ) ;
2026-05-14 16:47:27 +10:00
var query = transformService . GetModelFromJson < TemplateGeneratorMessage > ( 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
// Проверяем всё
2026-05-14 18:06:19 +10:00
var unitsInTemplate = query . UnitsInTemplate . Select ( s = > ( s . UnitId , s . UnitFieldValueId ) ) . ToList ( ) ;
if ( ! await validatorService . IsValidAsync ( query . JobId , query . UnitId , query . Index , 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 )
2026-01-16 18:26:06 +10:00
. ThenInclude ( g = > g ! . GroupType )
. Include ( t = > t . Tnk )
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
2026-01-16 18:26:06 +10:00
// === Создаём временный Template для подстановки шорткодов ===
var tempTemplateForShortcodes = new Template
2025-12-23 18:27:31 +10:00
{
2026-01-16 18:26:06 +10:00
Id = Guid . Empty , // ещё не создан
2026-05-15 15:11:04 +10:00
Name = string . Empty , // не используется
2025-12-23 18:27:31 +10:00
JobId = query . JobId ,
UnitId = query . UnitId ,
2026-01-16 18:26:06 +10:00
Index = query . Index ,
Job = job ,
Unit = null , // ShortcodesService сам догрузит при необходимости
2026-05-14 18:06:19 +10:00
UnitsInTemplate = query . UnitsInTemplate ? . Select ( uit = > new UnitsInTemplate { UnitId = uit . UnitId , UnitFieldValueId = uit . UnitFieldValueId } ) . ToList ( ) ? ? new List < UnitsInTemplate > ( )
2025-12-23 18:27:31 +10:00
} ;
2026-01-16 18:26:06 +10:00
var templateName = await shortcodesService . ApplyShortcodesAsync ( job . TemplateNameMask ! , tempTemplateForShortcodes ) ;
2025-08-19 11:13:56 +10:00
2026-02-05 15:41:36 +10:00
var responseArea = await shortcodesService . ApplyShortcodesAsync ( job . ResponseAreaMask , tempTemplateForShortcodes ) ;
2026-02-26 14:42:53 +10:00
var workGroup = await shortcodesService . ApplyShortcodesAsync ( job . WorkGroupMask , tempTemplateForShortcodes ) ;
var nextRun = await nextRunService . GetNextRunForNewTemplateAsync ( job . GroupId , workGroup , responseArea ) ;
if ( ! nextRun . HasValue )
{
logger . LogError ( "Job {JobId}, пытался создать шаблон с именем {templateName}, при получении nextRun вернулся null" , query . JobId , templateName ) ;
return ;
}
2025-11-20 16:21:03 +10:00
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 ,
2026-02-26 14:42:53 +10:00
NextRun = nextRun . Value ,
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 ,
2026-01-16 18:26:06 +10:00
Index = query . Index ,
2026-05-14 18:06:19 +10:00
UnitsInTemplate = query . UnitsInTemplate ? . Select ( uit = > new UnitsInTemplate { UnitId = uit . UnitId , UnitFieldValueId = uit . UnitFieldValueId } ) . ToList ( ) ? ? new List < UnitsInTemplate > ( )
2025-08-19 11:13:56 +10:00
} ;
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
}