/// <summary> /// Method for getting stat. units by Id and type /// </summary> /// <param name = "id"> Id stat. units </param> /// <param name = "type"> Type of stat. units </param> /// <param name = "userId"> User Id </param> /// <param name = "showDeleted"> Distance flag </param> /// <returns> </returns> public async Task <object> GetUnitByIdAndType(int id, StatUnitTypes type, string userId, bool showDeleted) { var item = await _commonSvc.GetStatisticalUnitByIdAndType(id, type, showDeleted); if (item == null) { throw new BadRequestException(nameof(Resource.NotFoundMessage)); } async Task FillRegionParents(Address address) { if (address?.Region?.ParentId == null) { return; } address.Region.Parent = await _regionService.GetRegionParents(address.Region.ParentId.Value); } await FillRegionParents(item.Address); await FillRegionParents(item.ActualAddress); await FillRegionParents(item.PostalAddress); var dataAttributes = await _userService.GetDataAccessAttributes(userId, item.UnitType); foreach (var person in item.PersonsUnits) { if (person.PersonTypeId != null) { person.Person.Role = (int)person.PersonTypeId; } } return(SearchItemVm.Create(item, item.UnitType, dataAttributes.GetReadablePropNames())); }
/// <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)); }