示例#1
0
        private async Task <SearchVm <QueueLogVm> > GetQueueLogForActivityUpload(int queueId, PaginatedQueryM query)
        {
            var orderBy   = string.IsNullOrEmpty(query.SortBy) ? nameof(DataUploadingLog.Id) : query.SortBy;
            var orderRule = query.SortAscending ? "ASC" : "DESC";
            var filtered  = _dbContext.DataUploadingLogs
                            .Where(x => x.DataSourceQueueId == queueId)
                            .OrderBy($"{orderBy} {orderRule}")
                            .GroupBy(x => x.TargetStatId);
            var total = await filtered.CountAsync();

            var result = (await filtered
                          .Skip(Pagination.CalculateSkip(query.PageSize, query.Page, total))
                          .Take(query.PageSize)
                          .AsNoTracking()
                          .ToListAsync())
                         .Select(x => new DataUploadingLog
            {
                DataSourceQueue = x.FirstOrDefault()?.DataSourceQueue,
                EndImportDate   = x.Select(y => y.EndImportDate).Max(),
                StartImportDate = x.Select(y => y.StartImportDate).Max(),
                TargetStatId    = x.FirstOrDefault()?.TargetStatId,
                StatUnitName    = x.FirstOrDefault()?.StatUnitName,
                Status          = x.Any(y => y.Status == DataUploadingLogStatuses.Error)
                        ? DataUploadingLogStatuses.Error
                        : x.Any(y => y.Status == DataUploadingLogStatuses.Warning)
                            ? DataUploadingLogStatuses.Warning
                            : DataUploadingLogStatuses.Done,
                Note = x.FirstOrDefault()?.Note,
            });

            return(SearchVm <QueueLogVm> .Create(result.Select(QueueLogVm.Create), total));
        }
示例#2
0
        /// <summary>
        /// Method for obtaining a detailed history of stat. units
        /// </summary>
        /// <param name = "type"> Type of stat. units </param>
        /// <param name = "id"> Id stat. units </param>
        /// <param name = "userId"> User Id </param>
        /// <param name = "isHistory"> Is the stat. historical unit </param>
        /// <returns> </returns>
        public async Task <object> ShowHistoryDetailsAsync(StatUnitTypes type, int id, string userId, bool isHistory)
        {
            var history = type == StatUnitTypes.EnterpriseGroup
                ? await FetchDetailedUnitHistoryAsync <EnterpriseGroup, EnterpriseGroupHistory>(id, userId, isHistory)
                : await FetchDetailedUnitHistoryAsync <StatisticalUnit, StatisticalUnitHistory>(id, userId, isHistory);

            var result = history.ToArray();

            return(SearchVm.Create(result, result.Length));
        }
示例#3
0
        /// <summary>
        /// Method for obtaining the history of stat. units
        /// </summary>
        /// <param name = "type"> Type of stat. Edinet </param>
        /// <param name = "id"> Id stat. Edinet </param>
        /// <returns> </returns>
        public async Task <object> ShowHistoryAsync(StatUnitTypes type, int id)
        {
            var history = type == StatUnitTypes.EnterpriseGroup
                ? await FetchUnitHistoryAsync <EnterpriseGroup, EnterpriseGroupHistory>(id)
                : await FetchUnitHistoryAsync <StatisticalUnit, StatisticalUnitHistory>(id);

            var result = history.ToArray();

            return(SearchVm.Create(result, result.Length));
        }
示例#4
0
        /// <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));
        }
示例#5
0
        public async Task <SearchVm <QueueVm> > GetAllDataSourceQueues(SearchQueryM query)
        {
            var sortBy = string.IsNullOrEmpty(query.SortBy)
                ? "Id"
                : query.SortBy;

            var orderRule = query.OrderByValue == OrderRule.Asc && !string.IsNullOrEmpty(query.SortBy)
                ? "ASC"
                : "DESC";

            var filtered = _dbContext.DataSourceQueues
                           .Include(x => x.DataSource)
                           .Include(x => x.User)
                           .AsNoTracking();

            if (query.Status.HasValue)
            {
                filtered = filtered.Where(x => x.Status == query.Status.Value);
            }

            if (query.DateFrom.HasValue && query.DateTo.HasValue)
            {
                filtered = filtered.Where(x => x.StartImportDate >= query.DateFrom.Value &&
                                          x.StartImportDate <= query.DateTo.Value);
            }
            else
            {
                if (query.DateFrom.HasValue)
                {
                    filtered = filtered.Where(x => x.StartImportDate >= query.DateFrom.Value);
                }

                if (query.DateTo.HasValue)
                {
                    filtered = filtered.Where(x => x.StartImportDate <= query.DateTo.Value);
                }
            }

            filtered = filtered.OrderBy($"{sortBy} {orderRule}");

            var total = await filtered.CountAsync();

            var result = await filtered
                         .Skip(Pagination.CalculateSkip(query.PageSize, query.Page, total))
                         .Take(query.PageSize)
                         .AsNoTracking()
                         .ToListAsync();

            return(SearchVm <QueueVm> .Create(result.Select(QueueVm.Create), total));
        }
