diff --git a/AsbCloudApp/Requests/GetStatRequest.cs b/AsbCloudApp/Requests/GetStatRequest.cs index 5ebb9550..6a942f48 100644 --- a/AsbCloudApp/Requests/GetStatRequest.cs +++ b/AsbCloudApp/Requests/GetStatRequest.cs @@ -1,20 +1,19 @@ using System.Collections.Generic; -using System.Linq; namespace AsbCloudApp.Requests; /// -/// +/// Запрос на получение статистики использования подсистем бурильщиком /// -public class GetStatRequest: RequestBase +public class GetStatRequest : RequestBase { /// - /// id + /// id скважин /// public IEnumerable IdsWells { get; set; } = new List(); /// - /// id + /// список ключей бурильщиков /// - public int? IdDriller { get; set; } + public IEnumerable IdsDrillers { get; set; } = new List(); } \ No newline at end of file diff --git a/AsbCloudInfrastructure/Repository/ScheduleRepository.cs b/AsbCloudInfrastructure/Repository/ScheduleRepository.cs index 413bb661..e1fa83b8 100644 --- a/AsbCloudInfrastructure/Repository/ScheduleRepository.cs +++ b/AsbCloudInfrastructure/Repository/ScheduleRepository.cs @@ -56,11 +56,11 @@ namespace AsbCloudInfrastructure.Repository public async Task> GetPageAsync(GetStatRequest request, CancellationToken token) { var idWell = request.IdsWells; - var idDriller = request.IdDriller; + var idsDrillers = request.IdsDrillers; var query = GetQuery().Where(s => request.IdsWells.Contains(s.IdWell)); - if (idDriller is not null) + if (idsDrillers.Any()) { - query.Where(s => s.IdDriller == idDriller); + query = query.Where(s => idsDrillers.Contains(s.IdDriller)); } var result = await query.ToArrayAsync(token); diff --git a/AsbCloudInfrastructure/Services/Subsystems/SubsystemService.cs b/AsbCloudInfrastructure/Services/Subsystems/SubsystemService.cs index ed2571a9..d2400ba3 100644 --- a/AsbCloudInfrastructure/Services/Subsystems/SubsystemService.cs +++ b/AsbCloudInfrastructure/Services/Subsystems/SubsystemService.cs @@ -67,22 +67,16 @@ internal class SubsystemService : ISubsystemService var detectedOperations = await detectedOperationService .GetOperationsAsync(byWellRequest, token); + var detectedOperationsByCurrentDriller = detectedOperations.Where(d => d.Driller?.Id == schedule.IdDriller); - var groupByDriller = detectedOperations - .Where(operation => operation.Driller is not null) - .GroupBy(operation => operation.Driller); - - foreach (var entry in groupByDriller) + var drillerOperationsStat = await CalcStatAsync(detectedOperationsByCurrentDriller, token); + var dto = new DrillerDetectedOperationStatDto { - var drillerOperationsStat = await CalcStatAsync(entry, token); - var dto = new DrillerDetectedOperationStatDto - { - Statistic = drillerOperationsStat, - Schedule = schedule, - Well = well, - }; - result.Add(dto); - } + Statistic = drillerOperationsStat, + Schedule = schedule, + Well = well, + }; + result.Add(dto); } return result; diff --git a/AsbCloudWebApi.IntegrationTests/Clients/IDrillerClient.cs b/AsbCloudWebApi.IntegrationTests/Clients/IDrillerClient.cs new file mode 100644 index 00000000..d8b9e26b --- /dev/null +++ b/AsbCloudWebApi.IntegrationTests/Clients/IDrillerClient.cs @@ -0,0 +1,11 @@ +using AsbCloudApp.Data; +using Refit; + +namespace AsbCloudWebApi.IntegrationTests.Clients; + +public interface IDrillerClient +{ + + [Get("/api/drillers")] + Task>> GetAsync([Query(CollectionFormat.Multi)] IEnumerable idsWells, CancellationToken token); +} \ No newline at end of file diff --git a/AsbCloudWebApi.IntegrationTests/Controllers/DrillControllerTest.cs b/AsbCloudWebApi.IntegrationTests/Controllers/DrillControllerTest.cs new file mode 100644 index 00000000..638630ca --- /dev/null +++ b/AsbCloudWebApi.IntegrationTests/Controllers/DrillControllerTest.cs @@ -0,0 +1,88 @@ +using AsbCloudApp.Data; +using AsbCloudApp.Requests; +using AsbCloudDb.Model; +using AsbCloudWebApi.IntegrationTests.Clients; +using AsbCloudWebApi.IntegrationTests.Data; +using System; +using Xunit; + +namespace AsbCloudWebApi.IntegrationTests.Controllers; + +public class DrillerControllerTest : BaseIntegrationTest +{ + private readonly IDrillerClient client; + + public DrillerControllerTest(WebAppFactoryFixture factory) + : base(factory) + { + client = factory.GetAuthorizedHttpClient(string.Empty); + + dbContext.CleanupDbSet(); + dbContext.CleanupDbSet(); + } + + [Fact] + public async Task GetByWellIds_returns_success() + { + //arrange + var well1 = CreateWellAsync(2); + var well2 = CreateWellAsync(3); + var well3 = CreateWellAsync(4); + dbContext.Wells.Add(well1); + dbContext.Wells.Add(well2); + dbContext.Wells.Add(well3); + + var driller1 = CreateDrillerAsync(1); + var driller2 = CreateDrillerAsync(2); + var driller3 = CreateDrillerAsync(3); + + var schedule1= CreateScheduleAsync(well1.Id, driller1); + var schedule2 = CreateScheduleAsync(well2.Id, driller2); + var schedule3 = CreateScheduleAsync(well3.Id, driller3); + + dbContext.Schedule.Add(schedule1); + dbContext.Schedule.Add(schedule2); + dbContext.Schedule.Add(schedule3); + + await dbContext.SaveChangesAsync(); + + ////act + var idsWells = dbContext.Wells.ToArray().Select(w => w.Id); + var response = await client.GetAsync(idsWells, CancellationToken.None); + + ////assert + Assert.NotNull(response.Content); + Assert.Equal(3, response.Content.Count()); + } + + private static Schedule CreateScheduleAsync(int idWell, Driller driller) => new() + { + IdWell = idWell, + ShiftStart = new TimeOnly(8, 0, 0), + ShiftEnd = new TimeOnly(20, 0, 0), + DrillStart = new DateTimeOffset(new DateTime(2024, 1, 1, 0, 0, 0, DateTimeKind.Utc)), + DrillEnd = new DateTimeOffset(new DateTime(2024, 2, 1, 0, 0, 0, DateTimeKind.Utc)), + Driller = driller + }; + + + private static Well CreateWellAsync(int idWell) => new() + { + Id = idWell, + IdWellType = 1, + IdState = 1, + Caption = $"Скважина {idWell}", + Latitude = 10, + Longitude = 20, + Timezone = Defaults.Timezone, + IdCluster = 1, + }; + + private static Driller CreateDrillerAsync(int idDriller) => new() + { + Id = idDriller, + Name = idDriller.ToString(), + Patronymic = idDriller.ToString(), + Surname= idDriller.ToString() + }; +} \ No newline at end of file diff --git a/AsbCloudWebApi/Controllers/DrillerController.cs b/AsbCloudWebApi/Controllers/DrillerController.cs index 0fa84e4a..48efca2c 100644 --- a/AsbCloudWebApi/Controllers/DrillerController.cs +++ b/AsbCloudWebApi/Controllers/DrillerController.cs @@ -1,7 +1,13 @@ using AsbCloudApp.Data; +using AsbCloudApp.Requests; using AsbCloudApp.Services; using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; namespace AsbCloudWebApi.Controllers { @@ -13,8 +19,38 @@ namespace AsbCloudWebApi.Controllers [Authorize] public class DrillerController : CrudController> { - public DrillerController(ICrudRepository service) + private IScheduleRepository scheduleRepository; + + public DrillerController(ICrudRepository service, IScheduleRepository scheduleRepository) : base(service) - { } + { + this.scheduleRepository = scheduleRepository; + } + + /// + /// Получить список бурильщиков по ключам скважин + /// + /// массив ключей скважин + /// token + /// все записи + [HttpGet("/api/drillers")] + [Permission] + [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] + public async Task GetAsync([FromQuery] IEnumerable idsWells, CancellationToken token) + { + var request = new GetStatRequest() + { + IdsWells = idsWells, + }; + var schedulePage = await scheduleRepository.GetPageAsync(request, token); + var drillers = schedulePage + .Select(s => s.Driller) + .Where(d => d is not null) + .GroupBy(d => d.Id) + .Select(group => group.First()) + .OrderBy(d => d.Surname); + + return Ok(drillers); + } } }