using System.Globalization; using Google.Apis.Sheets.v4; using WebAPI; using WebAPI.Controllers; using WebAPI.Models; namespace DiunaBIWebAPI.dataImporters; public class MorskaD1Importer( AppDbContext db, SpreadsheetsResource.ValuesResource googleSheetValues, LayersController controller) { 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); 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 array, int index) { return array != null && index >= 0 && index < array.Count; } }