示例#6
0
        /// <summary>
        /// Method for obtaining a list of regions
        /// </summary>
        /// <param name = "model"> Model </param>
        /// <param name = "predicate"> Predicate </param>
        /// <returns> </returns>
        public async Task <SearchVm <Region> > ListAsync(PaginatedQueryM model,
                                                         Expression <Func <Region, bool> > predicate = null)
        {
            IQueryable <Region> query = _context.Regions;

            if (predicate != null)
            {
                query = query.Where(predicate);
            }
            var total = await query.CountAsync();

            var regions = await query.OrderBy(v => v.Code)
                          .Skip(Pagination.CalculateSkip(model.PageSize, model.Page, total))
                          .Take(model.PageSize)
                          .ToListAsync();

            return(SearchVm <Region> .Create(regions, total));
        }
示例#7
0
        private async Task <SearchVm <QueueLogVm> > GetQueueLogForStatUnitUpload(int queueId, PaginatedQueryM query)
        {
            var orderBy   = string.IsNullOrEmpty(query.SortBy) ? nameof(DataUploadingLog.Id) : query.SortBy;
            var orderRule = query.SortAscending ? "ASC" : "DESC";
            var filtered  = _dbContext.DataUploadingLogs
                            .Where(x => x.DataSourceQueueId == queueId)
                            .OrderBy($"{orderBy} {orderRule}");

            var total = await filtered.CountAsync();

            var result = await filtered
                         .Skip(Pagination.CalculateSkip(query.PageSize, query.Page, total))
                         .Take(query.PageSize)
                         .AsNoTracking()
                         .ToListAsync();

            return(SearchVm <QueueLogVm> .Create(result.Select(QueueLogVm.Create), total));
        }
示例#8
0
        /// <summary>
        /// Method for obtaining all data sources
        /// </summary>
        /// <param name = "query"> Request </param>
        /// <returns> </returns>
        public async Task <SearchVm <DataSourceVm> > GetAllDataSources(SearchQueryM query)
        {
            var wildcard          = query.Wildcard;
            var statUnitType      = query.StatUnitType;
            var priority          = (DataSourcePriority)query.Priority;
            var allowedOperations = (DataSourceAllowedOperation)query.AllowedOperations;

            var sortBy = string.IsNullOrEmpty(query.SortBy)
                ? "Id"
                : query.SortBy;

            var orderRule = query.OrderByValue == OrderRule.Asc
                ? "ASC"
                : "DESC";

            var filtered = _context.DataSources
                           .AsNoTracking()
                           .Where(ds => string.IsNullOrEmpty(wildcard) || ds.Name.ToLower().Contains(wildcard.ToLower()))
                           .Where(ds => statUnitType == 0 || ds.StatUnitType == (StatUnitTypes)statUnitType)
                           .Where(ds => priority == 0 || ds.Priority == priority)
                           .Where(ds => allowedOperations == 0 || ds.AllowedOperations == allowedOperations)
                           .OrderBy($"{sortBy} {orderRule}");


            var total = await filtered.CountAsync();

            if (query.GetAll)
            {
                var res = await filtered.ToListAsync();

                return(SearchVm <DataSourceVm> .Create(res.Select(DataSourceVm.Create), total));
            }

            var result = await filtered
                         .Skip(Pagination.CalculateSkip(query.PageSize, query.Page, total))
                         .Take(query.PageSize)
                         .ToListAsync();

            return(SearchVm <DataSourceVm> .Create(result.Select(DataSourceVm.Create), total));
        }
