diff --git a/PARR.API/Contracts/V1/Requests/Queries/StatRobotHistoryQuery.cs b/PARR.API/Contracts/V1/Requests/Queries/StatRobotHistoryQuery.cs new file mode 100644 index 00000000..06ec616a --- /dev/null +++ b/PARR.API/Contracts/V1/Requests/Queries/StatRobotHistoryQuery.cs @@ -0,0 +1,17 @@ +using PARR.DAL.Contracts; + +namespace PARR.API.Contracts.V1.Requests.Queries +{ + public class StatRobotHistoryQuery + { + /// + /// Фильтр по уровню истории + /// + public RobotHistoryLevelEnum? HistoryLevel { get; set; } + + /// + /// Фильтр за дату + /// + public DateTime? Date { get; set; } + } +} diff --git a/PARR.API/Contracts/V1/Responses/Statistics/StatRobotHistoryResponse.cs b/PARR.API/Contracts/V1/Responses/Statistics/StatRobotHistoryResponse.cs new file mode 100644 index 00000000..b7bdc914 --- /dev/null +++ b/PARR.API/Contracts/V1/Responses/Statistics/StatRobotHistoryResponse.cs @@ -0,0 +1,13 @@ +namespace PARR.API.Contracts.V1.Responses.Statistics +{ + public class StatRobotHistoryResponse + { + public RobotHistoryLevelResponse? Level { get; set; } + + public int AllCount { get; set; } + + public int DateCount { get; set; } + + public DateTime Date { get; set; } + } +} diff --git a/PARR.API/Controllers/V1/Statistics/StatRobotHistoryController.cs b/PARR.API/Controllers/V1/Statistics/StatRobotHistoryController.cs index 93c2ad0f..e216fc50 100644 --- a/PARR.API/Controllers/V1/Statistics/StatRobotHistoryController.cs +++ b/PARR.API/Controllers/V1/Statistics/StatRobotHistoryController.cs @@ -1,8 +1,16 @@ -using Microsoft.AspNetCore.Authorization; +using AutoMapper; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; using PARR.API.Contracts.V1; +using PARR.API.Contracts.V1.Requests.Queries; +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.Models; +using PARR.DAL.Services.Interfaces; namespace PARR.API.Controllers.V1.Statistics { @@ -12,15 +20,65 @@ namespace PARR.API.Controllers.V1.Statistics [Authorize(Roles = ParrRoles.Administrator.Role)] public class StatRobotHistoryController : BaseApiController { + private readonly IRobotHistoryService robotHistoryService; + private readonly IRobotHistoryLevelService robotHistoryLevelService; + private readonly IMapper mapper; + + public StatRobotHistoryController( + IRobotHistoryService robotHistoryService, + IRobotHistoryLevelService robotHistoryLevelService, + IMapper mapper + ) + { + this.robotHistoryService = robotHistoryService; + this.robotHistoryLevelService = robotHistoryLevelService; + this.mapper = mapper; + } + /// /// Статистика истории роботов /// /// [HttpGet(ApiRoutes.StatRobotHistory.Get)] - public async Task Get() + public async Task Get([FromQuery] StatRobotHistoryQuery request) { - //todo: - return Ok(); + if (!request.Date.HasValue) + request.Date = DateTime.Now.Date; + + // всего + var allStat = await robotHistoryService.Get() + .GroupBy(t => t.HistoryLevel) + .Select(t => new { HistoryLevel = t.Key, Count = t.Count() }) + .ToListAsync(); + + // за дату + var dateStat = await robotHistoryService.Get() + .Where(t => t.DateCreated.DateTime.Date == request.Date.Value.Date) + .GroupBy(t => t.HistoryLevel) + .Select(t => new { HistoryLevel = t.Key, Count = t.Count() }) + .ToListAsync(); + + IQueryable levelsQuery = robotHistoryLevelService.Get(); + + if (request.HistoryLevel.HasValue) + levelsQuery = levelsQuery.Where(t => t.Level == (int)request.HistoryLevel.Value); + + var levels = await levelsQuery.OrderBy(t => t.Level).ToListAsync(); + + var response = new List(); + + foreach (var level in levels) + { + response.Add(new StatRobotHistoryResponse + { + AllCount = allStat.FirstOrDefault(t => t.HistoryLevel == level.Level)?.Count ?? 0, + DateCount = dateStat.FirstOrDefault(t => t.HistoryLevel == level.Level)?.Count ?? 0, + Date = request.Date.Value.Date, + Level = mapper.Map(level) + }); + } + + return Ok(new Response>(response, true)); } } }