/
rezvich
/
Prik
Обзор
Документация
Войти
/
rezvich
/
Prik
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
Application/IdentifyService.cs
157 строк
5 KB
rezvich
dd
26 июн 2026, 10:11
26 июн 2026, 10:11
10858a3
Код
Авторство
О чём код?
// prik/Application/IdentifyService using Microsoft.Extensions.Logging; using prik.Domain; using prik.Infrastructure.Db; namespace prik.Application { public interface IIdentifyService { Task IdentifyAsync(List<PrikRow> rows, CancellationToken ct); } public sealed class IdentifyService : IIdentifyService { private readonly ILogger<IdentifyService> _log; private readonly IDbConnectionFactory _db; private readonly IPeopleRepository _repo; public IdentifyService(ILogger<IdentifyService> log, IDbConnectionFactory db, IPeopleRepository repo) { _log = log; _db = db; _repo = repo; } public async Task IdentifyAsync(List<PrikRow> rows, CancellationToken ct) { using var conn = _db.Create(); conn.Open(); // 1) ENP var enps = rows .Select(r => r.Enp) .Where(s => !string.IsNullOrWhiteSpace(s)) .Select(s => s!.Trim()) .Distinct(StringComparer.OrdinalIgnoreCase) .ToList(); var byEnp = await _repo.FindByEnpAsync(conn, enps, ct); foreach (var r in rows) { if (r.PersonId is not null) { continue; } if (string.IsNullOrWhiteSpace(r.Enp)) { continue; } var enp = r.Enp.Trim(); if (byEnp.TryGetValue(enp, out var m)) { ApplyMatch(r, m); } } // 2) FIO+DR var needFio = rows.Where(r => r.PersonId is null) .Where(r => !string.IsNullOrWhiteSpace(r.Fam) && !string.IsNullOrWhiteSpace(r.Im) && r.Dr is not null) .Select(r => (fam: r.Fam!.Trim(), im: r.Im!.Trim(), ot: (r.Ot ?? "").Trim(), dr: r.Dr!.Value.Date)) .Distinct() .ToList(); var byFio = await _repo.FindByFioDrAsync(conn, needFio, ct); foreach (var r in rows) { if (r.PersonId is not null) { continue; } if (string.IsNullOrWhiteSpace(r.Fam) || string.IsNullOrWhiteSpace(r.Im) || r.Dr is null) { continue; } var key = $"{r.Fam.Trim()}|{r.Im.Trim()}|{(r.Ot ?? "").Trim()}|{r.Dr.Value:yyyy-MM-dd}"; if (byFio.TryGetValue(key, out var m)) { ApplyMatch(r, m); } } // 3) Документ var needDoc = rows.Where(r => r.PersonId is null) .Where(r => !string.IsNullOrWhiteSpace(r.Doctp) && !string.IsNullOrWhiteSpace(r.Docs) && !string.IsNullOrWhiteSpace(r.Docn)) .Select(r => (doctp: r.Doctp!.Trim(), docs: r.Docs!.Trim(), docn: r.Docn!.Trim(), docdt: r.Docdt?.Date)) .Distinct() .ToList(); var byDoc = await _repo.FindByDocAsync(conn, needDoc, ct); foreach (var r in rows) { if (r.PersonId is not null) { continue; } if (string.IsNullOrWhiteSpace(r.Doctp) || string.IsNullOrWhiteSpace(r.Docs) || string.IsNullOrWhiteSpace(r.Docn)) { continue; } var key = $"{r.Doctp.Trim()}|{r.Docs.Trim()}|{r.Docn.Trim()}|{(r.Docdt is null ? "" : r.Docdt.Value.ToString("yyyy-MM-dd"))}"; if (byDoc.TryGetValue(key, out var m)) { ApplyMatch(r, m); } } // финал int notFound = 0; foreach (var r in rows) { if (r.PersonId is null) { // r.Comment = "не удалось идентифицировать в РС"; notFound++; } } _log.LogInformation("Identify done. Total={Total} NotFound={NotFound}", rows.Count, notFound); } public static int GetAge(DateTime birthDate, DateTime onDate) { int age = onDate.Year - birthDate.Year; if (onDate.Date < birthDate.Date.AddYears(age)) { age--; } return age; } private static void ApplyMatch(PrikRow row, PeopleMatch match) { row.PersonId = match.Id; if (string.IsNullOrWhiteSpace(row.Enp) && !string.IsNullOrWhiteSpace(match.Enp)) { row.Enp = match.Enp; } row.RsDr = match.Dr?.Date; } } }