示例#9
0
        public async Task <SearchVm <ElasticStatUnit> > Search(SearchQueryM filter, string userId, bool isDeleted)
        {
            await Synchronize();

            var mustQueries =
                new List <Func <QueryContainerDescriptor <ElasticStatUnit>, QueryContainer> >
            {
                m => m.Term(p => p.Field(f => f.IsDeleted).Value(isDeleted))
            };

            var separators = new[] { ' ', '\t', '\r', '\n', ',', '.', '-' };

            if (!string.IsNullOrWhiteSpace(filter.Name))
            {
                var nameFilterParts = filter.Name.ToLower().Split(separators, StringSplitOptions.RemoveEmptyEntries);
                foreach (var nameFilter in nameFilterParts)
                {
                    mustQueries.Add(m => m.Prefix(p => p.Field(f => f.Name).Value(nameFilter)));
                }
            }

            if (filter.Type.Any())
            {
                mustQueries.Add(m => m.Terms(p => p.Field(f => f.UnitType).Terms(filter.Type)));
            }

            if (!string.IsNullOrWhiteSpace(filter.StatId))
            {
                mustQueries.Add(m => m.Terms(p => p.Field(f => f.StatId).Terms(filter.StatId)));
            }

            if (!string.IsNullOrWhiteSpace(filter.ExternalId))
            {
                mustQueries.Add(m => m.Terms(p => p.Field(f => f.ExternalId).Terms(filter.ExternalId)));
            }

            if (!string.IsNullOrWhiteSpace(filter.TaxRegId))
            {
                mustQueries.Add(m => m.Terms(p => p.Field(f => f.TaxRegId).Terms(filter.TaxRegId)));
            }

            if (!string.IsNullOrWhiteSpace(filter.Address))
            {
                string[] addressFilters = filter.Address.ToLower().Split(separators, StringSplitOptions.RemoveEmptyEntries);
                foreach (var addressFilter in addressFilters)
                {
                    mustQueries.Add(m => m
                                    .Bool(b => b
                                          .Should(s => s.Prefix(t => t.Field(f => f.AddressPart1).Value(addressFilter)) ||
                                                  s.Prefix(t => t.Field(f => f.AddressPart2).Value(addressFilter)) ||
                                                  s.Prefix(t => t.Field(f => f.AddressPart3).Value(addressFilter)))
                                          )
                                    );
                }
            }

            var turnoverQueries = new List <Func <QueryContainerDescriptor <ElasticStatUnit>, QueryContainer> >();

            if (filter.TurnoverFrom.HasValue)
            {
                double turnoverFrom = (double)filter.TurnoverFrom.Value;
                turnoverQueries.Add(m => m.Range(p => p.Field(f => f.Turnover).GreaterThanOrEquals(turnoverFrom)));
            }
            if (filter.TurnoverTo.HasValue)
            {
                double turnoverTo = (double)filter.TurnoverTo.Value;
                turnoverQueries.Add(m => m.Range(p => p.Field(f => f.Turnover).LessThanOrEquals(turnoverTo)));
            }

            var employeeQueries = new List <Func <QueryContainerDescriptor <ElasticStatUnit>, QueryContainer> >();

            if (filter.EmployeesNumberFrom.HasValue)
            {
                int employeesNumberFrom = (int)filter.EmployeesNumberFrom.Value;
                employeeQueries.Add(m => m.Range(p => p.Field(f => f.Employees).GreaterThanOrEquals(employeesNumberFrom)));
            }
            if (filter.EmployeesNumberTo.HasValue)
            {
                int employeesNumberTo = (int)filter.EmployeesNumberTo.Value;
                employeeQueries.Add(m => m.Range(p => p.Field(f => f.Employees).LessThanOrEquals(employeesNumberTo)));
            }
            if (filter.Comparison == ComparisonEnum.And || turnoverQueries.Count == 0 || employeeQueries.Count == 0)
            {
                mustQueries.AddRange(turnoverQueries);
                mustQueries.AddRange(employeeQueries);
            }
            else
            {
                mustQueries.Add(m => m.Bool(b => b.Should(s => s.Bool(b1 => b1.Must(turnoverQueries)), s => s.Bool(b2 => b2.Must(employeeQueries)))));
            }

            if (filter.LastChangeFrom.HasValue)
            {
                DateTime lastChangeFrom = filter.LastChangeFrom.Value.ToUniversalTime();
                mustQueries.Add(m => m.DateRange(p => p.Field(f => f.StartPeriod).GreaterThanOrEquals(lastChangeFrom)));
            }

            if (filter.LastChangeTo.HasValue)
            {
                DateTime lastChangeTo = filter.LastChangeTo.Value.ToUniversalTime().AddHours(23).AddMinutes(59).AddSeconds(59);
                mustQueries.Add(m => m.DateRange(p => p.Field(f => f.StartPeriod).LessThanOrEquals(lastChangeTo)));
            }

            if (filter.LastChangeFrom.HasValue || filter.LastChangeTo.HasValue)
            {
                mustQueries.Add(m => m.Bool(b => b.MustNot(mn => mn.Term(p => p.Field(f => f.ChangeReason).Value(ChangeReasons.Create)))));
            }

            if (filter.DataSourceClassificationId.HasValue)
            {
                int dataSourceClassificationId = filter.DataSourceClassificationId.Value;
                mustQueries.Add(m => m.Term(p => p.Field(f => f.DataSourceClassificationId).Value(dataSourceClassificationId)));
            }

            if (!filter.IncludeLiquidated.HasValue || !filter.IncludeLiquidated.Value)
            {
                mustQueries.Add(m => !m.Exists(e => e.Field(f => f.LiqDate)));
            }

            if (filter.RegMainActivityId.HasValue)
            {
                var regMainActivityIds = new[] { filter.RegMainActivityId.Value };
                mustQueries.Add(m => m.Terms(p => p.Field(f => f.ActivityCategoryIds).Terms(regMainActivityIds)));
            }

            if (filter.RegId.HasValue)
            {
                int id = filter.RegId.Value;
                mustQueries.Add(m => m.Term(p => p.Field(f => f.RegId).Value(id)));
            }
            if (filter.SectorCodeId.HasValue)
            {
                int sectorCodeId = filter.SectorCodeId.Value;
                mustQueries.Add(m => m.Term(p => p.Field(f => f.SectorCodeId).Value(sectorCodeId)));
            }

            if (filter.LegalFormId.HasValue)
            {
                int legalFormId = filter.LegalFormId.Value;
                mustQueries.Add(m => m.Term(p => p.Field(f => f.LegalFormId).Value(legalFormId)));
            }

            if (filter.RegionId.HasValue)
            {
                int regionId = filter.RegionId.Value;
                mustQueries.Add(m => m.Term(p => p.Field(f => f.RegionId).Value(regionId)));
            }

            Func <SearchDescriptor <ElasticStatUnit>, ISearchRequest> searchFunc;

            if (filter.SortBy.HasValue)
            {
                searchFunc = s =>
                             s.Index(StatUnitSearchIndexName).From(0).Take(10000).Query(q => q.Bool(b => b.Must(mustQueries))).TerminateAfter(2000);
            }
            else
            {
                searchFunc = s =>
                             s.Index(StatUnitSearchIndexName).From((filter.Page - 1) * filter.PageSize).Take(filter.PageSize)
                             .Query(q => q.Bool(b => b.Must(mustQueries))).TerminateAfter(2000);
            }

            var searchResponse = await _elasticClient.SearchAsync(searchFunc);

            if (!searchResponse.IsValid)
            {
                return(SearchVm <ElasticStatUnit> .Create(new List <ElasticStatUnit>(), 0));
            }
            //throw new Exception(searchResponse.DebugInformation);

            List <ElasticStatUnit> units = searchResponse.Documents.ToList();

            if (filter.SortBy.HasValue)
            {
                IOrderedEnumerable <ElasticStatUnit> sortQuery;
                switch (filter.SortBy.Value)
                {
                case SortFields.Employees:
                    sortQuery = filter.SortRule == OrderRule.Asc ? units.OrderBy(u => u.Employees) : units.OrderByDescending(u => u.Employees);
                    break;

                case SortFields.StatId:
                    sortQuery = filter.SortRule == OrderRule.Asc ? units.OrderBy(u => u.StatId) : units.OrderByDescending(u => u.StatId);
                    break;

                case SortFields.Turnover:
                    sortQuery = filter.SortRule == OrderRule.Asc ? units.OrderBy(u => u.Turnover) : units.OrderByDescending(u => u.Turnover);
                    break;

                default:
                    sortQuery = filter.SortRule == OrderRule.Asc ? units.OrderBy(u => u.Name) : units.OrderByDescending(u => u.Name);
                    break;
                }
                units = sortQuery.Skip((filter.Page - 1) * filter.PageSize).Take(filter.PageSize).ToList();
            }
            return(SearchVm <ElasticStatUnit> .Create(units, searchResponse.Total));
        }