diff --git a/AsbCloudApp/Repositories/IDepositRepository.cs b/AsbCloudApp/Repositories/IDepositRepository.cs index 87f732fb..8f2e421a 100644 --- a/AsbCloudApp/Repositories/IDepositRepository.cs +++ b/AsbCloudApp/Repositories/IDepositRepository.cs @@ -19,15 +19,6 @@ namespace AsbCloudApp.Repositories Task> GetAsync(int idCompany, CancellationToken token); - /// - /// Список месторождений/кустов/скважин у которых заполненны параметры бурения - /// - /// - /// - /// - Task> GetAllWithDrillParamsAsync(int idCompany, - CancellationToken token = default); - /// /// Список кустов месторождения доступных компании /// diff --git a/AsbCloudInfrastructure/Repository/DepositRepository.cs b/AsbCloudInfrastructure/Repository/DepositRepository.cs index 497ef3b6..344ad0f7 100644 --- a/AsbCloudInfrastructure/Repository/DepositRepository.cs +++ b/AsbCloudInfrastructure/Repository/DepositRepository.cs @@ -10,122 +10,100 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; -namespace AsbCloudInfrastructure.Repository +namespace AsbCloudInfrastructure.Repository; + +public class DepositRepository : IDepositRepository { + private readonly IAsbCloudDbContext db; + private readonly ITelemetryService telemetryService; - public class DepositRepository : IDepositRepository + public DepositRepository(IAsbCloudDbContext db, ITelemetryService telemetryService) { - private readonly IAsbCloudDbContext db; - private readonly ITelemetryService telemetryService; - - public DepositRepository(IAsbCloudDbContext db, ITelemetryService telemetryService) - { - this.db = db; - this.telemetryService = telemetryService; - } - - /// - public async Task> GetAsync(int idCompany, - CancellationToken token = default) - { - var wellEntities = await (from well in db.Wells - .Include(w => w.RelationCompaniesWells) - .Include(w => w.WellType) - .Include(w => w.Cluster) - .ThenInclude(c => c.Deposit) - where well.RelationCompaniesWells.Any(r => r.IdCompany == idCompany) - select well).ToListAsync(token) - .ConfigureAwait(false); - - var gDepositEntities = GroupWells(wellEntities); - - var dtos = CreateDepositDto(gDepositEntities); - - return dtos; - } - - /// - public async Task> GetAllWithDrillParamsAsync(int idCompany, - CancellationToken token = default) - { - var wellEntities = await (from well in db.Wells - .Include(w => w.RelationCompaniesWells) - .Include(w => w.WellType) - .Include(w => w.Cluster) - .ThenInclude(c => c.Deposit) - where well.RelationCompaniesWells.Any(r => r.IdCompany == idCompany) - select well).ToListAsync(token) - .ConfigureAwait(false); - - var gDepositEntities = GroupWells(wellEntities); - - var dtos = CreateDepositDto(gDepositEntities); - - return dtos; - } - - /// - public async Task> GetClustersAsync(int idCompany, - int depositId, CancellationToken token = default) - { - var entities = await GetWellsForCompany(idCompany) - .Select(e => e.Cluster) - .Where(e => e.IdDeposit == depositId) - .Distinct() - .AsNoTracking() - .ToListAsync(token) - .ConfigureAwait(false); - - var dtos = entities.Adapt>(); - - return dtos; - } - - private static IEnumerable>> GroupWells(IEnumerable wellEntities) - => wellEntities - .GroupBy(w => w.Cluster) - .GroupBy(c => c.Key.Deposit); - - private IQueryable GetWellsForCompany(int idCompany) - => db.Wells - .Include(w => w.RelationCompaniesWells) - .ThenInclude(r => r.Company) - .Include(w => w.Cluster) - .ThenInclude(c => c.Deposit) - .Where(w => w.RelationCompaniesWells.Any(c => c.IdCompany == idCompany)); - - private IEnumerable CreateDepositDto(IEnumerable>> gDepositEntities) - { - var dtos = gDepositEntities.Select(gDeposit => new DepositDto - { - Id = gDeposit.Key.Id, - Caption = gDeposit.Key.Caption, - Latitude = gDeposit.Key.Latitude, - Longitude = gDeposit.Key.Longitude, - Clusters = gDeposit.Select(gCluster => new ClusterDto - { - Id = gCluster.Key.Id, - Caption = gCluster.Key.Caption, - Latitude = gCluster.Key.Latitude, - Longitude = gCluster.Key.Longitude, - Wells = gCluster.Select(well => - { - var dto = well.Adapt(); - dto.WellType = well.WellType.Caption; - - dto.LastTelemetryDate = DateTimeOffset.MinValue; - - if (well.IdTelemetry != null) - dto.LastTelemetryDate = telemetryService.GetDatesRange(well.IdTelemetry.Value).To; - - dto.Cluster = gCluster.Key.Caption; - dto.Deposit = gDeposit.Key.Caption; - return dto; - }), - }), - }); - return dtos; - } + this.db = db; + this.telemetryService = telemetryService; } + /// + public async Task> GetAsync(int idCompany, + CancellationToken token = default) + { + var wellsQuery = db.Set() + .Include(w => w.RelationCompaniesWells) + .Include(w => w.WellType) + .Include(w => w.Cluster) + .ThenInclude(c => c.Deposit) + .Where(well => well.RelationCompaniesWells.Any(r => r.IdCompany == idCompany)); + + var wellEntities = await wellsQuery.ToArrayAsync(token); + + var gDepositEntities = GroupWells(wellEntities); + + var dtos = CreateDepositDto(gDepositEntities) + .ToArray(); + + return dtos; + } + + /// + public async Task> GetClustersAsync(int idCompany, + int depositId, CancellationToken token = default) + { + var entities = await GetWellsForCompany(idCompany) + .Select(e => e.Cluster) + .Where(e => e.IdDeposit == depositId) + .Distinct() + .AsNoTracking() + .ToListAsync(token) + .ConfigureAwait(false); + + var dtos = entities.Adapt>(); + + return dtos; + } + + private static IEnumerable>> GroupWells(IEnumerable wellEntities) + => wellEntities + .GroupBy(w => w.Cluster) + .GroupBy(c => c.Key.Deposit); + + private IQueryable GetWellsForCompany(int idCompany) + => db.Set() + .Include(w => w.RelationCompaniesWells) + .ThenInclude(r => r.Company) + .Include(w => w.Cluster) + .ThenInclude(c => c.Deposit) + .Where(w => w.RelationCompaniesWells.Any(c => c.IdCompany == idCompany)); + + private IEnumerable CreateDepositDto(IEnumerable>> gDepositEntities) + { + var dtos = gDepositEntities.Select(gDeposit => new DepositDto + { + Id = gDeposit.Key.Id, + Caption = gDeposit.Key.Caption, + Latitude = gDeposit.Key.Latitude, + Longitude = gDeposit.Key.Longitude, + Clusters = gDeposit.Select(gCluster => new ClusterDto + { + Id = gCluster.Key.Id, + Caption = gCluster.Key.Caption, + Latitude = gCluster.Key.Latitude, + Longitude = gCluster.Key.Longitude, + Wells = gCluster.Select(well => + { + var dto = well.Adapt(); + dto.WellType = well.WellType.Caption; + + dto.LastTelemetryDate = DateTimeOffset.MinValue; + + if (well.IdTelemetry != null) + dto.LastTelemetryDate = telemetryService.GetDatesRange(well.IdTelemetry.Value).To; + + dto.Cluster = gCluster.Key.Caption; + dto.Deposit = gDeposit.Key.Caption; + return dto; + }), + }), + }); + return dtos; + } } diff --git a/AsbCloudInfrastructure/Services/SAUB/TelemetryDataCache.cs b/AsbCloudInfrastructure/Services/SAUB/TelemetryDataCache.cs index 0dcfe080..9d99c700 100644 --- a/AsbCloudInfrastructure/Services/SAUB/TelemetryDataCache.cs +++ b/AsbCloudInfrastructure/Services/SAUB/TelemetryDataCache.cs @@ -152,8 +152,8 @@ namespace AsbCloudInfrastructure.Services.SAUB if (!cacheItem.LastData.Any()) return null; - var from = cacheItem.FirstByDate.DateTime; - var to = cacheItem.LastData[^1].DateTime; + var from = DateTime.SpecifyKind(cacheItem.FirstByDate.DateTime, DateTimeKind.Unspecified); + var to = DateTime.SpecifyKind(cacheItem.LastData[^1].DateTime, DateTimeKind.Unspecified); return new DatesRangeDto { diff --git a/AsbCloudWebApi/Controllers/DepositController.cs b/AsbCloudWebApi/Controllers/DepositController.cs index 9b8d0c91..4e9a287f 100644 --- a/AsbCloudWebApi/Controllers/DepositController.cs +++ b/AsbCloudWebApi/Controllers/DepositController.cs @@ -43,26 +43,6 @@ namespace AsbCloudWebApi.Controllers return Ok(result); } - /// - /// Получает список доступных пользователю месторождений (только скважины с параметрами бурения) - /// - /// Токен отмены задачи - /// - [HttpGet("drillParamsWells")] - [Permission] - [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] - public async Task GetDepositsDrillParamsAsync(CancellationToken token) - { - int? idCompany = User.GetCompanyId(); - - if (idCompany is null) - return Forbid(); - - var result = await depositService.GetAllWithDrillParamsAsync((int)idCompany, - token).ConfigureAwait(false); - return Ok(result); - } - /// /// Получает список доступных пользователю кустов месторождения /// diff --git a/AsbCloudWebApi/Middlewares/SimplifyExceptionsMiddleware.cs b/AsbCloudWebApi/Middlewares/SimplifyExceptionsMiddleware.cs index 3c444b4a..53191eaf 100644 --- a/AsbCloudWebApi/Middlewares/SimplifyExceptionsMiddleware.cs +++ b/AsbCloudWebApi/Middlewares/SimplifyExceptionsMiddleware.cs @@ -44,6 +44,9 @@ namespace AsbCloudWebApi.Middlewares } catch (Exception ex) // TODO: find explicit exception. Use Trace. Add body size to message. { + if (context.Response.HasStarted) + throw; + context.Response.Clear(); context.Response.StatusCode = 500;