RefactoringDI
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Diagnostics;
|
||||
using TrafagSalesExporter.Data;
|
||||
using TrafagSalesExporter.Models;
|
||||
|
||||
@@ -8,11 +7,9 @@ namespace TrafagSalesExporter.Services;
|
||||
public class ExportOrchestrationService
|
||||
{
|
||||
private readonly IDbContextFactory<AppDbContext> _dbFactory;
|
||||
private readonly HanaQueryService _hanaService;
|
||||
private readonly ExcelExportService _excelService;
|
||||
private readonly SharePointUploadService _sharePointService;
|
||||
private readonly RecordTransformationService _transformationService;
|
||||
private readonly ILogger<ExportOrchestrationService> _logger;
|
||||
private readonly ISiteExportService _siteExportService;
|
||||
private readonly IConsolidatedExportService _consolidatedExportService;
|
||||
private readonly IExportLogService _exportLogService;
|
||||
|
||||
public event Action? OnExportStatusChanged;
|
||||
|
||||
@@ -21,18 +18,14 @@ public class ExportOrchestrationService
|
||||
|
||||
public ExportOrchestrationService(
|
||||
IDbContextFactory<AppDbContext> dbFactory,
|
||||
HanaQueryService hanaService,
|
||||
ExcelExportService excelService,
|
||||
SharePointUploadService sharePointService,
|
||||
RecordTransformationService transformationService,
|
||||
ILogger<ExportOrchestrationService> logger)
|
||||
ISiteExportService siteExportService,
|
||||
IConsolidatedExportService consolidatedExportService,
|
||||
IExportLogService exportLogService)
|
||||
{
|
||||
_dbFactory = dbFactory;
|
||||
_hanaService = hanaService;
|
||||
_excelService = excelService;
|
||||
_sharePointService = sharePointService;
|
||||
_transformationService = transformationService;
|
||||
_logger = logger;
|
||||
_siteExportService = siteExportService;
|
||||
_consolidatedExportService = consolidatedExportService;
|
||||
_exportLogService = exportLogService;
|
||||
}
|
||||
|
||||
public bool IsExporting(int siteId)
|
||||
@@ -64,31 +57,7 @@ public class ExportOrchestrationService
|
||||
consolidatedRecords.AddRange(result.Records);
|
||||
}
|
||||
|
||||
if (consolidatedRecords.Count > 0)
|
||||
{
|
||||
var spConfig = await db.SharePointConfigs.FirstOrDefaultAsync();
|
||||
var outputDir = Path.Combine(AppContext.BaseDirectory, "output");
|
||||
var consolidatedPath = _excelService.CreateConsolidatedExcelFile(
|
||||
outputDir,
|
||||
DateTime.UtcNow.Date,
|
||||
consolidatedRecords
|
||||
.OrderBy(r => r.Land)
|
||||
.ThenBy(r => r.Tsc)
|
||||
.ThenByDescending(r => r.InvoiceDate ?? DateTime.MinValue)
|
||||
.ThenBy(r => r.InvoiceNumber)
|
||||
.ThenBy(r => r.PositionOnInvoice)
|
||||
.ToList());
|
||||
|
||||
if (spConfig is not null &&
|
||||
!string.IsNullOrWhiteSpace(spConfig.TenantId) &&
|
||||
!string.IsNullOrWhiteSpace(spConfig.ClientId) &&
|
||||
!string.IsNullOrWhiteSpace(spConfig.ClientSecret))
|
||||
{
|
||||
await _sharePointService.UploadAsync(
|
||||
spConfig.TenantId, spConfig.ClientId, spConfig.ClientSecret,
|
||||
spConfig.SiteUrl, spConfig.ExportFolder, "Alle", consolidatedPath);
|
||||
}
|
||||
}
|
||||
await _consolidatedExportService.ExportAsync(consolidatedRecords);
|
||||
}
|
||||
|
||||
public async Task ExportSiteByIdAsync(int siteId)
|
||||
@@ -102,6 +71,7 @@ public class ExportOrchestrationService
|
||||
private async Task<SiteExportResult?> ExportSiteAsync(Site site)
|
||||
{
|
||||
if (site.HanaServer is null) return null;
|
||||
SiteExportResult? result = null;
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
@@ -110,75 +80,17 @@ public class ExportOrchestrationService
|
||||
}
|
||||
NotifyChanged();
|
||||
|
||||
var sw = Stopwatch.StartNew();
|
||||
var log = new ExportLog
|
||||
{
|
||||
Timestamp = DateTime.Now,
|
||||
SiteId = site.Id,
|
||||
Land = site.Land,
|
||||
TSC = site.TSC
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
using var db = await _dbFactory.CreateDbContextAsync();
|
||||
var settings = await db.ExportSettings.FirstOrDefaultAsync() ?? new ExportSettings();
|
||||
var spConfig = await db.SharePointConfigs.FirstOrDefaultAsync();
|
||||
|
||||
UpdateStatus(site.Id, "HANA Abfrage...");
|
||||
var records = await Task.Run(() => _hanaService.GetSalesRecords(
|
||||
site.HanaServer, site.Schema, site.TSC, site.Land, settings.DateFilter));
|
||||
|
||||
UpdateStatus(site.Id, "Transformationen anwenden...");
|
||||
var rules = await db.FieldTransformationRules
|
||||
.Where(r => r.IsActive && r.SourceSystem == (string.IsNullOrWhiteSpace(site.SourceSystem) ? "SAP" : site.SourceSystem))
|
||||
.OrderBy(r => r.SortOrder)
|
||||
.ToListAsync();
|
||||
_transformationService.Apply(records, rules);
|
||||
|
||||
UpdateStatus(site.Id, "Excel erstellen...");
|
||||
var outputDir = Path.Combine(AppContext.BaseDirectory, "output");
|
||||
var filePath = _excelService.CreateExcelFile(outputDir, site.TSC, DateTime.UtcNow.Date, records);
|
||||
var fileName = Path.GetFileName(filePath);
|
||||
|
||||
if (spConfig is not null &&
|
||||
!string.IsNullOrWhiteSpace(spConfig.TenantId) &&
|
||||
!string.IsNullOrWhiteSpace(spConfig.ClientId) &&
|
||||
!string.IsNullOrWhiteSpace(spConfig.ClientSecret))
|
||||
{
|
||||
UpdateStatus(site.Id, "SharePoint Upload...");
|
||||
await _sharePointService.UploadAsync(
|
||||
spConfig.TenantId, spConfig.ClientId, spConfig.ClientSecret,
|
||||
spConfig.SiteUrl, spConfig.ExportFolder, site.Land, filePath);
|
||||
}
|
||||
|
||||
sw.Stop();
|
||||
log.Status = "OK";
|
||||
log.RowCount = records.Count;
|
||||
log.FileName = fileName;
|
||||
log.DurationSeconds = sw.Elapsed.TotalSeconds;
|
||||
|
||||
_logger.LogInformation("Export OK: {Land} ({TSC}) - {Rows} Zeilen in {Duration:F1}s",
|
||||
site.Land, site.TSC, records.Count, sw.Elapsed.TotalSeconds);
|
||||
|
||||
return new SiteExportResult(records, filePath);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
sw.Stop();
|
||||
log.Status = "Error";
|
||||
log.ErrorMessage = ex.Message;
|
||||
log.FileName = string.Empty;
|
||||
log.DurationSeconds = sw.Elapsed.TotalSeconds;
|
||||
|
||||
_logger.LogError(ex, "Export Fehler: {Land} ({TSC})", site.Land, site.TSC);
|
||||
return null;
|
||||
result = await _siteExportService.ExportAsync(site, status => UpdateStatus(site.Id, status));
|
||||
return result;
|
||||
}
|
||||
finally
|
||||
{
|
||||
using var db = await _dbFactory.CreateDbContextAsync();
|
||||
db.ExportLogs.Add(log);
|
||||
await db.SaveChangesAsync();
|
||||
if (result is not null)
|
||||
{
|
||||
await _exportLogService.WriteAsync(result.Log);
|
||||
}
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
@@ -201,6 +113,4 @@ public class ExportOrchestrationService
|
||||
{
|
||||
OnExportStatusChanged?.Invoke();
|
||||
}
|
||||
|
||||
private sealed record SiteExportResult(List<SalesRecord> Records, string FilePath);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user