using BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FineUIPro.Web.CQMS.ProcessControl
{
    public partial class InspectionManagementStatistics :PageBase
    {
        public DateTime StartDate;
        public DateTime NextDate;
        public DateTime NewDate;

        public int SunNumber;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BLL.CNProfessionalService.InitCNProfessionalDownList(this.drpCNProfessional, true);//专业
                BindGvInspectionManagement(null);
            }
        }
        #region 绑定GridView
        /// <summary>
        /// 绑定
        /// </summary>
        /// <param name="cNProfessionalId"></param>
        public void BindGvInspectionManagement(string cNProfessionalId)
        {
            List<Model.InspectionManagementStatistics> StatisticsList = new List<Model.InspectionManagementStatistics>();
            Model.Base_Project project = BLL.ProjectService.GetProjectByProjectId(this.CurrUser.LoginProjectId);
            StartDate = Convert.ToDateTime(project.StartDate);
            for (int i = 0; i < i + 1; i++)
            {
                Model.InspectionManagementStatistics Statistics = new Model.InspectionManagementStatistics();
                if (i == 0)
                {
                    NextDate = Convert.ToDateTime(DateTime.Parse(StartDate.ToString("yyyy-MM-dd")).AddMonths(1).ToShortDateString());
                    NewDate = Convert.ToDateTime(NextDate.Year + "-" + NextDate.Month + "-25");
                    Statistics.CheckDate = string.Format("{0:yyyy-MM-dd}", StartDate) + " 至 ";
                    if (StartDate.Day < 25)
                    {
                        if (DateTime.Now < NewDate)
                        {
                            //统计所给事件段的全部数量
                            List<Model.View_CQMS_InspectionManagementDetail> managementListSunNumber = BLL.InspectionManagementService.getInspectionManagementDetailListByCNProfessionalIdAndDate(this.CurrUser.LoginProjectId, cNProfessionalId, StartDate, DateTime.Now, false);
                            //统计所给事件段的合格数量
                            List<Model.View_CQMS_InspectionManagementDetail> managementListOneNumber = BLL.InspectionManagementService.getInspectionManagementDetailListByCNProfessionalIdAndDate(this.CurrUser.LoginProjectId, cNProfessionalId, StartDate, DateTime.Now, true);

                            Statistics.CheckDate += string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                            Statistics.SunNumber = managementListSunNumber.Count();
                            Statistics.OneStatisticsSunNumber = managementListOneNumber.Count();
                            if (managementListSunNumber.Count() != 0)//被除数不能为零
                            {
                                Statistics.OneStatistics = Math.Round((double)managementListOneNumber.Count() / (double)managementListSunNumber.Count() * 100, 2) + "%";//保留两位小数、后四舍五入
                            }
                            else
                            {
                                Statistics.OneStatistics = "0%";
                            }
                            StatisticsList.Add(Statistics);
                            break;
                        }
                        else
                        {
                            NextDate = Convert.ToDateTime(StartDate.Year + "-" + StartDate.Month + "-25");
                            //统计所给事件段的全部数量
                            List<Model.View_CQMS_InspectionManagementDetail> managementListSunNumber = BLL.InspectionManagementService.getInspectionManagementDetailListByCNProfessionalIdAndDate(this.CurrUser.LoginProjectId, cNProfessionalId, StartDate, NewDate, false);
                            //统计所给事件段的合格数量
                            List<Model.View_CQMS_InspectionManagementDetail> managementListOneNumber = BLL.InspectionManagementService.getInspectionManagementDetailListByCNProfessionalIdAndDate(this.CurrUser.LoginProjectId, cNProfessionalId, StartDate, NewDate, true);

                            Statistics.CheckDate += string.Format("{0:yyyy-MM-dd}", NextDate);
                            Statistics.SunNumber = managementListSunNumber.Count();
                            Statistics.OneStatisticsSunNumber = managementListOneNumber.Count();
                            if (managementListSunNumber.Count() != 0)//被除数不能为零
                            {
                                Statistics.OneStatistics = Math.Round((double)managementListOneNumber.Count() / (double)managementListSunNumber.Count() * 100, 2) + "%";//保留两位小数、后四舍五入
                            }
                            else
                            {
                                Statistics.OneStatistics = "0%";
                            }
                        }
                    }
                    else
                    {
                        if (DateTime.Now < NewDate)
                        {
                            //统计所给事件段的全部数量
                            List<Model.View_CQMS_InspectionManagementDetail> managementListSunNumber = BLL.InspectionManagementService.getInspectionManagementDetailListByCNProfessionalIdAndDate(this.CurrUser.LoginProjectId, cNProfessionalId, StartDate, DateTime.Now, false);
                            //统计所给事件段的合格数量
                            List<Model.View_CQMS_InspectionManagementDetail> managementListOneNumber = BLL.InspectionManagementService.getInspectionManagementDetailListByCNProfessionalIdAndDate(this.CurrUser.LoginProjectId, cNProfessionalId, StartDate, DateTime.Now, true);

                            Statistics.CheckDate += string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                            Statistics.SunNumber = managementListSunNumber.Count();
                            Statistics.OneStatisticsSunNumber = managementListOneNumber.Count();
                            if (managementListSunNumber.Count() != 0)//被除数不能为零
                            {
                                Statistics.OneStatistics = Math.Round((double)managementListOneNumber.Count() / (double)managementListSunNumber.Count() * 100, 2) + "%";//保留两位小数、后四舍五入
                            }
                            else
                            {
                                Statistics.OneStatistics = "0%";
                            }

                            StatisticsList.Add(Statistics);
                            break;
                        }
                        else
                        {
                            //统计所给事件段的全部数量
                            List<Model.View_CQMS_InspectionManagementDetail> managementListSunNumber = BLL.InspectionManagementService.getInspectionManagementDetailListByCNProfessionalIdAndDate(this.CurrUser.LoginProjectId, cNProfessionalId, StartDate, NewDate, false);
                            //统计所给事件段的合格数量
                            List<Model.View_CQMS_InspectionManagementDetail> managementListOneNumber = BLL.InspectionManagementService.getInspectionManagementDetailListByCNProfessionalIdAndDate(this.CurrUser.LoginProjectId, cNProfessionalId, StartDate, NewDate, true);
                            Statistics.CheckDate += string.Format("{0:yyyy-MM-dd}", NewDate);
                            Statistics.SunNumber = managementListSunNumber.Count();
                            Statistics.OneStatisticsSunNumber = managementListOneNumber.Count();
                            if (managementListSunNumber.Count() != 0)//被除数不能为零
                            {
                                Statistics.OneStatistics = Math.Round((double)managementListOneNumber.Count() / (double)managementListSunNumber.Count() * 100, 2) + "%";//保留两位小数、后四舍五入
                            }
                            else
                            {
                                Statistics.OneStatistics = "0%";
                            }
                        }
                    }
                }
                else
                {

                    if (StartDate.Day > 25)
                    {
                        Statistics.CheckDate = NewDate.Year + "-" + NewDate.Month + "-" + (NewDate.Day + 1) + " 至 ";
                        StartDate = Convert.ToDateTime(NewDate.Year + "-" + NewDate.Month + "-" + (NewDate.Day + 1));//获取上一记录的结束日期加一天为本次记录的开始日期
                    }
                    else
                    {
                        Statistics.CheckDate = NextDate.Year + "-" + NextDate.Month + "-" + (NextDate.Day + 1) + " 至 ";
                        StartDate = Convert.ToDateTime(NextDate.Year + "-" + NextDate.Month + "-" + (NextDate.Day + 1));//获取上一记录的结束日期加一天为本次记录的开始日期
                    }

                    NextDate = Convert.ToDateTime(DateTime.Parse(NextDate.ToString("yyyy-MM-dd")).AddMonths(1).ToShortDateString());
                    NewDate = Convert.ToDateTime(NextDate.Year + "-" + NextDate.Month + "-25");
                    if (DateTime.Now < NewDate)
                    {
                        //统计所给事件段的全部数量
                        List<Model.View_CQMS_InspectionManagementDetail> managementListSunNumber = BLL.InspectionManagementService.getInspectionManagementDetailListByCNProfessionalIdAndDate(this.CurrUser.LoginProjectId, cNProfessionalId, StartDate, DateTime.Now, false);
                        //统计所给事件段的合格数量
                        List<Model.View_CQMS_InspectionManagementDetail> managementListOneNumber = BLL.InspectionManagementService.getInspectionManagementDetailListByCNProfessionalIdAndDate(this.CurrUser.LoginProjectId, cNProfessionalId, StartDate, DateTime.Now, true);

                        Statistics.CheckDate += string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                        Statistics.SunNumber = managementListSunNumber.Count();
                        Statistics.OneStatisticsSunNumber = managementListOneNumber.Count();
                        if (managementListSunNumber.Count() != 0)//被除数不能为零
                        {
                            Statistics.OneStatistics = Math.Round((double)managementListOneNumber.Count() / (double)managementListSunNumber.Count() * 100, 2) + "%";//保留两位小数、后四舍五入
                        }
                        else
                        {
                            Statistics.OneStatistics = "0%";
                        }

                        StatisticsList.Add(Statistics);
                        break;
                    }
                    else
                    {
                        //统计所给事件段的全部数量
                        List<Model.View_CQMS_InspectionManagementDetail> managementListSunNumber = BLL.InspectionManagementService.getInspectionManagementDetailListByCNProfessionalIdAndDate(this.CurrUser.LoginProjectId, cNProfessionalId, StartDate, NewDate, false);
                        //统计所给事件段的合格数量
                        List<Model.View_CQMS_InspectionManagementDetail> managementListOneNumber = BLL.InspectionManagementService.getInspectionManagementDetailListByCNProfessionalIdAndDate(this.CurrUser.LoginProjectId, cNProfessionalId, StartDate, NewDate, true);
                        Statistics.CheckDate += string.Format("{0:yyyy-MM-dd}", NewDate);
                        Statistics.SunNumber = managementListSunNumber.Count();
                        Statistics.OneStatisticsSunNumber = managementListOneNumber.Count();
                        if (managementListSunNumber.Count() != 0)//被除数不能为零
                        {
                            Statistics.OneStatistics = Math.Round((double)managementListOneNumber.Count() / (double)managementListSunNumber.Count() * 100, 2) + "%";//保留两位小数、后四舍五入
                        }
                        else
                        {
                            Statistics.OneStatistics = "0%";
                        }
                    }
                }
                StatisticsList.Add(Statistics);
            }
            Model.InspectionManagementStatistics StatisticsLast = new Model.InspectionManagementStatistics();
            StatisticsLast.CheckDate = "合计";
            int sum1 = 0;
            int sum2 = 0;
            foreach (Model.InspectionManagementStatistics item in StatisticsList)
            {
                sum1 += item.OneStatisticsSunNumber;
                sum2 += item.SunNumber;

            }
            StatisticsLast.OneStatisticsSunNumber = sum1;
            StatisticsLast.SunNumber = sum2;
            if (sum2 != 0)//被除数不能为零
            {
                StatisticsLast.OneStatistics = Math.Round((double)sum1 / (double)sum2 * 100, 2) + "%";//保留两位小数、后四舍五入
            }
            else
            {
                StatisticsLast.OneStatistics = "0%";
            }
            StatisticsList.Add(StatisticsLast);
            this.Grid1.DataSource = StatisticsList;
            this.Grid1.DataBind();
        }
        #endregion

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (this.drpCNProfessional.SelectedValue !=BLL.Const._Null)
            {
                BindGvInspectionManagement(this.drpCNProfessional.SelectedValue);
            }
            else
            {
                BindGvInspectionManagement(null);
            }
        }


        protected void btnOut_Click(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;
            string filename = dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Second.ToString();

            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "GB2312";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.Write("<meta http-equiv=Content-Type content=text/html;charset=UTF-8>");

            Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode("控制点检查检测合格率统计" + filename, System.Text.Encoding.UTF8) + ".xls");
            Response.ContentType = "application/ms-excel";
            this.EnableViewState = false;
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            this.Grid1.RenderControl(oHtmlTextWriter);
            Response.Write(oStringWriter.ToString());
            Response.Flush();
            Response.End();
        }
    }
}