Implement parsing
This commit is contained in:
@ -1,12 +1,72 @@
|
||||
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) : DataParserBase(scope, operation)
|
||||
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.UtcNow,
|
||||
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>();
|
||||
|
||||
Reference in New Issue
Block a user