2025-05-31 19:26:02 +02:00
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
using Google.Apis.Sheets.v4;
|
|
|
|
|
|
using Google.Apis.Sheets.v4.Data;
|
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
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;
|
2025-06-02 18:53:25 +02:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2025-05-31 19:26:02 +02:00
|
|
|
|
|
|
|
|
|
|
namespace DiunaBI.Plugins.Morska.Processors;
|
|
|
|
|
|
|
2025-06-02 16:54:33 +02:00
|
|
|
|
public class T1R1Processor : MorskaBaseProcessor
|
|
|
|
|
|
{
|
|
|
|
|
|
public override string ProcessorType => "T1.R1";
|
|
|
|
|
|
|
|
|
|
|
|
private readonly AppDbContext _db;
|
|
|
|
|
|
private readonly SpreadsheetsResource.ValuesResource _googleSheetValues;
|
2025-06-02 18:53:25 +02:00
|
|
|
|
private readonly ILogger<T1R1Processor> _logger;
|
|
|
|
|
|
|
2025-06-02 16:54:33 +02:00
|
|
|
|
public T1R1Processor(
|
2025-06-02 18:53:25 +02:00
|
|
|
|
AppDbContext db,
|
|
|
|
|
|
SpreadsheetsResource.ValuesResource googleSheetValues,
|
|
|
|
|
|
ILogger<T1R1Processor> logger)
|
2025-06-02 16:54:33 +02:00
|
|
|
|
{
|
|
|
|
|
|
_db = db;
|
|
|
|
|
|
_googleSheetValues = googleSheetValues;
|
2025-06-02 18:53:25 +02:00
|
|
|
|
_logger = logger;
|
2025-06-02 16:54:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
public override void Process(Layer processWorker)
|
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}-R1-T1";
|
|
|
|
|
|
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 dynamicCodes = processWorker.Records?.Where(x => x.Code!.Contains("DynamicCode-"))
|
|
|
|
|
|
.OrderBy(x => int.Parse(x.Code!.Split('-')[1]))
|
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
|
|
var newRecords = new List<Record>();
|
|
|
|
|
|
|
|
|
|
|
|
for (var month = 1; month < 14; month++)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (year > DateTime.UtcNow.Year || ((year == DateTime.UtcNow.Year && month > DateTime.UtcNow.Month && month != 13)))
|
|
|
|
|
|
{
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var records = new List<Record>();
|
|
|
|
|
|
foreach (var source in sources)
|
|
|
|
|
|
{
|
|
|
|
|
|
var monthCopy = month;
|
2025-06-02 16:54:33 +02:00
|
|
|
|
var dataSource = _db.Layers.Where(x =>
|
2025-05-31 19:26:02 +02:00
|
|
|
|
x.Type == LayerType.Processed &&
|
|
|
|
|
|
!x.IsDeleted && !x.IsCancelled &&
|
|
|
|
|
|
x.Name != null && x.Name.Contains($"{year}/{monthCopy:D2}-{source.Desc1}-T3")
|
|
|
|
|
|
).Include(x => x.Records)
|
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
|
|
if (dataSource == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new Exception($"Source layer {year}/{monthCopy}-{source.Desc1}-T3 not found.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var codesRecord = processWorker.Records?.Where(x => x.Code == $"Codes-{source.Desc1}").FirstOrDefault();
|
|
|
|
|
|
if (codesRecord != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var codes = ProcessHelper.ParseCodes(codesRecord.Desc1!);
|
|
|
|
|
|
records.AddRange(dataSource.Records!.Where(x => codes.Contains(int.Parse(x.Code!))));
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
records.AddRange(dataSource.Records!);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (dynamicCodes != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var dynamicCode in dynamicCodes)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
if (dynamicCode.Desc1 == null)
|
|
|
|
|
|
{
|
2025-06-02 18:53:25 +02:00
|
|
|
|
_logger.LogWarning("T1R1: Formula in Record {RecordId} is missing. Process: {ProcessName} ({ProcessId})",
|
|
|
|
|
|
dynamicCode.Id, processWorker.Name, processWorker.Id);
|
2025-05-31 19:26:02 +02:00
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var calc = new BaseCalc(dynamicCode.Desc1);
|
|
|
|
|
|
if (!calc.IsFormulaCorrect())
|
|
|
|
|
|
{
|
2025-06-02 18:53:25 +02:00
|
|
|
|
_logger.LogWarning("T1R1: 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
|
|
|
|
|
|
{
|
|
|
|
|
|
records.Add(calc.CalculateT1(records));
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
2025-06-02 18:53:25 +02:00
|
|
|
|
_logger.LogWarning(e, "T1R1: 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, "T1R1: Calculation error for DynamicCode {RecordId}. Process: {ProcessName} ({ProcessId})",
|
|
|
|
|
|
dynamicCode.Id, processWorker.Name, processWorker.Id);
|
2025-05-31 19:26:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
newRecords.AddRange(records.Select(x => new Record
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = Guid.NewGuid(),
|
|
|
|
|
|
Code = $"{x.Code}{month:D2}",
|
|
|
|
|
|
CreatedAt = DateTime.UtcNow,
|
|
|
|
|
|
ModifiedAt = DateTime.UtcNow,
|
|
|
|
|
|
Value1 = x.Value32
|
|
|
|
|
|
}
|
|
|
|
|
|
));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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-05-31 19:26:02 +02:00
|
|
|
|
|
|
|
|
|
|
var sheetName = processWorker.Records?.SingleOrDefault(x => x.Code == "GoogleSheetName")?.Desc1;
|
|
|
|
|
|
if (sheetName == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new Exception("GoogleSheetName record not found");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UpdateReport(processedLayer.Id, sheetName);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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("T3MultiSourceSummary: Saved {RecordCount} records for layer {LayerId}", records.Count, layerId);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-31 19:26:02 +02:00
|
|
|
|
private void UpdateReport(Guid sourceId, string sheetName)
|
|
|
|
|
|
{
|
|
|
|
|
|
const string sheetId = "1pph-XowjlK5CIaCEV_A5buK4ceJ0Z0YoUlDI4VMkhhA";
|
2025-06-02 20:59:39 +02:00
|
|
|
|
var request = _googleSheetValues.Get(sheetId, $"{sheetName}!C4:DC4");
|
2025-05-31 19:26:02 +02:00
|
|
|
|
var response = request.Execute();
|
|
|
|
|
|
|
2025-06-02 16:54:33 +02:00
|
|
|
|
var r1 = _db.Layers
|
2025-05-31 19:26:02 +02:00
|
|
|
|
.Where(x => x.Id == sourceId)
|
|
|
|
|
|
.Include(x => x.Records)
|
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
|
|
var codesRow = response.Values[0];
|
|
|
|
|
|
|
|
|
|
|
|
var valueRange = new ValueRange
|
|
|
|
|
|
{
|
|
|
|
|
|
Values = new List<IList<object>>()
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
for (var i = 1; i <= 12; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
var values = new List<object>();
|
|
|
|
|
|
foreach (string code in codesRow)
|
|
|
|
|
|
{
|
|
|
|
|
|
var record = r1!.Records?.SingleOrDefault(x => x.Code == $"{code}{i:D2}");
|
|
|
|
|
|
if (record != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
values.Add(record.Value1!.Value);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
values.Add("0");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
valueRange.Values.Add(values);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// sum
|
|
|
|
|
|
var valuesSum = new List<object>();
|
|
|
|
|
|
var emptyRow = new List<object>();
|
|
|
|
|
|
foreach (string code in codesRow)
|
|
|
|
|
|
{
|
|
|
|
|
|
var record = r1!.Records?.SingleOrDefault(x => x.Code == $"{code}13");
|
|
|
|
|
|
emptyRow.Add("");
|
|
|
|
|
|
if (record != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
valuesSum.Add(record.Value1!.Value);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
valuesSum.Add("0");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
valueRange.Values.Add(emptyRow);
|
|
|
|
|
|
valueRange.Values.Add(valuesSum);
|
|
|
|
|
|
|
2025-06-02 20:59:39 +02:00
|
|
|
|
var update = _googleSheetValues.Update(valueRange, sheetId, $"{sheetName}!C7:DC20");
|
2025-05-31 19:26:02 +02:00
|
|
|
|
update.ValueInputOption =
|
|
|
|
|
|
SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.USERENTERED;
|
|
|
|
|
|
update.Execute();
|
|
|
|
|
|
|
|
|
|
|
|
// update time
|
|
|
|
|
|
var timeUtc = new List<object>
|
|
|
|
|
|
{
|
|
|
|
|
|
r1!.ModifiedAt.ToString("dd.MM.yyyy HH:mm:ss", CultureInfo.GetCultureInfo("pl-PL"))
|
|
|
|
|
|
};
|
|
|
|
|
|
var warsawTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
|
|
|
|
|
|
var warsawTime = TimeZoneInfo.ConvertTimeFromUtc(r1.ModifiedAt.ToUniversalTime(), warsawTimeZone);
|
|
|
|
|
|
var timeWarsaw = new List<object>
|
|
|
|
|
|
{
|
|
|
|
|
|
warsawTime.ToString("dd.MM.yyyy HH:mm:ss", CultureInfo.GetCultureInfo("pl-PL"))
|
|
|
|
|
|
};
|
|
|
|
|
|
var valueRangeTime = new ValueRange
|
|
|
|
|
|
{
|
|
|
|
|
|
Values = new List<IList<object>>()
|
|
|
|
|
|
};
|
|
|
|
|
|
valueRangeTime.Values.Add(timeUtc);
|
|
|
|
|
|
valueRangeTime.Values.Add(timeWarsaw);
|
|
|
|
|
|
|
2025-06-02 16:54:33 +02:00
|
|
|
|
var updateTimeUtc = _googleSheetValues.Update(valueRangeTime, sheetId, $"{sheetName}!G1:G2");
|
2025-05-31 19:26:02 +02:00
|
|
|
|
updateTimeUtc.ValueInputOption =
|
|
|
|
|
|
SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.USERENTERED;
|
|
|
|
|
|
updateTimeUtc.Execute();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|