T3SourceSummary processor
This commit is contained in:
@@ -123,7 +123,7 @@ namespace WebAPI.dataProcessors
|
||||
|
||||
List<Record> allRecords = dataSources.SelectMany(x => x.Records!).ToList();
|
||||
|
||||
foreach (Record baseRecord in dataSources.First()?.Records!)
|
||||
foreach (Record baseRecord in dataSources.Last()?.Records!)
|
||||
{
|
||||
List<Record> codeRecords = allRecords.Where(x => x.Code == baseRecord.Code).ToList();
|
||||
|
||||
|
||||
110
WebAPI/dataProcessors/t3.SourceSummary.processor.cs
Normal file
110
WebAPI/dataProcessors/t3.SourceSummary.processor.cs
Normal file
@@ -0,0 +1,110 @@
|
||||
using Google.Apis.Sheets.v4;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using WebAPI.Controllers;
|
||||
using WebAPI.Models;
|
||||
|
||||
namespace WebAPI.dataProcessors
|
||||
{
|
||||
public class T3SourceSummaryProcessor
|
||||
{
|
||||
private AppDbContext db;
|
||||
private SpreadsheetsResource.ValuesResource googleSheetValues;
|
||||
private LayersController controller;
|
||||
|
||||
public T3SourceSummaryProcessor(
|
||||
AppDbContext _db,
|
||||
SpreadsheetsResource.ValuesResource _googleSheetValues,
|
||||
LayersController _controller)
|
||||
{
|
||||
db = _db;
|
||||
googleSheetValues = _googleSheetValues;
|
||||
controller = _controller;
|
||||
}
|
||||
|
||||
public void process(Layer processWorker)
|
||||
{
|
||||
string? sourceName = processWorker?.Records?.Single(x => x.Code == "Source")?.Desc1;
|
||||
int year = int.Parse(processWorker?.Records?.Single(x => x.Code == "Year")?.Desc1 ?? "0");
|
||||
if (year == 0)
|
||||
{
|
||||
throw new Exception($"Year is empty, {processWorker!.Name}");
|
||||
}
|
||||
|
||||
Layer? processedLayer = db.Layers
|
||||
.Where(x => x.ParentId == processWorker!.Id
|
||||
&& !x.IsDeleted)
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.FirstOrDefault();
|
||||
|
||||
bool isNew = false;
|
||||
if (processedLayer == null)
|
||||
{
|
||||
isNew = true;
|
||||
processedLayer = new Layer
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Source = "",
|
||||
Type = LayerType.processed,
|
||||
ParentId = processWorker!.Id,
|
||||
Number = db.Layers.Count() + 1,
|
||||
};
|
||||
processedLayer.Name = $"L{processedLayer.Number}-P-{year}/13-{sourceName}-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.Sources = new List<ProcessSource>();
|
||||
processedLayer.ModifiedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D");
|
||||
processedLayer.ModifiedAt = DateTime.UtcNow;
|
||||
|
||||
List<Record> newRecords = new List<Record>();
|
||||
|
||||
List<Layer> sources = new List<Layer>();
|
||||
for (int i=1; i<13; i++)
|
||||
{
|
||||
Layer? source = db.Layers.Where(x =>
|
||||
x.Type == LayerType.processed
|
||||
&& !x.IsDeleted
|
||||
&& x.Name.Contains($"{year}/{i}-{sourceName}-T3"))
|
||||
.Include(x => x.Records)
|
||||
.FirstOrDefault();
|
||||
if (source != null)
|
||||
{
|
||||
sources.Add(source);
|
||||
}
|
||||
}
|
||||
|
||||
if (sources.Count == 0)
|
||||
{
|
||||
throw new Exception($"DataSources are empty, {processWorker!.Name}");
|
||||
}
|
||||
|
||||
List<Record> allRecords = sources.SelectMany(x => x.Records!).ToList();
|
||||
|
||||
foreach (Record baseRecord in sources.Last()?.Records!)
|
||||
{
|
||||
List<Record> codeRecords = allRecords.Where(x => x.Code == baseRecord.Code).ToList();
|
||||
Record processedRecord = new Record
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Code = baseRecord.Code,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
ModifiedAt = DateTime.UtcNow,
|
||||
Value1 = codeRecords.Sum(x => x.Value32)
|
||||
};
|
||||
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