zentrales excel der standarte

This commit is contained in:
2026-04-13 14:25:03 +02:00
parent 474d2215a2
commit 9a93920b71
156 changed files with 1933 additions and 5 deletions
@@ -10,7 +10,21 @@ public class ExcelExportService
Directory.CreateDirectory(outputDirectory);
var fileName = $"Sales_{tsc}_{fileDate:yyyy-MM-dd}.xlsx";
var fullPath = Path.Combine(outputDirectory, fileName);
WriteWorkbook(fullPath, records);
return fullPath;
}
public string CreateConsolidatedExcelFile(string outputDirectory, DateTime fileDate, List<SalesRecord> records)
{
Directory.CreateDirectory(outputDirectory);
var fileName = $"Sales_All_{fileDate:yyyy-MM-dd}.xlsx";
var fullPath = Path.Combine(outputDirectory, fileName);
WriteWorkbook(fullPath, records);
return fullPath;
}
private static void WriteWorkbook(string fullPath, List<SalesRecord> records)
{
using var workbook = new XLWorkbook();
var ws = workbook.Worksheets.Add("Sales");
@@ -84,6 +98,5 @@ public class ExcelExportService
ws.Columns().AdjustToContents();
workbook.SaveAs(fullPath);
return fullPath;
}
}
@@ -55,9 +55,39 @@ public class ExportOrchestrationService
{
using var db = await _dbFactory.CreateDbContextAsync();
var sites = await db.Sites.Include(s => s.HanaServer).Where(s => s.IsActive).ToListAsync();
var consolidatedRecords = new List<SalesRecord>();
foreach (var site in sites)
{
await ExportSiteAsync(site);
var result = await ExportSiteAsync(site);
if (result?.Records is { Count: > 0 })
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);
}
}
}
@@ -69,13 +99,13 @@ public class ExportOrchestrationService
await ExportSiteAsync(site);
}
private async Task ExportSiteAsync(Site site)
private async Task<SiteExportResult?> ExportSiteAsync(Site site)
{
if (site.HanaServer is null) return;
if (site.HanaServer is null) return null;
lock (_lock)
{
if (_runningExports.ContainsKey(site.Id)) return;
if (_runningExports.ContainsKey(site.Id)) return null;
_runningExports[site.Id] = "HANA Abfrage...";
}
NotifyChanged();
@@ -130,6 +160,8 @@ public class ExportOrchestrationService
_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)
{
@@ -140,6 +172,7 @@ public class ExportOrchestrationService
log.DurationSeconds = sw.Elapsed.TotalSeconds;
_logger.LogError(ex, "Export Fehler: {Land} ({TSC})", site.Land, site.TSC);
return null;
}
finally
{
@@ -168,4 +201,6 @@ public class ExportOrchestrationService
{
OnExportStatusChanged?.Invoke();
}
private sealed record SiteExportResult(List<SalesRecord> Records, string FilePath);
}