2021-07-19 15:11:01 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
2022-12-23 14:35:23 +05:00
|
|
|
|
using AsbCloudApp.Repositories;
|
2021-07-19 15:11:01 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using AsbCloudDb.Model;
|
2021-08-25 11:13:56 +05:00
|
|
|
|
using Mapster;
|
2021-07-19 15:11:01 +05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-03-19 17:21:05 +05:00
|
|
|
|
using System;
|
2021-07-19 15:11:01 +05:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2021-08-11 16:54:42 +05:00
|
|
|
|
using System.Threading;
|
2021-08-11 12:11:21 +05:00
|
|
|
|
using System.Threading.Tasks;
|
2021-07-19 15:11:01 +05:00
|
|
|
|
|
2024-05-20 09:20:48 +05:00
|
|
|
|
namespace AsbCloudInfrastructure.Repository;
|
|
|
|
|
|
|
|
|
|
public class DepositRepository : IDepositRepository
|
2021-07-19 15:11:01 +05:00
|
|
|
|
{
|
2024-05-20 09:20:48 +05:00
|
|
|
|
private readonly IAsbCloudDbContext db;
|
|
|
|
|
private readonly ITelemetryService telemetryService;
|
2023-04-18 16:22:53 +05:00
|
|
|
|
|
2024-05-20 09:20:48 +05:00
|
|
|
|
public DepositRepository(IAsbCloudDbContext db, ITelemetryService telemetryService)
|
2021-07-19 15:11:01 +05:00
|
|
|
|
{
|
2024-05-20 09:20:48 +05:00
|
|
|
|
this.db = db;
|
|
|
|
|
this.telemetryService = telemetryService;
|
|
|
|
|
}
|
2021-07-19 15:11:01 +05:00
|
|
|
|
|
2024-05-20 09:20:48 +05:00
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public async Task<IEnumerable<DepositDto>> GetAsync(int idCompany,
|
|
|
|
|
CancellationToken token = default)
|
|
|
|
|
{
|
|
|
|
|
var wellsQuery = db.Set<Well>()
|
|
|
|
|
.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);
|
2021-07-19 15:11:01 +05:00
|
|
|
|
|
2024-05-20 09:20:48 +05:00
|
|
|
|
var gDepositEntities = GroupWells(wellEntities);
|
2021-07-22 14:23:47 +05:00
|
|
|
|
|
2024-05-20 09:20:48 +05:00
|
|
|
|
var dtos = CreateDepositDto(gDepositEntities)
|
|
|
|
|
.ToArray();
|
2021-07-22 14:23:47 +05:00
|
|
|
|
|
2024-05-20 09:20:48 +05:00
|
|
|
|
return dtos;
|
|
|
|
|
}
|
2021-10-14 12:04:21 +05:00
|
|
|
|
|
2024-05-20 09:20:48 +05:00
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public async Task<IEnumerable<ClusterDto>> 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);
|
2022-04-11 18:00:34 +05:00
|
|
|
|
|
2024-05-20 09:20:48 +05:00
|
|
|
|
var dtos = entities.Adapt<IEnumerable<ClusterDto>>();
|
2021-10-14 12:04:21 +05:00
|
|
|
|
|
2024-05-20 09:20:48 +05:00
|
|
|
|
return dtos;
|
|
|
|
|
}
|
2021-10-14 12:04:21 +05:00
|
|
|
|
|
2024-05-20 09:20:48 +05:00
|
|
|
|
private static IEnumerable<IGrouping<Deposit, IGrouping<Cluster, Well>>> GroupWells(IEnumerable<Well> wellEntities)
|
|
|
|
|
=> wellEntities
|
|
|
|
|
.GroupBy(w => w.Cluster)
|
|
|
|
|
.GroupBy(c => c.Key.Deposit);
|
2021-07-19 15:11:01 +05:00
|
|
|
|
|
2024-05-20 09:20:48 +05:00
|
|
|
|
private IQueryable<Well> GetWellsForCompany(int idCompany)
|
|
|
|
|
=> db.Set<Well>()
|
|
|
|
|
.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));
|
2021-07-19 15:11:01 +05:00
|
|
|
|
|
2024-05-20 09:20:48 +05:00
|
|
|
|
private IEnumerable<DepositDto> CreateDepositDto(IEnumerable<IGrouping<Deposit, IGrouping<Cluster, Well>>> gDepositEntities)
|
|
|
|
|
{
|
|
|
|
|
var dtos = gDepositEntities.Select(gDeposit => new DepositDto
|
2021-10-14 12:04:21 +05:00
|
|
|
|
{
|
2024-05-20 09:20:48 +05:00
|
|
|
|
Id = gDeposit.Key.Id,
|
|
|
|
|
Caption = gDeposit.Key.Caption,
|
|
|
|
|
Latitude = gDeposit.Key.Latitude,
|
|
|
|
|
Longitude = gDeposit.Key.Longitude,
|
|
|
|
|
Clusters = gDeposit.Select(gCluster => new ClusterDto
|
2021-10-14 12:04:21 +05:00
|
|
|
|
{
|
2024-05-20 09:20:48 +05:00
|
|
|
|
Id = gCluster.Key.Id,
|
|
|
|
|
Caption = gCluster.Key.Caption,
|
|
|
|
|
Latitude = gCluster.Key.Latitude,
|
|
|
|
|
Longitude = gCluster.Key.Longitude,
|
|
|
|
|
Wells = gCluster.Select(well =>
|
2021-10-14 12:04:21 +05:00
|
|
|
|
{
|
2024-05-20 09:20:48 +05:00
|
|
|
|
var dto = well.Adapt<WellDto>();
|
|
|
|
|
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;
|
2021-10-14 12:04:21 +05:00
|
|
|
|
}),
|
2024-05-20 09:20:48 +05:00
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
return dtos;
|
2021-07-19 15:11:01 +05:00
|
|
|
|
}
|
|
|
|
|
}
|