From 07820a5042a9ef5e500b664a2ca1f682247bda9b Mon Sep 17 00:00:00 2001 From: cult Date: Fri, 29 Oct 2021 13:34:56 +0500 Subject: [PATCH] Deleted Apache Service for Excel files union --- .../Services/IDrillingProgramApacheService.cs | 11 --- AsbCloudInfrastructure/DependencyInjection.cs | 1 - .../Services/DrillingProgramApacheService.cs | 92 ------------------- 3 files changed, 104 deletions(-) delete mode 100644 AsbCloudApp/Services/IDrillingProgramApacheService.cs delete mode 100644 AsbCloudInfrastructure/Services/DrillingProgramApacheService.cs diff --git a/AsbCloudApp/Services/IDrillingProgramApacheService.cs b/AsbCloudApp/Services/IDrillingProgramApacheService.cs deleted file mode 100644 index 4f42dfbd..00000000 --- a/AsbCloudApp/Services/IDrillingProgramApacheService.cs +++ /dev/null @@ -1,11 +0,0 @@ -using AsbCloudApp.Data; -using System.Threading; -using System.Threading.Tasks; - -namespace AsbCloudApp.Services -{ - public interface IDrillingProgramApacheService - { - Task GetAsync(int idWell, CancellationToken token = default); - } -} \ No newline at end of file diff --git a/AsbCloudInfrastructure/DependencyInjection.cs b/AsbCloudInfrastructure/DependencyInjection.cs index 2aa69a58..373cf661 100644 --- a/AsbCloudInfrastructure/DependencyInjection.cs +++ b/AsbCloudInfrastructure/DependencyInjection.cs @@ -43,7 +43,6 @@ namespace AsbCloudInfrastructure services.AddTransient(); services.AddTransient(); services.AddTransient(); - services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); diff --git a/AsbCloudInfrastructure/Services/DrillingProgramApacheService.cs b/AsbCloudInfrastructure/Services/DrillingProgramApacheService.cs deleted file mode 100644 index 1f0cd0d5..00000000 --- a/AsbCloudInfrastructure/Services/DrillingProgramApacheService.cs +++ /dev/null @@ -1,92 +0,0 @@ -using AsbCloudApp.Data; -using AsbCloudApp.Services; -using NPOI.SS.UserModel; -using NPOI.XSSF.UserModel; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; - -namespace AsbCloudInfrastructure.Services -{ - public class DrillingProgramApacheService : IDrillingProgramApacheService - { - private readonly IFileService fileService; - private readonly IWellService wellService; - private const int idFileCategoryDrillingProgramItems = 13; - private const int idFileCategoryDrillingProgram = 14; - - public DrillingProgramApacheService(IFileService fileService, IWellService wellService) - { - this.fileService = fileService; - this.wellService = wellService; - } - - public async Task GetAsync(int idWell, CancellationToken token = default) - { - var filesInfos = await fileService.GetInfosByCategoryAsync(idWell, idFileCategoryDrillingProgramItems, token) - .ConfigureAwait(false); - - var well = await wellService.GetAsync(idWell, token) - .ConfigureAwait(false); - - var resultFileName = $"Программа бурения {well.Cluster} {well.Caption}.xlsx"; - - var matchFiles = await fileService.GetInfosByCategoryAsync(idWell, idFileCategoryDrillingProgram, token) - .ConfigureAwait(false); - - if (matchFiles is not null && matchFiles.Any()) - { - matchFiles = matchFiles.OrderByDescending(f => f.UploadDate); - var matchFilesIterator = matchFiles.GetEnumerator(); - matchFilesIterator.MoveNext(); - var matchFile = matchFilesIterator.Current; - while (matchFilesIterator.MoveNext()) - await fileService.DeleteAsync(matchFilesIterator.Current.Id, token) - .ConfigureAwait(false); - - if (filesInfos.All(f => f.UploadDate <= matchFile.UploadDate)) - return matchFile; - else - await fileService.DeleteAsync(matchFile.Id, token) - .ConfigureAwait(false); - } - - var fileNames = filesInfos - .Where(f => f.Name != resultFileName) - .Select(f => fileService.GetUrl(f)); - - var stream = new MemoryStream(1024 * 1024); - UniteExcelFiles(fileNames, stream); - var buffer = stream.ToArray(); - var fileStream = new MemoryStream(buffer); - return await fileService.SaveAsync(idWell, null, idFileCategoryDrillingProgram, resultFileName, fileStream, token) - .ConfigureAwait(false); - } - - private static void UniteExcelFiles(IEnumerable excelFilesNames, Stream stream) - { - IWorkbook product = new XSSFWorkbook(); - - foreach (var excelFileName in excelFilesNames) - { - IWorkbook book = new XSSFWorkbook(new FileStream(excelFileName, FileMode.Open)); - - for (int i = 0; i < book.NumberOfSheets; i++) - { - ISheet sheet = book.GetSheetAt(i); - try - { - sheet.CopyTo(product, sheet.SheetName, true, true); - } - catch - { - //what can't be done - can't be done. ignore it. - } - } - } - product.Write(stream); - } - } -} \ No newline at end of file