using AsbCloudApp.Data; using AsbCloudApp.Repositories; using AsbCloudApp.Services; using AsbCloudInfrastructure.Services.Trajectory; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; using System.IO; using System.Threading; using System.Threading.Tasks; namespace AsbCloudWebApi.Controllers.Trajectory { /// /// Плановые и фактические траектории (загрузка и хранение) /// [ApiController] [Authorize] public abstract class TrajectoryController : ControllerBase where Tdto : TrajectoryGeoDto { protected string fileName; private readonly IWellService wellService; private readonly TrajectoryImportService trajectoryImportService; private readonly ITrajectoryEditableRepository trajectoryRepository; public TrajectoryController(IWellService wellService, TrajectoryImportService trajectoryImportService, ITrajectoryEditableRepository trajectoryRepository) { this.trajectoryImportService = trajectoryImportService; this.wellService = wellService; this.trajectoryRepository = trajectoryRepository; } /// /// Возвращает excel шаблон для заполнения строк траектории /// /// Запрашиваемый файл [HttpGet("template")] [AllowAnonymous] [ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK, "application/octet-stream")] [ProducesResponseType(StatusCodes.Status204NoContent)] public IActionResult GetTemplate() { var stream = trajectoryImportService.GetTemplateFile(); return File(stream, "application/octet-stream", fileName); } /// /// Формируем excel файл с текущими строками траектории /// /// id скважины /// Токен отмены задачи /// Запрашиваемый файл [HttpGet("export")] [ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK, "application/octet-stream")] [ProducesResponseType(StatusCodes.Status204NoContent)] public async Task ExportAsync([FromRoute] int idWell, CancellationToken token) { if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); var stream = await trajectoryImportService.ExportAsync(idWell, token); var fileName = await trajectoryImportService.GetFileNameAsync(idWell, token); return File(stream, "application/octet-stream", fileName); } /// /// Импортирует координаты из excel (xlsx) файла /// /// id скважины /// Коллекция из одного файла xlsx /// Удалить операции перед импортом, если фал валидный /// Токен отмены задачи /// количество успешно записанных строк в БД [HttpPost("import/{deleteBeforeImport}")] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] [ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)] public async Task ImportAsync(int idWell, [FromForm] IFormFileCollection files, bool deleteBeforeImport, CancellationToken token) { int? idUser = User.GetUserId(); if (!idUser.HasValue) return Forbid(); if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); if (files.Count < 1) return this.ValidationBadRequest(nameof(files), "нет файла"); var file = files[0]; if (Path.GetExtension(file.FileName).ToLower() != ".xlsx") return this.ValidationBadRequest(nameof(files), "Требуется xlsx файл."); using Stream stream = file.OpenReadStream(); try { var trajectoryRows = await trajectoryImportService.ImportAsync(idWell, idUser.Value, stream, token); if (deleteBeforeImport) await trajectoryRepository.DeleteByIdWellAsync(idWell, token); var rowsCount = await trajectoryRepository.AddRangeAsync(trajectoryRows, token); return Ok(rowsCount); } catch (FileFormatException ex) { return this.ValidationBadRequest(nameof(files), ex.Message); } } /// /// Получаем список всех строк координат траектории (для клиента) /// /// id скважины /// Токен отмены задачи /// Список добавленных координат траектории [HttpGet] //[ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] public virtual async Task GetAsync([FromRoute] int idWell, CancellationToken token) { if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); var result = await trajectoryRepository.GetAsync(idWell, token); return Ok(result); } /// /// Добавить одну новую строчку координат для плановой траектории /// /// /// /// /// количество успешно записанных строк в БД [HttpPost] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] public async Task AddAsync(int idWell, [FromBody] Tdto row, CancellationToken token) { if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); var idUser = User.GetUserId(); if (!idUser.HasValue) return Forbid(); row.IdUser = idUser.Value; row.IdWell = idWell; var result = await trajectoryRepository.AddAsync(row, token); return Ok(result); } /// /// Добавить массив строчек координат для плановой траектории /// /// /// /// /// количество успешно записанных строк в БД [HttpPost("range")] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] public async Task AddRangeAsync(int idWell, [FromBody] IEnumerable rows, CancellationToken token) { if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); int? idUser = User.GetUserId(); if (!idUser.HasValue) return Forbid(); foreach (var item in rows) { item.IdUser = idUser.Value; item.IdWell = idWell; } var result = await trajectoryRepository.AddRangeAsync(rows, token); return Ok(result); } /// /// Изменить выбранную строку с координатами /// /// /// /// /// /// количество успешно обновленных строк в БД [HttpPut("{idRow}")] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] public async Task UpdateAsync(int idWell, int idRow, [FromBody] Tdto row, CancellationToken token) { if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); int? idUser = User.GetUserId(); if (!idUser.HasValue) return Forbid(); row.Id = idRow; row.IdUser = idUser.Value; row.IdWell = idWell; var result = await trajectoryRepository.UpdateAsync(row, token); return Ok(result); } /// /// Удалить выбранную строку с координатами /// /// /// /// /// количество успешно удаленных строк из БД [HttpDelete("{idRow}")] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] public async Task DeleteAsync(int idWell, int idRow, CancellationToken token) { if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); var result = await trajectoryRepository.DeleteRangeAsync(new int[] { idRow }, token); return Ok(result); } protected async Task CanUserAccessToWellAsync(int idWell, CancellationToken token) { int? idCompany = User.GetCompanyId(); return idCompany is not null && await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, idWell, token).ConfigureAwait(false); } } }