R6 GSheet export
This commit is contained in:
@@ -122,6 +122,8 @@ public class MorskaD6Processor : MorskaBaseProcessor
|
||||
|
||||
SaveProcessedLayer(processedLayer, newRecords);
|
||||
|
||||
UpdateGoogleSheetReport(processedLayer.Id);
|
||||
|
||||
_logger.LogInformation(
|
||||
"{ProcessorType}: Successfully processed {RecordCount} records for layer {LayerName} ({LayerId})",
|
||||
ProcessorType, newRecords.Count, processedLayer.Name, processedLayer.Id);
|
||||
@@ -199,7 +201,7 @@ public class MorskaD6Processor : MorskaBaseProcessor
|
||||
|
||||
// L8542-D-DEPARTMENTS
|
||||
var dictionary = _db.Layers.Include(x => x.Records).FirstOrDefault(x => x.Number == 8542);
|
||||
|
||||
|
||||
var departmentLookup = new Dictionary<string, string>();
|
||||
if (dictionary?.Records != null)
|
||||
{
|
||||
@@ -374,4 +376,236 @@ public class MorskaD6Processor : MorskaBaseProcessor
|
||||
return string.IsNullOrEmpty(originalDepartment) ? "OTHER" : originalDepartment;
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateGoogleSheetReport(Guid sourceId)
|
||||
{
|
||||
const string googleSheetName = "Raport_R6_DRAFT_2025";
|
||||
try
|
||||
{
|
||||
const string sheetId = "19AljwrZRg2Rc5hagkfK3u8LIo3x9_GmFNnZEeOJ5g_g";
|
||||
|
||||
_logger.LogDebug("{ProcessorType}: Updating Google Sheet report {SheetName}",
|
||||
ProcessorType, googleSheetName);
|
||||
|
||||
// Get processed layer data
|
||||
var processedLayer = _db.Layers
|
||||
.Where(x => x.Id == sourceId)
|
||||
.Include(x => x.Records)
|
||||
.AsNoTracking()
|
||||
.FirstOrDefault();
|
||||
|
||||
if (processedLayer == null)
|
||||
{
|
||||
throw new InvalidOperationException($"Processed layer {sourceId} not found");
|
||||
}
|
||||
|
||||
// Get codes from sheet header (row 4, columns C to AA)
|
||||
var codesResponse = _googleSheetValues.Get(sheetId, $"{googleSheetName}!C4:AA4").Execute();
|
||||
var codesRow = codesResponse.Values[0];
|
||||
|
||||
// Update data based on 6-digit codes from processedLayer
|
||||
UpdateMonthlyDataFromCodes(sheetId, codesRow, processedLayer);
|
||||
Thread.Sleep(1000);
|
||||
|
||||
// Update yearly summary data (row 20)
|
||||
UpdateYearlySummaryData(sheetId, codesRow, processedLayer);
|
||||
Thread.Sleep(1000);
|
||||
|
||||
// Update timestamps
|
||||
UpdateTimestamps(sheetId, processedLayer);
|
||||
Thread.Sleep(1000);
|
||||
|
||||
_logger.LogInformation("{ProcessorType}: Successfully updated Google Sheet report {SheetName}",
|
||||
ProcessorType, googleSheetName);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "{ProcessorType}: Failed to update Google Sheet report {SheetName}",
|
||||
ProcessorType, googleSheetName);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateYearlySummaryData(string sheetId, IList<object> codesRow, Layer processedLayer)
|
||||
{
|
||||
const string googleSheetName = "Raport_R6_DRAFT_2025";
|
||||
|
||||
if (processedLayer.Records == null)
|
||||
{
|
||||
_logger.LogWarning("{ProcessorType}: No records found in processed layer for yearly summary", ProcessorType);
|
||||
return;
|
||||
}
|
||||
|
||||
var summaryValues = new List<object>();
|
||||
|
||||
foreach (string fourDigitCode in codesRow)
|
||||
{
|
||||
// Calculate sum for all months (01-12) for this 4-digit code
|
||||
double yearlySum = 0;
|
||||
|
||||
for (var month = 1; month <= 12; month++)
|
||||
{
|
||||
var sixDigitCode = $"{fourDigitCode}{month:D2}";
|
||||
var record = processedLayer.Records.FirstOrDefault(x => x.Code == sixDigitCode);
|
||||
|
||||
if (record?.Value1.HasValue == true)
|
||||
{
|
||||
yearlySum += record.Value1.Value;
|
||||
}
|
||||
}
|
||||
|
||||
summaryValues.Add(yearlySum.ToString(CultureInfo.GetCultureInfo("pl-PL")));
|
||||
|
||||
_logger.LogDebug("{ProcessorType}: Calculated yearly sum for code {FourDigitCode}: {YearlySum}",
|
||||
ProcessorType, fourDigitCode, yearlySum);
|
||||
}
|
||||
|
||||
// Update row 20 with yearly sums
|
||||
var valueRange = new ValueRange
|
||||
{
|
||||
Values = new List<IList<object>> { summaryValues }
|
||||
};
|
||||
|
||||
var columnStart = "C";
|
||||
var columnEnd = GetColumnLetter(codesRow.Count + 1); // +1 because we start from C (index 2)
|
||||
var range = $"{googleSheetName}!{columnStart}20:{columnEnd}20";
|
||||
|
||||
var update = _googleSheetValues.Update(valueRange, sheetId, range);
|
||||
update.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.USERENTERED;
|
||||
update.Execute();
|
||||
|
||||
_logger.LogInformation("{ProcessorType}: Updated yearly summary data in row 20 with {CodeCount} totals",
|
||||
ProcessorType, codesRow.Count);
|
||||
}
|
||||
|
||||
private void UpdateMonthlyDataFromCodes(string sheetId, IList<object> codesRow, Layer processedLayer)
|
||||
{
|
||||
const string googleSheetName = "Raport_R6_DRAFT_2025";
|
||||
|
||||
if (processedLayer.Records == null)
|
||||
{
|
||||
_logger.LogWarning("{ProcessorType}: No records found in processed layer", ProcessorType);
|
||||
return;
|
||||
}
|
||||
|
||||
// Create a dictionary to store updates for each cell
|
||||
var cellUpdates = new Dictionary<string, string>();
|
||||
|
||||
foreach (var record in processedLayer.Records)
|
||||
{
|
||||
if (string.IsNullOrEmpty(record.Code) || record.Code.Length != 6)
|
||||
{
|
||||
_logger.LogWarning("{ProcessorType}: Invalid code format in record {RecordId}: {Code}",
|
||||
ProcessorType, record.Id, record.Code);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Extract 4-digit code and month from 6-digit code
|
||||
var fourDigitCode = record.Code.Substring(0, 4);
|
||||
var monthStr = record.Code.Substring(4, 2);
|
||||
|
||||
if (!int.TryParse(monthStr, out var month) || month < 1 || month > 12)
|
||||
{
|
||||
_logger.LogWarning("{ProcessorType}: Invalid month in code {Code}", ProcessorType, record.Code);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Find column index for the 4-digit code
|
||||
var columnIndex = -1;
|
||||
for (var i = 0; i < codesRow.Count; i++)
|
||||
{
|
||||
if (codesRow[i]?.ToString() == fourDigitCode)
|
||||
{
|
||||
columnIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (columnIndex == -1)
|
||||
{
|
||||
_logger.LogWarning("{ProcessorType}: Code {FourDigitCode} not found in sheet header",
|
||||
ProcessorType, fourDigitCode);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Calculate row (month 01 = row 7, month 02 = row 8, etc.)
|
||||
var targetRow = 6 + month; // row 7 for month 01, row 8 for month 02, etc.
|
||||
|
||||
// Calculate column letter (C = index 0, D = index 1, etc.)
|
||||
var targetColumn = GetColumnLetter(columnIndex + 2); // +2 because C is the first column (index 0)
|
||||
|
||||
var cellAddress = $"{targetColumn}{targetRow}";
|
||||
var value = record.Value1?.ToString(CultureInfo.GetCultureInfo("pl-PL")) ?? "0";
|
||||
|
||||
cellUpdates[cellAddress] = value;
|
||||
|
||||
_logger.LogDebug(
|
||||
"{ProcessorType}: Mapping code {SixDigitCode} (base: {FourDigitCode}, month: {Month}) to cell {CellAddress} with value {Value}",
|
||||
ProcessorType, record.Code, fourDigitCode, month, cellAddress, value);
|
||||
}
|
||||
|
||||
// Batch update all cells
|
||||
if (cellUpdates.Any())
|
||||
{
|
||||
var batchUpdateRequest = new BatchUpdateValuesRequest
|
||||
{
|
||||
ValueInputOption = "USER_ENTERED",
|
||||
Data = cellUpdates.Select(kvp => new ValueRange
|
||||
{
|
||||
Range = $"{googleSheetName}!{kvp.Key}",
|
||||
Values = new List<IList<object>> { new List<object> { kvp.Value } }
|
||||
}).ToList()
|
||||
};
|
||||
|
||||
var batchUpdate = _googleSheetValues.BatchUpdate(batchUpdateRequest, sheetId);
|
||||
batchUpdate.Execute();
|
||||
|
||||
_logger.LogInformation("{ProcessorType}: Updated {CellCount} cells in Google Sheet",
|
||||
ProcessorType, cellUpdates.Count);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogWarning("{ProcessorType}: No valid data found to update in Google Sheet", ProcessorType);
|
||||
}
|
||||
}
|
||||
|
||||
private string GetColumnLetter(int columnIndex)
|
||||
{
|
||||
var columnLetter = string.Empty;
|
||||
while (columnIndex >= 0)
|
||||
{
|
||||
columnLetter = (char)('A' + (columnIndex % 26)) + columnLetter;
|
||||
columnIndex = columnIndex / 26 - 1;
|
||||
}
|
||||
|
||||
return columnLetter;
|
||||
}
|
||||
|
||||
private void UpdateTimestamps(string sheetId, Layer processedLayer)
|
||||
{
|
||||
const string googleSheetName = "Raport_R6_DRAFT_2025";
|
||||
|
||||
var timeUtc = processedLayer.ModifiedAt.ToString("dd.MM.yyyy HH:mm:ss", CultureInfo.GetCultureInfo("pl-PL"));
|
||||
|
||||
var warsawTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
|
||||
var warsawTime = TimeZoneInfo.ConvertTimeFromUtc(processedLayer.ModifiedAt.ToUniversalTime(), warsawTimeZone);
|
||||
var timeWarsaw = warsawTime.ToString("dd.MM.yyyy HH:mm:ss", CultureInfo.GetCultureInfo("pl-PL"));
|
||||
|
||||
var valueRangeTime = new ValueRange
|
||||
{
|
||||
Values = new List<IList<object>>
|
||||
{
|
||||
new List<object> { timeUtc },
|
||||
new List<object> { timeWarsaw }
|
||||
}
|
||||
};
|
||||
|
||||
var updateTime = _googleSheetValues.Update(valueRangeTime, sheetId, $"{googleSheetName}!G1:G2");
|
||||
updateTime.ValueInputOption =
|
||||
SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.USERENTERED;
|
||||
updateTime.Execute();
|
||||
|
||||
_logger.LogDebug("{ProcessorType}: Updated timestamps in Google Sheet - UTC: {TimeUtc}, Warsaw: {TimeWarsaw}",
|
||||
ProcessorType, timeUtc, timeWarsaw);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user