public IEnumerable<IContent> SearchAll(SearchFormGroupModel searchModel)
        {
            var contentTypeService = ApplicationContext.Current.Services.ContentTypeService;
            var contentService = ApplicationContext.Current.Services.ContentService;

            var contentType = contentTypeService.GetContentType("FormGroups");
            if (contentType == null)
            {
                return new List<IContent>();
            }

            var contentNode = contentService.GetContentOfContentType(contentType.Id).FirstOrDefault();
            if (contentNode == null)
            {
                return new List<IContent>();
            }
            var parentId = contentNode.Id;
            var resultItems = contentService.GetChildren(parentId);

            var filteredResult = resultItems
                .Where(x =>
                {
                    var states = x.GetValue<string>("states");
                    var category = x.GetValue<string>("category");
                    var productType = x.GetValue<string>("productType");
                    var productName = x.GetValue<string>("productName");
                    var memberSituation = x.GetValue<string>("memberSituation");

                    return (!searchModel.States.Any() || !string.IsNullOrEmpty(states) && states.ToDelimitedList().ContainsAll(searchModel.States)) &&
                           (!searchModel.Categories.Any() || !string.IsNullOrEmpty(category) && category.ToDelimitedList().ContainsAll(searchModel.Categories)) &&
                           (!searchModel.ProductTypes.Any() || !string.IsNullOrEmpty(productType) && productType.ToDelimitedList().ContainsAll(searchModel.ProductTypes)) &&
                           (!searchModel.ProductNames.Any() || !string.IsNullOrEmpty(productName) && productName.ToDelimitedList().ContainsAll(searchModel.ProductNames)) &&
                           (!searchModel.MemberSituations.Any() || !string.IsNullOrEmpty(memberSituation) && memberSituation.ToDelimitedList().ContainsAll(searchModel.MemberSituations));
                })
                .Sort(x => x.GetValue<string>("groupName"), searchModel.SortDirection);
            return filteredResult;
        }
        public PagedResult<AgentFormListItem> SearchAgentForms(SearchAgentFormModel searchModel)
        {
            int pageSize = 10;
            var settingService = GleanerContext.Current.Services.SettingService;
            Int32.TryParse(settingService.AllSettings["ItemsPerPage"].IfNotNull(x => x.Value), out pageSize);
            var pageNumber = searchModel.PageNumber > 0 ? searchModel.PageNumber : 1;

            var formGroupService = GleanerContext.Current.Services.FormGroupService;
            var searchFormGroupModel = new SearchFormGroupModel
            {
                States = searchModel.States,
                Categories = new[] { searchModel.Category }.Where(x => !string.IsNullOrEmpty(x)).ToList(),
                ProductTypes = new[] { searchModel.ProductType }.Where(x => !string.IsNullOrEmpty(x)).ToList(),
                ProductNames = new[] { searchModel.ProductName }.Where(x => !string.IsNullOrEmpty(x)).ToList(),
                MemberSituations = new[] { searchModel.MemberSituation }.Where(x => !string.IsNullOrEmpty(x)).ToList()
            };

            var groups = formGroupService.SearchAll(searchFormGroupModel).ToList();

            var searchTerms = searchModel.TextSearch.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);

            var filteredItems = (
                from @group in groups
                let formIds = @group.GetValue<string>("forms").ToDelimitedList().Select(Int32.Parse)
                let forms = formIds.Any() ? ApplicationContext.Current.Services.ContentService.GetByIds(formIds) : new List<IContent>()

                from form in forms
                let formUploadValue = form.GetValue<string>("formUpload")
                let fileTypes = string.IsNullOrEmpty(formUploadValue)
                    ? new List<string>()
                    : formUploadValue.ToDelimitedList()
                        .Where(x => !string.IsNullOrEmpty(x))
                        .Select(x => Path.GetExtension(x).ToLowerInvariant().Replace(".", string.Empty))
                        .Distinct()
                        .ToList()
                let searchTermSource = string.Join(" ", form.GetValue<string>("formName"), form.GetValue<string>("formId"), form.GetValue<string>("formDescription"))
                where !searchTerms.Any() || searchTerms.All(searchTermSource.InvariantContains)
                select new AgentFormListItem
                {
                    GroupId = @group.Id,
                    GroupName = @group.GetValue<string>("groupName"),
                    GroupDescription = @group.GetValue<string>("groupDescription"),
                    FormUmbracoId = form.Id,
                    FormId = form.GetValue<string>("formId"),
                    FormName = form.GetValue<string>("formName"),
                    FileTypes = fileTypes,
                    UpdateDate = form.UpdateDate,
                    UpdateBy = ApplicationContext.Current.Services.UserService.GetUserById(form.WriterId).Name
                }
                ).ToList();
            var totalItems = filteredItems.Count;
            if (totalItems == 0)
            {
                return new PagedResult<AgentFormListItem>(0, 0, 0)
                {
                    Items = new List<AgentFormListItem>()
                };
            }

            var pagedResult = new PagedResult<AgentFormListItem>(totalItems, pageNumber, pageSize)
            {
                Items = filteredItems.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList()
            };
            return pagedResult;
        }
        public QueryResultDisplay SearchFormGroups(QueryDisplay query)
        {
            var states = query.Parameters.FirstOrDefault(x => x.FieldName == "states").IfNotNull(x => x.Value);
            var categories = query.Parameters.FirstOrDefault(x => x.FieldName == "categories").IfNotNull(x => x.Value);
            var productTypes = query.Parameters.FirstOrDefault(x => x.FieldName == "productTypes").IfNotNull(x => x.Value);
            var productNames = query.Parameters.FirstOrDefault(x => x.FieldName == "productNames").IfNotNull(x => x.Value);
            var memberSituations = query.Parameters.FirstOrDefault(x => x.FieldName == "memberSituations").IfNotNull(x => x.Value);
            if (query.ItemsPerPage < 1)
            {
                query.ItemsPerPage = _itemsPerPage;
            }

            var searchModel = new SearchFormGroupModel
            {
                SortDirection = query.SortDirection,

                States = !string.IsNullOrEmpty(states) ? states.ToDelimitedList() : new List<string>(),
                Categories = !string.IsNullOrEmpty(categories) ? categories.ToDelimitedList() : new List<string>(),
                ProductTypes = !string.IsNullOrEmpty(productTypes) ? productTypes.ToDelimitedList() : new List<string>(),
                ProductNames = !string.IsNullOrEmpty(productNames) ? productNames.ToDelimitedList() : new List<string>(),
                MemberSituations = !string.IsNullOrEmpty(memberSituations) ? memberSituations.ToDelimitedList() : new List<string>()
            };

            var forms = _formGroupService.SearchAll(searchModel);
            var totalItems = forms.Count();
            var pagedForms = forms
                .Skip(query.CurrentPage * query.ItemsPerPage)
                .Take(query.ItemsPerPage)
                .Select(Mapper.Map<IContent, ContentItemBasic<ContentPropertyBasic, IContent>>)
                .ToList();
            var result = QueryResultDisplay.Build(pagedForms, totalItems, query.CurrentPage, query.ItemsPerPage);
            return result;
        }