using AutoMapper; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using PARR.API.Contracts.V1; using PARR.API.Contracts.V1.Responses; using PARR.API.Contracts.V1.Responses.Base; using PARR.API.Contracts.V1.Responses.Statistics; using PARR.API.Controllers.V1.Base; using PARR.Constants; using PARR.DAL.Services.Interfaces; namespace PARR.API.Controllers.V1.Statistics { /// /// Статистика по заданиям для робота, по полю RobotConfigurations.TaskStatusCode /// [Authorize(Roles = ParrRoles.Administrator.Role)] public class StatRobotTaskController : BaseApiController { private readonly IRobotService robotService; private readonly IRobotConfigurationService robotConfigurationService; private readonly ITaskStatusService taskStatusService; private readonly IMapper mapper; public StatRobotTaskController( IRobotService robotService, IRobotConfigurationService robotConfigurationService, ITaskStatusService taskStatusService, IMapper mapper ) { this.robotService = robotService; this.robotConfigurationService = robotConfigurationService; this.taskStatusService = taskStatusService; this.mapper = mapper; } /// /// Статистика по заданиям для роботов /// /// [HttpGet(ApiRoutes.StatRobotTask.Get)] public async Task Get() { var confStat = await robotConfigurationService.Get() .GroupBy(t => new { t.RobotCode, t.TaskStatusCode }) .Select(t => new { RobotCode = t.Key.RobotCode, TaskStatusCode = t.Key.TaskStatusCode, Count = t.Count() }) .ToListAsync(); var robots = await robotService.Get().ToListAsync(); var statuses = await taskStatusService.Get().ToListAsync(); var response = new List(); foreach (var robot in robots) { var item = new StatRobotTaskResponse { Robot = mapper.Map(robot) }; statuses.ForEach(stat => { item.Statistics.Add(new StatRobotTasksResponse { Status = mapper.Map(stat), Count = confStat.FirstOrDefault(t => t.TaskStatusCode == stat.Code && t.RobotCode == robot.Code)?.Count ?? 0 }); }); item.Statistics = item.Statistics.OrderBy(t => t.Status?.Code).ToList(); response.Add(item); } response = response.OrderBy(t => t.Robot?.Code).ToList(); return Ok(new Response>(response, true)); } } }