From e0b6f798a168743a44e94d5ecdb09271b6ded452 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Zieliski?= Date: Tue, 11 Jun 2024 20:50:55 +0200 Subject: [PATCH] P-T4 --- WebAPI/Controllers/LayersController.cs | 7 ++ WebAPI/DiunaBI-WebAPI.csproj | 3 - .../t4.SingleSource.processor.cs | 116 ++++++++++++++++++ 3 files changed, 123 insertions(+), 3 deletions(-) create mode 100644 WebAPI/dataProcessors/t4.SingleSource.processor.cs diff --git a/WebAPI/Controllers/LayersController.cs b/WebAPI/Controllers/LayersController.cs index d98516e..7b98400 100644 --- a/WebAPI/Controllers/LayersController.cs +++ b/WebAPI/Controllers/LayersController.cs @@ -377,6 +377,7 @@ namespace WebAPI.Controllers "T3-SourceYearSummary", "T3-MultiSourceSummary", // AA "T3-MultiSourceYearSummary", // AA/13 + "T4-SingleSource", "T3-R1" }; @@ -548,6 +549,12 @@ namespace WebAPI.Controllers processor.process(processWorker!); break; } + case "T4-SingleSource": + { + T4SingleSourceProcessor processor = new T4SingleSourceProcessor(db, googleSheetValues, this); + processor.process(processWorker!); + break; + } case "T3-MultiSourceSummary": { T3MultiSourceSummaryProcessor processor = diff --git a/WebAPI/DiunaBI-WebAPI.csproj b/WebAPI/DiunaBI-WebAPI.csproj index 722f099..1892860 100644 --- a/WebAPI/DiunaBI-WebAPI.csproj +++ b/WebAPI/DiunaBI-WebAPI.csproj @@ -36,7 +36,4 @@ - - - diff --git a/WebAPI/dataProcessors/t4.SingleSource.processor.cs b/WebAPI/dataProcessors/t4.SingleSource.processor.cs new file mode 100644 index 0000000..05e9510 --- /dev/null +++ b/WebAPI/dataProcessors/t4.SingleSource.processor.cs @@ -0,0 +1,116 @@ +using DiunaBIWebAPI.dataProcessors; +using Google.Apis.Sheets.v4; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using System; +using System.Globalization; +using WebAPI.Controllers; +using WebAPI.Models; + +namespace WebAPI.dataProcessors +{ + public class T4SingleSourceProcessor + { + private readonly AppDbContext db; + private readonly SpreadsheetsResource.ValuesResource googleSheetValues; + private readonly LayersController controller; + + public T4SingleSourceProcessor( + AppDbContext _db, + SpreadsheetsResource.ValuesResource _googleSheetValues, + LayersController _controller) + { + db = _db; + googleSheetValues = _googleSheetValues; + controller = _controller; + } + + public void process(Layer processWorker) + { + int year = int.Parse(processWorker?.Records?.SingleOrDefault(x => x.Code == "Year")?.Desc1!); + int month = int.Parse(processWorker?.Records?.SingleOrDefault(x => x.Code == "Month")?.Desc1!); + string? sourceLayer = processWorker?.Records?.SingleOrDefault(x => x.Code == "SourceLayer")?.Desc1; + if (sourceLayer == null) + { + throw new Exception("SourceLayer record not found"); + } + Layer? sourceImportWorker = db.Layers.SingleOrDefault(x => x.Name == sourceLayer); + if (sourceImportWorker == null) + { + throw new Exception("SourceImportWorkerL layer not found"); + } + string? source= processWorker?.Records?.SingleOrDefault(x => x.Code == "Source")?.Desc1; + if (sourceLayer == null) + { + throw new Exception("Source record not found"); + } + + Layer? processedLayer = db.Layers + .Where(x => x.ParentId == processWorker!.Id) + .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}/{month}-{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.Sources = new List(); + processedLayer.ModifiedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"); + processedLayer.ModifiedAt = DateTime.UtcNow; + + + List newRecords = new List(); + + Layer? dataSource = db.Layers + .Include(x => x.Records) + .Where(x => x.ParentId == sourceImportWorker.Id + && !x.IsDeleted) + .OrderByDescending(x => x.CreatedAt) + .FirstOrDefault(); + + if (dataSource == null) + { + throw new Exception($"DataSource not found, {sourceImportWorker.Name}"); + } + + foreach (Record record in dataSource.Records!) + { + Record processedRecord = new Record + { + Id = Guid.NewGuid(), + Code = record.Code, + Desc1 = record.Desc1, + Value1 = record.Value1, + CreatedAt = DateTime.UtcNow, + ModifiedAt = DateTime.UtcNow + }; + 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(); + } + + } +}