123 lines
4.9 KiB
C#
123 lines
4.9 KiB
C#
|
||
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;
|
||
using PARR.DAL.TransformServices;
|
||
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;
|
||
private readonly IEsppScheduleTransformService esppScheduleTransformService;
|
||
|
||
public TemplateGenerator(
|
||
ILogger<TemplateGenerator> logger,
|
||
ITransformService transformService,
|
||
IValidatorService validatorService,
|
||
IJobService jobService,
|
||
IShortcodesService shortcodesService,
|
||
ITemplateService templateService,
|
||
IEsppScheduleTransformService esppScheduleTransformService
|
||
)
|
||
{
|
||
this.logger = logger;
|
||
this.transformService = transformService;
|
||
this.validatorService = validatorService;
|
||
this.jobService = jobService;
|
||
this.shortcodesService = shortcodesService;
|
||
this.templateService = templateService;
|
||
this.esppScheduleTransformService = esppScheduleTransformService;
|
||
}
|
||
public async Task GenerateTemplateAsync(string msg)
|
||
{
|
||
logger.LogInformation($"Получили запрос: {msg}");
|
||
|
||
var query = transformService.GetModelFromJson<TemplateGeneratorWorkerMq>(msg);
|
||
|
||
if (query == null)
|
||
return;
|
||
|
||
|
||
if (!await validatorService.IsValidAsync(query.JobId, query.UnitId))
|
||
{
|
||
logger.LogError($"Некорректные параметры регламентной работы или Unit {nameof(Job)}: {query.JobId}, {nameof(Unit)}: {query.UnitId}");
|
||
return;
|
||
}
|
||
|
||
var existing = await templateService
|
||
.Get()
|
||
.AsNoTracking()
|
||
.FirstOrDefaultAsync(t => t.JobId == query.JobId && t.UnitId == query.UnitId);
|
||
|
||
|
||
if (existing != null)
|
||
{
|
||
logger.LogWarning("Шаблон уже существует (Job={JobId}, Unit={UnitId}) → пропускаем Create", query.JobId, query.UnitId);
|
||
return;
|
||
}
|
||
|
||
var job = await jobService
|
||
.Get().AsNoTracking()
|
||
.Include(t => t.Group)
|
||
.FirstOrDefaultAsync(t => t.Id == query.JobId);
|
||
|
||
if (job == null)
|
||
{
|
||
logger.LogError("Job не найден: {JobId}", query.JobId);
|
||
return;
|
||
}
|
||
|
||
if (job.Group == null)
|
||
{
|
||
logger.LogError("Job {JobId} не привязан к JobGroup", query.JobId);
|
||
return;
|
||
}
|
||
|
||
var templateName = await shortcodesService.ApplyShortcodesAsync(job!.TemplateNameMask!, query.UnitId, query.JobId);
|
||
|
||
var nextRun = await esppScheduleTransformService.GetNextDateAsync(job.GroupId, job.Group!.ReferenceDate);
|
||
|
||
var template = new Template
|
||
{
|
||
Id = Guid.NewGuid(),
|
||
Name = templateName.ToUpper(),
|
||
UnitId = query.UnitId,
|
||
JobId = query.JobId,
|
||
IsActiveTemplate = query.IsActiveTemplate ?? false,
|
||
IsActiveSchedule = query.IsActiveSchedule ?? false,
|
||
NextRun = nextRun,
|
||
//IsUnused = false,
|
||
StatusTypeId = TemplateStatusTypeEnum.Used,
|
||
InitiatorComment = query.HistoryInitiator?.InitiatorComment,
|
||
InitiatorParrComponentId = query.HistoryInitiator?.InitiatorParrComponentId
|
||
};
|
||
|
||
if (await templateService.CreateAsync(template) && await templateService.CommitAsync(new HistoryInitiator { InitiatorComment = "Запрос на генерацию с тестового шаблона", InitiatorParrComponentId = ParrComponentsEnum.TemplateTaskGenerator }))
|
||
{
|
||
logger.LogInformation("Создан шаблон: Id={TemplateId}, Name={Name}, Job={JobId}, Unit={UnitId}",
|
||
template.Id, template.Name, query.JobId, query.UnitId);
|
||
}
|
||
else
|
||
{
|
||
logger.LogError("Ошибка создания шаблона: Name={Name}", templateName);
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|