Implement parsing

This commit is contained in:
2025-11-22 11:10:03 +09:00
parent 8095bf8ebc
commit dfb60cb9f0
8 changed files with 102 additions and 13 deletions

View File

@ -1,12 +1,17 @@
using Hcs.Broker;
using Hcs.WebApp.Data.Hcs;
using Hcs.WebApp.Data.Hcs;
using Hcs.WebApp.Services;
namespace Hcs.WebApp.BackgroundServices.DataParsers
{
public abstract class DataParserBase(IServiceScope scope, Operation operation) : IDataParser
public abstract class DataParserBase(IServiceScope scope, Operation operation, IWebHostEnvironment webHostEnvironment) : IDataParser
{
protected readonly IServiceScope scope = scope;
protected readonly Operation operation = operation;
protected readonly IWebHostEnvironment webHostEnvironment = webHostEnvironment;
private FileToParseService? fileToParseService;
protected FileToParseService FileToParseService => fileToParseService ??= scope.ServiceProvider.GetRequiredService<FileToParseService>();
public abstract Task ParseAsync();
}

View File

@ -1,15 +1,14 @@
using Hcs.Broker;
using Hcs.WebApp.Data.Hcs;
using Hcs.WebApp.Data.Hcs;
namespace Hcs.WebApp.BackgroundServices.DataParsers
{
public class DataParserFactory
{
public IDataParser CreateDataParser(IServiceScope scope, Operation operation)
public IDataParser CreateDataParser(IServiceScope scope, Operation operation, IWebHostEnvironment webHostEnvironment)
{
return operation.Type switch
{
Operation.OperationType.ParseHousesData_15_7_0_1 => new HousesDataParser_15_7_0_1(scope, operation),
Operation.OperationType.ParseHousesData_15_7_0_1 => new HousesDataParser_15_7_0_1(scope, operation, webHostEnvironment),
Operation.OperationType.NsiCommon_ExportNsiItem_15_7_0_1 or
Operation.OperationType.Nsi_ExportNsiItem_15_7_0_1 => throw new ArgumentException($"Нельзя использовать операцию с типом {operation.Type} для парсинга")

View File

@ -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>();