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_Photo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            BindPage();
    }
    void BindPage()
    {
  dlstNewPhotos.DataSource = SiteHelper.GetNewPhotos(24);
        dlstNewPhotos.DataBind();
        dlstHitPhotos.DataSource = SiteHelper.GetHitPhotos(16);
        dlstHitPhotos.DataBind();

        DataView dv = SiteHelper.GetNewPhotoSpaces(30);  //最近更新的空间
        rprUpdated.DataSource = dv;
        rprUpdated.DataBind();
 
      

       

        rprTagList.ItemCreated += new RepeaterItemEventHandler(rprTagList_ItemCreated);
        rprTagList.DataSource = SiteTools.GetTagTop(TagType.Photo, 38);//标签
        rprTagList.DataBind();
    }
    DataView GetNewPhotoSpaces(int count)
    {

        string key = string.Format("bfor_{0}_NewPhotoSpaces", count);
        DataView dv = HttpRuntime.Cache[key] as DataView;
        if (dv == null)
        {
            string sql = @"SELECT top {0}  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 Max(d.Created) 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 GetNewPhotos(int count)
    {
        string key = string.Format("bfor_{0}_newPhotos", count);
        DataView dv = HttpRuntime.Cache[key] as DataView;
        if (dv == null)
        {
            string sql = @"select top {0} d.*,c.[Name],c.[Account] from photo_Data d inner join photo_Categories c 
on c.ID=d.CategoryID order by d.Created 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;

    }
   
    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("Tag_Photos.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();
                }
            }

        }
    }
}
