public List<TpTreaty> GetTpTreaty(string hostCountryCode, string searchQuery)
        {
            if (!string.IsNullOrEmpty(searchQuery))
            {
                return GetTpTreatyByArticleContentSearch(hostCountryCode, searchQuery.Trim());
            }
            using (var work = new UnitOfWork())
            {

                var allTpTreaties = new List<TpTreaty>();
                var tpTreaties = new TpTreatyRepository(work).GetTpTreaty(hostCountryCode, searchQuery).ToList();
                var distinctConceptIds = tpTreaties.Select(b => b.OcdeArt1_ConceptId).Distinct()
                                                   .Union(tpTreaties.Select(b => b.OcdeArt2_ConceptId).Distinct())
                                                   .Union(tpTreaties.Select(b => b.OcdeArt3_ConceptId).Distinct())
                                                   .Union(tpTreaties.Select(b => b.CompetentAuthority_ConceptId).Distinct())
                                                   .Union(tpTreaties.Select(b => b.ReliefFromDouble_ConceptId).Distinct())
                                                   .Where(b => b!= null).Select(b => b);

                var conceptIds = string.Join(",", distinctConceptIds);
                var partnerCountryCodes = string.Join(",", tpTreaties.Select(b => b.Country1.Code).ToList());
                var articles = new TreetyBusiness().GetArticlesByHostCountry(hostCountryCode, partnerCountryCodes, conceptIds);
                var treatyBusiness = new TreetyBusiness();
                foreach (var treaty in tpTreaties)
                {
                    var countryPair = treatyBusiness.GetPairCountryCode(hostCountryCode, treaty.Country1.Code);
                    allTpTreaties.Add(new TpTreaty()
                                          {
                                                Id = treaty.Id,
                                                OcdeArt1 = !string.IsNullOrEmpty(treaty.OcdeArt1) ? treaty.OcdeArt1 : string.Empty,
                                                OcdeArt2 = !string.IsNullOrEmpty(treaty.OcdeArt2) ? treaty.OcdeArt2 : string.Empty,
                                                TreatyPartnerCountry = treaty.Country1.Name,
                                                TreatyPartnerCountryCode = treaty.Country1.Code,
                                                CompetentAuthority = !string.IsNullOrEmpty(treaty.CompetentAuthority) ? treaty.CompetentAuthority : string.Empty,
                                                ReliefFromDouble = !string.IsNullOrEmpty(treaty.ReliefFromDouble) ? treaty.ReliefFromDouble : string.Empty,
                                                OcdeArt1ConceptId = treaty.OcdeArt1_ConceptId,
                                                OcdeArt2ConceptId = treaty.OcdeArt2_ConceptId,
                                                ReliefFromDoubleConceptId = treaty.ReliefFromDouble_ConceptId,
                                                CompetentAuthorityConceptId = treaty.CompetentAuthority_ConceptId,
                                                OcdeArt3 = treaty.OcdeArt3,
                                                OcdeArt3ConceptId = treaty.OcdeArt3_ConceptId,
                                                IsContentAvailbleForCompetentAuthority = articles.Count(b => (b.HostCountryCode == countryPair[0] && b.PartnerCountryCode == countryPair[1])
                                                                                                              && b.ConceptId == treaty.CompetentAuthority_ConceptId && b.ArticleContent.Length > 0) > 0,
                                                IsContentAvailbleForReliefFromDouble = articles.Count(b => (b.HostCountryCode == countryPair[0] && b.PartnerCountryCode == countryPair[1])
                                                                                                              && b.ConceptId == treaty.ReliefFromDouble_ConceptId && b.ArticleContent.Length > 0) > 0,
                                                IsContentAvailbleForOcdeArt3 = articles.Count(b => (b.HostCountryCode == countryPair[0] && b.PartnerCountryCode == countryPair[1])
                                                                                                              && b.ConceptId == treaty.OcdeArt3_ConceptId && b.ArticleContent.Length > 0) > 0,
                                                IsContentAvailbleForOcdeArt2 = articles.Count(b => (b.HostCountryCode == countryPair[0] && b.PartnerCountryCode == countryPair[1])
                                                                                                              && b.ConceptId == treaty.OcdeArt2_ConceptId && b.ArticleContent.Length > 0) > 0,
                                                IsContentAvailbleForOcdeArt1 = articles.Count(b => (b.HostCountryCode == countryPair[0] && b.PartnerCountryCode == countryPair[1])
                                                                                                              && b.ConceptId == treaty.OcdeArt1_ConceptId && b.ArticleContent.Length > 0) > 0
                                          });

                }

                allTpTreaties = allTpTreaties.OrderBy(a => a.TreatyPartnerCountry).ToList();
                return allTpTreaties;
            }
        }
        public List<TpTreaty> GetTpTreatyByArticleContentSearch(string hostCountryCode, string searchQuery)
        {
            using (var work = new UnitOfWork())
             {

                var allTpTreaties = new List<TpTreaty>();
                var tpTreaties = new TpTreatyRepository(work).GetTpTreaty(hostCountryCode, null).ToList();
                var distinctConceptIds = GetDistinctConceptIds(tpTreaties);
                var conceptIds = string.Join(",", distinctConceptIds);
                if (conceptIds.Length == 0) return allTpTreaties;
                var partnerCountryCodes = string.Join(",", tpTreaties.Select(b => string.Format("'{0}'", b.Country1.Code)).ToList());
                if (partnerCountryCodes.Length == 0) return allTpTreaties;
                var articles =  work.UnityDbContext.TreatyArticleContentSearchSp(hostCountryCode, partnerCountryCodes,searchQuery, conceptIds).ToList();
                var articlesGroups = (from item in articles
                                       group item by new {item.HostCountryId,item.PartnerCountryId}
                                       into groupedItems
                                       select groupedItems).ToList();

                foreach (var itemGroup in articlesGroups)
                {
                    var treaty = tpTreaties.FirstOrDefault(b => (
                                                                 (b.HostCountryId == itemGroup.Key.HostCountryId && b.PartnerCountryId == itemGroup.Key.PartnerCountryId) ||
                                                                 (b.HostCountryId == itemGroup.Key.PartnerCountryId && b.PartnerCountryId == itemGroup.Key.HostCountryId)
                                                                )
                                                           );
                    if (treaty != null)
                    {
                        if (string.IsNullOrEmpty(treaty.OcdeArt1_ConceptId) && string.IsNullOrEmpty(treaty.OcdeArt2_ConceptId) && string.IsNullOrEmpty(treaty.OcdeArt3_ConceptId)
                            && string.IsNullOrEmpty(treaty.ReliefFromDouble_ConceptId) && string.IsNullOrEmpty(treaty.CompetentAuthority_ConceptId)) continue;
                        allTpTreaties.Add(new TpTreaty()
                        {
                            Id = treaty.Id,
                            OcdeArt1 = !string.IsNullOrEmpty(treaty.OcdeArt1) ? treaty.OcdeArt1 : string.Empty,
                            OcdeArt2 = !string.IsNullOrEmpty(treaty.OcdeArt2) ? treaty.OcdeArt2 : string.Empty,
                            TreatyPartnerCountry = treaty.Country1.Name,
                            TreatyPartnerCountryCode = treaty.Country1.Code,
                            CompetentAuthority = !string.IsNullOrEmpty(treaty.CompetentAuthority) ? treaty.CompetentAuthority : string.Empty,
                            ReliefFromDouble = !string.IsNullOrEmpty(treaty.ReliefFromDouble) ? treaty.ReliefFromDouble : string.Empty,
                            OcdeArt1ConceptId = treaty.OcdeArt1_ConceptId,
                            OcdeArt2ConceptId = treaty.OcdeArt2_ConceptId,
                            ReliefFromDoubleConceptId = treaty.ReliefFromDouble_ConceptId,
                            CompetentAuthorityConceptId = treaty.CompetentAuthority_ConceptId,
                            OcdeArt3 = treaty.OcdeArt3,
                            OcdeArt3ConceptId = treaty.OcdeArt3_ConceptId,
                            IsContentAvailbleForCompetentAuthority = itemGroup.Count(b => b.ConceptId == treaty.CompetentAuthority_ConceptId) > 0,
                            IsContentAvailbleForReliefFromDouble = itemGroup.Count(b => b.ConceptId == treaty.ReliefFromDouble_ConceptId) > 0,
                            IsContentAvailbleForOcdeArt3 = itemGroup.Count(b => b.ConceptId == treaty.OcdeArt3_ConceptId) > 0,
                            IsContentAvailbleForOcdeArt2 = itemGroup.Count(b => b.ConceptId == treaty.OcdeArt2_ConceptId) > 0,
                            IsContentAvailbleForOcdeArt1 = itemGroup.Count(b => b.ConceptId == treaty.OcdeArt1_ConceptId) > 0
                        });
                    }

                }
                return allTpTreaties;
            }
        }