예제 #1
0
        /// <summary>
        /// 获取感兴趣的问题
        /// </summary>
        /// <param name="tenantTypeId">租户类型id</param>
        /// <param name="userId">用户id</param>
        /// <param name="topNumber">查询条数</param>
        /// <returns>问题列表</returns>
        public IEnumerable <AskQuestion> GetTopInterestedQuestions(string tenantTypeId, long userId, int topNumber)
        {
            StringBuilder cacheKey = new StringBuilder();

            cacheKey.Append(RealTimeCacheHelper.GetListCacheKeyPrefix(CacheVersionType.AreaVersion, "UserId", userId));
            cacheKey.AppendFormat("GetTopInterestedQuestions::tenantTypeId-{0}", tenantTypeId);

            //从缓存里取列表,如果缓存里没有就去数据库取
            List <AskQuestion> questions = cacheService.Get <List <AskQuestion> >(cacheKey.ToString());

            if (questions != null && questions.Count() > 0)
            {
                return(questions.Take(topNumber));
            }

            IEnumerable <object> questionIds = null;

            //先查询关注标签下的问题
            //查询用户关注的标签
            SubscribeService   subscribeService = new SubscribeService(TenantTypeIds.Instance().AskTag());
            IEnumerable <long> tagIds           = subscribeService.GetAllObjectIds(userId);

            if (tagIds != null && tagIds.Count() > 0)
            {
                Sql sql;
                Sql whereSql;
                Sql orderSql;
                BuildSqlForGetTopInterestedQuestions(tenantTypeId, out sql, out whereSql, out orderSql);
                sql.InnerJoin("tn_ItemsInTags").On("spb_AskQuestions.QuestionId = tn_ItemsInTags.ItemId")
                .Append(whereSql)
                .Where("tn_ItemsInTags.TenantTypeId = @0", TenantTypeIds.Instance().AskQuestion())
                .Where("tn_ItemsInTags.Id in (@tagIds)", new { tagIds = tagIds })
                .Append(orderSql);
                questionIds = CreateDAO().FetchTopPrimaryKeys <AskQuestion>(100, sql);
                questions   = this.PopulateEntitiesByEntityIds(questionIds).ToList();
                if (questions != null && questions.Count() >= topNumber)
                {
                    //加入缓存
                    cacheService.Add(cacheKey.ToString(), questions, CachingExpirationType.UsualObjectCollection);
                    return(questions.Take(topNumber));
                }
            }



            //如果查询结果不够topNumber,从关注用户的问题中查找
            //查询用户关注的用户
            FollowService      followService   = new FollowService();
            IEnumerable <long> followedUserIds = followService.GetTopFollowedUserIds(userId, 100, null, Follow_SortBy.FollowerCount_Desc);

            if (followedUserIds != null && followedUserIds.Count() > 0)
            {
                Sql sql;
                Sql whereSql;
                Sql orderSql;
                BuildSqlForGetTopInterestedQuestions(tenantTypeId, out sql, out whereSql, out orderSql);
                sql.Append(whereSql)
                .Where("spb_AskQuestions.UserId in (@followedUserIds)", new { followedUserIds = followedUserIds })
                .Append(orderSql);
                questionIds = CreateDAO().FetchTopPrimaryKeys <AskQuestion>(100, sql);
                IEnumerable <AskQuestion> questionsFollow = this.PopulateEntitiesByEntityIds(questionIds);
                if (questionsFollow != null && questionsFollow.Count() > 0)
                {
                    if (questions == null)
                    {
                        questions = new List <AskQuestion>();
                    }
                    questions.AddRange(questionsFollow);
                    questions = questions.Distinct <AskQuestion>().ToList();
                }
                if (questions != null && questions.Count() >= topNumber)
                {
                    //加入缓存
                    cacheService.Add(cacheKey.ToString(), questions, CachingExpirationType.UsualObjectCollection);
                    return(questions.Take(topNumber));
                }
            }

            //如果查询结果还不够topNumber,从最新问题中查找
            Sql sqlNew;
            Sql whereSqlNew;
            Sql orderSqlNew;

            BuildSqlForGetTopInterestedQuestions(tenantTypeId, out sqlNew, out whereSqlNew, out orderSqlNew);
            sqlNew.Append(whereSqlNew).Append(orderSqlNew);
            questionIds = CreateDAO().FetchTopPrimaryKeys <AskQuestion>(100, sqlNew);
            IEnumerable <AskQuestion> questionsNew = this.PopulateEntitiesByEntityIds(questionIds);

            if (questionsNew != null && questionsNew.Count() > 0)
            {
                if (questions == null)
                {
                    questions = new List <AskQuestion>();
                }
                questions.AddRange(questionsNew);
                questions = questions.Distinct().ToList();
            }
            if (questions != null)
            {
                //加入缓存
                cacheService.Add(cacheKey.ToString(), questions, CachingExpirationType.UsualObjectCollection);
                return(questions.Take(topNumber));
            }

            return(questions);
        }