diff --git a/AsbCloudApp/Data/WellOperationDto.cs b/AsbCloudApp/Data/WellOperationDto.cs index a4e3ca9f..5428a204 100644 --- a/AsbCloudApp/Data/WellOperationDto.cs +++ b/AsbCloudApp/Data/WellOperationDto.cs @@ -1,18 +1,27 @@ -namespace AsbCloudApp.Data +using System; + +namespace AsbCloudApp.Data { public class WellOperationDto : IId { public int Id { get; set; } - public int IdWell { get; set; } + public int IdSection { get; set; } - public string Caption { get; set; } + public int IdCategory { get; set; } - public string Description { get; set; } + public string CategoryName { get; set; } - public double CasingSection { get; set; } + public string Type { get; set; } - public double WellDepth { get; set; } + public string WellDepth { get; set; } + public DateTime StartDate { get; set; } + + public double Duration { get; set; } + + public string Data { get; set; } + + public string Comment { get; set; } } } diff --git a/AsbCloudApp/Services/IWellOperationService.cs b/AsbCloudApp/Services/IWellOperationService.cs new file mode 100644 index 00000000..fe90fdce --- /dev/null +++ b/AsbCloudApp/Services/IWellOperationService.cs @@ -0,0 +1,13 @@ +using System.Threading; +using System.Threading.Tasks; +using AsbCloudApp.Data; + + +namespace AsbCloudApp.Services +{ + public interface IWellOperationService + { + Task> GetAllByWellIdAsync(int idWell, + int skip = 0, int take = 32, CancellationToken token = default); + } +} diff --git a/AsbCloudDb/Model/AsbCloudDbContext.cs b/AsbCloudDb/Model/AsbCloudDbContext.cs index 15c79cac..899368e3 100644 --- a/AsbCloudDb/Model/AsbCloudDbContext.cs +++ b/AsbCloudDb/Model/AsbCloudDbContext.cs @@ -27,14 +27,13 @@ namespace AsbCloudDb.Model public virtual DbSet TelemetryEvents { get; set; } public virtual DbSet TelemetryMessages { get; set; } public virtual DbSet TelemetryUsers { get; set; } - public virtual DbSet TelemetryOperations { get; set; } + public virtual DbSet TelemetryOperations { get; set; } public virtual DbSet TelemetryAnalysis { get; set; } public virtual DbSet WellSections { get; set; } public virtual DbSet WellSectionTypes { get; set; } public virtual DbSet WellOperations { get; set; } public virtual DbSet WellTypes { get; set; } public virtual DbSet LastData { get; set; } - public virtual DbSet TvdData { get; set; } //var options = new DbContextOptionsBuilder() // .UseNpgsql("Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True") @@ -164,10 +163,10 @@ namespace AsbCloudDb.Model .HasConstraintName("t_relation_company_well_t_company_id_fk"); }); - modelBuilder.Entity(entity => + modelBuilder.Entity(entity => { - entity.HasIndex(d => d.Depth); - entity.HasIndex(d => d.Date); + entity.HasIndex(d => d.WellDepth); + entity.HasIndex(d => d.StartDate); }); FillData(modelBuilder); @@ -205,26 +204,26 @@ namespace AsbCloudDb.Model }); }); - modelBuilder.Entity(entity => + modelBuilder.Entity(entity => { - entity.HasData(new List { - new Operation {Id = 1, OperationType = 0, Name = "Невозможно определить операцию"}, - new Operation {Id = 2, OperationType = 0, Name = "Роторное бурение" }, - new Operation {Id = 3, OperationType = 0, Name = "Слайдирование" }, - new Operation {Id = 4, OperationType = 0, Name = "Подъем с проработкой" }, - new Operation {Id = 5, OperationType = 0, Name = "Спуск с проработкой" }, - new Operation {Id = 6, OperationType = 0, Name = "Подъем с промывкой" }, - new Operation {Id = 7, OperationType = 0, Name = "Спуск с промывкой" }, - new Operation {Id = 8, OperationType = 0, Name = "Спуск в скважину" }, - new Operation {Id = 9, OperationType = 0, Name = "Спуск с вращением" }, - new Operation {Id = 10, OperationType = 0, Name = "Подъем из скважины" }, - new Operation {Id = 11, OperationType = 0, Name = "Подъем с вращением" }, - new Operation {Id = 12, OperationType = 0, Name = "Промывка в покое" }, - new Operation {Id = 13, OperationType = 0, Name = "Промывка с вращением" }, - new Operation {Id = 14, OperationType = 0, Name = "Удержание в клиньях" }, - new Operation {Id = 15, OperationType = 0, Name = "Неподвижное состояние" }, - new Operation {Id = 16, OperationType = 0, Name = "Вращение без циркуляции" }, - new Operation {Id = 17, OperationType = 0, Name = "На поверхности" } + entity.HasData(new List { + new OperationCategory {Id = 1, OperationType = 0, Name = "Невозможно определить операцию"}, + new OperationCategory {Id = 2, OperationType = 0, Name = "Роторное бурение" }, + new OperationCategory {Id = 3, OperationType = 0, Name = "Слайдирование" }, + new OperationCategory {Id = 4, OperationType = 0, Name = "Подъем с проработкой" }, + new OperationCategory {Id = 5, OperationType = 0, Name = "Спуск с проработкой" }, + new OperationCategory {Id = 6, OperationType = 0, Name = "Подъем с промывкой" }, + new OperationCategory {Id = 7, OperationType = 0, Name = "Спуск с промывкой" }, + new OperationCategory {Id = 8, OperationType = 0, Name = "Спуск в скважину" }, + new OperationCategory {Id = 9, OperationType = 0, Name = "Спуск с вращением" }, + new OperationCategory {Id = 10, OperationType = 0, Name = "Подъем из скважины" }, + new OperationCategory {Id = 11, OperationType = 0, Name = "Подъем с вращением" }, + new OperationCategory {Id = 12, OperationType = 0, Name = "Промывка в покое" }, + new OperationCategory {Id = 13, OperationType = 0, Name = "Промывка с вращением" }, + new OperationCategory {Id = 14, OperationType = 0, Name = "Удержание в клиньях" }, + new OperationCategory {Id = 15, OperationType = 0, Name = "Неподвижное состояние" }, + new OperationCategory {Id = 16, OperationType = 0, Name = "Вращение без циркуляции" }, + new OperationCategory {Id = 17, OperationType = 0, Name = "На поверхности" } }); }); diff --git a/AsbCloudDb/Model/IAsbCloudDbContext.cs b/AsbCloudDb/Model/IAsbCloudDbContext.cs index bf292a7b..aba8c1e1 100644 --- a/AsbCloudDb/Model/IAsbCloudDbContext.cs +++ b/AsbCloudDb/Model/IAsbCloudDbContext.cs @@ -22,7 +22,7 @@ namespace AsbCloudDb.Model DbSet FileCategories { get; set; } DbSet Telemetries { get; set; } DbSet TelemetryUsers { get; set; } - DbSet TelemetryOperations { get; set; } + DbSet TelemetryOperations { get; set; } DbSet TelemetryAnalysis { get; set; } DbSet Wells { get; set; } DbSet WellSections { get; set; } @@ -30,7 +30,6 @@ namespace AsbCloudDb.Model DbSet WellOperations { get; set; } DbSet WellTypes { get; set; } DbSet LastData { get; set; } - DbSet TvdData { get; set; } int SaveChanges(); int SaveChanges(bool acceptAllChangesOnSuccess); diff --git a/AsbCloudDb/Model/Operation.cs b/AsbCloudDb/Model/OperationCategory.cs similarity index 96% rename from AsbCloudDb/Model/Operation.cs rename to AsbCloudDb/Model/OperationCategory.cs index a41015bb..c900aef3 100644 --- a/AsbCloudDb/Model/Operation.cs +++ b/AsbCloudDb/Model/OperationCategory.cs @@ -6,7 +6,7 @@ using System.ComponentModel.DataAnnotations.Schema; namespace AsbCloudDb.Model { [Table("t_operations"), Comment("Справочник операций на скважине")] - public class Operation + public class OperationCategory { [Key] [Column("id")] diff --git a/AsbCloudDb/Model/TelemetryAnalysis.cs b/AsbCloudDb/Model/TelemetryAnalysis.cs index 2ef09d85..30346d6c 100644 --- a/AsbCloudDb/Model/TelemetryAnalysis.cs +++ b/AsbCloudDb/Model/TelemetryAnalysis.cs @@ -28,8 +28,8 @@ namespace AsbCloudDb.Model [JsonIgnore] [ForeignKey(nameof(IdOperation))] - [InverseProperty(nameof(Model.Operation.Analysis))] - public virtual Operation Operation { get; set; } + [InverseProperty(nameof(Model.OperationCategory.Analysis))] + public virtual OperationCategory Operation { get; set; } [Column("unix_date", TypeName = "bigint"), Comment("Unix timestamp для Linq запросов с вычислением дат")] diff --git a/AsbCloudDb/Model/TvdData.cs b/AsbCloudDb/Model/TvdData.cs deleted file mode 100644 index 087d8cfd..00000000 --- a/AsbCloudDb/Model/TvdData.cs +++ /dev/null @@ -1,48 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore; -using System.ComponentModel.DataAnnotations; -using System.ComponentModel.DataAnnotations.Schema; -using System.Text.Json.Serialization; - -namespace AsbCloudDb.Model -{ - [Table("t_tvd_data"), Comment("Данные для графика глубина-день")] - public class TvdData - { - [Key] - [Column("id")] - public int Id { get; set; } - - [Column("id_section"), Comment("Id секции скважины")] - public int IdSection { get; set; } - - [Column("id_category"), Comment("Id категории операции")] - public int IdCategory { get; set; } - - [Column("type"), Comment("План или Факт")] - public string Type { get; set; } - - [Column("depth"), Comment("Глубина, на которой производилась операция")] - public string Depth { get; set; } - - [Column("date"), Comment("Дата операции")] - public DateTime Date { get; set; } - - [Column("duration_hours"), Comment("Продолжительность в часах")] - public double Duration { get; set; } - - [Column("data"), Comment("Данные по операции")] - public string Data { get; set; } - - [Column("comment"), Comment("Комментарий")] - public string Comment { get; set; } - - [JsonIgnore] - [ForeignKey(nameof(IdSection))] - public virtual WellSection WellSection { get; set; } - - [JsonIgnore] - [ForeignKey(nameof(IdCategory))] - public virtual Operation Operation { get; set; } - } -} diff --git a/AsbCloudDb/Model/WellOperation.cs b/AsbCloudDb/Model/WellOperation.cs index 946b7119..b5fa40ff 100644 --- a/AsbCloudDb/Model/WellOperation.cs +++ b/AsbCloudDb/Model/WellOperation.cs @@ -1,36 +1,51 @@ -using Microsoft.EntityFrameworkCore; +using System; +using Microsoft.EntityFrameworkCore; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Text.Json.Serialization; -#nullable disable - namespace AsbCloudDb.Model { - [Table("t_well_operation"), Comment("Операции по скважине")] - public class WellOperation: IId, IIdWell + [Table("t_well_operations"), Comment("Данные по операциям на скважине")] + public class WellOperation { [Key] [Column("id")] public int Id { get; set; } - [Column("id_well")] + [Column("id_well"), Comment("Id скважины")] public int IdWell { get; set; } - [Column("caption")] - public string Caption { get; set; } + [Column("id_section"), Comment("Id секции скважины")] + public int IdSection { get; set; } - [Column("description")] - public string Description { get; set; } + [Column("id_category"), Comment("Id категории операции")] + public int IdCategory { get; set; } - [Column("casing_section")] - public double CasingSection { get; set; } + [Column("type"), Comment("План или Факт")] + public string Type { get; set; } - [Column("well_depth")] - public double WellDepth { get; set; } + [Column("depth"), Comment("Глубина, на которой производилась операция")] + public string WellDepth { get; set; } + + [Column("date"), Comment("Дата начала операции")] + public DateTime StartDate { get; set; } + + [Column("duration_hours"), Comment("Продолжительность в часах")] + public double Duration { get; set; } + + [Column("data"), Comment("Данные по операции")] + public string Data { get; set; } + + [Column("comment"), Comment("Комментарий")] + public string Comment { get; set; } [JsonIgnore] - [ForeignKey(nameof(IdWell))] - public virtual Well Well { get; set; } + [ForeignKey(nameof(IdSection))] + public virtual WellSection WellSection { get; set; } + + [JsonIgnore] + [ForeignKey(nameof(IdCategory))] + public virtual OperationCategory OperationCategory { get; set; } } } diff --git a/AsbCloudInfrastructure/DependencyInjection.cs b/AsbCloudInfrastructure/DependencyInjection.cs index 09488b1f..2289e979 100644 --- a/AsbCloudInfrastructure/DependencyInjection.cs +++ b/AsbCloudInfrastructure/DependencyInjection.cs @@ -37,6 +37,7 @@ namespace AsbCloudInfrastructure services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient, LastDataService>(); services.AddTransient, LastDataService>(); diff --git a/AsbCloudInfrastructure/Services/TelemetryAnalyticsService.cs b/AsbCloudInfrastructure/Services/TelemetryAnalyticsService.cs index 2004ed8a..bbccbc42 100644 --- a/AsbCloudInfrastructure/Services/TelemetryAnalyticsService.cs +++ b/AsbCloudInfrastructure/Services/TelemetryAnalyticsService.cs @@ -17,9 +17,9 @@ namespace AsbCloudInfrastructure.Services private readonly IAsbCloudDbContext db; private readonly ITelemetryService telemetryService; private readonly ISaubDataCache saubDataCache; - private readonly CacheTable cacheOperations; + private readonly CacheTable cacheOperations; private readonly TelemetryOperationDetectorService operationDetectorService; - private readonly IEnumerable operations; + private readonly IEnumerable operations; public TelemetryAnalyticsService(IAsbCloudDbContext db, ITelemetryService telemetryService, ISaubDataCache saubDataCache, CacheDb cacheDb) @@ -27,7 +27,7 @@ namespace AsbCloudInfrastructure.Services this.db = db; this.telemetryService = telemetryService; this.saubDataCache = saubDataCache; - cacheOperations = cacheDb.GetCachedTable((AsbCloudDbContext)db); + cacheOperations = cacheDb.GetCachedTable((AsbCloudDbContext)db); operations = cacheOperations.Select(c => true); operationDetectorService = new TelemetryOperationDetectorService(operations); } diff --git a/AsbCloudInfrastructure/Services/TelemetryOperationDetector.cs b/AsbCloudInfrastructure/Services/TelemetryOperationDetector.cs index 8ed66629..bd99f812 100644 --- a/AsbCloudInfrastructure/Services/TelemetryOperationDetector.cs +++ b/AsbCloudInfrastructure/Services/TelemetryOperationDetector.cs @@ -7,7 +7,7 @@ namespace AsbCloudInfrastructure.Services public class TelemetryOperationDetector { public int Order { get; set; } - public Operation Operation { get; set; } + public OperationCategory Operation { get; set; } public Func Detect { get; set; } } } diff --git a/AsbCloudInfrastructure/Services/TelemetryOperationDetectorService.cs b/AsbCloudInfrastructure/Services/TelemetryOperationDetectorService.cs index 5c676114..66c0b4ae 100644 --- a/AsbCloudInfrastructure/Services/TelemetryOperationDetectorService.cs +++ b/AsbCloudInfrastructure/Services/TelemetryOperationDetectorService.cs @@ -9,7 +9,7 @@ namespace AsbCloudInfrastructure.Services { private readonly IEnumerable detectors; - public TelemetryOperationDetectorService(IEnumerable operations) + public TelemetryOperationDetectorService(IEnumerable operations) { detectors = new List() { @@ -180,8 +180,8 @@ namespace AsbCloudInfrastructure.Services }; } - public Operation DetectOperation(TelemetryAnalysisDto data) => + public OperationCategory DetectOperation(TelemetryAnalysisDto data) => detectors.OrderBy(d => d.Order).First(o => o.Detect(data)).Operation - ?? new Operation { Id = 1, Name = "Невозможно определить операцию" }; + ?? new OperationCategory { Id = 1, Name = "Невозможно определить операцию" }; } } diff --git a/AsbCloudInfrastructure/Services/WellOperationService.cs b/AsbCloudInfrastructure/Services/WellOperationService.cs new file mode 100644 index 00000000..aaa37aa2 --- /dev/null +++ b/AsbCloudInfrastructure/Services/WellOperationService.cs @@ -0,0 +1,61 @@ +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using AsbCloudApp.Data; +using AsbCloudApp.Services; +using AsbCloudDb.Model; +using AsbCloudInfrastructure.Services.Cache; +using Mapster; + +namespace AsbCloudInfrastructure.Services +{ + public class WellOperationService : IWellOperationService + { + private readonly IAsbCloudDbContext context; + private readonly DbSet dbSet; + private readonly CacheTable cachedOperationTypes; + + public WellOperationService(IAsbCloudDbContext context, Cache.CacheDb cache) + { + this.context = context; + dbSet = context.Set(); + cachedOperationTypes = cache.GetCachedTable((DbContext)context); + } + + public async Task> GetAllByWellIdAsync(int idWell, + int skip = 0, int take = 32, CancellationToken token = default) + { + var query = dbSet + .Include(s => s.WellSection) + .Where(s => s.WellSection.IdWell == idWell) + .AsNoTracking(); + + var result = new PaginationContainer + { + Skip = skip, + Take = take, + Count = await query.CountAsync(token).ConfigureAwait(false), + }; + + query = query + .OrderBy(e => e.WellDepth); + + if (skip > 0) + query = query.Skip(skip); + + query = query.Take(take); + + var entities = await query.Take(take).ToListAsync(token).ConfigureAwait(false); + + foreach (var item in entities) + { + var dto = item.Adapt(); + //dto.SectionType = item.WellSectionType.Caption; + result.Items.Add(dto); + } + + return result; + } + } +} diff --git a/AsbCloudWebApi/Controllers/WellOperationController.cs b/AsbCloudWebApi/Controllers/WellOperationController.cs new file mode 100644 index 00000000..6ca3fe4a --- /dev/null +++ b/AsbCloudWebApi/Controllers/WellOperationController.cs @@ -0,0 +1,46 @@ +using System.Threading; +using System.Threading.Tasks; +using AsbCloudApp.Data; +using AsbCloudApp.Services; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Authorization; + +namespace AsbCloudWebApi.Controllers +{ + /// + /// Контроллер операций на скважине + /// + [Route("api/wellOperation")] + [ApiController] + [Authorize] + public class WellOperationController : ControllerBase + { + private readonly IWellOperationService operationService; + private readonly IWellService wellService; + + public WellOperationController(IWellOperationService operationService, IWellService wellService) + { + this.operationService = operationService; + this.wellService = wellService; + } + + [HttpGet] + [ProducesResponseType(typeof(PaginationContainer), (int)System.Net.HttpStatusCode.OK)] + public async Task GetAllAsync(int idWell, int skip = 0, int take = 32, + CancellationToken token = default) + { + if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) + return Forbid(); + + var result = await operationService.GetAllByWellIdAsync(idWell, skip, take, token).ConfigureAwait(false); + return Ok(result); + } + + private async Task CanUserAccessToWellAsync(int idWell, CancellationToken token = default) + { + int? idCompany = User.GetCompanyId(); + return idCompany is not null && await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, + idWell, token).ConfigureAwait(false); + } + } +}