using BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace FineUIPro.Web.DataShowLocal
{
    public partial class NCRStatiscItem : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string projectId = Request.Params["projectId"];
                Model.Base_Project project = BLL.ProjectService.GetProjectByProjectId(projectId);
                this.txtStartDate.Text = "2015-01-01";
                this.txtEndDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                if (project != null)
                {
                    if (project.StartDate != null)
                    {
                        this.txtStartDate.Text = string.Format("{0:yyyy-MM-dd}", project.StartDate);
                    }
                }
                BindStatisc(projectId);
            }
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtStartDate.Text.Trim()) || string.IsNullOrEmpty(this.txtEndDate.Text.Trim()))
            {
                Alert.ShowInTop("开始和结束日期不能为空!", MessageBoxIcon.Warning);
                return;
            }
            if (Convert.ToDateTime(this.txtStartDate.Text.Trim()) >= Convert.ToDateTime(this.txtEndDate.Text.Trim()))
            {
                Alert.ShowInTop("开始日期不能大于结束日期!", MessageBoxIcon.Warning);
                return;
            }
            string projectId = Request.Params["projectId"];
            BindStatisc(projectId);
        }
        #region 绑定GridView
        /// 
        /// 绑定
        /// 
        /// 
        /// 
        public void BindStatisc(string projectid)
        {
            DateTime startDate = Convert.ToDateTime(this.txtStartDate.Text.Trim());
            DateTime endDate = Convert.ToDateTime(this.txtEndDate.Text.Trim());
            //DateTime projectStartDate = Convert.ToDateTime("2015-01-01");
            List StatisticsList = new List();
            //Model.Base_Project project = BLL.ProjectService.getProjectByProjectId(this.CurrUser.LoginProjectId);
            //if (project != null)
            //{
            //    if (project.StartDate != null)
            //    {
            //        projectStartDate = Convert.ToDateTime(project.StartDate);
            //    }
            //}
            int i = 1;
            if (this.ckType.SelectedValue == "1")  //按单位
            {
                //所有集合
                List totalManagementList = BLL.NCRManagementService.GetNCRManagementListByDate(projectid, startDate, endDate);
                List unitNames = (from x in totalManagementList select x.ReceiveUnit).Distinct().ToList();
                foreach (var item in unitNames)
                {
                    //当期集合
                    List managementList = BLL.NCRManagementService.GetNCRManagementListByUnitNameAndDate(projectid, item, startDate, endDate);
                    Model.NCRStatisc NCRStatisc = new Model.NCRStatisc();
                    NCRStatisc.Num = i;
                    NCRStatisc.WorkName = BLL.UnitService.getUnitNamesUnitIds(item);
                    NCRStatisc.CheckNum = managementList.Count();
                    NCRStatisc.OKNum = managementList.Count(x => x.Status == "3");
                    NCRStatisc.NotOKNum = managementList.Count(x => x.Status != "3");
                    NCRStatisc.Remark = "";
                    if (NCRStatisc.CheckNum != 0)//被除数不能为零
                    {
                        NCRStatisc.OKRate = Math.Round((double)NCRStatisc.OKNum / (double)NCRStatisc.CheckNum * 100, 2) + "%";//保留两位小数、后四舍五入
                    }
                    else
                    {
                        NCRStatisc.OKRate = "0%";
                    }
                    StatisticsList.Add(NCRStatisc);
                    i++;
                }
            }
            else   //按专业
            {
                var cNProfessionals = from x in Funs.DB.Base_CNProfessional where x.CNProfessionalId != Const.CNProfessionalConstructId orderby x.SortIndex select x;
                foreach (var item in cNProfessionals)
                {
                    //所有集合
                    List managementList = BLL.NCRManagementService.GetNCRManagementListByCNProfessionalIdAndDate(projectid, item.CNProfessionalId, startDate, endDate);
                    Model.NCRStatisc NCRStatisc = new Model.NCRStatisc();
                    NCRStatisc.Num = i;
                    NCRStatisc.WorkName = item.ProfessionalName;
                    NCRStatisc.CheckNum = managementList.Count();
                    NCRStatisc.OKNum = managementList.Count(x => x.Status == "3");
                    NCRStatisc.NotOKNum = managementList.Count(x => x.Status != "3");
                    NCRStatisc.Remark = "";
                    if (NCRStatisc.CheckNum != 0)//被除数不能为零
                    {
                        NCRStatisc.OKRate = Math.Round((double)NCRStatisc.OKNum / (double)NCRStatisc.CheckNum * 100, 2) + "%";//保留两位小数、后四舍五入
                    }
                    else
                    {
                        NCRStatisc.OKRate = "0%";
                    }
                    StatisticsList.Add(NCRStatisc);
                    i++;
                }
            }
            if (StatisticsList.Count() > 0)   //增加总计
            {
                Model.NCRStatisc StatisticsLast = new Model.NCRStatisc();
                StatisticsLast.Num = StatisticsList.Count() + 1;
                StatisticsLast.WorkName = "合计";
                StatisticsLast.Remark = "";
                foreach (Model.NCRStatisc item in StatisticsList)
                {
                    StatisticsLast.CheckNum += item.CheckNum;
                    StatisticsLast.OKNum += item.OKNum;
                    StatisticsLast.NotOKNum += item.NotOKNum;
                }
                if (StatisticsLast.CheckNum != 0)//被除数不能为零
                {
                    StatisticsLast.OKRate = Math.Round((double)StatisticsLast.OKNum / (double)StatisticsLast.CheckNum * 100, 2) + "%";//保留两位小数、后四舍五入
                }
                else
                {
                    StatisticsLast.OKRate = "0%";
                }
                StatisticsList.Add(StatisticsLast);
            }
            this.Grid1.DataSource = StatisticsList;
            this.Grid1.DataBind();
        }
        #endregion
        protected void ckType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.ckType.SelectedValue == "1")
            {
                this.Grid1.Columns[1].HeaderText = "接收单位";
            }
            else
            {
                this.Grid1.Columns[1].HeaderText = "专业";
            }
            BindStatisc(Request.Params["projectId"]);
        }
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindStatisc(Request.Params["projectId"]);
        }
    }
}