101 lines
3.6 KiB
C#
101 lines
3.6 KiB
C#
using Hcs.WebApp.Data.Hcs;
|
|
using Hcs.WebApp.Services;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Sylvan.Data.Excel;
|
|
|
|
namespace Hcs.WebApp.BackgroundServices.DataParsers
|
|
{
|
|
public class HousesDataParser_15_7_0_1(
|
|
IServiceScope scope,
|
|
Operation operation,
|
|
IWebHostEnvironment webHostEnvironment) : DataParserBase(scope, operation, webHostEnvironment)
|
|
{
|
|
public override async Task ParseAsync()
|
|
{
|
|
await ParseFile();
|
|
await CompleteOperation();
|
|
}
|
|
|
|
private async Task ParseFile()
|
|
{
|
|
const int batchMaxSize = 100;
|
|
const string mkd = "Многоквартирный";
|
|
|
|
var batch = new List<House>();
|
|
var houseService = scope.ServiceProvider.GetRequiredService<HouseService>();
|
|
|
|
var fileToParse = await FileToParseService.GetFileToParseByOperationIdAsync(operation.Id);
|
|
var fullPath = Path.Combine(webHostEnvironment.WebRootPath, fileToParse.Path);
|
|
using var stream = new FileStream(fullPath, FileMode.Open);
|
|
using ExcelDataReader reader = ExcelDataReader.Create(stream, ExcelWorkbookType.ExcelXml);
|
|
while (reader.Read())
|
|
{
|
|
var fiasId = reader.GetString(2);
|
|
var houseType = reader.GetString(7);
|
|
var roomNum = reader.GetString(13);
|
|
var hcsId = reader.GetString(25);
|
|
|
|
if (!string.IsNullOrEmpty(hcsId))
|
|
{
|
|
if (string.IsNullOrEmpty(roomNum))
|
|
{
|
|
var isMkd = houseType == mkd;
|
|
batch.Add(new House()
|
|
{
|
|
FiasId = Guid.Parse(fiasId),
|
|
HcsId = Guid.Parse(hcsId),
|
|
IsMkd = isMkd,
|
|
IsZhd = !isMkd,
|
|
SyncedAt = DateTime.Now,
|
|
LastSyncOperationId = operation.Id
|
|
});
|
|
}
|
|
}
|
|
|
|
if (batch.Count >= batchMaxSize)
|
|
{
|
|
await houseService.UpsertHouses(batch);
|
|
|
|
batch.Clear();
|
|
}
|
|
}
|
|
|
|
if (batch.Count > 0)
|
|
{
|
|
await houseService.UpsertHouses(batch);
|
|
}
|
|
}
|
|
|
|
private async Task CompleteOperation()
|
|
{
|
|
var headquartersService = scope.ServiceProvider.GetRequiredService<HeadquartersService>();
|
|
var fileToParseService = scope.ServiceProvider.GetRequiredService<FileToParseService>();
|
|
|
|
using var context = headquartersService.GetNewContext();
|
|
var executionStrategy = context.Database.CreateExecutionStrategy();
|
|
await executionStrategy.ExecuteAsync(async () =>
|
|
{
|
|
using var transaction = await context.Database.BeginTransactionAsync();
|
|
try
|
|
{
|
|
var now = DateTime.Now;
|
|
|
|
var fileToParse = await fileToParseService.GetFileToParseByOperationIdAsync(context, operation.Id);
|
|
fileToParse.ParsedAt = now;
|
|
await context.SaveChangesAsync();
|
|
|
|
await headquartersService.SetOperationEndedAsync(context, operation.Id, now);
|
|
|
|
await transaction.CommitAsync();
|
|
}
|
|
catch
|
|
{
|
|
await transaction.RollbackAsync();
|
|
|
|
throw;
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|