/// <summary> /// Stat search method. units /// </summary> /// <param name = "filter"> Request </param> /// <param name = "userId"> User Id </param> /// <param name = "isDeleted"> Distance flag </param> /// <returns> </returns> public async Task <SearchVm> Search(SearchQueryM filter, string userId, bool isDeleted = false) { await _elasticService.CheckElasticSearchConnection(); bool isAdmin = await _userService.IsInRoleAsync(userId, DefaultRoleNames.Administrator); long totalCount; List <ElasticStatUnit> units; if (filter.IsEmpty()) { var baseQuery = _dbContext.StatUnitSearchView .Where(s => s.IsDeleted == isDeleted && s.LiqDate == null); totalCount = baseQuery.Count(); units = (await baseQuery.Skip((filter.Page - 1) * filter.PageSize).Take(filter.PageSize).ToListAsync()) .Select(Mapper.Map <StatUnitSearchView, ElasticStatUnit>).ToList(); } else { var searchResponse = await _elasticService.Search(filter, userId, isDeleted); totalCount = searchResponse.TotalCount; units = searchResponse.Result.ToList(); } var finalIds = units.Where(x => x.UnitType != StatUnitTypes.EnterpriseGroup) .Select(x => x.RegId).ToList(); var finalRegionIds = units.Select(x => x.RegionId).ToList(); var unitsToPersonNames = await GetUnitsToPersonNamesByUnitIds(finalIds); var unitsToMainActivities = await GetUnitsToPrimaryActivities(finalIds); var regions = await GetRegionsFullPaths(finalRegionIds); var permissions = await _userService.GetDataAccessAttributes(userId, null); var helper = new StatUnitCheckPermissionsHelper(_dbContext); var result = units .Select(x => new SearchViewAdapterModel(x, unitsToPersonNames[x.RegId], unitsToMainActivities[x.RegId], regions.GetValueOrDefault(x.RegionId))) .Select(x => SearchItemVm.Create(x, x.UnitType, permissions.GetReadablePropNames(), !isAdmin && !helper.IsRegionOrActivityContains(userId, x.RegionId != null ? new List <int> { (int)x.RegionId } : new List <int>(), x.ActivityCategoryIds))); return(SearchVm.Create(result, totalCount)); }
/// <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)); }