Morska FK2 Importer
This commit is contained in:
@@ -173,15 +173,37 @@ namespace WebAPI.Controllers
|
|||||||
{
|
{
|
||||||
try
|
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;
|
string? startDate = importWorker.Records!.FirstOrDefault(x => x.Code == "StartDate")?.Desc1;
|
||||||
if (startDate == null)
|
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;
|
string? endDate = importWorker.Records!.Where(x => x.Code == "EndDate").First().Desc1;
|
||||||
if (endDate == null)
|
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 startDateParsed = DateTime.ParseExact(startDate!, "yyyy.MM.dd", null);
|
||||||
var endDateParsed = DateTime.ParseExact(endDate!, "yyyy.MM.dd", null);
|
var endDateParsed = DateTime.ParseExact(endDate!, "yyyy.MM.dd", null);
|
||||||
@@ -486,7 +508,7 @@ namespace WebAPI.Controllers
|
|||||||
Console.WriteLine($"DiunaLog: {message}");
|
Console.WriteLine($"DiunaLog: {message}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
internal bool IsImportedLayerUpToDate(Layer importWorker)
|
private bool IsImportedLayerUpToDate(Layer importWorker)
|
||||||
{
|
{
|
||||||
if (googleSheetValues is null)
|
if (googleSheetValues is null)
|
||||||
{
|
{
|
||||||
@@ -532,7 +554,7 @@ namespace WebAPI.Controllers
|
|||||||
Record? record = newestLayer.Records!.Where(x => x.Code == data[0][i].ToString()).FirstOrDefault();
|
Record? record = newestLayer.Records!.Where(x => x.Code == data[0][i].ToString()).FirstOrDefault();
|
||||||
if (record == null)
|
if (record == null)
|
||||||
{
|
{
|
||||||
WriteToConsole("Code not foundin DiunaBI", data[0][i].ToString()!);
|
WriteToConsole("Code not found in DiunaBI", data[0][i].ToString()!);
|
||||||
isUpToDate = false;
|
isUpToDate = false;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,6 +38,5 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="dataProcessors\" />
|
<Folder Include="dataProcessors\" />
|
||||||
<Folder Include="dataImporters\" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
136
WebAPI/dataImporters/morska.fk2.importer.cs
Normal file
136
WebAPI/dataImporters/morska.fk2.importer.cs
Normal file
@@ -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<Record> newRecords = new List<Record>();
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user