2023-09-15 16:32:46 +10:00
using AutoMapper ;
2023-12-04 10:16:00 +10:00
using Microsoft.AspNetCore.Authorization ;
2023-09-15 16:32:46 +10:00
using Microsoft.AspNetCore.Mvc ;
using Microsoft.EntityFrameworkCore ;
using PARR.API.Contracts.V1 ;
2023-10-13 14:21:49 +10:00
using PARR.API.Contracts.V1.Requests ;
2023-09-15 16:32:46 +10:00
using PARR.API.Contracts.V1.Requests.Queries ;
using PARR.API.Contracts.V1.Responses ;
using PARR.API.Contracts.V1.Responses.Base ;
using PARR.API.Controllers.V1.Base ;
using PARR.API.Extensions ;
2024-10-08 09:40:08 +10:00
using PARR.API.Services.Interfaces ;
2023-09-26 12:26:37 +10:00
using PARR.BLL.Helpers ;
2024-10-08 10:29:34 +10:00
using PARR.Common.Domain ;
2023-11-22 12:04:33 +10:00
using PARR.Constants ;
2023-09-15 16:32:46 +10:00
using PARR.DAL.Contracts ;
using PARR.DAL.DomainModels ;
2026-01-13 14:36:07 +10:00
using PARR.DAL.DomainServices.Shortcodes ;
2023-09-15 16:32:46 +10:00
using PARR.DAL.Models ;
using PARR.DAL.Services.Interfaces ;
namespace PARR.API.Controllers.V1
{
2023-12-05 16:32:34 +10:00
/// <summary>
/// Шаблоны
/// </summary>
2023-12-04 10:16:00 +10:00
[Authorize(Roles = ParrRoles.Administrator.Role)]
2023-09-15 16:32:46 +10:00
public class TemplateController : BaseApiController
{
private readonly IMapper mapper ;
private readonly ITemplateService templateService ;
2023-10-13 14:21:49 +10:00
private readonly IRobotConfigurationService robotConfigurationService ;
2024-10-08 09:40:08 +10:00
private readonly IClientService clientService ;
2025-12-09 12:20:27 +10:00
private readonly ILogger < TemplateController > logger ;
2026-01-13 14:36:07 +10:00
private readonly IShortcodesService shortcodesService ;
2023-09-15 16:32:46 +10:00
public TemplateController (
IMapper mapper ,
2023-10-04 12:00:43 +10:00
ITemplateService templateService ,
2024-10-08 09:40:08 +10:00
IRobotConfigurationService robotConfigurationService ,
2025-12-09 12:20:27 +10:00
IClientService clientService ,
2026-01-13 14:36:07 +10:00
ILogger < TemplateController > logger ,
IShortcodesService shortcodesService
2023-09-15 16:32:46 +10:00
)
{
this . mapper = mapper ;
this . templateService = templateService ;
2023-10-13 14:21:49 +10:00
this . robotConfigurationService = robotConfigurationService ;
2024-10-08 09:40:08 +10:00
this . clientService = clientService ;
2025-12-09 12:20:27 +10:00
this . logger = logger ;
2026-01-13 14:36:07 +10:00
this . shortcodesService = shortcodesService ;
2023-09-15 16:32:46 +10:00
}
/// <summary>
/// Получить список всех шаблонов постранично
/// </summary>
/// <param name="paginationQuery"></param>
/// <returns></returns>
[HttpGet(ApiRoutes.Template.GetAll)]
public async Task < IActionResult > GetAll ( [ FromQuery ] PaginationQuery paginationQuery , [ FromQuery ] TemplateQuery filter )
{
var paginationFilter = mapper . Map < PaginationFilter > ( paginationQuery ) ;
2025-06-27 13:42:13 +10:00
IQueryable < Template > query = templateService . GetWithIncludes ( ) . AsNoTracking ( )
. Include ( t = > t . Unit ) . ThenInclude ( t = > t ! . UnitValues ) . ThenInclude ( t = > t . Field )
. Include ( t = > t . Unit ) . ThenInclude ( t = > t ! . UnitValues ) . ThenInclude ( t = > t . Value )
2023-10-13 14:21:49 +10:00
. Include ( t = > t . RobotConfigurations ) . ThenInclude ( t = > t . Robot )
2023-10-06 15:13:40 +10:00
. Include ( t = > t . RobotConfigurations ) . ThenInclude ( t = > t . TaskStatus )
. Include ( t = > t . RobotConfigurations ) . ThenInclude ( t = > t . RobotStatus )
2024-05-08 10:16:42 +10:00
. Include ( t = > t . Orders )
2025-12-02 09:49:08 +10:00
. Include ( t = > t . StatusType )
2025-12-26 11:21:45 +10:00
. Include ( t = > t . Job ) . ThenInclude ( t = > t . Group ) . ThenInclude ( t = > t . ScheduleExcludeType )
. Include ( t = > t . Job ) . ThenInclude ( t = > t . Group ) . ThenInclude ( t = > t . ScheduleExcludeTypeCalendar )
2023-09-26 12:26:37 +10:00
. OrderBy ( t = > t . Name )
. AsSplitQuery ( ) ;
2023-09-15 16:32:46 +10:00
2023-09-26 12:26:37 +10:00
if ( ! string . IsNullOrEmpty ( filter . Mask ) )
query = query . Where ( t = > EF . Functions . Like ( t . Name . ToLower ( ) , SqlHelpers . RegexToLike ( filter . Mask ) ) ) ;
2025-09-10 10:40:12 +10:00
if ( filter . JobId . HasValue )
query = query . Where ( t = > t . JobId = = filter . JobId ) ;
2024-04-11 09:22:43 +10:00
2025-12-02 09:49:08 +10:00
if ( filter . StatusTypeId . HasValue )
query = query . Where ( t = > t . StatusTypeId = = filter . StatusTypeId ) ;
2025-12-12 10:45:20 +10:00
#region С т а т у с с и н х р о н и з а ц и и
switch ( filter . SyncStatus )
{
case ( TemplateQuerySyncStatusEnum . Waiting ) :
query = query . Where ( t = > t . RobotConfigurations . Any ( x = > x . TaskStatusCode ! = ( int ) TaskStatusEnum . Ok ) ) ;
break ;
case ( TemplateQuerySyncStatusEnum . Complete ) :
query = query . Where ( t = > t . RobotConfigurations . All ( x = > x . TaskStatusCode = = ( int ) TaskStatusEnum . Ok ) ) ;
break ;
case ( TemplateQuerySyncStatusEnum . Error ) :
query = query . Where ( t = > t . RobotConfigurations . Any ( x = > x . RobotStatusCode = = ( int ) RobotStatusEnum . Error ) ) ;
break ;
default :
break ;
}
#endregion
2023-09-15 16:32:46 +10:00
var templates = await templateService . GetPage ( query , paginationFilter ) . ToListAsync ( ) ;
if ( ! templates . Any ( ) )
return NoContent ( ) ;
var templateResponse = mapper . Map < List < TemplateResponse > > ( templates ) ;
2026-01-13 14:36:07 +10:00
foreach ( var responseItem in templateResponse )
{
await ApplyTemplateShortcodesAsync ( responseItem , templates . First ( t = > t . Id = = responseItem . Id ) ) ;
}
2023-09-15 16:32:46 +10:00
var paginationResponse = new PagedResponse < TemplateResponse > ( templateResponse , true ) . GetPaginatedProps ( paginationFilter , query ) ;
return Ok ( paginationResponse ) ;
}
/// <summary>
/// Получить шаблон по id
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
2023-10-12 12:06:47 +10:00
[HttpGet(ApiRoutes.Template.Get)]
public async Task < IActionResult > GetById ( [ FromRoute ] Guid id )
{
2025-06-27 13:42:13 +10:00
var template = await templateService . GetWithIncludes ( ) . AsNoTracking ( )
. Include ( t = > t . Unit ) . ThenInclude ( t = > t ! . UnitValues ) . ThenInclude ( t = > t . Field )
. Include ( t = > t . Unit ) . ThenInclude ( t = > t ! . UnitValues ) . ThenInclude ( t = > t . Value )
2023-10-12 12:06:47 +10:00
. Include ( t = > t . RobotConfigurations ) . ThenInclude ( t = > t . Robot )
. Include ( t = > t . RobotConfigurations ) . ThenInclude ( t = > t . TaskStatus )
. Include ( t = > t . RobotConfigurations ) . ThenInclude ( t = > t . RobotStatus )
2024-10-08 09:40:08 +10:00
. Include ( t = > t . Orders )
2025-12-02 09:49:08 +10:00
. Include ( t = > t . StatusType )
2025-12-26 11:21:45 +10:00
. Include ( t = > t . Job ) . ThenInclude ( t = > t . Group ) . ThenInclude ( t = > t . ScheduleExcludeType )
. Include ( t = > t . Job ) . ThenInclude ( t = > t . Group ) . ThenInclude ( t = > t . ScheduleExcludeTypeCalendar )
2023-10-12 12:06:47 +10:00
. AsSplitQuery ( )
. FirstOrDefaultAsync ( t = > t . Id = = id ) ;
2023-09-15 16:32:46 +10:00
2023-10-12 12:06:47 +10:00
if ( template = = null )
return NotFound ( ) ;
2023-09-15 16:32:46 +10:00
2023-10-12 12:06:47 +10:00
var response = mapper . Map < TemplateResponse > ( template ) ;
2026-01-13 14:36:07 +10:00
await ApplyTemplateShortcodesAsync ( response , template ) ;
2023-09-15 16:32:46 +10:00
2023-10-12 12:06:47 +10:00
return Ok ( new Response < TemplateResponse > ( response , true ) ) ;
}
2023-09-15 16:32:46 +10:00
2023-10-13 14:21:49 +10:00
/// <summary>
/// Изменить статус у шаблона по ИД (активировать/деактивировать)
/// </summary>
2023-10-17 10:53:02 +10:00
/// <param name="id">ИД шаблона</param>
2023-10-13 14:21:49 +10:00
/// <returns></returns>
[HttpPut(ApiRoutes.Template.ChangeState)]
public async Task < IActionResult > ChangeState ( [ FromRoute ] Guid id , [ FromBody ] TemplateChangeStateRequest request )
{
2025-11-26 14:34:03 +10:00
//TODO:!!! Возможно тут не надо сразу менять, а отправить запрос в TemplateActivator и пусть он сам занимается своей работой!!!
2023-10-13 14:21:49 +10:00
var template = await templateService . GetWithIncludes ( )
2025-06-27 13:42:13 +10:00
. Include ( t = > t . Unit ) . ThenInclude ( t = > t ! . UnitValues ) . ThenInclude ( t = > t . Field )
. Include ( t = > t . Unit ) . ThenInclude ( t = > t ! . UnitValues ) . ThenInclude ( t = > t . Value )
2023-10-13 14:21:49 +10:00
. Include ( t = > t . RobotConfigurations ) . ThenInclude ( t = > t . Robot )
. Include ( t = > t . RobotConfigurations ) . ThenInclude ( t = > t . TaskStatus )
. Include ( t = > t . RobotConfigurations ) . ThenInclude ( t = > t . RobotStatus )
2024-05-08 10:16:42 +10:00
. Include ( t = > t . Orders )
2025-12-02 09:49:08 +10:00
. Include ( t = > t . StatusType )
2025-12-26 11:21:45 +10:00
. Include ( t = > t . Job ) . ThenInclude ( t = > t . Group ) . ThenInclude ( t = > t . ScheduleExcludeType )
. Include ( t = > t . Job ) . ThenInclude ( t = > t . Group ) . ThenInclude ( t = > t . ScheduleExcludeTypeCalendar )
2023-10-13 14:21:49 +10:00
. AsSplitQuery ( )
. FirstOrDefaultAsync ( t = > t . Id = = id ) ;
if ( template = = null )
return BadRequest ( new Response ( false , new List < ErrorModel > { new ErrorModel { Message = $"Н е найден шаблона с id: {id}" } } ) ) ;
if ( template . IsActiveTemplate ! = request . IsActiveTemplate )
{
template . IsActiveTemplate = request . IsActiveTemplate ;
//необходимо обновить шаблон
2025-11-26 14:34:03 +10:00
var config = robotConfigurationService . GetFromTemplateByRobotCode ( RobotsEnum . TemplateOrder , template ) ;
robotConfigurationService . ChangeTaskStatus ( TaskStatusEnum . Updating , config ) ;
2023-10-13 14:21:49 +10:00
}
if ( template . IsActiveSchedule ! = request . IsActiveSchedule )
{
template . IsActiveSchedule = request . IsActiveSchedule ;
//необходимо обновить расписание
2025-11-26 14:34:03 +10:00
var config = robotConfigurationService . GetFromTemplateByRobotCode ( RobotsEnum . ScheduleOrder , template ) ;
robotConfigurationService . ChangeTaskStatus ( TaskStatusEnum . Updating , config ) ;
2023-10-13 14:21:49 +10:00
}
2024-10-08 10:29:34 +10:00
if ( ! await templateService . CommitAsync ( new HistoryInitiator { InitiatorComment = "Изменён статус шаблона/расписания" , InitiatorIp = clientService . GetClientIp ( ) ? . ToString ( ) , InitiatorParrComponentId = ParrComponentsEnum . Api } ) )
2023-10-13 14:21:49 +10:00
return BadRequest ( new Response ( false , new List < ErrorModel > { new ErrorModel { Message = "Ошибка при изменении шаблона." } } ) ) ;
2025-06-27 13:42:13 +10:00
var templateToResponse = await templateService . GetWithIncludes ( ) . AsNoTracking ( )
. Include ( t = > t . Unit ) . ThenInclude ( t = > t . UnitValues ) . ThenInclude ( t = > t . Field )
. Include ( t = > t . Unit ) . ThenInclude ( t = > t . UnitValues ) . ThenInclude ( t = > t . Value )
2023-10-13 14:21:49 +10:00
. Include ( t = > t . RobotConfigurations ) . ThenInclude ( t = > t . Robot )
. Include ( t = > t . RobotConfigurations ) . ThenInclude ( t = > t . TaskStatus )
. Include ( t = > t . RobotConfigurations ) . ThenInclude ( t = > t . RobotStatus )
2024-05-08 10:16:42 +10:00
. Include ( t = > t . Orders )
2025-12-02 09:49:08 +10:00
. Include ( t = > t . StatusType )
2025-12-26 11:21:45 +10:00
. Include ( t = > t . Job ) . ThenInclude ( t = > t . Group ) . ThenInclude ( t = > t . ScheduleExcludeType )
. Include ( t = > t . Job ) . ThenInclude ( t = > t . Group ) . ThenInclude ( t = > t . ScheduleExcludeTypeCalendar )
2023-10-13 14:21:49 +10:00
. AsSplitQuery ( )
2026-01-13 14:36:07 +10:00
. FirstAsync ( t = > t . Id = = id ) ;
2023-10-13 14:21:49 +10:00
var response = mapper . Map < TemplateResponse > ( templateToResponse ) ;
2026-01-13 14:36:07 +10:00
await ApplyTemplateShortcodesAsync ( response , templateToResponse ) ;
2023-10-13 14:21:49 +10:00
return Ok ( new Response < TemplateResponse > ( response , true ) ) ;
}
2026-01-13 14:36:07 +10:00
private async Task ApplyTemplateShortcodesAsync ( TemplateResponse response , Template template )
{
response . WorkGroup = await shortcodesService . ApplyShortcodesAsync ( template . Job . WorkGroupMask , template ) ;
response . ResponseArea = await shortcodesService . ApplyShortcodesAsync ( template . Job . ResponseAreaMask , template ) ;
}
2023-09-15 16:32:46 +10:00
}
}