Files
parr_api/PARR.API/Controllers/V1/Statistics/StatWorkGroupWorkLoadController.cs

98 lines
3.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using AutoMapper;
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.API.Services.Interfaces;
using PARR.DAL.Models;
using PARR.DAL.Services.Interfaces;
namespace PARR.API.Controllers.V1.Statistics
{
/// <summary>
/// Статистика загрузки регламентными работами по рабочим группам
/// </summary>
public class StatWorkGroupWorkLoadController : BaseApiController
{
private readonly ITemplateService templateService;
private readonly IWorkLoadService workLoadService;
private readonly IWorkGroupService workGroupService;
private readonly IMapper mapper;
public StatWorkGroupWorkLoadController(
ITemplateService templateService,
IWorkLoadService workLoadService,
IWorkGroupService workGroupService,
IMapper mapper
)
{
this.templateService = templateService;
this.workLoadService = workLoadService;
this.workGroupService = workGroupService;
this.mapper = mapper;
}
/// <summary>
/// Статистика загрузки регламентными работами рабочих групп по ЗО
/// </summary>
/// <param name="responseAreaCode"></param>
/// <param name="requestQuery"></param>
/// <returns></returns>
[HttpGet(ApiRoutes.StatWorkGroupWorkLoad.Get)]
public async Task<IActionResult> Get([FromRoute] int responseAreaCode, [FromQuery] WorkloadBaseQuery requestQuery)
{
var daysList = workLoadService.GetDatesForPeriod(requestQuery);
IQueryable<Template> query = templateService.Get()
.Include(t => t.Host);
query = workLoadService.FilterTemplatesQuery(query, requestQuery).Where(t => t.Host!.WorkGroup!.ResponseAreaCode == responseAreaCode);
var groupedQuery = query.GroupBy(t => new { t.NextRun.DateTime.Date, t.Host!.WorkGroupId })
.Select(t => new
{
WorkGroupId = t.Key.WorkGroupId,
Date = t.Key.Date,
TemplateCount = t.Count()
});
var queryResult = await groupedQuery.ToListAsync();
//if (!queryResult.Any())
// return NoContent();
var allWorkGroups = await workGroupService.Get()
.Include(t => t.ResponseArea)
.Where(t => t.ResponseAreaCode == responseAreaCode)
.ToListAsync();
var response = new List<StatWorkGroupWorkLoadResponse>();
allWorkGroups.ForEach(wg =>
{
var workLoads = daysList.Select(t => new StatWorkLoadDataResponse
{
Date = t,
TemplateCount = queryResult.FirstOrDefault(x => x.WorkGroupId == wg.Id && x.Date.Date == t.Date)?.TemplateCount ?? 0
}).OrderBy(t => t.Date).ToList();
response.Add(new StatWorkGroupWorkLoadResponse
{
WorkGroup = mapper.Map<WorkGroupResponse>(wg),
WorkLoad = workLoads
});
});
response = response.OrderBy(t => t.WorkGroup.Name).ToList();
return Ok(new Response<List<StatWorkGroupWorkLoadResponse>>(response, true));
}
}
}