141 lines
5.8 KiB
C#
141 lines
5.8 KiB
C#
using Azure;
|
|
using Google.Apis.Sheets.v4;
|
|
using Google.Apis.Sheets.v4.Data;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Newtonsoft.Json.Linq;
|
|
using System.Globalization;
|
|
using WebAPI.Controllers;
|
|
using WebAPI.Models;
|
|
|
|
namespace WebAPI.dataProcessors
|
|
{
|
|
public class CopyProcessor
|
|
{
|
|
private AppDbContext db;
|
|
private SpreadsheetsResource.ValuesResource googleSheetValues;
|
|
private LayersController controller;
|
|
|
|
public CopyProcessor(
|
|
AppDbContext _db,
|
|
SpreadsheetsResource.ValuesResource _googleSheetValues,
|
|
LayersController _controller)
|
|
{
|
|
db = _db;
|
|
googleSheetValues = _googleSheetValues;
|
|
controller = _controller;
|
|
}
|
|
|
|
public void process(Layer sourceImportWorker, Guid? parentId)
|
|
{
|
|
string? sheetId = sourceImportWorker.Records!.Where(x => x.Code == "SheetId").FirstOrDefault()?.Desc1;
|
|
if (sheetId == null)
|
|
{
|
|
throw new Exception($"SheetId not found, {sourceImportWorker.Name}");
|
|
}
|
|
string? sheetTabName = sourceImportWorker.Records!.Where(x => x.Code == "SheetTabName").FirstOrDefault()?.Desc1;
|
|
if (sheetId == null)
|
|
{
|
|
throw new Exception($"SheetTabName not found, {sourceImportWorker.Name}");
|
|
}
|
|
string? importYearCell = sourceImportWorker.Records!.Where(x => x.Code == "ImportYear").FirstOrDefault()?.Desc1;
|
|
if (importYearCell == null)
|
|
{
|
|
throw new Exception($"ImportYear not found, {sourceImportWorker.Name}");
|
|
}
|
|
string? importMonthCell = sourceImportWorker.Records!.Where(x => x.Code == "ImportMonth").FirstOrDefault()?.Desc1;
|
|
if (importMonthCell == null)
|
|
{
|
|
throw new Exception($"ImportMonth not found, {sourceImportWorker.Name}");
|
|
}
|
|
string? importNameCell = sourceImportWorker.Records!.Where(x => x.Code == "ImportName").FirstOrDefault()?.Desc1;
|
|
if (importNameCell == null)
|
|
{
|
|
throw new Exception($"ImportName not found, {sourceImportWorker.Name}");
|
|
}
|
|
var nameResponse = googleSheetValues.Get(sheetId, $"{sheetTabName}!{importNameCell}:{importNameCell}").Execute();
|
|
string? name = nameResponse.Values[0][0].ToString();
|
|
if (name == null)
|
|
{
|
|
throw new Exception($"ImportName cell is empty, {sourceImportWorker.Name}");
|
|
}
|
|
var yearResponse = googleSheetValues.Get(sheetId, $"{sheetTabName}!{importYearCell}:{importYearCell}").Execute();
|
|
string? year = yearResponse.Values[0][0].ToString();
|
|
if (year == null)
|
|
{
|
|
throw new Exception($"ImportYear cell is empty, {sourceImportWorker.Name}");
|
|
}
|
|
var monthResponse = googleSheetValues.Get(sheetId, $"{sheetTabName}!{importMonthCell}:{importMonthCell}").Execute();
|
|
string? month = monthResponse.Values[0][0].ToString();
|
|
if (month == null)
|
|
{
|
|
throw new Exception($"ImportMonth cell is empty, {sourceImportWorker.Name}");
|
|
}
|
|
|
|
Layer? processedLayer = db.Layers
|
|
.Where(x => x.ParentId == parentId)
|
|
.OrderByDescending(x => x.CreatedAt)
|
|
.FirstOrDefault();
|
|
|
|
bool isNew = false;
|
|
if (processedLayer == null)
|
|
{
|
|
isNew = true;
|
|
processedLayer = new Layer
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
Source = "",
|
|
Type = LayerType.processed,
|
|
ParentId = parentId,
|
|
Number = db.Layers.Count() + 1,
|
|
};
|
|
processedLayer.Name = $"L{processedLayer.Number}-P-{name}-{year}/{month}-{DateTime.Now.ToString("yyyyMMddHHmm")}";
|
|
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.Records = new List<Record>();
|
|
processedLayer.Sources = new List<ProcessSource>();
|
|
processedLayer.ModifiedById = Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D");
|
|
processedLayer.ModifiedAt = DateTime.UtcNow;
|
|
|
|
|
|
Layer dataSource = db.Layers
|
|
.Include(x => x.Records)
|
|
.Where(x => x.ParentId == sourceImportWorker.Id && !x.IsDeleted)
|
|
.OrderByDescending(x => x.CreatedAt)
|
|
.First();
|
|
/*
|
|
processedLayer.Sources.Add(new ProcessSource
|
|
{
|
|
LayerId = processedLayer.Id,
|
|
SourceId = dataSource.Id
|
|
});
|
|
*/
|
|
|
|
foreach (Record source in dataSource.Records!)
|
|
{
|
|
Record record = new Record
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
Code = source.Code,
|
|
Value1 = source.Value1,
|
|
CreatedAt = DateTime.UtcNow,
|
|
ModifiedAt = DateTime.UtcNow
|
|
};
|
|
processedLayer.Records.Add(record);
|
|
}
|
|
if (isNew)
|
|
{
|
|
db.Layers.Add(processedLayer);
|
|
} else
|
|
{
|
|
db.Layers.Update(processedLayer);
|
|
}
|
|
controller.SaveRecords(processedLayer.Id, processedLayer.Records, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
|
|
db.SaveChanges();
|
|
}
|
|
}
|
|
}
|