From 153f5894adcda5792854a26ec179aa35a5fc6eee Mon Sep 17 00:00:00 2001 From: Roman Efremov Date: Mon, 25 Nov 2024 13:49:07 +0500 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D1=82=D1=8C=20=D1=85=D1=80=D0=B0=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D1=82=D0=B5=D1=85=D0=BD=D0=BE=D0=BB=D0=BE=D0=B3?= =?UTF-8?q?=D0=B8=D1=87=D0=B5=D1=81=D0=BA=D0=B8=D1=85=20=D1=81=D0=BE=D0=BE?= =?UTF-8?q?=D0=B1=D1=89=D0=B5=D0=BD=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/TechMessagesController.cs | 44 ++++++++++++++++ .../PersistenceDbContext.cs | 5 +- Persistence.Database/Entity/TechMessage.cs | 30 +++++++++++ .../Model/IPersistenceDbContext.cs | 2 + .../Repositories/TechMessagesRepository.cs | 51 +++++++++++++++++++ Persistence/API/ITechMessages.cs | 35 +++++++++++++ Persistence/Models/TechMessageDto.cs | 40 +++++++++++++++ .../Repositories/ITechMessagesRepository.cs | 34 +++++++++++++ 8 files changed, 240 insertions(+), 1 deletion(-) create mode 100644 Persistence.API/Controllers/TechMessagesController.cs create mode 100644 Persistence.Database/Entity/TechMessage.cs create mode 100644 Persistence.Repository/Repositories/TechMessagesRepository.cs create mode 100644 Persistence/API/ITechMessages.cs create mode 100644 Persistence/Models/TechMessageDto.cs create mode 100644 Persistence/Repositories/ITechMessagesRepository.cs diff --git a/Persistence.API/Controllers/TechMessagesController.cs b/Persistence.API/Controllers/TechMessagesController.cs new file mode 100644 index 0000000..0eb183d --- /dev/null +++ b/Persistence.API/Controllers/TechMessagesController.cs @@ -0,0 +1,44 @@ +using Microsoft.AspNetCore.Mvc; +using Persistence.Models; +using Persistence.Repositories; + +namespace Persistence.API.Controllers +{ + [ApiController] + [Route("api/[controller]")] + public class TechMessagesController : ControllerBase, ITechMessages + { + private readonly ITechMessagesRepository techMessagesRepository; + + public TechMessagesController(ITechMessagesRepository techMessagesRepository) + { + this.techMessagesRepository = techMessagesRepository; + } + + public Task>> GetPage(RequestDto request, CancellationToken token) + { + throw new NotImplementedException(); + } + + public async Task> GetStatistics(int importantId, string autoDrillingSystem, CancellationToken token) + { + var result = await techMessagesRepository.GetStatistics(importantId, autoDrillingSystem, token); + + return Ok(result); + } + + public async Task>> GetSystems(CancellationToken token) + { + var result = await techMessagesRepository.GetSystems(token); + + return Ok(result); + } + + public async Task> InsertRange(IEnumerable dtos, CancellationToken token) + { + var result = await techMessagesRepository.InsertRange(dtos, token); + + return Ok(result); + } + } +} diff --git a/Persistence.Database.Postgres/PersistenceDbContext.cs b/Persistence.Database.Postgres/PersistenceDbContext.cs index 58d201c..75fd28a 100644 --- a/Persistence.Database.Postgres/PersistenceDbContext.cs +++ b/Persistence.Database.Postgres/PersistenceDbContext.cs @@ -1,4 +1,5 @@ using Microsoft.EntityFrameworkCore; +using Persistence.Database.Entity; using System.Data.Common; namespace Persistence.Database.Model; @@ -8,7 +9,9 @@ public partial class PersistenceDbContext : DbContext, IPersistenceDbContext public DbSet Setpoint => Set(); - public PersistenceDbContext() + public DbSet TechMessage => Set(); + + public PersistenceDbContext() : base() { diff --git a/Persistence.Database/Entity/TechMessage.cs b/Persistence.Database/Entity/TechMessage.cs new file mode 100644 index 0000000..b4135c1 --- /dev/null +++ b/Persistence.Database/Entity/TechMessage.cs @@ -0,0 +1,30 @@ +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using Microsoft.EntityFrameworkCore; + +namespace Persistence.Database.Entity +{ + public class TechMessage + { + [Key, Comment("Id события")] + public Guid EventId { get; set; } + + [Comment("Id Категории важности")] + public int ImportantId { get; set; } + + [Comment("Дата возникновения")] + public DateTimeOffset OccurrenceDate { get; set; } + + [Comment("Глубина забоя")] + public double? Depth { get; set; } + + [Column(TypeName = "varchar(512)"), Comment("Текст сообщения")] + public string? MessageText { get; set; } + + [Column(TypeName = "varchar(256)"), Comment("Система автобурения, к которой относится сообщение")] + public string? AutoDrillingSystem { get; set; } + + [Comment("Id пользователя за пультом бурильщика")] + public Guid UserId { get; set; } + } +} diff --git a/Persistence.Database/Model/IPersistenceDbContext.cs b/Persistence.Database/Model/IPersistenceDbContext.cs index 2c1aebb..759c4a2 100644 --- a/Persistence.Database/Model/IPersistenceDbContext.cs +++ b/Persistence.Database/Model/IPersistenceDbContext.cs @@ -1,5 +1,6 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; +using Persistence.Database.Entity; using System; using System.Collections.Generic; using System.Diagnostics.Metrics; @@ -12,6 +13,7 @@ public interface IPersistenceDbContext : IDisposable { DbSet DataSaub { get; } DbSet Setpoint { get; } + DbSet TechMessage { get; } DatabaseFacade Database { get; } Task SaveChangesAsync(CancellationToken cancellationToken); } diff --git a/Persistence.Repository/Repositories/TechMessagesRepository.cs b/Persistence.Repository/Repositories/TechMessagesRepository.cs new file mode 100644 index 0000000..e7a37bc --- /dev/null +++ b/Persistence.Repository/Repositories/TechMessagesRepository.cs @@ -0,0 +1,51 @@ +using Mapster; +using Microsoft.EntityFrameworkCore; +using Persistence.Database.Entity; +using Persistence.Models; +using Persistence.Repositories; + +namespace Persistence.Repository.Repositories +{ + public class TechMessagesRepository : ITechMessagesRepository + { + private DbContext db; + public TechMessagesRepository(DbContext db) + { + this.db = db; + } + + protected virtual IQueryable GetQueryReadOnly() => db.Set(); + + public async Task GetStatistics(int importantId, string autoDrillingSystem, CancellationToken token) + { + var query = GetQueryReadOnly(); + var count = await query + .Where(e => e.ImportantId == importantId && e.AutoDrillingSystem == autoDrillingSystem) + .CountAsync(); + + return count; + } + + public async Task> GetSystems(CancellationToken token) + { + var query = GetQueryReadOnly(); + var entities = await query + .Select(e => e.AutoDrillingSystem ?? string.Empty) + .Distinct() + .ToArrayAsync(token); + var dtos = entities.Order(); + + return dtos; + } + + public async Task InsertRange(IEnumerable dtos, CancellationToken token) + { + var entities = dtos.Select(d => d.Adapt()); + + await db.Set().AddRangeAsync(entities, token); + var result = await db.SaveChangesAsync(token); + + return result; + } + } +} diff --git a/Persistence/API/ITechMessages.cs b/Persistence/API/ITechMessages.cs new file mode 100644 index 0000000..95af161 --- /dev/null +++ b/Persistence/API/ITechMessages.cs @@ -0,0 +1,35 @@ +using Microsoft.AspNetCore.Mvc; +using Persistence.Models; + +namespace Persistence.API +{ + /// + /// Интерфейс для API сообщений о состояниях работы систем автобурения (АБ) + /// + public interface ITechMessages : ITableDataApi + { + /// + /// Добавление новых сообщений + /// + /// + /// + /// + Task> InsertRange(IEnumerable dtos, CancellationToken token); + + /// + /// Получение списка систем АБ + /// + /// + /// + Task>> GetSystems(CancellationToken token); + + /// + /// Получение статистики + /// + /// Id Категории важности + /// Система АБ + /// + /// + Task> GetStatistics(int importantId, string autoDrillingSystem, CancellationToken token); + } +} diff --git a/Persistence/Models/TechMessageDto.cs b/Persistence/Models/TechMessageDto.cs new file mode 100644 index 0000000..274a1d5 --- /dev/null +++ b/Persistence/Models/TechMessageDto.cs @@ -0,0 +1,40 @@ +namespace Persistence.Models +{ + public class TechMessageDto + { + /// + /// Id события + /// + public Guid EventId { get; set; } + + /// + /// Id Категории важности + /// + public int ImportantId { get; set; } + + /// + /// Дата возникновения + /// + public DateTimeOffset OccurrenceDate { get; set; } + + /// + /// Глубина забоя + /// + public double? Depth { get; set; } + + /// + /// Текст сообщения + /// + public string? MessageText { get; set; } + + /// + /// Система автобурения, к которой относится сообщение + /// + public string? AutoDrillingSystem { get; set; } + + /// + /// Id пользователя за пультом бурильщика + /// + public Guid UserId { get; set; } + } +} diff --git a/Persistence/Repositories/ITechMessagesRepository.cs b/Persistence/Repositories/ITechMessagesRepository.cs new file mode 100644 index 0000000..c681184 --- /dev/null +++ b/Persistence/Repositories/ITechMessagesRepository.cs @@ -0,0 +1,34 @@ +using Persistence.Models; + +namespace Persistence.Repositories +{ + /// + /// Интерфейс по работе с технологическими сообщениями + /// + public interface ITechMessagesRepository + { + /// + /// Добавление новых сообщений + /// + /// + /// + /// + Task InsertRange(IEnumerable dtos, CancellationToken token); + + /// + /// Получение списка уникальных названий систем АБ + /// + /// + /// + Task> GetSystems(CancellationToken token); + + /// + /// Получение количества сообщений по категориям и системам автобурения + /// + /// Id Категории важности + /// Система автобурения + /// + /// + Task GetStatistics(int importantId, string autoDrillingSystem, CancellationToken token); + } +}