using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace WebApplication2
{
	/// <summary>
	/// adv_one_detail 的摘要说明。
	/// </summary>
	public partial class adv_one_detail : System.Web.UI.Page
	{
	  
		private static int select=1;
		protected void Page_Load(object sender, System.EventArgs e)
        {
          
			if(!Page.IsPostBack)
			{
				if(this.Session["USERNAME"]==null||this.Session["Role"]==null||this.Session["USERNAME"].ToString()==""||this.Session["Role"].ToString()=="")
				{
					Response.Redirect("login.aspx");
				}
				else
				{
					string [] role=this.Session["Role"].ToString().Split('#');
					bool exists=false;
					for(int i=0;i<role.Length;i++)
					{
                        if (role[i].Equals("13") || role[i].Equals("0"))
						{
							exists=true;break;
						}
					}
					if(exists)
                    {
                       
						this.Label1.Text="查询某个时段";
						this.begdate.Value=DateTime.Now.ToShortDateString();
						this.enddate.Value=DateTime.Now.ToShortDateString();
						ddlbind();
					}
					else
					{Response.Redirect("NoAuthor.aspx");}
				
				}
			
				
			}
             
			// 在此处放置用户代码以初始化页面
		}
		private void ddlbind()
		{
			SB.ADOHelper help=new SB.ADOHelper();
			ArrayList al=help.AdvListID();
			for(int i=0;i<al.Count;i++)
			{
				this.DropDownList1.Items.Add(al[i].ToString().Split('&')[1].ToString());
			}
			for(int i=0;i<al.Count;i++)
			{
				this.DropDownList1.Items[i].Value=al[i].ToString().Split('&')[0].ToString();
			}
		}
		private void dgbind()
		{  SB.ADOHelper help=new SB.ADOHelper();
           DataTable dt=new DataTable();
		   dt.Columns.Add(new DataColumn("时间段",typeof(string)));
           dt.Columns.Add(new DataColumn("点击量",typeof(string)));
		   dt.Columns.Add(new DataColumn("弹窗数",typeof(string)));
		   dt.Columns.Add(new DataColumn("PV量",typeof(string)));
			if(help.conn.State==ConnectionState.Closed)
			{
				help.conn.Open();
			}
			SqlCommand cmd=new SqlCommand();
			SqlDataReader rd=null;
			cmd.Connection=help.conn;
			string listid=this.DropDownList1.SelectedValue;
	          cmd.CommandText="select count(*) as Quantity,convert(smalldatetime,convert(varchar(10),HitData)) as hitdate  from HistoryDetail where AdvListId='"+listid+"' and convert(smalldatetime,convert(varchar(10),HitData))>='"+SB.ADOHelper.inputText(this.begdate.Value)+"' and  convert(smalldatetime,convert(varchar(10),HitData))<='"+SB.ADOHelper.inputText(this.enddate.Value)+"' group by  CONVERT(smalldatetime, CONVERT(varchar(10), HitData)) ";
			if(this.CheckBox1.Checked)
			{
				cmd.CommandText="select count(*) as Quantity,convert(smalldatetime,convert(varchar(10),HitData)) as hitdate from ToDayDetail where AdvListId='"+listid+"' and convert(smalldatetime,convert(varchar(10),HitData))>='"+SB.ADOHelper.inputText(this.begdate.Value)+"' and  convert(smalldatetime,convert(varchar(10),HitData))<='"+SB.ADOHelper.inputText(this.enddate.Value)+"' group by  CONVERT(smalldatetime, CONVERT(varchar(10), HitData))";
			}
			try
			{
				rd=cmd.ExecuteReader();
				while(rd.Read())
				{	DataRow newrow=dt.NewRow();
					newrow["时间段"]=Convert.ToDateTime(rd["hitdate"].ToString()).ToShortDateString();
					newrow["点击量"]=rd["Quantity"].ToString();
						newrow["弹窗数"]="0";
						newrow["PV量"]="0";
					dt.Rows.Add(newrow);
				}
			 rd.Close();
				
			}
			catch
			{Response.Write("<script>alert('数据读取失败!或日期格式不正确!')</script>");return;}
			int sumhit=0,sumshow=0,sumpv=0;
			for(int i=0;i<dt.Rows.Count;i++)
			{
				try
				{
					sumhit+=Convert.ToInt32(dt.Rows[i][1].ToString());
					sumshow+=Convert.ToInt32(dt.Rows[i][2].ToString());
					sumpv+=Convert.ToInt32(dt.Rows[i][3].ToString());
				}
				catch{}
			}
	     	DataRow row=dt.NewRow();
			row["时间段"]="合计";
			row["点击量"]=sumhit;
			row["弹窗数"]=sumshow;
			row["PV量"]=sumpv;
			dt.Rows.Add(row);
			this.DataGrid1.DataSource=new DataView(dt);
			this.DataGrid1.DataBind();
            //
			if(this.checkbox2.Checked)
			{
				dt.Rows.Clear();
				cmd.CommandText="select count(*) as Quantity,HitHour from HistoryDetail where AdvListId='"+listid+"' and convert(smalldatetime,convert(varchar(10),HitData))>='"+SB.ADOHelper.inputText(this.begdate.Value)+"' and  convert(smalldatetime,convert(varchar(10),HitData))<='"+SB.ADOHelper.inputText(this.enddate.Value)+"' group by HitHour";
				if(this.CheckBox1.Checked)
				{
					cmd.CommandText="select count(*) as Quantity,HitHour from ToDayDetail where AdvListId='"+listid+"' and convert(smalldatetime,convert(varchar(10),HitData))>='"+SB.ADOHelper.inputText(this.begdate.Value)+"' and  convert(smalldatetime,convert(varchar(10),HitData))<='"+SB.ADOHelper.inputText(this.enddate.Value)+"' group by HitHour";
				}
				try
				{
					rd=cmd.ExecuteReader();
					while(rd.Read())
					{
						DataRow newrow=dt.NewRow();
                        newrow["时间段"] = rd["HitHour"].ToString()+":00";
						newrow["点击量"]=rd["Quantity"].ToString();
						newrow["弹窗数"]="0";
						newrow["PV量"]="0";
						dt.Rows.Add(newrow);
					}
					rd.Close();
			
				}
				catch(Exception ex)
				{
					string ss=ex.ToString();
				}
				this.Datagrid3.DataSource=new DataView(dt);
				this.Datagrid3.DataBind();
			}
				help.conn.Close();
			
			
            
			
		}
		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			this.ImageButton1.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButton1_Click);
			this.ImageButton2.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButton2_Click);
			this.Imagebutton3.Click += new System.Web.UI.ImageClickEventHandler(this.Imagebutton3_Click);

		}
		#endregion

		protected void Button1_Click(object sender, System.EventArgs e)
		{
			if(select==1)
			{
				dgbind();
			}
			if(select==2)
			{
				dg2bind();	
			}
			if(select==3)
			{
				dg3bind();
			}
		}

		private void ImageButton2_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			select=2;
			this.lb2.Text="请选择广告";
			this.Label1.Text="查询申请广告的用户";
			this.Datagrid2.DataSource=null;
			this.Datagrid2.DataBind();
			this.Datagrid3.DataSource=null;
			this.Datagrid3.DataBind();
			this.pl2.Visible=false;
			this.pl3.Visible=true;
			this.pl4.Visible=true;
			this.pl5.Visible=false;
			this.pl6.Visible=true;
			
		}

		private void ImageButton1_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			select=1;
			this.DataGrid1.DataSource=null;
			this.DataGrid1.DataBind();
			this.Datagrid3.DataSource=null;
			this.Datagrid3.DataBind();
			this.lb2.Text="请选择广告";
			this.Label1.Text="查询某个时段";  
			this.pl2.Visible=true;
			this.pl3.Visible=false;
			this.pl4.Visible=true;
			this.pl5.Visible=false;
			this.pl6.Visible=true;
			
		}
		private void dg2bind()
		{
           string advlistid=this.DropDownList1.SelectedValue;
			DataTable dtall=new DataTable();
			dtall.Columns.Add(new DataColumn("日期",typeof(string)));
			dtall.Columns.Add(new DataColumn("用户名",typeof(string)));
			dtall.Columns.Add(new DataColumn("网站名",typeof(string)));
			dtall.Columns.Add(new DataColumn("点击量",typeof(string)));
			dtall.Columns.Add(new DataColumn("弹窗数",typeof(string)));
			dtall.Columns.Add(new DataColumn("PV量",typeof(string)));
			DataTable dt=new DataTable();
			dt.Columns.Add(new DataColumn("websiteid",typeof(string)));
			dt.Columns.Add(new DataColumn("advlistid",typeof(string)));
			SB.ADOHelper help=new SB.ADOHelper();
			if(help.conn.State==ConnectionState.Closed)
			{help.conn.Open();}
		    SqlCommand cmd=new SqlCommand();
			SqlDataReader rd=null;
			cmd.CommandText="select distinct WebsiteId,AdvListId from ApplicationAdv where AdvListId='"+advlistid+"' and Appearance<>2";
			cmd.Connection=help.conn;
			try
			{
				rd=cmd.ExecuteReader();
				while(rd.Read())
				{
					DataRow row=dt.NewRow();
					row["websiteid"]=rd["WebsiteId"].ToString();
					row["advlistid"]=rd["AdvListId"].ToString();
					dt.Rows.Add(row);
				}
				rd.Close();
                help.conn.Close();
			}
			catch
			{Response.Write("<script>alert('数据读取失败!或日期格式不正确!')</script>");return;}
			
			for(int i=0;i<dt.Rows.Count;i++)
			{
				if(help.conn.State==ConnectionState.Closed)
				{help.conn.Open();}
				if(!this.CheckBox1.Checked)
				{
					cmd.CommandText="select count(*) as Quantity,Website.UserName,Website.WebsiteName from HistoryDetail,Website where HistoryDetail.WebsiteId='"+dt.Rows[i][0].ToString()+"' and Website.id='"+dt.Rows[i][0].ToString()+"' and HistoryDetail.AdvListId='"+dt.Rows[i][1].ToString()+"' and convert(smalldatetime,convert(varchar(10),HitData))>='"+SB.ADOHelper.inputText(this.begdate.Value)+"' and  convert(smalldatetime,convert(varchar(10),HitData))<='"+SB.ADOHelper.inputText(this.enddate.Value)+"' group by UserName,WebsiteName order by Quantity";
				}
				else
				{
					cmd.CommandText="select count(*) as Quantity,Website.UserName,Website.WebsiteName from ToDayDetail,Website where ToDayDetail.WebsiteId='"+dt.Rows[i][0].ToString()+"' and Website.id='"+dt.Rows[i][0].ToString()+"' and ToDayDetail.AdvListId='"+dt.Rows[i][1].ToString()+"' and convert(smalldatetime,convert(varchar(10),HitData))>='"+SB.ADOHelper.inputText(this.begdate.Value)+"' and  convert(smalldatetime,convert(varchar(10),HitData))<='"+SB.ADOHelper.inputText(this.enddate.Value)+"' group by UserName,WebsiteName order by Quantity ";
				}
				rd=cmd.ExecuteReader();
				while(rd.Read())
				{
					DataRow row=dtall.NewRow();
					row["网站名"]=rd["WebsiteName"].ToString();
					row["用户名"]=rd["UserName"].ToString();
					if(this.CheckBox1.Checked)
					{
						row["日期"]=DateTime.Now.ToShortDateString();
					}
					else
					{
                       row["日期"]=this.begdate.Value+"至"+this.enddate.Value;
					}			
						row["点击量"]=rd["Quantity"].ToString().Trim()==""? "0":rd["Quantity"].ToString();					
						row["弹窗数"]="0";			
						row["PV量"]="0";
					dtall.Rows.Add(row);
				}
				rd.Close();
                help.conn.Close();
			}
			
			this.Datagrid2.DataSource=new DataView(dtall);
			Datagrid2.DataBind();
			//
			//
			if(this.checkbox2.Checked)
			{
				DataTable newtable=new DataTable();
				newtable.Columns.Add(new DataColumn("时间段",typeof(string)));
				newtable.Columns.Add(new DataColumn("点击量",typeof(string)));
				newtable.Columns.Add(new DataColumn("弹窗数",typeof(string)));
				newtable.Columns.Add(new DataColumn("PV量",typeof(string)));
				cmd.CommandText="select count(*) as Quantity,HitHour from HistoryDetail where AdvListId='"+advlistid+"' and convert(smalldatetime,convert(varchar(10),HitData))>='"+SB.ADOHelper.inputText(this.begdate.Value)+"' and  convert(smalldatetime,convert(varchar(10),HitData))<='"+SB.ADOHelper.inputText(this.enddate.Value)+"' group by HitHour";
				if(this.CheckBox1.Checked)
				{
					cmd.CommandText="select count(*) as Quantity,HitHour from ToDayDetail where AdvListId='"+advlistid+"' and convert(smalldatetime,convert(varchar(10),HitData))>='"+SB.ADOHelper.inputText(this.begdate.Value)+"' and  convert(smalldatetime,convert(varchar(10),HitData))<='"+SB.ADOHelper.inputText(this.enddate.Value)+"' group by HitHour";
				}
				try
				{
                    if (help.conn.State == ConnectionState.Closed)
                    { help.conn.Open(); }
					rd=cmd.ExecuteReader();
					while(rd.Read())
					{
						DataRow newrow=newtable.NewRow();
                        newrow["时间段"] = rd["HitHour"].ToString() + ":00";
						newrow["点击量"]=rd["Quantity"].ToString();
						newrow["弹窗数"]="0";
						newrow["PV量"]="0";
						newtable.Rows.Add(newrow);
					}
					rd.Close();
                    help.conn.Close();
				
				}
				catch
				{}
				this.Datagrid3.DataSource=new DataView(newtable);
				this.Datagrid3.DataBind();   
			}
				//help.conn.Close();
		}

		protected void CheckBox1_CheckedChanged(object sender, System.EventArgs e)
		{
			this.begdate.Value=DateTime.Now.ToShortDateString();
			this.enddate.Value=DateTime.Now.ToShortDateString();
			if(CheckBox1.Checked)
			{
				this.begdate.Disabled=true;
				this.enddate.Disabled=true;
			}
			else
			{
				this.begdate.Disabled=false;
				this.enddate.Disabled=false;
			}
		}

		private void Imagebutton3_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{   select=3;
			this.Datagrid2.DataSource=null;
			this.Datagrid2.DataBind();
			this.Datagrid3.DataSource=null;
			this.Datagrid3.DataBind();
			this.lb2.Text="请输入用户名";
			this.Label1.Text="查找某一个用户";
			this.pl2.Visible=false;
			this.pl3.Visible=true;
			this.pl4.Visible=false;
			this.pl5.Visible=true;
			this.pl6.Visible=true;	
		}
		private void dg3bind()
		{
			DataTable dtall=new DataTable();
			dtall.Columns.Add(new DataColumn("日期",typeof(string)));
			dtall.Columns.Add(new DataColumn("用户名",typeof(string)));
			dtall.Columns.Add(new DataColumn("网站名",typeof(string)));
			dtall.Columns.Add(new DataColumn("点击量",typeof(string)));
			dtall.Columns.Add(new DataColumn("弹窗数",typeof(string)));
			dtall.Columns.Add(new DataColumn("PV量",typeof(string)));
			
			if(this.TexBox1.Text.Trim()!="")
			{
				SB.ADOHelper help=new SB.ADOHelper();
				if(help.conn.State==ConnectionState.Closed)
				{
					help.conn.Open();
				}
				SqlCommand cmd=new SqlCommand();
				SqlDataReader rd=null;
                cmd.CommandText = "select count(*) as Quantity,convert(smalldatetime,convert(varchar(10),HitData)) as hitdate,Website.UserName,Website.WebsiteName from HistoryDetail,Website where Website.UserName='" + SB.ADOHelper.inputText(this.TexBox1.Text.Trim()) + "' and HistoryDetail.WebsiteId=Website.id  and convert(smalldatetime,convert(varchar(10),HitData))>='" + SB.ADOHelper.inputText(this.begdate.Value) + "' and  convert(smalldatetime,convert(varchar(10),HitData))<='" + SB.ADOHelper.inputText(this.enddate.Value) + "' group by  CONVERT(smalldatetime, CONVERT(varchar(10), HitData)),UserName,WebsiteName ";
				
				if(this.CheckBox1.Checked)
				{
                    cmd.CommandText = "select count(*) as Quantity,convert(smalldatetime,convert(varchar(10),HitData)) as hitdate,Website.UserName,Website.WebsiteName from ToDayDetail,Website where Website.UserName='" + SB.ADOHelper.inputText(this.TexBox1.Text.Trim()) + "' and ToDayDetail.WebsiteId=Website.id  and convert(smalldatetime,convert(varchar(10),HitData))>='" + SB.ADOHelper.inputText(this.begdate.Value) + "' and  convert(smalldatetime,convert(varchar(10),HitData))<='" + SB.ADOHelper.inputText(this.enddate.Value) + "' group by  CONVERT(smalldatetime, CONVERT(varchar(10), HitData)),UserName,WebsiteName ";
				}
				cmd.Connection=help.conn;
				try
				{
					rd=cmd.ExecuteReader();
					while(rd.Read())
					{
						DataRow row=dtall.NewRow();
						row["用户名"]=rd["UserName"].ToString();
						row["网站名"]=rd["WebsiteName"].ToString();
						row["点击量"]=rd["Quantity"].ToString();
						row["弹窗数"]="0";
						row["PV量"]="0";
						row["日期"]=Convert.ToDateTime(rd["hitdate"].ToString()).ToShortDateString();
						dtall.Rows.Add(row);
					}
					rd.Close();
				}
				catch
				{
					Response.Write("<script>alert('数据读取失败!或日期格式不正确!')</script>");return;
				}	
				this.Datagrid2.DataSource=new DataView(dtall);
				this.Datagrid2.DataBind();
				//
				//
				if(this.checkbox2.Checked)
				{
					DataTable newtable=new DataTable();
					newtable.Columns.Add(new DataColumn("时间段",typeof(string)));
					newtable.Columns.Add(new DataColumn("点击量",typeof(string)));
					newtable.Columns.Add(new DataColumn("弹窗数",typeof(string)));
					newtable.Columns.Add(new DataColumn("PV量",typeof(string)));
					cmd.CommandText="select count(*) as Quantity,HitHour from HistoryDetail where HistoryDetail.WebUserName='"+SB.ADOHelper.inputText(this.TexBox1.Text.Trim())+"' and convert(smalldatetime,convert(varchar(10),HitData))>='"+SB.ADOHelper.inputText(this.begdate.Value)+"' and  convert(smalldatetime,convert(varchar(10),HitData))<='"+SB.ADOHelper.inputText(this.enddate.Value)+"' group by HitHour";
					if(this.CheckBox1.Checked)
					{
						cmd.CommandText="select count(*) as Quantity,HitHour from ToDayDetail where ToDayDetail.WebUserName='"+SB.ADOHelper.inputText(this.TexBox1.Text.Trim())+"' and convert(smalldatetime,convert(varchar(10),HitData))>='"+SB.ADOHelper.inputText(this.begdate.Value)+"' and  convert(smalldatetime,convert(varchar(10),HitData))<='"+SB.ADOHelper.inputText(this.enddate.Value)+"' group by HitHour";
					}
					try
					{
						rd=cmd.ExecuteReader();
						while(rd.Read())
						{
							DataRow newrow=newtable.NewRow();
                            newrow["时间段"] = rd["HitHour"].ToString() + ":00";
							newrow["点击量"]=rd["Quantity"].ToString();
							newrow["弹窗数"]="0";
							newrow["PV量"]="0";
							newtable.Rows.Add(newrow);
						}
						rd.Close();
						
					}
					catch
					{}
					this.Datagrid3.DataSource=new DataView(newtable);
					this.Datagrid3.DataBind();

				}
				help.conn.Close();
				}
				else
				{
					Response.Write("<script>alert('请输入用户名!')</script>");
				}
			
		}

		protected void checkbox2_CheckedChanged(object sender, System.EventArgs e)
		{
			if(!this.checkbox2.Checked)
			{
				this.Datagrid3.DataSource=null;
				this.Datagrid3.DataBind();
			}
		}

		
	}
}
