DD.WellWorkover.Cloud/AsbCloudInfrastructure/Repository/DepositRepository.cs

126 lines
4.7 KiB
C#
Raw Normal View History

using AsbCloudApp.Data;
using AsbCloudApp.Repositories;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
2021-08-25 11:13:56 +05:00
using Mapster;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Repository
{
#nullable enable
public class DepositRepository : IDepositRepository
{
private readonly IAsbCloudDbContext db;
private readonly IWellService wellService;
public DepositRepository(IAsbCloudDbContext db, IWellService wellService)
{
this.db = db;
this.wellService = wellService;
}
/// <inheritdoc/>
public async Task<IEnumerable<DepositDto>> 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);
2021-12-07 11:34:06 +05:00
var gDepositEntities = GroupWells(wellEntities);
var dtos = CreateDepositDto(gDepositEntities);
return dtos;
}
2022-04-11 18:00:34 +05:00
/// <inheritdoc/>
public async Task<IEnumerable<DepositDto>> 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)
2022-12-05 12:39:25 +05:00
where well.RelationCompaniesWells.Any(r => r.IdCompany == idCompany)
2022-04-11 18:00:34 +05:00
select well).ToListAsync(token)
.ConfigureAwait(false);
2021-12-07 11:34:06 +05:00
var gDepositEntities = GroupWells(wellEntities);
var dtos = CreateDepositDto(gDepositEntities);
return dtos;
}
/// <inheritdoc/>
public async Task<IEnumerable<ClusterDto>> GetClustersAsync(int idCompany,
int depositId, CancellationToken token = default)
{
2021-12-22 11:41:18 +05:00
var entities = await GetWellsForCompany(idCompany)
.Select(e => e.Cluster)
2021-07-21 15:29:19 +05:00
.Where(e => e.IdDeposit == depositId)
.Distinct()
.AsNoTracking()
.ToListAsync(token)
.ConfigureAwait(false);
var dtos = entities.Adapt<IEnumerable<ClusterDto>>();
return dtos;
}
2021-12-07 11:34:06 +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-12-22 11:41:18 +05:00
private IQueryable<Well> 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));
2021-12-22 11:41:18 +05:00
2022-04-11 18:00:34 +05:00
private IEnumerable<DepositDto> CreateDepositDto(IEnumerable<IGrouping<Deposit, IGrouping<Cluster, Well>>> 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,
2022-04-11 18:00:34 +05:00
Wells = gCluster.Select(well =>
{
var dto = well.Adapt<WellDto>();
dto.WellType = well.WellType.Caption;
dto.LastTelemetryDate = wellService.GetLastTelemetryDate(well.Id).DateTime;
dto.Cluster = gCluster.Key.Caption;
dto.Deposit = gDeposit.Key.Caption;
return dto;
}),
}),
});
return dtos;
}
}
#nullable disable
}