using BLL;
using FineUIPro.Web.Controls;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using AspNet = System.Web.UI.WebControls;
namespace FineUIPro.Web.HJGL.WeldingReport
{
    public partial class WelderPerformance : PageBase
    {
        #region 加载
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Funs.DropDownPageSize(this.ddlPageSize);
                this.ddlPageSize.SelectedValue = this.Grid1.PageSize.ToString();
                this.cbSteType.DataTextField = "Text";
                this.cbSteType.DataValueField = "Value";
                this.cbSteType.DataSource = BLL.DropListService.HJGL_GetSteTypeList();
                this.cbSteType.DataBind();
                //单位
                // var unit = BLL.UnitService.GetUnitByUnitId(this.CurrUser.UnitId);
                //if (unit != null && unit.UnitTypeId == BLL.Const.ProjectUnitType_2)
                if (this.CurrUser.UnitId!=Const.UnitId_TCC&&this.CurrUser.UserId!=Const.hfnbdId)
                {
                    BLL.UnitService.InitSubUnitNameDownList(this.drpUnit, this.CurrUser.LoginProjectId, this.CurrUser.UnitId, true);
                    this.drpUnit.SelectedValue = this.CurrUser.UnitId;
                    BLL.WorkAreaService.InitWorkAreaProjectUnitDropDownList(this.drpWorkArea, this.CurrUser.LoginProjectId, this.CurrUser.UnitId, true);
                    BLL.WelderService.InitWelderListByUnit(this.ddlWloName, this.CurrUser.LoginProjectId, this.CurrUser.UnitId, true, "-请选择-");
                }
                else
                {
                    var supervisonUnit = BLL.WorkAreaService.IsSupervisor(this.CurrUser.UnitId, this.CurrUser.LoginProjectId);
                    if (supervisonUnit)
                    {
                        BLL.UnitService.InitSubUnitNameDownList(this.drpUnit, this.CurrUser.LoginProjectId, this.CurrUser.UnitId, true);
                    }
                    else
                    {
                        BLL.UnitService.InitUnitByProjectIdUnitTypeDropDownList(this.drpUnit, this.CurrUser.LoginProjectId, BLL.Const.ProjectUnitType_2, true);
                    }
                    Funs.FineUIPleaseSelect(this.drpWorkArea);
                    Funs.FineUIPleaseSelect(this.ddlWloName);
                }
                Funs.FineUIPleaseSelect(this.drpTeamGroup);
                //BindGrid();
            }
        }
        #endregion
        #region 绑定BindGrid
        /// 
        /// 
        /// 
        private DataTable tb = null;
        /// 
        /// 绑定数据
        /// 
        private void BindGrid()
        {
            List listStr = new List
            {
                new SqlParameter("@projectId", this.CurrUser.LoginProjectId)
            };
            if (this.drpUnit.SelectedValue != BLL.Const._Null&&!string.IsNullOrEmpty(this.drpUnit.SelectedValue))
            {
                listStr.Add(new SqlParameter("@unitcode", this.drpUnit.SelectedValue));
            }
            else
            {
                listStr.Add(new SqlParameter("@unitcode", null));
            }
            if (this.drpWorkArea.SelectedValue != BLL.Const._Null&&!string.IsNullOrEmpty(this.drpWorkArea.SelectedValue))
            {
                listStr.Add(new SqlParameter("@workareacode", this.drpWorkArea.SelectedValue));
            }
            else
            {
                listStr.Add(new SqlParameter("@workareacode", null));
            }
            
            
                listStr.Add(new SqlParameter("@steel", null));
            
            if (this.ddlWloName.SelectedValue!=BLL.Const._Null&&!string.IsNullOrEmpty(this.ddlWloName.SelectedValue))
            {
                listStr.Add(new SqlParameter("@wloName", this.ddlWloName.SelectedValue));
            }
            else
            {
                listStr.Add(new SqlParameter("@wloName", null));
            }
            if (!string.IsNullOrEmpty(this.txtStarTime.Text))
            {
                listStr.Add(new SqlParameter("@date1", this.txtStarTime.Text.Trim()));
            }
            else
            {
                listStr.Add(new SqlParameter("@date1", null));
            }
            if (!string.IsNullOrEmpty(this.txtEndTime.Text))
            {
                listStr.Add(new SqlParameter("@date2", this.txtEndTime.Text.Trim()));
            }
            else
            {
                listStr.Add(new SqlParameter("@date2", null));
            }
            if (BLL.WorkAreaService.IsSupervisor(this.CurrUser.UnitId, this.CurrUser.LoginProjectId))
            {
                listStr.Add(new SqlParameter("@supervisorUnitId", this.CurrUser.UnitId));
            }
            else
            {
                listStr.Add(new SqlParameter("@supervisorUnitId", null));
            }
            SqlParameter[] parameter = listStr.ToArray();
            tb = SQLHelper.GetDataTableRunProc("sp_rpt_WelderPerformance", parameter);
            if (this.drpSteType.Values!=null && this.drpSteType.Values.Length>0)
            {
             
                if (tb != null)
                {
                    List needReMoveRow = new List();
                    foreach (DataRow row in tb.Rows)
                    {
                        foreach (string stel in this.drpSteType.Values)
                        {
                            if (row["Steels"] != DBNull.Value && row["Steels"].ToString() != "" && !row["Steels"].ToString().Contains(stel))
                            {
                                needReMoveRow.Add(row);
                                break;
                            }
                        }
                    }
                    foreach (DataRow row in needReMoveRow)
                    {
                        tb.Rows.Remove(row);
                    }
                }
            }
            this.Grid1.RecordCount = tb.Rows.Count;
            var table = this.GetPagedDataTable(Grid1, tb);
            Grid1.DataSource = table;
            Grid1.DataBind();
        }
        /// 
        /// 改变索引事件
        /// 
        /// 
        /// 
        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            BindGrid();
        }
        /// 
        /// 分页下拉选择事件
        /// 
        /// 
        /// 
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
            BindGrid();
        }
        /// 
        /// 排序
        /// 
        /// 
        /// 
        protected void Grid1_Sort(object sender, FineUIPro.GridSortEventArgs e)
        {
            this.BindGrid();
        }
        #endregion
        #region 计算合计
        /// 
        /// 计算合计
        /// 
        protected void OutputSummaryData()
        {
            if (tb != null)
            {
                decimal? nowtotal_din = 0;//本期总达因值          
                int nowtotal_jot = 0;//本期总焊口
                int nowtotal_repairjot = 0;//本期总返口数
                int current_count_film = 0;//本期拍片焊口数
                int current_pass_film = 0;//本期拍片焊口合格数
                int nowtotalfilm = 0;//本期拍片总数
                int nowpassfilm = 0;//本期拍片合格数
                int nownotpassfilm = 0;//本期拍片不合格数
                decimal? totaldin = 0;//总达因值
                int total_jot = 0;//总焊口
                int total_repairjot = 0;//总返修口
                int totalfilm = 0;//总片数
                int passfilm = 0;//合格片数
                int notpassfilm = 0;//不合格数
                double tofd_total_donedin = 0;
                double tofd_pass_donedin = 0;
                double tofd_no_pass_donedin = 0;
                double tdfw_total_donedin = 0;
                double tdfw_pass_donedin = 0;
                double tdfw_no_pass_donedin = 0;
                double ut_total_donedin = 0;
                double ut_pass_donedin = 0;
                double ut_no_pass_donedin = 0;
                foreach (DataRow row in tb.Rows)
                {
                    nowtotal_din += Funs.GetNewDecimalOrZero(row["nowtotal_din"].ToString());
                    nowtotal_jot += Funs.GetNewIntOrZero(row["nowtotal_jot"].ToString());
                    nowtotal_repairjot += Funs.GetNewIntOrZero(row["nowtotal_repairjot"].ToString());
                    current_count_film += Funs.GetNewIntOrZero(row["current_count_film"].ToString());
                    current_pass_film += Funs.GetNewIntOrZero(row["current_pass_film"].ToString());
                    nowtotalfilm += Funs.GetNewIntOrZero(row["nowtotalfilm"].ToString());
                    nowpassfilm += Funs.GetNewIntOrZero(row["nowpassfilm"].ToString());
                    nownotpassfilm += Funs.GetNewIntOrZero(row["nownotpassfilm"].ToString());
                    totaldin += Funs.GetNewDecimalOrZero(row["totaldin"].ToString());
                    total_jot += Funs.GetNewIntOrZero(row["total_jot"].ToString());
                    total_repairjot += Funs.GetNewIntOrZero(row["total_repairjot"].ToString());
                    totalfilm += Funs.GetNewIntOrZero(row["totalfilm"].ToString());
                    passfilm += Funs.GetNewIntOrZero(row["passfilm"].ToString());
                    notpassfilm += Funs.GetNewIntOrZero(row["notpassfilm"].ToString());
                    tofd_total_donedin += Funs.GetNewDoubleOrZero(row["tofd_total_donedin"].ToString());
                    tofd_pass_donedin += Funs.GetNewDoubleOrZero(row["tofd_pass_donedin"].ToString());
                    tofd_no_pass_donedin += Funs.GetNewDoubleOrZero(row["tofd_no_pass_donedin"].ToString());
                    tdfw_total_donedin += Funs.GetNewDoubleOrZero(row["tdfw_total_donedin"].ToString());
                    tdfw_pass_donedin += Funs.GetNewDoubleOrZero(row["tdfw_no_pass_donedin"].ToString());
                    tdfw_no_pass_donedin += Funs.GetNewDoubleOrZero(row["tdfw_no_pass_donedin"].ToString());
                    ut_total_donedin += Funs.GetNewDoubleOrZero(row["ut_total_donedin"].ToString());
                    ut_pass_donedin += Funs.GetNewDoubleOrZero(row["ut_pass_donedin"].ToString());
                    ut_no_pass_donedin += Funs.GetNewDoubleOrZero(row["ut_no_pass_donedin"].ToString());
                }
                JObject summary = new JObject
            {
                { "tfNumber", "合计" },
                { "nowtotal_din", nowtotal_din },
                { "nowtotal_jot", nowtotal_jot },
                { "nowtotal_repairjot", nowtotal_repairjot },
                { "current_count_film", current_count_film },
                { "current_pass_film", current_pass_film },
                { "nowtotalfilm", nowtotalfilm },
                { "nowpassfilm", nowpassfilm },
                { "nownotpassfilm", nownotpassfilm },
                { "totaldin", totaldin },
                { "total_jot", total_jot },
                { "total_repairjot", total_repairjot },
                { "totalfilm", totalfilm },
                { "passfilm", passfilm },
                { "notpassfilm", notpassfilm },
                { "tofd_total_donedin", tofd_total_donedin },
                { "tofd_pass_donedin", tofd_pass_donedin },
                { "tofd_no_pass_donedin", tofd_no_pass_donedin },
                { "tofdpassrate", tofd_pass_donedin >0? (100*(tofd_pass_donedin-tofd_no_pass_donedin)/tofd_pass_donedin).ToString("0.000")+"%":""},
                { "tdfw_total_donedin", tdfw_total_donedin },
                { "tdfw_pass_donedin", tdfw_pass_donedin },
                { "tdfw_no_pass_donedin", tdfw_no_pass_donedin },
                { "tdfwpassrate", tdfw_pass_donedin >0? (100*(tdfw_pass_donedin-tdfw_no_pass_donedin)/tdfw_pass_donedin).ToString("0.000")+"%":""},
                { "ut_total_donedin", ut_total_donedin },
                { "ut_pass_donedin", ut_pass_donedin },
                { "ut_no_pass_donedin", ut_no_pass_donedin },
                { "utpassrate", ut_pass_donedin >0? (100*(ut_pass_donedin-ut_no_pass_donedin)/ut_pass_donedin).ToString("0.000")+"%":""}
            };
                Grid1.SummaryData = summary;
            }
        }
        #endregion        
        #region 统计按钮事件
        /// 
        /// 统计
        /// 
        /// 
        /// 
        protected void BtnAnalyse_Click(object sender, EventArgs e)
        {
            BindGrid();
            OutputSummaryData();
        }
        #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;
            Response.Write(GetGridTableHtml(Grid1));
            Response.End();
        }
        /// 
        /// 导出方法
        /// 
        /// 
        /// 
        private string GetGridTableHtml(Grid grid)
        {
            StringBuilder sb = new StringBuilder();
            MultiHeaderTable mht = new MultiHeaderTable();
            mht.ResolveMultiHeaderTable(Grid1.Columns);
            sb.Append("");
            sb.Append("");
            foreach (List
");
            return sb.ToString();
        }
           #endregion
        #region DropDownList下拉选择事件
        /// 
        /// 单位下拉选择事件
        /// 
        /// 
        /// 
        protected void drpUnit_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.drpWorkArea.Items.Clear();
            this.drpTeamGroup.Items.Clear();
            if (this.drpUnit.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpUnit.SelectedValue))
            {
                BLL.WorkAreaService.InitWorkAreaProjectUnitDropDownList(this.drpWorkArea, this.CurrUser.LoginProjectId, this.drpUnit.SelectedValue, true);
                this.drpWorkArea.SelectedIndex = 0;
                BLL.TeamGroupService.InitTeamGroupProjectUnitDropDownList(this.drpTeamGroup, this.CurrUser.LoginProjectId, this.drpUnit.SelectedValue, true);
                this.drpTeamGroup.SelectedIndex = 0;
            }
        }
        /// 
        /// 班组下拉选择事件
        /// 
        /// 
        /// 
        protected void drpTeamGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ddlWloName.Items.Clear();
            if (this.drpTeamGroup.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpTeamGroup.SelectedValue))
            {
                BLL.WelderService.InitWelderListByTeamGroupId(this.ddlWloName, this.CurrUser.LoginProjectId, this.drpTeamGroup.SelectedValue, true, "-请选择-");
                this.ddlWloName.SelectedIndex = 0;
            }
        }
        #endregion
    }
}