using BLL;
using FineUIPro.Web.Controls;
using System;
using System.Data;
using System.Text;
using AspNet = System.Web.UI.WebControls;
namespace FineUIPro.Web.ZHGL.DataStatistics
{
    public partial class ProjectPersonStatics : PageBase
    {
       
        #region 加载页面
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
        }
        /// 
        /// 绑定数据
        /// 
        private void BindGrid()
        {
            string sql = @"select newid()id, * ,a.PersonNum1+a.PersonNum2  TotalNum from (
                        select case when a.IsForeign = 1 then '境外项目'else '境内项目' end IsForeign, count(distinct a.ProjectId) ProNum,count(distinct case when  b.UnitType =1  then c.IdentityCard else null end) PersonNum1,count(distinct case when  b.UnitType  !=1  or  b.UnitType is null  then c.IdentityCard else null end)PersonNum2 from Base_Project a 
                        left join Project_ProjectUnit b on a.ProjectId = b.ProjectId
                        left join  SitePerson_Person c  on a.ProjectId = c.ProjectId and b.UnitId = c.UnitId
                        where ProjectState =1 and (ProjectAttribute is null or ProjectAttribute ='GONGCHENG')   
                        group by  a.IsForeign) a  ";
          
           var tb1 = SQLHelper.GetDataTableRunText(sql, null);
          
            
            Grid1.DataSource = tb1;
            
            Grid1.DataBind();
        }
        #endregion
        #region 查询
        /// 
        /// 查询
        /// 
        /// 
        /// 
        protected void TextBox_TextChanged(object sender, EventArgs e)
        {
            this.BindGrid();
        }
        #endregion
         
        #region 导出按钮
        /// 导出按钮
        /// 
        /// 
        /// 
        protected void btnOut_Click(object sender, EventArgs e)
        {
            Response.ClearContent();
            string filename = Funs.GetNewFileName();
            Response.AddHeader("content-disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode("项目现场人员管理" + filename, System.Text.Encoding.UTF8) + ".xls");
            Response.ContentType = "application/excel";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            this.Grid1.PageSize = this.Grid1.RecordCount;
            BindGrid();
            Response.Write(GetGridTableHtml(Grid1));
            Response.End();
        }
        /// 
        /// 导出方法
        /// 
        /// 
        /// 
        private string GetGridTableHtml(Grid grid)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("");
            sb.Append("
");
            sb.Append("");
            foreach (GridColumn column in grid.Columns)
            {
                sb.AppendFormat("| {0} | ", column.HeaderText);
            }
            sb.Append("
");
            foreach (GridRow row in grid.Rows)
            {
                sb.Append("");
                foreach (GridColumn column in grid.Columns)
                {
                    string html = row.Values[column.ColumnIndex].ToString();
                    if (column.ColumnID == "tfPageIndex")
                    {
                        html = (row.FindControl("lblPageIndex") as AspNet.Label).Text;
                    }
                    sb.AppendFormat("| {0} | ", html);
                }
                sb.Append("
");
            }
            sb.Append("
");
            return sb.ToString();
        }
        #endregion
         
    }
}