Files
DiunaBI/WebAPI/dataProcessors/t1.r1.processor.cs

275 lines
10 KiB
C#
Raw Normal View History

2024-06-06 11:49:18 +02:00
using System.Globalization;
using DiunaBIWebAPI.dataProcessors;
2024-05-06 20:07:21 +02:00
using Google.Apis.Sheets.v4;
using Google.Apis.Sheets.v4.Data;
using Microsoft.EntityFrameworkCore;
2024-08-24 12:55:29 +02:00
using WebAPI.Calculator;
2024-05-06 20:07:21 +02:00
using WebAPI.Controllers;
using WebAPI.Models;
2024-06-18 19:53:52 +02:00
namespace WebAPI.dataProcessors;
2024-08-24 12:55:29 +02:00
public class T1R1Processor(
AppDbContext db,
SpreadsheetsResource.ValuesResource googleSheetValues,
LayersController controller,
LogsController logsController)
2024-05-06 20:07:21 +02:00
{
2024-06-18 19:53:52 +02:00
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();
2024-06-18 22:24:04 +02:00
if (sources!.Count == 0)
2024-05-06 20:07:21 +02:00
{
2024-06-18 19:53:52 +02:00
throw new Exception("Source record not found");
2024-05-06 20:07:21 +02:00
}
2024-08-24 12:55:29 +02:00
var processedLayer = db.Layers
2024-06-18 19:53:52 +02:00
.Where(x => x.ParentId == processWorker.Id
&& !x.IsDeleted)
.OrderByDescending(x => x.CreatedAt)
.FirstOrDefault();
2024-05-06 20:07:21 +02:00
2024-06-18 19:53:52 +02:00
var isNew = false;
if (processedLayer == null)
{
isNew = true;
processedLayer = new Layer
2024-05-06 20:07:21 +02:00
{
2024-06-18 19:53:52 +02:00
Id = Guid.NewGuid(),
2024-06-18 22:02:19 +02:00
Type = LayerType.Processed,
2024-06-18 19:53:52 +02:00
ParentId = processWorker.Id,
2024-08-24 12:55:29 +02:00
Number = db.Layers.Count() + 1
2024-06-18 19:53:52 +02:00
};
2024-12-31 16:33:03 +01:00
processedLayer.Name = $"L{processedLayer.Number}-P-{year}-R1-T1";
2024-06-18 19:53:52 +02:00
processedLayer.CreatedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D");
2024-05-06 20:07:21 +02:00
processedLayer.ModifiedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D");
2024-06-18 19:53:52 +02:00
processedLayer.CreatedAt = DateTime.UtcNow;
2024-05-06 20:07:21 +02:00
processedLayer.ModifiedAt = DateTime.UtcNow;
2024-06-18 19:53:52 +02:00
}
2024-06-18 22:02:19 +02:00
2024-06-18 19:53:52 +02:00
processedLayer.ModifiedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D");
processedLayer.ModifiedAt = DateTime.UtcNow;
2024-05-06 20:07:21 +02:00
2024-08-24 12:55:29 +02:00
var dynamicCodes = processWorker.Records?.Where(x => x.Code!.Contains("DynamicCode-"))
.OrderBy(x => int.Parse(x.Code!.Split('-')[1]))
.ToList();
2024-05-06 20:07:21 +02:00
2024-08-24 12:55:29 +02:00
var newRecords = new List<Record>();
2025-01-02 12:20:55 +01:00
for (var month = 1; month < 14; month++)
2024-06-18 19:53:52 +02:00
{
2025-01-02 12:20:55 +01:00
if (year > DateTime.UtcNow.Year || (( year == DateTime.UtcNow.Year && month > DateTime.UtcNow.Month && month != 13)))
2024-08-24 12:55:29 +02:00
{
continue;
}
var records = new List<Record>();
foreach (var source in sources)
2024-05-06 20:07:21 +02:00
{
2024-06-18 19:53:52 +02:00
var monthCopy = month;
2024-08-24 12:55:29 +02:00
var dataSource = db.Layers.Where(x =>
2024-06-18 22:02:19 +02:00
x.Type == LayerType.Processed &&
2024-05-06 20:07:21 +02:00
!x.IsDeleted &&
x.Name != null && x.Name.Contains($"{year}/{monthCopy:D2}-{source.Desc1}-T3")
2024-08-24 12:55:29 +02:00
).Include(x => x.Records)
2024-05-06 20:07:21 +02:00
.FirstOrDefault();
2024-08-24 12:55:29 +02:00
if (dataSource == null)
2024-05-06 20:07:21 +02:00
{
2024-08-24 12:55:29 +02:00
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!))));
2024-05-06 20:07:21 +02:00
}
else
{
2024-08-24 12:55:29 +02:00
records.AddRange(dataSource.Records!);
2024-05-06 20:07:21 +02:00
}
}
2024-08-24 12:55:29 +02:00
if (dynamicCodes != null)
2024-05-06 20:07:21 +02:00
{
2024-08-24 12:55:29 +02:00
foreach (var dynamicCode in dynamicCodes)
2024-06-18 19:53:52 +02:00
{
2024-08-24 12:55:29 +02:00
try
{
if (dynamicCode.Desc1 == null)
{
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;
}
2024-05-06 20:07:21 +02:00
2024-08-24 12:55:29 +02:00
var calc = new BaseCalc(dynamicCode.Desc1);
if (!calc.IsFormulaCorrect())
{
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;
}
2024-05-06 20:07:21 +02:00
2024-08-24 12:55:29 +02:00
try
{
records.Add(calc.CalculateT1(records));
}
catch (Exception e)
{
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)
{
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
});
}
2024-05-06 20:07:21 +02:00
}
2024-08-24 12:55:29 +02:00
}
2024-06-18 19:53:52 +02:00
2024-08-24 12:55:29 +02:00
newRecords.AddRange(records.Select(x => new Record
2024-05-06 20:07:21 +02:00
{
2024-06-18 19:53:52 +02:00
Id = Guid.NewGuid(),
Code = $"{x.Code}{month:D2}",
2024-06-18 19:53:52 +02:00
CreatedAt = DateTime.UtcNow,
ModifiedAt = DateTime.UtcNow,
Value1 = x.Value32
2024-08-24 12:55:29 +02:00
}
));
}
2024-06-18 19:53:52 +02:00
if (isNew)
{
2024-08-24 12:55:29 +02:00
db.Layers.Add(processedLayer);
2024-06-18 19:53:52 +02:00
}
else
{
2024-08-24 12:55:29 +02:00
db.Layers.Update(processedLayer);
2024-06-18 19:53:52 +02:00
}
2024-08-24 12:55:29 +02:00
controller.SaveRecords(processedLayer.Id, newRecords, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
db.SaveChanges();
2024-12-31 09:49:28 +01:00
var sheetName = processWorker.Records?.SingleOrDefault(x => x.Code == "GoogleSheetName")?.Desc1;
if (sheetName == null)
{
throw new Exception("GoogleSheetName record not found");
}
UpdateReport(processedLayer.Id, sheetName);
2024-06-18 19:53:52 +02:00
}
2024-12-31 09:49:28 +01:00
private void UpdateReport(Guid sourceId, string sheetName)
2024-06-18 19:53:52 +02:00
{
const string sheetId = "1pph-XowjlK5CIaCEV_A5buK4ceJ0Z0YoUlDI4VMkhhA";
2024-10-22 08:52:23 +02:00
var request = googleSheetValues.Get(sheetId, $"{sheetName}!C4:CQ4");
2024-06-18 19:53:52 +02:00
var response = request.Execute();
2024-08-24 12:55:29 +02:00
var r1 = db.Layers
2024-12-31 09:49:28 +01:00
.Where(x => x.Id == sourceId)
2024-06-18 19:53:52 +02:00
.Include(x => x.Records)
.FirstOrDefault();
var codesRow = response.Values[0];
2024-08-24 12:55:29 +02:00
var valueRange = new ValueRange
{
Values = new List<IList<object>>()
};
for (var i = 1; i <= 12; i++)
2024-06-18 19:53:52 +02:00
{
var values = new List<object>();
2024-05-06 20:07:21 +02:00
foreach (string code in codesRow)
{
var record = r1!.Records?.SingleOrDefault(x => x.Code == $"{code}{i:D2}");
2024-05-06 20:07:21 +02:00
if (record != null)
{
2024-06-18 19:53:52 +02:00
values.Add(record.Value1!.Value);
2024-05-06 20:07:21 +02:00
}
else
{
2024-06-18 19:53:52 +02:00
values.Add("0");
2024-05-06 20:07:21 +02:00
}
}
2024-08-24 12:55:29 +02:00
valueRange.Values.Add(values);
2024-06-18 19:53:52 +02:00
}
2024-08-24 12:55:29 +02:00
2024-06-18 19:53:52 +02:00
// 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)
2024-06-06 11:49:18 +02:00
{
2024-06-18 19:53:52 +02:00
valuesSum.Add(record.Value1!.Value);
}
else
2024-06-06 11:49:18 +02:00
{
2024-06-18 19:53:52 +02:00
valuesSum.Add("0");
}
2024-05-06 20:07:21 +02:00
}
2024-08-24 12:55:29 +02:00
valueRange.Values.Add(emptyRow);
valueRange.Values.Add(valuesSum);
2024-06-18 19:53:52 +02:00
2024-10-22 08:52:23 +02:00
var update = googleSheetValues.Update(valueRange, sheetId, $"{sheetName}!C7:CQ20");
2024-08-24 12:55:29 +02:00
update.ValueInputOption =
SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.USERENTERED;
update.Execute();
2024-06-18 19:53:52 +02:00
// update time
var timeUtc = new List<object>
{
2024-08-13 08:21:33 +02:00
r1!.ModifiedAt.ToString("dd.MM.yyyy HH:mm:ss", CultureInfo.GetCultureInfo("pl-PL"))
2024-06-18 19:53:52 +02:00
};
var warsawTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
var warsawTime = TimeZoneInfo.ConvertTimeFromUtc(r1.ModifiedAt.ToUniversalTime(), warsawTimeZone);
var timeWarsaw = new List<object>
{
2024-08-13 08:21:33 +02:00
warsawTime.ToString("dd.MM.yyyy HH:mm:ss", CultureInfo.GetCultureInfo("pl-PL"))
2024-06-18 19:53:52 +02:00
};
2024-08-24 12:55:29 +02:00
var valueRangeTime = new ValueRange
2024-06-18 19:53:52 +02:00
{
2024-08-24 12:55:29 +02:00
Values = new List<IList<object>> ()
2024-06-18 19:53:52 +02:00
};
2024-08-24 12:55:29 +02:00
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();
2024-05-06 20:07:21 +02:00
}
2024-06-18 19:53:52 +02:00
}