2025-05-31 19:26:02 +02:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
using DiunaBI.Core.Models;
|
2025-06-06 22:15:23 +02:00
|
|
|
|
using DiunaBI.Core.Database.Context;
|
2025-06-02 18:53:25 +02:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using Google.Apis.Sheets.v4;
|
2025-05-31 19:26:02 +02:00
|
|
|
|
|
|
|
|
|
|
namespace DiunaBI.Plugins.Morska.Processors;
|
|
|
|
|
|
|
2025-06-02 16:54:33 +02:00
|
|
|
|
public class T4SingleSourceProcessor : MorskaBaseProcessor
|
2025-05-31 19:26:02 +02:00
|
|
|
|
{
|
2025-06-02 16:54:33 +02:00
|
|
|
|
public override string ProcessorType => "T4.SingleSource";
|
|
|
|
|
|
|
|
|
|
|
|
private readonly AppDbContext _db;
|
2025-06-02 18:53:25 +02:00
|
|
|
|
private readonly SpreadsheetsResource.ValuesResource _googleSheetValues;
|
|
|
|
|
|
private readonly ILogger<T4SingleSourceProcessor> _logger;
|
|
|
|
|
|
|
2025-06-02 16:54:33 +02:00
|
|
|
|
public T4SingleSourceProcessor(
|
2025-06-02 18:53:25 +02:00
|
|
|
|
AppDbContext db,
|
|
|
|
|
|
SpreadsheetsResource.ValuesResource googleSheetValues,
|
|
|
|
|
|
ILogger<T4SingleSourceProcessor> logger)
|
2025-06-02 16:54:33 +02:00
|
|
|
|
{
|
|
|
|
|
|
_db = db;
|
2025-06-02 18:53:25 +02:00
|
|
|
|
_googleSheetValues = googleSheetValues;
|
|
|
|
|
|
_logger = logger;
|
2025-06-02 16:54:33 +02:00
|
|
|
|
}
|
2025-06-02 18:53:25 +02:00
|
|
|
|
|
2025-06-02 16:54:33 +02:00
|
|
|
|
public override void Process(Layer processWorker)
|
2025-05-31 19:26:02 +02:00
|
|
|
|
{
|
2025-06-02 18:53:25 +02:00
|
|
|
|
_logger.LogInformation("T4SingleSource: Starting processing for {ProcessWorkerName} ({ProcessWorkerId})",
|
|
|
|
|
|
processWorker.Name, processWorker.Id);
|
|
|
|
|
|
|
2025-05-31 19:26:02 +02:00
|
|
|
|
var year = int.Parse(processWorker.Records?.SingleOrDefault(x => x.Code == "Year")?.Desc1!);
|
|
|
|
|
|
var month = int.Parse(processWorker.Records?.SingleOrDefault(x => x.Code == "Month")?.Desc1!);
|
|
|
|
|
|
var sourceLayer = processWorker.Records?.SingleOrDefault(x => x.Code == "SourceLayer")?.Desc1;
|
|
|
|
|
|
if (sourceLayer == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new Exception("SourceLayer record not found");
|
|
|
|
|
|
}
|
2025-06-02 18:53:25 +02:00
|
|
|
|
|
2025-06-02 16:54:33 +02:00
|
|
|
|
var sourceImportWorker = _db.Layers.SingleOrDefault(x => x.Name == sourceLayer && !x.IsDeleted && !x.IsCancelled);
|
2025-05-31 19:26:02 +02:00
|
|
|
|
if (sourceImportWorker == null)
|
|
|
|
|
|
{
|
2025-06-02 18:53:25 +02:00
|
|
|
|
throw new Exception("SourceImportWorker layer not found");
|
2025-05-31 19:26:02 +02:00
|
|
|
|
}
|
2025-06-02 18:53:25 +02:00
|
|
|
|
|
2025-05-31 19:26:02 +02:00
|
|
|
|
var source = processWorker.Records?.SingleOrDefault(x => x.Code == "Source")?.Desc1;
|
2025-06-02 18:53:25 +02:00
|
|
|
|
if (source == null)
|
2025-05-31 19:26:02 +02:00
|
|
|
|
{
|
|
|
|
|
|
throw new Exception("Source record not found");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-02 16:54:33 +02:00
|
|
|
|
var processedLayer = _db.Layers
|
2025-06-02 19:13:41 +02:00
|
|
|
|
.Where(x => x.ParentId == processWorker.Id
|
|
|
|
|
|
&& !x.IsDeleted && !x.IsCancelled)
|
2025-05-31 19:26:02 +02:00
|
|
|
|
.OrderByDescending(x => x.CreatedAt)
|
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
|
|
var isNew = false;
|
|
|
|
|
|
if (processedLayer == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
isNew = true;
|
|
|
|
|
|
processedLayer = new Layer
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = Guid.NewGuid(),
|
|
|
|
|
|
Type = LayerType.Processed,
|
|
|
|
|
|
ParentId = processWorker.Id,
|
2025-06-02 16:54:33 +02:00
|
|
|
|
Number = _db.Layers.Count() + 1
|
2025-05-31 19:26:02 +02:00
|
|
|
|
};
|
|
|
|
|
|
processedLayer.Name = $"L{processedLayer.Number}-P-{year}/{month:D2}-{source}-T4";
|
|
|
|
|
|
processedLayer.CreatedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D");
|
|
|
|
|
|
processedLayer.ModifiedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D");
|
|
|
|
|
|
processedLayer.CreatedAt = DateTime.UtcNow;
|
|
|
|
|
|
processedLayer.ModifiedAt = DateTime.UtcNow;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
processedLayer.ModifiedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D");
|
|
|
|
|
|
processedLayer.ModifiedAt = DateTime.UtcNow;
|
|
|
|
|
|
|
2025-06-02 16:54:33 +02:00
|
|
|
|
var dataSource = _db.Layers
|
2025-05-31 19:26:02 +02:00
|
|
|
|
.Include(x => x.Records)
|
|
|
|
|
|
.Where(x => x.ParentId == sourceImportWorker.Id
|
|
|
|
|
|
&& !x.IsDeleted && !x.IsCancelled)
|
|
|
|
|
|
.OrderByDescending(x => x.CreatedAt)
|
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
|
|
if (dataSource == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new Exception($"DataSource not found, {sourceImportWorker.Name}");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var newRecords = dataSource.Records!.Select(record => new Record
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = Guid.NewGuid(),
|
|
|
|
|
|
Code = record.Code,
|
|
|
|
|
|
Desc1 = record.Desc1,
|
|
|
|
|
|
Value1 = record.Value1,
|
|
|
|
|
|
CreatedAt = DateTime.UtcNow,
|
|
|
|
|
|
ModifiedAt = DateTime.UtcNow
|
2025-06-02 18:53:25 +02:00
|
|
|
|
}).ToList();
|
2025-05-31 19:26:02 +02:00
|
|
|
|
|
|
|
|
|
|
if (isNew)
|
|
|
|
|
|
{
|
2025-06-02 16:54:33 +02:00
|
|
|
|
_db.Layers.Add(processedLayer);
|
2025-05-31 19:26:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-06-02 16:54:33 +02:00
|
|
|
|
_db.Layers.Update(processedLayer);
|
2025-05-31 19:26:02 +02:00
|
|
|
|
}
|
2025-06-02 18:53:25 +02:00
|
|
|
|
|
|
|
|
|
|
SaveRecords(processedLayer.Id, newRecords);
|
2025-06-02 16:54:33 +02:00
|
|
|
|
_db.SaveChanges();
|
2025-06-02 18:53:25 +02:00
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("T4SingleSource: Successfully completed processing for {ProcessWorkerName} ({ProcessWorkerId})",
|
|
|
|
|
|
processWorker.Name, processWorker.Id);
|
2025-05-31 19:26:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-02 18:53:25 +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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_logger.LogDebug("T4SingleSource: Saved {RecordCount} records for layer {LayerId}", records.Count, layerId);
|
|
|
|
|
|
}
|
2025-05-31 19:26:02 +02:00
|
|
|
|
}
|