DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/ProcessMap/ProcessMapReportService.cs

83 lines
3.0 KiB
C#

using AsbCloudApp.Data.ProcessMap;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.DailyReport;
using ClosedXML.Excel;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Services.ProcessMap
{
#nullable enable
public class ProcessMapReportService : IProcessMapReportService
{
private readonly IAsbCloudDbContext context;
private readonly IProcessMapRepository processMapRepository;
private readonly IProcessMapService processMapService;
public ProcessMapReportService(IAsbCloudDbContext context, IProcessMapRepository processMapRepository, IProcessMapService processMapService)
{
this.context = context;
this.processMapRepository = processMapRepository;
this.processMapService = processMapService;
}
public async Task<Stream> MakeReportAsync(int idWell, CancellationToken token)
{
var stream = GetExcelTemplateStream();
using var workbook = new XLWorkbook(stream, XLEventTracking.Disabled);
var data = await processMapService.GetProcessMapAsync(idWell, token);
FillProcessMapToWorkbook(workbook, data);
MemoryStream memoryStream = new MemoryStream();
workbook.SaveAs(memoryStream, new SaveOptions { });
memoryStream.Seek(0, SeekOrigin.Begin);
return memoryStream;
}
private static void FillProcessMapToWorkbook(XLWorkbook workbook, IEnumerable<ProcessMapReportDto> dto)
{
var rowsCount = 4;
var columnCount = 2;
var countMerge = 27;
var sheet = workbook.Worksheets.FirstOrDefault();
if (sheet is null)
return;
sheet.Row(rowsCount).Cell(columnCount).Value = "saddadasdasdasds";
sheet.Range(rowsCount, columnCount, rowsCount, countMerge).Row(1).Merge();
SetBorder(sheet.Row(rowsCount).Cell(columnCount).Style);
rowsCount++;
sheet.Row(rowsCount).Cell(columnCount).Value = 2;
sheet.Row(rowsCount).Cell(columnCount).Value = 3;
columnCount++;
sheet.Row(rowsCount).Cell(columnCount).Value = 4;
}
private Stream GetExcelTemplateStream()
{
var stream = System.Reflection.Assembly.GetExecutingAssembly()
.GetManifestResourceStream("AsbCloudInfrastructure.Services.ProcessMap.ProcessMapReportTemplate.xlsx");
return stream!;
}
private static IXLStyle SetBorder(IXLStyle style)
{
style.Border.RightBorder = XLBorderStyleValues.Medium;
style.Border.LeftBorder = XLBorderStyleValues.Medium;
style.Border.TopBorder = XLBorderStyleValues.Medium;
style.Border.BottomBorder = XLBorderStyleValues.Medium;
style.Border.InsideBorder = XLBorderStyleValues.Medium;
return style;
}
}
#nullable disable
}