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-06-06 20:49:09 +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-07-26 11:11:24 +02:00
|
|
|
|
using DiunaBI.Core.Services;
|
|
|
|
|
|
using Google.Apis.Sheets.v4;
|
|
|
|
|
|
using Google.Apis.Sheets.v4.Data;
|
2025-05-31 19:26:02 +02:00
|
|
|
|
|
|
|
|
|
|
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-07-26 11:11:24 +02:00
|
|
|
|
|
2025-06-02 16:54:33 +02:00
|
|
|
|
private readonly AppDbContext _db;
|
2025-07-26 11:11:24 +02:00
|
|
|
|
private readonly SpreadsheetsResource.ValuesResource _googleSheetValues;
|
2025-06-02 20:11:29 +02:00
|
|
|
|
private readonly ILogger<MorskaD3Importer> _logger;
|
2025-06-02 16:54:33 +02:00
|
|
|
|
|
2025-06-06 20:49:09 +02:00
|
|
|
|
// Configuration properties
|
|
|
|
|
|
private string? ImportYear { get; set; }
|
|
|
|
|
|
private string? ImportMonth { get; set; }
|
|
|
|
|
|
private string? ImportName { get; set; }
|
|
|
|
|
|
private string? ImportType { get; set; }
|
2025-07-26 11:11:24 +02:00
|
|
|
|
private string? SheetId { get; set; }
|
2025-06-06 20:49:09 +02:00
|
|
|
|
private bool IsEnabled { get; set; }
|
|
|
|
|
|
|
2025-07-26 11:11:24 +02:00
|
|
|
|
// Cached deserialized data
|
|
|
|
|
|
private List<Record>? _cachedRecords;
|
|
|
|
|
|
private string? _cachedDataInboxId;
|
|
|
|
|
|
|
2025-06-02 16:54:33 +02:00
|
|
|
|
public MorskaD3Importer(
|
2025-06-02 20:11:29 +02:00
|
|
|
|
AppDbContext db,
|
2025-07-26 11:11:24 +02:00
|
|
|
|
SpreadsheetsResource.ValuesResource googleSheetValues,
|
2025-06-02 20:11:29 +02:00
|
|
|
|
ILogger<MorskaD3Importer> logger)
|
2025-06-02 16:54:33 +02:00
|
|
|
|
{
|
|
|
|
|
|
_db = db;
|
2025-07-26 11:11:24 +02:00
|
|
|
|
_googleSheetValues = googleSheetValues;
|
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-06 20:49:09 +02:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-07-26 11:11:24 +02:00
|
|
|
|
_logger.LogInformation("{ImporterType}: Starting import for {ImportWorkerName} ({ImportWorkerId})",
|
2025-06-06 20:49:09 +02:00
|
|
|
|
ImporterType, importWorker.Name, importWorker.Id);
|
|
|
|
|
|
|
2025-07-26 11:11:24 +02:00
|
|
|
|
// Clear cache at start
|
|
|
|
|
|
_cachedRecords = null;
|
|
|
|
|
|
_cachedDataInboxId = null;
|
|
|
|
|
|
|
2025-06-06 20:49:09 +02:00
|
|
|
|
LoadConfiguration(importWorker);
|
2025-07-26 11:11:24 +02:00
|
|
|
|
|
|
|
|
|
|
// Deserialize data early - right after LoadConfiguration
|
|
|
|
|
|
DeserializeDataInboxData();
|
|
|
|
|
|
|
2025-06-06 20:49:09 +02:00
|
|
|
|
if (!ShouldPerformImport(importWorker))
|
|
|
|
|
|
{
|
2025-07-26 11:11:24 +02:00
|
|
|
|
_logger.LogInformation("{ImporterType}: Import not needed for {ImportWorkerName}",
|
2025-06-06 20:49:09 +02:00
|
|
|
|
ImporterType, importWorker.Name);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ValidateConfiguration();
|
2025-06-02 20:11:29 +02:00
|
|
|
|
|
2025-06-06 20:49:09 +02:00
|
|
|
|
PerformImport(importWorker);
|
2025-07-26 11:11:24 +02:00
|
|
|
|
|
|
|
|
|
|
// Export to Google Sheets after successful import
|
|
|
|
|
|
ExportToGoogleSheets();
|
|
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("{ImporterType}: Successfully completed import for {ImportWorkerName}",
|
2025-06-06 20:49:09 +02:00
|
|
|
|
ImporterType, importWorker.Name);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
2025-05-31 19:26:02 +02:00
|
|
|
|
{
|
2025-07-26 11:11:24 +02:00
|
|
|
|
_logger.LogError(e, "{ImporterType}: Failed to import {ImportWorkerName} ({ImportWorkerId})",
|
2025-06-06 20:49:09 +02:00
|
|
|
|
ImporterType, importWorker.Name, importWorker.Id);
|
|
|
|
|
|
throw;
|
2025-05-31 19:26:02 +02:00
|
|
|
|
}
|
2025-07-26 11:11:24 +02:00
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
// Clear cache after import
|
|
|
|
|
|
_cachedRecords = null;
|
|
|
|
|
|
_cachedDataInboxId = null;
|
|
|
|
|
|
}
|
2025-06-06 20:49:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void LoadConfiguration(Layer importWorker)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (importWorker.Records == null) return;
|
|
|
|
|
|
|
|
|
|
|
|
ImportYear = GetRecordValue(importWorker.Records, "ImportYear");
|
|
|
|
|
|
ImportMonth = GetRecordValue(importWorker.Records, "ImportMonth");
|
|
|
|
|
|
ImportName = GetRecordValue(importWorker.Records, "ImportName");
|
|
|
|
|
|
ImportType = GetRecordValue(importWorker.Records, "ImportType");
|
2025-07-26 11:11:24 +02:00
|
|
|
|
SheetId = GetRecordValue(importWorker.Records, "SheetId");
|
2025-06-06 20:49:09 +02:00
|
|
|
|
IsEnabled = GetRecordValue(importWorker.Records, "IsEnabled") == "True";
|
2025-06-02 20:11:29 +02:00
|
|
|
|
|
2025-07-26 11:11:24 +02:00
|
|
|
|
_logger.LogDebug(
|
|
|
|
|
|
"{ImporterType}: Configuration loaded for {ImportWorkerName} - Type: {ImportType}, SheetId: {SheetId}",
|
|
|
|
|
|
ImporterType, importWorker.Name, ImportType, SheetId);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void DeserializeDataInboxData()
|
|
|
|
|
|
{
|
|
|
|
|
|
var dataInbox = _db.DataInbox.OrderByDescending(x => x.CreatedAt).FirstOrDefault(x => x.Name == ImportType);
|
|
|
|
|
|
if (dataInbox == null)
|
2025-05-31 19:26:02 +02:00
|
|
|
|
{
|
2025-07-26 11:11:24 +02:00
|
|
|
|
throw new InvalidOperationException($"DataInbox not found for type: {ImportType}");
|
2025-05-31 19:26:02 +02:00
|
|
|
|
}
|
2025-06-02 20:11:29 +02:00
|
|
|
|
|
2025-07-26 11:11:24 +02:00
|
|
|
|
_logger.LogDebug("{ImporterType}: Found DataInbox {DataInboxId}, created at {CreatedAt}",
|
|
|
|
|
|
ImporterType, dataInbox.Id, dataInbox.CreatedAt);
|
|
|
|
|
|
|
|
|
|
|
|
try
|
2025-05-31 19:26:02 +02:00
|
|
|
|
{
|
2025-07-26 11:11:24 +02:00
|
|
|
|
var data = Convert.FromBase64String(dataInbox.Data);
|
|
|
|
|
|
var jsonString = Encoding.UTF8.GetString(data);
|
|
|
|
|
|
|
|
|
|
|
|
_logger.LogDebug("{ImporterType}: Decoded {DataSize} bytes from base64",
|
|
|
|
|
|
ImporterType, data.Length);
|
|
|
|
|
|
|
|
|
|
|
|
var records = JsonSerializer.Deserialize<List<Record>>(jsonString);
|
|
|
|
|
|
if (records == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new InvalidOperationException($"DataInbox.Data is empty for: {dataInbox.Name}");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_logger.LogDebug("{ImporterType}: Deserialized {RecordCount} records from JSON",
|
|
|
|
|
|
ImporterType, records.Count);
|
|
|
|
|
|
|
|
|
|
|
|
// Cache the deserialized data
|
|
|
|
|
|
_cachedRecords = records;
|
|
|
|
|
|
_cachedDataInboxId = dataInbox.Id.ToString();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (FormatException e)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogError(e, "{ImporterType}: Invalid base64 data in DataInbox {DataInboxName}",
|
|
|
|
|
|
ImporterType, ImportType);
|
|
|
|
|
|
throw new InvalidOperationException($"Invalid base64 data in DataInbox: {ImportType}", e);
|
2025-05-31 19:26:02 +02:00
|
|
|
|
}
|
2025-07-26 11:11:24 +02:00
|
|
|
|
catch (JsonException e)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogError(e, "{ImporterType}: Invalid JSON data in DataInbox {DataInboxName}",
|
|
|
|
|
|
ImporterType, ImportType);
|
|
|
|
|
|
throw new InvalidOperationException($"Invalid JSON data in DataInbox: {ImportType}", e);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-06-02 20:11:29 +02:00
|
|
|
|
|
2025-07-26 11:11:24 +02:00
|
|
|
|
private List<Record> GetFilteredRecords()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_cachedRecords == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new InvalidOperationException("Data has not been deserialized yet");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var filteredRecords = _cachedRecords.Where(x => x.Code!.StartsWith($"{ImportYear}{ImportMonth}")).ToList();
|
|
|
|
|
|
if (filteredRecords.Count == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new InvalidOperationException($"No records found for period: {ImportYear}{ImportMonth}");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_logger.LogDebug("{ImporterType}: Filtered to {FilteredCount} records for period {Year}{Month}",
|
|
|
|
|
|
ImporterType, filteredRecords.Count, ImportYear, ImportMonth);
|
|
|
|
|
|
|
|
|
|
|
|
return filteredRecords;
|
2025-06-06 20:49:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private bool ShouldPerformImport(Layer importWorker)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!IsEnabled)
|
2025-05-31 19:26:02 +02:00
|
|
|
|
{
|
2025-07-26 11:11:24 +02:00
|
|
|
|
_logger.LogDebug("{ImporterType}: Import disabled for {ImportWorkerName}",
|
2025-06-06 20:49:09 +02:00
|
|
|
|
ImporterType, importWorker.Name);
|
|
|
|
|
|
return false;
|
2025-05-31 19:26:02 +02:00
|
|
|
|
}
|
2025-06-02 20:11:29 +02:00
|
|
|
|
|
2025-07-26 11:11:24 +02:00
|
|
|
|
var dataInbox = _db.DataInbox.OrderByDescending(x => x.CreatedAt).FirstOrDefault(x => x.Name == ImportType);
|
|
|
|
|
|
if (dataInbox == null)
|
2025-06-06 20:49:09 +02:00
|
|
|
|
{
|
2025-07-26 11:11:24 +02:00
|
|
|
|
_logger.LogDebug("{ImporterType}: No DataInbox found for type {ImportType}",
|
|
|
|
|
|
ImporterType, ImportType);
|
2025-06-06 20:49:09 +02:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2025-07-26 11:11:24 +02:00
|
|
|
|
|
|
|
|
|
|
// Check if imported layer is up to date
|
|
|
|
|
|
if (!IsImportedLayerUpToDate(importWorker))
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogDebug("{ImporterType}: Layer is out of date, import needed for {ImportWorkerName}",
|
|
|
|
|
|
ImporterType, importWorker.Name);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2025-06-06 20:49:09 +02:00
|
|
|
|
|
2025-07-26 11:11:24 +02:00
|
|
|
|
_logger.LogDebug("{ImporterType}: Layer is up to date for {ImportWorkerName}",
|
|
|
|
|
|
ImporterType, importWorker.Name);
|
|
|
|
|
|
return false;
|
2025-06-06 20:49:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void ValidateConfiguration()
|
|
|
|
|
|
{
|
|
|
|
|
|
var errors = new List<string>();
|
2025-06-02 20:11:29 +02:00
|
|
|
|
|
2025-06-06 20:49:09 +02:00
|
|
|
|
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 (string.IsNullOrEmpty(ImportType)) errors.Add("ImportType is required");
|
2025-07-26 11:11:24 +02:00
|
|
|
|
if (string.IsNullOrEmpty(SheetId)) errors.Add("SheetId is required");
|
2025-06-06 20:49:09 +02:00
|
|
|
|
|
|
|
|
|
|
if (errors.Any())
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new InvalidOperationException($"Configuration validation failed: {string.Join(", ", errors)}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private bool IsImportedLayerUpToDate(Layer importWorker)
|
|
|
|
|
|
{
|
|
|
|
|
|
var newestLayer = _db.Layers
|
|
|
|
|
|
.Include(x => x.Records)
|
2025-07-26 11:11:24 +02:00
|
|
|
|
.Where(x =>
|
|
|
|
|
|
x.ParentId == importWorker.Id
|
|
|
|
|
|
&& x.Name!.Contains($"I-{ImportName}-{ImportYear}/{ImportMonth}"))
|
2025-06-06 20:49:09 +02:00
|
|
|
|
.OrderByDescending(x => x.CreatedAt)
|
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
|
|
if (newestLayer == null)
|
|
|
|
|
|
{
|
2025-07-26 11:11:24 +02:00
|
|
|
|
_logger.LogDebug("{ImporterType}: No child layers found for {ImportWorkerName}, import needed",
|
2025-06-06 20:49:09 +02:00
|
|
|
|
ImporterType, importWorker.Name);
|
2025-07-26 11:11:24 +02:00
|
|
|
|
return false;
|
2025-06-06 20:49:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-07-26 11:11:24 +02:00
|
|
|
|
var currentRecords = GetFilteredRecords();
|
|
|
|
|
|
|
|
|
|
|
|
// Compare record counts first
|
|
|
|
|
|
if (newestLayer.Records?.Count != currentRecords.Count)
|
2025-06-06 20:49:09 +02:00
|
|
|
|
{
|
2025-07-26 11:11:24 +02:00
|
|
|
|
_logger.LogDebug("{ImporterType}: Record count mismatch - DB: {DbCount}, DataInbox: {DataCount}",
|
|
|
|
|
|
ImporterType, newestLayer.Records?.Count ?? 0, currentRecords.Count);
|
|
|
|
|
|
return false;
|
2025-06-06 20:49:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-26 11:11:24 +02:00
|
|
|
|
// Compare individual records
|
|
|
|
|
|
foreach (var currentRecord in currentRecords)
|
|
|
|
|
|
{
|
|
|
|
|
|
var existingRecord = newestLayer.Records?.FirstOrDefault(x => x.Code == currentRecord.Code);
|
|
|
|
|
|
if (existingRecord == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogDebug("{ImporterType}: Record with code {Code} not found in existing layer",
|
|
|
|
|
|
ImporterType, currentRecord.Code);
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Compare all relevant fields
|
|
|
|
|
|
if (Math.Abs((existingRecord.Value1 ?? 0) - (currentRecord.Value1 ?? 0)) > 0.001 ||
|
|
|
|
|
|
existingRecord.Desc1 != currentRecord.Desc1)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogDebug("{ImporterType}: Data difference found for code {Code}",
|
|
|
|
|
|
ImporterType, currentRecord.Code);
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-06-06 20:49:09 +02:00
|
|
|
|
|
2025-07-26 11:11:24 +02:00
|
|
|
|
_logger.LogDebug("{ImporterType}: All records match, layer is up to date for {ImportWorkerName}",
|
|
|
|
|
|
ImporterType, importWorker.Name);
|
|
|
|
|
|
return true;
|
2025-06-06 20:49:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
2025-07-26 11:11:24 +02:00
|
|
|
|
_logger.LogError(e, "{ImporterType}: Error checking if layer {ImportWorkerName} is up to date",
|
2025-06-06 20:49:09 +02:00
|
|
|
|
ImporterType, importWorker.Name);
|
2025-07-26 11:11:24 +02:00
|
|
|
|
return false;
|
2025-06-06 20:49:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void PerformImport(Layer importWorker)
|
|
|
|
|
|
{
|
2025-07-26 11:11:24 +02:00
|
|
|
|
_logger.LogDebug("{ImporterType}: Starting import for DataInbox type {ImportType}",
|
2025-06-06 20:49:09 +02:00
|
|
|
|
ImporterType, ImportType);
|
|
|
|
|
|
|
2025-07-26 11:11:24 +02:00
|
|
|
|
var filteredRecords = GetFilteredRecords();
|
|
|
|
|
|
|
|
|
|
|
|
// Prepare records for saving
|
|
|
|
|
|
var recordsToSave = filteredRecords.Select(x =>
|
2025-05-31 19:26:02 +02:00
|
|
|
|
{
|
2025-07-26 11:11:24 +02:00
|
|
|
|
x.Id = Guid.NewGuid();
|
|
|
|
|
|
x.CreatedAt = DateTime.UtcNow;
|
|
|
|
|
|
x.ModifiedAt = DateTime.UtcNow;
|
|
|
|
|
|
return x;
|
|
|
|
|
|
}).ToList();
|
2025-06-02 20:11:29 +02:00
|
|
|
|
|
2025-07-26 11:11:24 +02:00
|
|
|
|
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-{ImportName}-{ImportYear}/{ImportMonth}-{DateTime.Now:yyyyMMddHHmm}";
|
|
|
|
|
|
|
|
|
|
|
|
_db.Layers.Add(layer);
|
|
|
|
|
|
SaveRecords(layer.Id, recordsToSave);
|
|
|
|
|
|
_db.SaveChanges();
|
|
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation(
|
|
|
|
|
|
"{ImporterType}: Successfully imported {RecordCount} records for layer {LayerName} ({LayerId})",
|
|
|
|
|
|
ImporterType, recordsToSave.Count, layer.Name, layer.Id);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void ExportToGoogleSheets()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrEmpty(SheetId))
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogWarning("{ImporterType}: SheetId not configured, skipping Google Sheets export",
|
|
|
|
|
|
ImporterType);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2025-06-02 20:11:29 +02:00
|
|
|
|
|
|
|
|
|
|
try
|
2025-05-31 19:26:02 +02:00
|
|
|
|
{
|
2025-07-26 11:11:24 +02:00
|
|
|
|
_logger.LogDebug("{ImporterType}: Starting Google Sheets export to {SheetId}",
|
|
|
|
|
|
ImporterType, SheetId);
|
2025-06-02 20:11:29 +02:00
|
|
|
|
|
2025-07-26 11:11:24 +02:00
|
|
|
|
var filteredRecords = GetFilteredRecords();
|
|
|
|
|
|
var sheetTabName = ProcessHelper.GetSheetName(int.Parse(ImportMonth!), int.Parse(ImportYear!));
|
2025-06-02 20:11:29 +02:00
|
|
|
|
|
2025-07-26 11:11:24 +02:00
|
|
|
|
_logger.LogDebug("{ImporterType}: Using sheet tab name: {SheetTabName}",
|
|
|
|
|
|
ImporterType, sheetTabName);
|
|
|
|
|
|
|
|
|
|
|
|
// Get current sheet data
|
|
|
|
|
|
var currentSheetData = _googleSheetValues.Get(SheetId!, $"{sheetTabName}!C7:D200").Execute();
|
2025-06-02 20:11:29 +02:00
|
|
|
|
|
2025-07-26 11:11:24 +02:00
|
|
|
|
var updateRequests = new List<ValueRange>();
|
|
|
|
|
|
|
|
|
|
|
|
for (var rowIndex = 0; rowIndex < 194; rowIndex++)
|
2025-06-02 20:11:29 +02:00
|
|
|
|
{
|
2025-07-26 11:11:24 +02:00
|
|
|
|
if (currentSheetData.Values == null || rowIndex >= currentSheetData.Values.Count)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
var existingRow = currentSheetData.Values[rowIndex];
|
|
|
|
|
|
if (existingRow.Count == 0 || string.IsNullOrEmpty(existingRow[0]?.ToString()))
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
var accountCode = existingRow[0].ToString()!;
|
|
|
|
|
|
var matchingRecord = filteredRecords.FirstOrDefault(x => x.Desc1 == accountCode);
|
|
|
|
|
|
if (matchingRecord == null)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
var newValue = matchingRecord.Value1?.ToString(CultureInfo.GetCultureInfo("pl-PL")) ?? "0";
|
|
|
|
|
|
var existingValue = (existingRow.Count > 1 ? existingRow[1] : "")?.ToString();
|
|
|
|
|
|
|
|
|
|
|
|
if (existingValue != newValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
var range = $"{sheetTabName}!D{rowIndex + 7}";
|
|
|
|
|
|
updateRequests.Add(new ValueRange
|
|
|
|
|
|
{
|
|
|
|
|
|
Range = range,
|
|
|
|
|
|
Values = new List<IList<object>> { new List<object> { newValue } }
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
_logger.LogDebug("{ImporterType}: Scheduled update for {AccountCode} at {Range} with value {Value}",
|
|
|
|
|
|
ImporterType, accountCode, range, newValue);
|
|
|
|
|
|
}
|
2025-06-02 20:11:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-26 11:11:24 +02:00
|
|
|
|
if (updateRequests.Count > 0)
|
2025-06-02 20:11:29 +02:00
|
|
|
|
{
|
2025-07-26 11:11:24 +02:00
|
|
|
|
var batchUpdate = new BatchUpdateValuesRequest
|
|
|
|
|
|
{
|
|
|
|
|
|
Data = updateRequests,
|
|
|
|
|
|
ValueInputOption = "USER_ENTERED"
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
_googleSheetValues.BatchUpdate(batchUpdate, SheetId!).Execute();
|
2025-06-02 20:11:29 +02:00
|
|
|
|
|
2025-07-26 11:11:24 +02:00
|
|
|
|
_logger.LogInformation("{ImporterType}: Updated {Count} cells in Google Sheet {SheetId}",
|
|
|
|
|
|
ImporterType, updateRequests.Count, SheetId);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
2025-06-02 20:11:29 +02:00
|
|
|
|
{
|
2025-07-26 11:11:24 +02:00
|
|
|
|
_logger.LogInformation("{ImporterType}: No changes to export to Google Sheet {SheetId}",
|
|
|
|
|
|
ImporterType, SheetId);
|
|
|
|
|
|
}
|
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-07-26 11:11:24 +02:00
|
|
|
|
_logger.LogError(e, "{ImporterType}: Failed to export to Google Sheets {SheetId}",
|
|
|
|
|
|
ImporterType, SheetId);
|
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 20:49:09 +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)
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-26 11:11:24 +02:00
|
|
|
|
_logger.LogDebug("{ImporterType}: Saved {RecordCount} records for layer {LayerId}",
|
2025-06-06 20:49:09 +02:00
|
|
|
|
ImporterType, records.Count, layerId);
|
2025-05-31 19:26:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|