Plugins engine is working
This commit is contained in:
@@ -0,0 +1,147 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using DiunaBI.Core.Models;
|
||||
using DiunaBI.Database.Context;
|
||||
using DiunaBI.Core.Services;
|
||||
|
||||
namespace DiunaBI.Plugins.Morska.Processors;
|
||||
|
||||
public class T3SingleSourceProcessor : MorskaBaseProcessor
|
||||
{
|
||||
public override string ProcessorType => "T3.SingleSource";
|
||||
private readonly AppDbContext _db;
|
||||
public T3SingleSourceProcessor(
|
||||
AppDbContext db)
|
||||
{
|
||||
_db = db;
|
||||
}
|
||||
public override void Process(Layer processWorker)
|
||||
{
|
||||
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");
|
||||
}
|
||||
var sourceImportWorker = _db.Layers.SingleOrDefault(x => x.Name == sourceLayer && !x.IsDeleted && !x.IsCancelled);
|
||||
if (sourceImportWorker == null)
|
||||
{
|
||||
throw new Exception("SourceImportWorkerL layer not found");
|
||||
}
|
||||
var source = processWorker.Records?.SingleOrDefault(x => x.Code == "Source")?.Desc1;
|
||||
if (sourceLayer == null)
|
||||
{
|
||||
throw new Exception("Source record not found");
|
||||
}
|
||||
|
||||
var processedLayer = _db.Layers
|
||||
.Where(x => x.ParentId == processWorker.Id)
|
||||
.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,
|
||||
Number = _db.Layers.Count() + 1
|
||||
};
|
||||
processedLayer.Name = $"L{processedLayer.Number}-P-{year}/{month:D2}-{source}-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>();
|
||||
|
||||
var dataSources = _db.Layers
|
||||
.Include(x => x.Records)
|
||||
.Where(x => x.ParentId == sourceImportWorker.Id
|
||||
&& !x.IsDeleted && !x.IsCancelled)
|
||||
.OrderBy(x => x.CreatedAt)
|
||||
.AsNoTracking()
|
||||
.ToList();
|
||||
if (dataSources.Count == 0)
|
||||
{
|
||||
throw new Exception($"DataSources are empty, {sourceImportWorker.Name}");
|
||||
}
|
||||
|
||||
var allRecords = dataSources.SelectMany(x => x.Records!).ToList();
|
||||
|
||||
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
|
||||
};
|
||||
|
||||
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++)
|
||||
{
|
||||
var dayVal = codeRecords
|
||||
.Where(x => x.CreatedAt.Day == i && x.CreatedAt.Month == month).MaxBy(x => x.CreatedAt)?.Value1;
|
||||
if (dayVal == null)
|
||||
{
|
||||
ProcessHelper.SetValue(processedRecord, i, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
var processedVal = dayVal - previousValue;
|
||||
ProcessHelper.SetValue(processedRecord, i, processedVal);
|
||||
previousValue = (double)dayVal;
|
||||
}
|
||||
}
|
||||
//last day
|
||||
var lastVal = codeRecords
|
||||
.Where(x => x.CreatedAt.Date >= new DateTime(year, month, lastDayInMonth)).MaxBy(x => x.CreatedAt)?.Value1;
|
||||
|
||||
if (lastVal == null)
|
||||
{
|
||||
ProcessHelper.SetValue(processedRecord, lastDayInMonth, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
ProcessHelper.SetValue(processedRecord, lastDayInMonth, (double)lastVal - previousValue);
|
||||
}
|
||||
|
||||
// copy last value
|
||||
var valueToCopy = codeRecords.MaxBy(x => x.CreatedAt)?.Value1;
|
||||
ProcessHelper.SetValue(processedRecord, 32, valueToCopy);
|
||||
|
||||
newRecords.Add(processedRecord);
|
||||
}
|
||||
|
||||
if (isNew)
|
||||
{
|
||||
_db.Layers.Add(processedLayer);
|
||||
}
|
||||
else
|
||||
{
|
||||
_db.Layers.Update(processedLayer);
|
||||
}
|
||||
//TODO: save records
|
||||
//controller.SaveRecords(processedLayer.Id, newRecords, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
|
||||
_db.SaveChanges();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user