DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/DrillingProgramService.cs

94 lines
3.6 KiB
C#
Raw Normal View History

2021-08-29 17:25:16 +05:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Data;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using Microsoft.EntityFrameworkCore;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
namespace AsbCloudInfrastructure.Services
{
public class DrillingProgramService : IDrillingProgramService
{
private readonly IFileService fileService;
2021-08-29 17:27:24 +05:00
private readonly IWellService wellService;
2021-08-31 09:52:32 +05:00
private const int idFileCategoryDrillingProgramItems = 13;
private const int idFileCategoryDrillingProgram = 14;
2021-08-29 17:25:16 +05:00
2021-08-29 17:27:24 +05:00
public DrillingProgramService(IFileService fileService, IWellService wellService)
2021-08-29 17:25:16 +05:00
{
this.fileService = fileService;
this.wellService = wellService;
}
public async Task<FileInfoDto> GetAsync(int idWell, CancellationToken token = default)
{
2021-08-31 09:52:32 +05:00
var filesInfos = await fileService.GetInfosByCategoryAsync(idWell, idFileCategoryDrillingProgramItems, token)
2021-08-29 17:25:16 +05:00
.ConfigureAwait(false);
var well = await wellService.GetAsync(idWell, token)
.ConfigureAwait(false);
var resultFileName = $"Программа бурения {well.Cluster} {well.Caption}.xlsx";
2021-08-31 09:52:32 +05:00
var matchFiles = await fileService.GetInfosByCategoryAsync(idWell, idFileCategoryDrillingProgram, token)
.ConfigureAwait(false);
if (matchFiles is not null && matchFiles.Any()) {
2021-08-31 09:52:32 +05:00
matchFiles = matchFiles.OrderByDescending(f => f.UploadDate);
var matchFilesIterator = matchFiles.GetEnumerator();
matchFilesIterator.MoveNext();
var matchFile = matchFilesIterator.Current;
while (matchFilesIterator.MoveNext())
await fileService.DeletedAsync(matchFilesIterator.Current.Id, token)
.ConfigureAwait(false);
2021-08-31 09:52:32 +05:00
2021-08-29 17:25:16 +05:00
if (filesInfos.All(f => f.UploadDate <= matchFile.UploadDate))
return matchFile;
else
2021-08-31 09:52:32 +05:00
await fileService.DeletedAsync(matchFile.Id, token)
.ConfigureAwait(false);
2021-08-29 17:25:16 +05:00
}
var fileNames = filesInfos
.Where(f => f.Name != resultFileName)
.Select(f => fileService.GetFileName(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)
2021-08-29 17:25:16 +05:00
.ConfigureAwait(false);
}
private static void UniteExcelFiles(IEnumerable<string> 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);
}
}
}