From b0c39b8ec17c690856686710e5ba677942f6083d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Zieliski?= Date: Thu, 6 Jun 2024 14:33:08 +0200 Subject: [PATCH] Morska FK2 Importer --- WebAPI/Controllers/LayersController.cs | 30 ++++- WebAPI/DiunaBI-WebAPI.csproj | 1 - WebAPI/dataImporters/morska.fk2.importer.cs | 136 ++++++++++++++++++++ 3 files changed, 162 insertions(+), 5 deletions(-) create mode 100644 WebAPI/dataImporters/morska.fk2.importer.cs diff --git a/WebAPI/Controllers/LayersController.cs b/WebAPI/Controllers/LayersController.cs index 9595fbf..8d866c4 100644 --- a/WebAPI/Controllers/LayersController.cs +++ b/WebAPI/Controllers/LayersController.cs @@ -173,15 +173,37 @@ namespace WebAPI.Controllers { try { + string? type = importWorker.Records!.FirstOrDefault(x => x.Code == "ImportType")?.Desc1; + if (type == null) + { + type = "Standard"; + } + + if (type == "FK2") + { + MorskaFk2Importer importer = new MorskaFk2Importer(db, googleSheetValues, this); + importer.import(importWorker); + + logsController.AddEntry(new LogEntry + { + Title = $"{importWorker!.Name}, {importWorker.Id}", + Type = LogEntryType.info, + LogType = LogType.import, + Message = "Success", + CreatedAt = DateTime.UtcNow + }); + return Ok(); + } + string? startDate = importWorker.Records!.FirstOrDefault(x => x.Code == "StartDate")?.Desc1; if (startDate == null) { - throw new Exception("Year record nod found"); + throw new Exception("StartDate record nod found"); } string? endDate = importWorker.Records!.Where(x => x.Code == "EndDate").First().Desc1; if (endDate == null) { - throw new Exception("Year record nod found"); + throw new Exception("EndDate record nod found"); } var startDateParsed = DateTime.ParseExact(startDate!, "yyyy.MM.dd", null); var endDateParsed = DateTime.ParseExact(endDate!, "yyyy.MM.dd", null); @@ -486,7 +508,7 @@ namespace WebAPI.Controllers Console.WriteLine($"DiunaLog: {message}"); } } - internal bool IsImportedLayerUpToDate(Layer importWorker) + private bool IsImportedLayerUpToDate(Layer importWorker) { if (googleSheetValues is null) { @@ -532,7 +554,7 @@ namespace WebAPI.Controllers Record? record = newestLayer.Records!.Where(x => x.Code == data[0][i].ToString()).FirstOrDefault(); if (record == null) { - WriteToConsole("Code not foundin DiunaBI", data[0][i].ToString()!); + WriteToConsole("Code not found in DiunaBI", data[0][i].ToString()!); isUpToDate = false; continue; } diff --git a/WebAPI/DiunaBI-WebAPI.csproj b/WebAPI/DiunaBI-WebAPI.csproj index 946bf03..722f099 100644 --- a/WebAPI/DiunaBI-WebAPI.csproj +++ b/WebAPI/DiunaBI-WebAPI.csproj @@ -38,6 +38,5 @@ - diff --git a/WebAPI/dataImporters/morska.fk2.importer.cs b/WebAPI/dataImporters/morska.fk2.importer.cs new file mode 100644 index 0000000..5fb70f8 --- /dev/null +++ b/WebAPI/dataImporters/morska.fk2.importer.cs @@ -0,0 +1,136 @@ +using System; +using System.Globalization; +using Google.Apis.Sheets.v4; +using WebAPI; +using WebAPI.Controllers; +using WebAPI.Models; + +namespace DiunaBIWebAPI.dataImporters +{ + public class MorskaFk2Importer + { + private readonly AppDbContext db; + private readonly SpreadsheetsResource.ValuesResource googleSheetValues; + private readonly LayersController controller; + + public MorskaFk2Importer( + AppDbContext _db, + SpreadsheetsResource.ValuesResource _googleSheetValues, + LayersController _controller) + { + db = _db; + googleSheetValues = _googleSheetValues; + controller = _controller; + } + + public void import(Layer importWorker) + { + string? sheetId = importWorker.Records!.Where(x => x.Code == "SheetId").FirstOrDefault()?.Desc1; + if (sheetId == null) + { + throw new Exception($"SheetId not found, {importWorker.Name}"); + } + string? sheetTabName = importWorker.Records!.Where(x => x.Code == "SheetTabName").FirstOrDefault()?.Desc1; + if (sheetTabName == null) + { + throw new Exception($"SheetTabName not found, {importWorker.Name}"); + } + string? importYearCell = importWorker.Records!.Where(x => x.Code == "ImportYear").FirstOrDefault()?.Desc1; + if (importYearCell == null) + { + throw new Exception($"ImportYear not found, {importWorker.Name}"); + } + string? importMonthCell = importWorker.Records!.Where(x => x.Code == "ImportMonth").FirstOrDefault()?.Desc1; + if (importMonthCell == null) + { + throw new Exception($"ImportMonth not found, {importWorker.Name}"); + } + string? importNameCell = importWorker.Records!.Where(x => x.Code == "ImportName").FirstOrDefault()?.Desc1; + if (importNameCell == null) + { + throw new Exception($"ImportName not found, {importWorker.Name}"); + } + string? checkSumCell = importWorker.Records!.Where(x => x.Code == "SheetId").FirstOrDefault()?.Desc1; + if (checkSumCell == null) + { + throw new Exception($"SheetId not found, {importWorker.Name}"); + } + string? dataRange = importWorker.Records!.Where(x => x.Code == "DataRange").FirstOrDefault()?.Desc1; + if (dataRange == null) + { + throw new Exception($"DataRange not found, {importWorker.Name}"); + } + + var nameResponse = googleSheetValues.Get(sheetId, $"{sheetTabName}!{importNameCell}:{importNameCell}").Execute(); + string? name = nameResponse.Values?[0][0].ToString(); + if (name == null) + { + throw new Exception($"ImportName cell is empty, {importWorker.Name}"); + } + var yearResponse = googleSheetValues.Get(sheetId, $"{sheetTabName}!{importYearCell}:{importYearCell}").Execute(); + string? year = yearResponse.Values[0][0].ToString(); + if (year == null) + { + throw new Exception($"ImportYear cell is empty, {importWorker.Name}"); + } + var monthResponse = googleSheetValues.Get(sheetId, $"{sheetTabName}!{importMonthCell}:{importMonthCell}").Execute(); + string? month = monthResponse.Values[0][0].ToString(); + if (month == null) + { + throw new Exception($"ImportMonth cell is empty, {importWorker.Name}"); + } + Layer layer = new Layer + { + Source = "GoogleSheet", + Number = db.Layers.Count() + 1, + ParentId = importWorker.Id, + Type = LayerType.import, + CreatedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"), + ModifiedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"), + CreatedAt = DateTime.UtcNow, + ModifiedAt = DateTime.UtcNow + }; + layer.Name = $"L{layer.Number}-I-{name}2-{year}/{month}-{DateTime.Now.ToString("yyyyMMddHHmm", CultureInfo.InvariantCulture)}"; + + List newRecords = new List(); + + var dataRangeResponse = googleSheetValues.Get(sheetId, $"{sheetTabName}!{dataRange}").Execute(); + var data = dataRangeResponse.Values; + for (int i = 0; i < data.Count; i++) + { + if (data[i].Count > 8 && (string)data[i][2] != String.Empty) + { + string[] dateArr = data[i][0].ToString()!.Split("."); + if (dateArr.Length != 3) + { + throw new Exception($"Invalid date in row {i}"); + } + + string number = data[i][1].ToString()!; + if (number.Length == 1) number = $"0{number}"; + string code = dateArr[2] + dateArr[1] + dateArr[0] + number; + double value; + if ( + data[i][8].ToString()?.Length > 0 && + double.TryParse(data[i][8].ToString(), CultureInfo.GetCultureInfo("pl-PL"), out value)) + { + Record record = new Record + { + Id = Guid.NewGuid(), + Code = code, + Desc1 = data[i][2].ToString(), + Value1 = value, + CreatedAt = DateTime.UtcNow, + ModifiedAt = DateTime.UtcNow + }; + newRecords.Add(record); + }; + } + } + db.Layers.Add(layer); + controller.SaveRecords(layer.Id, newRecords, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D")); + db.SaveChanges(); + } + } +} +