2021-07-19 15:11:01 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Services
|
|
|
|
|
{
|
2021-07-19 15:31:50 +05:00
|
|
|
|
public class ClusterService : IClusterService
|
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
|
|
|
|
|
2021-10-20 12:52:31 +05:00
|
|
|
|
public ClusterService(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
|
|
|
|
}
|
|
|
|
|
|
2021-08-11 16:54:42 +05:00
|
|
|
|
public async Task<IEnumerable<DepositDto>> GetDepositsAsync(int idCompany,
|
|
|
|
|
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
|
|
|
|
|
2021-10-14 12:04:21 +05:00
|
|
|
|
public async Task<IEnumerable<DepositDto>> GetDepositsDrillParamsAsync(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)
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-11 16:54:42 +05:00
|
|
|
|
public async Task<IEnumerable<ClusterDto>> GetClustersAsync(int idCompany,
|
|
|
|
|
CancellationToken token = default)
|
2021-07-19 15:57:51 +05:00
|
|
|
|
{
|
2021-12-22 11:41:18 +05:00
|
|
|
|
var entities = await GetWellsForCompany(idCompany)
|
2021-07-19 15:57:51 +05:00
|
|
|
|
.Select(e => e.Cluster)
|
|
|
|
|
.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:57:51 +05:00
|
|
|
|
|
2022-06-06 15:43:47 +05:00
|
|
|
|
var dtos = entities.Adapt<IEnumerable<ClusterDto>>();
|
2021-07-19 15:57:51 +05:00
|
|
|
|
|
|
|
|
|
return dtos;
|
|
|
|
|
}
|
|
|
|
|
|
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-08-24 10:59:10 +05:00
|
|
|
|
public async Task<IEnumerable<WellDto>> GetWellsAsync(int idCompany,
|
2021-08-11 16:54:42 +05:00
|
|
|
|
int idCluster, 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-21 15:22:58 +05:00
|
|
|
|
.Where(e => e.IdCluster == idCluster)
|
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
|
|
|
|
|
|
|
|
|
var dtos = entities.Select(e => new WellDto
|
|
|
|
|
{
|
|
|
|
|
Id = e.Id,
|
|
|
|
|
Caption = e.Caption,
|
|
|
|
|
Latitude = e.Latitude,
|
|
|
|
|
Longitude = e.Longitude,
|
|
|
|
|
Cluster = e.Cluster.Caption,
|
|
|
|
|
Deposit = e.Cluster.Deposit.Caption,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return dtos;
|
|
|
|
|
}
|
2021-10-14 12:04:21 +05:00
|
|
|
|
|
2021-12-07 11:34:06 +05:00
|
|
|
|
private static IEnumerable<IGrouping<Deposit, IGrouping<Cluster, Well>>> GroupWells(IEnumerable<Well> wellEntities)
|
2021-10-14 12:04:21 +05:00
|
|
|
|
{
|
|
|
|
|
return wellEntities
|
|
|
|
|
.GroupBy(w => w.Cluster)
|
|
|
|
|
.GroupBy(c => c.Key.Deposit);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-22 11:41:18 +05:00
|
|
|
|
private IQueryable<Well> GetWellsForCompany(int idCompany)
|
|
|
|
|
{
|
|
|
|
|
return 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));
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
|
return 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 =>
|
|
|
|
|
{
|
2021-11-18 11:32:13 +05:00
|
|
|
|
var dto = well.Adapt<WellDto>();
|
|
|
|
|
dto.WellType = well.WellType?.Caption;
|
2022-01-05 17:50:45 +05:00
|
|
|
|
dto.LastTelemetryDate = wellService.GetLastTelemetryDate(well.Id).DateTime;
|
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
|
|
|
|
}),
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-07-19 15:11:01 +05:00
|
|
|
|
}
|
|
|
|
|
}
|