imports: save records

This commit is contained in:
Michał Zieliński
2025-06-02 20:11:29 +02:00
parent 5046a29ee1
commit 8efd1edfad
6 changed files with 366 additions and 119 deletions

View File

@@ -1,6 +1,7 @@
using System.Globalization;
using System.Text;
using System.Text.Json;
using Microsoft.Extensions.Logging;
using DiunaBI.Core.Models;
using DiunaBI.Database.Context;
@@ -9,74 +10,136 @@ namespace DiunaBI.Plugins.Morska.Importers;
public class MorskaD3Importer : MorskaBaseImporter
{
public override string ImporterType => "MorskaD3";
private readonly AppDbContext _db;
private readonly ILogger<MorskaD3Importer> _logger;
public MorskaD3Importer(
AppDbContext db)
AppDbContext db,
ILogger<MorskaD3Importer> logger)
{
_db = db;
_logger = logger;
}
public override void Import(Layer importWorker)
{
_logger.LogInformation("MorskaD3: Starting import for {ImportWorkerName} ({ImportWorkerId})",
importWorker.Name, importWorker.Id);
var year = importWorker.Records!.FirstOrDefault(x => x.Code == "ImportYear")?.Desc1;
if (year == null)
{
throw new Exception($"ImportYear not found, {importWorker.Name}");
}
var month = importWorker.Records!.FirstOrDefault(x => x.Code == "ImportMonth")?.Desc1;
if (month == null)
{
throw new Exception($"ImportMonth not found, {importWorker.Name}");
}
var name = importWorker.Records!.FirstOrDefault(x => x.Code == "ImportName")?.Desc1;
if (name == null)
{
throw new Exception($"ImportName not found, {importWorker.Name}");
}
var type = importWorker.Records!.FirstOrDefault(x => x.Code == "ImportType")?.Desc1;
if (name == null)
if (type == null)
{
throw new Exception($"ImportType not found, {importWorker.Name}");
}
_logger.LogDebug("MorskaD3: Looking for DataInbox with type {Type}", type);
var dataInbox = _db.DataInbox.OrderByDescending(x => x.CreatedAt).FirstOrDefault(x => x.Name == type);
if (dataInbox == null)
{
throw new Exception($"DataInbox not found, {type}");
}
var data = Convert.FromBase64String(dataInbox.Data);
var tst = Encoding.UTF8.GetString(data);
var records = JsonSerializer.Deserialize<List<Record>>(Encoding.UTF8.GetString(data));
if (records == null)
{
throw new Exception($"DataInbox.Data is empty, {dataInbox.Name}");
}
records = records.Where(x => x.Code!.StartsWith($"{year}{month}")).ToList();
if (records.Count == 0)
{
throw new Exception($"No records found for {year}{month}");
}
records = records.Select(x =>
{
x.Id = Guid.NewGuid();
x.CreatedAt = DateTime.UtcNow;
x.ModifiedAt = DateTime.UtcNow;
return x;
}).ToList();
var layer = new Layer
{
Number = _db.Layers.Count() + 1,
ParentId = importWorker.Id,
Type = LayerType.Import,
CreatedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"),
ModifiedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"),
CreatedAt = DateTime.UtcNow,
ModifiedAt = DateTime.UtcNow
};
layer.Name = $"L{layer.Number}-I-{name}-{year}/{month}-{DateTime.Now.ToString("yyyyMMddHHmm", CultureInfo.InvariantCulture)}";
_db.Layers.Add(layer);
// TODO: Save records to the layer
//controller.SaveRecords(layer.Id, records, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
_db.SaveChanges();
_logger.LogDebug("MorskaD3: Found DataInbox {DataInboxId}, created at {CreatedAt}",
dataInbox.Id, dataInbox.CreatedAt);
try
{
var data = Convert.FromBase64String(dataInbox.Data);
var jsonString = Encoding.UTF8.GetString(data);
_logger.LogDebug("MorskaD3: Decoded {DataSize} bytes from base64", data.Length);
var records = JsonSerializer.Deserialize<List<Record>>(jsonString);
if (records == null)
{
throw new Exception($"DataInbox.Data is empty, {dataInbox.Name}");
}
_logger.LogDebug("MorskaD3: Deserialized {RecordCount} records from JSON", records.Count);
records = records.Where(x => x.Code!.StartsWith($"{year}{month}")).ToList();
if (records.Count == 0)
{
throw new Exception($"No records found for {year}{month}");
}
_logger.LogDebug("MorskaD3: Filtered to {FilteredCount} records for period {Year}{Month}",
records.Count, year, month);
records = records.Select(x =>
{
x.Id = Guid.NewGuid();
x.CreatedAt = DateTime.UtcNow;
x.ModifiedAt = DateTime.UtcNow;
return x;
}).ToList();
var layer = new Layer
{
Id = Guid.NewGuid(),
Number = _db.Layers.Count() + 1,
ParentId = importWorker.Id,
Type = LayerType.Import,
CreatedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"),
ModifiedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"),
CreatedAt = DateTime.UtcNow,
ModifiedAt = DateTime.UtcNow
};
layer.Name = $"L{layer.Number}-I-{name}-{year}/{month}-{DateTime.Now:yyyyMMddHHmm}";
_db.Layers.Add(layer);
SaveRecords(layer.Id, records);
_db.SaveChanges();
_logger.LogInformation("MorskaD3: Successfully imported {RecordCount} records for layer {LayerName} ({LayerId})",
records.Count, layer.Name, layer.Id);
}
catch (Exception e)
{
_logger.LogError(e, "MorskaD3: Error processing DataInbox {DataInboxId}", dataInbox.Id);
throw;
}
}
private void SaveRecords(Guid layerId, ICollection<Record> records)
{
var toDelete = _db.Records.Where(x => x.LayerId == layerId).ToList();
if (toDelete.Count > 0)
{
_db.Records.RemoveRange(toDelete);
}
foreach (var record in records)
{
record.CreatedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D");
record.CreatedAt = DateTime.UtcNow;
record.ModifiedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D");
record.ModifiedAt = DateTime.UtcNow;
record.LayerId = layerId;
_db.Records.Add(record);
}
_logger.LogDebug("MorskaD3: Saved {RecordCount} records for layer {LayerId}", records.Count, layerId);
}
}