2025-05-31 19:26:02 +02:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2025-06-02 18:53:25 +02:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
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
|
|
|
|
using DiunaBI.Core.Services;
|
|
|
|
|
|
using DiunaBI.Core.Services.Calculations;
|
|
|
|
|
|
|
|
|
|
|
|
namespace DiunaBI.Plugins.Morska.Processors;
|
|
|
|
|
|
|
2025-06-02 16:54:33 +02:00
|
|
|
|
public class T3MultiSourceYearSummaryProcessor : MorskaBaseProcessor
|
2025-05-31 19:26:02 +02:00
|
|
|
|
{
|
2025-06-02 16:54:33 +02:00
|
|
|
|
public override string ProcessorType => "T3.MultiSourceYearSummary";
|
2025-06-02 18:53:25 +02:00
|
|
|
|
|
2025-06-02 16:54:33 +02:00
|
|
|
|
private readonly AppDbContext _db;
|
2025-06-02 18:53:25 +02:00
|
|
|
|
private readonly ILogger<T3MultiSourceYearSummaryProcessor> _logger;
|
|
|
|
|
|
|
|
|
|
|
|
public T3MultiSourceYearSummaryProcessor(
|
|
|
|
|
|
AppDbContext db,
|
|
|
|
|
|
ILogger<T3MultiSourceYearSummaryProcessor> logger)
|
2025-06-02 16:54:33 +02:00
|
|
|
|
{
|
|
|
|
|
|
_db = db;
|
2025-06-02 18:53:25 +02:00
|
|
|
|
_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("T3MultiSourceYearSummary: 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 sources = processWorker.Records?.Where(x => x.Code == "Source").ToList();
|
|
|
|
|
|
if (sources!.Count == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new Exception("Source record not found");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-02 16:54:33 +02:00
|
|
|
|
var processedLayer = _db.Layers
|
2025-05-31 19:26:02 +02:00
|
|
|
|
.Where(x => x.ParentId == processWorker.Id
|
|
|
|
|
|
&& !x.IsDeleted && !x.IsCancelled)
|
|
|
|
|
|
.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}/13-AA-T3";
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
var newRecords = new List<Record>();
|
|
|
|
|
|
|
2025-06-02 16:54:33 +02:00
|
|
|
|
var dataSources = sources.Select(source => _db.Layers.Where(x => x.Type == LayerType.Processed && !x.IsDeleted && !x.IsCancelled && x.Name != null && x.Name.Contains($"{year}/13-{source.Desc1}-T3"))
|
2025-05-31 19:26:02 +02:00
|
|
|
|
.Include(x => x.Records)
|
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
|
.FirstOrDefault())
|
|
|
|
|
|
.OfType<Layer>()
|
|
|
|
|
|
.ToList();
|
2025-06-02 18:53:25 +02:00
|
|
|
|
|
2025-05-31 19:26:02 +02:00
|
|
|
|
if (dataSources.Count == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new Exception("DataSources are empty");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var allRecords = dataSources
|
|
|
|
|
|
.SelectMany(x => x.Records!).ToList();
|
|
|
|
|
|
var baseCodes = allRecords.Select(x => x.Code!.Remove(0, 1)).Distinct().ToList();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var baseCode in baseCodes)
|
|
|
|
|
|
{
|
|
|
|
|
|
var codeRecords = allRecords.Where(x =>
|
|
|
|
|
|
x.Code![1..] == baseCode)
|
|
|
|
|
|
.ToList();
|
|
|
|
|
|
var codeRecordsValidation = allRecords.Where(x =>
|
|
|
|
|
|
x.Code![1..] == baseCode)
|
|
|
|
|
|
.ToList();
|
|
|
|
|
|
var processedRecord = new Record
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = Guid.NewGuid(),
|
|
|
|
|
|
Code = $"9{baseCode}",
|
|
|
|
|
|
CreatedAt = DateTime.UtcNow,
|
|
|
|
|
|
ModifiedAt = DateTime.UtcNow
|
|
|
|
|
|
};
|
|
|
|
|
|
var validationRecord = new Record();
|
|
|
|
|
|
for (var i = 1; i < 33; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
ProcessHelper.SetValue(processedRecord, i,
|
|
|
|
|
|
codeRecords.Sum(x => ProcessHelper.GetValue(x, i)));
|
|
|
|
|
|
|
|
|
|
|
|
ProcessHelper.SetValue(validationRecord, i,
|
|
|
|
|
|
codeRecordsValidation.Sum(x => ProcessHelper.GetValue(x, i)));
|
|
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
|
double.Abs((double)(ProcessHelper.GetValue(processedRecord, i) -
|
|
|
|
|
|
ProcessHelper.GetValue(validationRecord, i))!) > 0.01)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new Exception($"ValidationError: Code {baseCode}, " +
|
|
|
|
|
|
$"Value{i} ({ProcessHelper.GetValue(processedRecord, i)} | " +
|
|
|
|
|
|
$"{ProcessHelper.GetValue(validationRecord, i)})");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
newRecords.Add(processedRecord);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Dynamic Codes
|
|
|
|
|
|
var dynamicCodes = processWorker.Records?
|
|
|
|
|
|
.Where(x => x.Code!.Contains("DynamicCode-"))
|
|
|
|
|
|
.OrderBy(x => int.Parse(x.Code!.Split('-')[1])).ToList();
|
2025-06-02 18:53:25 +02:00
|
|
|
|
|
2025-05-31 19:26:02 +02:00
|
|
|
|
if (dynamicCodes != null && dynamicCodes.Count != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var dynamicCode in dynamicCodes)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
if (dynamicCode.Desc1 == null)
|
|
|
|
|
|
{
|
2025-06-02 18:53:25 +02:00
|
|
|
|
_logger.LogWarning("T3MultiSourceYearSummary: Formula in Record {RecordId} is missing. Process: {ProcessName} ({ProcessId})",
|
|
|
|
|
|
dynamicCode.Id, processWorker.Name, processWorker.Id);
|
2025-05-31 19:26:02 +02:00
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2025-06-02 18:53:25 +02:00
|
|
|
|
|
2025-05-31 19:26:02 +02:00
|
|
|
|
var calc = new BaseCalc(dynamicCode.Desc1);
|
|
|
|
|
|
if (!calc.IsFormulaCorrect())
|
|
|
|
|
|
{
|
2025-06-02 18:53:25 +02:00
|
|
|
|
_logger.LogWarning("T3MultiSourceYearSummary: Formula {Expression} in Record {RecordId} is not correct. Process: {ProcessName} ({ProcessId})",
|
|
|
|
|
|
calc.Expression, dynamicCode.Id, processWorker.Name, processWorker.Id);
|
2025-05-31 19:26:02 +02:00
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
newRecords.Add(calc.CalculateT3(newRecords));
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
2025-06-02 18:53:25 +02:00
|
|
|
|
_logger.LogWarning(e, "T3MultiSourceYearSummary: Formula {Expression} in Record {RecordId} calculation error. Process: {ProcessName} ({ProcessId})",
|
|
|
|
|
|
calc.Expression, dynamicCode.Id, processWorker.Name, processWorker.Id);
|
2025-05-31 19:26:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
2025-06-02 18:53:25 +02:00
|
|
|
|
_logger.LogWarning(e, "T3MultiSourceYearSummary: Calculation error for DynamicCode {RecordId}. Process: {ProcessName} ({ProcessId})",
|
|
|
|
|
|
dynamicCode.Id, processWorker.Name, processWorker.Id);
|
2025-05-31 19:26:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-06-02 18:53:25 +02:00
|
|
|
|
|
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("T3MultiSourceYearSummary: Successfully completed processing for {ProcessWorkerName} ({ProcessWorkerId})",
|
|
|
|
|
|
processWorker.Name, processWorker.Id);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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("T3MultiSourceYearSummary: Saved {RecordCount} records for layer {LayerId}", records.Count, layerId);
|
2025-05-31 19:26:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|