2021-09-10 11:28:57 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
|
|
|
|
using AsbCloudApp.Services;
|
2021-09-23 11:37:57 +05:00
|
|
|
|
using ClosedXML.Excel;
|
|
|
|
|
using ClosedXML.Excel.Drawings;
|
2021-08-29 17:25:16 +05:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2021-08-31 09:52:32 +05:00
|
|
|
|
var matchFiles = await fileService.GetInfosByCategoryAsync(idWell, idFileCategoryDrillingProgram, token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
2021-09-10 11:28:57 +05:00
|
|
|
|
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)
|
2021-09-01 15:55:10 +05:00
|
|
|
|
.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
|
|
|
|
}
|
|
|
|
|
|
2021-09-23 11:37:57 +05:00
|
|
|
|
var resultFileName = $"Программа бурения {well.Cluster} {well.Caption}.xlsx";
|
|
|
|
|
|
2021-08-29 17:25:16 +05:00
|
|
|
|
var fileNames = filesInfos
|
|
|
|
|
.Where(f => f.Name != resultFileName)
|
2021-09-23 10:53:48 +05:00
|
|
|
|
.Select(f => fileService.GetUrl(f));
|
2021-08-29 17:25:16 +05:00
|
|
|
|
|
2021-09-23 11:37:57 +05:00
|
|
|
|
var resultExcelPath = Path.Combine(fileService.RootPath,
|
|
|
|
|
$"{idWell}", $"{idFileCategoryDrillingProgram}",
|
|
|
|
|
resultFileName);
|
|
|
|
|
|
|
|
|
|
UniteExcelFiles(fileNames, resultExcelPath);
|
|
|
|
|
|
|
|
|
|
return await fileService.SaveAsync(idWell, null, idFileCategoryDrillingProgram,
|
|
|
|
|
resultExcelPath, null, token)
|
2021-08-29 17:25:16 +05:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-23 11:37:57 +05:00
|
|
|
|
private static void UniteExcelFiles(IEnumerable<string> excelFilesNames, string resultExcelPath)
|
2021-08-29 17:25:16 +05:00
|
|
|
|
{
|
2021-09-23 11:37:57 +05:00
|
|
|
|
var resultExcelFile = new XLWorkbook(XLEventTracking.Disabled);
|
|
|
|
|
|
|
|
|
|
const int maxAllowedColumns = 256;
|
2021-08-29 17:25:16 +05:00
|
|
|
|
|
|
|
|
|
foreach (var excelFileName in excelFilesNames)
|
|
|
|
|
{
|
2021-09-23 11:37:57 +05:00
|
|
|
|
using var sourceExcelFile = new XLWorkbook(excelFileName, XLEventTracking.Disabled);
|
2021-08-29 17:25:16 +05:00
|
|
|
|
|
2021-09-23 11:37:57 +05:00
|
|
|
|
foreach (var sheet in sourceExcelFile.Worksheets)
|
2021-08-29 17:25:16 +05:00
|
|
|
|
{
|
2021-09-23 11:37:57 +05:00
|
|
|
|
var imagesInfos = sheet.Pictures.Select(p => new ImageInfo
|
|
|
|
|
{
|
|
|
|
|
Id = p.Id,
|
|
|
|
|
Data = p.ImageStream.GetBuffer(),
|
|
|
|
|
Height = p.Height,
|
|
|
|
|
Width = p.Width,
|
|
|
|
|
TopLeftCellAddress = p.TopLeftCell.Address,
|
|
|
|
|
Left = p.Left,
|
|
|
|
|
Top = p.Top
|
|
|
|
|
}).ToList();
|
|
|
|
|
|
|
|
|
|
if (sheet.Columns().Count() > maxAllowedColumns)
|
2021-08-29 17:25:16 +05:00
|
|
|
|
{
|
2021-09-23 11:37:57 +05:00
|
|
|
|
var resultSheet = resultExcelFile.Worksheets.Add(sheet.Name);
|
|
|
|
|
|
|
|
|
|
var rngData = GetCellsRange(sheet);
|
|
|
|
|
|
|
|
|
|
rngData.CopyTo(resultSheet.Cell(1, 1));
|
|
|
|
|
|
|
|
|
|
var lastRowWithData = rngData.LastRowUsed().RangeAddress
|
|
|
|
|
.LastAddress.RowNumber;
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i < lastRowWithData; i++)
|
|
|
|
|
{
|
|
|
|
|
resultSheet.Row(i).Height = sheet.Row(i).Height;
|
|
|
|
|
resultSheet.Column(i).Width = sheet.Column(i).Width;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CopyImagesToAnotherSheet(imagesInfos, resultSheet);
|
2021-08-29 17:25:16 +05:00
|
|
|
|
}
|
2021-09-23 11:37:57 +05:00
|
|
|
|
else
|
2021-08-29 17:25:16 +05:00
|
|
|
|
{
|
2021-09-23 11:37:57 +05:00
|
|
|
|
RemovePicturesFromSheet(sheet);
|
|
|
|
|
|
|
|
|
|
var resultSheet = sheet.CopyTo(resultExcelFile, sheet.Name);
|
|
|
|
|
|
|
|
|
|
CopyImagesToAnotherSheet(imagesInfos, resultSheet);
|
2021-08-29 17:25:16 +05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-09-23 11:37:57 +05:00
|
|
|
|
|
|
|
|
|
resultExcelFile.SaveAs(resultExcelPath,
|
|
|
|
|
new SaveOptions { EvaluateFormulasBeforeSaving = true});
|
2021-08-29 17:25:16 +05:00
|
|
|
|
}
|
2021-09-23 11:37:57 +05:00
|
|
|
|
|
|
|
|
|
private static IXLWorksheet CopyImagesToAnotherSheet(IEnumerable<ImageInfo> imagesInfos,
|
|
|
|
|
IXLWorksheet resultSheet)
|
|
|
|
|
{
|
|
|
|
|
foreach (var image in imagesInfos)
|
|
|
|
|
{
|
|
|
|
|
var stream = new MemoryStream();
|
|
|
|
|
stream.Write(image.Data, 0, image.Data.Length);
|
|
|
|
|
|
|
|
|
|
resultSheet.AddPicture(stream)
|
|
|
|
|
.WithPlacement(XLPicturePlacement.Move)
|
|
|
|
|
.WithSize(image.Width, image.Height)
|
|
|
|
|
.MoveTo(resultSheet.Cell(image.TopLeftCellAddress),
|
|
|
|
|
image.Left, image.Top);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resultSheet;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void RemovePicturesFromSheet(IXLWorksheet sheet)
|
|
|
|
|
{
|
|
|
|
|
var filteredPics = sheet.Pictures.Select(p => p.Name).Distinct().ToList();
|
|
|
|
|
|
|
|
|
|
foreach (var n in filteredPics)
|
|
|
|
|
sheet.Pictures.Delete(n);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static IXLRange GetCellsRange(IXLWorksheet sheet)
|
|
|
|
|
{
|
|
|
|
|
var firstTableCell = sheet.FirstCellUsed();
|
|
|
|
|
var lastTableCell = sheet.LastCellUsed();
|
|
|
|
|
var rngData = sheet.Range(firstTableCell.Address, lastTableCell.Address);
|
|
|
|
|
|
|
|
|
|
return rngData;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class ImageInfo
|
|
|
|
|
{
|
|
|
|
|
public int Id { get; set; }
|
|
|
|
|
public byte[] Data { get; set; }
|
|
|
|
|
public int Height { get; set; }
|
|
|
|
|
public int Width { get; set; }
|
|
|
|
|
public IXLAddress TopLeftCellAddress { get; set; }
|
|
|
|
|
public int Left { get; set; }
|
|
|
|
|
public int Top { get; set; }
|
2021-08-29 17:25:16 +05:00
|
|
|
|
}
|
|
|
|
|
}
|