Files
DiunaBI/WebAPI/dataProcessors/t3.SingleSource.processor.cs

141 lines
5.1 KiB
C#
Raw Normal View History

2023-12-01 15:19:19 +01:00
using DiunaBIWebAPI.dataProcessors;
2023-10-30 16:39:13 +01:00
using Microsoft.EntityFrameworkCore;
using WebAPI.Controllers;
2023-12-01 15:19:19 +01:00
using WebAPI.Models;
2024-06-18 22:02:19 +02:00
namespace WebAPI.dataProcessors;
2024-12-20 08:45:19 +01:00
public class T3SingleSourceProcessor(
AppDbContext db,
2025-03-03 13:06:53 +01:00
LayersController controller)
2023-12-01 15:19:19 +01:00
{
2024-06-18 22:02:19 +02:00
public void Process(Layer processWorker)
2023-12-01 15:19:19 +01:00
{
2024-06-18 22:02:19 +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)
2023-12-01 15:19:19 +01:00
{
2024-06-18 22:02:19 +02:00
throw new Exception("SourceLayer record not found");
2023-12-01 15:19:19 +01:00
}
var sourceImportWorker = db.Layers.SingleOrDefault(x => x.Name == sourceLayer && !x.IsDeleted && !x.IsCancelled);
2024-06-18 22:02:19 +02:00
if (sourceImportWorker == null)
2023-10-30 16:39:13 +01:00
{
2024-06-18 22:02:19 +02:00
throw new Exception("SourceImportWorkerL layer not found");
}
var source = processWorker.Records?.SingleOrDefault(x => x.Code == "Source")?.Desc1;
2024-06-18 22:02:19 +02:00
if (sourceLayer == null)
{
throw new Exception("Source record not found");
}
2023-10-30 16:39:13 +01:00
2024-12-20 08:45:19 +01:00
var processedLayer = db.Layers
2024-06-18 22:02:19 +02:00
.Where(x => x.ParentId == processWorker.Id)
.OrderByDescending(x => x.CreatedAt)
.FirstOrDefault();
2023-10-30 16:39:13 +01:00
2024-06-18 22:02:19 +02:00
var isNew = false;
if (processedLayer == null)
{
isNew = true;
processedLayer = new Layer
2023-10-30 16:39:13 +01:00
{
2024-06-18 22:02:19 +02:00
Id = Guid.NewGuid(),
Type = LayerType.Processed,
ParentId = processWorker.Id,
2024-12-20 08:45:19 +01:00
Number = db.Layers.Count() + 1
2024-06-18 22:02:19 +02:00
};
processedLayer.Name = $"L{processedLayer.Number}-P-{year}/{month:D2}-{source}-T3";
2024-06-18 22:02:19 +02:00
processedLayer.CreatedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D");
2023-12-01 15:19:19 +01:00
processedLayer.ModifiedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D");
2024-06-18 22:02:19 +02:00
processedLayer.CreatedAt = DateTime.UtcNow;
2023-12-01 15:19:19 +01:00
processedLayer.ModifiedAt = DateTime.UtcNow;
2024-06-18 22:02:19 +02:00
}
processedLayer.ModifiedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D");
processedLayer.ModifiedAt = DateTime.UtcNow;
2023-12-01 15:19:19 +01:00
2024-06-18 22:02:19 +02:00
var newRecords = new List<Record>();
2023-12-01 15:19:19 +01:00
2024-12-20 08:45:19 +01:00
var dataSources = db.Layers
2024-06-18 22:02:19 +02:00
.Include(x => x.Records)
.Where(x => x.ParentId == sourceImportWorker.Id
&& !x.IsDeleted && !x.IsCancelled)
2024-06-18 22:02:19 +02:00
.OrderBy(x => x.CreatedAt)
2025-02-19 13:14:06 +01:00
.AsNoTracking()
2024-06-18 22:02:19 +02:00
.ToList();
if (dataSources.Count == 0)
{
throw new Exception($"DataSources are empty, {sourceImportWorker.Name}");
}
2023-12-01 15:19:19 +01:00
2024-06-18 22:02:19 +02:00
var allRecords = dataSources.SelectMany(x => x.Records!).ToList();
2023-12-01 15:19:19 +01:00
2024-06-18 22:02:19 +02:00
foreach (var baseRecord in dataSources.Last().Records!)
{
var codeRecords = allRecords.Where(x => x.Code == baseRecord.Code).ToList();
var processedRecord = new Record
{
Id = Guid.NewGuid(),
Code = baseRecord.Code,
CreatedAt = DateTime.UtcNow,
ModifiedAt = DateTime.UtcNow
};
2023-10-30 16:39:13 +01:00
2024-06-18 22:02:19 +02:00
var lastDayInMonth = DateTime.DaysInMonth(year, month);
//day 1
var firstVal = codeRecords
.Where(x => x.CreatedAt.Date <= new DateTime(year, month, 1)).MaxBy(x => x.CreatedAt)?.Value1 ?? 0;
ProcessHelper.SetValue(processedRecord, 1, firstVal);
var previousValue = firstVal;
//days 2-29/30
for (var i = 2; i < lastDayInMonth; i++)
2024-06-18 22:02:19 +02:00
{
var dayVal = codeRecords
.Where(x => x.CreatedAt.Day == i && x.CreatedAt.Month == month).MaxBy(x => x.CreatedAt)?.Value1;
if (dayVal == null)
2023-10-30 16:39:13 +01:00
{
2024-06-18 22:02:19 +02:00
ProcessHelper.SetValue(processedRecord, i, 0);
}
else
2023-10-30 16:39:13 +01:00
{
2024-06-18 22:02:19 +02:00
var processedVal = dayVal - previousValue;
ProcessHelper.SetValue(processedRecord, i, processedVal);
previousValue = (double)dayVal;
2023-10-30 16:39:13 +01:00
}
2023-12-01 15:19:19 +01:00
}
2024-06-18 22:02:19 +02:00
//last day
var lastVal = codeRecords
.Where(x => x.CreatedAt.Date >= new DateTime(year, month, lastDayInMonth)).MaxBy(x => x.CreatedAt)?.Value1;
if (lastVal == null)
2023-10-30 16:39:13 +01:00
{
2024-06-18 22:02:19 +02:00
ProcessHelper.SetValue(processedRecord, lastDayInMonth, 0);
}
else
2023-10-30 16:39:13 +01:00
{
2024-06-18 22:02:19 +02:00
ProcessHelper.SetValue(processedRecord, lastDayInMonth, (double)lastVal - previousValue);
2023-10-30 16:39:13 +01:00
}
2024-06-18 22:02:19 +02:00
// copy last value
var valueToCopy = codeRecords.MaxBy(x => x.CreatedAt)?.Value1;
ProcessHelper.SetValue(processedRecord, 32, valueToCopy);
newRecords.Add(processedRecord);
2023-12-01 15:19:19 +01:00
}
2024-06-18 22:02:19 +02:00
if (isNew)
{
2024-12-20 08:45:19 +01:00
db.Layers.Add(processedLayer);
}
else
2024-06-18 22:02:19 +02:00
{
2024-12-20 08:45:19 +01:00
db.Layers.Update(processedLayer);
2024-06-18 22:02:19 +02:00
}
2024-12-20 08:45:19 +01:00
controller.SaveRecords(processedLayer.Id, newRecords, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
db.SaveChanges();
2023-12-01 15:19:19 +01:00
}
2024-06-18 22:02:19 +02:00
}