using BLL;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web.UI.WebControls;
namespace FineUIPro.Web.JDGL.WBS
{
    public partial class WorkloadInput : PageBase
    {
        #region 页面加载
        /// 
        /// 页面加载
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetButtonPower();
                InitTreeMenu();
                this.txtMonths.Text = string.Format("{0:yyyy-MM}", DateTime.Now);
                //Funs.FineUIPleaseSelect(this.drpWeek);
            }
        }
        /// 
        /// 树加载
        /// 
        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();
            bool needAddTempNode = false;   //是否需要增加空节点
            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);
                    //}
                }
            }
        }
        #endregion
        #region   Tree点击事件
        /// 
        /// Tree点击事件
        /// 
        /// 
        /// 
        protected void trWBS_NodeCommand(object sender, TreeCommandEventArgs e)
        {
            InitWeek();
            this.Grid1.Columns[4].HeaderText = "本月计划完成量";
            this.Grid1.Columns[7].HeaderText = "本月完成量";
            BindGrid();
        }
        #endregion
        #region  修改关闭窗口
        /// 
        /// 关闭窗口
        /// 
        /// 
        /// 
        protected void Window1_Close(object sender, WindowCloseEventArgs e)
        {
            ShowNotify("录入成功!", MessageBoxIcon.Success);
        }
        #endregion
        /// 
        /// 右键录入事件
        /// 
        /// 
        /// 
        protected void btnMenuEdit_Click(object sender, EventArgs e)
        {
            if (this.trWBS.SelectedNode != null)
            {
                this.hdSelectId.Text = this.trWBS.SelectedNode.NodeID;
                PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("WorkloadInputEditAll.aspx?Id={0}", this.trWBS.SelectedNode.NodeID, "编辑 - ")));
            }
            else
            {
                ShowNotify("请选择树节点!", MessageBoxIcon.Warning);
            }
        }
        /// 
        /// 右键导入事件
        /// 
        /// 
        /// 
        protected void btnMenuImport_Click(object sender, EventArgs e)
        {
            if (this.trWBS.SelectedNode != null)
            {
                if (this.trWBS.SelectedNode.CommandName != "project" && this.trWBS.SelectedNode.CommandName != "installation" && this.trWBS.SelectedNode.CommandName != "cnProfession")   //非项目、装置、专业节点可以修改
                {
                    this.hdSelectId.Text = this.trWBS.SelectedNode.NodeID;
                    PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("WorkloadInputIn.aspx?Id={0}", this.trWBS.SelectedNode.NodeID, "编辑 - ")));
                }
            }
            else
            {
                ShowNotify("请选择树节点!", MessageBoxIcon.Warning);
            }
        }
        #region 保存事件
        /// 
        /// 保存
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtMonths.Text.Trim()))
            {
                Alert.ShowInTop("请选择月份!", MessageBoxIcon.Warning);
                return;
            }
            DateTime months = Convert.ToDateTime(this.txtMonths.Text.Trim() + "-01");
            if (Grid1.Rows.Count > 0)
            {
                decimal changeThisPlanValue = 0, changeThisRealCost = 0, changeThisPlanCost = 0,  //当月总变化完成成本、完成预算
                        oldThisPlanValue = 0, oldThisRealCost = 0, oldThisPlanCost = 0,
                        thisPlanValue = 0, thisRealCost = 0, thisPlanCost = 0;
                JArray mergedData = Grid1.GetMergedData();
                //if (this.drpWeek.SelectedValue == BLL.Const._Null)  //保存月记录
                //{
                foreach (JObject mergedRow in mergedData)
                {
                    oldThisPlanValue = 0;
                    oldThisRealCost = 0;
                    oldThisPlanCost = 0;
                    JObject values = mergedRow.Value("values");
                    string costControlDetailId = values.Value("CostControlDetailId");
                    string costControlId = values.Value("CostControlId");
                    string thisNum = values.Value("ThisNum");
                    string planPrice = values.Value("PlanPrice");
                    Model.WBS_CostControlDetail costControlDetail = BLL.CostControlDetailService.GetCostControlDetailByCostControlDetailId(costControlDetailId);
                    Model.WBS_CostControl costControl = BLL.CostControlService.GetCostControlByCostControlId(costControlId);
                    if (costControlDetail != null)
                    {
                        oldThisRealCost = (costControlDetail.ThisNum ?? 0) * (costControl.RealPrice ?? 0);
                        oldThisPlanCost = (costControlDetail.ThisNum ?? 0) * (costControl.PlanPrice ?? 0);
                        if (!string.IsNullOrEmpty(thisNum))
                        {
                            costControlDetail.ThisNum = Convert.ToDecimal(thisNum);
                        }
                        thisPlanValue = (costControlDetail.PlanNum ?? 0) * (costControl.PlanPrice ?? 0);
                        thisRealCost = (costControlDetail.ThisNum ?? 0) * (costControl.RealPrice ?? 0);
                        thisPlanCost = (costControlDetail.ThisNum ?? 0) * (costControl.PlanPrice ?? 0);
                        BLL.CostControlDetailService.UpdateCostControlDetail(costControlDetail);
                    }
                    else
                    {
                        costControlDetail = new Model.WBS_CostControlDetail();
                        costControlDetail.CostControlDetailId = costControlDetailId;
                        costControlDetail.CostControlId = costControlId;
                        costControlDetail.Months = months;
                        if (!string.IsNullOrEmpty(thisNum))
                        {
                            costControlDetail.ThisNum = Convert.ToDecimal(thisNum);
                        }
                        thisPlanValue = 0;
                        thisRealCost = (costControlDetail.ThisNum ?? 0) * (costControl.RealPrice ?? 0);
                        thisPlanCost = (costControlDetail.ThisNum ?? 0) * (costControl.PlanPrice ?? 0);
                        BLL.CostControlDetailService.AddCostControlDetail(costControlDetail);
                    }
                    //累加变化值,计算总的变化值
                    changeThisRealCost += thisRealCost - oldThisRealCost;
                    changeThisPlanCost += thisPlanCost - oldThisPlanCost;
                }
                //累计所有子节点的计划值
                var costControls = BLL.CostControlService.GetCostControlsByWbsSetId(this.trWBS.SelectedNodeID);
                foreach (var item in costControls)
                {
                    Model.WBS_CostControlDetail costControlDetail = BLL.CostControlDetailService.GetCostControlDetailByCostControlIdAndMonths(item.CostControlId, months);
                    if (costControlDetail != null)
                    {
                        changeThisPlanValue += (costControlDetail.PlanNum ?? 0) * (item.PlanPrice ?? 0);
                    }
                }
                Model.WBS_CostControlParentDetail parentDetail = BLL.CostControlParentDetailService.GetCostControlParentDetailByParentIdAndMonths(this.trWBS.SelectedNodeID, months);
                if (parentDetail != null)  //如果工作项节点存在,则计算计划值差异值
                {
                    changeThisPlanValue = changeThisPlanValue - (parentDetail.ThisPlanValue ?? 0);
                }
                //更新树节点
                Model.Wbs_WbsSet wbsSet = BLL.WbsSetService.GetWbsSetByWbsSetId(this.trWBS.SelectedNodeID);
                //更新工作包、工作项
                UpdateWbsSetDetail(this.trWBS.SelectedNodeID, months, changeThisPlanValue, changeThisRealCost, changeThisPlanCost);
                //更新分部
                Model.WBS_CostControlParentDetail unitProjectDetail = BLL.CostControlParentDetailService.GetCostControlParentDetailByParentIdAndMonths(wbsSet.UnitProjectId, months);
                if (unitProjectDetail != null)
                {
                    unitProjectDetail.ThisPlanValue += changeThisPlanValue;
                    unitProjectDetail.ThisRealCost += changeThisRealCost;
                    unitProjectDetail.ThisPlanCost += changeThisPlanCost;
                    BLL.CostControlParentDetailService.UpdateCostControlParentDetail(unitProjectDetail);
                }
                else
                {
                    unitProjectDetail = new Model.WBS_CostControlParentDetail();
                    unitProjectDetail.CostControlParentDetailId = SQLHelper.GetNewID();
                    unitProjectDetail.ParentId = wbsSet.UnitProjectId;
                    unitProjectDetail.Months = months;
                    unitProjectDetail.ThisPlanValue = changeThisPlanValue;
                    unitProjectDetail.ThisRealCost = changeThisRealCost;
                    unitProjectDetail.ThisPlanCost = changeThisPlanCost;
                    BLL.CostControlParentDetailService.AddCostControlParentDetail(unitProjectDetail);
                }
                //更新专业
                Model.WBS_CostControlParentDetail cnProfessionDetail = BLL.CostControlParentDetailService.GetCostControlParentDetailByParentIdAndMonths(wbsSet.CnProfessionId, months);
                if (cnProfessionDetail != null)
                {
                    cnProfessionDetail.ThisPlanValue += changeThisPlanValue;
                    cnProfessionDetail.ThisRealCost += changeThisRealCost;
                    cnProfessionDetail.ThisPlanCost += changeThisPlanCost;
                    BLL.CostControlParentDetailService.UpdateCostControlParentDetail(cnProfessionDetail);
                }
                else
                {
                    cnProfessionDetail = new Model.WBS_CostControlParentDetail();
                    cnProfessionDetail.CostControlParentDetailId = SQLHelper.GetNewID();
                    cnProfessionDetail.ParentId = wbsSet.CnProfessionId;
                    cnProfessionDetail.Months = months;
                    cnProfessionDetail.ThisPlanValue = changeThisPlanValue;
                    cnProfessionDetail.ThisRealCost = changeThisRealCost;
                    cnProfessionDetail.ThisPlanCost = changeThisPlanCost;
                    BLL.CostControlParentDetailService.AddCostControlParentDetail(cnProfessionDetail);
                }
                //更新装置
                UpdateInstallationDetail(wbsSet.InstallationId, months, changeThisPlanValue, changeThisRealCost, changeThisPlanCost);
                //}
                //else   //保存周记录
                //{
                //    string[] strs = this.drpWeek.SelectedValue.Split(',');
                //    DateTime startDate = Convert.ToDateTime(strs[0]);
                //    DateTime endDate = Convert.ToDateTime(strs[1]);
                //    foreach (JObject mergedRow in mergedData)
                //    {
                //        oldThisPlanValue = 0;
                //        oldThisRealCost = 0;
                //        oldThisPlanCost = 0;
                //        JObject values = mergedRow.Value("values");
                //        string costControlDetailId = values.Value("CostControlDetailId");
                //        string costControlId = values.Value("CostControlId");
                //        string thisNum = values.Value("ThisNum");
                //        string planPrice = values.Value("PlanPrice");
                //        Model.WBS_CostControlDetail costControlDetail = BLL.CostControlDetailService.GetCostControlDetailByCostControlDetailId(costControlDetailId);
                //        Model.WBS_CostControl costControl = BLL.CostControlService.GetCostControlByCostControlId(costControlId);
                //        if (costControlDetail != null)
                //        {
                //            oldThisRealCost = (costControlDetail.ThisNum ?? 0) * (costControl.RealPrice ?? 0);
                //            oldThisPlanCost = (costControlDetail.ThisNum ?? 0) * (costControl.PlanPrice ?? 0);
                //            if (!string.IsNullOrEmpty(thisNum))
                //            {
                //                costControlDetail.ThisNum = Convert.ToDecimal(thisNum);
                //            }
                //            thisPlanValue = (costControlDetail.PlanNum ?? 0) * (costControl.PlanPrice ?? 0);
                //            thisRealCost = (costControlDetail.ThisNum ?? 0) * (costControl.RealPrice ?? 0);
                //            thisPlanCost = (costControlDetail.ThisNum ?? 0) * (costControl.PlanPrice ?? 0);
                //            BLL.CostControlDetailService.UpdateCostControlDetail(costControlDetail);
                //        }
                //        else
                //        {
                //            costControlDetail = new Model.WBS_CostControlDetail();
                //            costControlDetail.CostControlDetailId = costControlDetailId;
                //            costControlDetail.CostControlId = costControlId;
                //            costControlDetail.Months = months;
                //            costControlDetail.StartDate = startDate;
                //            costControlDetail.EndDate = endDate;
                //            if (!string.IsNullOrEmpty(thisNum))
                //            {
                //                costControlDetail.ThisNum = Convert.ToDecimal(thisNum);
                //            }
                //            thisPlanValue = 0;
                //            thisRealCost = (costControlDetail.ThisNum ?? 0) * (costControl.RealPrice ?? 0);
                //            thisPlanCost = (costControlDetail.ThisNum ?? 0) * (costControl.PlanPrice ?? 0);
                //            BLL.CostControlDetailService.AddCostControlDetail(costControlDetail);
                //        }
                //        //累加变化值,计算总的变化值
                //        changeThisRealCost += thisRealCost - oldThisRealCost;
                //        changeThisPlanCost += thisPlanCost - oldThisPlanCost;
                //    }
                //    //累计所有子节点的计划值
                //    var costControls = BLL.CostControlService.GetCostControlsByWbsSetId(this.trWBS.SelectedNodeID);
                //    foreach (var item in costControls)
                //    {
                //        Model.WBS_CostControlDetail costControlDetail = BLL.CostControlDetailService.GetCostControlDetailByCostControlIdAndMonthsWeek(item.CostControlId, months, startDate);
                //        if (costControlDetail != null)
                //        {
                //            changeThisPlanValue += (costControlDetail.PlanNum ?? 0) * (item.PlanPrice ?? 0);
                //        }
                //    }
                //    Model.WBS_CostControlParentDetail parentDetail = BLL.CostControlParentDetailService.GetCostControlParentDetailByParentIdAndMonthsWeek(this.trWBS.SelectedNodeID, months, startDate);
                //    if (parentDetail != null)  //如果工作项节点存在,则计算计划值差异值
                //    {
                //        changeThisPlanValue = changeThisPlanValue - (parentDetail.ThisPlanValue ?? 0);
                //    }
                //    //更新树节点
                //    Model.Wbs_WbsSet wbsSet = BLL.WbsSetService.GetWbsSetByWbsSetId(this.trWBS.SelectedNodeID);
                //    //更新工作包、工作项
                //    UpdateWeekWbsSetDetail(this.trWBS.SelectedNodeID, months, startDate, endDate, changeThisPlanValue, changeThisRealCost, changeThisPlanCost);
                //    //更新分部
                //    Model.WBS_CostControlParentDetail unitProjectDetail = BLL.CostControlParentDetailService.GetCostControlParentDetailByParentIdAndMonthsWeek(wbsSet.UnitProjectId, months, startDate);
                //    if (unitProjectDetail != null)
                //    {
                //        unitProjectDetail.ThisPlanValue += changeThisPlanValue;
                //        unitProjectDetail.ThisRealCost += changeThisRealCost;
                //        unitProjectDetail.ThisPlanCost += changeThisPlanCost;
                //        BLL.CostControlParentDetailService.UpdateCostControlParentDetail(unitProjectDetail);
                //    }
                //    else
                //    {
                //        unitProjectDetail = new Model.WBS_CostControlParentDetail();
                //        unitProjectDetail.CostControlParentDetailId = SQLHelper.GetNewID();
                //        unitProjectDetail.ParentId = wbsSet.UnitProjectId;
                //        unitProjectDetail.Months = months;
                //        unitProjectDetail.StartDate = startDate;
                //        unitProjectDetail.EndDate = endDate;
                //        unitProjectDetail.ThisPlanValue = changeThisPlanValue;
                //        unitProjectDetail.ThisRealCost = changeThisRealCost;
                //        unitProjectDetail.ThisPlanCost = changeThisPlanCost;
                //        BLL.CostControlParentDetailService.AddCostControlParentDetail(unitProjectDetail);
                //    }
                //    //更新专业
                //    Model.WBS_CostControlParentDetail cnProfessionDetail = BLL.CostControlParentDetailService.GetCostControlParentDetailByParentIdAndMonthsWeek(wbsSet.CnProfessionId, months, startDate);
                //    if (cnProfessionDetail != null)
                //    {
                //        cnProfessionDetail.ThisPlanValue += changeThisPlanValue;
                //        cnProfessionDetail.ThisRealCost += changeThisRealCost;
                //        cnProfessionDetail.ThisPlanCost += changeThisPlanCost;
                //        BLL.CostControlParentDetailService.UpdateCostControlParentDetail(cnProfessionDetail);
                //    }
                //    else
                //    {
                //        cnProfessionDetail = new Model.WBS_CostControlParentDetail();
                //        cnProfessionDetail.CostControlParentDetailId = SQLHelper.GetNewID();
                //        cnProfessionDetail.ParentId = wbsSet.CnProfessionId;
                //        cnProfessionDetail.Months = months;
                //        cnProfessionDetail.StartDate = startDate;
                //        cnProfessionDetail.EndDate = endDate;
                //        cnProfessionDetail.ThisPlanValue = changeThisPlanValue;
                //        cnProfessionDetail.ThisRealCost = changeThisRealCost;
                //        cnProfessionDetail.ThisPlanCost = changeThisPlanCost;
                //        BLL.CostControlParentDetailService.AddCostControlParentDetail(cnProfessionDetail);
                //    }
                //    //更新装置
                //    UpdateWeekInstallationDetail(wbsSet.InstallationId, months, startDate, endDate, changeThisPlanValue, changeThisRealCost, changeThisPlanCost);
                //}
                //BindGrid();
                Alert.ShowInTop("保存成功!", MessageBoxIcon.Success);
            }
            else
            {
                Alert.ShowInTop("请选择工作项节点!", MessageBoxIcon.Warning);
            }
        }
        #endregion
        #region  更新工作包、工作项
        /// 
        /// 更新月工作包、工作项
        /// 
        /// 
        /// 
        /// 
        /// 
        private void UpdateWbsSetDetail(string wbsSetId, DateTime months, decimal changeThisPlanValue, decimal changeThisRealCost, decimal changeThisPlanCost)
        {
            Model.Wbs_WbsSet wbsSet = BLL.WbsSetService.GetWbsSetByWbsSetId(wbsSetId);
            if (wbsSet != null)
            {
                Model.WBS_CostControlParentDetail parentDetail = BLL.CostControlParentDetailService.GetCostControlParentDetailByParentIdAndMonths(wbsSetId, months);
                if (parentDetail != null)
                {
                    parentDetail.ThisPlanValue += changeThisPlanValue;
                    parentDetail.ThisRealCost += changeThisRealCost;
                    parentDetail.ThisPlanCost += changeThisPlanCost;
                    BLL.CostControlParentDetailService.UpdateCostControlParentDetail(parentDetail);
                }
                else
                {
                    parentDetail = new Model.WBS_CostControlParentDetail();
                    parentDetail.CostControlParentDetailId = SQLHelper.GetNewID();
                    parentDetail.ParentId = wbsSetId;
                    parentDetail.Months = months;
                    parentDetail.ThisPlanValue = changeThisPlanValue;
                    parentDetail.ThisRealCost = changeThisRealCost;
                    parentDetail.ThisPlanCost = changeThisPlanCost;
                    BLL.CostControlParentDetailService.AddCostControlParentDetail(parentDetail);
                }
                if (wbsSet.SuperWbsSetId != null)   //还存在上级节点,需要继续循环
                {
                    UpdateWbsSetDetail(wbsSet.SuperWbsSetId, months, changeThisPlanValue, changeThisRealCost, changeThisPlanCost);
                }
            }
        }
        /// 
        /// 更新周工作包、工作项
        /// 
        /// 
        /// 
        /// 
        /// 
        private void UpdateWeekWbsSetDetail(string wbsSetId, DateTime months, DateTime startDate, DateTime endDate, decimal changeThisPlanValue, decimal changeThisRealCost, decimal changeThisPlanCost)
        {
            Model.Wbs_WbsSet wbsSet = BLL.WbsSetService.GetWbsSetByWbsSetId(wbsSetId);
            if (wbsSet != null)
            {
                Model.WBS_CostControlParentDetail parentDetail = BLL.CostControlParentDetailService.GetCostControlParentDetailByParentIdAndMonthsWeek(wbsSetId, months, startDate);
                if (parentDetail != null)
                {
                    parentDetail.ThisPlanValue += changeThisPlanValue;
                    parentDetail.ThisRealCost += changeThisRealCost;
                    parentDetail.ThisPlanCost += changeThisPlanCost;
                    BLL.CostControlParentDetailService.UpdateCostControlParentDetail(parentDetail);
                }
                else
                {
                    parentDetail = new Model.WBS_CostControlParentDetail();
                    parentDetail.CostControlParentDetailId = SQLHelper.GetNewID();
                    parentDetail.ParentId = wbsSetId;
                    parentDetail.Months = months;
                    parentDetail.StartDate = startDate;
                    parentDetail.EndDate = endDate;
                    parentDetail.ThisPlanValue = changeThisPlanValue;
                    parentDetail.ThisRealCost = changeThisRealCost;
                    parentDetail.ThisPlanCost = changeThisPlanCost;
                    BLL.CostControlParentDetailService.AddCostControlParentDetail(parentDetail);
                }
                if (wbsSet.SuperWbsSetId != null)   //还存在上级节点,需要继续循环
                {
                    UpdateWeekWbsSetDetail(wbsSet.SuperWbsSetId, months, startDate, endDate, changeThisPlanValue, changeThisRealCost, changeThisPlanCost);
                }
            }
        }
        #endregion
        #region  更新装置
        /// 
        /// 更新月装置
        /// 
        /// 
        /// 
        /// 
        /// 
        private void UpdateInstallationDetail(string installationId, DateTime months, decimal changeThisPlanValue, decimal changeThisRealCost, decimal changeThisPlanCost)
        {
            Model.Project_Installation installation = BLL.Project_InstallationService.GetInstallationByInstallationId(installationId);
            if (installation != null)
            {
                Model.WBS_CostControlParentDetail parentDetail = BLL.CostControlParentDetailService.GetCostControlParentDetailByParentIdAndMonths(installationId, months);
                if (parentDetail != null)
                {
                    parentDetail.ThisPlanValue += changeThisPlanValue;
                    parentDetail.ThisRealCost += changeThisRealCost;
                    parentDetail.ThisPlanCost += changeThisPlanCost;
                    BLL.CostControlParentDetailService.UpdateCostControlParentDetail(parentDetail);
                }
                else
                {
                    parentDetail = new Model.WBS_CostControlParentDetail();
                    parentDetail.CostControlParentDetailId = SQLHelper.GetNewID();
                    parentDetail.ParentId = installationId;
                    parentDetail.Months = months;
                    parentDetail.ThisPlanValue = changeThisPlanValue;
                    parentDetail.ThisRealCost = changeThisRealCost;
                    parentDetail.ThisPlanCost = changeThisPlanCost;
                    BLL.CostControlParentDetailService.AddCostControlParentDetail(parentDetail);
                }
                if (installation.SuperInstallationId != null)   //还存在上级节点,需要继续循环
                {
                    UpdateInstallationDetail(installation.SuperInstallationId, months, changeThisPlanValue, changeThisRealCost, changeThisPlanCost);
                }
            }
        }
        /// 
        /// 更新周装置
        /// 
        /// 
        /// 
        /// 
        /// 
        private void UpdateWeekInstallationDetail(string installationId, DateTime months, DateTime startDate, DateTime endDate, decimal changeThisPlanValue, decimal changeThisRealCost, decimal changeThisPlanCost)
        {
            Model.Project_Installation installation = BLL.Project_InstallationService.GetInstallationByInstallationId(installationId);
            if (installation != null)
            {
                Model.WBS_CostControlParentDetail parentDetail = BLL.CostControlParentDetailService.GetCostControlParentDetailByParentIdAndMonths(installationId, months);
                if (parentDetail != null)
                {
                    parentDetail.ThisPlanValue += changeThisPlanValue;
                    parentDetail.ThisRealCost += changeThisRealCost;
                    parentDetail.ThisPlanCost += changeThisPlanCost;
                    BLL.CostControlParentDetailService.UpdateCostControlParentDetail(parentDetail);
                }
                else
                {
                    parentDetail = new Model.WBS_CostControlParentDetail();
                    parentDetail.CostControlParentDetailId = SQLHelper.GetNewID();
                    parentDetail.ParentId = installationId;
                    parentDetail.Months = months;
                    parentDetail.StartDate = startDate;
                    parentDetail.EndDate = endDate;
                    parentDetail.ThisPlanValue = changeThisPlanValue;
                    parentDetail.ThisRealCost = changeThisRealCost;
                    parentDetail.ThisPlanCost = changeThisPlanCost;
                    BLL.CostControlParentDetailService.AddCostControlParentDetail(parentDetail);
                }
                if (installation.SuperInstallationId != null)   //还存在上级节点,需要继续循环
                {
                    UpdateWeekInstallationDetail(installation.SuperInstallationId, months, startDate, endDate, changeThisPlanValue, changeThisRealCost, changeThisPlanCost);
                }
            }
        }
        #endregion
        #region 绑定数据
        private string upStartDate = string.Empty;
        private string upEndDate = string.Empty;
        protected void Grid1_RowDataBound(object sender, GridRowEventArgs e)
        {
            string id = e.RowID;
        }
        /// 
        /// 绑定数据
        /// 
        /// 
        /// 
        protected void Grid1_FilterChange(object sender, EventArgs e)
        {
            BindGrid();
        }
        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }
        /// 
        /// Grid1排序
        /// 
        /// 
        /// 
        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            Grid1.SortDirection = e.SortDirection;
            Grid1.SortField = e.SortField;
            BindGrid();
        }
        /// 
        /// 分页下拉选择事件
        /// 
        /// 
        /// 
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
            BindGrid();
        }
        /// 
        /// 加载Grid
        /// 
        private void BindGrid()
        {
            if (!string.IsNullOrEmpty(this.txtMonths.Text.Trim()))
            {
                DateTime months = Convert.ToDateTime(this.txtMonths.Text.Trim() + "-01");
                //var codeList = BLL.WbsSetMatchCostControlService.GetWbsSetMatchCostControls(this.trWBS.SelectedNodeID);
                //Model.Wbs_WbsSet wbeSet = BLL.WbsSetService.GetWbsSetByWbsSetId(this.trWBS.SelectedNodeID);
                //if (codeList.Count > 0)   //存在费控对应关系项
                //{
                //List codes = new List();
                //foreach (var item in codeList)
                //{
                //    codes.Add(item.CostControlCode);
                //}
                //var list = BLL.CostControlService.GetCostControlsByCostControlCodes(codes, this.trWBS.SelectedNodeID);
                var list = BLL.CostControlService.GetSelectedCostControlsByWbsSetId(this.trWBS.SelectedNodeID);
                if (list.Count > 0)
                {
                    List details = new List();
                    //if (this.drpWeek.SelectedValue == BLL.Const._Null)   //月份记录
                    //{
                    this.Grid1.Columns[4].HeaderText = "本月计划完成量";
                    this.Grid1.Columns[7].HeaderText = "本月完成量";
                    foreach (var item in list)
                    {
                        Model.View_WBS_CostControlDetail detail = BLL.CostControlDetailService.GetViewCostControlDetailByCostControlIdAndMonth(item.CostControlId, months);
                        if (detail == null)
                        {
                            detail = new Model.View_WBS_CostControlDetail();
                            detail.CostControlDetailId = SQLHelper.GetNewID();
                            detail.CostControlId = item.CostControlId;
                            detail.CostControlCode = item.CostControlCode;
                            detail.CostControlName = item.CostControlName;
                            detail.TotalNum = item.TotalNum;
                            detail.Unit = item.Unit;
                            detail.PlanNum = null;
                            detail.RealPrice = item.RealPrice;
                            detail.PlanPrice = item.PlanPrice;
                        }
                        if (detail.ThisNum == 0)
                        {
                            detail.ThisNum = null;
                        }
                        details.Add(detail);
                    }
                    //}
                    //else   //周记录
                    //{
                    //    this.Grid1.Columns[4].HeaderText = "本周计划完成量";
                    //    this.Grid1.Columns[7].HeaderText = "本周完成量";
                    //    foreach (var item in list)
                    //    {
                    //        if (item.TotalNum != null)
                    //        {
                    //            DateTime startDate = Convert.ToDateTime(this.drpWeek.SelectedValue.Split(',')[0]);
                    //            Model.View_WBS_CostControlDetail detail = BLL.CostControlDetailService.GetViewCostControlDetailByCostControlIdAndMonthWeek(item.CostControlId, months, startDate);
                    //            if (detail == null)
                    //            {
                    //                detail = new Model.View_WBS_CostControlDetail();
                    //                detail.CostControlDetailId = SQLHelper.GetNewID();
                    //                detail.CostControlId = item.CostControlId;
                    //                detail.CostControlCode = item.CostControlCode;
                    //                detail.CostControlName = item.CostControlName;
                    //                detail.TotalNum = item.TotalNum;
                    //                detail.Unit = item.Unit;
                    //                detail.PlanNum = null;
                    //                detail.RealPrice = item.RealPrice;
                    //                detail.PlanPrice = item.PlanPrice;
                    //            }
                    //            else
                    //            {
                    //                Model.WBS_CostControlDetail oldDetail = BLL.CostControlDetailService.GetCostControlDetailByCostControlDetailId(detail.CostControlDetailId);
                    //                if (oldDetail.PlanNum != null)
                    //                {
                    //                    detail.PlanNum = Convert.ToDouble(oldDetail.PlanNum);
                    //                }
                    //                else
                    //                {
                    //                    detail.PlanNum = null;
                    //                }
                    //                if (oldDetail.ThisNum != null)
                    //                {
                    //                    detail.ThisNum = Convert.ToDouble(oldDetail.ThisNum);
                    //                }
                    //                else
                    //                {
                    //                    detail.ThisNum = null;
                    //                }
                    //            }
                    //            details.Add(detail);
                    //        }
                    //    }
                    //}
                    Grid1.DataSource = details;
                    Grid1.DataBind();
                }
                else
                {
                    Grid1.DataSource = null;
                    Grid1.DataBind();
                }
            }
        }
        #endregion
        #region 获取按钮权限
        /// 
        /// 获取按钮权限
        /// 
        /// 
        /// 
        private void GetButtonPower()
        {
            if (Request.Params["value"] == "0")
            {
                return;
            }
            var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.WorkloadInputMenuId);
            if (buttonList.Count() > 0)
            {
                if (buttonList.Contains(BLL.Const.BtnSave))
                {
                    this.btnMenuEdit.Hidden = false;
                    this.btnEditAll.Hidden = false;
                    this.btnImport.Hidden = false;
                }
            }
        }
        #endregion
        #region 加载周计划下拉框
        protected void InitWeek()
        {
            if (!string.IsNullOrEmpty(this.txtMonths.Text.Trim()))
            {
                DateTime months = Convert.ToDateTime(this.txtMonths.Text.Trim() + "-01");
                string costControlId = string.Empty;
                var list = BLL.CostControlService.GetCostControlsByWbsSetId(this.trWBS.SelectedNodeID);
                if (list.Count > 0)
                {
                    foreach (var item in list)
                    {
                        if (item.TotalNum != null)
                        {
                            if (BLL.CostControlDetailService.IsExitWeekCostControlDetailByCostControlIdAndMonth(item.CostControlId, months))
                            {
                                //BLL.CostControlDetailService.InitWeekPlanList(this.drpWeek, item.CostControlId, months, true);
                                //this.drpWeek.SelectedValue = BLL.Const._Null;
                                break;
                            }
                        }
                    }
                }
            }
        }
        #endregion
        #region  月份选择事件
        /// 
        /// 月份选择事件
        /// 
        /// 
        /// 
        protected void txtMonths_TextChanged(object sender, EventArgs e)
        {
            BindGrid();
            //InitWeek();
        }
        #endregion
        protected void drpWeek_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGrid();
        }
        #region 全项目录入
        /// 
        /// 全项目录入按钮
        /// 
        /// 
        /// 
        protected void btnEditAll_Click(object sender, EventArgs e)
        {
            PageContext.RegisterStartupScript(Window2.GetShowReference(String.Format("WorkloadInputEditAll.aspx?ProjectId={0}", this.CurrUser.LoginProjectId, "导入 - ")));
        }
        #endregion
        #region 导入
        /// 
        /// 导入按钮
        /// 
        /// 
        /// 
        protected void btnImport_Click(object sender, EventArgs e)
        {
            PageContext.RegisterStartupScript(Window2.GetShowReference(String.Format("WorkloadInputIn.aspx?ProjectId={0}", this.CurrUser.LoginProjectId, "导入 - ")));
        }
        #endregion
    }
}