2026-05-15 16:09:49 +10:00
using Microsoft.AspNetCore.Authorization ;
using Microsoft.AspNetCore.Mvc ;
2025-11-01 15:29:12 +10:00
using PARR.API.Contracts.V1 ;
2025-12-01 15:27:18 +10:00
using PARR.API.Contracts.V1.Requests ;
2025-11-01 15:29:12 +10:00
using PARR.API.Contracts.V1.Responses.Base ;
using PARR.API.Controllers.V1.Base ;
using PARR.API.Services.Interfaces ;
using PARR.API.Settings ;
2026-04-14 16:27:27 +10:00
using PARR.Core.Common.Interfaces.RabbitServices ;
2026-05-04 11:55:07 +10:00
using PARR.Core.Services.MatchingStatusService ;
2026-04-14 12:01:49 +10:00
using PARR.Domain.Common.Rabbit.Messages ;
2026-05-15 16:09:49 +10:00
using PARR.Domain.Common.Roles ;
2026-04-13 16:58:30 +10:00
using PARR.Domain.Entities.Base.History ;
using PARR.Domain.Enums ;
2025-11-01 15:29:12 +10:00
namespace PARR.API.Controllers.V1
{
2026-05-15 16:09:49 +10:00
[Authorize(Roles = ParrRoles.Administrator.Role)]
2025-11-01 15:29:12 +10:00
public class SyncTaskController : BaseApiController
{
private readonly ILogger < SyncTaskController > logger ;
2026-04-14 12:01:49 +10:00
private readonly IRabbitService mqService ;
2025-11-01 15:29:12 +10:00
private readonly IUriService uriService ;
private readonly MqSettings mqSettings ;
2025-12-01 15:27:18 +10:00
private readonly IClientService clientService ;
2026-01-16 11:04:26 +10:00
private readonly IMatchingStatusService matchingStatusService ;
2025-11-01 15:29:12 +10:00
public SyncTaskController (
ILogger < SyncTaskController > logger ,
2026-04-14 12:01:49 +10:00
IRabbitService mqService ,
2025-11-01 15:29:12 +10:00
IUriService uriService ,
2025-12-01 15:27:18 +10:00
MqSettings mqSettings ,
2026-01-16 11:04:26 +10:00
IClientService clientService ,
IMatchingStatusService matchingStatusService
2025-11-01 15:29:12 +10:00
)
{
this . logger = logger ;
this . mqService = mqService ;
this . uriService = uriService ;
this . mqSettings = mqSettings ;
2025-12-01 15:27:18 +10:00
this . clientService = clientService ;
2026-01-16 11:04:26 +10:00
this . matchingStatusService = matchingStatusService ;
2025-11-01 15:29:12 +10:00
}
/// <summary>
2025-12-19 14:06:44 +10:00
/// Отправить задание в Matcher
2025-11-01 15:29:12 +10:00
/// </summary>
2025-12-19 14:06:44 +10:00
[HttpPost(ApiRoutes.SyncTask.MatchTemplates)]
public async Task < IActionResult > MatchTemplatesForJob ( [ FromBody ] MatchTemplatesRequest request )
2025-11-01 15:29:12 +10:00
{
2025-12-19 14:06:44 +10:00
//todo: Валидатор! Валидатор то забыли!!!
2026-01-16 11:04:26 +10:00
// проверяем, если уже идет синхронизация по этому объекту, то ахтунг, ошибка
var matchingStatus = await matchingStatusService . GetStatusAsync ( request . ObjectId , request . EntityType ) ;
if ( matchingStatus . IsMatchingObject )
{
logger . LogWarning ( "Прекращена попытка повторного формирования задания для matcher. objectId: {objectId}, entityType: {entityType}, action: {action}" , request . ObjectId , request . EntityType , request . Action ) ;
return BadRequest ( new Response ( false , new List < ErrorModel > { new ErrorModel { Message = $"Уже выполняется задание синхронизации этого объекта" } } ) ) ;
}
2025-11-01 15:29:12 +10:00
//24b3529a-dd43-444a-b10e-ac54fefd046a
var matchTemplateTask = new TemplateMatcherMq
{
2025-12-19 14:06:44 +10:00
Id = request . ObjectId ,
// EntityType = Constants.SyncTaskEntityTypeEnum.Job,
EntityType = request . EntityType ,
2025-12-01 15:27:18 +10:00
Action = request . Action ,
Initiator = new HistoryInitiator
{
InitiatorIp = clientService . GetClientIp ( ) ? . ToString ( ) ,
InitiatorParrComponentId = ParrComponentsEnum . Api ,
2025-12-19 14:06:44 +10:00
InitiatorComment = $"Отправлен запрос из GUI, action: {request.Action.ToString()}, entityType: {request.EntityType.ToString()}"
2025-12-01 15:27:18 +10:00
}
2025-11-01 15:29:12 +10:00
} ;
2026-01-22 10:56:02 +10:00
//var msg = JsonSerializer.Serialize(matchTemplateTask);
//logger.LogDebug("Подготовлено сообщение: {Message}", new[] { msg });
2025-11-01 15:29:12 +10:00
2026-01-22 10:56:02 +10:00
//var result = await mqService.SendAsync(mqSettings.TemplatesMatcher, new[] { msg });
var result = await mqService . SendAsync ( mqSettings . TemplatesMatcher , new List < object > { matchTemplateTask } ) ;
2025-11-01 15:29:12 +10:00
logger . LogDebug ( "Получен код отпрвки: {IsSuccess}" , result . IsSuccess ) ;
if ( ! result . IsSuccess )
return BadRequest ( new Response ( false , new List < ErrorModel > { new ErrorModel { Message = "Ошибка отправки сообщения в очередь" } } ) ) ;
2025-12-19 14:06:44 +10:00
//var createdUri = uriService.GetAllUri(ApiRoutes.SyncTask.MatchTemplatesForJob);
2025-11-01 15:29:12 +10:00
2025-12-19 14:06:44 +10:00
return Created ( "" , new Response < string? > ( null , true , new List < ErrorModel > ( ) , "Отправлен запрос на сопоставление шаблонов" ) ) ;
2025-11-01 15:29:12 +10:00
}
}
}