95 lines
3.3 KiB
C#
95 lines
3.3 KiB
C#
|
|
using Microsoft.EntityFrameworkCore;
|
|||
|
|
using DiunaBI.Core.Models;
|
|||
|
|
using DiunaBI.Database.Context;
|
|||
|
|
|
|||
|
|
namespace DiunaBI.Plugins.Morska.Processors;
|
|||
|
|
|
|||
|
|
public class T4SingleSourceProcessor(
|
|||
|
|
AppDbContext db)
|
|||
|
|
{
|
|||
|
|
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 && !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 &&
|
|||
|
|
!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,
|
|||
|
|
Number = db.Layers.Count() + 1
|
|||
|
|
};
|
|||
|
|
processedLayer.Name = $"L{processedLayer.Number}-P-{year}/{month:D2}-{source}-T4";
|
|||
|
|
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 dataSource = db.Layers
|
|||
|
|
.Include(x => x.Records)
|
|||
|
|
.Where(x => x.ParentId == sourceImportWorker.Id
|
|||
|
|
&& !x.IsDeleted && !x.IsCancelled)
|
|||
|
|
.OrderByDescending(x => x.CreatedAt)
|
|||
|
|
.AsNoTracking()
|
|||
|
|
.FirstOrDefault();
|
|||
|
|
|
|||
|
|
if (dataSource == null)
|
|||
|
|
{
|
|||
|
|
throw new Exception($"DataSource not found, {sourceImportWorker.Name}");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var newRecords = dataSource.Records!.Select(record => new Record
|
|||
|
|
{
|
|||
|
|
Id = Guid.NewGuid(),
|
|||
|
|
Code = record.Code,
|
|||
|
|
Desc1 = record.Desc1,
|
|||
|
|
Value1 = record.Value1,
|
|||
|
|
CreatedAt = DateTime.UtcNow,
|
|||
|
|
ModifiedAt = DateTime.UtcNow
|
|||
|
|
})
|
|||
|
|
.ToList();
|
|||
|
|
|
|||
|
|
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();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|