diff --git a/PARR.AIHITRelationshipsSyncer/Services/Implementations/RelationshipsSyncSrevice.cs b/PARR.AIHITRelationshipsSyncer/Services/Implementations/RelationshipsSyncSrevice.cs index a82108fb..b7109a60 100644 --- a/PARR.AIHITRelationshipsSyncer/Services/Implementations/RelationshipsSyncSrevice.cs +++ b/PARR.AIHITRelationshipsSyncer/Services/Implementations/RelationshipsSyncSrevice.cs @@ -54,8 +54,7 @@ namespace PARR.AIHITRelationshipsSyncer.Services.Implementations //Из данных АИХ ИТ получаем имена всех дочерних ЭК var childsName = parrent.Where(t => t.ChildEk != parentName && t.ChildEk != null).Select(t => t.ChildEk); - //Убираем ЭК типа РПА-РОБИН-РЦКУ-СПИУИ-ИНФО-27-ГОР и РПА - РОБИН - РЦКУ - СПиУИ - ИНФО - 27 - ГОР - childsName = childsName.Select(t => t?.ToLower().Trim()).Distinct(); + childsName = childsName.Select(t => t?.Trim()).Distinct(); //Проходим циклом по полученному списку для актуализации связей в полученном нами экземпляре Unit var isChanged = await SyncChildSUnitAsync(unit, childsName); @@ -113,7 +112,7 @@ namespace PARR.AIHITRelationshipsSyncer.Services.Implementations var isChanged = false; //удаляем лишние дочерние связи существующие только в БД - var childsToDelete = unit.ChildUnits.Where(t => !childsName.Any(a => t.ChildUnit?.Name.ToLower() == a!.ToLower().Trim())); + var childsToDelete = unit.ChildUnits.Where(t => !childsName.Any(a => t.ChildUnit?.Name.ToLower() == a!.ToLower())); if (childsToDelete.Any()) { DeleteChilds(unit, childsToDelete); @@ -122,7 +121,7 @@ namespace PARR.AIHITRelationshipsSyncer.Services.Implementations } //проверяем отсутсвующие связи - var absentChildrens = childsName.Where(c => !unit.ChildUnits.Any(x => x.ChildUnit?.Name.ToLower() == c.ToLower().Trim())); + var absentChildrens = childsName.Where(c => !unit.ChildUnits.Any(x => x.ChildUnit?.Name.ToLower() == c?.ToLower())); foreach (var absentChildren in absentChildrens) { var childUnit = await GetUnitByNameAsync(absentChildren!); @@ -132,15 +131,20 @@ namespace PARR.AIHITRelationshipsSyncer.Services.Implementations return false; } - unit.ChildUnits.Add( - new UnitInUnit - { - ParentUnitId = unit.Id, - ChildUnitId = childUnit.Id, - DateCreated = DateTimeOffset.UtcNow, - } - ); - logger.LogInformation($"----- Создан связь Unit {unit.Name} - {childUnit.Name}-----"); + if (unit.ChildUnits.Any(x => x.ChildUnitId == childUnit.Id)) + logger.LogWarning($"Попытка создать существующую связь для {unit.Name} - {childUnit.Name}"); + else + { + unit.ChildUnits.Add( + new UnitInUnit + { + ParentUnitId = unit.Id, + ChildUnitId = childUnit.Id, + DateCreated = DateTimeOffset.UtcNow, + } + ); + logger.LogInformation($"----- Создан связь Unit {unit.Name} - {childUnit.Name}-----"); + } if (!isChanged) isChanged = true;