/// <summary> /// Connection search method /// </summary> /// <param name = "search"> Link search model </param> /// <param name = "userId"> user ID </param> /// <returns> </returns> public async Task <List <UnitNodeVm> > Search(LinkSearchM search, string userId) { var searchModel = new SearchQueryM() { Name = search.Wildcard, Type = search.Type != null ? new List <StatUnitTypes> { (StatUnitTypes)search.Type } : new List <StatUnitTypes>(), RegId = search.Id, RegionId = search.RegionCode, LastChangeFrom = search.LastChangeFrom, LastChangeTo = search.LastChangeTo, DataSourceClassificationId = search.DataSourceClassificationId, PageSize = 20 }; var searchResponse = await _elasticService.Search(searchModel, userId, false); var units = searchResponse.Result.ToList(); var list = new List <IStatisticalUnit>(); var listIds = units.Select(x => x.RegId).ToList(); var type = search.Type; if (type == null || type == StatUnitTypes.EnterpriseGroup) { var entGroup = _commonSvc.GetUnitsList <EnterpriseGroup>(false) .Where(x => listIds.Contains(x.RegId)) .Include(x => x.EnterpriseUnits) .ThenInclude(x => x.LegalUnits) .ThenInclude(x => x.LocalUnits); list.AddRange(entGroup); list.AddRange(entGroup.SelectMany(x => x.EnterpriseUnits.Where(y => y.IsDeleted == false))); list.AddRange(entGroup.SelectMany(x => x.EnterpriseUnits.Where(y => y.IsDeleted == false).SelectMany(y => y.LegalUnits.Where(z => z.IsDeleted == false)))); list.AddRange(entGroup.SelectMany(x => x.EnterpriseUnits.Where(y => y.IsDeleted == false).SelectMany(y => y.LegalUnits.Where(z => z.IsDeleted == false).SelectMany(z => z.LocalUnits.Where(l => l.IsDeleted == false))))); } if (type == null || type == StatUnitTypes.EnterpriseUnit) { var entUnit = _commonSvc.GetUnitsList <EnterpriseUnit>(false) .Where(x => listIds.Contains(x.RegId)) .Include(x => x.LegalUnits) .ThenInclude(x => x.LocalUnits) .Include(x => x.EnterpriseGroup); list.AddRange(entUnit.Where(x => x.EnterpriseGroup.IsDeleted == false).Select(x => x.EnterpriseGroup)); list.AddRange(entUnit); list.AddRange(entUnit.SelectMany(x => x.LegalUnits.Where(y => y.IsDeleted == false))); list.AddRange(entUnit.SelectMany(x => x.LegalUnits.Where(y => y.IsDeleted == false).SelectMany(y => y.LocalUnits.Where(z => z.IsDeleted == false)))); } if (type == null || type == StatUnitTypes.LegalUnit) { var legalUnit = _commonSvc.GetUnitsList <LegalUnit>(false) .Where(x => listIds.Contains(x.RegId)) .Include(x => x.EnterpriseUnit) .ThenInclude(x => x.EnterpriseGroup) .Include(x => x.LocalUnits); list.AddRange(legalUnit.Where(x => x.EnterpriseUnit.IsDeleted == false).Select(x => x.EnterpriseUnit).Where(x => x.EnterpriseGroup.IsDeleted == false).Select(x => x.EnterpriseGroup)); list.AddRange(legalUnit.Where(x => x.EnterpriseUnit.IsDeleted == false).Select(x => x.EnterpriseUnit)); list.AddRange(legalUnit); list.AddRange(legalUnit.SelectMany(x => x.LocalUnits.Where(y => y.IsDeleted == false))); } if (type == null || type == StatUnitTypes.LocalUnit) { var localUnit = _commonSvc.GetUnitsList <LocalUnit>(false) .Where(x => listIds.Contains(x.RegId)) .Include(x => x.LegalUnit) .ThenInclude(x => x.EnterpriseUnit) .ThenInclude(x => x.EnterpriseGroup); list.AddRange(localUnit.Where(x => x.LegalUnit.IsDeleted == false).Select(x => x.LegalUnit).Where(x => x.EnterpriseUnit.IsDeleted == false).Select(x => x.EnterpriseUnit).Where(x => x.EnterpriseGroup.IsDeleted == false).Select(x => x.EnterpriseGroup)); list.AddRange(localUnit.Where(x => x.LegalUnit.IsDeleted == false).Select(x => x.LegalUnit).Where(x => x.EnterpriseUnit.IsDeleted == false).Select(x => x.EnterpriseUnit)); list.AddRange(localUnit.Where(x => x.LegalUnit.IsDeleted == false).Select(x => x.LegalUnit)); list.AddRange(localUnit); } return(ToNodeVm(list, listIds)); }