using BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
namespace FineUIPro.Web.JDGL.CostAnalysis
{
    public partial class EarnedValueCurve : PageBase
    {
        #region 加载
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindChart();
                InitTreeMenu();
            }
        }
        #endregion
        #region 加载
        /// 
        /// 树加载
        /// 
        private void InitTreeMenu()
        {
            this.trWBS.Nodes.Clear();
            this.trWBS.ShowBorder = false;
            this.trWBS.ShowHeader = false;
            this.trWBS.EnableIcons = true;
            this.trWBS.AutoScroll = true;
            this.trWBS.EnableSingleClickExpand = true;
            //var project = BLL.ProjectService.GetProjectByProjectId(this.CurrUser.LoginProjectId);
            //if (project != null)
            //{
            //    TreeNode rootNode = new TreeNode();
            //    rootNode.Text = project.ProjectName;
            //    rootNode.NodeID = project.ProjectId;
            //    rootNode.CommandName = "project";
            //    rootNode.EnableExpandEvent = true;
            //    this.trWBS.Nodes.Add(rootNode);
            //    if (BLL.Project_InstallationService.IsExitProjectInstallation(project.ProjectId))
            //    {
            //        TreeNode emptyNode = new TreeNode();
            //        emptyNode.Text = "";
            //        emptyNode.NodeID = "";
            //        rootNode.Nodes.Add(emptyNode);
            //    }
            //}
            Model.Project_Installation installation = BLL.Project_InstallationService.GetProjectInstallationByProjectId(this.CurrUser.LoginProjectId);
            if (installation != null)
            {
                TreeNode newNode = new TreeNode();
                newNode.Text = "[" + installation.InstallationCode + "]" + installation.InstallationName;
                newNode.NodeID = installation.InstallationId;
                newNode.CommandName = "installation";
                newNode.EnableExpandEvent = true;
                newNode.EnableClickEvent = true;
                newNode.EnableCheckBox = false;
                this.trWBS.Nodes.Add(newNode);
                TreeNode emptyNode = new TreeNode();
                emptyNode.Text = "";
                emptyNode.NodeID = "";
                newNode.Nodes.Add(emptyNode);
            }
        }
        #endregion
        #region 树节点展开
        /// 
        /// 树节点展开
        /// 
        /// 
        /// 
        protected void trWBS_NodeExpand(object sender, TreeNodeEventArgs e)
        {
            e.Node.Nodes.Clear();
            if (e.Node.CommandName == "project")  //展开项目节点
            {
                var installations = from x in Funs.DB.Project_Installation
                                    where x.ProjectId == e.Node.NodeID && x.SuperInstallationId == "0"
                                    orderby x.InstallationCode
                                    select x;
                foreach (var installation in installations)
                {
                    TreeNode newNode = new TreeNode();
                    newNode.Text = "[" + installation.InstallationCode + "]" + installation.InstallationName;
                    newNode.NodeID = installation.InstallationId;
                    newNode.CommandName = "installation";
                    newNode.EnableExpandEvent = true;
                    newNode.EnableClickEvent = true;
                    e.Node.Nodes.Add(newNode);
                    TreeNode emptyNode = new TreeNode();
                    emptyNode.Text = "";
                    emptyNode.NodeID = "";
                    newNode.Nodes.Add(emptyNode);
                }
            }
            else if (e.Node.CommandName == "installation")  //展开装置/单元节点
            {
                var installations = from x in Funs.DB.Project_Installation
                                    where x.SuperInstallationId == e.Node.NodeID
                                    orderby x.InstallationCode
                                    select x;
                if (installations.Count() > 0)
                {
                    foreach (var installation in installations)
                    {
                        TreeNode newNode = new TreeNode();
                        newNode.Text = "[" + installation.InstallationCode + "]" + installation.InstallationName;
                        newNode.NodeID = installation.InstallationId;
                        newNode.CommandName = "installation";
                        newNode.EnableExpandEvent = true;
                        newNode.EnableClickEvent = true;
                        e.Node.Nodes.Add(newNode);
                        TreeNode emptyNode = new TreeNode();
                        emptyNode.Text = "";
                        emptyNode.NodeID = "";
                        newNode.Nodes.Add(emptyNode);
                    }
                }
                else
                {
                    var cnProfessions = from x in Funs.DB.WBS_CnProfession where x.InstallationId == e.Node.NodeID && x.IsApprove == true orderby x.OldId select x;
                    if (cnProfessions.Count() > 0)   //普通装置主项
                    {
                        foreach (var cnProfession in cnProfessions)
                        {
                            TreeNode newNode = new TreeNode();
                            newNode.Text = cnProfession.CnProfessionName;
                            newNode.NodeID = cnProfession.CnProfessionId;
                            newNode.CommandName = "cnProfession";
                            newNode.EnableExpandEvent = true;
                            newNode.EnableClickEvent = true;
                            e.Node.Nodes.Add(newNode);
                            TreeNode emptyNode = new TreeNode();
                            emptyNode.Text = "";
                            emptyNode.NodeID = "";
                            newNode.Nodes.Add(emptyNode);
                        }
                    }
                    else       //总图
                    {
                        var unitProjects = from x in Funs.DB.Wbs_UnitProject where x.InstallationId == e.Node.NodeID && x.SuperUnitProjectId == null && x.IsApprove == true orderby x.SortIndex, x.UnitProjectCode select x;
                        foreach (var unitProject in unitProjects)
                        {
                            TreeNode newNode = new TreeNode();
                            newNode.Text = unitProject.UnitProjectName;
                            newNode.NodeID = unitProject.UnitProjectId;
                            newNode.CommandName = "unitProject";
                            newNode.EnableExpandEvent = true;
                            if (unitProject.IsApprove == true)
                            {
                                newNode.Checked = true;
                            }
                            else
                            {
                                newNode.Checked = false;
                            }
                            newNode.EnableClickEvent = true;
                            e.Node.Nodes.Add(newNode);
                            TreeNode emptyNode = new TreeNode();
                            emptyNode.Text = "";
                            emptyNode.NodeID = "";
                            newNode.Nodes.Add(emptyNode);
                        }
                    }
                }
            }
            else if (e.Node.CommandName == "cnProfession")   //展开专业节点
            {
                var unitProjects = from x in Funs.DB.Wbs_UnitProject where x.CnProfessionId == e.Node.NodeID && x.SuperUnitProjectId == null && x.IsApprove == true orderby x.SortIndex, x.UnitProjectCode select x;
                foreach (var unitProject in unitProjects)
                {
                    TreeNode newNode = new TreeNode();
                    newNode.Text = unitProject.UnitProjectName;
                    newNode.NodeID = unitProject.UnitProjectId;
                    newNode.CommandName = "unitProject";
                    newNode.EnableExpandEvent = true;
                    newNode.EnableCheckBox = true;
                    newNode.EnableCheckEvent = true;
                    if (unitProject.IsSelected == true && unitProject.IsApprove == null)
                    {
                        unitProject.IsApprove = true;
                        BLL.UnitProjectService.UpdateUnitProject(unitProject);
                    }
                    if (unitProject.IsApprove == true)
                    {
                        newNode.Checked = true;
                    }
                    else
                    {
                        newNode.Checked = false;
                    }
                    newNode.EnableClickEvent = true;
                    e.Node.Nodes.Add(newNode);
                    TreeNode emptyNode = new TreeNode();
                    emptyNode.Text = "";
                    emptyNode.NodeID = "";
                    newNode.Nodes.Add(emptyNode);
                }
            }
            else if (e.Node.CommandName == "unitProject")   //展开单位工程节点
            {
                var wbsSet1s = from x in Funs.DB.Wbs_WbsSet where x.UnitProjectId == e.Node.NodeID && x.SuperWbsSetId == null && x.IsApprove == true orderby x.WbsSetCode select x;
                if (wbsSet1s.Count() > 0)
                {
                    foreach (var wbsSet1 in wbsSet1s)
                    {
                        TreeNode newNode = new TreeNode();
                        newNode.Text = wbsSet1.WbsSetName;
                        newNode.NodeID = wbsSet1.WbsSetId;
                        newNode.CommandName = "wbsSet";
                        newNode.EnableExpandEvent = true;
                        newNode.EnableCheckBox = true;
                        newNode.EnableCheckEvent = true;
                        newNode.EnableClickEvent = true;
                        e.Node.Nodes.Add(newNode);
                        var wbsSets = BLL.WbsSetService.GetWbsSetsBySuperWbsSetId(wbsSet1.WbsSetId);
                        if (wbsSets.Count > 0)
                        {
                            TreeNode emptyNode = new TreeNode();
                            emptyNode.Text = "";
                            emptyNode.NodeID = "";
                            newNode.Nodes.Add(emptyNode);
                        }
                        //needAddTempNode = false;
                        //var wbsSets = BLL.WbsSetService.GetWbsSetsBySuperWbsSetId(wbsSet1.WbsSetId);
                        //foreach (var wbsSet in wbsSets)
                        //{
                        //    var childWbsSets = from x in Funs.DB.Wbs_WbsSet where x.SuperWbsSetId == wbsSet.WbsSetId select x;
                        //    if (childWbsSets.Count() > 0)
                        //    {
                        //        needAddTempNode = true;
                        //        break;
                        //    }
                        //}
                        //if (needAddTempNode)
                        //{
                        //    TreeNode emptyNode = new TreeNode();
                        //    emptyNode.Text = "";
                        //    emptyNode.NodeID = "";
                        //    newNode.Nodes.Add(emptyNode);
                        //}
                    }
                }
            }
            else if (e.Node.CommandName == "wbsSet")   //展开分部/子分部/分项/子分项工程节点
            {
                var childWbsSets = BLL.WbsSetService.GetApproveWbsSetsBySuperWbsSetId(e.Node.NodeID);
                foreach (var wbsSet in childWbsSets)
                {
                    TreeNode newNode = new TreeNode();
                    newNode.Text = wbsSet.WbsSetName;
                    newNode.NodeID = wbsSet.WbsSetId;
                    newNode.CommandName = "wbsSet";
                    newNode.EnableExpandEvent = true;
                    newNode.EnableClickEvent = true;
                    e.Node.Nodes.Add(newNode);
                    var wbsSets = BLL.WbsSetService.GetApproveWbsSetsBySuperWbsSetId(wbsSet.WbsSetId);
                    if (wbsSets.Count > 0)
                    {
                        TreeNode emptyNode = new TreeNode();
                        emptyNode.Text = "";
                        emptyNode.NodeID = "";
                        newNode.Nodes.Add(emptyNode);
                    }
                    //needAddTempNode = false;
                    //var wbsSets = BLL.WbsSetService.GetWbsSetsBySuperWbsSetId(wbsSet.WbsSetId);
                    //foreach (var wbsSetc in wbsSets)
                    //{
                    //    var childWbsSets1 = from x in Funs.DB.Wbs_WbsSet where x.SuperWbsSetId == wbsSetc.WbsSetId select x;
                    //    if (childWbsSets1.Count() > 0)
                    //    {
                    //        needAddTempNode = true;
                    //        break;
                    //    }
                    //}
                    //if (needAddTempNode)
                    //{
                    //    TreeNode emptyNode = new TreeNode();
                    //    emptyNode.Text = "";
                    //    emptyNode.NodeID = "";
                    //    newNode.Nodes.Add(emptyNode);
                    //}
                }
            }
            BindChart();
        }
        #endregion
        #region Tree点击事件
        /// 
        /// Tree点击事件
        /// 
        /// 
        /// 
        protected void trWBS_NodeCommand(object sender, TreeCommandEventArgs e)
        {
            BindChart();
        }
        #endregion
        #region 数据绑定
        /// 
        /// 数据绑定
        /// 
        private void BindChart()
        {
            string id = this.trWBS.SelectedNodeID;
            Model.WBS_CostControl costControl = BLL.CostControlService.GetCostControlByCostControlId(id);
            if (costControl != null)
            {
                string strSql = "select distinct (cast(YEAR(Months) as varchar(4))+'.'+cast(MONTH(Months) as varchar(2))) as 月份,t.Months," +
                                 "ThisRealCost as '本月已完工作实际费用-ACWP',ThisPlanCost as '本月已完工作预算费用-BCWP',ThisPlanValue as '本月计划工作预算费用-BCWS',TotalPlanValue as '累计计划工作预算费用-BCWS',TotalRealCost as '累计已完工作实际费用-ACWP',TotalPlanCost as '累计已完工作预算费用-BCWP' " +
                                 "from dbo.View_WBS_CostControlDetail as t where CostControlId=@Id order by t.Months";
                //string date = DateTime.Now.Year + "-" + DateTime.Now.Month + "-01";
                SqlParameter[] parameter = new SqlParameter[]
                        {
                        new SqlParameter("@Id",id),
                            //new SqlParameter("@Months",date),
                        };
                DataTable dt = SQLHelper.GetDataTableRunText(strSql, parameter);
                this.Grid1.DataSource = dt;
                this.Grid1.DataBind();
                decimal lastbcws = 0, bcws = 0, lastacwp = 0, acwp = 0, lastbcwp = 0, bcwp = 0;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dt.Rows[i]["本月已完工作实际费用-ACWP"] = Funs.GetNewDecimalOrZero(dt.Rows[i]["本月已完工作实际费用-ACWP"].ToString()) / 10000;
                    dt.Rows[i]["本月已完工作预算费用-BCWP"] = Funs.GetNewDecimalOrZero(dt.Rows[i]["本月已完工作预算费用-BCWP"].ToString()) / 10000;
                    dt.Rows[i]["本月计划工作预算费用-BCWS"] = Funs.GetNewDecimalOrZero(dt.Rows[i]["本月计划工作预算费用-BCWS"].ToString()) / 10000;
                    bcws = Funs.GetNewDecimalOrZero(dt.Rows[i]["累计计划工作预算费用-BCWS"].ToString());
                    acwp = Funs.GetNewDecimalOrZero(dt.Rows[i]["累计已完工作实际费用-ACWP"].ToString());
                    bcwp = Funs.GetNewDecimalOrZero(dt.Rows[i]["累计已完工作预算费用-BCWP"].ToString());
                    if (bcws == lastbcws)
                    {
                        if (Funs.GetNewDateTimeOrNow(dt.Rows[i]["Months"].ToString())>DateTime.Now)
                        {
                            dt.Rows[i]["累计计划工作预算费用-BCWS"] = DBNull.Value;
                        }
                        else
                        {
                            dt.Rows[i]["累计计划工作预算费用-BCWS"] = bcws / 10000;
                        }
                    }
                    else
                    {
                        dt.Rows[i]["累计计划工作预算费用-BCWS"] = bcws / 10000;
                    }
                    if (acwp == lastacwp)
                    {
                        if (Funs.GetNewDateTimeOrNow(dt.Rows[i]["Months"].ToString()) > DateTime.Now)
                        {
                            dt.Rows[i]["累计已完工作实际费用-ACWP"] = DBNull.Value;
                        }
                        else
                        {
                            dt.Rows[i]["累计已完工作实际费用-ACWP"] = acwp / 10000;
                        }
                    }
                    else
                    {
                        dt.Rows[i]["累计已完工作实际费用-ACWP"] = acwp / 10000;
                    }
                    if (bcwp == lastbcwp)
                    {
                        if (Funs.GetNewDateTimeOrNow(dt.Rows[i]["Months"].ToString()) > DateTime.Now)
                        {
                            dt.Rows[i]["累计已完工作预算费用-BCWP"] = DBNull.Value;
                        }
                        else
                        {
                            dt.Rows[i]["累计已完工作预算费用-BCWP"] = bcwp / 10000;
                        }
                    }
                    else
                    {
                        dt.Rows[i]["累计已完工作预算费用-BCWP"] = bcwp / 10000;
                    }
                    lastbcws = bcws;
                    lastacwp = acwp;
                    lastbcwp = bcwp;
                }
                this.ChartEV.CreateMaryChart(dt, 1020, 420, null);
            }
            else
            {
                string strSql = "select distinct (cast(YEAR(Months) as varchar(4))+'.'+cast(MONTH(Months) as varchar(2))) as 月份,t.Months," +
                               "ThisRealCost as '本月已完工作实际费用-ACWP',ThisPlanCost as '本月已完工作预算费用-BCWP',ThisPlanValue as '本月计划工作预算费用-BCWS',TotalPlanValue as '累计计划工作预算费用-BCWS',TotalRealCost as '累计已完工作实际费用-ACWP',TotalPlanCost as '累计已完工作预算费用-BCWP' " +
                               "from dbo.View_WBS_CostControlParentDetail as t where ParentId=@Id order by t.Months";
                //string date = DateTime.Now.Year + "-" + DateTime.Now.Month + "-01";
                SqlParameter[] parameter = new SqlParameter[]
                        {
                        new SqlParameter("@Id",id),
                            //new SqlParameter("@Months",date),
                        };
                DataTable dt = SQLHelper.GetDataTableRunText(strSql, parameter);
                this.Grid1.DataSource = dt;
                this.Grid1.DataBind();
                decimal lastbcws = 0, bcws = 0, lastacwp = 0, acwp = 0, lastbcwp = 0, bcwp = 0;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dt.Rows[i]["本月已完工作实际费用-ACWP"] = Funs.GetNewDecimalOrZero(dt.Rows[i]["本月已完工作实际费用-ACWP"].ToString()) / 10000;
                    dt.Rows[i]["本月已完工作预算费用-BCWP"] = Funs.GetNewDecimalOrZero(dt.Rows[i]["本月已完工作预算费用-BCWP"].ToString()) / 10000;
                    dt.Rows[i]["本月计划工作预算费用-BCWS"] = Funs.GetNewDecimalOrZero(dt.Rows[i]["本月计划工作预算费用-BCWS"].ToString()) / 10000;
                    bcws = Funs.GetNewDecimalOrZero(dt.Rows[i]["累计计划工作预算费用-BCWS"].ToString());
                    acwp = Funs.GetNewDecimalOrZero(dt.Rows[i]["累计已完工作实际费用-ACWP"].ToString());
                    bcwp = Funs.GetNewDecimalOrZero(dt.Rows[i]["累计已完工作预算费用-BCWP"].ToString());
                    if (bcws == lastbcws)
                    {
                        if (Funs.GetNewDateTimeOrNow(dt.Rows[i]["Months"].ToString()) > DateTime.Now)
                        {
                            dt.Rows[i]["累计计划工作预算费用-BCWS"] = DBNull.Value;
                        }
                        else
                        {
                            dt.Rows[i]["累计计划工作预算费用-BCWS"] = bcws / 10000;
                        }
                    }
                    else
                    {
                        dt.Rows[i]["累计计划工作预算费用-BCWS"] = bcws / 10000;
                    }
                    if (acwp == lastacwp)
                    {
                        if (Funs.GetNewDateTimeOrNow(dt.Rows[i]["Months"].ToString()) > DateTime.Now)
                        {
                            dt.Rows[i]["累计已完工作实际费用-ACWP"] = DBNull.Value;
                        }
                        else
                        {
                            dt.Rows[i]["累计已完工作实际费用-ACWP"] = acwp / 10000;
                        }
                    }
                    else
                    {
                        dt.Rows[i]["累计已完工作实际费用-ACWP"] = acwp / 10000;
                    }
                    if (bcwp == lastbcwp)
                    {
                        if (Funs.GetNewDateTimeOrNow(dt.Rows[i]["Months"].ToString()) > DateTime.Now)
                        {
                            dt.Rows[i]["累计已完工作预算费用-BCWP"] = DBNull.Value;
                        }
                        else
                        {
                            dt.Rows[i]["累计已完工作预算费用-BCWP"] = bcwp / 10000;
                        }
                    }
                    else
                    {
                        dt.Rows[i]["累计已完工作预算费用-BCWP"] = bcwp / 10000;
                    }
                    lastbcws = bcws;
                    lastacwp = acwp;
                    lastbcwp = bcwp;
                }
                this.ChartEV.CreateMaryChart(dt, 1020, 420, null);
            }
        }
        #endregion
        #region 将DataTable行列转换
        ///   
        /// 将DataTable行列转换  
        ///   
        /// 要转换的DataTable  
        /// 要作为Column的哪列  
        ///   
        private DataTable Col2Row(DataTable src, int columnHead)
        {
            DataTable result = new DataTable();
            DataColumn myHead = src.Columns[columnHead];
            result.Columns.Add(myHead.ColumnName);
            FineUIPro.RenderField bf;
            bf = new FineUIPro.RenderField();
            bf.ColumnID = myHead.ColumnName;
            bf.DataField = myHead.ColumnName;
            bf.HeaderText = myHead.ColumnName;
            Grid1.Columns.Add(bf);
            for (int i = 0; i < src.Rows.Count; i++)
            {
                result.Columns.Add(src.Rows[i][myHead].ToString());
                bf = new FineUIPro.RenderField();
                bf.ColumnID = src.Rows[i][myHead].ToString();
                bf.DataField = src.Rows[i][myHead].ToString();
                bf.HeaderText = src.Rows[i][myHead].ToString();
                Grid1.Columns.Add(bf);
            }
            Grid1.DataKeyNames = new string[] { "月份" };
            foreach (DataColumn col in src.Columns)
            {
                if (col == myHead)
                    continue;
                object[] newRow = new object[src.Rows.Count + 1];
                newRow[0] = col.ColumnName;
                for (int i = 0; i < src.Rows.Count; i++)
                {
                    newRow[i + 1] = src.Rows[i][col];
                }
                result.Rows.Add(newRow);
            }
            return result;
        }
        private DataTable Col2Row(DataTable src, string columnHead)
        {
            for (int i = 0; i < src.Columns.Count; i++)
            {
                if (src.Columns[i].ColumnName.ToUpper() == columnHead.ToUpper())
                    return Col2Row(src, i);
            }
            return new DataTable();
        }
        #endregion
    }
}