diff --git a/AsbCloudApp/Data/SAUB/TelemetryInfoDto.cs b/AsbCloudApp/Data/SAUB/TelemetryInfoDto.cs index 732fca23..13b60fdb 100644 --- a/AsbCloudApp/Data/SAUB/TelemetryInfoDto.cs +++ b/AsbCloudApp/Data/SAUB/TelemetryInfoDto.cs @@ -66,4 +66,4 @@ public class TelemetryInfoDto /// комментарий /// public string? Comment { get; set; } -} +} \ No newline at end of file diff --git a/AsbCloudApp/Data/SAUB/VersionDto.cs b/AsbCloudApp/Data/SAUB/VersionDto.cs new file mode 100644 index 00000000..581640d3 --- /dev/null +++ b/AsbCloudApp/Data/SAUB/VersionDto.cs @@ -0,0 +1,54 @@ +using System; + +namespace AsbCloudApp.Data.SAUB; + +/// +/// Версия ПО +/// +public class VersionDto +{ + /// + /// Идентификатор скважины + /// + public int IdWell { get; set; } + + /// + /// название скважины + /// + public string Well { get; set; } = string.Empty; + + /// + /// название куста + /// + public string Cluster { get; set; } = string.Empty; + + /// + /// название месторождения + /// + public string Deposit { get; set; } = string.Empty; + + /// + /// название заказчика + /// + public string? Customer { get; set; } + + /// + /// версия ПО панели оператора + /// + public string? HmiVersion { get; set; } + + /// + /// версия ПО ПЛК САУБ + /// + public string? SaubPlcVersion { get; set; } + + /// + /// версия ПО ПЛК Спин мастер + /// + public string? SpinPlcVersion { get; set; } + + /// + /// версия ПО ПЛК Памп мастер + /// + public string? PumpPlcVersion { get; set; } +} \ No newline at end of file diff --git a/AsbCloudApp/Requests/TelemetryInfoRequest.cs b/AsbCloudApp/Requests/TelemetryInfoRequest.cs deleted file mode 100644 index fb6f68d4..00000000 --- a/AsbCloudApp/Requests/TelemetryInfoRequest.cs +++ /dev/null @@ -1,20 +0,0 @@ -namespace AsbCloudApp.Requests; - -/// -/// Запрос получения информации по телеметрии -/// -public class TelemetryInfoRequest -{ - /// - /// Идентификатор компании - /// - public int IdCompany { get; set; } - - /// - /// Определяет состояние скважины - /// 0 - неизвестно, - /// 1 - в работе, - /// 2 - завершена - /// - public int IdWellState { get; set; } -} \ No newline at end of file diff --git a/AsbCloudApp/Requests/VersionRequest.cs b/AsbCloudApp/Requests/VersionRequest.cs new file mode 100644 index 00000000..5264543e --- /dev/null +++ b/AsbCloudApp/Requests/VersionRequest.cs @@ -0,0 +1,61 @@ +using System.Collections.Generic; + +namespace AsbCloudApp.Requests; + +/// +/// Запрос получения версий ПО +/// +public class VersionRequestBase +{ + /// + /// Конструктор по умолчанию + /// + public VersionRequestBase() + { + } + + /// + /// Копирующий конструктор + /// + /// + public VersionRequestBase(VersionRequestBase request) + { + IdWellState = request.IdWellState; + IdsWell = request.IdsWell; + } + + /// + /// Необязательный параметр. Определяет состояние скважины + /// null - возвращаются все записи + /// 0 - неизвестно, + /// 1 - в работе, + /// 2 - завершена + /// + public int? IdWellState { get; set; } + + /// + /// Идентификаторы скважин + /// + public IEnumerable? IdsWell { get; set; } +} + +/// +/// Запрос получения версий ПО +/// +public class VersionRequest : VersionRequestBase +{ + /// + /// Конструктор + /// + public VersionRequest(int idCompany, + VersionRequestBase request) + : base(request) + { + IdCompany = idCompany; + } + + /// + /// Идентификатор компании + /// + public int IdCompany { get; set; } +} \ No newline at end of file diff --git a/AsbCloudApp/Services/ITelemetryService.cs b/AsbCloudApp/Services/ITelemetryService.cs index 9ebdb36e..c1b4ba99 100644 --- a/AsbCloudApp/Services/ITelemetryService.cs +++ b/AsbCloudApp/Services/ITelemetryService.cs @@ -64,12 +64,12 @@ public interface ITelemetryService Task UpdateInfoAsync(string uid, TelemetryInfoDto info, CancellationToken token); /// - /// Получить данные о телеметрии + /// Получить версии ПО /// /// /// /// - Task> GetInfoAsync(TelemetryInfoRequest request, CancellationToken token); + Task> GetVersionsAsync(VersionRequest request, CancellationToken token); /// /// Слить данные телеметрии в одну diff --git a/AsbCloudInfrastructure/Services/SAUB/TelemetryService.cs b/AsbCloudInfrastructure/Services/SAUB/TelemetryService.cs index d67f3a18..6de56bdc 100644 --- a/AsbCloudInfrastructure/Services/SAUB/TelemetryService.cs +++ b/AsbCloudInfrastructure/Services/SAUB/TelemetryService.cs @@ -78,43 +78,53 @@ public class TelemetryService : ITelemetryService DropTelemetryCache(); } - public async Task> GetInfoAsync(TelemetryInfoRequest request, CancellationToken token) + public async Task> GetVersionsAsync(VersionRequest request, CancellationToken token) { - var idTelemetries = await db.Set() + var query = db.Set() .Include(x => x.RelationCompaniesWells) - .Where(x => x.RelationCompaniesWells.Any(y => y.IdCompany == request.IdCompany) && - x.IdState == request.IdWellState) - .Where(x => x.IdTelemetry.HasValue) - .Select(x => x.IdTelemetry!.Value) - .ToArrayAsync(token); + .ThenInclude(x => x.Company) + .Include(x => x.Cluster) + .ThenInclude(c => c.Deposit) + .Where(x => x.RelationCompaniesWells.Any(y => y.IdCompany == request.IdCompany)); - var key = $"TelemetryInfo_{string.Join("", idTelemetries)}"; + if (request.IdWellState.HasValue) + query = query.Where(x => x.IdState == request.IdWellState); - var result = await memoryCache.GetOrCreateAsync(key, async entry => - { - entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(10); + if (request.IdsWell?.Any() == true) + query = query.Where(x => request.IdsWell.Contains(x.Id)); - var query = db.Set() - .Where(x => idTelemetries.Contains(x.Id)) - .OrderBy(x => x.Info.DrillingStartDate); + var wells = await query.Where(x => x.IdTelemetry.HasValue) + .ToDictionaryAsync(x => x.Id, x => x, token); - var entities = await query - .AsNoTracking() - .ToArrayAsync(token); + var idTelemetries = wells.Select(x => x.Value.IdTelemetry!.Value); - var dtos = entities.Select(x => + var telemetries = GetTelemetryCache() + .Where(x => idTelemetries.Contains(x.Id)) + .OrderBy(x => x.Info.DrillingStartDate); + + var dtos = telemetries.Select(x => + { + var well = wells[x.Well!.Id]; + + var dto = new VersionDto { - var dto = x.Info.Adapt(); - dto.DrillingStartDate = x.Info.DrillingStartDate.ToRemoteDateTime(dto.TimeZoneOffsetTotalHours); - return dto; - }); + IdWell = well.Id, + Well = well.Caption, + Cluster = well.Cluster.Caption, + Deposit = well.Cluster.Deposit.Caption, + Customer = well.RelationCompaniesWells.Select(r => r.Company).FirstOrDefault(c => c.IdCompanyType == 1)?.Caption, + HmiVersion = x.Info.HmiVersion, + SaubPlcVersion = x.Info.SaubPlcVersion, + SpinPlcVersion = x.Info.SpinPlcVersion, + }; - return dtos; + return dto; }); - return result!; + return dtos; } + [Obsolete("This method will be private. Use TelemetryDto.TimeZone prop.")] public SimpleTimezoneDto GetTimezone(int idTelemetry) { diff --git a/AsbCloudWebApi/Controllers/TelemetryInfoController.cs b/AsbCloudWebApi/Controllers/TelemetryInfoController.cs deleted file mode 100644 index b7890f18..00000000 --- a/AsbCloudWebApi/Controllers/TelemetryInfoController.cs +++ /dev/null @@ -1,57 +0,0 @@ -using System.Collections.Generic; -using System.Threading; -using System.Threading.Tasks; -using AsbCloudApp.Data.SAUB; -using AsbCloudApp.Requests; -using AsbCloudApp.Services; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; - -namespace AsbCloudWebApi.Controllers; - -/// -/// Версии прошивок -/// -[Route("api/[controller]")] -[ApiController] -[Authorize] -public class TelemetryInfoController : ControllerBase -{ - private readonly ITelemetryService telemetryService; - - public TelemetryInfoController(ITelemetryService telemetryService) - { - this.telemetryService = telemetryService; - } - - /// - /// Получить список версий прошивок - /// - /// Определяет состояние скважины - /// 0 - неизвестно, - /// 1 - в работе, - /// 2 - завершена - /// - /// - [HttpGet] - [Permission] - [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] - public async Task GetAsync([FromQuery] int idWellState, CancellationToken token) - { - var idCompany = User.GetCompanyId(); - - if (!idCompany.HasValue) - return Forbid(); - - var requestToService = new TelemetryInfoRequest - { - IdCompany = idCompany.Value, - IdWellState = idWellState - }; - - var telemetriesInfo = await telemetryService.GetInfoAsync(requestToService, token); - - return Ok(telemetriesInfo); - } -} \ No newline at end of file diff --git a/AsbCloudWebApi/Controllers/VersionController.cs b/AsbCloudWebApi/Controllers/VersionController.cs new file mode 100644 index 00000000..c62b53ee --- /dev/null +++ b/AsbCloudWebApi/Controllers/VersionController.cs @@ -0,0 +1,50 @@ +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using AsbCloudApp.Data.SAUB; +using AsbCloudApp.Requests; +using AsbCloudApp.Services; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; + +namespace AsbCloudWebApi.Controllers; + +/// +/// Версии ПО +/// +[Route("api/[controller]")] +[ApiController] +[Authorize] +public class VersionController : ControllerBase +{ + private readonly ITelemetryService telemetryService; + + public VersionController(ITelemetryService telemetryService) + { + this.telemetryService = telemetryService; + } + + /// + /// Получить список версий ПО + /// + /// + /// + /// + [HttpGet] + [Permission] + [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] + public async Task GetAsync([FromQuery] VersionRequestBase request, CancellationToken token) + { + var idCompany = User.GetCompanyId(); + + if (!idCompany.HasValue) + return Forbid(); + + var requestToService = new VersionRequest(idCompany.Value, request); + + var version = await telemetryService.GetVersionsAsync(requestToService, token); + + return Ok(version); + } +} \ No newline at end of file