using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Spaces;
using System.Web.Caching;
public partial class Index_Top : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            BindPage();
    }
    void BindPage()
    {
        rprTagList.ItemCreated += new RepeaterItemEventHandler(rprTagList_ItemCreated);
        rprTagList.DataSource = SiteTools.GetTagTop(TagType.All, 40);//标签
        rprTagList.DataBind();

        dlstTopicTop.DataSource = SiteHelper.GetTopicNumsTop(20);
        dlstTopicTop.DataBind();

        dlstTopicHitTop.DataSource = SiteHelper.GetTopicHitTop(20);
        dlstTopicHitTop.DataBind();

        dlstFeedBackTop.DataSource = SiteHelper.GetFeedBackTop(20);
        dlstFeedBackTop.DataBind();

        dlstPhotoCountTop.DataSource = SiteHelper.GetPhotoCountTop(20);
        dlstPhotoCountTop.DataBind();
    
       // rprBBSHitUser.DataSource = GetBBSHitUserTop(38);
       // rprBBSHitUser.DataBind();


    }

    void rprTagList_ItemCreated(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            TagItem item = e.Item.DataItem as TagItem;
            if (item != null)
            {
                HyperLink lnkTag = e.Item.FindControl("lnkTag") as HyperLink;
                if (lnkTag != null)
                {
                    //lnkTag.Text = string.Format("<font color=\"#CC3300\">{0}</font>", item.TagName);
                    lnkTag.Text = string.Format("{0}", item.TagName);
                    lnkTag.NavigateUrl = string.Format("TagTopList.aspx?TagName={0}", Server.UrlEncode(item.TagName.TrimStart(' ').TrimEnd(' ')));
                    lnkTag.Target = "_blank";

                }
                Literal RefCount = e.Item.FindControl("RefCount") as Literal;
                if (RefCount != null)
                {
                    RefCount.Text = item.RefCount.ToString();
                }
            }

        }
    }
    DataView GetBBSHitUserTop(int count)
    {

        string key = string.Format("bfor_{0}_BBSHitUser_", count);
        DataView dv = HttpRuntime.Cache[key] as DataView;
        if (dv == null)
        {
            string sql = @"SELECT top {0} count(d.ID) as TopicCount,d.Account,c.Title FROM 
[BBS_Topic] d  
inner join [User_UserConfig] c     on (c.Account=d.Account)
group by d.[Account],c.Title order by TopicCount desc";
            dv = SpacesDataProvider.Instance().GetDataView(string.Format(sql, count));
            HttpRuntime.Cache.Add(key, dv, null, DateTime.Now.AddHours(3), TimeSpan.Zero, CacheItemPriority.Default, null);

        }
        return dv;

    }
    DataView GetPhotoCountTop(int count)
    {
        string key = string.Format("bfor_{0}_PhotoCountTop_", count);
        DataView dv = HttpRuntime.Cache[key] as DataView;
        if (dv == null)
        {
            string sql = @"SELECT top {0} count(d.PhotoID) as PhotoCount,pc.Account,uc.Title FROM 
[Photo_Data] d 
inner join [photo_Categories] pc on (d.CategoryID=pc.ID) 
inner join [User_UserConfig] uc     on (uc.Account=pc.Account)
group by pc.[Account],uc.Title order by PhotoCount desc";
            dv = SpacesDataProvider.Instance().GetDataView(string.Format(sql, count));
            HttpRuntime.Cache.Add(key, dv, null, DateTime.Now.AddHours(3), TimeSpan.Zero, CacheItemPriority.Default, null);

        }
        return dv;

    }
   
    
   
}
