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-06-06 21:33:25 +02:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2025-05-31 19:26:02 +02:00
|
|
|
|
using DiunaBI.Core.Models;
|
2025-06-06 22:15:23 +02:00
|
|
|
|
using DiunaBI.Core.Database.Context;
|
2025-05-31 19:26:02 +02:00
|
|
|
|
|
|
|
|
|
|
namespace DiunaBI.Plugins.Morska.Importers;
|
|
|
|
|
|
|
2025-06-02 16:54:33 +02:00
|
|
|
|
public class MorskaFk2Importer : MorskaBaseImporter
|
2025-05-31 19:26:02 +02:00
|
|
|
|
{
|
2025-06-06 20:23:36 +02:00
|
|
|
|
public override string ImporterType => "Morska.Import.FK2";
|
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<MorskaFk2Importer> _logger;
|
|
|
|
|
|
|
2025-06-06 21:33:25 +02:00
|
|
|
|
// Configuration properties
|
|
|
|
|
|
private string? SheetId { get; set; }
|
|
|
|
|
|
private string? SheetTabName { get; set; }
|
|
|
|
|
|
private string? DataRange { get; set; }
|
|
|
|
|
|
private string? ImportYear { get; set; }
|
|
|
|
|
|
private string? ImportMonth { get; set; }
|
|
|
|
|
|
private string? ImportName { get; set; }
|
|
|
|
|
|
private DateTime? StartDate { get; set; }
|
|
|
|
|
|
private DateTime? EndDate { get; set; }
|
|
|
|
|
|
private bool IsEnabled { get; set; }
|
|
|
|
|
|
|
2025-06-07 13:51:27 +02:00
|
|
|
|
// Cache for sheet data
|
|
|
|
|
|
private IList<IList<object>>? _cachedSheetData;
|
|
|
|
|
|
private string? _cachedDataKey;
|
|
|
|
|
|
|
2025-06-02 16:54:33 +02:00
|
|
|
|
public MorskaFk2Importer(
|
2025-06-02 20:11:29 +02:00
|
|
|
|
AppDbContext db,
|
|
|
|
|
|
SpreadsheetsResource.ValuesResource googleSheetValues,
|
|
|
|
|
|
ILogger<MorskaFk2Importer> 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-06 21:33:25 +02:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogInformation("{ImporterType}: Starting import for {ImportWorkerName} ({ImportWorkerId})",
|
|
|
|
|
|
ImporterType, importWorker.Name, importWorker.Id);
|
|
|
|
|
|
|
2025-06-07 13:51:27 +02:00
|
|
|
|
// ✅ Clear cache at start
|
|
|
|
|
|
_cachedSheetData = null;
|
|
|
|
|
|
_cachedDataKey = null;
|
|
|
|
|
|
|
2025-06-06 21:33:25 +02:00
|
|
|
|
LoadConfiguration(importWorker);
|
|
|
|
|
|
|
|
|
|
|
|
if (!ShouldPerformImport(importWorker))
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogInformation("{ImporterType}: Import not needed for {ImportWorkerName}",
|
|
|
|
|
|
ImporterType, importWorker.Name);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ValidateConfiguration();
|
|
|
|
|
|
|
|
|
|
|
|
PerformImport(importWorker);
|
|
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("{ImporterType}: Successfully completed import for {ImportWorkerName}",
|
|
|
|
|
|
ImporterType, importWorker.Name);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogError(e, "{ImporterType}: Failed to import {ImportWorkerName} ({ImportWorkerId})",
|
|
|
|
|
|
ImporterType, importWorker.Name, importWorker.Id);
|
|
|
|
|
|
throw;
|
|
|
|
|
|
}
|
2025-06-07 13:51:27 +02:00
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
// ✅ Clear cache after import
|
|
|
|
|
|
_cachedSheetData = null;
|
|
|
|
|
|
_cachedDataKey = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ✅ Dodaj metodę cache
|
|
|
|
|
|
private IList<IList<object>>? GetSheetData()
|
|
|
|
|
|
{
|
|
|
|
|
|
var currentDataKey = $"{SheetId}#{SheetTabName}#{DataRange}";
|
|
|
|
|
|
|
|
|
|
|
|
if (_cachedSheetData != null && _cachedDataKey == currentDataKey)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogDebug("{ImporterType}: Using cached sheet data for {DataKey}",
|
|
|
|
|
|
ImporterType, currentDataKey);
|
|
|
|
|
|
return _cachedSheetData;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogDebug("{ImporterType}: Fetching data from Google Sheets API for {DataKey}",
|
|
|
|
|
|
ImporterType, currentDataKey);
|
|
|
|
|
|
|
|
|
|
|
|
var dataRangeResponse = _googleSheetValues.Get(SheetId!, $"{SheetTabName}!{DataRange}").Execute();
|
|
|
|
|
|
_cachedSheetData = dataRangeResponse.Values;
|
|
|
|
|
|
_cachedDataKey = currentDataKey;
|
|
|
|
|
|
|
|
|
|
|
|
_logger.LogDebug("{ImporterType}: Cached {RowCount} rows from Google Sheet",
|
|
|
|
|
|
ImporterType, _cachedSheetData?.Count ?? 0);
|
|
|
|
|
|
|
|
|
|
|
|
return _cachedSheetData;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogError(e, "{ImporterType}: Error fetching data from Google Sheet {SheetId}",
|
|
|
|
|
|
ImporterType, SheetId);
|
|
|
|
|
|
throw;
|
|
|
|
|
|
}
|
2025-06-06 21:33:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void LoadConfiguration(Layer importWorker)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (importWorker.Records == null) return;
|
|
|
|
|
|
|
|
|
|
|
|
SheetId = GetRecordValue(importWorker.Records, "SheetId");
|
|
|
|
|
|
SheetTabName = GetRecordValue(importWorker.Records, "SheetTabName");
|
|
|
|
|
|
DataRange = GetRecordValue(importWorker.Records, "DataRange");
|
|
|
|
|
|
ImportYear = GetRecordValue(importWorker.Records, "ImportYear");
|
|
|
|
|
|
ImportMonth = GetRecordValue(importWorker.Records, "ImportMonth");
|
|
|
|
|
|
ImportName = GetRecordValue(importWorker.Records, "ImportName");
|
|
|
|
|
|
IsEnabled = GetRecordValue(importWorker.Records, "IsEnabled") == "True";
|
|
|
|
|
|
|
|
|
|
|
|
var startDateStr = GetRecordValue(importWorker.Records, "StartDate");
|
|
|
|
|
|
if (startDateStr != null && DateTime.TryParseExact(startDateStr, "yyyy.MM.dd", null, DateTimeStyles.None, out var startDate))
|
|
|
|
|
|
{
|
|
|
|
|
|
StartDate = startDate;
|
|
|
|
|
|
}
|
2025-06-02 20:11:29 +02:00
|
|
|
|
|
2025-06-06 21:33:25 +02:00
|
|
|
|
var endDateStr = GetRecordValue(importWorker.Records, "EndDate");
|
|
|
|
|
|
if (endDateStr != null && DateTime.TryParseExact(endDateStr, "yyyy.MM.dd", null, DateTimeStyles.None, out var endDate))
|
2025-05-31 19:26:02 +02:00
|
|
|
|
{
|
2025-06-06 21:33:25 +02:00
|
|
|
|
EndDate = endDate;
|
2025-05-31 19:26:02 +02:00
|
|
|
|
}
|
2025-06-02 20:11:29 +02:00
|
|
|
|
|
2025-06-06 21:33:25 +02:00
|
|
|
|
_logger.LogDebug("{ImporterType}: Configuration loaded for {ImportWorkerName}",
|
|
|
|
|
|
ImporterType, importWorker.Name);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private bool ShouldPerformImport(Layer importWorker)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!IsEnabled)
|
2025-05-31 19:26:02 +02:00
|
|
|
|
{
|
2025-06-06 21:33:25 +02:00
|
|
|
|
_logger.LogDebug("{ImporterType}: Import disabled for {ImportWorkerName}",
|
|
|
|
|
|
ImporterType, importWorker.Name);
|
|
|
|
|
|
return false;
|
2025-05-31 19:26:02 +02:00
|
|
|
|
}
|
2025-06-02 20:11:29 +02:00
|
|
|
|
|
2025-06-06 21:33:25 +02:00
|
|
|
|
if (StartDate.HasValue && EndDate.HasValue)
|
2025-05-31 19:26:02 +02:00
|
|
|
|
{
|
2025-06-06 21:33:25 +02:00
|
|
|
|
var now = DateTime.UtcNow.Date;
|
|
|
|
|
|
if (now >= StartDate.Value.Date && now <= EndDate.Value.Date)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogDebug("{ImporterType}: Within date range, import needed for {ImportWorkerName}",
|
|
|
|
|
|
ImporterType, importWorker.Name);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!IsImportedLayerUpToDate(importWorker))
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogDebug("{ImporterType}: Outside date range but layer is out of date, import needed for {ImportWorkerName}",
|
|
|
|
|
|
ImporterType, importWorker.Name);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_logger.LogDebug("{ImporterType}: Outside date range and layer is up to date for {ImportWorkerName}",
|
|
|
|
|
|
ImporterType, importWorker.Name);
|
|
|
|
|
|
return false;
|
2025-05-31 19:26:02 +02:00
|
|
|
|
}
|
2025-06-02 20:11:29 +02:00
|
|
|
|
|
2025-06-06 21:33:25 +02:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void ValidateConfiguration()
|
|
|
|
|
|
{
|
|
|
|
|
|
var errors = new List<string>();
|
|
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(SheetId)) errors.Add("SheetId is required");
|
|
|
|
|
|
if (string.IsNullOrEmpty(SheetTabName)) errors.Add("SheetTabName is required");
|
|
|
|
|
|
if (string.IsNullOrEmpty(DataRange)) errors.Add("DataRange is required");
|
|
|
|
|
|
if (string.IsNullOrEmpty(ImportYear)) errors.Add("ImportYear is required");
|
|
|
|
|
|
if (string.IsNullOrEmpty(ImportMonth)) errors.Add("ImportMonth is required");
|
|
|
|
|
|
if (string.IsNullOrEmpty(ImportName)) errors.Add("ImportName is required");
|
|
|
|
|
|
|
|
|
|
|
|
if (errors.Any())
|
2025-05-31 19:26:02 +02:00
|
|
|
|
{
|
2025-06-06 21:33:25 +02:00
|
|
|
|
throw new InvalidOperationException($"Configuration validation failed: {string.Join(", ", errors)}");
|
2025-05-31 19:26:02 +02:00
|
|
|
|
}
|
2025-06-06 21:33:25 +02:00
|
|
|
|
}
|
2025-06-02 20:11:29 +02:00
|
|
|
|
|
2025-06-06 21:33:25 +02:00
|
|
|
|
private bool IsImportedLayerUpToDate(Layer importWorker)
|
|
|
|
|
|
{
|
|
|
|
|
|
var newestLayer = _db.Layers
|
|
|
|
|
|
.Include(x => x.Records)
|
|
|
|
|
|
.Where(x => x.ParentId == importWorker.Id)
|
|
|
|
|
|
.OrderByDescending(x => x.CreatedAt)
|
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
|
|
if (newestLayer == null)
|
2025-05-31 19:26:02 +02:00
|
|
|
|
{
|
2025-06-06 21:33:25 +02:00
|
|
|
|
_logger.LogDebug("{ImporterType}: No child layers found for {ImportWorkerName}, treating as up to date",
|
|
|
|
|
|
ImporterType, importWorker.Name);
|
|
|
|
|
|
return true;
|
2025-05-31 19:26:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-06 21:33:25 +02:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-06-07 13:51:27 +02:00
|
|
|
|
// ✅ Użyj cache zamiast bezpośredniego API
|
|
|
|
|
|
var data = GetSheetData();
|
2025-06-06 21:33:25 +02:00
|
|
|
|
|
|
|
|
|
|
if (data == null || data.Count == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogWarning("{ImporterType}: No data found in sheet for {ImportWorkerName}",
|
|
|
|
|
|
ImporterType, importWorker.Name);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var isUpToDate = true;
|
|
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < data.Count; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (data[i].Count <= 9 || string.IsNullOrEmpty(data[i][3]?.ToString())) continue;
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var dateArr = data[i][1].ToString()!.Split(".");
|
|
|
|
|
|
if (dateArr.Length != 3) continue;
|
|
|
|
|
|
|
|
|
|
|
|
var number = data[i][2].ToString()!;
|
|
|
|
|
|
if (number.Length == 1) number = $"0{number}";
|
|
|
|
|
|
var code = dateArr[2] + dateArr[1] + dateArr[0] + number;
|
|
|
|
|
|
|
|
|
|
|
|
var record = newestLayer.Records?.FirstOrDefault(x => x.Code == code);
|
|
|
|
|
|
if (record == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogDebug("{ImporterType}: Code {Code} not found in database for {ImportWorkerName}",
|
|
|
|
|
|
ImporterType, code, importWorker.Name);
|
|
|
|
|
|
isUpToDate = false;
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (double.TryParse(data[i][9]?.ToString(), CultureInfo.GetCultureInfo("pl-PL"), out var value) &&
|
|
|
|
|
|
Math.Abs((double)(record.Value1 - value)!) >= 0.01)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogDebug("{ImporterType}: Value mismatch for code {Code}: DB={DbValue}, Sheet={SheetValue}",
|
|
|
|
|
|
ImporterType, code, record.Value1, value);
|
|
|
|
|
|
isUpToDate = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (record.Desc1 != data[i][3]?.ToString())
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogDebug("{ImporterType}: Description mismatch for code {Code}: DB={DbDesc}, Sheet={SheetDesc}",
|
|
|
|
|
|
ImporterType, code, record.Desc1, data[i][3]?.ToString());
|
|
|
|
|
|
isUpToDate = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogWarning(ex, "{ImporterType}: Error processing row {Index} for comparison",
|
|
|
|
|
|
ImporterType, i);
|
|
|
|
|
|
isUpToDate = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_logger.LogDebug("{ImporterType}: Layer {ImportWorkerName} is {Status}",
|
|
|
|
|
|
ImporterType, importWorker.Name, isUpToDate ? "up to date" : "outdated");
|
|
|
|
|
|
|
|
|
|
|
|
return isUpToDate;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
2025-05-31 19:26:02 +02:00
|
|
|
|
{
|
2025-06-06 21:33:25 +02:00
|
|
|
|
_logger.LogError(e, "{ImporterType}: Error checking if layer {ImportWorkerName} is up to date",
|
|
|
|
|
|
ImporterType, importWorker.Name);
|
|
|
|
|
|
throw;
|
2025-05-31 19:26:02 +02:00
|
|
|
|
}
|
2025-06-06 21:33:25 +02:00
|
|
|
|
}
|
2025-05-31 19:26:02 +02:00
|
|
|
|
|
2025-06-06 21:33:25 +02:00
|
|
|
|
private void PerformImport(Layer importWorker)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogDebug("{ImporterType}: Importing from sheet {SheetId}, tab {SheetTabName}, range {DataRange}",
|
|
|
|
|
|
ImporterType, SheetId, SheetTabName, DataRange);
|
2025-06-02 20:11:29 +02:00
|
|
|
|
|
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
|
|
|
|
|
|
};
|
2025-06-06 21:33:25 +02:00
|
|
|
|
layer.Name = $"L{layer.Number}-I-{ImportName}-{ImportYear}/{ImportMonth}-{DateTime.Now:yyyyMMddHHmm}";
|
2025-05-31 19:26:02 +02:00
|
|
|
|
|
|
|
|
|
|
var newRecords = new List<Record>();
|
|
|
|
|
|
|
2025-06-02 20:11:29 +02:00
|
|
|
|
try
|
2025-05-31 19:26:02 +02:00
|
|
|
|
{
|
2025-06-07 13:51:27 +02:00
|
|
|
|
// ✅ Użyj cache zamiast bezpośredniego API
|
|
|
|
|
|
var data = GetSheetData();
|
2025-06-02 20:11:29 +02:00
|
|
|
|
|
2025-06-07 13:51:27 +02:00
|
|
|
|
_logger.LogDebug("{ImporterType}: Using data with {RowCount} rows from cache",
|
2025-06-06 21:33:25 +02:00
|
|
|
|
ImporterType, data?.Count ?? 0);
|
2025-06-02 20:11:29 +02:00
|
|
|
|
|
|
|
|
|
|
if (data != null)
|
2025-05-31 19:26:02 +02:00
|
|
|
|
{
|
2025-06-02 20:11:29 +02:00
|
|
|
|
for (var i = 0; i < data.Count; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (data[i].Count <= 9 || string.IsNullOrEmpty(data[i][3]?.ToString()))
|
|
|
|
|
|
{
|
2025-06-06 21:33:25 +02:00
|
|
|
|
_logger.LogDebug("{ImporterType}: Skipping row {Index} - insufficient columns or empty desc",
|
|
|
|
|
|
ImporterType, i);
|
2025-06-02 20:11:29 +02:00
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var dateArr = data[i][1].ToString()!.Split(".");
|
|
|
|
|
|
if (dateArr.Length != 3)
|
|
|
|
|
|
{
|
2025-06-06 21:33:25 +02:00
|
|
|
|
_logger.LogWarning("{ImporterType}: Invalid date format in row {Index}: {Date}",
|
|
|
|
|
|
ImporterType, i, data[i][1]);
|
|
|
|
|
|
throw new InvalidOperationException($"Invalid date in row {i}");
|
2025-06-02 20:11:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var number = data[i][2].ToString()!;
|
|
|
|
|
|
if (number.Length == 1) number = $"0{number}";
|
|
|
|
|
|
var code = dateArr[2] + dateArr[1] + dateArr[0] + number;
|
|
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(data[i][9]?.ToString()) ||
|
|
|
|
|
|
!double.TryParse(data[i][9].ToString(), CultureInfo.GetCultureInfo("pl-PL"), out var value))
|
|
|
|
|
|
{
|
2025-06-06 21:33:25 +02:00
|
|
|
|
_logger.LogDebug("{ImporterType}: Skipping row {Index} - empty or invalid value",
|
|
|
|
|
|
ImporterType, i);
|
2025-06-02 20:11:29 +02:00
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var record = new Record
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = Guid.NewGuid(),
|
|
|
|
|
|
Code = code,
|
|
|
|
|
|
Desc1 = data[i][3].ToString(),
|
|
|
|
|
|
Value1 = value,
|
|
|
|
|
|
CreatedAt = DateTime.UtcNow,
|
|
|
|
|
|
ModifiedAt = DateTime.UtcNow
|
|
|
|
|
|
};
|
|
|
|
|
|
newRecords.Add(record);
|
|
|
|
|
|
}
|
2025-05-31 19:26:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-02 20:11:29 +02:00
|
|
|
|
_db.Layers.Add(layer);
|
|
|
|
|
|
SaveRecords(layer.Id, newRecords);
|
|
|
|
|
|
_db.SaveChanges();
|
|
|
|
|
|
|
2025-06-06 21:33:25 +02:00
|
|
|
|
_logger.LogInformation("{ImporterType}: Successfully imported {RecordCount} records for layer {LayerName} ({LayerId})",
|
|
|
|
|
|
ImporterType, newRecords.Count, layer.Name, layer.Id);
|
2025-06-02 20:11:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
2025-06-07 13:51:27 +02:00
|
|
|
|
_logger.LogError(e, "{ImporterType}: Error importing data from cached sheet data", ImporterType);
|
2025-06-02 20:11:29 +02:00
|
|
|
|
throw;
|
2025-05-31 19:26:02 +02:00
|
|
|
|
}
|
2025-06-02 20:11:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-06 21:33:25 +02:00
|
|
|
|
private string? GetRecordValue(ICollection<Record> records, string code)
|
|
|
|
|
|
{
|
|
|
|
|
|
return records.FirstOrDefault(x => x.Code == code)?.Desc1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
{
|
|
|
|
|
|
_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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-06 21:33:25 +02:00
|
|
|
|
_logger.LogDebug("{ImporterType}: Saved {RecordCount} records for layer {LayerId}",
|
|
|
|
|
|
ImporterType, records.Count, layerId);
|
2025-05-31 19:26:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|