2023-09-22 15:30:57 +10:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using PARR.BLL.Contracts;
|
|
|
|
|
|
using PARR.BLL.Domain.Mq;
|
2023-09-21 15:21:33 +10:00
|
|
|
|
using PARR.BLL.Services.Interfaces;
|
2023-09-22 15:30:57 +10:00
|
|
|
|
using PARR.DAL.Models;
|
|
|
|
|
|
using PARR.GeneratorTemplates.Services;
|
2023-09-21 15:21:33 +10:00
|
|
|
|
using PARR.GeneratorTemplates.Settings;
|
2023-09-26 11:01:20 +10:00
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Text.Json;
|
2023-09-21 15:21:33 +10:00
|
|
|
|
|
|
|
|
|
|
namespace PARR.GeneratorTemplates
|
|
|
|
|
|
{
|
|
|
|
|
|
internal class GeneratorTemplate : IGeneratorTemplate
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly MqSettings mqSettings;
|
|
|
|
|
|
private readonly IMqService mqService;
|
|
|
|
|
|
private readonly ILogger<GeneratorTemplate> logger;
|
2023-09-22 15:30:57 +10:00
|
|
|
|
private readonly ITransformService transformService;
|
|
|
|
|
|
private readonly IValidatorService validatorService;
|
|
|
|
|
|
private readonly IServiceProvider serviceProvider;
|
2023-09-21 15:21:33 +10:00
|
|
|
|
|
|
|
|
|
|
public GeneratorTemplate(
|
|
|
|
|
|
MqSettings mqSettings,
|
|
|
|
|
|
IMqService mqService,
|
2023-09-22 15:30:57 +10:00
|
|
|
|
ILogger<GeneratorTemplate> logger,
|
|
|
|
|
|
ITransformService transformService,
|
|
|
|
|
|
IValidatorService validatorService,
|
|
|
|
|
|
IServiceProvider serviceProvider
|
2023-09-21 15:21:33 +10:00
|
|
|
|
)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.mqSettings = mqSettings;
|
|
|
|
|
|
this.mqService = mqService;
|
|
|
|
|
|
this.logger = logger;
|
2023-09-22 15:30:57 +10:00
|
|
|
|
this.transformService = transformService;
|
|
|
|
|
|
this.validatorService = validatorService;
|
|
|
|
|
|
this.serviceProvider = serviceProvider;
|
2023-09-21 15:21:33 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-26 11:01:20 +10:00
|
|
|
|
public void Start()
|
2023-09-21 15:21:33 +10:00
|
|
|
|
{
|
2023-09-26 11:01:20 +10:00
|
|
|
|
#region test
|
|
|
|
|
|
// string msg = "{\"WorkId\":\"91219bec-e5f7-4a1a-b4c9-c135035d2498\",\"ApplicationId\":\"13044a1f-fa0c-476f-b501-2b8d06bac9f8\",\"Action\":\"create\",\"Ek\":\"ВРТ*ДВС\",\"StatusEk\":0}";
|
|
|
|
|
|
// string msg = "{\"WorkId\":\"91219bec-e5f7-4a1a-b4c9-c135035d2498\",\"ApplicationId\":\"13044a1f-fa0c-476f-b501-2b8d06bac9f8\",\"Action\":\"deactivate\",\"Ek\":\"ВРТ*ДВС\",\"StatusEk\":0}";
|
2023-09-22 15:30:57 +10:00
|
|
|
|
|
2023-09-26 11:01:20 +10:00
|
|
|
|
// await GenerateTemplates(msg);
|
|
|
|
|
|
#endregion
|
2023-09-21 16:38:38 +10:00
|
|
|
|
|
2023-09-26 11:01:20 +10:00
|
|
|
|
var isConnected = mqService.InitConsumer(mqSettings, GenerateTemplates);
|
2023-09-21 16:38:38 +10:00
|
|
|
|
|
2023-09-26 11:01:20 +10:00
|
|
|
|
if (!isConnected)
|
|
|
|
|
|
throw new Exception("Ошибка при подключении к RabbitMq");
|
2023-09-21 15:21:33 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Stop()
|
|
|
|
|
|
{
|
2023-09-21 16:38:38 +10:00
|
|
|
|
mqService.Dispose();
|
2023-09-21 15:21:33 +10:00
|
|
|
|
}
|
2023-09-22 15:30:57 +10:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async Task GenerateTemplates(string msg)
|
|
|
|
|
|
{
|
2023-09-25 13:45:11 +10:00
|
|
|
|
logger.LogInformation($"Получили запрос: {msg}");
|
2023-09-22 15:30:57 +10:00
|
|
|
|
|
|
|
|
|
|
var query = transformService.GetModelFromJson<GeneratorTemplateMq>(msg);
|
|
|
|
|
|
if (query == null)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
if (!await validatorService.IsValidApplicationAndWorksAsync(query.ApplicationId, query.WorkId))
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError($"Невалидны параметры {nameof(query.ApplicationId)}: {query.ApplicationId}, " +
|
|
|
|
|
|
$"{nameof(query.WorkId)}: {query.WorkId}," +
|
|
|
|
|
|
$" нет таких значений или они не связаны в таблице ${nameof(ApplicationsInWork)}");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Enum.TryParse(typeof(GenerateTemplateActionsEnum), query.Action, true, out var action);
|
|
|
|
|
|
|
|
|
|
|
|
using (var scope = serviceProvider.CreateScope())
|
|
|
|
|
|
{
|
|
|
|
|
|
var templateManager = scope.ServiceProvider.GetService<ITemplateManager>();
|
|
|
|
|
|
if (templateManager == null)
|
|
|
|
|
|
throw new Exception($"Не найден сервис: {nameof(ITemplateManager)}");
|
|
|
|
|
|
|
|
|
|
|
|
switch (action)
|
|
|
|
|
|
{
|
|
|
|
|
|
case GenerateTemplateActionsEnum.create:
|
|
|
|
|
|
await templateManager.CreateTemplates(query);
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case GenerateTemplateActionsEnum.deactivate:
|
|
|
|
|
|
await templateManager.DeactivateTemplates(query);
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
logger.LogError($"Во входящем запросе, поле {nameof(query.Action)} не содержит допустимых значений");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-21 15:21:33 +10:00
|
|
|
|
}
|
|
|
|
|
|
}
|