using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Caching;

namespace BookDemo.QA
{
    //搜索缓存类
    public class SearchCache : BaseCache<List<SearchCacheInfo>>
    {
        public const string CACHE_KEY = "SearchCache";
        public SearchCache()
            : base(CACHE_KEY)
        {
            base._timeout = 5;
        }
        public override List<SearchCacheInfo> Update()
        {
            BookDemoEntities ctx = new BookDemoEntities();
            List<SearchCacheInfo> searchInfo =
                               (from q in ctx.QAQuestion//从QAQuestion中查询
                                       .Include("QAAnswer")//关联查询QAAnswer
                                       .Include("QACatalog")//关联查询QACatalog
                                       .Include("QAUserInfo")//关联查询QAUserInfo
                                from a in q.QAAnswer//同时关联查询QAQAAnswer
                                where q.State == 1//提问状态必须为“已解决”
                                && a.BestAnswer == true//回答状态必须为“最佳答案”
                                select new SearchCacheInfo()
                                {
                                    QuestionId = q.Id,//提问ID
                                    QuestionTitle = q.Title,//提问标题
                                    QuestionText = q.Text,//提问内容
                                    QuestionUserName = q.QAUserInfo.UserName,//提问者用户名
                                    BestAnswerText = a.Text,//最佳答案内容
                                    BestAnswerUserName = a.QAUserInfo.UserName,//最佳答案的用户名
                                    BestAnswerTime = a.PostDatetime,//最佳回答时间
                                    CatalogId = q.QACatalog.Id,//所属目录ID
                                    CatalogName = q.QACatalog.Name//所属目录名称
                                }
                          ).ToList();//转为List<T>类型，此类型为匿名类型
            base.Data = searchInfo;
            return searchInfo;
        }
    }
}
