diff --git a/AsbCloudApp/Data/PlanFactBase.cs b/AsbCloudApp/Data/PlanFactBase.cs new file mode 100644 index 00000000..033afeed --- /dev/null +++ b/AsbCloudApp/Data/PlanFactBase.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AsbCloudApp.Data +{ + public class PlanFactBase + { + public T Plan { get; set; } + public T Fact { get; set; } + } +} diff --git a/AsbCloudApp/Data/StatClusterDto.cs b/AsbCloudApp/Data/StatClusterDto.cs index 6105d519..df16d746 100644 --- a/AsbCloudApp/Data/StatClusterDto.cs +++ b/AsbCloudApp/Data/StatClusterDto.cs @@ -2,9 +2,10 @@ namespace AsbCloudApp.Data { - public class StatClusterDto : ClusterDto + public class StatClusterDto //: ClusterDto { - public List Sections { get; set; } - public List WellsTotals { get; set; } + public int Id { get; set; } + public string Caption { get; set; } + public IEnumerable StatsWells { get; set; } } } diff --git a/AsbCloudApp/Data/StatOperationDto.cs b/AsbCloudApp/Data/StatOperationDto.cs deleted file mode 100644 index 1d6b8e96..00000000 --- a/AsbCloudApp/Data/StatOperationDto.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace AsbCloudApp.Data -{ - public class StatOperationDto: IId - { - public int Id { get; set; } - public string Caption { get; set; } - public StatOperationBase Plan { get; set; } - public StatOperationBase Fact { get; set; } - } -} diff --git a/AsbCloudApp/Data/StatOperationBase.cs b/AsbCloudApp/Data/StatOperationsDto.cs similarity index 97% rename from AsbCloudApp/Data/StatOperationBase.cs rename to AsbCloudApp/Data/StatOperationsDto.cs index 8241ce33..ac4496f1 100644 --- a/AsbCloudApp/Data/StatOperationBase.cs +++ b/AsbCloudApp/Data/StatOperationsDto.cs @@ -2,7 +2,7 @@ namespace AsbCloudApp.Data { - public class StatOperationBase + public class StatOperationsDto { /// /// Дата и время начала diff --git a/AsbCloudApp/Data/StatSectionDto.cs b/AsbCloudApp/Data/StatSectionDto.cs new file mode 100644 index 00000000..eeda800d --- /dev/null +++ b/AsbCloudApp/Data/StatSectionDto.cs @@ -0,0 +1,8 @@ +namespace AsbCloudApp.Data +{ + public class StatSectionDto : PlanFactBase + { + public int Id { get; set; } + public string Caption { get; set; } + } +} diff --git a/AsbCloudApp/Data/StatWellDto.cs b/AsbCloudApp/Data/StatWellDto.cs index 05440cce..6bca2f91 100644 --- a/AsbCloudApp/Data/StatWellDto.cs +++ b/AsbCloudApp/Data/StatWellDto.cs @@ -2,10 +2,12 @@ namespace AsbCloudApp.Data { - public class StatWellDto : WellDto + public class StatWellDto //: WellDto { - public IEnumerable Sections { get; set; } - public StatOperationDto Total { get; set; } + public int Id { get; set; } + public string Caption { get; set; } + public IEnumerable Sections { get; set; } + public PlanFactBase Total { get; set; } public IEnumerable Companies { get; set; } } } diff --git a/AsbCloudApp/Services/IWellService.cs b/AsbCloudApp/Services/IWellService.cs index 2aa8f3be..95afc6b3 100644 --- a/AsbCloudApp/Services/IWellService.cs +++ b/AsbCloudApp/Services/IWellService.cs @@ -11,5 +11,7 @@ namespace AsbCloudApp.Services Task> GetTransmittingWellsAsync(int idCompany, CancellationToken token); Task IsCompanyInvolvedInWellAsync(int idCompany, int idWell, CancellationToken token); Task> GetOperationsAsync(int idWell, CancellationToken token); + Task GetWellCaptionByIdAsync(int idWell, CancellationToken token); + Task> GetCompaniesAsync(int idWell, CancellationToken token); } } diff --git a/AsbCloudInfrastructure/Services/ClusterService.cs b/AsbCloudInfrastructure/Services/ClusterService.cs index fef1620a..e04e8ea9 100644 --- a/AsbCloudInfrastructure/Services/ClusterService.cs +++ b/AsbCloudInfrastructure/Services/ClusterService.cs @@ -1,6 +1,7 @@ using AsbCloudApp.Data; using AsbCloudApp.Services; using AsbCloudDb.Model; +using Mapster; using Microsoft.EntityFrameworkCore; using System.Collections.Generic; using System.Linq; @@ -74,13 +75,7 @@ namespace AsbCloudInfrastructure.Services .ToListAsync(token) .ConfigureAwait(false); - var dtos = entities.Select(e => new ClusterDto - { - Id = e.Id, - Caption = e.Caption, - Latitude = e.Latitude, - Longitude = e.Longitude, - }); + var dtos = entities.Adapt(); return dtos; } @@ -96,13 +91,7 @@ namespace AsbCloudInfrastructure.Services .ToListAsync(token) .ConfigureAwait(false); - var dtos = entities.Select(e => new ClusterDto - { - Id = e.Id, - Caption = e.Caption, - Latitude = e.Latitude, - Longitude = e.Longitude, - }); + var dtos = entities.Adapt(); return dtos; } diff --git a/AsbCloudInfrastructure/Services/WellOperationsStatService.cs b/AsbCloudInfrastructure/Services/WellOperationsStatService.cs index e91d3597..0ab66869 100644 --- a/AsbCloudInfrastructure/Services/WellOperationsStatService.cs +++ b/AsbCloudInfrastructure/Services/WellOperationsStatService.cs @@ -56,7 +56,10 @@ namespace AsbCloudInfrastructure.Services public class WellOperationsStatService : IWellOperationsStatService { private readonly IAsbCloudDbContext db; + private readonly IWellService wellService; private readonly CacheTable cachedSectionsTypes; + private readonly CacheTable cachedWell; + private readonly CacheTable cacheCluster; private const int idOperationBhaAssembly = 1025; private const int idOperationBhaDisassembly = 1026; private const int idOperationNonProductiveTime = 1043; @@ -65,36 +68,40 @@ namespace AsbCloudInfrastructure.Services private const int idOperationBhaUp = 1047; private const int IdOperationCasingDown = 1048; - public WellOperationsStatService(IAsbCloudDbContext db, Cache.CacheDb cache) + public WellOperationsStatService(IAsbCloudDbContext db, CacheDb cache, IWellService wellService) { this.db = db; + this.wellService = wellService; cachedSectionsTypes = cache.GetCachedTable((DbContext)db); + cachedWell = cache.GetCachedTable((DbContext)db); + cacheCluster = cache.GetCachedTable((DbContext)db); } public async Task GetOperationStatByClusterAsync(int idCluster, CancellationToken token = default) { var operations = await db.WellOperations - .Include(o => o.Well) .Where(o => o.Well.IdCluster == idCluster) .OrderBy(o => o.StartDate) .AsNoTracking() .ToListAsync(token); - var wellsIds = operations.Select(o => o.IdWell).Distinct(); - var sectionsStats = new List(wellsIds.Count() * 3); - var wellsTotals = new List(wellsIds.Count()); + var cluster = await cacheCluster.FirstOrDefaultAsync(c => c.Id == idCluster, token); - foreach(var idWell in wellsIds) + var wellsIds = operations.Select(o => o.IdWell).Distinct(); + + var statsWells = new List(wellsIds.Count()); + + foreach (var idWell in wellsIds) { - var wellOperations = operations.Where(o => o.IdWell == idWell); - sectionsStats.AddRange(GetWellSectionsStats(wellOperations)); - wellsTotals.Add(GetWellStats(wellOperations)); + var statWellDto = await CalcStatWell(operations, idWell, token); + statsWells.Add(statWellDto); } var statClusterDto = new StatClusterDto { - Sections = sectionsStats, - WellsTotals = wellsTotals, + Id = idCluster, + Caption = cluster.Caption, + StatsWells = statsWells, }; return statClusterDto; } @@ -108,36 +115,62 @@ namespace AsbCloudInfrastructure.Services .AsNoTracking() .ToListAsync(token); + var statWellDto = await CalcStatWell(operations, idWell, token); + return statWellDto; + } + + private async Task CalcStatWell(IEnumerable operations, int idWell, + CancellationToken token = default) + { + var wellOperations = operations + .Where(o => o.IdWell == idWell); + + var well = await cachedWell.FirstOrDefaultAsync(w => w.Id == idWell, token); + var statWellDto = new StatWellDto { - Sections = GetWellSectionsStats(operations), - Total = GetWellStats(operations), + Id = idWell, + Caption = well.Caption, + Companies = await wellService.GetCompaniesAsync(idWell, token), + Sections = CalcSectionsStats(operations), + Total = GetStat(operations), }; return statWellDto; } - private IEnumerable GetWellSectionsStats(IEnumerable operations) + private IEnumerable CalcSectionsStats(IEnumerable operations) { - var sectionTypesIds = operations.Select(o => o.IdWellSectionType).Distinct(); - var sections = new List(sectionTypesIds.Count()); + var sectionTypeIds = operations + .Select(o => o.IdWellSectionType) + .Distinct(); + + var sectionTypes = cachedSectionsTypes + .Where(s => sectionTypeIds.Contains(s.Id)) + .ToDictionary(s => s.Id); + + var sections = new List(sectionTypes.Count); var operationsPlan = MakeOperationsExt(operations.Where(o => o.IdType == 0)); var operationsFact = MakeOperationsExt(operations.Where(o => o.IdType == 0)); - foreach (var idSectionType in sectionTypesIds) + foreach ((var id, var sectionType) in sectionTypes) { - var statPlan = CalcSectionStat(operationsPlan, idSectionType); - var statFact = CalcSectionStat(operationsFact, idSectionType); - StatOperationDto section = MakeWellSectionDto(idSectionType, statPlan, statFact); + StatSectionDto section = new StatSectionDto + { + Id = id, + Caption = sectionType.Caption, + Plan = CalcSectionStat(operationsPlan, id), + Fact = CalcSectionStat(operationsFact, id), + }; sections.Add(section); } return sections; } - private static StatOperationDto GetWellStats(IEnumerable operations) + private static PlanFactBase GetStat(IEnumerable operations) { var operationsPlan = MakeOperationsExt(operations.Where(o => o.IdType == 0)); var operationsFact = MakeOperationsExt(operations.Where(o => o.IdType == 0)); - var section = new StatOperationDto + var section = new PlanFactBase { Plan = CalcStat(operationsPlan), Fact = CalcStat(operationsFact), @@ -145,18 +178,7 @@ namespace AsbCloudInfrastructure.Services return section; } - private StatOperationDto MakeWellSectionDto(int idSectionType, StatOperationBase statPlan, StatOperationBase statFact) - { - return new StatOperationDto - { - Id = idSectionType, - Caption = cachedSectionsTypes.FirstOrDefault(s => s.Id == idSectionType)?.Caption, - Plan = statPlan, - Fact = statFact, - }; - } - - private static StatOperationBase CalcSectionStat(IEnumerable operations, int idSectionType) + private static StatOperationsDto CalcSectionStat(IEnumerable operations, int idSectionType) { var sectionOperations = operations .Where(o => o.IdWellSectionType == idSectionType) @@ -165,9 +187,9 @@ namespace AsbCloudInfrastructure.Services return CalcStat(sectionOperations); } - private static StatOperationBase CalcStat(IEnumerable operations) + private static StatOperationsDto CalcStat(IEnumerable operations) { - var section = new StatOperationBase + var section = new StatOperationsDto { Start = operations.First().Start, End = operations.Max(o => (o.Start.AddHours(o.DurationHours))), diff --git a/AsbCloudInfrastructure/Services/WellService.cs b/AsbCloudInfrastructure/Services/WellService.cs index 3be51a77..972f55c2 100644 --- a/AsbCloudInfrastructure/Services/WellService.cs +++ b/AsbCloudInfrastructure/Services/WellService.cs @@ -17,12 +17,14 @@ namespace AsbCloudInfrastructure.Services private readonly IAsbCloudDbContext db; private readonly ITelemetryTracker telemetryTracker; private readonly CacheTable cacheRelationCompaniesWells; + private readonly CacheTable cacheWells; public WellService(IAsbCloudDbContext db, ITelemetryTracker telemetryTracker, CacheDb cacheDb) { this.db = db; this.telemetryTracker = telemetryTracker; cacheRelationCompaniesWells = cacheDb.GetCachedTable((AsbCloudDbContext)db); + cacheWells = cacheDb.GetCachedTable((AsbCloudDbContext)db); } public async Task> GetTransmittingWellsAsync(int idCompany, CancellationToken token) @@ -74,5 +76,23 @@ namespace AsbCloudInfrastructure.Services return wellDto; } + + public async Task GetWellCaptionByIdAsync(int idWell, CancellationToken token) + { + var entity = await cacheWells.FirstOrDefaultAsync(w => w.Id == idWell, token).ConfigureAwait(false); + var dto = entity.Adapt(); + return dto.Caption; + } + + public async Task> GetCompaniesAsync(int idWell, CancellationToken token) + { + var well = await db.Wells + .Include(w=>w.RelationCompaniesWells) + .ThenInclude(r=>r.Company) + .FirstOrDefaultAsync(w => w.Id == idWell, token) + .ConfigureAwait(false); + var companies = well.RelationCompaniesWells.Select(r => r.Company); + return companies.Adapt(); + } } } diff --git a/AsbCloudWebApi/Controllers/WellOperationStatController.cs b/AsbCloudWebApi/Controllers/WellOperationStatController.cs index b303c643..a1b7d46a 100644 --- a/AsbCloudWebApi/Controllers/WellOperationStatController.cs +++ b/AsbCloudWebApi/Controllers/WellOperationStatController.cs @@ -27,7 +27,7 @@ namespace AsbCloudWebApi.Controllers [HttpGet] [Route("well/{idWell}/stat")] - [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] + [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] public async Task GetOperationStatByWellAsync(int idWell, CancellationToken token = default) {