DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/DrillTestReport/DrillTestReportService.cs
Olga Nemt b7ce104e4e - Экспорт отчета Drill test
- Добавлен новый extension метод AssemblyExtensions.GetTemplateCopyStreamAsync
- IAutoGeneratedDailyReportMakerService переименован в IReportMakerService и стал более универсальным
2023-10-20 11:24:04 +05:00

104 lines
4.1 KiB
C#

using AsbCloudApp.Data;
using AsbCloudApp.Data.DrillTestReport;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using AsbCloudApp.Services.AutoGeneratedDailyReports;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Services.DrillTestReport
{
public class DrillTestReportService : IDrillTestReportService
{
private readonly IWellService wellService;
private readonly IDrillTestRepository drillTestRepository;
private readonly ITelemetryService telemetryService;
private readonly IReportMakerService<DrillTestReportDataDto> drillTestReportMakerService;
public DrillTestReportService(
IWellService wellService,
IDrillTestRepository drillTestRepository,
ITelemetryService telemetryService,
IReportMakerService<DrillTestReportDataDto> drillTestReportMakerService)
{
this.wellService = wellService;
this.drillTestRepository = drillTestRepository;
this.telemetryService = telemetryService;
this.drillTestReportMakerService = drillTestReportMakerService;
}
public async Task<(string fileName, Stream stream)> GenerateAsync(int idWell, int id, CancellationToken cancellationToken)
{
var telemetry = telemetryService.GetOrDefaultTelemetryByIdWell(idWell);
if (telemetry is null)
throw new Exception($"Telemetry with idWell: {idWell} does not exist.");
var dto = await drillTestRepository.GetAsync(telemetry.Id, id, cancellationToken);
var timezone = telemetryService.GetTimezone(telemetry.Id);
var remoteDateTime = dto.TimeStampStart.ToRemoteDateTime(timezone.Hours);
dto.TimeStampStart = remoteDateTime;
var cluster = telemetry.Info?.Cluster ?? "-";
var deposit = telemetry.Info?.Deposit ?? "-";
var wellCaption = wellService.GetOrDefault(idWell)!.Caption ?? "-";
var report = new DrillTestReportDataDto()
{
Data = dto,
Caption = string.Format("Месторождение: {0}, куст: {1}, скважина: {2}", deposit, cluster, wellCaption),
Date = DateTime.Now,
};
var fileName = string.Format("Drill_test_{0}.xlsx", remoteDateTime.ToString("dd.mm.yyyy_HH_MM_ss"));
var stream = await drillTestReportMakerService.MakeReportAsync(report, cancellationToken);
return (fileName, stream);
}
public async Task<PaginationContainer<DrillTestReportInfoDto>> GetListAsync(int idWell, FileReportRequest request, CancellationToken cancellationToken)
{
var telemetry = telemetryService.GetOrDefaultTelemetryByIdWell(idWell);
if (telemetry is null)
throw new Exception($"Telemetry with idWell: {idWell} does not exist.");
var result = new PaginationContainer<DrillTestReportInfoDto>
{
Skip = request.Skip ?? 0,
Take = request.Take ?? 10,
Items = Enumerable.Empty<DrillTestReportInfoDto>()
};
var reports = new List<DrillTestReportInfoDto>();
var timezone = telemetryService.GetTimezone(telemetry.Id);
var dtos = await drillTestRepository.GetAllAsync(telemetry.Id, request, cancellationToken);
foreach (var dto in dtos)
{
var remoteDateTime = dto.TimeStampStart.ToRemoteDateTime(timezone.Hours);
reports.Add(new DrillTestReportInfoDto
{
FileName = string.Format("Drill_test_{0}", remoteDateTime),
DrillDepth = (dto.Params
.Where(p => p.DepthDrillStep.HasValue)
.Sum(x => x.DepthDrillStep) ?? 0) + dto.DepthStart,
DateTime = remoteDateTime,
Id = dto.Id,
});
}
result.Items = reports;
return result;
}
}
}