Отдельный метод CreateReportAsync, который вызывается из метода ворка

This commit is contained in:
Olga Nemt 2023-12-25 15:47:03 +05:00
parent 58edaf4204
commit ae072e7438
3 changed files with 76 additions and 72 deletions

View File

@ -24,6 +24,17 @@ namespace AsbCloudApp.Services
string EnqueueCreateReportWork(int idWell, int idUser, ReportParametersRequest request,
Action<object, string> handleReportProgress);
/// <summary>
/// Создание отчета
/// </summary>
/// <param name="idWell"></param>
/// <param name="idUser"></param>
/// <param name="request"></param>
/// <param name="progressHandler"></param>
/// <param name="token"></param>
/// <returns></returns>
Task CreateReportAsync(int idWell, int idUser, ReportParametersRequest request, Action<object, string> progressHandler, CancellationToken token);
/// <summary>
/// Получить предполагаемый список страниц рапорта
/// </summary>

View File

@ -1,14 +1,7 @@
using AsbCloudApp.Data;
using AsbCloudApp.Requests;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services;
using Mapster;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
@ -20,82 +13,23 @@ namespace AsbCloudInfrastructure.Background
internal class WorkToCreateReport : Work
{
private int idWell;
private DateTime begin;
private DateTime end;
private int stepSeconds;
private int format;
private int idUser;
private ReportParametersRequest request;
private Action<object, string> progressHandler;
public WorkToCreateReport(int idWell, int idUser, ReportParametersRequest request, Action<object, string> progressHandler) : base("")
{
this.idWell = idWell;
this.idUser = idUser;
this.begin = request.Begin;
this.end = request.End;
this.stepSeconds = request.StepSeconds;
this.format = request.Format;
this.request = request;
this.progressHandler = progressHandler;
Id = $"create report by wellid:{idWell} for userid:{idUser} requested at {DateTime.Now}";
}
protected override async Task Action(string id, IServiceProvider services, Action<string, double?> onProgress, CancellationToken token)
{
var wellService = services.GetRequiredService<IWellService>();
using var context = services.GetRequiredService<IAsbCloudDbContext>();
var fileService = services.GetRequiredService<FileService>();
var timezoneOffset = wellService.GetTimezone(idWell).Hours;
var beginRemote = begin.ToTimeZoneOffsetHours(timezoneOffset);
var endRemote = end.ToTimeZoneOffsetHours(timezoneOffset);
var beginUtc = begin.ToUtcDateTimeOffset(timezoneOffset);
var endUtc = end.ToUtcDateTimeOffset(timezoneOffset);
var tempDir = Path.Combine(Path.GetTempPath(), "report");
var generator = ReportService.GetReportGenerator(idWell, beginRemote, endRemote, stepSeconds, format, context);
var reportFileName = Path.Combine(tempDir, generator.GetReportDefaultFileName());
var totalPages = generator.GetPagesCount();
generator.OnProgress += (s, e) =>
{
var arg = e.Adapt<ReportProgressDto>();
onProgress(arg.Operation ?? string.Empty, arg.Progress);
progressHandler.Invoke(arg, id);
};
generator.Make(reportFileName);
var ReportCategoryId = context.FileCategories
.AsNoTracking()
.First(c => c.Name.Equals("Рапорт"))
.Id;
var fileInfo = (await fileService.MoveAsync(idWell, idUser, ReportCategoryId, reportFileName, reportFileName, token))!;
progressHandler.Invoke(new
{
Operation = "done",
Progress = 100f,
TotalPages = totalPages,
CurrentPage = totalPages,
file = fileInfo,
}, id);
var newReportProperties = new ReportProperty
{
IdWell = idWell,
IdFile = fileInfo.Id,
Begin = beginUtc,
End = endUtc,
Step = stepSeconds,
Format = format
};
context.ReportProperties.Add(newReportProperties);
context.SaveChanges();
var reportService = services.GetRequiredService<IReportService>();
await reportService.CreateReportAsync(idWell, idUser, request, progressHandler, token);
}
}
}

View File

@ -4,9 +4,11 @@ using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Background;
using AsbSaubReport;
using Mapster;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
@ -113,7 +115,64 @@ public class ReportService : IReportService
return dtos;
}
public static IReportGenerator GetReportGenerator(int idWell, DateTime begin,
public async Task CreateReportAsync(
int idWell,
int idUser,
ReportParametersRequest request,
Action<object, string> progressHandler,
CancellationToken token)
{
var workId = $"create report by wellid:{idWell} for userid:{idUser} requested at {DateTime.Now}";
var timezoneOffset = wellService.GetTimezone(idWell).Hours;
var beginRemote = request.Begin.ToTimeZoneOffsetHours(timezoneOffset);
var endRemote = request.End.ToTimeZoneOffsetHours(timezoneOffset);
var beginUtc = request.Begin.ToUtcDateTimeOffset(timezoneOffset);
var endUtc = request.End.ToUtcDateTimeOffset(timezoneOffset);
var tempDir = Path.Combine(Path.GetTempPath(), "report");
var generator = GetReportGenerator(idWell, beginRemote, endRemote, request.StepSeconds, request.Format, db);
var reportFileName = Path.Combine(tempDir, generator.GetReportDefaultFileName());
var totalPages = generator.GetPagesCount();
generator.OnProgress += (s, e) =>
{
var arg = e.Adapt<ReportProgressDto>();
progressHandler(arg, workId);
};
generator.Make(reportFileName);
var ReportCategoryId = db.FileCategories
.AsNoTracking()
.First(c => c.Name.Equals("Рапорт"))
.Id;
var fileInfo = (await fileService.MoveAsync(idWell, idUser, ReportCategoryId, reportFileName, reportFileName, token))!;
progressHandler(new
{
Operation = "done",
Progress = 100f,
TotalPages = totalPages,
CurrentPage = totalPages,
file = fileInfo,
}, workId);
var newReportProperties = new ReportProperty
{
IdWell = idWell,
IdFile = fileInfo.Id,
Begin = beginUtc,
End = endUtc,
Step = request.StepSeconds,
Format = request.Format
};
db.ReportProperties.Add(newReportProperties);
db.SaveChanges();
}
private static IReportGenerator GetReportGenerator(int idWell, DateTime begin,
DateTime end, int stepSeconds, int format, IAsbCloudDbContext context)
{
var dataSource = new ReportDataSourcePgCloud(context, idWell);