diff --git a/AsbCloudApp/Services/IProcessMapRepository.cs b/AsbCloudApp/Repositories/IProcessMapRepository.cs similarity index 59% rename from AsbCloudApp/Services/IProcessMapRepository.cs rename to AsbCloudApp/Repositories/IProcessMapRepository.cs index 8c9ab6f0..0bc9ac7b 100644 --- a/AsbCloudApp/Services/IProcessMapRepository.cs +++ b/AsbCloudApp/Repositories/IProcessMapRepository.cs @@ -1,11 +1,14 @@ using AsbCloudApp.Data.ProcessMap; +using AsbCloudApp.Requests; +using AsbCloudApp.Services; using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; -namespace AsbCloudApp.Services +namespace AsbCloudApp.Repositories { +#nullable enable /// /// /// @@ -20,5 +23,14 @@ namespace AsbCloudApp.Services /// Task> GetAllAsync(int idWell, DateTime? updateFrom, CancellationToken token = default); + + /// + /// + /// + /// + /// + /// + Task> GetProcessMapAsync(IEnumerable requests, CancellationToken token); } +#nullable disable } \ No newline at end of file diff --git a/AsbCloudApp/Repositories/IWellCompositeRepository.cs b/AsbCloudApp/Repositories/IWellCompositeRepository.cs index c14fd11c..8b24d32f 100644 --- a/AsbCloudApp/Repositories/IWellCompositeRepository.cs +++ b/AsbCloudApp/Repositories/IWellCompositeRepository.cs @@ -1,4 +1,5 @@ using AsbCloudApp.Data; +using AsbCloudApp.Data.ProcessMap; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; @@ -27,6 +28,14 @@ namespace AsbCloudApp.Repositories /// /// Task SaveAsync(int idWell, IEnumerable wellComposites, CancellationToken token); + + /// + /// Получение РТК по композитной скважине + /// + /// + /// + /// + Task> GetCompositeProcessMap(int idWell, CancellationToken token); } #nullable disable } diff --git a/AsbCloudApp/Requests/ProcessMapRequest.cs b/AsbCloudApp/Requests/ProcessMapRequest.cs new file mode 100644 index 00000000..55adb7fd --- /dev/null +++ b/AsbCloudApp/Requests/ProcessMapRequest.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; + +namespace AsbCloudApp.Requests +{ +#nullable enable + /// + /// Параметры для запроса получения РТК + /// + public class ProcessMapRequest + { + /// + /// Идентификатор скважины + /// + public int IdWell { get; set; } + + /// + /// Тип секции + /// + public int? IdWellSectionType { get; set; } + + /// + /// Дата обновления + /// + public DateTime? UpdateFrom { get; set; } + } +#nullable disable +} diff --git a/AsbCloudApp/Services/IProcessMapService.cs b/AsbCloudApp/Services/IProcessMapService.cs index 3d2eeff4..11bb2846 100644 --- a/AsbCloudApp/Services/IProcessMapService.cs +++ b/AsbCloudApp/Services/IProcessMapService.cs @@ -1,6 +1,5 @@ using AsbCloudApp.Data.ProcessMap; using System.Collections.Generic; -using System.IO; using System.Threading; using System.Threading.Tasks; diff --git a/AsbCloudInfrastructure/PredicateBuilder.cs b/AsbCloudInfrastructure/PredicateBuilder.cs new file mode 100644 index 00000000..ff0bf720 --- /dev/null +++ b/AsbCloudInfrastructure/PredicateBuilder.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; + +namespace AsbCloudInfrastructure +{ +#nullable enable + /// + /// stolen from https://github.com/lotosbin/BinbinPredicateBuilder + /// + public static class PredicateBuilder + { + /// + /// Combines the first predicate with the second using the logical "and". + /// + public static Expression> And(this Expression> first, Expression> second) + { + return first.Compose(second, Expression.AndAlso); + } + + /// + /// Combines the first predicate with the second using the logical "or". + /// + public static Expression> Or(this Expression> first, Expression> second) + { + return first.Compose(second, Expression.OrElse); + } + + /// + /// Negates the predicate. + /// + public static Expression> Not(this Expression> expression) + { + var negated = Expression.Not(expression.Body); + return Expression.Lambda>(negated, expression.Parameters); + } + + private static Expression Compose(this Expression first, Expression second, Func merge) + { + var map = first.Parameters + .Select((f, i) => new { f, s = second.Parameters[i] }) + .ToDictionary(p => p.s, p => p.f); + + var tryReplaceParametr = (Expression node) => + { + if (node is ParameterExpression parameter) + if (map.TryGetValue(parameter, out ParameterExpression? replacement)) + return replacement; + return node; + }; + + var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body); + return Expression.Lambda(merge(first.Body, secondBody), first.Parameters); + } + + class ParameterRebinder : ExpressionVisitor + { + private readonly Dictionary map; + + private ParameterRebinder(Dictionary map) + { + this.map = map; + } + + public static Expression ReplaceParameters(Dictionary map, Expression exp) + { + return new ParameterRebinder(map).Visit(exp); + } + + protected override Expression VisitParameter(ParameterExpression parametr) + { + if (map.TryGetValue(parametr, out ParameterExpression? replacement)) + parametr = replacement; + return parametr; + } + } + } +} +#nullable disable \ No newline at end of file diff --git a/AsbCloudInfrastructure/Repository/ProcessMapRepository.cs b/AsbCloudInfrastructure/Repository/ProcessMapRepository.cs index f24ce61d..09f1f0a4 100644 --- a/AsbCloudInfrastructure/Repository/ProcessMapRepository.cs +++ b/AsbCloudInfrastructure/Repository/ProcessMapRepository.cs @@ -1,12 +1,17 @@ using AsbCloudApp.Data; using AsbCloudApp.Data.ProcessMap; +using AsbCloudApp.Repositories; +using AsbCloudApp.Requests; using AsbCloudApp.Services; using AsbCloudDb.Model; +using DocumentFormat.OpenXml.Spreadsheet; using Mapster; using Microsoft.EntityFrameworkCore; +using Org.BouncyCastle.Asn1.Ocsp; using System; using System.Collections.Generic; using System.Linq; +using System.Linq.Expressions; using System.Threading; using System.Threading.Tasks; @@ -30,7 +35,15 @@ namespace AsbCloudInfrastructure.Repository public async Task> GetAllAsync(int idWell, DateTime? updateFrom, CancellationToken token) { - var entities = await BuildQuery(idWell, updateFrom) + var requests = new[] + { + new ProcessMapRequest { + IdWell = idWell, + UpdateFrom = updateFrom + } + }; + + var entities = await BuildQuery(requests) .OrderBy(e => e.DepthStart) .ThenBy(e => e.Id) .ToListAsync(token) @@ -40,6 +53,15 @@ namespace AsbCloudInfrastructure.Repository return dtos; } + public async Task> GetProcessMapAsync(IEnumerable requests, CancellationToken token) + { + var entities = await BuildQuery(requests) + .ToListAsync(token) + .ConfigureAwait(false); + var dtos = entities.Select(Convert).ToList(); + return dtos; + } + public override async Task InsertAsync(ProcessMapDto dto, CancellationToken token) { @@ -56,18 +78,31 @@ namespace AsbCloudInfrastructure.Repository return result; } - - private IQueryable BuildQuery(int idWell, DateTime? updateFrom) + private IQueryable BuildQuery(IEnumerable requests) { - var query = GetQuery().Where(e => e.IdWell == idWell); - - if (updateFrom is not null) + var query = GetQuery(); + if (requests.Any()) { - var timezone = wellService.GetTimezone(idWell); - var updateFromUtc = updateFrom?.ToUtcDateTimeOffset(timezone.Hours); - query.Where(e => e.LastUpdate >= updateFromUtc); - } + Expression> predicate = map => false; + foreach (var request in requests) + { + Expression> predicate2 = map => map.IdWell == request.IdWell; + if (request.IdWellSectionType is not null) + predicate2 = predicate2.And(map => map.IdWellSectionType == request.IdWellSectionType); + + if (request.UpdateFrom is not null) + { + var timezone = wellService.GetTimezone(request.IdWell); + var updateFromUtc = request.UpdateFrom?.ToUtcDateTimeOffset(timezone.Hours); + predicate2 = predicate2.And(map => map.LastUpdate >= updateFromUtc); + } + + predicate = predicate.Or(predicate2); + } + query = query.Where(predicate); + + } return query; } protected override ProcessMapDto Convert(ProcessMap entity) diff --git a/AsbCloudInfrastructure/Repository/WellCompositeRepository.cs b/AsbCloudInfrastructure/Repository/WellCompositeRepository.cs index de56ee60..d9b7b8ab 100644 --- a/AsbCloudInfrastructure/Repository/WellCompositeRepository.cs +++ b/AsbCloudInfrastructure/Repository/WellCompositeRepository.cs @@ -1,8 +1,11 @@ using AsbCloudApp.Data; +using AsbCloudApp.Data.ProcessMap; using AsbCloudApp.Repositories; +using AsbCloudApp.Requests; using AsbCloudDb.Model; using Mapster; using Microsoft.EntityFrameworkCore; +using System; using System.Collections.Generic; using System.Linq; using System.Threading; @@ -14,10 +17,12 @@ namespace AsbCloudInfrastructure.Repository public class WellCompositeRepository : IWellCompositeRepository { private readonly IAsbCloudDbContext db; + private readonly IProcessMapRepository processMapRepository; - public WellCompositeRepository(IAsbCloudDbContext db) + public WellCompositeRepository(IAsbCloudDbContext db, IProcessMapRepository processMapRepository) { this.db = db; + this.processMapRepository = processMapRepository; } /// @@ -44,6 +49,51 @@ namespace AsbCloudInfrastructure.Repository return db.SaveChangesAsync(token); } + /// + public async Task> GetCompositeProcessMap(int idWell, CancellationToken token) + { + var dtos = await GetAsync(idWell, token); + + var requests = dtos.Select(x => new ProcessMapRequest { + IdWell = x.IdWellSrc, + IdWellSectionType = x.IdWellSectionType + }); + + var processMap = await processMapRepository.GetProcessMapAsync(requests, token); + + var result = processMap.Select(x => new ProcessMapDto + { + IdWell = x.IdWell, + IdWellSectionType = x.IdWellSectionType, + RopPlan = x.RopPlan, + DepthStart = x.DepthStart, + DepthEnd = x.DepthEnd, + AxialLoad = new PlanFactDto + { + Plan = x.AxialLoad.Fact ?? x.AxialLoad.Plan, + }, + Flow = new PlanFactDto + { + Plan = x.Flow.Fact ?? x.Flow.Plan + }, + Pressure = new PlanFactDto + { + Plan = x.Pressure.Fact ?? x.Pressure.Plan + }, + TopDriveSpeed = new PlanFactDto + { + Plan = x.TopDriveSpeed.Fact ?? x.TopDriveSpeed.Plan + }, + TopDriveTorque = new PlanFactDto + { + Plan = x.TopDriveTorque.Fact ?? x.TopDriveTorque.Plan + }, + LastUpdate = DateTime.UtcNow + }); + + return result; + } + private static WellComposite Convert(int idWell, WellCompositeDto dto) { var entity = dto.Adapt(); diff --git a/AsbCloudInfrastructure/Services/ProcessMap/ProcessMapService.cs b/AsbCloudInfrastructure/Services/ProcessMap/ProcessMapService.cs index 2b932368..0ede2e82 100644 --- a/AsbCloudInfrastructure/Services/ProcessMap/ProcessMapService.cs +++ b/AsbCloudInfrastructure/Services/ProcessMap/ProcessMapService.cs @@ -43,6 +43,7 @@ namespace AsbCloudInfrastructure.Services.ProcessMap this.subsystemOperationTimeService = subsystemOperationTimeService; } + /// public async Task> GetProcessMapAsync(int idWell, CancellationToken token) { var well = wellService.GetOrDefault(idWell) diff --git a/AsbCloudWebApi/Controllers/ProcessMapController.cs b/AsbCloudWebApi/Controllers/ProcessMapController.cs index f5c955aa..3a1430b9 100644 --- a/AsbCloudWebApi/Controllers/ProcessMapController.cs +++ b/AsbCloudWebApi/Controllers/ProcessMapController.cs @@ -1,4 +1,5 @@ using AsbCloudApp.Data.ProcessMap; +using AsbCloudApp.Repositories; using AsbCloudApp.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; diff --git a/AsbCloudWebApi/Controllers/WellCompositeController.cs b/AsbCloudWebApi/Controllers/WellCompositeController.cs index c6f5c9cf..66a0d65b 100644 --- a/AsbCloudWebApi/Controllers/WellCompositeController.cs +++ b/AsbCloudWebApi/Controllers/WellCompositeController.cs @@ -1,4 +1,5 @@ using AsbCloudApp.Data; +using AsbCloudApp.Data.ProcessMap; using AsbCloudApp.Repositories; using AsbCloudApp.Services; using Microsoft.AspNetCore.Authorization; @@ -21,7 +22,8 @@ namespace AsbCloudWebApi.Controllers private readonly IWellCompositeRepository wellCompositeRepository; private readonly IWellService wellService; - public WellCompositeController(IWellCompositeRepository wellCompositeRepository, IWellService wellService) + public WellCompositeController(IWellCompositeRepository wellCompositeRepository, + IWellService wellService) { this.wellCompositeRepository = wellCompositeRepository; this.wellService = wellService; @@ -63,6 +65,24 @@ namespace AsbCloudWebApi.Controllers return Ok(result); } + /// + /// Получение РТК по композитной скважине + /// + /// + /// + /// + [HttpGet("getCompositeProcessMap")] + [Permission] + [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] + public async Task GetCompositeProcessMap(int idWell, CancellationToken token = default) + { + if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) + return Forbid(); + + var result = await wellCompositeRepository.GetCompositeProcessMap(idWell, token).ConfigureAwait(false); + return Ok(result); + } + private async Task CanUserAccessToWellAsync(int idWell, CancellationToken token = default) { int? idCompany = User.GetCompanyId();