Add connection diagnostics and visual field transformation mapping
This commit is contained in:
@@ -11,6 +11,7 @@ public class ExportOrchestrationService
|
||||
private readonly HanaQueryService _hanaService;
|
||||
private readonly ExcelExportService _excelService;
|
||||
private readonly SharePointUploadService _sharePointService;
|
||||
private readonly RecordTransformationService _transformationService;
|
||||
private readonly ILogger<ExportOrchestrationService> _logger;
|
||||
|
||||
public event Action? OnExportStatusChanged;
|
||||
@@ -23,12 +24,14 @@ public class ExportOrchestrationService
|
||||
HanaQueryService hanaService,
|
||||
ExcelExportService excelService,
|
||||
SharePointUploadService sharePointService,
|
||||
RecordTransformationService transformationService,
|
||||
ILogger<ExportOrchestrationService> logger)
|
||||
{
|
||||
_dbFactory = dbFactory;
|
||||
_hanaService = hanaService;
|
||||
_excelService = excelService;
|
||||
_sharePointService = sharePointService;
|
||||
_transformationService = transformationService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
@@ -96,6 +99,13 @@ public class ExportOrchestrationService
|
||||
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);
|
||||
|
||||
@@ -32,6 +32,38 @@ public class HanaQueryService
|
||||
return result;
|
||||
}
|
||||
|
||||
public ConnectionTestResult TestConnectionDetailed(HanaServer server)
|
||||
{
|
||||
var testResult = new ConnectionTestResult
|
||||
{
|
||||
TestedAtUtc = DateTime.UtcNow,
|
||||
ConnectionStringPreview = server.GetConnectionStringPreview(),
|
||||
Stage = "Verbindungsaufbau"
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
var connectionString = server.BuildConnectionString();
|
||||
using var connection = new HanaConnection(connectionString);
|
||||
connection.Open();
|
||||
|
||||
testResult.Stage = "Ping-Query";
|
||||
using var command = new HanaCommand("SELECT 1 FROM DUMMY", connection);
|
||||
command.ExecuteScalar();
|
||||
|
||||
testResult.Success = true;
|
||||
testResult.Stage = "OK";
|
||||
return testResult;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
testResult.Success = false;
|
||||
testResult.ErrorMessage = ex.Message;
|
||||
testResult.ExceptionType = ex.GetType().Name;
|
||||
return testResult;
|
||||
}
|
||||
}
|
||||
|
||||
public void TestConnection(HanaServer server)
|
||||
{
|
||||
var connectionString = server.BuildConnectionString();
|
||||
@@ -173,3 +205,13 @@ LEFT JOIN {schema}.""OSLP"" emp ON h.""SlpCode"" = emp.""SlpCode""
|
||||
WHERE h.""CANCELED"" = 'N' AND h.""DocDate"" >= '{dateFilter}'
|
||||
ORDER BY h.""DocDate"" DESC, h.""DocNum"", p.""LineNum""";
|
||||
}
|
||||
|
||||
public class ConnectionTestResult
|
||||
{
|
||||
public bool Success { get; set; }
|
||||
public DateTime TestedAtUtc { get; set; }
|
||||
public string Stage { get; set; } = string.Empty;
|
||||
public string ErrorMessage { get; set; } = string.Empty;
|
||||
public string ExceptionType { get; set; } = string.Empty;
|
||||
public string ConnectionStringPreview { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
using System.Reflection;
|
||||
using TrafagSalesExporter.Models;
|
||||
|
||||
namespace TrafagSalesExporter.Services;
|
||||
|
||||
public class RecordTransformationService
|
||||
{
|
||||
private static readonly Dictionary<string, PropertyInfo> PropertyMap = typeof(SalesRecord)
|
||||
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
|
||||
.ToDictionary(p => p.Name, p => p, StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
public void Apply(List<SalesRecord> records, IEnumerable<FieldTransformationRule> rules)
|
||||
{
|
||||
var orderedRules = rules.Where(r => r.IsActive).OrderBy(r => r.SortOrder).ToList();
|
||||
if (orderedRules.Count == 0 || records.Count == 0) return;
|
||||
|
||||
foreach (var record in records)
|
||||
{
|
||||
foreach (var rule in orderedRules)
|
||||
{
|
||||
ApplyRule(record, rule);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void ApplyRule(SalesRecord record, FieldTransformationRule rule)
|
||||
{
|
||||
if (!PropertyMap.TryGetValue(rule.SourceField, out var sourceProp)) return;
|
||||
if (!PropertyMap.TryGetValue(rule.TargetField, out var targetProp)) return;
|
||||
|
||||
var sourceValue = sourceProp.GetValue(record);
|
||||
object? result = rule.TransformationType switch
|
||||
{
|
||||
"Copy" => sourceValue,
|
||||
"Uppercase" => sourceValue?.ToString()?.ToUpperInvariant(),
|
||||
"Lowercase" => sourceValue?.ToString()?.ToLowerInvariant(),
|
||||
"Prefix" => $"{rule.Argument}{sourceValue}",
|
||||
"Suffix" => $"{sourceValue}{rule.Argument}",
|
||||
"Replace" => ApplyReplace(sourceValue?.ToString(), rule.Argument),
|
||||
"Constant" => rule.Argument,
|
||||
_ => sourceValue
|
||||
};
|
||||
|
||||
SetPropertyValue(record, targetProp, result);
|
||||
}
|
||||
|
||||
private static string ApplyReplace(string? input, string? argument)
|
||||
{
|
||||
if (string.IsNullOrEmpty(input)) return string.Empty;
|
||||
if (string.IsNullOrWhiteSpace(argument)) return input;
|
||||
|
||||
var parts = argument.Split("=>", 2, StringSplitOptions.TrimEntries);
|
||||
if (parts.Length != 2) return input;
|
||||
return input.Replace(parts[0], parts[1], StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
private static void SetPropertyValue(SalesRecord record, PropertyInfo property, object? value)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (property.PropertyType == typeof(string))
|
||||
{
|
||||
property.SetValue(record, value?.ToString() ?? string.Empty);
|
||||
return;
|
||||
}
|
||||
|
||||
if (property.PropertyType == typeof(int))
|
||||
{
|
||||
if (int.TryParse(value?.ToString(), out var parsedInt)) property.SetValue(record, parsedInt);
|
||||
return;
|
||||
}
|
||||
|
||||
if (property.PropertyType == typeof(decimal))
|
||||
{
|
||||
if (decimal.TryParse(value?.ToString(), out var parsedDecimal)) property.SetValue(record, parsedDecimal);
|
||||
return;
|
||||
}
|
||||
|
||||
if (property.PropertyType == typeof(DateTime?) || property.PropertyType == typeof(DateTime))
|
||||
{
|
||||
if (DateTime.TryParse(value?.ToString(), out var parsedDate)) property.SetValue(record, parsedDate);
|
||||
return;
|
||||
}
|
||||
|
||||
property.SetValue(record, value);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// skip invalid conversion to keep export running
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user