Add new Backend structure with proper .NET 8 projects
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
using System.Globalization;
|
||||
using Google.Apis.Sheets.v4;
|
||||
using Google.Apis.Sheets.v4.Data;
|
||||
using DiunaBI.Core.Models;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace DiunaBI.Core.Services.Exports;
|
||||
|
||||
public class GoogleSheetExport
|
||||
{
|
||||
private readonly GoogleDriveHelper _googleDriveHelper;
|
||||
private readonly SpreadsheetsResource.ValuesResource _googleSheetValues;
|
||||
private readonly IConfiguration _configuration;
|
||||
public GoogleSheetExport(
|
||||
GoogleDriveHelper googleDriveHelper,
|
||||
SpreadsheetsResource.ValuesResource googleSheetValues,
|
||||
IConfiguration configuration)
|
||||
{
|
||||
_googleDriveHelper = googleDriveHelper;
|
||||
_googleSheetValues = googleSheetValues;
|
||||
_configuration = configuration;
|
||||
}
|
||||
public void Export(Layer layer)
|
||||
{
|
||||
if (_googleDriveHelper.Service is null)
|
||||
{
|
||||
throw new Exception("Google Drive API not initialized");
|
||||
}
|
||||
try
|
||||
{
|
||||
|
||||
var data = new List<IList<object>> { new List<object> { layer.Name! } };
|
||||
|
||||
switch (layer.Type)
|
||||
{
|
||||
case LayerType.Import:
|
||||
{
|
||||
data.Add(new List<object> { "Code", "Value1" });
|
||||
data.AddRange(layer.Records!.Select(record => new List<object> { record.Code!, record.Value1! }));
|
||||
break;
|
||||
}
|
||||
case LayerType.Administration:
|
||||
{
|
||||
data.Add(new List<object> { "Code", "Desc1" });
|
||||
data.AddRange(layer.Records!.Select(record => new List<object> { record.Code!, record.Desc1! }));
|
||||
break;
|
||||
}
|
||||
case LayerType.Processed:
|
||||
{
|
||||
data.Add(new List<object> { "Code", "Value1", "Value2", "Value3", "Value3",
|
||||
"Value5", "Value6", "Value7", "Value8", "Value9", "Value10",
|
||||
"Value11", "Value12", "Value13", "Value14", "Value15", "Value16",
|
||||
"Value17", "Value18", "Value19", "Value20", "Value21", "Value22",
|
||||
"Value23", "Value24", "Value25", "Value26", "Value27", "Value28",
|
||||
"Value29", "Value30", "Value31", "Value32"});
|
||||
|
||||
data.AddRange(layer.Records!.Select(record => new List<object>
|
||||
{
|
||||
record.Code!,
|
||||
record.Value1!,
|
||||
record.Value2!,
|
||||
record.Value3!,
|
||||
record.Value4!,
|
||||
record.Value5!,
|
||||
record.Value6!,
|
||||
record.Value7!,
|
||||
record.Value8!,
|
||||
record.Value9!,
|
||||
record.Value10!,
|
||||
record.Value11!,
|
||||
record.Value12!,
|
||||
record.Value13!,
|
||||
record.Value14!,
|
||||
record.Value15!,
|
||||
record.Value16!,
|
||||
record.Value17!,
|
||||
record.Value18!,
|
||||
record.Value19!,
|
||||
record.Value20!,
|
||||
record.Value21!,
|
||||
record.Value22!,
|
||||
record.Value23!,
|
||||
record.Value24!,
|
||||
record.Value25!,
|
||||
record.Value26!,
|
||||
record.Value27!,
|
||||
record.Value28!,
|
||||
record.Value29!,
|
||||
record.Value30!,
|
||||
record.Value31!,
|
||||
record.Value32!
|
||||
}));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
throw new Exception("Wrong LayerType");
|
||||
}
|
||||
|
||||
var body = new Google.Apis.Drive.v3.Data.File
|
||||
{
|
||||
Name = $"{DateTime.Now.ToString(new CultureInfo("pl-PL"))}",
|
||||
MimeType = "application/vnd.google-apps.spreadsheet",
|
||||
Parents = new List<string?> { _configuration["exportDirectory"] }
|
||||
};
|
||||
var request = _googleDriveHelper.Service.Files.Create(body);
|
||||
var file = request.Execute();
|
||||
|
||||
var sheetId = file.Id;
|
||||
var range = $"Sheet1!A1:AG${data.Count}";
|
||||
|
||||
var valueRange = new ValueRange { Values = data };
|
||||
|
||||
var updateRequest = _googleSheetValues.Update(valueRange, sheetId, range);
|
||||
updateRequest.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.RAW;
|
||||
updateRequest.Execute();
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
using System.Globalization;
|
||||
using Google.Apis.Sheets.v4;
|
||||
using DiunaBI.Core.Models;
|
||||
using DiunaBI.Database.Context;
|
||||
|
||||
namespace DiunaBI.Plugins.Morska.Importers;
|
||||
|
||||
public class MorskaD1Importer(
|
||||
AppDbContext db,
|
||||
SpreadsheetsResource.ValuesResource googleSheetValues)
|
||||
{
|
||||
public void Import(Layer importWorker)
|
||||
{
|
||||
var sheetId = importWorker.Records!.FirstOrDefault(x => x.Code == "SheetId")?.Desc1;
|
||||
if (sheetId == null)
|
||||
{
|
||||
throw new Exception($"SheetId not found, {importWorker.Name}");
|
||||
}
|
||||
var sheetTabName = importWorker.Records!.FirstOrDefault(x => x.Code == "SheetTabName")?.Desc1;
|
||||
if (sheetTabName == null)
|
||||
{
|
||||
throw new Exception($"SheetTabName not found, {importWorker.Name}");
|
||||
}
|
||||
var year = importWorker.Records!.FirstOrDefault(x => x.Code == "ImportYear")?.Desc1;
|
||||
if (year == null)
|
||||
{
|
||||
throw new Exception($"ImportYear not found, {importWorker.Name}");
|
||||
}
|
||||
var month = importWorker.Records!.FirstOrDefault(x => x.Code == "ImportMonth")?.Desc1;
|
||||
if (month == null)
|
||||
{
|
||||
throw new Exception($"ImportMonth not found, {importWorker.Name}");
|
||||
}
|
||||
var name = importWorker.Records!.FirstOrDefault(x => x.Code == "ImportName")?.Desc1;
|
||||
if (name == null)
|
||||
{
|
||||
throw new Exception($"ImportName not found, {importWorker.Name}");
|
||||
}
|
||||
|
||||
var dataRange = importWorker.Records!.FirstOrDefault(x => x.Code == "DataRange")?.Desc1;
|
||||
if (dataRange == null)
|
||||
{
|
||||
throw new Exception($"DataRange not found, {importWorker.Name}");
|
||||
}
|
||||
|
||||
var layer = new Layer
|
||||
{
|
||||
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}-{year}/{month}-{DateTime.Now.ToString("yyyyMMddHHmm", CultureInfo.InvariantCulture)}";
|
||||
|
||||
var dataRangeResponse = googleSheetValues.Get(sheetId, $"{sheetTabName}!{dataRange}").Execute();
|
||||
var data = dataRangeResponse.Values;
|
||||
var newRecords = (from t in data
|
||||
where t.Count > 1 && (string)t[0] != string.Empty
|
||||
select new Record
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Code = t[0].ToString(),
|
||||
Value1 = IndexExists(t, 3) ? ParseValue(t[3]?.ToString()) : null,
|
||||
Value2 = IndexExists(t, 4) ? ParseValue(t[4]?.ToString()) : null,
|
||||
Value3 = IndexExists(t, 5) ? ParseValue(t[5]?.ToString()) : null,
|
||||
Value4 = IndexExists(t, 6) ? ParseValue(t[6]?.ToString()) : null,
|
||||
Value5 = IndexExists(t, 7) ? ParseValue(t[7]?.ToString()) : null,
|
||||
Value6 = IndexExists(t, 8) ? ParseValue(t[8]?.ToString()) : null,
|
||||
Value7 = IndexExists(t, 9) ? ParseValue(t[9]?.ToString()) : null,
|
||||
Value8 = IndexExists(t, 10) ? ParseValue(t[10]?.ToString()) : null,
|
||||
Value9 = IndexExists(t, 11) ? ParseValue(t[11]?.ToString()) : null,
|
||||
Value10 = IndexExists(t, 12) ? ParseValue(t[12]?.ToString()) : null,
|
||||
Value11 = IndexExists(t, 13) ? ParseValue(t[13]?.ToString()) : null,
|
||||
Value12 = IndexExists(t, 14) ? ParseValue(t[14]?.ToString()) : null,
|
||||
Value13 = IndexExists(t, 15) ? ParseValue(t[15]?.ToString()) : null,
|
||||
Value14 = IndexExists(t, 16) ? ParseValue(t[16]?.ToString()) : null,
|
||||
Value15 = IndexExists(t, 17) ? ParseValue(t[17]?.ToString()) : null,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
ModifiedAt = DateTime.UtcNow
|
||||
}).ToList();
|
||||
db.Layers.Add(layer);
|
||||
// TODO: Save records to the layer
|
||||
//controller.SaveRecords(layer.Id, newRecords, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
|
||||
db.SaveChanges();
|
||||
}
|
||||
|
||||
private double? ParseValue(string? value)
|
||||
{
|
||||
if (string.IsNullOrEmpty(value) || value == "#DIV/0!") return null;
|
||||
value = new string(value.Where(c => char.IsDigit(c) || c == '.' || c == ',' || c == '-').ToArray());
|
||||
try
|
||||
{
|
||||
double.TryParse(value, CultureInfo.GetCultureInfo("pl-PL"), out var result);
|
||||
return result;
|
||||
}
|
||||
catch (FormatException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
private bool IndexExists(IList<object> array, int index)
|
||||
{
|
||||
return array != null && index >= 0 && index < array.Count;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using DiunaBI.Core.Models;
|
||||
using DiunaBI.Database.Context;
|
||||
|
||||
namespace DiunaBI.Plugins.Morska.Importers;
|
||||
|
||||
public class MorskaD3Importer(
|
||||
AppDbContext db)
|
||||
{
|
||||
public void Import(Layer importWorker)
|
||||
{
|
||||
var year = importWorker.Records!.FirstOrDefault(x => x.Code == "ImportYear")?.Desc1;
|
||||
if (year == null)
|
||||
{
|
||||
throw new Exception($"ImportYear not found, {importWorker.Name}");
|
||||
}
|
||||
var month = importWorker.Records!.FirstOrDefault(x => x.Code == "ImportMonth")?.Desc1;
|
||||
if (month == null)
|
||||
{
|
||||
throw new Exception($"ImportMonth not found, {importWorker.Name}");
|
||||
}
|
||||
var name = importWorker.Records!.FirstOrDefault(x => x.Code == "ImportName")?.Desc1;
|
||||
if (name == null)
|
||||
{
|
||||
throw new Exception($"ImportName not found, {importWorker.Name}");
|
||||
}
|
||||
var type = importWorker.Records!.FirstOrDefault(x => x.Code == "ImportType")?.Desc1;
|
||||
if (name == null)
|
||||
{
|
||||
throw new Exception($"ImportType not found, {importWorker.Name}");
|
||||
}
|
||||
var dataInbox = db.DataInbox.OrderByDescending(x => x.CreatedAt).FirstOrDefault(x => x.Name == type);
|
||||
if (dataInbox == null)
|
||||
{
|
||||
throw new Exception($"DataInbox not found, {type}");
|
||||
}
|
||||
var data = Convert.FromBase64String(dataInbox.Data);
|
||||
var tst = Encoding.UTF8.GetString(data);
|
||||
var records = JsonSerializer.Deserialize<List<Record>>(Encoding.UTF8.GetString(data));
|
||||
if (records == null)
|
||||
{
|
||||
throw new Exception($"DataInbox.Data is empty, {dataInbox.Name}");
|
||||
}
|
||||
records = records.Where(x => x.Code!.StartsWith($"{year}{month}")).ToList();
|
||||
if (records.Count == 0)
|
||||
{
|
||||
throw new Exception($"No records found for {year}{month}");
|
||||
}
|
||||
records = records.Select(x =>
|
||||
{
|
||||
x.Id = Guid.NewGuid();
|
||||
x.CreatedAt = DateTime.UtcNow;
|
||||
x.ModifiedAt = DateTime.UtcNow;
|
||||
return x;
|
||||
}).ToList();
|
||||
var layer = new Layer
|
||||
{
|
||||
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}-{year}/{month}-{DateTime.Now.ToString("yyyyMMddHHmm", CultureInfo.InvariantCulture)}";
|
||||
|
||||
db.Layers.Add(layer);
|
||||
// TODO: Save records to the layer
|
||||
//controller.SaveRecords(layer.Id, records, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
|
||||
db.SaveChanges();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
using System.Globalization;
|
||||
using Google.Apis.Sheets.v4;
|
||||
using DiunaBI.Core.Models;
|
||||
using DiunaBI.Database.Context;
|
||||
|
||||
namespace DiunaBI.Plugins.Morska.Importers;
|
||||
|
||||
public class MorskaFk2Importer(
|
||||
AppDbContext db,
|
||||
SpreadsheetsResource.ValuesResource googleSheetValues)
|
||||
{
|
||||
public void Import(Layer importWorker)
|
||||
{
|
||||
var sheetId = importWorker.Records!.FirstOrDefault(x => x.Code == "SheetId")?.Desc1;
|
||||
if (sheetId == null)
|
||||
{
|
||||
throw new Exception($"SheetId not found, {importWorker.Name}");
|
||||
}
|
||||
var sheetTabName = importWorker.Records!.FirstOrDefault(x => x.Code == "SheetTabName")?.Desc1;
|
||||
if (sheetTabName == null)
|
||||
{
|
||||
throw new Exception($"SheetTabName not found, {importWorker.Name}");
|
||||
}
|
||||
var year = importWorker.Records!.FirstOrDefault(x => x.Code == "ImportYear")?.Desc1;
|
||||
if (year == null)
|
||||
{
|
||||
throw new Exception($"ImportYear not found, {importWorker.Name}");
|
||||
}
|
||||
var month = importWorker.Records!.FirstOrDefault(x => x.Code == "ImportMonth")?.Desc1;
|
||||
if (month == null)
|
||||
{
|
||||
throw new Exception($"ImportMonth not found, {importWorker.Name}");
|
||||
}
|
||||
var name = importWorker.Records!.FirstOrDefault(x => x.Code == "ImportName")?.Desc1;
|
||||
if (name == null)
|
||||
{
|
||||
throw new Exception($"ImportName not found, {importWorker.Name}");
|
||||
}
|
||||
|
||||
var dataRange = importWorker.Records!.FirstOrDefault(x => x.Code == "DataRange")?.Desc1;
|
||||
if (dataRange == null)
|
||||
{
|
||||
throw new Exception($"DataRange not found, {importWorker.Name}");
|
||||
}
|
||||
|
||||
var layer = new Layer
|
||||
{
|
||||
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}-{year}/{month}-{DateTime.Now.ToString("yyyyMMddHHmm", CultureInfo.InvariantCulture)}";
|
||||
|
||||
var newRecords = new List<Record>();
|
||||
|
||||
var dataRangeResponse = googleSheetValues.Get(sheetId, $"{sheetTabName}!{dataRange}").Execute();
|
||||
var data = dataRangeResponse.Values;
|
||||
for (var i = 0; i < data.Count; i++)
|
||||
{
|
||||
if (data[i].Count <= 9 || (string)data[i][3] == string.Empty) continue;
|
||||
var dateArr = data[i][1].ToString()!.Split(".");
|
||||
if (dateArr.Length != 3)
|
||||
{
|
||||
throw new Exception($"Invalid date in row {i}");
|
||||
}
|
||||
|
||||
var number = data[i][2].ToString()!;
|
||||
if (number.Length == 1) number = $"0{number}";
|
||||
var code = dateArr[2] + dateArr[1] + dateArr[0] + number;
|
||||
if (!(data[i][9].ToString()?.Length > 0) ||
|
||||
!double.TryParse(data[i][9].ToString(), CultureInfo.GetCultureInfo("pl-PL"), out var value)) continue;
|
||||
var record = new Record
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Code = code,
|
||||
Desc1 = data[i][3].ToString(),
|
||||
Value1 = value,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
ModifiedAt = DateTime.UtcNow
|
||||
};
|
||||
newRecords.Add(record);
|
||||
}
|
||||
db.Layers.Add(layer);
|
||||
// TODO: Save records to the layer
|
||||
//controller.SaveRecords(layer.Id, newRecords, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
|
||||
db.SaveChanges();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
using System.Globalization;
|
||||
using Google.Apis.Sheets.v4;
|
||||
using DiunaBI.Core.Models;
|
||||
using DiunaBI.Database.Context;
|
||||
|
||||
namespace DiunaBI.Plugins.Morska.Importers;
|
||||
|
||||
public class MorskaImporter(
|
||||
AppDbContext db,
|
||||
SpreadsheetsResource.ValuesResource googleSheetValues)
|
||||
{
|
||||
public void Import(Layer importWorker)
|
||||
{
|
||||
var sheetId = importWorker.Records!.FirstOrDefault(x => x.Code == "SheetId")?.Desc1;
|
||||
if (sheetId == null)
|
||||
{
|
||||
throw new Exception($"SheetId not found, {importWorker.Name}");
|
||||
}
|
||||
var sheetTabName = importWorker.Records!.FirstOrDefault(x => x.Code == "SheetTabName")?.Desc1;
|
||||
if (sheetTabName == null)
|
||||
{
|
||||
throw new Exception($"SheetTabName not found, {importWorker.Name}");
|
||||
}
|
||||
var year = importWorker.Records!.FirstOrDefault(x => x.Code == "ImportYear")?.Desc1;
|
||||
if (year == null)
|
||||
{
|
||||
throw new Exception($"ImportYear not found, {importWorker.Name}");
|
||||
}
|
||||
var month = importWorker.Records!.FirstOrDefault(x => x.Code == "ImportMonth")?.Desc1;
|
||||
if (month == null)
|
||||
{
|
||||
throw new Exception($"ImportMonth not found, {importWorker.Name}");
|
||||
}
|
||||
var name = importWorker.Records!.FirstOrDefault(x => x.Code == "ImportName")?.Desc1;
|
||||
if (name == null)
|
||||
{
|
||||
throw new Exception($"ImportName not found, {importWorker.Name}");
|
||||
}
|
||||
|
||||
var dataRange = importWorker.Records!.FirstOrDefault(x => x.Code == "DataRange")?.Desc1;
|
||||
if (dataRange == null)
|
||||
{
|
||||
throw new Exception($"DataRange not found, {importWorker.Name}");
|
||||
}
|
||||
|
||||
var layer = new Layer
|
||||
{
|
||||
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}-{year}/{month}-{DateTime.Now:yyyyMMddHHmm}";
|
||||
|
||||
var newRecords = new List<Record>();
|
||||
|
||||
var dataRangeResponse = googleSheetValues.Get(sheetId, $"{sheetTabName}!{dataRange}").Execute();
|
||||
var data = dataRangeResponse.Values;
|
||||
for (var i = 0; i < data[1].Count; i++)
|
||||
{
|
||||
if (!(data[0][i].ToString()?.Length > 0) ||
|
||||
!double.TryParse(data[1][i].ToString(), CultureInfo.GetCultureInfo("pl-PL"), out var value)) continue;
|
||||
var record = new Record
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Code = data[0][i].ToString(),
|
||||
Value1 = value,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
ModifiedAt = DateTime.UtcNow
|
||||
};
|
||||
newRecords.Add(record);
|
||||
}
|
||||
db.Layers.Add(layer);
|
||||
// TODO: Save records to the layer
|
||||
//controller.SaveRecords(layer.Id, newRecords, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
|
||||
db.SaveChanges();
|
||||
}
|
||||
}
|
||||
287
src/Backend/DiunaBI.Plugins.Morska/Processors/t1.r1.processor.cs
Normal file
287
src/Backend/DiunaBI.Plugins.Morska/Processors/t1.r1.processor.cs
Normal file
@@ -0,0 +1,287 @@
|
||||
using System.Globalization;
|
||||
using Google.Apis.Sheets.v4;
|
||||
using Google.Apis.Sheets.v4.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using DiunaBI.Core.Models;
|
||||
using DiunaBI.Database.Context;
|
||||
using DiunaBI.Core.Services;
|
||||
using DiunaBI.Core.Services.Calculations;
|
||||
|
||||
namespace DiunaBI.Plugins.Morska.Processors;
|
||||
|
||||
public class T1R1Processor(
|
||||
AppDbContext db,
|
||||
SpreadsheetsResource.ValuesResource googleSheetValues)
|
||||
{
|
||||
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!.Count == 0)
|
||||
{
|
||||
throw new Exception("Source record not found");
|
||||
}
|
||||
|
||||
var processedLayer = db.Layers
|
||||
.Where(x => x.ParentId == processWorker.Id
|
||||
&& !x.IsDeleted && !x.IsCancelled)
|
||||
.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}-P-{year}-R1-T1";
|
||||
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 dynamicCodes = processWorker.Records?.Where(x => x.Code!.Contains("DynamicCode-"))
|
||||
.OrderBy(x => int.Parse(x.Code!.Split('-')[1]))
|
||||
.ToList();
|
||||
|
||||
var newRecords = new List<Record>();
|
||||
|
||||
for (var month = 1; month < 14; month++)
|
||||
{
|
||||
if (year > DateTime.UtcNow.Year || ((year == DateTime.UtcNow.Year && month > DateTime.UtcNow.Month && month != 13)))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var records = new List<Record>();
|
||||
foreach (var source in sources)
|
||||
{
|
||||
var monthCopy = month;
|
||||
var dataSource = db.Layers.Where(x =>
|
||||
x.Type == LayerType.Processed &&
|
||||
!x.IsDeleted && !x.IsCancelled &&
|
||||
x.Name != null && x.Name.Contains($"{year}/{monthCopy:D2}-{source.Desc1}-T3")
|
||||
).Include(x => x.Records)
|
||||
.AsNoTracking()
|
||||
.FirstOrDefault();
|
||||
|
||||
if (dataSource == null)
|
||||
{
|
||||
throw new Exception($"Source layer {year}/{monthCopy}-{source.Desc1}-T3 not found.");
|
||||
}
|
||||
|
||||
var codesRecord = processWorker.Records?.Where(x => x.Code == $"Codes-{source.Desc1}").FirstOrDefault();
|
||||
if (codesRecord != null)
|
||||
{
|
||||
var codes = ProcessHelper.ParseCodes(codesRecord.Desc1!);
|
||||
records.AddRange(dataSource.Records!.Where(x => codes.Contains(int.Parse(x.Code!))));
|
||||
}
|
||||
else
|
||||
{
|
||||
records.AddRange(dataSource.Records!);
|
||||
}
|
||||
}
|
||||
|
||||
if (dynamicCodes != null)
|
||||
{
|
||||
foreach (var dynamicCode in dynamicCodes)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (dynamicCode.Desc1 == null)
|
||||
{
|
||||
//TODO throw exception or log warning
|
||||
/*
|
||||
logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = $"{processWorker.Name}, {processWorker.Id}",
|
||||
Type = LogEntryType.Warning,
|
||||
LogType = LogType.Process,
|
||||
Message = $"Formula in Record {dynamicCode.Id} is missing.",
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
*/
|
||||
continue;
|
||||
}
|
||||
|
||||
var calc = new BaseCalc(dynamicCode.Desc1);
|
||||
if (!calc.IsFormulaCorrect())
|
||||
{
|
||||
//TODO throw exception or log warning
|
||||
/*
|
||||
logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = $"{processWorker.Name}, {processWorker.Id}",
|
||||
Type = LogEntryType.Warning,
|
||||
LogType = LogType.Process,
|
||||
Message = $"Formula {calc.Expression} in Record {dynamicCode.Id} is not correct",
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
*/
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
records.Add(calc.CalculateT1(records));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
//TODO throw exception or log warning
|
||||
/*
|
||||
logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = $"{processWorker.Name}, {processWorker.Id}",
|
||||
Type = LogEntryType.Warning,
|
||||
LogType = LogType.Process,
|
||||
Message =
|
||||
$"Formula {calc.Expression} in Record {dynamicCode.Id} error: {e.Message}",
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
*/
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
//TODO throw exception or log warning
|
||||
/*
|
||||
logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = $"{processWorker.Name}, {processWorker.Id}",
|
||||
Type = LogEntryType.Warning,
|
||||
LogType = LogType.Process,
|
||||
Message = $"Calculation error {dynamicCode.Id}: {e.Message} ",
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
*/
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
newRecords.AddRange(records.Select(x => new Record
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Code = $"{x.Code}{month:D2}",
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
ModifiedAt = DateTime.UtcNow,
|
||||
Value1 = x.Value32
|
||||
}
|
||||
));
|
||||
}
|
||||
|
||||
if (isNew)
|
||||
{
|
||||
db.Layers.Add(processedLayer);
|
||||
}
|
||||
else
|
||||
{
|
||||
db.Layers.Update(processedLayer);
|
||||
}
|
||||
//TODO: Save records to the layer
|
||||
//controller.SaveRecords(processedLayer.Id, newRecords, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
|
||||
db.SaveChanges();
|
||||
|
||||
var sheetName = processWorker.Records?.SingleOrDefault(x => x.Code == "GoogleSheetName")?.Desc1;
|
||||
if (sheetName == null)
|
||||
{
|
||||
throw new Exception("GoogleSheetName record not found");
|
||||
}
|
||||
|
||||
UpdateReport(processedLayer.Id, sheetName);
|
||||
}
|
||||
|
||||
private void UpdateReport(Guid sourceId, string sheetName)
|
||||
{
|
||||
const string sheetId = "1pph-XowjlK5CIaCEV_A5buK4ceJ0Z0YoUlDI4VMkhhA";
|
||||
var request = googleSheetValues.Get(sheetId, $"{sheetName}!C4:DA4");
|
||||
var response = request.Execute();
|
||||
|
||||
var r1 = db.Layers
|
||||
.Where(x => x.Id == sourceId)
|
||||
.Include(x => x.Records)
|
||||
.AsNoTracking()
|
||||
.FirstOrDefault();
|
||||
|
||||
var codesRow = response.Values[0];
|
||||
|
||||
var valueRange = new ValueRange
|
||||
{
|
||||
Values = new List<IList<object>>()
|
||||
};
|
||||
|
||||
for (var i = 1; i <= 12; i++)
|
||||
{
|
||||
var values = new List<object>();
|
||||
foreach (string code in codesRow)
|
||||
{
|
||||
var record = r1!.Records?.SingleOrDefault(x => x.Code == $"{code}{i:D2}");
|
||||
if (record != null)
|
||||
{
|
||||
values.Add(record.Value1!.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
values.Add("0");
|
||||
}
|
||||
}
|
||||
valueRange.Values.Add(values);
|
||||
}
|
||||
|
||||
// sum
|
||||
var valuesSum = new List<object>();
|
||||
var emptyRow = new List<object>();
|
||||
foreach (string code in codesRow)
|
||||
{
|
||||
var record = r1!.Records?.SingleOrDefault(x => x.Code == $"{code}13");
|
||||
emptyRow.Add("");
|
||||
if (record != null)
|
||||
{
|
||||
valuesSum.Add(record.Value1!.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
valuesSum.Add("0");
|
||||
}
|
||||
}
|
||||
|
||||
valueRange.Values.Add(emptyRow);
|
||||
valueRange.Values.Add(valuesSum);
|
||||
|
||||
var update = googleSheetValues.Update(valueRange, sheetId, $"{sheetName}!C7:DA20");
|
||||
update.ValueInputOption =
|
||||
SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.USERENTERED;
|
||||
update.Execute();
|
||||
|
||||
// update time
|
||||
var timeUtc = new List<object>
|
||||
{
|
||||
r1!.ModifiedAt.ToString("dd.MM.yyyy HH:mm:ss", CultureInfo.GetCultureInfo("pl-PL"))
|
||||
};
|
||||
var warsawTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
|
||||
var warsawTime = TimeZoneInfo.ConvertTimeFromUtc(r1.ModifiedAt.ToUniversalTime(), warsawTimeZone);
|
||||
var timeWarsaw = new List<object>
|
||||
{
|
||||
warsawTime.ToString("dd.MM.yyyy HH:mm:ss", CultureInfo.GetCultureInfo("pl-PL"))
|
||||
};
|
||||
var valueRangeTime = new ValueRange
|
||||
{
|
||||
Values = new List<IList<object>>()
|
||||
};
|
||||
valueRangeTime.Values.Add(timeUtc);
|
||||
valueRangeTime.Values.Add(timeWarsaw);
|
||||
|
||||
var updateTimeUtc = googleSheetValues.Update(valueRangeTime, sheetId, $"{sheetName}!G1:G2");
|
||||
updateTimeUtc.ValueInputOption =
|
||||
SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.USERENTERED;
|
||||
updateTimeUtc.Execute();
|
||||
}
|
||||
}
|
||||
192
src/Backend/DiunaBI.Plugins.Morska/Processors/t1.r3.processor.cs
Normal file
192
src/Backend/DiunaBI.Plugins.Morska/Processors/t1.r3.processor.cs
Normal file
@@ -0,0 +1,192 @@
|
||||
using System.Globalization;
|
||||
using System.Text.RegularExpressions;
|
||||
using DiunaBI.Core.Services;
|
||||
using Google.Apis.Sheets.v4;
|
||||
using Google.Apis.Sheets.v4.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using DiunaBI.Core.Models;
|
||||
using DiunaBI.Database.Context;
|
||||
|
||||
namespace DiunaBI.Plugins.Morska.Processors;
|
||||
|
||||
public class T1R3Processor(
|
||||
AppDbContext db,
|
||||
SpreadsheetsResource.ValuesResource googleSheetValues)
|
||||
{
|
||||
public void Process(Layer processWorker)
|
||||
{
|
||||
var year = int.Parse(processWorker.Records?.SingleOrDefault(x => x.Code == "Year")?.Desc1!);
|
||||
var source = processWorker.Records?.Where(x => x.Code == "Source").First().Desc1;
|
||||
if (source == null)
|
||||
{
|
||||
throw new Exception("Source record not found");
|
||||
}
|
||||
|
||||
var processedLayer = db.Layers
|
||||
.Where(x => x.ParentId == processWorker.Id
|
||||
&& !x.IsDeleted && !x.IsCancelled)
|
||||
.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}-P-{year}-R3-T1";
|
||||
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>();
|
||||
|
||||
string pattern = @$"^L\d+-P-{year}/\d+-{source}-T5$";
|
||||
var dataSources = db.Layers
|
||||
.Where(x => !x.IsDeleted && !x.IsCancelled)
|
||||
.Include(layer => layer.Records!)
|
||||
.AsNoTracking()
|
||||
.AsEnumerable()
|
||||
.Where(x => Regex.IsMatch(x.Name!, pattern))
|
||||
.ToList();
|
||||
|
||||
foreach (var dataSource in dataSources)
|
||||
{
|
||||
var month = ProcessHelper.ExtractMonthFromLayerName(dataSource.Name!);
|
||||
if (month == null)
|
||||
{
|
||||
throw new Exception($"Month not found: {dataSource.Name}");
|
||||
}
|
||||
|
||||
foreach (var record in dataSource.Records!)
|
||||
{
|
||||
if (record.Value1 == null) continue;
|
||||
for (var i = 1; i < 33; i++)
|
||||
{
|
||||
if (ProcessHelper.GetValue(record, i) == null) continue;
|
||||
|
||||
var newRecord = new Record
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Code = $"{record.Code}{month}{i:D2}",
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
ModifiedAt = DateTime.UtcNow,
|
||||
Value1 = i == 1 ? record.Value1 : record.Value1 * ProcessHelper.GetValue(record, i) / 100,
|
||||
Desc1 = record.Desc1
|
||||
};
|
||||
|
||||
newRecords.Add(newRecord);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (isNew)
|
||||
{
|
||||
db.Layers.Add(processedLayer);
|
||||
}
|
||||
else
|
||||
{
|
||||
db.Layers.Update(processedLayer);
|
||||
}
|
||||
//TODO save records
|
||||
//controller.SaveRecords(processedLayer.Id, newRecords, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
|
||||
db.SaveChanges();
|
||||
|
||||
UpdateReport(processedLayer.Id, year);
|
||||
}
|
||||
|
||||
private void UpdateReport(Guid sourceId, int year)
|
||||
{
|
||||
const string sheetId = "10Xo8BBF92nM7_JzzeOuWp49Gz8OsYuCxLDOeChqpW_8";
|
||||
|
||||
var r3 = db.Layers
|
||||
.Where(x => x.Id == sourceId)
|
||||
.Include(x => x.Records)
|
||||
.AsNoTracking()
|
||||
.FirstOrDefault();
|
||||
|
||||
for (var i = 1; i <= 12; i++)
|
||||
{
|
||||
var sheetName = ProcessHelper.GetSheetName(i, year);
|
||||
ValueRange? dataRangeResponse;
|
||||
try
|
||||
{
|
||||
dataRangeResponse = googleSheetValues.Get(sheetId, $"{sheetName}!A7:A200").Execute();
|
||||
}
|
||||
catch
|
||||
{
|
||||
continue; // Sheet not exist
|
||||
}
|
||||
|
||||
if (dataRangeResponse == null) continue; // Sheet not exist
|
||||
var data = dataRangeResponse.Values;
|
||||
|
||||
var updateValueRange = new ValueRange
|
||||
{
|
||||
Values = new List<IList<object>>()
|
||||
};
|
||||
|
||||
foreach (var row in data)
|
||||
{
|
||||
if (row.Count == 0) continue;
|
||||
var code = row[0].ToString();
|
||||
|
||||
var updateRow = new List<object>();
|
||||
|
||||
for (var j = 1; j < 16; j++)
|
||||
{
|
||||
var codeRecord = r3!.Records!.FirstOrDefault(x => x.Code == $"{code}{i:D2}{j:D2}");
|
||||
if (codeRecord is { Value1: not null })
|
||||
{
|
||||
updateRow.Add(codeRecord.Value1);
|
||||
}
|
||||
else
|
||||
{
|
||||
updateRow.Add("");
|
||||
}
|
||||
}
|
||||
|
||||
updateValueRange.Values.Add(updateRow);
|
||||
}
|
||||
|
||||
dataRangeResponse.Values = data;
|
||||
var update = googleSheetValues.Update(updateValueRange, sheetId, $"{sheetName}!C7:Q200");
|
||||
update.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.USERENTERED;
|
||||
update.Execute();
|
||||
|
||||
// update time
|
||||
var timeUtc = new List<object>
|
||||
{
|
||||
r3!.ModifiedAt.ToString("dd.MM.yyyy HH:mm:ss", CultureInfo.GetCultureInfo("pl-PL"))
|
||||
};
|
||||
var warsawTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
|
||||
var warsawTime = TimeZoneInfo.ConvertTimeFromUtc(r3.ModifiedAt.ToUniversalTime(), warsawTimeZone);
|
||||
var timeWarsaw = new List<object>
|
||||
{
|
||||
warsawTime.ToString("dd.MM.yyyy HH:mm:ss", CultureInfo.GetCultureInfo("pl-PL"))
|
||||
};
|
||||
var valueRangeTime = new ValueRange
|
||||
{
|
||||
Values = new List<IList<object>>()
|
||||
};
|
||||
valueRangeTime.Values.Add(timeUtc);
|
||||
valueRangeTime.Values.Add(timeWarsaw);
|
||||
|
||||
var updateTimeUtc = googleSheetValues.Update(valueRangeTime, sheetId, $"{sheetName}!G1:G2");
|
||||
updateTimeUtc.ValueInputOption =
|
||||
SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.USERENTERED;
|
||||
updateTimeUtc.Execute();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
using DiunaBI.Core.Services;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using DiunaBI.Core.Models;
|
||||
using DiunaBI.Database.Context;
|
||||
|
||||
namespace DiunaBI.Plugins.Morska.Processors;
|
||||
|
||||
public class T3MultiSourceCopySelectedCodesProcessor(
|
||||
AppDbContext db)
|
||||
{
|
||||
public void Process(Layer processWorker)
|
||||
{
|
||||
var year = int.Parse(processWorker.Records?.SingleOrDefault(x => x.Code == "Year")?.Desc1!);
|
||||
var month = int.Parse(processWorker.Records?.SingleOrDefault(x => x.Code == "Month")?.Desc1!);
|
||||
var sources = processWorker.Records?.Where(x => x.Code == "Source").ToList();
|
||||
if (sources!.Count == 0)
|
||||
{
|
||||
throw new Exception("Source record not found");
|
||||
}
|
||||
var codes = processWorker.Records?.SingleOrDefault(x => x.Code == "Codes")?.Desc1;
|
||||
if (codes == null)
|
||||
{
|
||||
throw new Exception("Codes record not found");
|
||||
}
|
||||
|
||||
var codesList = ProcessHelper.ParseCodes(codes);
|
||||
|
||||
var processedLayer = db.Layers
|
||||
.Where(x => x.ParentId == processWorker.Id
|
||||
&& !x.IsDeleted && !x.IsCancelled)
|
||||
.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}-P-{year}/{month:D2}-AB-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.ModifiedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D");
|
||||
processedLayer.ModifiedAt = DateTime.UtcNow;
|
||||
|
||||
var dataSources = sources.Select(source => db.Layers
|
||||
.Where(x => x.Type == LayerType.Processed && !x.IsDeleted && !x.IsCancelled && x.Name != null && x.Name.Contains($"{year}/{month:D2}-{source.Desc1}-T3"))
|
||||
.Include(x => x.Records).AsNoTracking()
|
||||
.FirstOrDefault())
|
||||
.OfType<Layer>()
|
||||
.ToList();
|
||||
if (dataSources.Count == 0)
|
||||
{
|
||||
throw new Exception("DataSources are empty");
|
||||
}
|
||||
|
||||
|
||||
var newRecords = dataSources
|
||||
.SelectMany(x => x.Records!)
|
||||
.Where(x => codesList.Contains(int.Parse(x.Code!)))
|
||||
.Select(x =>
|
||||
{
|
||||
var newRecord = new Record
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Code = x.Code,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
ModifiedAt = DateTime.UtcNow
|
||||
};
|
||||
for (var i = 1; i < 33; i++)
|
||||
{
|
||||
ProcessHelper.SetValue(newRecord, i, ProcessHelper.GetValue(x, i));
|
||||
}
|
||||
return newRecord;
|
||||
})
|
||||
.ToList();
|
||||
if (isNew)
|
||||
{
|
||||
db.Layers.Add(processedLayer);
|
||||
}
|
||||
else
|
||||
{
|
||||
db.Layers.Update(processedLayer);
|
||||
}
|
||||
//TODO: Save records
|
||||
//controller.SaveRecords(processedLayer.Id, newRecords, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
|
||||
db.SaveChanges();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
using DiunaBI.Core.Services;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using DiunaBI.Core.Models;
|
||||
using DiunaBI.Database.Context;
|
||||
|
||||
namespace DiunaBI.Plugins.Morska.Processors;
|
||||
|
||||
public class T3MultiSourceCopySelectedCodesYearSummaryProcessor(
|
||||
AppDbContext db)
|
||||
{
|
||||
public void Process(Layer processWorker)
|
||||
{
|
||||
var year = int.Parse(processWorker.Records?.SingleOrDefault(x => x.Code == "Year")?.Desc1!);
|
||||
|
||||
var processedLayer = db.Layers
|
||||
.Where(x => x.ParentId == processWorker.Id
|
||||
&& !x.IsDeleted && !x.IsCancelled)
|
||||
.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}-P-{year}/13-AB-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.ModifiedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D");
|
||||
processedLayer.ModifiedAt = DateTime.UtcNow;
|
||||
|
||||
var newRecords = new List<Record>();
|
||||
|
||||
var dataSources = new List<Layer>();
|
||||
|
||||
for (var i = 1; i < 13; i++)
|
||||
{
|
||||
var j = i;
|
||||
var dataSource = db.Layers.Where(x =>
|
||||
x.Type == LayerType.Processed
|
||||
&& !x.IsDeleted && !x.IsCancelled
|
||||
&& x.Name != null && x.Name.Contains($"{year}/{j:D2}-AB-T3"))
|
||||
.Include(x => x.Records)
|
||||
.AsNoTracking()
|
||||
.FirstOrDefault();
|
||||
if (dataSource != null)
|
||||
{
|
||||
dataSources.Add(dataSource);
|
||||
}
|
||||
}
|
||||
|
||||
if (dataSources.Count == 0)
|
||||
{
|
||||
throw new Exception("DataSources are empty");
|
||||
}
|
||||
|
||||
var allRecords = dataSources.SelectMany(x => x.Records!).ToList();
|
||||
|
||||
foreach (var baseRecord in dataSources.Last().Records!)
|
||||
{
|
||||
var codeRecords = allRecords.Where(x => x.Code == baseRecord.Code).ToList();
|
||||
var processedRecord = new Record
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Code = baseRecord.Code,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
ModifiedAt = DateTime.UtcNow
|
||||
};
|
||||
for (var i = 1; i < 33; i++)
|
||||
{
|
||||
ProcessHelper.SetValue(processedRecord, i,
|
||||
codeRecords.Sum(x => ProcessHelper.GetValue(x, i)));
|
||||
}
|
||||
|
||||
newRecords.Add(processedRecord);
|
||||
}
|
||||
|
||||
if (isNew)
|
||||
{
|
||||
db.Layers.Add(processedLayer);
|
||||
}
|
||||
else
|
||||
{
|
||||
db.Layers.Update(processedLayer);
|
||||
}
|
||||
//TODO: save records
|
||||
//controller.SaveRecords(processedLayer.Id, newRecords, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
|
||||
db.SaveChanges();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
using DiunaBI.Core.Services;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using DiunaBI.Core.Models;
|
||||
using DiunaBI.Database.Context;
|
||||
using DiunaBI.Core.Services.Calculations;
|
||||
|
||||
namespace DiunaBI.Plugins.Morska.Processors;
|
||||
|
||||
public class T3MultiSourceSummaryProcessor(
|
||||
AppDbContext db)
|
||||
{
|
||||
public void Process(Layer processWorker)
|
||||
{
|
||||
var year = int.Parse(processWorker.Records?.SingleOrDefault(x => x.Code == "Year")?.Desc1!);
|
||||
var month = int.Parse(processWorker.Records?.SingleOrDefault(x => x.Code == "Month")?.Desc1!);
|
||||
var sources = processWorker.Records?.Where(x => x.Code == "Source").ToList();
|
||||
if (sources!.Count == 0)
|
||||
{
|
||||
throw new Exception("Source record not found");
|
||||
}
|
||||
|
||||
var processedLayer = db.Layers
|
||||
.Where(x => x.ParentId == processWorker.Id
|
||||
&& !x.IsDeleted && !x.IsCancelled)
|
||||
.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}-P-{year}/{month:D2}-AA-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.ModifiedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D");
|
||||
processedLayer.ModifiedAt = DateTime.UtcNow;
|
||||
|
||||
var newRecords = new List<Record>();
|
||||
|
||||
var dataSources = sources.Select(source => db.Layers.Where(x => x.Type == LayerType.Processed && !x.IsDeleted && !x.IsCancelled && x.Name != null && x.Name.Contains($"{year}/{month:D2}-{source.Desc1}-T3"))
|
||||
.Include(x => x.Records)
|
||||
.AsNoTracking()
|
||||
.FirstOrDefault())
|
||||
.OfType<Layer>()
|
||||
.ToList();
|
||||
|
||||
if (dataSources.Count == 0)
|
||||
{
|
||||
throw new Exception("DataSources are empty");
|
||||
}
|
||||
|
||||
var allRecords = dataSources.SelectMany(x => x.Records!).ToList();
|
||||
var baseCodes = allRecords.Select(x => x.Code!.Remove(0, 1)).Distinct().ToList();
|
||||
|
||||
foreach (var baseCode in baseCodes)
|
||||
{
|
||||
|
||||
var codeRecords = allRecords.Where(x =>
|
||||
x.Code![1..] == baseCode)
|
||||
.ToList();
|
||||
var processedRecord = new Record
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Code = $"9{baseCode}",
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
ModifiedAt = DateTime.UtcNow
|
||||
};
|
||||
for (var i = 1; i < 33; i++)
|
||||
{
|
||||
ProcessHelper.SetValue(processedRecord, i,
|
||||
codeRecords.Sum(x => ProcessHelper.GetValue(x, i)));
|
||||
}
|
||||
newRecords.Add(processedRecord);
|
||||
}
|
||||
|
||||
// Dynamic Codes
|
||||
var dynamicCodes = processWorker.Records?
|
||||
.Where(x => x.Code!.Contains("DynamicCode-"))
|
||||
.OrderBy(x => int.Parse(x.Code!.Split('-')[1])).ToList();
|
||||
if (dynamicCodes != null && dynamicCodes.Count != 0)
|
||||
{
|
||||
foreach (var dynamicCode in dynamicCodes)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (dynamicCode.Desc1 == null)
|
||||
{
|
||||
//TODO: log warning
|
||||
/*
|
||||
logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = $"{processWorker.Name}, {processWorker.Id}",
|
||||
Type = LogEntryType.Warning,
|
||||
LogType = LogType.Process,
|
||||
Message = $"Formula in Record {dynamicCode.Id} is missing.",
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
*/
|
||||
continue;
|
||||
}
|
||||
var calc = new BaseCalc(dynamicCode.Desc1);
|
||||
if (!calc.IsFormulaCorrect())
|
||||
{
|
||||
//TODO: log warning
|
||||
/*
|
||||
logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = $"{processWorker.Name}, {processWorker.Id}",
|
||||
Type = LogEntryType.Warning,
|
||||
LogType = LogType.Process,
|
||||
Message = $"Formula {calc.Expression} in Record {dynamicCode.Id} is not correct",
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
*/
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
newRecords.Add(calc.CalculateT3(newRecords));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
//TODO: log warning
|
||||
/*
|
||||
logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = $"{processWorker.Name}, {processWorker.Id}",
|
||||
Type = LogEntryType.Warning,
|
||||
LogType = LogType.Process,
|
||||
Message = $"Formula {calc.Expression} in Record {dynamicCode.Id} error: {e.Message}",
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
*/
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
// TODO: log warning
|
||||
/*
|
||||
logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = $"{processWorker.Name}, {processWorker.Id}",
|
||||
Type = LogEntryType.Warning,
|
||||
LogType = LogType.Process,
|
||||
Message = $"Calculation error {dynamicCode.Id}: {e.Message} ",
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
*/
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (isNew)
|
||||
{
|
||||
db.Layers.Add(processedLayer);
|
||||
}
|
||||
else
|
||||
{
|
||||
db.Layers.Update(processedLayer);
|
||||
}
|
||||
//TODO: save records
|
||||
//controller.SaveRecords(processedLayer.Id, newRecords, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
|
||||
db.SaveChanges();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,198 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using DiunaBI.Core.Models;
|
||||
using DiunaBI.Database.Context;
|
||||
using DiunaBI.Core.Services;
|
||||
using DiunaBI.Core.Services.Calculations;
|
||||
|
||||
namespace DiunaBI.Plugins.Morska.Processors;
|
||||
|
||||
public class T3MultiSourceYearSummaryProcessor(
|
||||
AppDbContext db)
|
||||
{
|
||||
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!.Count == 0)
|
||||
{
|
||||
throw new Exception("Source record not found");
|
||||
}
|
||||
|
||||
var processedLayer = db.Layers
|
||||
.Where(x => x.ParentId == processWorker.Id
|
||||
&& !x.IsDeleted && !x.IsCancelled)
|
||||
.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}-P-{year}/13-AA-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.ModifiedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D");
|
||||
processedLayer.ModifiedAt = DateTime.UtcNow;
|
||||
|
||||
var newRecords = new List<Record>();
|
||||
|
||||
var dataSources = sources.Select(source => db.Layers.Where(x => x.Type == LayerType.Processed && !x.IsDeleted && !x.IsCancelled && x.Name != null && x.Name.Contains($"{year}/13-{source.Desc1}-T3"))
|
||||
.Include(x => x.Records)
|
||||
.AsNoTracking()
|
||||
.FirstOrDefault())
|
||||
.OfType<Layer>()
|
||||
.ToList();
|
||||
if (dataSources.Count == 0)
|
||||
{
|
||||
throw new Exception("DataSources are empty");
|
||||
}
|
||||
|
||||
if (dataSources.Count == 0)
|
||||
{
|
||||
throw new Exception("DataSourcesValidation are empty");
|
||||
}
|
||||
|
||||
var allRecords = dataSources
|
||||
.SelectMany(x => x.Records!).ToList();
|
||||
var baseCodes = allRecords.Select(x => x.Code!.Remove(0, 1)).Distinct().ToList();
|
||||
|
||||
foreach (var baseCode in baseCodes)
|
||||
{
|
||||
|
||||
var codeRecords = allRecords.Where(x =>
|
||||
x.Code![1..] == baseCode)
|
||||
.ToList();
|
||||
var codeRecordsValidation = allRecords.Where(x =>
|
||||
x.Code![1..] == baseCode)
|
||||
.ToList();
|
||||
var processedRecord = new Record
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Code = $"9{baseCode}",
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
ModifiedAt = DateTime.UtcNow
|
||||
};
|
||||
var validationRecord = new Record();
|
||||
for (var i = 1; i < 33; i++)
|
||||
{
|
||||
ProcessHelper.SetValue(processedRecord, i,
|
||||
codeRecords.Sum(x => ProcessHelper.GetValue(x, i)));
|
||||
|
||||
ProcessHelper.SetValue(validationRecord, i,
|
||||
codeRecordsValidation.Sum(x => ProcessHelper.GetValue(x, i)));
|
||||
|
||||
if (
|
||||
double.Abs((double)(ProcessHelper.GetValue(processedRecord, i) -
|
||||
ProcessHelper.GetValue(validationRecord, i))!) > 0.01)
|
||||
{
|
||||
throw new Exception($"ValidationError: Code {baseCode}, " +
|
||||
$"Value{i} ({ProcessHelper.GetValue(processedRecord, i)} | " +
|
||||
$"{ProcessHelper.GetValue(validationRecord, i)})");
|
||||
}
|
||||
}
|
||||
newRecords.Add(processedRecord);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Dynamic Codes
|
||||
var dynamicCodes = processWorker.Records?
|
||||
.Where(x => x.Code!.Contains("DynamicCode-"))
|
||||
.OrderBy(x => int.Parse(x.Code!.Split('-')[1])).ToList();
|
||||
if (dynamicCodes != null && dynamicCodes.Count != 0)
|
||||
{
|
||||
foreach (var dynamicCode in dynamicCodes)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (dynamicCode.Desc1 == null)
|
||||
{
|
||||
//TODO: log warning
|
||||
/*
|
||||
logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = $"{processWorker.Name}, {processWorker.Id}",
|
||||
Type = LogEntryType.Warning,
|
||||
LogType = LogType.Process,
|
||||
Message = $"Formula in Record {dynamicCode.Id} is missing.",
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
*/
|
||||
continue;
|
||||
}
|
||||
var calc = new BaseCalc(dynamicCode.Desc1);
|
||||
if (!calc.IsFormulaCorrect())
|
||||
{
|
||||
//TODO: log warning
|
||||
/*
|
||||
logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = $"{processWorker.Name}, {processWorker.Id}",
|
||||
Type = LogEntryType.Warning,
|
||||
LogType = LogType.Process,
|
||||
Message = $"Formula {calc.Expression} in Record {dynamicCode.Id} is not correct",
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
*/
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
newRecords.Add(calc.CalculateT3(newRecords));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
//TODO: log warning
|
||||
/*
|
||||
logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = $"{processWorker.Name}, {processWorker.Id}",
|
||||
Type = LogEntryType.Warning,
|
||||
LogType = LogType.Process,
|
||||
Message = $"Formula {calc.Expression} in Record {dynamicCode.Id} error: {e.Message}",
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
*/
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
//TODO: log warning
|
||||
/*
|
||||
logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = $"{processWorker.Name}, {processWorker.Id}",
|
||||
Type = LogEntryType.Warning,
|
||||
LogType = LogType.Process,
|
||||
Message = $"Calculation error {dynamicCode.Id}: {e.Message} ",
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
*/
|
||||
}
|
||||
}
|
||||
}
|
||||
if (isNew)
|
||||
{
|
||||
db.Layers.Add(processedLayer);
|
||||
}
|
||||
else
|
||||
{
|
||||
db.Layers.Update(processedLayer);
|
||||
}
|
||||
//TODO: save records
|
||||
//controller.SaveRecords(processedLayer.Id, newRecords, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
|
||||
db.SaveChanges();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using DiunaBI.Core.Models;
|
||||
using DiunaBI.Database.Context;
|
||||
using DiunaBI.Core.Services;
|
||||
|
||||
namespace DiunaBI.Plugins.Morska.Processors;
|
||||
|
||||
public class T3SingleSourceProcessor(
|
||||
AppDbContext db)
|
||||
{
|
||||
public void Process(Layer processWorker)
|
||||
{
|
||||
var year = int.Parse(processWorker.Records?.SingleOrDefault(x => x.Code == "Year")?.Desc1!);
|
||||
var month = int.Parse(processWorker.Records?.SingleOrDefault(x => x.Code == "Month")?.Desc1!);
|
||||
var sourceLayer = processWorker.Records?.SingleOrDefault(x => x.Code == "SourceLayer")?.Desc1;
|
||||
if (sourceLayer == null)
|
||||
{
|
||||
throw new Exception("SourceLayer record not found");
|
||||
}
|
||||
var sourceImportWorker = db.Layers.SingleOrDefault(x => x.Name == sourceLayer && !x.IsDeleted && !x.IsCancelled);
|
||||
if (sourceImportWorker == null)
|
||||
{
|
||||
throw new Exception("SourceImportWorkerL layer not found");
|
||||
}
|
||||
var source = processWorker.Records?.SingleOrDefault(x => x.Code == "Source")?.Desc1;
|
||||
if (sourceLayer == null)
|
||||
{
|
||||
throw new Exception("Source record not found");
|
||||
}
|
||||
|
||||
var processedLayer = db.Layers
|
||||
.Where(x => x.ParentId == processWorker.Id)
|
||||
.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}-P-{year}/{month:D2}-{source}-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.ModifiedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D");
|
||||
processedLayer.ModifiedAt = DateTime.UtcNow;
|
||||
|
||||
|
||||
var newRecords = new List<Record>();
|
||||
|
||||
var dataSources = db.Layers
|
||||
.Include(x => x.Records)
|
||||
.Where(x => x.ParentId == sourceImportWorker.Id
|
||||
&& !x.IsDeleted && !x.IsCancelled)
|
||||
.OrderBy(x => x.CreatedAt)
|
||||
.AsNoTracking()
|
||||
.ToList();
|
||||
if (dataSources.Count == 0)
|
||||
{
|
||||
throw new Exception($"DataSources are empty, {sourceImportWorker.Name}");
|
||||
}
|
||||
|
||||
var allRecords = dataSources.SelectMany(x => x.Records!).ToList();
|
||||
|
||||
foreach (var baseRecord in dataSources.Last().Records!)
|
||||
{
|
||||
var codeRecords = allRecords.Where(x => x.Code == baseRecord.Code).ToList();
|
||||
|
||||
var processedRecord = new Record
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Code = baseRecord.Code,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
ModifiedAt = DateTime.UtcNow
|
||||
};
|
||||
|
||||
var lastDayInMonth = DateTime.DaysInMonth(year, month);
|
||||
//day 1
|
||||
var firstVal = codeRecords
|
||||
.Where(x => x.CreatedAt.Date <= new DateTime(year, month, 1)).MaxBy(x => x.CreatedAt)?.Value1 ?? 0;
|
||||
ProcessHelper.SetValue(processedRecord, 1, firstVal);
|
||||
var previousValue = firstVal;
|
||||
//days 2-29/30
|
||||
for (var i = 2; i < lastDayInMonth; i++)
|
||||
{
|
||||
var dayVal = codeRecords
|
||||
.Where(x => x.CreatedAt.Day == i && x.CreatedAt.Month == month).MaxBy(x => x.CreatedAt)?.Value1;
|
||||
if (dayVal == null)
|
||||
{
|
||||
ProcessHelper.SetValue(processedRecord, i, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
var processedVal = dayVal - previousValue;
|
||||
ProcessHelper.SetValue(processedRecord, i, processedVal);
|
||||
previousValue = (double)dayVal;
|
||||
}
|
||||
}
|
||||
//last day
|
||||
var lastVal = codeRecords
|
||||
.Where(x => x.CreatedAt.Date >= new DateTime(year, month, lastDayInMonth)).MaxBy(x => x.CreatedAt)?.Value1;
|
||||
|
||||
if (lastVal == null)
|
||||
{
|
||||
ProcessHelper.SetValue(processedRecord, lastDayInMonth, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
ProcessHelper.SetValue(processedRecord, lastDayInMonth, (double)lastVal - previousValue);
|
||||
}
|
||||
|
||||
// copy last value
|
||||
var valueToCopy = codeRecords.MaxBy(x => x.CreatedAt)?.Value1;
|
||||
ProcessHelper.SetValue(processedRecord, 32, valueToCopy);
|
||||
|
||||
newRecords.Add(processedRecord);
|
||||
}
|
||||
|
||||
if (isNew)
|
||||
{
|
||||
db.Layers.Add(processedLayer);
|
||||
}
|
||||
else
|
||||
{
|
||||
db.Layers.Update(processedLayer);
|
||||
}
|
||||
//TODO: save records
|
||||
//controller.SaveRecords(processedLayer.Id, newRecords, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
|
||||
db.SaveChanges();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using DiunaBI.Core.Models;
|
||||
using DiunaBI.Database.Context;
|
||||
using DiunaBI.Core.Services;
|
||||
|
||||
namespace DiunaBI.Plugins.Morska.Processors;
|
||||
|
||||
public class T3SourceYearSummaryProcessor(
|
||||
AppDbContext db)
|
||||
{
|
||||
public void Process(Layer processWorker)
|
||||
{
|
||||
var year = processWorker.Records?.SingleOrDefault(x => x.Code == "Year")?.Desc1;
|
||||
var source = processWorker.Records?.SingleOrDefault(x => x.Code == "Source")?.Desc1;
|
||||
if (source == null)
|
||||
{
|
||||
throw new Exception("Source record not found");
|
||||
}
|
||||
var processedLayer = db.Layers
|
||||
.Where(x => x.ParentId == processWorker.Id
|
||||
&& !x.IsDeleted && !x.IsCancelled)
|
||||
.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}-P-{year}/13-{source}-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.ModifiedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D");
|
||||
processedLayer.ModifiedAt = DateTime.UtcNow;
|
||||
|
||||
var newRecords = new List<Record>();
|
||||
|
||||
var dataSources = new List<Layer>();
|
||||
for (var i = 1; i < 13; i++)
|
||||
{
|
||||
var j = i;
|
||||
var dataSource = db.Layers.Where(x =>
|
||||
x.Type == LayerType.Processed
|
||||
&& !x.IsDeleted && !x.IsCancelled
|
||||
&& x.Name != null && x.Name.Contains($"{year}/{j:D2}-{source}-T3"))
|
||||
.Include(x => x.Records)
|
||||
.AsNoTracking()
|
||||
.FirstOrDefault();
|
||||
if (dataSource != null)
|
||||
{
|
||||
dataSources.Add(dataSource);
|
||||
}
|
||||
}
|
||||
|
||||
if (dataSources.Count == 0)
|
||||
{
|
||||
throw new Exception("DataSources are empty");
|
||||
}
|
||||
|
||||
var allRecords = dataSources.SelectMany(x => x.Records!).ToList();
|
||||
|
||||
foreach (var baseRecord in dataSources.Last().Records!)
|
||||
{
|
||||
var codeRecords = allRecords.Where(x => x.Code == baseRecord.Code).ToList();
|
||||
var processedRecord = new Record
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Code = baseRecord.Code,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
ModifiedAt = DateTime.UtcNow
|
||||
};
|
||||
for (var i = 1; i < 33; i++)
|
||||
{
|
||||
ProcessHelper.SetValue(processedRecord, i,
|
||||
codeRecords.Sum(x => ProcessHelper.GetValue(x, i)));
|
||||
}
|
||||
newRecords.Add(processedRecord);
|
||||
}
|
||||
|
||||
if (isNew)
|
||||
{
|
||||
db.Layers.Add(processedLayer);
|
||||
}
|
||||
else
|
||||
{
|
||||
db.Layers.Update(processedLayer);
|
||||
}
|
||||
//TODO: save records
|
||||
//controller.SaveRecords(processedLayer.Id, newRecords, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
|
||||
db.SaveChanges();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using DiunaBI.Core.Models;
|
||||
using DiunaBI.Database.Context;
|
||||
|
||||
namespace DiunaBI.Plugins.Morska.Processors;
|
||||
|
||||
public class T4SingleSourceProcessor(
|
||||
AppDbContext db)
|
||||
{
|
||||
public void Process(Layer processWorker)
|
||||
{
|
||||
var year = int.Parse(processWorker.Records?.SingleOrDefault(x => x.Code == "Year")?.Desc1!);
|
||||
var month = int.Parse(processWorker.Records?.SingleOrDefault(x => x.Code == "Month")?.Desc1!);
|
||||
var sourceLayer = processWorker.Records?.SingleOrDefault(x => x.Code == "SourceLayer")?.Desc1;
|
||||
if (sourceLayer == null)
|
||||
{
|
||||
throw new Exception("SourceLayer record not found");
|
||||
}
|
||||
var sourceImportWorker = db.Layers.SingleOrDefault(x => x.Name == sourceLayer && !x.IsDeleted && !x.IsCancelled);
|
||||
if (sourceImportWorker == null)
|
||||
{
|
||||
throw new Exception("SourceImportWorkerL layer not found");
|
||||
}
|
||||
var source = processWorker.Records?.SingleOrDefault(x => x.Code == "Source")?.Desc1;
|
||||
if (sourceLayer == null)
|
||||
{
|
||||
throw new Exception("Source record not found");
|
||||
}
|
||||
|
||||
var processedLayer = db.Layers
|
||||
.Where(x => x.ParentId == processWorker.Id &&
|
||||
!x.IsDeleted && !x.IsCancelled)
|
||||
.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}-P-{year}/{month:D2}-{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.ModifiedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D");
|
||||
processedLayer.ModifiedAt = DateTime.UtcNow;
|
||||
|
||||
|
||||
var dataSource = db.Layers
|
||||
.Include(x => x.Records)
|
||||
.Where(x => x.ParentId == sourceImportWorker.Id
|
||||
&& !x.IsDeleted && !x.IsCancelled)
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.AsNoTracking()
|
||||
.FirstOrDefault();
|
||||
|
||||
if (dataSource == null)
|
||||
{
|
||||
throw new Exception($"DataSource not found, {sourceImportWorker.Name}");
|
||||
}
|
||||
|
||||
var newRecords = dataSource.Records!.Select(record => new Record
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Code = record.Code,
|
||||
Desc1 = record.Desc1,
|
||||
Value1 = record.Value1,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
ModifiedAt = DateTime.UtcNow
|
||||
})
|
||||
.ToList();
|
||||
|
||||
if (isNew)
|
||||
{
|
||||
db.Layers.Add(processedLayer);
|
||||
}
|
||||
else
|
||||
{
|
||||
db.Layers.Update(processedLayer);
|
||||
}
|
||||
// TODO: save records
|
||||
//controller.SaveRecords(processedLayer.Id, newRecords, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
|
||||
db.SaveChanges();
|
||||
}
|
||||
|
||||
}
|
||||
362
src/Backend/DiunaBI.Plugins.Morska/Processors/t4.r2.processor.cs
Normal file
362
src/Backend/DiunaBI.Plugins.Morska/Processors/t4.r2.processor.cs
Normal file
@@ -0,0 +1,362 @@
|
||||
using System.Globalization;
|
||||
using Google.Apis.Sheets.v4;
|
||||
using Google.Apis.Sheets.v4.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using DiunaBI.Core.Models;
|
||||
using DiunaBI.Database.Context;
|
||||
using DiunaBI.Core.Services;
|
||||
|
||||
|
||||
namespace DiunaBI.Plugins.Morska.Processors;
|
||||
|
||||
public class T4R2Processor(
|
||||
AppDbContext db,
|
||||
SpreadsheetsResource.ValuesResource googleSheetValues)
|
||||
{
|
||||
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!.Count == 0)
|
||||
{
|
||||
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 && !x.IsCancelled)
|
||||
.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);
|
||||
}
|
||||
|
||||
List<string> lastSourceCodes = [];
|
||||
|
||||
for (var month = 1; month <= 12; month++)
|
||||
{
|
||||
if ((year == DateTime.UtcNow.Year && month <= DateTime.UtcNow.Month) || year < DateTime.UtcNow.Year)
|
||||
{
|
||||
var monthCopy = month;
|
||||
var dataSource = db.Layers.Where(x =>
|
||||
x.Type == LayerType.Processed &&
|
||||
!x.IsDeleted && !x.IsCancelled &&
|
||||
x.Name != null && x.Name.Contains($"{year}/{monthCopy:D2}-{source.Desc1}-T")
|
||||
)
|
||||
.Include(x => x.Records)
|
||||
.AsNoTracking()
|
||||
.FirstOrDefault();
|
||||
if (dataSource != null)
|
||||
{
|
||||
lastSourceCodes = dataSource.Records!.Select(x => x.Code!).ToList();
|
||||
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: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
|
||||
{
|
||||
// TODO: log warning
|
||||
/*
|
||||
logsController.AddEntry(new LogEntry
|
||||
{
|
||||
Title = $"{processWorker.Name}, {processWorker.Id}",
|
||||
Type = LogEntryType.Warning,
|
||||
LogType = LogType.Process,
|
||||
Message = $"Data source {year}/{month:D2}-{source.Desc1}-T3 not found",
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
*/
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//0 values for future months
|
||||
if (source.Desc1 == "FK2" || lastSourceCodes.Count <= 0) continue;
|
||||
var news = lastSourceCodes
|
||||
.Where(x => sourceCodes.Contains(int.Parse(x)))
|
||||
.Select(x =>
|
||||
{
|
||||
var newRecord = new Record
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Code = $"{x}{month:D2}",
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
ModifiedAt = DateTime.UtcNow,
|
||||
Value1 = 0,
|
||||
};
|
||||
return newRecord;
|
||||
}
|
||||
).ToList();
|
||||
newRecords.AddRange(news);
|
||||
}
|
||||
}
|
||||
|
||||
// year summary
|
||||
var dataSourceSum = db.Layers.Where(x =>
|
||||
x.Type == LayerType.Processed &&
|
||||
!x.IsDeleted && !x.IsCancelled &&
|
||||
x.Name != null && x.Name.Contains($"{year}/13-{source.Desc1}-T")
|
||||
)
|
||||
.Include(x => x.Records)
|
||||
.AsNoTracking()
|
||||
.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
|
||||
{
|
||||
// TODO: log warning
|
||||
/*
|
||||
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);
|
||||
}
|
||||
// TODO: save records
|
||||
//controller.SaveRecords(processedLayer.Id, newRecords, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
|
||||
db.SaveChanges();
|
||||
|
||||
var reportSheetName = processWorker.Records?.SingleOrDefault(x => x.Code == "GoogleSheetName")?.Desc1;
|
||||
if (reportSheetName == null)
|
||||
{
|
||||
throw new Exception("GoogleSheetName record not found");
|
||||
}
|
||||
|
||||
var invoicesSheetName = processWorker.Records?.SingleOrDefault(x => x.Code == "GoogleSheetName-Invoices")?.Desc1;
|
||||
if (invoicesSheetName == null)
|
||||
{
|
||||
throw new Exception("GoogleSheetName-Invoices record not found");
|
||||
}
|
||||
UpdateReport(processedLayer.Id, reportSheetName, invoicesSheetName);
|
||||
}
|
||||
|
||||
private void UpdateReport(Guid sourceId, string reportSheetName, string invoicesSheetName)
|
||||
{
|
||||
const string sheetId = "1FsUmk_YRIeeGzFCX9tuUJCaLyRtjutX2ZGAEU1DMfJQ";
|
||||
var request = googleSheetValues.Get(sheetId, "C4:Z4");
|
||||
var response = request.Execute();
|
||||
|
||||
var r2 = db.Layers
|
||||
.Where(x => x.Id == sourceId && !x.IsDeleted && !x.IsCancelled)
|
||||
.Include(x => x.Records)
|
||||
.AsNoTracking()
|
||||
.FirstOrDefault();
|
||||
|
||||
const int startRow = 6;
|
||||
|
||||
var codesRow = response.Values[0];
|
||||
for (var i = 1; i <= 12; i++)
|
||||
{
|
||||
var values = new List<object>();
|
||||
var month = i < 10 ? $"0{i}" : i.ToString();
|
||||
var row = (startRow + i).ToString();
|
||||
foreach (string code in codesRow)
|
||||
{
|
||||
var record = r2!.Records?.SingleOrDefault(x => x.Code == $"{code}{month}");
|
||||
if (record != null)
|
||||
{
|
||||
values.Add(record.Value1!.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
values.Add("0");
|
||||
}
|
||||
}
|
||||
var valueRange = new ValueRange
|
||||
{
|
||||
Values = new List<IList<object>> { values }
|
||||
};
|
||||
var update = googleSheetValues.Update(valueRange, sheetId, $"{reportSheetName}!C{row}:XZ{row}");
|
||||
update.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.USERENTERED;
|
||||
update.Execute();
|
||||
}
|
||||
|
||||
// sum
|
||||
var valuesSum = new List<object>();
|
||||
var emptyRow = new List<object>();
|
||||
var rowEmpty = (startRow + 13).ToString();
|
||||
var rowSum = (startRow + 14).ToString();
|
||||
foreach (string code in codesRow)
|
||||
{
|
||||
var record = r2!.Records?.SingleOrDefault(x => x.Code == $"{code}13");
|
||||
emptyRow.Add("");
|
||||
if (record != null)
|
||||
{
|
||||
valuesSum.Add(record.Value1!.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
valuesSum.Add("0");
|
||||
}
|
||||
}
|
||||
// insert empty row before sum
|
||||
var valueRangeEmpty = new ValueRange
|
||||
{
|
||||
Values = new List<IList<object>> { emptyRow }
|
||||
};
|
||||
var updateEmpty = googleSheetValues.Update(valueRangeEmpty, sheetId, $"{reportSheetName}!C{rowEmpty}:XZ{rowEmpty}");
|
||||
updateEmpty.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.USERENTERED;
|
||||
updateEmpty.Execute();
|
||||
|
||||
var valueRangeSum = new ValueRange
|
||||
{
|
||||
Values = new List<IList<object>> { valuesSum }
|
||||
};
|
||||
var updateSum = googleSheetValues.Update(valueRangeSum, sheetId, $"{reportSheetName}!C{rowSum}:XZ{rowSum}");
|
||||
updateSum.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.USERENTERED;
|
||||
updateSum.Execute();
|
||||
|
||||
// update time
|
||||
var timeUtc = new List<object>
|
||||
{
|
||||
r2!.ModifiedAt.ToString("dd.MM.yyyy HH:mm:ss", CultureInfo.GetCultureInfo("pl-PL"))
|
||||
};
|
||||
var valueRangeUtcTime = new ValueRange
|
||||
{
|
||||
Values = new List<IList<object>> { timeUtc }
|
||||
};
|
||||
var updateTimeUtc = googleSheetValues.Update(valueRangeUtcTime, sheetId, $"{reportSheetName}!G1");
|
||||
updateTimeUtc.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.USERENTERED;
|
||||
updateTimeUtc.Execute();
|
||||
|
||||
var warsawTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
|
||||
var warsawTime = TimeZoneInfo.ConvertTimeFromUtc(r2.ModifiedAt.ToUniversalTime(), warsawTimeZone);
|
||||
var timeWarsaw = new List<object>
|
||||
{
|
||||
warsawTime.ToString("dd.MM.yyyy HH:mm:ss", CultureInfo.GetCultureInfo("pl-PL"))
|
||||
};
|
||||
var valueRangeWarsawTime = new ValueRange
|
||||
{
|
||||
Values = new List<IList<object>> { timeWarsaw }
|
||||
};
|
||||
var updateTimeWarsaw = googleSheetValues.Update(valueRangeWarsawTime, sheetId, $"{reportSheetName}!G2");
|
||||
updateTimeWarsaw.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.USERENTERED;
|
||||
updateTimeWarsaw.Execute();
|
||||
|
||||
//invoices
|
||||
|
||||
var invoices = r2.Records!.Where(x => x.Code!.Length == 12)
|
||||
.OrderByDescending(x => x.Code);
|
||||
|
||||
var invoicesValues = new List<IList<object>>();
|
||||
var cleanUpValues = new List<IList<object>>();
|
||||
foreach (var invoice in invoices)
|
||||
{
|
||||
var invoiceDate =
|
||||
DateTime.ParseExact(invoice.Code!.Substring(0, 8), "yyyyMMdd", CultureInfo.InvariantCulture)
|
||||
.ToString("dd.MM.yyyy", CultureInfo.GetCultureInfo("pl-PL"));
|
||||
var invoiceRow = new List<object>
|
||||
{
|
||||
invoiceDate,
|
||||
"",
|
||||
invoice.Desc1!,
|
||||
invoice.Value1!
|
||||
};
|
||||
invoicesValues.Add(invoiceRow);
|
||||
|
||||
var cleanupRow = new List<object>
|
||||
{
|
||||
"", "", "", ""
|
||||
};
|
||||
cleanUpValues.Add(cleanupRow);
|
||||
}
|
||||
|
||||
|
||||
var cleanupValueRange = new ValueRange { Values = cleanUpValues };
|
||||
var cleanupInvoices = googleSheetValues.Update(cleanupValueRange, sheetId, $"{invoicesSheetName}!A6:E");
|
||||
cleanupInvoices.ValueInputOption =
|
||||
SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.USERENTERED;
|
||||
cleanupInvoices.Execute();
|
||||
|
||||
|
||||
var invoicesValueRange = new ValueRange { Values = invoicesValues };
|
||||
var updateInvoices = googleSheetValues.Update(invoicesValueRange, sheetId, $"{invoicesSheetName}!A6:E");
|
||||
updateInvoices.ValueInputOption =
|
||||
SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.USERENTERED;
|
||||
updateInvoices.Execute();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
using DiunaBI.Core.Services;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using DiunaBI.Core.Models;
|
||||
using DiunaBI.Database.Context;
|
||||
|
||||
namespace DiunaBI.Plugins.Morska.Processors;
|
||||
|
||||
public class T5LastValuesProcessor(
|
||||
AppDbContext db)
|
||||
{
|
||||
public void Process(Layer processWorker)
|
||||
{
|
||||
var year = int.Parse(processWorker.Records?.SingleOrDefault(x => x.Code == "Year")?.Desc1!);
|
||||
var month = int.Parse(processWorker.Records?.SingleOrDefault(x => x.Code == "Month")?.Desc1!);
|
||||
var sourceLayer = processWorker.Records?.SingleOrDefault(x => x.Code == "SourceLayer")?.Desc1;
|
||||
if (sourceLayer == null) throw new Exception("SourceLayer record not found");
|
||||
var sourceImportWorker = db.Layers.SingleOrDefault(x => x.Name == sourceLayer && !x.IsDeleted && !x.IsCancelled);
|
||||
if (sourceImportWorker == null) throw new Exception("SourceImportWorker layer not found");
|
||||
var source = processWorker.Records?.SingleOrDefault(x => x.Code == "Source")?.Desc1;
|
||||
if (sourceLayer == null) throw new Exception("Source record not found");
|
||||
|
||||
var processedLayer = db.Layers
|
||||
.Where(x => x.ParentId == processWorker.Id
|
||||
&& !x.IsDeleted && !x.IsCancelled)
|
||||
.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}-P-{year}/{month:D2}-{source}-T5";
|
||||
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>();
|
||||
|
||||
var dataSources = db.Layers
|
||||
.Include(x => x.Records)
|
||||
.Where(x => x.ParentId == sourceImportWorker.Id
|
||||
&& !x.IsDeleted && !x.IsCancelled)
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.AsNoTracking()
|
||||
.ToList();
|
||||
|
||||
if (dataSources.Count == 0) throw new Exception($"DataSource is empty, {sourceImportWorker.Name}");
|
||||
|
||||
var codes = dataSources.SelectMany(x => x.Records!).Select(x => x.Code).Distinct().ToList();
|
||||
|
||||
foreach (var code in codes)
|
||||
{
|
||||
var lastRecord = dataSources.SelectMany(x => x.Records!).Where(x => x.Code == code).OrderByDescending(x => x.CreatedAt).FirstOrDefault();
|
||||
if (lastRecord == null) continue;
|
||||
|
||||
var processedRecord = new Record
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Code = code,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
ModifiedAt = DateTime.UtcNow
|
||||
};
|
||||
|
||||
for (var i = 1; i < 33; i++)
|
||||
{
|
||||
if (ProcessHelper.GetValue(lastRecord, i) != null)
|
||||
{
|
||||
ProcessHelper.SetValue(processedRecord, i, ProcessHelper.GetValue(lastRecord, i));
|
||||
}
|
||||
}
|
||||
|
||||
newRecords.Add(processedRecord);
|
||||
}
|
||||
|
||||
if (isNew)
|
||||
db.Layers.Add(processedLayer);
|
||||
else
|
||||
db.Layers.Update(processedLayer);
|
||||
// TODO: save records
|
||||
//controller.SaveRecords(processedLayer.Id, newRecords, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
|
||||
db.SaveChanges();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user