diff --git a/AsbCloudApp/Services/IWellOperationImportService.cs b/AsbCloudApp/Services/IWellOperationImportService.cs index 7ef96ab3..e0f2e39d 100644 --- a/AsbCloudApp/Services/IWellOperationImportService.cs +++ b/AsbCloudApp/Services/IWellOperationImportService.cs @@ -5,6 +5,7 @@ namespace AsbCloudApp.Services public interface IWellOperationImportService { Stream Export(int idWell); + Stream GetExcelTemplateStream(); void Import(int idWell, Stream stream, bool deleteWellOperationsBeforeImport = false); } } \ No newline at end of file diff --git a/AsbCloudInfrastructure/Services/WellOperationService/WellOperationImportService.cs b/AsbCloudInfrastructure/Services/WellOperationService/WellOperationImportService.cs index 87f76289..f3133814 100644 --- a/AsbCloudInfrastructure/Services/WellOperationService/WellOperationImportService.cs +++ b/AsbCloudInfrastructure/Services/WellOperationService/WellOperationImportService.cs @@ -90,10 +90,15 @@ namespace AsbCloudInfrastructure.Services.WellOperationService return MakeExelFileStream(operations); } + public Stream GetExcelTemplateStream() { + var stream = System.Reflection.Assembly.GetExecutingAssembly() + .GetManifestResourceStream("AsbCloudInfrastructure.Services.WellOperationService.WellOperationImportTemplate.xltx"); + return stream; + } + private Stream MakeExelFileStream(IEnumerable operations) { - using Stream ecxelTemplateStream = System.Reflection.Assembly.GetExecutingAssembly() - .GetManifestResourceStream("AsbCloudInfrastructure.Services.WellOperationService.WellOperationImportTemplate.xltx"); + using Stream ecxelTemplateStream = GetExcelTemplateStream(); using var workbook = new XLWorkbook(ecxelTemplateStream, XLEventTracking.Disabled); AddOperationsToWorkbook(workbook, operations); @@ -225,12 +230,14 @@ namespace AsbCloudInfrastructure.Services.WellOperationService } }; - // проверка диапазона дат - if (operations.Min(o => o.DateStart) - operations.Max(o => o.DateStart) > drillingDurationLimitMax) - parseErrors.Add($"Лист {sheet.Name} содержит диапазон дат больше {drillingDurationLimitMax}"); - if (parseErrors.Any()) throw new FileFormatException(string.Join("\r\n", parseErrors)); + else + { + if (operations.Any()) + if (operations.Min(o => o.DateStart) - operations.Max(o => o.DateStart) > drillingDurationLimitMax) + parseErrors.Add($"Лист {sheet.Name} содержит диапазон дат больше {drillingDurationLimitMax}"); + } return operations; } diff --git a/AsbCloudWebApi/Controllers/WellOperationController.cs b/AsbCloudWebApi/Controllers/WellOperationController.cs index 9c218049..1a9d4ec9 100644 --- a/AsbCloudWebApi/Controllers/WellOperationController.cs +++ b/AsbCloudWebApi/Controllers/WellOperationController.cs @@ -179,16 +179,15 @@ namespace AsbCloudWebApi.Controllers /// Импортирует операции из excel (xlsx) файла /// /// id скважины - /// Коллекция файлов - 1 файл xlsx - /// Удалить операции перед импортом, если фал валидный + /// Коллекция из одного файла xlsx + /// Удалить операции перед импортом = 1, если фал валидный /// Токен отмены задачи /// [HttpPost] - [Route("import")] - [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] - public async Task ImportAsync(int idWell, + [Route("import/{options}")] + public async Task ImportAsync(int idWell, [FromForm] IFormFileCollection files, - bool deleteWellOperationsBeforeImport = false, + int options = 0, CancellationToken token = default) { int? idCompany = User.GetCompanyId(); @@ -205,13 +204,13 @@ namespace AsbCloudWebApi.Controllers return BadRequest("нет файла"); var file = files[0]; - if(Path.GetExtension( file.FileName).ToLower() != "*.xlsx") + if(Path.GetExtension( file.FileName).ToLower() != ".xlsx") return BadRequest("Требуется xlsx файл."); using Stream stream = file.OpenReadStream(); try { - wellOperationImportService.Import(idWell, stream, deleteWellOperationsBeforeImport); + wellOperationImportService.Import(idWell, stream, (options & 1) > 0); } catch(FileFormatException ex) { @@ -222,7 +221,7 @@ namespace AsbCloudWebApi.Controllers } /// - /// Возвращает файл с диска на сервере + /// Создает excel файл с операциями по скважине /// /// id скважины /// Токен отмены задачи @@ -246,6 +245,22 @@ namespace AsbCloudWebApi.Controllers return File(stream, "application/octet-stream", fileName); } + /// + /// Возвращает шаблон файла импорта + /// + /// Токен отмены задачи + /// Запрашиваемый файл + [HttpGet] + [Route("tamplate")] + [AllowAnonymous] + [ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK)] + public IActionResult GetTamplate() + { + var stream = wellOperationImportService.GetExcelTemplateStream(); + var fileName = "ЕЦП_шаблон_файла_операций.xlsx"; + return File(stream, "application/octet-stream", fileName); + } + private async Task CanUserAccessToWellAsync(int idWell, CancellationToken token = default) { int? idCompany = User.GetCompanyId();