2025-05-31 19:26:02 +02:00
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Text.Json;
|
2025-06-02 20:11:29 +02:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2025-05-31 19:26:02 +02:00
|
|
|
|
using DiunaBI.Core.Models;
|
|
|
|
|
|
using DiunaBI.Database.Context;
|
|
|
|
|
|
|
|
|
|
|
|
namespace DiunaBI.Plugins.Morska.Importers;
|
|
|
|
|
|
|
2025-06-02 16:54:33 +02:00
|
|
|
|
public class MorskaD3Importer : MorskaBaseImporter
|
2025-05-31 19:26:02 +02:00
|
|
|
|
{
|
2025-06-06 20:23:36 +02:00
|
|
|
|
public override string ImporterType => "Morska.Import.D3";
|
2025-06-02 20:11:29 +02:00
|
|
|
|
|
2025-06-02 16:54:33 +02:00
|
|
|
|
private readonly AppDbContext _db;
|
2025-06-02 20:11:29 +02:00
|
|
|
|
private readonly ILogger<MorskaD3Importer> _logger;
|
2025-06-02 16:54:33 +02:00
|
|
|
|
|
|
|
|
|
|
public MorskaD3Importer(
|
2025-06-02 20:11:29 +02:00
|
|
|
|
AppDbContext db,
|
|
|
|
|
|
ILogger<MorskaD3Importer> logger)
|
2025-06-02 16:54:33 +02:00
|
|
|
|
{
|
|
|
|
|
|
_db = db;
|
2025-06-02 20:11:29 +02:00
|
|
|
|
_logger = logger;
|
2025-06-02 16:54:33 +02:00
|
|
|
|
}
|
2025-06-02 20:11:29 +02:00
|
|
|
|
|
2025-06-02 16:54:33 +02:00
|
|
|
|
public override void Import(Layer importWorker)
|
2025-05-31 19:26:02 +02:00
|
|
|
|
{
|
2025-06-02 20:11:29 +02:00
|
|
|
|
_logger.LogInformation("MorskaD3: Starting import for {ImportWorkerName} ({ImportWorkerId})",
|
|
|
|
|
|
importWorker.Name, importWorker.Id);
|
|
|
|
|
|
|
2025-05-31 19:26:02 +02:00
|
|
|
|
var year = importWorker.Records!.FirstOrDefault(x => x.Code == "ImportYear")?.Desc1;
|
|
|
|
|
|
if (year == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new Exception($"ImportYear not found, {importWorker.Name}");
|
|
|
|
|
|
}
|
2025-06-02 20:11:29 +02:00
|
|
|
|
|
2025-05-31 19:26:02 +02:00
|
|
|
|
var month = importWorker.Records!.FirstOrDefault(x => x.Code == "ImportMonth")?.Desc1;
|
|
|
|
|
|
if (month == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new Exception($"ImportMonth not found, {importWorker.Name}");
|
|
|
|
|
|
}
|
2025-06-02 20:11:29 +02:00
|
|
|
|
|
2025-05-31 19:26:02 +02:00
|
|
|
|
var name = importWorker.Records!.FirstOrDefault(x => x.Code == "ImportName")?.Desc1;
|
|
|
|
|
|
if (name == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new Exception($"ImportName not found, {importWorker.Name}");
|
|
|
|
|
|
}
|
2025-06-02 20:11:29 +02:00
|
|
|
|
|
2025-05-31 19:26:02 +02:00
|
|
|
|
var type = importWorker.Records!.FirstOrDefault(x => x.Code == "ImportType")?.Desc1;
|
2025-06-02 20:11:29 +02:00
|
|
|
|
if (type == null)
|
2025-05-31 19:26:02 +02:00
|
|
|
|
{
|
|
|
|
|
|
throw new Exception($"ImportType not found, {importWorker.Name}");
|
|
|
|
|
|
}
|
2025-06-02 20:11:29 +02:00
|
|
|
|
|
|
|
|
|
|
_logger.LogDebug("MorskaD3: Looking for DataInbox with type {Type}", type);
|
|
|
|
|
|
|
2025-06-02 16:54:33 +02:00
|
|
|
|
var dataInbox = _db.DataInbox.OrderByDescending(x => x.CreatedAt).FirstOrDefault(x => x.Name == type);
|
2025-05-31 19:26:02 +02:00
|
|
|
|
if (dataInbox == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new Exception($"DataInbox not found, {type}");
|
|
|
|
|
|
}
|
2025-06-02 20:11:29 +02:00
|
|
|
|
|
|
|
|
|
|
_logger.LogDebug("MorskaD3: Found DataInbox {DataInboxId}, created at {CreatedAt}",
|
|
|
|
|
|
dataInbox.Id, dataInbox.CreatedAt);
|
|
|
|
|
|
|
|
|
|
|
|
try
|
2025-05-31 19:26:02 +02:00
|
|
|
|
{
|
2025-06-02 20:11:29 +02:00
|
|
|
|
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);
|
2025-05-31 19:26:02 +02:00
|
|
|
|
}
|
2025-06-02 20:11:29 +02:00
|
|
|
|
catch (Exception e)
|
2025-05-31 19:26:02 +02:00
|
|
|
|
{
|
2025-06-02 20:11:29 +02:00
|
|
|
|
_logger.LogError(e, "MorskaD3: Error processing DataInbox {DataInboxId}", dataInbox.Id);
|
|
|
|
|
|
throw;
|
2025-05-31 19:26:02 +02:00
|
|
|
|
}
|
2025-06-02 20:11:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void SaveRecords(Guid layerId, ICollection<Record> records)
|
|
|
|
|
|
{
|
|
|
|
|
|
var toDelete = _db.Records.Where(x => x.LayerId == layerId).ToList();
|
|
|
|
|
|
if (toDelete.Count > 0)
|
2025-05-31 19:26:02 +02:00
|
|
|
|
{
|
2025-06-02 20:11:29 +02:00
|
|
|
|
_db.Records.RemoveRange(toDelete);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var record in records)
|
2025-05-31 19:26:02 +02:00
|
|
|
|
{
|
2025-06-02 20:11:29 +02:00
|
|
|
|
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);
|
2025-05-31 19:26:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|