Merge pull request 'Наработка по бурильщикам (Добработка)' (#246) from feature/#31401776-operation-time-for-driller into dev

Reviewed-on: http://test.digitaldrilling.ru:8080/DDrilling/AsbCloudServer/pulls/246
This commit is contained in:
Никита Фролов 2024-04-03 14:58:55 +05:00
commit 061f31ea70
6 changed files with 153 additions and 25 deletions

View File

@ -1,20 +1,19 @@
using System.Collections.Generic;
using System.Linq;
namespace AsbCloudApp.Requests;
/// <summary>
/// Запрос на получение статистики использования подсистем бурильщиком
/// Запрос на получение статистики использования подсистем бурильщиком
/// </summary>
public class GetStatRequest : RequestBase
{
/// <summary>
/// id скважин
/// id скважин
/// </summary>
public IEnumerable<int> IdsWells { get; set; } = new List<int>();
/// <summary>
/// id Бурильщика
/// список ключей бурильщиков
/// </summary>
public int? IdDriller { get; set; }
public IEnumerable<int> IdsDrillers { get; set; } = new List<int>();
}

View File

@ -56,11 +56,11 @@ namespace AsbCloudInfrastructure.Repository
public async Task<IEnumerable<ScheduleDto>> 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);

View File

@ -67,14 +67,9 @@ 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(entry, token);
var drillerOperationsStat = await CalcStatAsync(detectedOperationsByCurrentDriller, token);
var dto = new DrillerDetectedOperationStatDto
{
Statistic = drillerOperationsStat,
@ -83,7 +78,6 @@ internal class SubsystemService : ISubsystemService
};
result.Add(dto);
}
}
return result;
}

View File

@ -0,0 +1,11 @@
using AsbCloudApp.Data;
using Refit;
namespace AsbCloudWebApi.IntegrationTests.Clients;
public interface IDrillerClient
{
[Get("/api/drillers")]
Task<IApiResponse<IEnumerable<DrillerDto>>> GetAsync([Query(CollectionFormat.Multi)] IEnumerable<int> idsWells, CancellationToken token);
}

View File

@ -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<IDrillerClient>(string.Empty);
dbContext.CleanupDbSet<Driller>();
dbContext.CleanupDbSet<Schedule>();
}
[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()
};
}

View File

@ -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<DrillerDto, ICrudRepository<DrillerDto>>
{
public DrillerController(ICrudRepository<DrillerDto> service)
private IScheduleRepository scheduleRepository;
public DrillerController(ICrudRepository<DrillerDto> service, IScheduleRepository scheduleRepository)
: base(service)
{ }
{
this.scheduleRepository = scheduleRepository;
}
/// <summary>
/// Получить список бурильщиков по ключам скважин
/// </summary>
/// <param name="idsWells">массив ключей скважин</param>
/// <param name="token">token</param>
/// <returns>все записи</returns>
[HttpGet("/api/drillers")]
[Permission]
[ProducesResponseType(typeof(IEnumerable<DrillerDto>), StatusCodes.Status200OK)]
public async Task<IActionResult> GetAsync([FromQuery] IEnumerable<int> 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);
}
}
}