using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using BLL;
using AspNet = System.Web.UI.WebControls;
namespace FineUIPro.Web.ZHGL.ManagementReport
{
    public partial class ReportRemind : PageBase
    {
        #region 定义项
        /// 
        /// 清单主键
        /// 
        public string ReportRemindId
        {
            get
            {
                return (string)ViewState["ReportRemindId"];
            }
            set
            {
                ViewState["ReportRemindId"] = value;
            }
        }
        #endregion
        #region 加载页面
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ////权限按钮方法
                this.GetButtonPower();
                BLL.ConstValue.InitConstValueDropDownList(this.drpMonth, ConstValue.Group_0009, true);
                BLL.ConstValue.InitConstValueDropDownList(this.drpYear, ConstValue.Group_0008, true);
                BLL.ConstValue.InitConstValueDropDownList(this.drpQuarter, ConstValue.Group_0011, true);
                List projects = (from x in Funs.DB.Base_Project
                                                     where x.ProjectState == BLL.Const.ProjectState_1 || x.ProjectState == null
                                                     select x).ToList();
                drpProject.DataValueField = "ProjectId";
                drpProject.DataTextField = "ProjectName";
                var projectlist = projects;
                drpProject.DataSource = projectlist;
                drpProject.DataBind();
                Funs.FineUIPleaseSelect(drpProject);
                ddlPageSize.SelectedValue = Grid1.PageSize.ToString();
                LoadData();
                // 绑定表格
                BindGrid();
            }
        }
        /// 
        /// 生成数据
        /// 
        private void LoadData()
        {
            DateTime now = DateTime.Now.Date;
            List projects = (from x in Funs.DB.Base_Project
                                                 where x.ProjectState == BLL.Const.ProjectState_1 || x.ProjectState == null
                                                 select x).ToList();
            foreach (var project in projects)
            {
                if (project.StartDate <= now)  //已开工
                {
                    //处理月报
                    DateTime date = Convert.ToDateTime(now.Year.ToString() + "-" + now.Month.ToString() + "-01").AddDays(-1);  //上月最后一天 
                    if (project.StartDate <= date)  //上月已开工
                    {
                        DateTime month = Convert.ToDateTime(now.Year.ToString() + "-" + now.Month.ToString() + "-01").AddMonths(-1);
                        Model.Manager_MonthReportB monthReport = BLL.MonthReportBService.GetMonthReportByMonth(month, project.ProjectId);
                        Model.Manager_MonthReportC monthReportC = BLL.MonthReportCService.GetMonthReportByMonths(month, project.ProjectId);
                        Model.Manager_MonthReportD monthReportD = BLL.MonthReportDService.GetMonthReportByMonths(month, project.ProjectId);
                        if (monthReport == null && monthReportC == null && monthReportD == null && !BLL.MonthReportService.GetMonthReportByDate(month, project.ProjectId))
                        {
                            Model.ManagementReport_ReportRemind oldReportRemind = (from x in Funs.DB.ManagementReport_ReportRemind
                                                                                   where x.ProjectId == project.ProjectId && x.Months == month && x.ReportName == "管理月报"
                                                                                   select x).FirstOrDefault();
                            if (oldReportRemind == null)
                            {
                                Model.ManagementReport_ReportRemind reportRemind = new Model.ManagementReport_ReportRemind
                                {
                                    ProjectId = project.ProjectId,
                                    Months = month,
                                    ReportName = "管理月报",
                                    CompileDate = now
                                };
                                BLL.ReportRemindService.AddReportRemind(reportRemind);
                            }
                        }
                        Model.InformationProject_MillionsMonthlyReport millionsMonthlyReport = Funs.DB.InformationProject_MillionsMonthlyReport.FirstOrDefault(x => x.ProjectId == project.ProjectId
                            && x.Year == month.Year && x.Month == month.Month && x.States == BLL.Const.State_2);
                        if (millionsMonthlyReport == null)
                        {
                            Model.ManagementReport_ReportRemind oldReportRemind = (from x in Funs.DB.ManagementReport_ReportRemind
                                                                                   where x.ProjectId == project.ProjectId && x.Year == month.Year && x.Month == month.Month && x.ReportName == "百万工时安全统计月报"
                                                                                   select x).FirstOrDefault();
                            if (oldReportRemind == null)
                            {
                                Model.ManagementReport_ReportRemind reportRemind = new Model.ManagementReport_ReportRemind
                                {
                                    ProjectId = project.ProjectId,
                                    Year = month.Year,
                                    Month = month.Month,
                                    ReportName = "百万工时安全统计月报",
                                    CompileDate = now
                                };
                                BLL.ReportRemindService.AddReportRemind(reportRemind);
                            }
                        }
                        Model.InformationProject_AccidentCauseReport accidentCauseReport = Funs.DB.InformationProject_AccidentCauseReport.FirstOrDefault(x => x.ProjectId == project.ProjectId && x.Year == month.Year && x.Month == month.Month);
                        if (accidentCauseReport == null)
                        {
                            Model.ManagementReport_ReportRemind oldReportRemind = (from x in Funs.DB.ManagementReport_ReportRemind
                                                                                   where x.ProjectId == project.ProjectId && x.Year == month.Year && x.Month == month.Month && x.ReportName == "职工伤亡事故原因分析报"
                                                                                   select x).FirstOrDefault();
                            if (oldReportRemind == null)
                            {
                                Model.ManagementReport_ReportRemind reportRemind = new Model.ManagementReport_ReportRemind
                                {
                                    ProjectId = project.ProjectId,
                                    Year = month.Year,
                                    Month = month.Month,
                                    ReportName = "职工伤亡事故原因分析报",
                                    CompileDate = now
                                };
                                BLL.ReportRemindService.AddReportRemind(reportRemind);
                            }
                        }
                        if (project.IsForeign == true)   //海外项目
                        {
                            Model.Manager_MonthReportE monthReportE = Funs.DB.Manager_MonthReportE.FirstOrDefault(x => x.ProjectId == project.ProjectId && x.Months.Value.Year == month.Year && x.Months.Value.Month == month.Month);
                            if (monthReportE == null)
                            {
                                Model.ManagementReport_ReportRemind oldReportRemind = (from x in Funs.DB.ManagementReport_ReportRemind
                                                                                       where x.ProjectId == project.ProjectId && x.Year == month.Year && x.Month == month.Month && x.ReportName == "海外工程项目月度HSSE统计表"
                                                                                       select x).FirstOrDefault();
                                if (oldReportRemind == null)
                                {
                                    Model.ManagementReport_ReportRemind reportRemind = new Model.ManagementReport_ReportRemind
                                    {
                                        ProjectId = project.ProjectId,
                                        Months = month,
                                        Year = month.Year,
                                        Month = month.Month,
                                        ReportName = "海外工程项目月度HSSE统计表",
                                        CompileDate = now
                                    };
                                    BLL.ReportRemindService.AddReportRemind(reportRemind);
                                }
                            }
                        }
                        //处理季报
                        DateTime showDate = DateTime.Now.AddMonths(-3);
                        int year = showDate.Year;
                        int quarter = Funs.GetNowQuarterlyByTime(showDate);
                        Model.InformationProject_SafetyQuarterlyReport safetyQuarterlyReport = Funs.DB.InformationProject_SafetyQuarterlyReport.FirstOrDefault(x => x.ProjectId == project.ProjectId && x.YearId == year && x.Quarters == quarter);
                        if (safetyQuarterlyReport == null)
                        {
                            //删除未上报月报信息
                            Model.ManagementReport_ReportRemind oldReportRemind = (from x in Funs.DB.ManagementReport_ReportRemind
                                                                                   where x.ProjectId == project.ProjectId && x.Year == year && x.Quarterly == quarter && x.ReportName == "安全生产数据季报"
                                                                                   select x).FirstOrDefault();
                            if (oldReportRemind == null)
                            {
                                Model.ManagementReport_ReportRemind reportRemind = new Model.ManagementReport_ReportRemind
                                {
                                    ProjectId = project.ProjectId,
                                    Year = year,
                                    Quarterly = quarter,
                                    ReportName = "安全生产数据季报",
                                    CompileDate = now
                                };
                                BLL.ReportRemindService.AddReportRemind(reportRemind);
                            }
                        }
                        Model.InformationProject_DrillConductedQuarterlyReport drillConductedQuarterlyReport = Funs.DB.InformationProject_DrillConductedQuarterlyReport.FirstOrDefault(x => x.ProjectId == project.ProjectId && x.YearId == year && x.Quarter == quarter);
                        if (drillConductedQuarterlyReport == null)
                        {
                            //删除未上报月报信息
                            Model.ManagementReport_ReportRemind oldReportRemind = (from x in Funs.DB.ManagementReport_ReportRemind
                                                                                   where x.ProjectId == project.ProjectId && x.Year == year && x.Quarterly == quarter && x.ReportName == "应急演练开展情况季报"
                                                                                   select x).FirstOrDefault();
                            if (oldReportRemind == null)
                            {
                                Model.ManagementReport_ReportRemind reportRemind = new Model.ManagementReport_ReportRemind
                                {
                                    ProjectId = project.ProjectId,
                                    Year = year,
                                    Quarterly = quarter,
                                    ReportName = "应急演练开展情况季报",
                                    CompileDate = now
                                };
                                BLL.ReportRemindService.AddReportRemind(reportRemind);
                            }
                        }
                        //处理半年报
                        int halfYear = Funs.GetNowHalfYearByTime(now);
                        Model.InformationProject_DrillPlanHalfYearReport drillPlanHalfYearReport = Funs.DB.InformationProject_DrillPlanHalfYearReport.FirstOrDefault(x => x.ProjectId == project.ProjectId && x.YearId == year && x.HalfYearId == halfYear);
                        if (drillPlanHalfYearReport == null)
                        {
                            //删除未上报月报信息
                            Model.ManagementReport_ReportRemind oldReportRemind = (from x in Funs.DB.ManagementReport_ReportRemind
                                                                                   where x.ProjectId == project.ProjectId && x.Year == year && x.HalfYear == halfYear && x.ReportName == "应急演练工作计划半年报"
                                                                                   select x).FirstOrDefault();
                            if (oldReportRemind == null)
                            {
                                Model.ManagementReport_ReportRemind reportRemind = new Model.ManagementReport_ReportRemind
                                {
                                    ProjectId = project.ProjectId,
                                    Year = year,
                                    HalfYear = halfYear,
                                    ReportName = "应急演练工作计划半年报",
                                    CompileDate = now
                                };
                                BLL.ReportRemindService.AddReportRemind(reportRemind);
                            }
                        }
                    }
                }
            }
        }
        #region 转换字符串
        /// 
        /// 转换报表名称
        /// 
        /// 
        /// 
        protected string ConvertReportName(object ReportRemindId)
        {
            string reportName = string.Empty;
            if (ReportRemindId != null)
            {
                string quarterly = string.Empty;
                string halfYear = string.Empty;
                Model.ManagementReport_ReportRemind reportRemind = BLL.ReportRemindService.GetReportRemindByReportRemindId(ReportRemindId.ToString());
                if (reportRemind != null)
                {
                    if (reportRemind.Quarterly == 1)
                    {
                        quarterly = "一季度";
                    }
                    else if (reportRemind.Quarterly == 2)
                    {
                        quarterly = "二季度";
                    }
                    else if (reportRemind.Quarterly == 3)
                    {
                        quarterly = "三季度";
                    }
                    else if (reportRemind.Quarterly == 4)
                    {
                        quarterly = "四季度";
                    }
                    if (reportRemind.HalfYear == 1)
                    {
                        halfYear = "上半年";
                    }
                    else if (reportRemind.HalfYear == 2)
                    {
                        halfYear = "下半年";
                    }
                    if (reportRemind.ReportName == "管理月报")
                    {
                        reportName = reportRemind.Months.Value.Year + "年" + reportRemind.Months.Value.Month + "月管理月报未上报";
                    }
                    else if (reportRemind.ReportName == "百万工时安全统计月报")
                    {
                        reportName = reportRemind.Year.ToString() + "年" + reportRemind.Month.ToString() + "月百万工时安全统计月报未上报";
                    }
                    else if (reportRemind.ReportName == "职工伤亡事故原因分析报")
                    {
                        reportName = reportRemind.Year.ToString() + "年" + reportRemind.Month.ToString() + "月职工伤亡事故原因分析报未上报";
                    }
                    else if (reportRemind.ReportName == "安全生产数据季报")
                    {
                        reportName = reportRemind.Year.ToString() + "年" + quarterly + "安全生产数据季报未上报";
                    }
                    else if (reportRemind.ReportName == "应急演练开展情况季报")
                    {
                        reportName = reportRemind.Year.ToString() + "年" + quarterly + "应急演练开展情况季报未上报";
                    }
                    else if (reportRemind.ReportName == "应急演练工作计划半年报")
                    {
                        reportName = reportRemind.Year.ToString() + "年" + halfYear + "应急演练工作计划半年报未上报";
                    }
                    else if (reportRemind.ReportName == "海外工程项目月度HSSE统计表")
                    {
                        reportName = reportRemind.Year.ToString() + "年" + reportRemind.Month.ToString() + "月海外工程项目月度HSSE统计表";
                    }
                }
            }
            return reportName;
        }
        #endregion
        /// 
        /// 绑定数据
        /// 
        private void BindGrid()
        {
            string strSql = "select ReportRemind.ReportRemindId,ReportRemind.ProjectId,ReportRemind.CompileDate,Project.ProjectCode,Project.ProjectName"
                          + @" from ManagementReport_ReportRemind AS ReportRemind "
                          + @" LEFT JOIN Base_Project AS Project ON ReportRemind.ProjectId=Project.ProjectId WHERE 1=1 ";
            List listStr = new List();
            if (this.drpProject.SelectedValue != BLL.Const._Null)
            {
                strSql += " AND ReportRemind.ProjectId = @ProjectId";
                listStr.Add(new SqlParameter("@ProjectId", this.drpProject.SelectedValue));
            }
            if (this.drpYear.SelectedValue != BLL.Const._Null)
            {
                strSql += " AND (ReportRemind.Year = @Year OR (ReportRemind.Months IS NOT NULL AND YEAR(ReportRemind.Months)=@YearM))";
                listStr.Add(new SqlParameter("@Year", this.drpYear.SelectedValue));
                listStr.Add(new SqlParameter("@YearM", this.drpYear.SelectedValue));
            }
            if (this.drpMonth.SelectedValue != BLL.Const._Null)
            {
                strSql += " AND (ReportRemind.Month = @Month OR (ReportRemind.Months IS NOT NULL AND Month(ReportRemind.Months)=@MonthM))";
                listStr.Add(new SqlParameter("@Month", this.drpMonth.SelectedValue));
                listStr.Add(new SqlParameter("@MonthM", this.drpMonth.SelectedValue));
            }
            if (this.drpQuarter.SelectedValue != BLL.Const._Null)
            {
                strSql += " AND ReportRemind.Quarterly = @Quarterly";
                listStr.Add(new SqlParameter("@Quarterly", this.drpQuarter.SelectedValue));
            }
            if (!string.IsNullOrEmpty(this.txtReportName.Text.Trim()))
            {
                strSql += " AND ReportRemind.ReportName LIKE @ReportName";
                listStr.Add(new SqlParameter("@ReportName", "%" + this.txtReportName.Text.Trim() + "%"));
            }
            strSql += " order by ReportRemind.CompileDate desc";
            SqlParameter[] parameter = listStr.ToArray();
            DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
            Grid1.RecordCount = tb.Rows.Count;
            //tb = GetFilteredTable(Grid1.FilteredData, tb);
            var table = this.GetPagedDataTable(Grid1, tb);
            Grid1.DataSource = table;
            Grid1.DataBind();
        }
        #endregion
        #region 查询
        /// 
        /// 查询
        /// 
        /// 
        /// 
        protected void TextBox_TextChanged(object sender, EventArgs e)
        {
            this.BindGrid();
        }
        #endregion
        #region 表头过滤
        protected void Grid1_FilterChange(object sender, EventArgs e)
        {
            BindGrid();
        }
        #endregion
        #region 页索引改变事件
        /// 
        /// 页索引改变事件
        /// 
        /// 
        /// 
        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }
        #endregion
        #region 排序
        /// 
        /// 排序
        /// 
        /// 
        /// 
        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            Grid1.SortDirection = e.SortDirection;
            Grid1.SortField = e.SortField;
            BindGrid();
        }
        #endregion
        #region 分页选择下拉改变事件
        /// 
        /// 分页选择下拉改变事件
        /// 
        /// 
        /// 
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
            BindGrid();
        }
        #endregion
        #region 删除
        /// 
        /// 右键删除事件
        /// 
        /// 
        /// 
        protected void btnMenuDel_Click(object sender, EventArgs e)
        {
            this.DeleteData();
        }
        /// 
        /// 删除方法
        /// 
        private void DeleteData()
        {
            if (Grid1.SelectedRowIndexArray.Length > 0)
            {
                bool isShow = false;
                if (Grid1.SelectedRowIndexArray.Length == 1)
                {
                    isShow = true;
                }
                foreach (int rowIndex in Grid1.SelectedRowIndexArray)
                {
                    string rowID = Grid1.DataKeys[rowIndex][0].ToString();
                    if (this.judgementDelete(rowID, isShow))
                    {
                        Model.ManagementReport_ReportRemind reportRemind = BLL.ReportRemindService.GetReportRemindByReportRemindId(rowID);
                        if (reportRemind != null)
                        {
                            BLL.LogService.AddSys_Log(this.CurrUser, null, reportRemind.ReportRemindId, BLL.Const.ServerReportRemindMenuId, BLL.Const.BtnDelete);
                            BLL.ReportRemindService.DeleteReportRemindByReportRemind(reportRemind);
                        }
                    }
                }
                BindGrid();
                ShowNotify("删除数据成功!(表格数据已重新绑定)", MessageBoxIcon.Success);
            }
        }
        /// 
        /// 判断是否可删除
        /// 
        /// 
        /// 
        /// 
        private bool judgementDelete(string rowID, bool isShow)
        {
            string content = string.Empty;
            if (string.IsNullOrEmpty(content))
            {
                return true;
            }
            else
            {
                if (isShow)
                {
                    Alert.ShowInTop(content);
                }
                return false;
            }
        }
        #endregion
        #region 获取按钮权限
        /// 
        /// 获取按钮权限
        /// 
        /// 
        /// 
        private void GetButtonPower()
        {
            if (Request.Params["value"] == "0")
            {
                return;
            }
            var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.ServerReportRemindMenuId);
            if (buttonList.Count() > 0)
            {
                if (buttonList.Contains(BLL.Const.BtnDelete))
                {
                    this.btnMenuDel.Hidden = false;
                }
            }
        }
        #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 = 500;
            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 == "tfNumber")
                    {
                        html = (row.FindControl("lblNumber") as AspNet.Label).Text;
                    }
                    if (column.ColumnID == "lblReportName")
                    {
                        html = (row.FindControl("lblReportName") as AspNet.Label).Text;
                    }
                    sb.AppendFormat("| {0}", html);
                }
                sb.Append(" | 
");
            }
            sb.Append("
");
            return sb.ToString();
        }
        #endregion
    }
}