75 lines
2.8 KiB
C#
75 lines
2.8 KiB
C#
|
|
using System.Globalization;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.Text.Json;
|
|||
|
|
using DiunaBI.Core.Models;
|
|||
|
|
using DiunaBI.Database.Context;
|
|||
|
|
|
|||
|
|
namespace DiunaBI.Plugins.Morska.Importers;
|
|||
|
|
|
|||
|
|
public class MorskaD3Importer(
|
|||
|
|
AppDbContext db)
|
|||
|
|
{
|
|||
|
|
public void Import(Layer importWorker)
|
|||
|
|
{
|
|||
|
|
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)
|
|||
|
|
{
|
|||
|
|
throw new Exception($"ImportType not found, {importWorker.Name}");
|
|||
|
|
}
|
|||
|
|
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();
|
|||
|
|
}
|
|||
|
|
}
|