diff --git a/Persistence.API/Controllers/TechMessagesController.cs b/Persistence.API/Controllers/TechMessagesController.cs index a7b0094..e5705b9 100644 --- a/Persistence.API/Controllers/TechMessagesController.cs +++ b/Persistence.API/Controllers/TechMessagesController.cs @@ -52,7 +52,7 @@ public class TechMessagesController : ControllerBase /// /// [HttpGet("statistics")] - public async Task>> GetStatistics([FromQuery] IEnumerable autoDrillingSystem, [FromQuery] IEnumerable categoryIds, CancellationToken token) + public async Task>> GetStatistics([FromQuery] IEnumerable autoDrillingSystem, [FromQuery] IEnumerable categoryIds, CancellationToken token) { var result = await techMessagesRepository.GetStatistics(autoDrillingSystem, categoryIds, token); @@ -103,16 +103,17 @@ public class TechMessagesController : ControllerBase /// /// Добавить новые технологические сообщения /// + /// /// /// /// - [HttpPost] + [HttpPost("{systemId}")] [ProducesResponseType(typeof(int), (int)HttpStatusCode.Created)] - public async Task AddRange([FromBody] IEnumerable dtos, CancellationToken token) + public async Task AddRange([FromRoute] Guid systemId, [FromBody] IEnumerable dtos, CancellationToken token) { var userId = User.GetUserId(); - var result = await techMessagesRepository.AddRange(dtos, userId, token); + var result = await techMessagesRepository.AddRange(systemId, dtos, userId, token); return CreatedAtAction(nameof(AddRange), result); } diff --git a/Persistence.Client/Clients/Interfaces/ITechMessagesClient.cs b/Persistence.Client/Clients/Interfaces/ITechMessagesClient.cs index bbf635c..ce8a7ca 100644 --- a/Persistence.Client/Clients/Interfaces/ITechMessagesClient.cs +++ b/Persistence.Client/Clients/Interfaces/ITechMessagesClient.cs @@ -14,7 +14,7 @@ public interface ITechMessagesClient : IDisposable /// /// /// - Task AddRange(IEnumerable dtos, CancellationToken token); + Task AddRange(Guid systemId, IEnumerable dtos, CancellationToken token); /// /// Получить диапазон дат, для которых есть данные в репозитории @@ -43,16 +43,16 @@ public interface ITechMessagesClient : IDisposable /// /// Получить статистику по системам /// - /// + /// /// /// /// - Task> GetStatistics(string autoDrillingSystem, int categoryId, CancellationToken token); + Task> GetStatistics(IEnumerable systemIds, IEnumerable categoryIds, CancellationToken token); /// /// Получить список всех систем /// /// /// - Task> GetSystems(CancellationToken token); + Task> GetSystems(CancellationToken token); } \ No newline at end of file diff --git a/Persistence.Client/Clients/Interfaces/Refit/IRefitTechMessagesClient.cs b/Persistence.Client/Clients/Interfaces/Refit/IRefitTechMessagesClient.cs index 64cb49e..138a0cd 100644 --- a/Persistence.Client/Clients/Interfaces/Refit/IRefitTechMessagesClient.cs +++ b/Persistence.Client/Clients/Interfaces/Refit/IRefitTechMessagesClient.cs @@ -1,4 +1,5 @@ -using Persistence.Models; +using Microsoft.AspNetCore.Mvc; +using Persistence.Models; using Persistence.Models.Requests; using Refit; @@ -11,11 +12,11 @@ namespace Persistence.Client.Clients.Interfaces.Refit [Get($"{BaseRoute}")] Task>> GetPage([Query] PaginationRequest request, CancellationToken token); - [Post($"{BaseRoute}")] - Task> AddRange([Body] IEnumerable dtos, CancellationToken token); + [Post($"{BaseRoute}/{{systemId}}")] + Task> AddRange(Guid systemId, [Body] IEnumerable dtos, CancellationToken token); [Get($"{BaseRoute}/systems")] - Task>> GetSystems(CancellationToken token); + Task>> GetSystems(CancellationToken token); [Get($"{BaseRoute}/range")] Task> GetDatesRangeAsync(CancellationToken token); @@ -24,6 +25,6 @@ namespace Persistence.Client.Clients.Interfaces.Refit Task>> GetPart(DateTimeOffset dateBegin, int take, CancellationToken token); [Get($"{BaseRoute}/statistics")] - Task>> GetStatistics([Query] string autoDrillingSystem, [Query] int categoryId, CancellationToken token); + Task>> GetStatistics([Query] IEnumerable systemIds, [Query] IEnumerable categoryIds, CancellationToken token); } } diff --git a/Persistence.Client/Clients/TechMessagesClient.cs b/Persistence.Client/Clients/TechMessagesClient.cs index c1000b1..7950784 100644 --- a/Persistence.Client/Clients/TechMessagesClient.cs +++ b/Persistence.Client/Clients/TechMessagesClient.cs @@ -24,17 +24,17 @@ public class TechMessagesClient : BaseClient, ITechMessagesClient return result; } - public async Task AddRange(IEnumerable dtos, CancellationToken token) + public async Task AddRange(Guid systemId, IEnumerable dtos, CancellationToken token) { var result = await ExecutePostResponse( - async () => await refitTechMessagesClient.AddRange(dtos, token), token); + async () => await refitTechMessagesClient.AddRange(systemId, dtos, token), token); return result; } - public async Task> GetSystems(CancellationToken token) + public async Task> GetSystems(CancellationToken token) { - var result = await ExecuteGetResponse>( + var result = await ExecuteGetResponse>( async () => await refitTechMessagesClient.GetSystems(token), token); return result; @@ -56,10 +56,10 @@ public class TechMessagesClient : BaseClient, ITechMessagesClient return result; } - public async Task> GetStatistics(string autoDrillingSystem, int categoryId, CancellationToken token) + public async Task> GetStatistics(IEnumerable systemIds, IEnumerable categoryIds, CancellationToken token) { var result = await ExecuteGetResponse>( - async () => await refitTechMessagesClient.GetStatistics(autoDrillingSystem, categoryId, token), token); + async () => await refitTechMessagesClient.GetStatistics(systemIds, categoryIds, token), token); return result; } diff --git a/Persistence.Database.Postgres/Migrations/20241202072250_TechMessageMigration.Designer.cs b/Persistence.Database.Postgres/Migrations/20241212041758_TechMessageMigration.Designer.cs similarity index 70% rename from Persistence.Database.Postgres/Migrations/20241202072250_TechMessageMigration.Designer.cs rename to Persistence.Database.Postgres/Migrations/20241212041758_TechMessageMigration.Designer.cs index 6ed33f7..0124f24 100644 --- a/Persistence.Database.Postgres/Migrations/20241202072250_TechMessageMigration.Designer.cs +++ b/Persistence.Database.Postgres/Migrations/20241212041758_TechMessageMigration.Designer.cs @@ -9,10 +9,10 @@ using Persistence.Database.Model; #nullable disable -namespace Persistence.Database.Postgres.Migrations +namespace Persistence.Database.Postgres.Migrations.PersistencePostgres { - [DbContext(typeof(PersistenceDbContext))] - [Migration("20241202072250_TechMessageMigration")] + [DbContext(typeof(PersistencePostgresContext))] + [Migration("20241212041758_TechMessageMigration")] partial class TechMessageMigration { /// @@ -48,6 +48,30 @@ namespace Persistence.Database.Postgres.Migrations b.ToTable("DrillingSystem"); }); + modelBuilder.Entity("Persistence.Database.Entity.ParameterData", b => + { + b.Property("DiscriminatorId") + .HasColumnType("uuid") + .HasComment("Дискриминатор системы"); + + b.Property("ParameterId") + .HasColumnType("integer") + .HasComment("Id параметра"); + + b.Property("Timestamp") + .HasColumnType("timestamp with time zone") + .HasComment("Временная отметка"); + + b.Property("Value") + .IsRequired() + .HasColumnType("varchar(256)") + .HasComment("Значение параметра в виде строки"); + + b.HasKey("DiscriminatorId", "ParameterId", "Timestamp"); + + b.ToTable("ParameterData"); + }); + modelBuilder.Entity("Persistence.Database.Entity.TechMessage", b => { b.Property("EventId") @@ -59,27 +83,23 @@ namespace Persistence.Database.Postgres.Migrations .HasColumnType("integer") .HasComment("Id Категории важности"); - b.Property("Depth") - .HasColumnType("double precision") - .HasComment("Глубина забоя"); + b.Property("EventState") + .HasColumnType("integer") + .HasComment("Статус события"); - b.Property("MessageText") + b.Property("SystemId") + .HasColumnType("uuid") + .HasComment("Id системы, к которой относится сообщение"); + + b.Property("Text") .IsRequired() .HasColumnType("varchar(512)") .HasComment("Текст сообщения"); - b.Property("SystemId") - .HasColumnType("uuid") - .HasComment("Id системы автобурения, к которой относится сообщение"); - b.Property("Timestamp") .HasColumnType("timestamp with time zone") .HasComment("Дата возникновения"); - b.Property("UserId") - .HasColumnType("uuid") - .HasComment("Id пользователя за пультом бурильщика"); - b.HasKey("EventId"); b.HasIndex("SystemId"); @@ -110,6 +130,59 @@ namespace Persistence.Database.Postgres.Migrations }); }); + modelBuilder.Entity("Persistence.Database.Model.ChangeLog", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid") + .HasComment("Ключ записи"); + + b.Property("Creation") + .HasColumnType("timestamp with time zone") + .HasComment("Дата создания записи"); + + b.Property("DepthEnd") + .HasColumnType("double precision") + .HasComment("Глубина забоя на дату окончания интервала"); + + b.Property("DepthStart") + .HasColumnType("double precision") + .HasComment("Глубина забоя на дату начала интервала"); + + b.Property("IdAuthor") + .HasColumnType("uuid") + .HasComment("Автор изменения"); + + b.Property("IdDiscriminator") + .HasColumnType("uuid") + .HasComment("Дискриминатор таблицы"); + + b.Property("IdEditor") + .HasColumnType("uuid") + .HasComment("Редактор"); + + b.Property("IdNext") + .HasColumnType("uuid") + .HasComment("Id заменяющей записи"); + + b.Property("IdSection") + .HasColumnType("uuid") + .HasComment("Ключ секции"); + + b.Property("Obsolete") + .HasColumnType("timestamp with time zone") + .HasComment("Дата устаревания (например при удалении)"); + + b.Property("Value") + .IsRequired() + .HasColumnType("jsonb") + .HasComment("Значение"); + + b.HasKey("Id"); + + b.ToTable("ChangeLog"); + }); + modelBuilder.Entity("Persistence.Database.Model.DataSaub", b => { b.Property("Date") diff --git a/Persistence.Database.Postgres/Migrations/20241202072250_TechMessageMigration.cs b/Persistence.Database.Postgres/Migrations/20241212041758_TechMessageMigration.cs similarity index 81% rename from Persistence.Database.Postgres/Migrations/20241202072250_TechMessageMigration.cs rename to Persistence.Database.Postgres/Migrations/20241212041758_TechMessageMigration.cs index ccf18d4..2ee7a55 100644 --- a/Persistence.Database.Postgres/Migrations/20241202072250_TechMessageMigration.cs +++ b/Persistence.Database.Postgres/Migrations/20241212041758_TechMessageMigration.cs @@ -3,7 +3,7 @@ using Microsoft.EntityFrameworkCore.Migrations; #nullable disable -namespace Persistence.Database.Postgres.Migrations +namespace Persistence.Database.Postgres.Migrations.PersistencePostgres { /// public partial class TechMessageMigration : Migration @@ -31,10 +31,9 @@ namespace Persistence.Database.Postgres.Migrations EventId = table.Column(type: "uuid", nullable: false, comment: "Id события"), CategoryId = table.Column(type: "integer", nullable: false, comment: "Id Категории важности"), Timestamp = table.Column(type: "timestamp with time zone", nullable: false, comment: "Дата возникновения"), - Depth = table.Column(type: "double precision", nullable: true, comment: "Глубина забоя"), - MessageText = table.Column(type: "varchar(512)", nullable: false, comment: "Текст сообщения"), - SystemId = table.Column(type: "uuid", nullable: false, comment: "Id системы автобурения, к которой относится сообщение"), - UserId = table.Column(type: "uuid", nullable: false, comment: "Id пользователя за пультом бурильщика") + Text = table.Column(type: "varchar(512)", nullable: false, comment: "Текст сообщения"), + SystemId = table.Column(type: "uuid", nullable: false, comment: "Id системы, к которой относится сообщение"), + EventState = table.Column(type: "integer", nullable: false, comment: "Статус события") }, constraints: table => { diff --git a/Persistence.Database.Postgres/Migrations/PersistencePostgresContextModelSnapshot.cs b/Persistence.Database.Postgres/Migrations/PersistencePostgresContextModelSnapshot.cs index 6ab8159..5a67396 100644 --- a/Persistence.Database.Postgres/Migrations/PersistencePostgresContextModelSnapshot.cs +++ b/Persistence.Database.Postgres/Migrations/PersistencePostgresContextModelSnapshot.cs @@ -1,6 +1,5 @@ // using System; -using System.Collections.Generic; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; @@ -48,8 +47,8 @@ namespace Persistence.Database.Postgres.Migrations modelBuilder.Entity("Persistence.Database.Entity.ParameterData", b => { - b.Property("DiscriminatorId") - .HasColumnType("integer") + b.Property("DiscriminatorId") + .HasColumnType("uuid") .HasComment("Дискриминатор системы"); b.Property("ParameterId") @@ -81,27 +80,23 @@ namespace Persistence.Database.Postgres.Migrations .HasColumnType("integer") .HasComment("Id Категории важности"); - b.Property("Depth") - .HasColumnType("double precision") - .HasComment("Глубина забоя"); + b.Property("EventState") + .HasColumnType("integer") + .HasComment("Статус события"); - b.Property("MessageText") + b.Property("SystemId") + .HasColumnType("uuid") + .HasComment("Id системы, к которой относится сообщение"); + + b.Property("Text") .IsRequired() .HasColumnType("varchar(512)") .HasComment("Текст сообщения"); - b.Property("SystemId") - .HasColumnType("uuid") - .HasComment("Id системы автобурения, к которой относится сообщение"); - b.Property("Timestamp") .HasColumnType("timestamp with time zone") .HasComment("Дата возникновения"); - b.Property("UserId") - .HasColumnType("uuid") - .HasComment("Id пользователя за пультом бурильщика"); - b.HasKey("EventId"); b.HasIndex("SystemId"); @@ -137,48 +132,48 @@ namespace Persistence.Database.Postgres.Migrations b.Property("Id") .ValueGeneratedOnAdd() .HasColumnType("uuid") - .HasColumnName("Id"); + .HasComment("Ключ записи"); b.Property("Creation") .HasColumnType("timestamp with time zone") - .HasColumnName("Creation"); + .HasComment("Дата создания записи"); b.Property("DepthEnd") .HasColumnType("double precision") - .HasColumnName("DepthEnd"); + .HasComment("Глубина забоя на дату окончания интервала"); b.Property("DepthStart") .HasColumnType("double precision") - .HasColumnName("DepthStart"); + .HasComment("Глубина забоя на дату начала интервала"); b.Property("IdAuthor") .HasColumnType("uuid") - .HasColumnName("IdAuthor"); + .HasComment("Автор изменения"); b.Property("IdDiscriminator") .HasColumnType("uuid") - .HasColumnName("IdDiscriminator"); + .HasComment("Дискриминатор таблицы"); b.Property("IdEditor") .HasColumnType("uuid") - .HasColumnName("IdEditor"); + .HasComment("Редактор"); b.Property("IdNext") .HasColumnType("uuid") - .HasColumnName("IdNext"); + .HasComment("Id заменяющей записи"); b.Property("IdSection") .HasColumnType("uuid") - .HasColumnName("IdSection"); + .HasComment("Ключ секции"); b.Property("Obsolete") .HasColumnType("timestamp with time zone") - .HasColumnName("Obsolete"); + .HasComment("Дата устаревания (например при удалении)"); - b.Property>("Value") + b.Property("Value") .IsRequired() .HasColumnType("jsonb") - .HasColumnName("Value"); + .HasComment("Значение"); b.HasKey("Id"); diff --git a/Persistence.Database/Entity/TechMessage.cs b/Persistence.Database/Entity/TechMessage.cs index ea29cc2..5c8b534 100644 --- a/Persistence.Database/Entity/TechMessage.cs +++ b/Persistence.Database/Entity/TechMessage.cs @@ -15,19 +15,16 @@ namespace Persistence.Database.Entity [Comment("Дата возникновения")] public DateTimeOffset Timestamp { get; set; } - [Comment("Глубина забоя")] - public double? Depth { get; set; } - [Column(TypeName = "varchar(512)"), Comment("Текст сообщения")] - public required string MessageText { get; set; } + public required string Text { get; set; } - [Required, Comment("Id системы автобурения, к которой относится сообщение")] + [Required, Comment("Id системы, к которой относится сообщение")] public required Guid SystemId { get; set; } [Required, ForeignKey(nameof(SystemId)), Comment("Система автобурения, к которой относится сообщение")] public virtual required DrillingSystem System { get; set; } - [Comment("Id пользователя за пультом бурильщика")] - public Guid UserId { get; set; } + [Comment("Статус события")] + public int EventState { get; set; } } } diff --git a/Persistence.IntegrationTests/Controllers/TechMessagesControllerTest.cs b/Persistence.IntegrationTests/Controllers/TechMessagesControllerTest.cs index 3501f87..6ad95b3 100644 --- a/Persistence.IntegrationTests/Controllers/TechMessagesControllerTest.cs +++ b/Persistence.IntegrationTests/Controllers/TechMessagesControllerTest.cs @@ -4,6 +4,7 @@ using Persistence.Client; using Persistence.Client.Clients.Interfaces; using Persistence.Database.Entity; using Persistence.Models; +using Persistence.Models.Enumerations; using Persistence.Models.Requests; using Xunit; @@ -53,7 +54,7 @@ namespace Persistence.IntegrationTests.Controllers public async Task GetPage_AfterSave_returns_success() { //arrange - var dtos = await InsertRange(); + var dtos = await InsertRange(Guid.NewGuid()); var dtosCount = dtos.Count(); var requestDto = new PaginationRequest() { @@ -73,7 +74,7 @@ namespace Persistence.IntegrationTests.Controllers [Fact] public async Task InsertRange_returns_success() { - await InsertRange(); + await InsertRange(Guid.NewGuid()); } [Fact] @@ -81,6 +82,7 @@ namespace Persistence.IntegrationTests.Controllers { //arrange const string exceptionMessage = "Ошибка валидации, формата или маршрутизации запроса"; + var systemId = Guid.NewGuid(); var dtos = new List() { new TechMessageDto() @@ -88,17 +90,15 @@ namespace Persistence.IntegrationTests.Controllers EventId = Guid.NewGuid(), CategoryId = -1, // < 0 Timestamp = DateTimeOffset.UtcNow, - Depth = -1, // < 0 - MessageText = string.Empty, // length < 0 - System = string.Concat(Enumerable.Repeat(nameof(TechMessageDto.System), 100)), // length > 256 - UserId = Guid.NewGuid() + Text = string.Empty, // length < 0 + EventState = EventState.Triggered } }; try { //act - var response = await techMessagesClient.AddRange(dtos, new CancellationToken()); + var response = await techMessagesClient.AddRange(systemId, dtos, new CancellationToken()); } catch (Exception ex) { @@ -127,19 +127,15 @@ namespace Persistence.IntegrationTests.Controllers public async Task GetSystems_AfterSave_returns_success() { //arrange - var dtos = await InsertRange(); - var systems = dtos - .Select(e => e.System) - .Distinct() - .ToArray(); + var dtos = await InsertRange(Guid.NewGuid()); //act var response = await techMessagesClient.GetSystems(CancellationToken.None); //assert Assert.NotNull(response); - string?[]? content = response?.ToArray(); - Assert.Equal(systems, content); + var expectedSystemCount = 1; + Assert.Equal(expectedSystemCount, response!.Count()); } [Fact] @@ -150,11 +146,11 @@ namespace Persistence.IntegrationTests.Controllers dbContext.CleanupDbSet(); dbContext.CleanupDbSet(); - var imortantId = 1; - var autoDrillingSystem = nameof(TechMessageDto.System); + var imortantIds = new [] { 1 }; + var systemIds = new [] { Guid.NewGuid() }; //act - var response = await techMessagesClient.GetStatistics(autoDrillingSystem, imortantId, CancellationToken.None); + var response = await techMessagesClient.GetStatistics(systemIds, imortantIds, CancellationToken.None); //assert Assert.NotNull(response); @@ -165,19 +161,20 @@ namespace Persistence.IntegrationTests.Controllers public async Task GetStatistics_AfterSave_returns_success() { //arrange - var imortantId = 0; - var autoDrillingSystem = nameof(TechMessageDto.System); - var dtos = await InsertRange(); - var filteredDtos = dtos.Where(e => e.CategoryId == imortantId && e.System == autoDrillingSystem); + var categoryIds = new[] { 1 }; + var systemId = Guid.NewGuid(); + var dtos = await InsertRange(systemId); + var filteredDtos = dtos.Where(e => categoryIds.Contains(e.CategoryId)); //act - var response = await techMessagesClient.GetStatistics(autoDrillingSystem, imortantId, CancellationToken.None); + var response = await techMessagesClient.GetStatistics([systemId], categoryIds, CancellationToken.None); //assert Assert.NotNull(response); + Assert.NotEmpty(response); var categories = response - .FirstOrDefault()?.Categories - .FirstOrDefault(e => e.Key == 0).Value; + .FirstOrDefault()!.Categories + .Count(); Assert.Equal(filteredDtos.Count(), categories); } @@ -202,7 +199,7 @@ namespace Persistence.IntegrationTests.Controllers public async Task GetDatesRange_AfterSave_returns_success() { //arrange - await InsertRange(); + await InsertRange(Guid.NewGuid()); //act var response = await techMessagesClient.GetDatesRangeAsync(CancellationToken.None); @@ -234,7 +231,7 @@ namespace Persistence.IntegrationTests.Controllers //arrange var dateBegin = DateTimeOffset.UtcNow; var take = 1; - await InsertRange(); + await InsertRange(Guid.NewGuid()); //act var response = await techMessagesClient.GetPart(dateBegin, take, CancellationToken.None); @@ -244,7 +241,7 @@ namespace Persistence.IntegrationTests.Controllers Assert.NotEmpty(response); } - private async Task> InsertRange() + private async Task> InsertRange(Guid systemId) { //arrange memoryCache.Remove(SystemCacheKey); @@ -258,26 +255,22 @@ namespace Persistence.IntegrationTests.Controllers EventId = Guid.NewGuid(), CategoryId = 1, Timestamp = DateTimeOffset.UtcNow, - Depth = 1.11, - MessageText = nameof(TechMessageDto.MessageText), - System = nameof(TechMessageDto.System).ToLower(), - UserId = Guid.NewGuid() + Text = nameof(TechMessageDto.Text), + EventState = Models.Enumerations.EventState.Triggered }, new TechMessageDto() { EventId = Guid.NewGuid(), CategoryId = 2, Timestamp = DateTimeOffset.UtcNow, - Depth = 2.22, - MessageText = nameof(TechMessageDto.MessageText), - System = nameof(TechMessageDto.System).ToLower(), - UserId = Guid.NewGuid() + Text = nameof(TechMessageDto.Text), + EventState = Models.Enumerations.EventState.Triggered } }; //act - var response = await techMessagesClient.AddRange(dtos, CancellationToken.None); + var response = await techMessagesClient.AddRange(systemId, dtos, CancellationToken.None); //assert Assert.Equal(dtos.Count, response); diff --git a/Persistence.Repository/Repositories/TechMessagesRepository.cs b/Persistence.Repository/Repositories/TechMessagesRepository.cs index 4659da0..3779892 100644 --- a/Persistence.Repository/Repositories/TechMessagesRepository.cs +++ b/Persistence.Repository/Repositories/TechMessagesRepository.cs @@ -1,6 +1,7 @@ using Mapster; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Caching.Memory; +using Newtonsoft.Json.Linq; using Persistence.Database.Entity; using Persistence.Models; using Persistence.Models.Requests; @@ -50,12 +51,11 @@ namespace Persistence.Repository.Repositories return dto; } - public async Task> GetStatistics(IEnumerable autoDrillingSystem, IEnumerable categoryIds, CancellationToken token) + public async Task> GetStatistics(IEnumerable systems, IEnumerable categoryIds, CancellationToken token) { var query = GetQueryReadOnly(); - var systems = autoDrillingSystem.Select(s => s.ToLower().Trim()); var result = await query - .Where(e => systems.Count() == 0 || systems.Contains(e.System.Name.ToLower().Trim())) + .Where(e => systems.Count() == 0 || systems.Contains(e.System.SystemId)) .GroupBy(e => e.System.Name, (key, group) => new { System = key, @@ -81,27 +81,16 @@ namespace Persistence.Repository.Repositories return entities; } - public async Task> GetSystems(CancellationToken token) - { - var entities = await GetDrillingSystems(token); - var result = entities.Select(e => e.Name); - - return result; - } - - public async Task AddRange(IEnumerable dtos, Guid userId, CancellationToken token) + public async Task AddRange(Guid systemId, IEnumerable dtos, Guid userId, CancellationToken token) { var entities = new List(); foreach (var dto in dtos) { var entity = dto.Adapt(); - var systems = await GetDrillingSystems(token); - var systemId = systems.FirstOrDefault(e => e.Name.ToLower().Trim() == dto.System.ToLower().Trim())?.SystemId - ?? await CreateDrillingSystem(dto.System, token); + await CreateSystemIfNotExist(systemId, token); entity.SystemId = systemId; - entity.UserId = userId; entities.Add(entity); } @@ -125,6 +114,22 @@ namespace Persistence.Repository.Repositories return dtos; } + public async Task> GetSystems(CancellationToken token) + { + var systems = await memoryCache.GetOrCreateAsync(SystemCacheKey, async f => + { + f.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(CacheExpirationInMinutes); + + var query = db.Set(); + var entities = await query.ToListAsync(token); + var dtos = entities.Select(e => e.Adapt()); + + return dtos; + }); + + return systems ?? []; + } + public async Task GetDatesRangeAsync(CancellationToken token) { var query = GetQueryReadOnly() @@ -144,35 +149,26 @@ namespace Persistence.Repository.Repositories return result; } - private async Task> GetDrillingSystems(CancellationToken token) + private async Task CreateSystemIfNotExist(Guid systemId, CancellationToken token) { - var systems = await memoryCache.GetOrCreateAsync(SystemCacheKey, async f => + var systems = await GetSystems(token); + var system = systems?.FirstOrDefault(e => e.SystemId == systemId); + + if (system == null) { - f.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(CacheExpirationInMinutes); + system = new DrillingSystemDto() + { + SystemId = systemId, + Name = string.Empty + }; - var query = db.Set(); - var entities = await query.ToListAsync(token); - var dtos = entities.Select(e => e.Adapt()); + var entity = system.Adapt(); - return dtos; - }); + await db.Set().AddAsync(entity); + await db.SaveChangesAsync(token); - return systems!; - } - private async Task CreateDrillingSystem(string name, CancellationToken token) - { - memoryCache.Remove(SystemCacheKey); - - var entity = new Database.Entity.DrillingSystem() - { - SystemId = default, - Name = name.ToLower().Trim() - }; - - await db.Set().AddAsync(entity); - await db.SaveChangesAsync(token); - - return entity.SystemId; + memoryCache.Remove(SystemCacheKey); + } } } } diff --git a/Persistence/Models/Enumerations/EventState.cs b/Persistence/Models/Enumerations/EventState.cs new file mode 100644 index 0000000..3f4103c --- /dev/null +++ b/Persistence/Models/Enumerations/EventState.cs @@ -0,0 +1,6 @@ +namespace Persistence.Models.Enumerations; +public enum EventState +{ + NotTriggered = 0, + Triggered = 1, +} diff --git a/Persistence/Models/TechMessageDto.cs b/Persistence/Models/TechMessageDto.cs index 84da656..f429fe7 100644 --- a/Persistence/Models/TechMessageDto.cs +++ b/Persistence/Models/TechMessageDto.cs @@ -1,4 +1,5 @@ using System.ComponentModel.DataAnnotations; +using Persistence.Models.Enumerations; namespace Persistence.Models { @@ -24,29 +25,16 @@ namespace Persistence.Models /// public DateTimeOffset Timestamp { get; set; } - /// - /// Глубина забоя - /// - [Range(0, double.MaxValue, ErrorMessage = "Глубина забоя не может быть меньше 0")] - public double? Depth { get; set; } - /// /// Текст сообщения /// [Required] [StringLength(512, MinimumLength = 1, ErrorMessage = "Допустимая длина текста сообщения от 1 до 512 символов")] - public required string MessageText { get; set; } + public required string Text { get; set; } /// - /// Система автобурения, к которой относится сообщение + /// Статус события /// - [Required] - [StringLength(256, MinimumLength = 1, ErrorMessage = "Допустимая длина наименования системы АБ от 1 до 256 символов")] - public required string System { get; set; } - - /// - /// Id пользователя за пультом бурильщика - /// - public Guid UserId { get; set; } + public EventState EventState { get; set; } } } diff --git a/Persistence/Repositories/ITechMessagesRepository.cs b/Persistence/Repositories/ITechMessagesRepository.cs index ae92912..c442e94 100644 --- a/Persistence/Repositories/ITechMessagesRepository.cs +++ b/Persistence/Repositories/ITechMessagesRepository.cs @@ -22,14 +22,14 @@ namespace Persistence.Repositories /// /// /// - Task AddRange(IEnumerable dtos, Guid userId, CancellationToken token); + Task AddRange(Guid systemId, IEnumerable dtos, Guid userId, CancellationToken token); /// - /// Получение списка уникальных названий систем АБ + /// Получение списка систем /// /// /// - Task> GetSystems(CancellationToken token); + Task> GetSystems(CancellationToken token); /// /// Получение количества сообщений по категориям и системам автобурения @@ -38,7 +38,7 @@ namespace Persistence.Repositories /// Система автобурения /// /// - Task> GetStatistics(IEnumerable autoDrillingSystem, IEnumerable categoryIds, CancellationToken token); + Task> GetStatistics(IEnumerable autoDrillingSystem, IEnumerable categoryIds, CancellationToken token); /// /// Получить порцию записей, начиная с заданной даты