using AsbCloudApp.Data; using AsbCloudApp.Services; using AsbCloudDb.Model; using Microsoft.EntityFrameworkCore; using System.Collections.Generic; using System.Linq; namespace AsbCloudInfrastructure.Services { public class ClusterService : IClusterService { private readonly IAsbCloudDbContext db; public ClusterService(IAsbCloudDbContext db) { this.db = db; } public IEnumerable GetDeposits(int idCompany) { var wellEntities = (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).ToList(); var gDepositEntities = wellEntities .GroupBy(w => w.Cluster) .GroupBy(c => c.Key.Deposit); var dtos = gDepositEntities.Select(gDeposit => new DepositDto { Id = gDeposit.Key.Id, Caption = gDeposit.Key.Caption, Latitude = gDeposit.Key.Latitude, Longitude = gDeposit.Key.Longitude, Description = "", Clusters = gDeposit.Select(gCluster=>new ClusterDto { Id = gCluster.Key.Id, Caption = gCluster.Key.Caption, Latitude = gCluster.Key.Latitude, Longitude = gCluster.Key.Longitude, Description = "", Wells = gCluster.Select(well => new WellDto { Id = well.Id, Caption = well.Caption, Latitude = well.Latitude, Longitude = well.Longitude, WellType = well.WellType?.Caption, Cluster = gCluster.Key.Caption, Deposit = gDeposit.Key.Caption, }), }), }); return dtos; } public IEnumerable GetClusters(int idCompany) { var entities = db.GetWellsForCompany(idCompany) .Select(e => e.Cluster) .Distinct() .ToList(); var dtos = entities.Select(e => new ClusterDto { Id = e.Id, Caption = e.Caption, Latitude = e.Latitude, Longitude = e.Longitude, }); return dtos; } public IEnumerable GetClusters(int idCompany, int depositId) { var entities = db.GetWellsForCompany(idCompany) .Select(e => e.Cluster) .Where(e => e.IdDeposit == depositId) .Distinct() .ToList(); var dtos = entities.Select(e => new ClusterDto { Id = e.Id, Caption = e.Caption, Latitude = e.Latitude, Longitude = e.Longitude, }); return dtos; } public IEnumerable GetWells(int idCompany, int idCluster) { var entities = db.GetWellsForCompany(idCompany) .Where(e => e.IdCluster == idCluster) .ToList(); 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; } public ClusterStatDto GetStat(int idCompany, int idCluster) { var wellEntities = from w in db.Wells where w.IdCluster == idCluster && w.RelationCompaniesWells.Any(c => c.IdCompany == idCompany) select w; var wellStatDtos = wellEntities.Select(e => new WellStatDto { Id = e.Id, Caption = e.Caption, Cluster = e.Cluster.Caption, Deposit = e.Cluster.Deposit.Caption, Latitude = e.Latitude, Longitude = e.Longitude, FactEnd = e.FactEnd, FactStart = e.FactStart, PlanEnd = e.PlanEnd, PlanStart = e.PlanStart, RateOfPenetrationFact = e.RateOfPenetrationFact, RateOfPenetrationPlan = e.RateOfPenetrationPlan, RouteSpeedFact = e.RouteSpeedFact, RouteSpeedPlan = e.RouteSpeedPlan, Sections = e.Sections.Select(s => new WellSectionDto { BhaDownSpeedFact = s.BhaDownSpeedFact, BhaDownSpeedPlan = s.BhaDownSpeedPlan, BhaUpSpeedFact = s.BhaUpSpeedFact, BhaUpSpeedPlan = s.BhaUpSpeedPlan, BuildDaysFact = s.BuildDaysFact, BuildDaysPlan = s.BuildDaysPlan, CasingDownSpeedFact = s.CasingDownSpeedFact, CasingDownSpeedPlan = s.CasingDownSpeedPlan, RateOfPenetrationFact = s.RateOfPenetrationFact, RateOfPenetrationPlan = s.RateOfPenetrationPlan, RouteSpeedFact = s.RouteSpeedFact, RouteSpeedPlan = s.RouteSpeedPlan, SectionType = s.WellSectionType.Caption, WellDepthFact = s.WellDepthFact, WellDepthPlan = s.WellDepthPlan, }), UnProductiveDays = e.UnProductiveDays, Companies = e.RelationCompaniesWells.Select(c => new CompanyDto { Id = c.Company.Id, Caption = c.Company.Caption, CompanyType = c.Company.CompanyType.Caption, }), WellType = e.WellType.Caption, }).ToList(); if (!wellStatDtos.Any()) return null; var clusterById = db.Clusters.FirstOrDefault(c => c.Id == idCluster); return new ClusterStatDto { Id = clusterById.Id, Description = "", Caption = clusterById.Caption, Latitude = clusterById.Latitude, Longitude = clusterById.Longitude, WellsStat = wellStatDtos, }; } } }