T5 processor
This commit is contained in:
@@ -5,19 +5,10 @@ using WebAPI.Models;
|
||||
|
||||
namespace WebAPI.dataProcessors;
|
||||
|
||||
public class T3SingleSourceProcessor
|
||||
public class T3SingleSourceProcessor(
|
||||
AppDbContext db,
|
||||
LayersController controller)
|
||||
{
|
||||
private readonly AppDbContext _db;
|
||||
private readonly LayersController _controller;
|
||||
|
||||
public T3SingleSourceProcessor(
|
||||
AppDbContext db,
|
||||
LayersController controller)
|
||||
{
|
||||
_db = db;
|
||||
_controller = controller;
|
||||
}
|
||||
|
||||
public void Process(Layer processWorker)
|
||||
{
|
||||
var year = int.Parse(processWorker.Records?.SingleOrDefault(x => x.Code == "Year")?.Desc1!);
|
||||
@@ -27,7 +18,7 @@ public class T3SingleSourceProcessor
|
||||
{
|
||||
throw new Exception("SourceLayer record not found");
|
||||
}
|
||||
var sourceImportWorker = _db.Layers.SingleOrDefault(x => x.Name == sourceLayer);
|
||||
var sourceImportWorker = db.Layers.SingleOrDefault(x => x.Name == sourceLayer);
|
||||
if (sourceImportWorker == null)
|
||||
{
|
||||
throw new Exception("SourceImportWorkerL layer not found");
|
||||
@@ -38,7 +29,7 @@ public class T3SingleSourceProcessor
|
||||
throw new Exception("Source record not found");
|
||||
}
|
||||
|
||||
var processedLayer = _db.Layers
|
||||
var processedLayer = db.Layers
|
||||
.Where(x => x.ParentId == processWorker.Id)
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.FirstOrDefault();
|
||||
@@ -52,7 +43,7 @@ public class T3SingleSourceProcessor
|
||||
Id = Guid.NewGuid(),
|
||||
Type = LayerType.Processed,
|
||||
ParentId = processWorker.Id,
|
||||
Number = _db.Layers.Count() + 1
|
||||
Number = db.Layers.Count() + 1
|
||||
};
|
||||
processedLayer.Name = $"L{processedLayer.Number}-P-{year}/{month}-{source}-T3";
|
||||
processedLayer.CreatedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D");
|
||||
@@ -67,7 +58,7 @@ public class T3SingleSourceProcessor
|
||||
|
||||
var newRecords = new List<Record>();
|
||||
|
||||
var dataSources = _db.Layers
|
||||
var dataSources = db.Layers
|
||||
.Include(x => x.Records)
|
||||
.Where(x => x.ParentId == sourceImportWorker.Id
|
||||
&& !x.IsDeleted)
|
||||
@@ -135,13 +126,13 @@ public class T3SingleSourceProcessor
|
||||
|
||||
if (isNew)
|
||||
{
|
||||
_db.Layers.Add(processedLayer);
|
||||
db.Layers.Add(processedLayer);
|
||||
} else
|
||||
{
|
||||
_db.Layers.Update(processedLayer);
|
||||
db.Layers.Update(processedLayer);
|
||||
}
|
||||
_controller.SaveRecords(processedLayer.Id, newRecords, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
|
||||
_db.SaveChanges();
|
||||
controller.SaveRecords(processedLayer.Id, newRecords, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
|
||||
db.SaveChanges();
|
||||
}
|
||||
|
||||
}
|
||||
93
WebAPI/dataProcessors/t5.LastValues.processor.cs
Normal file
93
WebAPI/dataProcessors/t5.LastValues.processor.cs
Normal file
@@ -0,0 +1,93 @@
|
||||
using DiunaBIWebAPI.dataProcessors;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using WebAPI.Controllers;
|
||||
using WebAPI.Models;
|
||||
|
||||
namespace WebAPI.dataProcessors;
|
||||
|
||||
public class T5LastValuesProcessor(
|
||||
AppDbContext db,
|
||||
LayersController controller)
|
||||
{
|
||||
public 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);
|
||||
if (sourceImportWorker == null) throw new Exception("SourceImportWorker 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}-T5";
|
||||
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)
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.ToList();
|
||||
|
||||
if (dataSources.Count == 0) throw new Exception($"DataSource is empty, {sourceImportWorker.Name}");
|
||||
|
||||
var codes = dataSources.SelectMany(x => x.Records!).Select(x => x.Code).Distinct().ToList();
|
||||
|
||||
foreach (var code in codes)
|
||||
{
|
||||
var lastRecord = dataSources.SelectMany(x => x.Records!).Where(x => x.Code == code).OrderByDescending(x => x.CreatedAt).FirstOrDefault();
|
||||
if (lastRecord == null) continue;
|
||||
|
||||
var processedRecord = new Record
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Code = code,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
ModifiedAt = DateTime.UtcNow
|
||||
};
|
||||
|
||||
for (var i = 1; i < 33; i++)
|
||||
{
|
||||
if (ProcessHelper.GetValue(lastRecord, i) != null)
|
||||
{
|
||||
ProcessHelper.SetValue(processedRecord, i, ProcessHelper.GetValue(lastRecord, i));
|
||||
}
|
||||
}
|
||||
|
||||
newRecords.Add(processedRecord);
|
||||
}
|
||||
|
||||
if (isNew)
|
||||
db.Layers.Add(processedLayer);
|
||||
else
|
||||
db.Layers.Update(processedLayer);
|
||||
controller.SaveRecords(processedLayer.Id, newRecords, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
|
||||
db.SaveChanges();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user