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;
|
|
|
|
|
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
|
|
|
|
|
2022-12-23 14:35:23 +05:00
|
|
|
|
namespace AsbCloudInfrastructure.Repository
|
2021-07-19 15:11:01 +05:00
|
|
|
|
{
|
2023-04-18 16:22:53 +05:00
|
|
|
|
|
2022-12-23 14:35:23 +05:00
|
|
|
|
public class DepositRepository : IDepositRepository
|
2021-07-19 15:11:01 +05:00
|
|
|
|
{
|
|
|
|
|
private readonly IAsbCloudDbContext db;
|
2021-10-20 12:52:31 +05:00
|
|
|
|
private readonly IWellService wellService;
|
2021-07-19 15:11:01 +05:00
|
|
|
|
|
2022-12-23 14:35:23 +05:00
|
|
|
|
public DepositRepository(IAsbCloudDbContext db, IWellService wellService)
|
2021-07-19 15:11:01 +05:00
|
|
|
|
{
|
|
|
|
|
this.db = db;
|
2021-10-20 12:52:31 +05:00
|
|
|
|
this.wellService = wellService;
|
2021-07-19 15:11:01 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-23 14:35:23 +05:00
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public async Task<IEnumerable<DepositDto>> GetAsync(int idCompany,
|
2021-08-11 16:54:42 +05:00
|
|
|
|
CancellationToken token = default)
|
2021-07-19 15:11:01 +05:00
|
|
|
|
{
|
2021-08-11 12:11:21 +05:00
|
|
|
|
var wellEntities = await (from well in db.Wells
|
2021-07-22 14:23:47 +05:00
|
|
|
|
.Include(w => w.RelationCompaniesWells)
|
|
|
|
|
.Include(w => w.WellType)
|
2021-08-09 15:41:42 +05:00
|
|
|
|
.Include(w => w.Cluster)
|
2021-07-22 14:23:47 +05:00
|
|
|
|
.ThenInclude(c => c.Deposit)
|
2021-08-24 10:59:10 +05:00
|
|
|
|
where well.RelationCompaniesWells.Any(r => r.IdCompany == idCompany)
|
|
|
|
|
select well).ToListAsync(token)
|
2021-08-11 17:26:02 +05:00
|
|
|
|
.ConfigureAwait(false);
|
2021-07-22 14:23:47 +05:00
|
|
|
|
|
2021-12-07 11:34:06 +05:00
|
|
|
|
var gDepositEntities = GroupWells(wellEntities);
|
2021-07-22 14:23:47 +05:00
|
|
|
|
|
2021-10-14 12:04:21 +05:00
|
|
|
|
var dtos = CreateDepositDto(gDepositEntities);
|
|
|
|
|
|
|
|
|
|
return dtos;
|
|
|
|
|
}
|
2022-04-11 18:00:34 +05:00
|
|
|
|
|
2022-12-23 14:35:23 +05:00
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public async Task<IEnumerable<DepositDto>> GetAllWithDrillParamsAsync(int idCompany,
|
2021-10-14 12:04:21 +05:00
|
|
|
|
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)
|
2021-10-14 12:04:21 +05:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
2021-12-07 11:34:06 +05:00
|
|
|
|
var gDepositEntities = GroupWells(wellEntities);
|
2021-10-14 12:04:21 +05:00
|
|
|
|
|
|
|
|
|
var dtos = CreateDepositDto(gDepositEntities);
|
2021-07-19 15:11:01 +05:00
|
|
|
|
|
|
|
|
|
return dtos;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-23 14:35:23 +05:00
|
|
|
|
/// <inheritdoc/>
|
2021-08-24 10:59:10 +05:00
|
|
|
|
public async Task<IEnumerable<ClusterDto>> GetClustersAsync(int idCompany,
|
2021-08-11 16:54:42 +05:00
|
|
|
|
int depositId, CancellationToken token = default)
|
2021-07-19 15:11:01 +05:00
|
|
|
|
{
|
2021-12-22 11:41:18 +05:00
|
|
|
|
var entities = await GetWellsForCompany(idCompany)
|
2021-07-19 15:11:01 +05:00
|
|
|
|
.Select(e => e.Cluster)
|
2021-07-21 15:29:19 +05:00
|
|
|
|
.Where(e => e.IdDeposit == depositId)
|
2021-07-19 15:11:01 +05:00
|
|
|
|
.Distinct()
|
2021-08-11 10:16:01 +05:00
|
|
|
|
.AsNoTracking()
|
2021-08-11 17:26:02 +05:00
|
|
|
|
.ToListAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
2021-07-19 15:11:01 +05:00
|
|
|
|
|
2022-06-06 15:43:47 +05:00
|
|
|
|
var dtos = entities.Adapt<IEnumerable<ClusterDto>>();
|
2021-07-19 15:11:01 +05:00
|
|
|
|
|
|
|
|
|
return dtos;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-07 11:34:06 +05:00
|
|
|
|
private static IEnumerable<IGrouping<Deposit, IGrouping<Cluster, Well>>> GroupWells(IEnumerable<Well> wellEntities)
|
2022-12-23 14:35:23 +05:00
|
|
|
|
=> wellEntities
|
2021-10-14 12:04:21 +05:00
|
|
|
|
.GroupBy(w => w.Cluster)
|
|
|
|
|
.GroupBy(c => c.Key.Deposit);
|
|
|
|
|
|
2021-12-22 11:41:18 +05:00
|
|
|
|
private IQueryable<Well> GetWellsForCompany(int idCompany)
|
2022-12-23 14:35:23 +05:00
|
|
|
|
=> 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)
|
2021-10-14 12:04:21 +05:00
|
|
|
|
{
|
2022-12-23 14:35:23 +05:00
|
|
|
|
var dtos = gDepositEntities.Select(gDeposit => new DepositDto
|
2021-10-14 12:04:21 +05:00
|
|
|
|
{
|
|
|
|
|
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 =>
|
|
|
|
|
{
|
2021-11-18 11:32:13 +05:00
|
|
|
|
var dto = well.Adapt<WellDto>();
|
2023-02-17 17:36:25 +05:00
|
|
|
|
dto.WellType = well.WellType.Caption;
|
2023-05-19 17:57:07 +05:00
|
|
|
|
dto.LastTelemetryDate = wellService.GetLastTelemetryDate(well.Id);
|
2021-11-18 11:32:13 +05:00
|
|
|
|
dto.Cluster = gCluster.Key.Caption;
|
|
|
|
|
dto.Deposit = gDeposit.Key.Caption;
|
|
|
|
|
return dto;
|
2021-10-14 12:04:21 +05:00
|
|
|
|
}),
|
|
|
|
|
}),
|
|
|
|
|
});
|
2022-12-23 14:35:23 +05:00
|
|
|
|
return dtos;
|
2021-10-14 12:04:21 +05:00
|
|
|
|
}
|
2021-07-19 15:11:01 +05:00
|
|
|
|
}
|
2023-04-18 16:22:53 +05:00
|
|
|
|
|
2021-07-19 15:11:01 +05:00
|
|
|
|
}
|