2025-05-31 19:26:02 +02:00
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
using Google.Apis.Sheets.v4;
|
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 MorskaImporter : MorskaBaseImporter
|
2025-05-31 19:26:02 +02:00
|
|
|
|
{
|
2025-06-02 16:54:33 +02:00
|
|
|
|
public override string ImporterType => "MorskaImporter";
|
2025-06-02 20:11:29 +02:00
|
|
|
|
|
2025-06-02 16:54:33 +02:00
|
|
|
|
private readonly AppDbContext _db;
|
|
|
|
|
|
private readonly SpreadsheetsResource.ValuesResource _googleSheetValues;
|
2025-06-02 20:11:29 +02:00
|
|
|
|
private readonly ILogger<MorskaImporter> _logger;
|
|
|
|
|
|
|
2025-06-02 16:54:33 +02:00
|
|
|
|
public MorskaImporter(
|
|
|
|
|
|
AppDbContext db,
|
2025-06-02 20:11:29 +02:00
|
|
|
|
SpreadsheetsResource.ValuesResource googleSheetValues,
|
|
|
|
|
|
ILogger<MorskaImporter> logger)
|
2025-06-02 16:54:33 +02:00
|
|
|
|
{
|
|
|
|
|
|
_db = db;
|
|
|
|
|
|
_googleSheetValues = googleSheetValues;
|
2025-06-02 20:11:29 +02:00
|
|
|
|
_logger = logger;
|
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("MorskaImporter: Starting import for {ImportWorkerName} ({ImportWorkerId})",
|
|
|
|
|
|
importWorker.Name, importWorker.Id);
|
|
|
|
|
|
|
2025-05-31 19:26:02 +02:00
|
|
|
|
var sheetId = importWorker.Records!.FirstOrDefault(x => x.Code == "SheetId")?.Desc1;
|
|
|
|
|
|
if (sheetId == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new Exception($"SheetId not found, {importWorker.Name}");
|
|
|
|
|
|
}
|
2025-06-02 20:11:29 +02:00
|
|
|
|
|
2025-05-31 19:26:02 +02:00
|
|
|
|
var sheetTabName = importWorker.Records!.FirstOrDefault(x => x.Code == "SheetTabName")?.Desc1;
|
|
|
|
|
|
if (sheetTabName == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new Exception($"SheetTabName not found, {importWorker.Name}");
|
|
|
|
|
|
}
|
2025-06-02 20:11:29 +02:00
|
|
|
|
|
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}");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var dataRange = importWorker.Records!.FirstOrDefault(x => x.Code == "DataRange")?.Desc1;
|
|
|
|
|
|
if (dataRange == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new Exception($"DataRange not found, {importWorker.Name}");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-02 20:11:29 +02:00
|
|
|
|
_logger.LogDebug("MorskaImporter: Importing from sheet {SheetId}, tab {SheetTabName}, range {DataRange}",
|
|
|
|
|
|
sheetId, sheetTabName, dataRange);
|
|
|
|
|
|
|
2025-05-31 19:26:02 +02:00
|
|
|
|
var layer = new Layer
|
|
|
|
|
|
{
|
2025-06-02 20:11:29 +02:00
|
|
|
|
Id = Guid.NewGuid(),
|
2025-06-02 16:54:33 +02:00
|
|
|
|
Number = _db.Layers.Count() + 1,
|
2025-05-31 19:26:02 +02:00
|
|
|
|
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}";
|
|
|
|
|
|
|
|
|
|
|
|
var newRecords = new List<Record>();
|
|
|
|
|
|
|
2025-06-02 20:11:29 +02:00
|
|
|
|
try
|
2025-05-31 19:26:02 +02:00
|
|
|
|
{
|
2025-06-02 20:11:29 +02:00
|
|
|
|
var dataRangeResponse = _googleSheetValues.Get(sheetId, $"{sheetTabName}!{dataRange}").Execute();
|
|
|
|
|
|
var data = dataRangeResponse.Values;
|
|
|
|
|
|
|
|
|
|
|
|
_logger.LogDebug("MorskaImporter: Retrieved {RowCount} rows from Google Sheet", data?.Count ?? 0);
|
|
|
|
|
|
|
|
|
|
|
|
if (data != null && data.Count >= 2)
|
2025-05-31 19:26:02 +02:00
|
|
|
|
{
|
2025-06-02 20:11:29 +02:00
|
|
|
|
for (var i = 0; i < data[1].Count; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!(data[0][i].ToString()?.Length > 0) ||
|
|
|
|
|
|
!double.TryParse(data[1][i].ToString(), CultureInfo.GetCultureInfo("pl-PL"), out var value))
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogDebug("MorskaImporter: Skipping column {Index} - empty code or invalid value", i);
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var record = new Record
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = Guid.NewGuid(),
|
|
|
|
|
|
Code = data[0][i].ToString(),
|
|
|
|
|
|
Value1 = value,
|
|
|
|
|
|
CreatedAt = DateTime.UtcNow,
|
|
|
|
|
|
ModifiedAt = DateTime.UtcNow
|
|
|
|
|
|
};
|
|
|
|
|
|
newRecords.Add(record);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_db.Layers.Add(layer);
|
|
|
|
|
|
|
|
|
|
|
|
SaveRecords(layer.Id, newRecords);
|
|
|
|
|
|
_db.SaveChanges();
|
|
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("MorskaImporter: Successfully imported {RecordCount} records for layer {LayerName} ({LayerId})",
|
|
|
|
|
|
newRecords.Count, layer.Name, layer.Id);
|
2025-05-31 19:26:02 +02:00
|
|
|
|
}
|
2025-06-02 20:11:29 +02:00
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogError(e, "MorskaImporter: Error importing data from Google Sheet {SheetId}", sheetId);
|
|
|
|
|
|
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("MorskaImporter: Saved {RecordCount} records for layer {LayerId}", records.Count, layerId);
|
2025-05-31 19:26:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|