176 lines
6.4 KiB
C#
176 lines
6.4 KiB
C#
using DiunaBIWebAPI.dataProcessors;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using WebAPI.Controllers;
|
|
using WebAPI.Models;
|
|
|
|
|
|
namespace WebAPI.dataProcessors;
|
|
|
|
public class T4R2Processor
|
|
{
|
|
private readonly AppDbContext _db;
|
|
private readonly LayersController _controller;
|
|
private readonly LogsController _logsController;
|
|
|
|
public T4R2Processor(
|
|
AppDbContext db,
|
|
LayersController controller,
|
|
LogsController logsController)
|
|
{
|
|
_db = db;
|
|
_controller = controller;
|
|
_logsController = logsController;
|
|
}
|
|
|
|
public void Process(Layer processWorker)
|
|
{
|
|
var year = int.Parse(processWorker.Records?.SingleOrDefault(x => x.Code == "Year")?.Desc1!);
|
|
var sources = processWorker.Records?.Where(x => x.Code == "Source").ToList();
|
|
if (!sources!.Any())
|
|
{
|
|
throw new Exception("Source record not found");
|
|
}
|
|
|
|
var layerName = processWorker.Records?.SingleOrDefault(x => x.Code == "LayerName")?.Desc1;
|
|
if (layerName == null)
|
|
{
|
|
throw new Exception("LayerName record not found");
|
|
}
|
|
|
|
|
|
var processedLayer = _db.Layers
|
|
.Where(x => x.ParentId == processWorker.Id
|
|
&& !x.IsDeleted)
|
|
.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}-{layerName}";
|
|
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>();
|
|
|
|
foreach (var source in sources!)
|
|
{
|
|
var rawSourceCodes = processWorker.Records?.SingleOrDefault(x => x.Code == $"Codes-{source.Desc1}")
|
|
?.Desc1;
|
|
var sourceCodes = new List<int>();
|
|
if (rawSourceCodes != null)
|
|
{
|
|
sourceCodes = ProcessHelper.ParseCodes(rawSourceCodes);
|
|
}
|
|
|
|
for (var month = 1; month <= DateTime.UtcNow.Month; month++)
|
|
{
|
|
var monthCopy = month;
|
|
var dataSource = _db.Layers.Where(x =>
|
|
x.Type == LayerType.Processed &&
|
|
!x.IsDeleted &&
|
|
x.Name != null && x.Name.Contains($"{year}/{monthCopy}-{source.Desc1}-T")
|
|
)
|
|
.Include(x => x.Records)
|
|
.FirstOrDefault();
|
|
if (dataSource != null)
|
|
{
|
|
var news = dataSource.Records!
|
|
.Where(x => sourceCodes.Count <= 0 || sourceCodes.Contains(int.Parse(x.Code!)))
|
|
.Select(x =>
|
|
{
|
|
var newRecord = new Record
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
Code = x.Code + month.ToString("D2"),
|
|
CreatedAt = DateTime.UtcNow,
|
|
ModifiedAt = DateTime.UtcNow,
|
|
Value1 = source.Desc1 != "FK2" ? x.Value32 : x.Value1,
|
|
Desc1 = x.Desc1
|
|
};
|
|
return newRecord;
|
|
}
|
|
).ToList();
|
|
newRecords.AddRange(news);
|
|
}
|
|
else
|
|
{
|
|
_logsController.AddEntry(new LogEntry
|
|
{
|
|
Title = $"{processWorker.Name}, {processWorker.Id}",
|
|
Type = LogEntryType.Warning,
|
|
LogType = LogType.Process,
|
|
Message = $"Data source {year}/{month}-{source.Desc1}-T3 not found",
|
|
CreatedAt = DateTime.UtcNow
|
|
});
|
|
}
|
|
}
|
|
|
|
// year summery
|
|
var dataSourceSum = _db.Layers.Where(x =>
|
|
x.Type == LayerType.Processed &&
|
|
!x.IsDeleted &&
|
|
x.Name != null && x.Name.Contains($"{year}/13-{source.Desc1}-T")
|
|
)
|
|
.Include(x => x.Records)
|
|
.FirstOrDefault();
|
|
if (dataSourceSum != null)
|
|
{
|
|
var news = dataSourceSum.Records!
|
|
.Where(x => sourceCodes.Count <= 0 || sourceCodes.Contains(int.Parse(x.Code!)))
|
|
.Select(x =>
|
|
{
|
|
var newRecord = new Record
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
Code = x.Code + "13",
|
|
CreatedAt = DateTime.UtcNow,
|
|
ModifiedAt = DateTime.UtcNow,
|
|
Value1 = x.Value32
|
|
};
|
|
return newRecord;
|
|
}
|
|
).ToList();
|
|
newRecords.AddRange(news);
|
|
}
|
|
else
|
|
{
|
|
_logsController.AddEntry(new LogEntry
|
|
{
|
|
Title = $"{processWorker.Name}, {processWorker.Id}",
|
|
Type = LogEntryType.Warning,
|
|
LogType = LogType.Process,
|
|
Message = $"Data source {year}/13-{source.Desc1}-T3 not found",
|
|
CreatedAt = DateTime.UtcNow
|
|
});
|
|
}
|
|
}
|
|
|
|
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();
|
|
}
|
|
} |