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

184 lines
7.3 KiB
C#
Raw Normal View History

2021-07-21 15:29:19 +05:00
using AsbCloudApp.Data;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
2021-07-21 15:29:19 +05:00
using AsbSaubReport;
using AsbSaubReportPdf;
2021-07-21 15:29:19 +05:00
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
2021-07-21 15:29:19 +05:00
using System.Linq;
namespace AsbCloudInfrastructure.Services
{
public class ReportService : IReportService
{
private readonly IAsbCloudDbContext db;
private readonly IConfiguration configuration;
private readonly ITelemetryService telemetryService;
private readonly IFileService fileService;
private readonly IBackgroundQueue queue;
2021-07-28 09:47:13 +05:00
public ReportService(IAsbCloudDbContext db, IConfiguration configuration,
ITelemetryService telemetryService, IFileService fileService,
IBackgroundQueue queue)
{
this.db = db;
this.configuration = configuration;
this.telemetryService = telemetryService;
this.fileService = fileService;
this.queue = queue;
ReportCategoryId = db.FileCategories.AsNoTracking()
.FirstOrDefault(c =>
c.Name.Equals("Рапорт")).Id;
}
public int ReportCategoryId { get; private set; }
public int CreateReport(int idWell, int idUser, int stepSeconds, int format, DateTime begin,
2021-07-21 15:29:19 +05:00
DateTime end, Action<float, string, int> progressHandler, Action<string, int> reportNameHandler)
{
2021-05-20 12:38:25 +05:00
var newReportId = queue.EnqueueTask((id) =>
{
var optionsBuilder = new DbContextOptionsBuilder<AsbCloudDbContext>();
optionsBuilder.UseNpgsql(configuration.GetConnectionString("DefaultConnection"));
using (var context = new AsbCloudDbContext(optionsBuilder.Options))
{
2021-07-27 14:43:30 +05:00
var generator = GetReportGenerator(idWell, begin, end, stepSeconds, format, context);
2021-07-21 15:29:19 +05:00
generator.OnProgress += (s, e) => progressHandler.Invoke(e.progress, e.operation, id);
var newReportName = generator.Make();
2021-07-21 15:29:19 +05:00
if (newReportName is not null)
{
var shorReportName = Path.GetFileName(newReportName);
reportNameHandler.Invoke(shorReportName, id);
var newReportFile = new AsbCloudDb.Model.File
{
2021-07-27 14:43:30 +05:00
IdWell = idWell,
IdAuthor = idUser,
IdCategory = ReportCategoryId,
Name = newReportName,
Date = DateTime.Now,
};
context.Files.Add(newReportFile);
context.SaveChanges();
var newReportProperties = new ReportProperties
{
IdWell = idWell,
IdFile = newReportFile.Id,
Begin = begin,
End = end,
Step = stepSeconds,
Format = format
};
context.ReportProperties.Add(newReportProperties);
context.SaveChanges();
}
}
});
return newReportId;
}
2021-07-27 14:43:30 +05:00
public int GetReportPagesCount(int idWell, DateTime begin, DateTime end, int stepSeconds, int format)
{
2021-07-27 14:43:30 +05:00
var generator = GetReportGenerator(idWell, begin, end, stepSeconds, format, (AsbCloudDbContext)db);
return generator.GetPagesCount();
}
2021-07-27 14:43:30 +05:00
public IEnumerable<ReportPropertiesDto> GetSuitableReports(int idWell, DateTime begin, DateTime end, int stepSeconds, int format)
{
2021-07-27 14:43:30 +05:00
var suitableReportsFromDb = GetSuitableReportsFromDb(idWell, begin, end, stepSeconds, format);
2021-07-21 15:29:19 +05:00
var suitableReportsProperties = suitableReportsFromDb.Select(r => new ReportPropertiesDto
{
Id = r.Id,
2021-07-28 09:47:13 +05:00
Name = Path.GetFileName(r.File.Name),
FullName = r.File.Name,
2021-07-28 09:46:58 +05:00
IdWell = r.IdWell,
2021-07-28 09:47:13 +05:00
Date = r.File.Date,
Begin = r.Begin,
End = r.End,
Step = r.Step,
Format = r.Format == 0 ? ".pdf" : ".las"
});
return suitableReportsProperties;
}
2021-07-27 14:43:30 +05:00
public DatesRangeDto GetReportsDatesRange(int idWell)
{
var telemetryId = telemetryService.GetIdTelemetryByIdWell(idWell);
if (telemetryId is null)
return null;
var datesRange = (from d in db.DataSaubBases
where d.IdTelemetry == telemetryId
2021-07-21 15:29:19 +05:00
select d.Date).Union(
2021-08-10 16:37:13 +05:00
from m in db.TelemetryMessages
where m.IdTelemetry == telemetryId
select m.Date).DefaultIfEmpty()
.GroupBy(g => true)
.AsNoTracking()
.Select(g => new
{
From = g.Min(),
To = g.Max()
}).OrderBy(gr => gr.From)
.FirstOrDefault();
2021-07-21 15:29:19 +05:00
return new DatesRangeDto
{
From = datesRange.From,
To = datesRange.To.Year == 1 ? DateTime.MaxValue : datesRange.To
};
}
private IEnumerable<ReportProperties> GetSuitableReportsFromDb(int idWell, DateTime begin, DateTime end, int stepSeconds, int format)
{
var suitableReportsNames = (from r in db.ReportProperties.Include(r => r.File)
2021-07-27 14:43:30 +05:00
where r.IdWell == idWell
&& r.Begin >= begin
&& r.End <= end
&& r.Step <= stepSeconds
&& r.Format == format
select r).OrderBy(o => o.File.Date)
.AsNoTracking()
.Take(512).ToList();
return suitableReportsNames;
}
2021-07-27 14:43:30 +05:00
private IReportGenerator GetReportGenerator(int idWell, DateTime begin, DateTime end, int stepSeconds, int format, AsbCloudDbContext context)
{
2021-07-27 14:43:30 +05:00
var dataSource = new ReportDataSourcePgCloud(context, idWell);
2021-05-19 15:50:09 +05:00
IReportGenerator generator;
2021-07-21 15:29:19 +05:00
switch (format)
2021-05-19 15:50:09 +05:00
{
case 1: //LAS
generator = new AsbSaubReportLas.LasReprotGenerator(dataSource);
break;
case 0: //PDF
2021-05-19 15:50:09 +05:00
default:
generator = new PdfReprotGenerator(dataSource);
break;
2021-05-19 15:50:09 +05:00
}
generator.ReportDirectory = Path.Combine(fileService.RootPath, $"{idWell}", $"{ReportCategoryId}");
generator.Begin = begin;
generator.End = end;
generator.Step = TimeSpan.FromSeconds(stepSeconds);
generator.WithCharts = true;
generator.WithEvents = true;
2021-07-28 09:47:13 +05:00
return generator;
}
}
}