using BLL;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
namespace FineUIPro.Web.JDGL.WBSPlan
{
    public partial class WBSPlanSet : PageBase
    {
        #region 页面加载
        /// 
        /// 页面加载
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitTreeMenu();
            }
            else
            {
                if (GetRequestEventArgument() == "UPDATE_SUMMARY")
                {
                    // 页面要求重新计算合计行的值
                    OutputSummaryData();
                }
            }
        }
        #endregion
        #region 计算合计
        /// 
        /// 计算合计
        /// 
        private void OutputSummaryData()
        {
            decimal totalValue = 0, totalRate = 0, moneys = 0;
            int i = 0;
            JArray mergedData = Grid1.GetMergedData();
            if (mergedData.Count() > 0)
            {
                JObject firstValues = mergedData[0].Value("values");
                if (!string.IsNullOrEmpty(firstValues.Value("WeightsMoney")))
                {
                    moneys = Convert.ToDecimal(firstValues.Value("WeightsMoney"));
                    moneys = decimal.Round(moneys, 2);
                }
                foreach (JObject mergedRow in mergedData)
                {
                    JObject values = mergedRow.Value("values");
                    if (!string.IsNullOrEmpty(values.Value("PlanValue")))
                    {
                        totalValue += Convert.ToDecimal(values.Value("PlanValue"));
                    }
                    else
                    {
                        totalValue += 0;
                    }
                    if (moneys != 0)
                    {
                        totalRate = decimal.Round(totalValue / moneys * 100, 2);
                    }
                    else
                    {
                        totalRate = 0;
                    }
                    this.Grid1.Rows[i].Values[1] = values.Value("PlanValue");
                    this.Grid1.Rows[i].Values[2] = totalValue;
                    this.Grid1.Rows[i].Values[3] = totalRate;
                    i++;
                }
                JObject summary = new JObject();
                summary.Add("YearsMonthsStr", "未分配费用余额:");
                summary.Add("PlanValue", moneys - totalValue);
                Grid1.SummaryData = summary;               
            }
        }
        #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 installations = from x in Funs.DB.Project_Installation where x.ProjectId == this.CurrUser.LoginProjectId orderby x.InstallationCode select x;
            foreach (var installation in installations)
            {
                TreeNode rootNode = new TreeNode();
                rootNode.Text = installation.InstallationName;
                rootNode.NodeID = installation.InstallationId;
                rootNode.CommandName = "installation";
                rootNode.CommandArgument = installation.Weights == null ? null : installation.Weights.ToString();
                rootNode.ToolTip = installation.InstallationCode;
                rootNode.EnableExpandEvent = true;
                rootNode.EnableClickEvent = true;
                this.trWBS.Nodes.Add(rootNode);
                if (string.IsNullOrEmpty(Request.Params["ToWbs"]))   //非计划调整弹出页面
                {
                    TreeNode emptyNode = new TreeNode();
                    emptyNode.Text = "";
                    emptyNode.NodeID = "";
                    rootNode.Nodes.Add(emptyNode);
                }
                else   //导航弹出页面
                {
                    Model.Wbs_WbsSet wbsSetParm = BLL.WbsSetService.GetWbsSetByWbsSetId(Request.Params["ToWbs"]);
                    if (wbsSetParm != null)
                    {
                        string wbsSetParentId1 = string.Empty;
                        string wbsSetParentId2 = string.Empty;
                        string wbsSetParentId3 = string.Empty;
                        if (!string.IsNullOrEmpty(wbsSetParm.SuperWbsSetId))
                        {
                            wbsSetParentId1 = wbsSetParm.SuperWbsSetId;
                            Model.Wbs_WbsSet wbsSetParent1 = BLL.WbsSetService.GetWbsSetByWbsSetId(wbsSetParentId1);
                            if (wbsSetParent1 != null)
                            {
                                if (!string.IsNullOrEmpty(wbsSetParent1.SuperWbsSetId))
                                {
                                    wbsSetParentId2 = wbsSetParent1.SuperWbsSetId;
                                    Model.Wbs_WbsSet wbsSetParent2 = BLL.WbsSetService.GetWbsSetByWbsSetId(wbsSetParentId2);
                                    if (wbsSetParent2 != null)
                                    {
                                        if (!string.IsNullOrEmpty(wbsSetParent2.SuperWbsSetId))
                                        {
                                            wbsSetParentId3 = wbsSetParent2.SuperWbsSetId;
                                        }
                                    }
                                }
                            }
                        }
                        Model.Wbs_UnitProject unitProject1 = BLL.UnitProjectService.GetUnitProjectByUnitProjectId(wbsSetParm.UnitProjectId);
                        Model.WBS_CnProfession cnProfession1 = BLL.CnProfessionService.GetCnProfessionByCnProfessionId(wbsSetParm.CnProfessionId);
                        if (cnProfession1 != null)
                        {
                            if (cnProfession1.InstallationId == installation.InstallationId)
                            {
                                rootNode.Expanded = true;
                                var cnProfessions = from x in Funs.DB.WBS_CnProfession where x.InstallationId == cnProfession1.InstallationId orderby x.OldId select x;
                                foreach (var cnProfession in cnProfessions)
                                {
                                    var unitProjects1 = from x in Funs.DB.Wbs_UnitProject where x.CnProfessionId == cnProfession.CnProfessionId && x.IsApprove == true select x;
                                    if (unitProjects1.Count() > 0)
                                    {
                                        TreeNode newCnProfessionNode = new TreeNode();
                                        newCnProfessionNode.Text = cnProfession.CnProfessionName;
                                        newCnProfessionNode.NodeID = cnProfession.CnProfessionId;
                                        newCnProfessionNode.CommandName = "cnProfession";
                                        newCnProfessionNode.CommandArgument = cnProfession.Weights == null ? null : cnProfession.Weights.ToString();
                                        newCnProfessionNode.ToolTip = cnProfession.CnProfessionCode;
                                        newCnProfessionNode.EnableExpandEvent = true;
                                        newCnProfessionNode.EnableClickEvent = true;
                                        rootNode.Nodes.Add(newCnProfessionNode);
                                        if (cnProfession.CnProfessionId != cnProfession1.CnProfessionId)
                                        {
                                            TreeNode emptyNode = new TreeNode();
                                            emptyNode.Text = "";
                                            emptyNode.NodeID = "";
                                            newCnProfessionNode.Nodes.Add(emptyNode);
                                        }
                                        else
                                        {
                                            newCnProfessionNode.Expanded = true;
                                            var unitProjects = from x in Funs.DB.Wbs_UnitProject where x.CnProfessionId == cnProfession1.CnProfessionId && x.SuperUnitProjectId == null && x.IsApprove == true orderby x.UnitProjectCode select x;
                                            foreach (var unitProject in unitProjects)
                                            {
                                                TreeNode newUnitProjectNode = new TreeNode();
                                                newUnitProjectNode.Text = unitProject.UnitProjectName;
                                                newUnitProjectNode.NodeID = unitProject.UnitProjectId;
                                                newUnitProjectNode.CommandName = "unitProject";
                                                newUnitProjectNode.CommandArgument = unitProject.Weights == null ? null : unitProject.Weights.ToString();
                                                newUnitProjectNode.ToolTip = unitProject.UnitProjectCode;
                                                newUnitProjectNode.EnableExpandEvent = true;
                                                newUnitProjectNode.EnableClickEvent = true;
                                                newCnProfessionNode.Nodes.Add(newUnitProjectNode);
                                                if (unitProject1.SuperUnitProjectId == null)   //不存在子单位工程
                                                {
                                                    if (unitProject1.UnitProjectId != unitProject.UnitProjectId)
                                                    {
                                                        TreeNode emptyNode = new TreeNode();
                                                        emptyNode.Text = "";
                                                        emptyNode.NodeID = "";
                                                        newUnitProjectNode.Nodes.Add(emptyNode);
                                                    }
                                                    else
                                                    {
                                                        newUnitProjectNode.Expanded = true;
                                                        var wbsSet1s = from x in Funs.DB.Wbs_WbsSet where x.Flag == 1 && x.UnitProjectId == unitProject1.UnitProjectId && x.IsApprove == true orderby x.WbsSetCode select x;
                                                        if (wbsSet1s.Count() > 0)
                                                        {
                                                            foreach (var wbsSet1 in wbsSet1s)
                                                            {
                                                                TreeNode newwbsSet1Node = new TreeNode();
                                                                newwbsSet1Node.Text = wbsSet1.WbsSetName;
                                                                newwbsSet1Node.NodeID = wbsSet1.WbsSetId;
                                                                newwbsSet1Node.CommandName = "wbsSet";
                                                                newwbsSet1Node.CommandArgument = wbsSet1.Weights == null ? null : wbsSet1.Weights.ToString();
                                                                newwbsSet1Node.ToolTip = wbsSet1.WbsSetCode;
                                                                newwbsSet1Node.EnableExpandEvent = true;
                                                                newwbsSet1Node.EnableClickEvent = true;
                                                                newUnitProjectNode.Nodes.Add(newwbsSet1Node);
                                                                var childWbsSets2 = BLL.WbsSetService.GetApproveWbsSetsBySuperWbsSetId(newwbsSet1Node.NodeID);
                                                                if (childWbsSets2.Count() > 0)
                                                                {
                                                                    if (BLL.WbsSetService.IsExitWbsSetById(wbsSet1.WbsSetId, wbsSetParm.WbsSetId))
                                                                    {
                                                                        newwbsSet1Node.Expanded = true;
                                                                        foreach (var wbsSet2 in childWbsSets2)
                                                                        {
                                                                            TreeNode newwbsSet2Node = new TreeNode();
                                                                            newwbsSet2Node.Text = wbsSet2.WbsSetName;
                                                                            newwbsSet2Node.NodeID = wbsSet2.WbsSetId;
                                                                            newwbsSet2Node.CommandName = "wbsSet";
                                                                            newwbsSet2Node.CommandArgument = wbsSet2.Weights == null ? null : wbsSet2.Weights.ToString();
                                                                            newwbsSet2Node.ToolTip = wbsSet2.WbsSetCode;
                                                                            newwbsSet2Node.EnableExpandEvent = true;
                                                                            newwbsSet2Node.EnableClickEvent = true;
                                                                            newwbsSet1Node.Nodes.Add(newwbsSet2Node);
                                                                            var childWbsSets3 = BLL.WbsSetService.GetApproveWbsSetsBySuperWbsSetId(newwbsSet2Node.NodeID);
                                                                            if (BLL.WbsSetService.IsExitWbsSetById(wbsSet2.WbsSetId, wbsSetParm.WbsSetId))
                                                                            {
                                                                                newwbsSet2Node.Expanded = true;
                                                                                if (childWbsSets3.Count() > 0)
                                                                                {
                                                                                    foreach (var wbsSet3 in childWbsSets3)
                                                                                    {
                                                                                        TreeNode newwbsSet3Node = new TreeNode();
                                                                                        newwbsSet3Node.Text = wbsSet3.WbsSetName;
                                                                                        newwbsSet3Node.NodeID = wbsSet3.WbsSetId;
                                                                                        newwbsSet3Node.CommandName = "wbsSet";
                                                                                        newwbsSet3Node.CommandArgument = wbsSet3.Weights == null ? null : wbsSet3.Weights.ToString();
                                                                                        newwbsSet3Node.ToolTip = wbsSet3.WbsSetCode;
                                                                                        newwbsSet3Node.EnableExpandEvent = true;
                                                                                        newwbsSet3Node.EnableClickEvent = true;
                                                                                        newwbsSet2Node.Nodes.Add(newwbsSet3Node);
                                                                                        var childWbsSets4 = BLL.WbsSetService.GetApproveWbsSetsBySuperWbsSetId(newwbsSet3Node.NodeID);
                                                                                        if (BLL.WbsSetService.IsExitWbsSetById(wbsSet3.WbsSetId, wbsSetParm.WbsSetId))
                                                                                        {
                                                                                            newwbsSet3Node.Expanded = true;
                                                                                            if (childWbsSets4.Count() > 0)
                                                                                            {
                                                                                                foreach (var wbsSet4 in childWbsSets4)
                                                                                                {
                                                                                                    TreeNode newwbsSet4Node = new TreeNode();
                                                                                                    newwbsSet4Node.Text = wbsSet4.WbsSetName;
                                                                                                    newwbsSet4Node.NodeID = wbsSet4.WbsSetId;
                                                                                                    newwbsSet4Node.CommandName = "wbsSet";
                                                                                                    newwbsSet4Node.CommandArgument = wbsSet4.Weights == null ? null : wbsSet4.Weights.ToString();
                                                                                                    newwbsSet4Node.ToolTip = wbsSet4.WbsSetCode;
                                                                                                    newwbsSet4Node.EnableExpandEvent = true;
                                                                                                    newwbsSet4Node.EnableClickEvent = true;
                                                                                                    newwbsSet3Node.Nodes.Add(newwbsSet4Node);
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                        else
                                                                                        {
                                                                                            if (childWbsSets4.Count() > 0)
                                                                                            {
                                                                                                TreeNode emptyNode = new TreeNode();
                                                                                                emptyNode.Text = "";
                                                                                                emptyNode.NodeID = "";
                                                                                                newwbsSet3Node.Nodes.Add(emptyNode);
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                            else
                                                                            {
                                                                                if (childWbsSets3.Count() > 0)
                                                                                {
                                                                                    TreeNode emptyNode = new TreeNode();
                                                                                    emptyNode.Text = "";
                                                                                    emptyNode.NodeID = "";
                                                                                    newwbsSet2Node.Nodes.Add(emptyNode);
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                    else
                                                                    {
                                                                        TreeNode emptyNode = new TreeNode();
                                                                        emptyNode.Text = "";
                                                                        emptyNode.NodeID = "";
                                                                        newwbsSet1Node.Nodes.Add(emptyNode);
                                                                    }
                                                                }
                                                            }
                                                        }
                                                        else    //单位工程下直接是分项内容,如质量行为
                                                        {
                                                            var wbsSet3s = from x in Funs.DB.Wbs_WbsSet where x.Flag == 3 && x.UnitProjectId == unitProject1.UnitProjectId && x.IsApprove == true orderby x.WbsSetCode select x;
                                                            if (wbsSet3s.Count() > 0)
                                                            {
                                                                foreach (var wbsSet3 in wbsSet3s)
                                                                {
                                                                    TreeNode newwbsSet3Node = new TreeNode();
                                                                    newwbsSet3Node.Text = wbsSet3.WbsSetName;
                                                                    newwbsSet3Node.NodeID = wbsSet3.WbsSetId;
                                                                    newwbsSet3Node.CommandName = "wbsSet";
                                                                    newwbsSet3Node.CommandArgument = wbsSet3.Weights == null ? null : wbsSet3.Weights.ToString();
                                                                    newwbsSet3Node.ToolTip = wbsSet3.WbsSetCode;
                                                                    newwbsSet3Node.EnableExpandEvent = true;
                                                                    newwbsSet3Node.EnableClickEvent = true;
                                                                    newUnitProjectNode.Nodes.Add(newwbsSet3Node);
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                                else   //存在子单位工程
                                                {
                                                    if (unitProject1.SuperUnitProjectId != unitProject.UnitProjectId)
                                                    {
                                                        TreeNode emptyNode = new TreeNode();
                                                        emptyNode.Text = "";
                                                        emptyNode.NodeID = "";
                                                        newUnitProjectNode.Nodes.Add(emptyNode);
                                                    }
                                                    else
                                                    {
                                                        newUnitProjectNode.Expanded = true;
                                                        var childUnitProjects = BLL.UnitProjectService.GetUnitProjectsBySuperUnitProjectId(newUnitProjectNode.NodeID);
                                                        foreach (var childUnitProject in childUnitProjects)
                                                        {
                                                            TreeNode newChildUnitProjectNode = new TreeNode();
                                                            newChildUnitProjectNode.Text = childUnitProject.UnitProjectName;
                                                            newChildUnitProjectNode.NodeID = childUnitProject.UnitProjectId;
                                                            newChildUnitProjectNode.CommandName = "childUnitProject";
                                                            newChildUnitProjectNode.CommandArgument = childUnitProject.Weights == null ? null : childUnitProject.Weights.ToString();
                                                            newChildUnitProjectNode.ToolTip = childUnitProject.UnitProjectCode;
                                                            newChildUnitProjectNode.EnableExpandEvent = true;
                                                            newChildUnitProjectNode.EnableClickEvent = true;
                                                            newUnitProjectNode.Nodes.Add(newChildUnitProjectNode);
                                                            if (unitProject1.UnitProjectId != childUnitProject.UnitProjectId)
                                                            {
                                                                TreeNode emptyNode = new TreeNode();
                                                                emptyNode.Text = "";
                                                                emptyNode.NodeID = "";
                                                                newChildUnitProjectNode.Nodes.Add(emptyNode);
                                                            }
                                                            else
                                                            {
                                                                newChildUnitProjectNode.Expanded = true;
                                                                var wbsSet1s = from x in Funs.DB.Wbs_WbsSet where x.Flag == 1 && x.UnitProjectId == unitProject1.UnitProjectId && x.IsApprove == true orderby x.WbsSetCode select x;
                                                                foreach (var wbsSet1 in wbsSet1s)
                                                                {
                                                                    TreeNode newwbsSet1Node = new TreeNode();
                                                                    newwbsSet1Node.Text = wbsSet1.WbsSetName;
                                                                    newwbsSet1Node.NodeID = wbsSet1.WbsSetId;
                                                                    newwbsSet1Node.CommandName = "wbsSet";
                                                                    newwbsSet1Node.CommandArgument = wbsSet1.Weights == null ? null : wbsSet1.Weights.ToString();
                                                                    newwbsSet1Node.ToolTip = wbsSet1.WbsSetCode;
                                                                    newwbsSet1Node.EnableExpandEvent = true;
                                                                    newwbsSet1Node.EnableClickEvent = true;
                                                                    newChildUnitProjectNode.Nodes.Add(newwbsSet1Node);
                                                                    var childWbsSets2 = BLL.WbsSetService.GetApproveWbsSetsBySuperWbsSetId(newwbsSet1Node.NodeID);
                                                                    if (childWbsSets2.Count() > 0)
                                                                    {
                                                                        if (BLL.WbsSetService.IsExitWbsSetById(wbsSet1.WbsSetId, wbsSetParm.WbsSetId))
                                                                        {
                                                                            newwbsSet1Node.Expanded = true;
                                                                            foreach (var wbsSet2 in childWbsSets2)
                                                                            {
                                                                                TreeNode newwbsSet2Node = new TreeNode();
                                                                                newwbsSet2Node.Text = wbsSet2.WbsSetName;
                                                                                newwbsSet2Node.NodeID = wbsSet2.WbsSetId;
                                                                                newwbsSet2Node.CommandName = "wbsSet";
                                                                                newwbsSet2Node.CommandArgument = wbsSet2.Weights == null ? null : wbsSet2.Weights.ToString();
                                                                                newwbsSet2Node.ToolTip = wbsSet2.WbsSetCode;
                                                                                newwbsSet2Node.EnableExpandEvent = true;
                                                                                newwbsSet2Node.EnableClickEvent = true;
                                                                                newwbsSet1Node.Nodes.Add(newwbsSet2Node);
                                                                                var childWbsSets3 = BLL.WbsSetService.GetApproveWbsSetsBySuperWbsSetId(newwbsSet2Node.NodeID);
                                                                                if (BLL.WbsSetService.IsExitWbsSetById(wbsSet2.WbsSetId, wbsSetParm.WbsSetId))
                                                                                {
                                                                                    newwbsSet2Node.Expanded = true;
                                                                                    if (childWbsSets3.Count() > 0)
                                                                                    {
                                                                                        foreach (var wbsSet3 in childWbsSets3)
                                                                                        {
                                                                                            TreeNode newwbsSet3Node = new TreeNode();
                                                                                            newwbsSet3Node.Text = wbsSet3.WbsSetName;
                                                                                            newwbsSet3Node.NodeID = wbsSet3.WbsSetId;
                                                                                            newwbsSet3Node.CommandName = "wbsSet";
                                                                                            newwbsSet3Node.CommandArgument = wbsSet3.Weights == null ? null : wbsSet3.Weights.ToString();
                                                                                            newwbsSet3Node.ToolTip = wbsSet3.WbsSetCode;
                                                                                            newwbsSet3Node.EnableExpandEvent = true;
                                                                                            newwbsSet3Node.EnableClickEvent = true;
                                                                                            newwbsSet2Node.Nodes.Add(newwbsSet3Node);
                                                                                            var childWbsSets4 = BLL.WbsSetService.GetApproveWbsSetsBySuperWbsSetId(newwbsSet3Node.NodeID);
                                                                                            if (BLL.WbsSetService.IsExitWbsSetById(wbsSet3.WbsSetId, wbsSetParm.WbsSetId))
                                                                                            {
                                                                                                newwbsSet3Node.Expanded = true;
                                                                                                if (childWbsSets4.Count() > 0)
                                                                                                {
                                                                                                    foreach (var wbsSet4 in childWbsSets4)
                                                                                                    {
                                                                                                        TreeNode newwbsSet4Node = new TreeNode();
                                                                                                        newwbsSet4Node.Text = wbsSet4.WbsSetName;
                                                                                                        newwbsSet4Node.NodeID = wbsSet4.WbsSetId;
                                                                                                        newwbsSet4Node.CommandName = "wbsSet";
                                                                                                        newwbsSet4Node.CommandArgument = wbsSet4.Weights == null ? null : wbsSet4.Weights.ToString();
                                                                                                        newwbsSet4Node.ToolTip = wbsSet4.WbsSetCode;
                                                                                                        newwbsSet4Node.EnableExpandEvent = true;
                                                                                                        newwbsSet4Node.EnableClickEvent = true;
                                                                                                        newwbsSet3Node.Nodes.Add(newwbsSet4Node);
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                            else
                                                                                            {
                                                                                                if (childWbsSets4.Count() > 0)
                                                                                                {
                                                                                                    TreeNode emptyNode = new TreeNode();
                                                                                                    emptyNode.Text = "";
                                                                                                    emptyNode.NodeID = "";
                                                                                                    newwbsSet3Node.Nodes.Add(emptyNode);
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                                else
                                                                                {
                                                                                    if (childWbsSets3.Count() > 0)
                                                                                    {
                                                                                        TreeNode emptyNode = new TreeNode();
                                                                                        emptyNode.Text = "";
                                                                                        emptyNode.NodeID = "";
                                                                                        newwbsSet2Node.Nodes.Add(emptyNode);
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                        else
                                                                        {
                                                                            TreeNode emptyNode = new TreeNode();
                                                                            emptyNode.Text = "";
                                                                            emptyNode.NodeID = "";
                                                                            newwbsSet1Node.Nodes.Add(emptyNode);
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        this.trWBS.SelectedNodeID = wbsSetParm.WbsSetId;
                        this.GetGridTitle();
                        BindGrid();
                        OutputSummaryData();
                    }
                }
            }
        }
        #endregion
        #region 展开树
        /// 
        /// 展开树
        /// 
        /// 
        /// 
        protected void trWBS_NodeExpand(object sender, TreeNodeEventArgs e)
        {
            e.Node.Nodes.Clear();
            if (e.Node.CommandName == "installation")  //展开装置/单元节点
            {
                var cnProfessions = from x in Funs.DB.WBS_CnProfession where x.InstallationId == e.Node.NodeID orderby x.OldId select x;
                foreach (var cnProfession in cnProfessions)
                {
                    var unitProjects = from x in Funs.DB.Wbs_UnitProject where x.CnProfessionId == cnProfession.CnProfessionId && x.IsApprove == true select x;
                    if (unitProjects.Count() > 0)
                    {
                        TreeNode newNode = new TreeNode();
                        newNode.Text = cnProfession.CnProfessionName;
                        newNode.NodeID = cnProfession.CnProfessionId;
                        newNode.CommandName = "cnProfession";
                        newNode.CommandArgument = cnProfession.Weights == null ? null : cnProfession.Weights.ToString();
                        newNode.ToolTip = cnProfession.CnProfessionCode;
                        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 == "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.CommandArgument = unitProject.Weights == null ? null : unitProject.Weights.ToString();
                    newNode.ToolTip = unitProject.UnitProjectCode;
                    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 == "unitProject")   //展开单位工程节点
            {
                var childUnitProjects = from x in Funs.DB.Wbs_UnitProject where x.SuperUnitProjectId == e.Node.NodeID && x.IsApprove == true orderby x.SortIndex, x.UnitProjectCode select x;
                if (childUnitProjects.Count() > 0)   //存在子单位工程
                {
                    foreach (var childUnitProject in childUnitProjects)
                    {
                        TreeNode newNode = new TreeNode();
                        newNode.Text = childUnitProject.UnitProjectName;
                        newNode.NodeID = childUnitProject.UnitProjectId;
                        newNode.CommandName = "childUnitProject";
                        newNode.CommandArgument = childUnitProject.Weights == null ? null : childUnitProject.Weights.ToString();
                        newNode.ToolTip = childUnitProject.UnitProjectCode;
                        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 wbsSet1s = from x in Funs.DB.Wbs_WbsSet where x.Flag == 1 && x.UnitProjectId == e.Node.NodeID && x.IsApprove == true orderby x.SortIndex, 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.CommandArgument = wbsSet1.Weights == null ? null : wbsSet1.Weights.ToString();
                            newNode.ToolTip = wbsSet1.WbsSetCode;
                            newNode.EnableExpandEvent = true;
                            newNode.EnableClickEvent = true;
                            e.Node.Nodes.Add(newNode);
                            var childWbsSets2 = BLL.WbsSetService.GetApproveWbsSetsBySuperWbsSetId(newNode.NodeID);
                            if (childWbsSets2.Count() > 0)
                            {
                                TreeNode emptyNode = new TreeNode();
                                emptyNode.Text = "";
                                emptyNode.NodeID = "";
                                newNode.Nodes.Add(emptyNode);
                            }
                        }
                    }
                    else    //单位工程下直接是分项内容,如质量行为
                    {
                        var wbsSet3s = from x in Funs.DB.Wbs_WbsSet where x.Flag == 3 && x.UnitProjectId == e.Node.NodeID && x.IsApprove == true orderby x.SortIndex, x.WbsSetCode select x;
                        if (wbsSet3s.Count() > 0)
                        {
                            foreach (var wbsSet3 in wbsSet3s)
                            {
                                TreeNode newNode = new TreeNode();
                                newNode.Text = wbsSet3.WbsSetName;
                                newNode.NodeID = wbsSet3.WbsSetId;
                                newNode.CommandName = "wbsSet";
                                newNode.CommandArgument = wbsSet3.Weights == null ? null : wbsSet3.Weights.ToString();
                                newNode.ToolTip = wbsSet3.WbsSetCode;
                                newNode.EnableExpandEvent = true;
                                newNode.EnableClickEvent = true;
                                e.Node.Nodes.Add(newNode);
                                var childWbsSets2 = BLL.WbsSetService.GetApproveWbsSetsBySuperWbsSetId(newNode.NodeID);
                                if (childWbsSets2.Count() > 0)
                                {
                                    TreeNode emptyNode = new TreeNode();
                                    emptyNode.Text = "";
                                    emptyNode.NodeID = "";
                                    newNode.Nodes.Add(emptyNode);
                                }
                            }
                        }
                    }
                }
            }
            else if (e.Node.CommandName == "childUnitProject")   //展开子单位工程节点
            {
                var wbsSet1s = from x in Funs.DB.Wbs_WbsSet where x.Flag == 1 && x.UnitProjectId == e.Node.NodeID && x.IsApprove == true orderby x.SortIndex, x.WbsSetCode select x;
                foreach (var wbsSet1 in wbsSet1s)
                {
                    TreeNode newNode = new TreeNode();
                    newNode.Text = wbsSet1.WbsSetName;
                    newNode.NodeID = wbsSet1.WbsSetId;
                    newNode.CommandName = "wbsSet";
                    newNode.CommandArgument = wbsSet1.Weights == null ? null : wbsSet1.Weights.ToString();
                    newNode.ToolTip = wbsSet1.WbsSetCode;
                    newNode.EnableExpandEvent = true;
                    newNode.EnableClickEvent = true;
                    e.Node.Nodes.Add(newNode);
                    var childWbsSets2 = BLL.WbsSetService.GetApproveWbsSetsBySuperWbsSetId(newNode.NodeID);
                    if (childWbsSets2.Count() > 0)
                    {
                        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.CommandArgument = wbsSet.Weights == null ? null : wbsSet.Weights.ToString();
                    newNode.ToolTip = wbsSet.WbsSetCode;
                    newNode.EnableExpandEvent = true;
                    newNode.EnableClickEvent = true;
                    e.Node.Nodes.Add(newNode);
                    var childWbsSets2 = BLL.WbsSetService.GetApproveWbsSetsBySuperWbsSetId(newNode.NodeID);
                    if (childWbsSets2.Count() > 0)
                    {
                        TreeNode emptyNode = new TreeNode();
                        emptyNode.Text = "";
                        emptyNode.NodeID = "";
                        newNode.Nodes.Add(emptyNode);
                    }
                }
            }
        }
        #endregion
        #region 获取Grid表头
        /// 
        /// 获取Grid表头
        /// 
        private void GetGridTitle()
        {
            this.Grid1.Title = string.Empty;
            string title = string.Empty;
            string nodeTooltip = string.Empty;
            string nodeText = string.Empty;
            TreeNode parent1 = new TreeNode();
            if (this.trWBS.SelectedNode != null)
            {
                nodeTooltip = this.trWBS.SelectedNode.ToolTip;
                nodeText = this.trWBS.SelectedNode.Text;
                parent1 = this.trWBS.SelectedNode.ParentNode;
            }
            if (parent1 != null)
            {
                TreeNode parent2 = parent1.ParentNode;
                if (parent2 != null)
                {
                    TreeNode parent3 = parent2.ParentNode;
                    if (parent3 != null)
                    {
                        TreeNode parent4 = parent3.ParentNode;
                        if (parent4 != null)
                        {
                            TreeNode parent5 = parent4.ParentNode;
                            if (parent5 != null)
                            {
                                TreeNode parent6 = parent5.ParentNode;
                                if (parent6 != null)
                                {
                                    title = parent6.ToolTip + "-" + parent5.ToolTip + "-" + nodeTooltip + "/" + parent6.Text + "-" + parent5.Text + "-" + parent4.Text + "-" + parent3.Text + "-" + parent2.Text + "-" + parent1.Text + "-" + nodeText;
                                }
                                else
                                {
                                    if (parent5.CommandName == "installation")
                                    {
                                        title = parent5.ToolTip + "-" + parent4.ToolTip + "-" + nodeTooltip + "/" + parent5.Text + "-" + parent4.Text + "-" + parent3.Text + "-" + parent2.Text + "-" + parent1.Text + "-" + nodeText;
                                    }
                                }
                            }
                            else
                            {
                                if (parent4.CommandName == "installation")
                                {
                                    title = parent4.ToolTip + "-" + parent3.ToolTip + "-" + nodeTooltip + "/" + parent4.Text + "-" + parent3.Text + "-" + parent2.Text + "-" + parent1.Text + "-" + nodeText;
                                }
                            }
                        }
                        else
                        {
                            if (parent3.CommandName == "installation")
                            {
                                title = parent3.ToolTip + "-" + parent2.ToolTip + "-" + nodeTooltip + "/" + parent3.Text + "-" + parent2.Text + "-" + parent1.Text + "-" + nodeText;
                            }
                        }
                    }
                    else
                    {
                        if (parent2.CommandName == "installation")
                        {
                            title = parent2.ToolTip + "-" + parent1.ToolTip + "-" + nodeTooltip + "/" + parent2.Text + "-" + parent1.Text + "-" + nodeText;
                        }
                    }
                }
                else
                {
                    if (parent1.CommandName == "installation")
                    {
                        title = parent1.ToolTip + "-" + nodeTooltip + "/" + parent1.Text + "-" + nodeText;
                    }
                }
            }
            else
            {
                title = nodeTooltip + "/" + nodeText;
            }
            this.Grid1.Title = title;
        }
        #endregion
        #region Tree点击事件
        /// 
        /// Tree点击事件
        /// 
        /// 
        /// 
        protected void trWBS_NodeCommand(object sender, TreeCommandEventArgs e)
        {
            GetGridTitle();
            BindGrid();
            OutputSummaryData();
        }
        #endregion
        #region 保存方法
        /// 
        /// 保存方法
        /// 
        /// 
        private void SaveData(string message)
        {
            decimal totalValue = 0, moneys = 0;
            JArray mergedData = Grid1.GetMergedData();
            JObject firstValues = mergedData[0].Value("values");
            if (!string.IsNullOrEmpty(firstValues.Value("WeightsMoney")))
            {
                moneys = Convert.ToDecimal(firstValues.Value("WeightsMoney"));
            }
            foreach (JObject mergedRow in mergedData)
            {
                JObject values = mergedRow.Value("values");
                if (!string.IsNullOrEmpty(values.Value("PlanValue")))
                {
                    totalValue += Convert.ToDecimal(values.Value("PlanValue"));
                }
                else
                {
                    totalValue += 0;
                }
            }
            if (moneys != totalValue)
            {
                Alert.ShowInTop("金额分配不符合要求!", MessageBoxIcon.Warning);
                return;
            }
            Model.Wbs_WbsSet wbsSet = BLL.WbsSetService.GetWbsSetByWbsSetId(this.trWBS.SelectedNodeID);
            if (wbsSet != null)
            {
                if (wbsSet.IsPlanApprove == true)
                {
                    wbsSet.IsPlanApprove = null;
                    BLL.WbsSetService.UpdateWbsSet(wbsSet);
                    btnSave.ConfirmText = string.Empty;
                    var details = from x in Funs.DB.WbsDetail where x.ToWbs == wbsSet.WbsSetId select x;
                    if (details.Count() > 0)
                    {
                        foreach (var detail in details)
                        {
                            Model.WbsDetailHistory detailHistory = new Model.WbsDetailHistory();
                            detailHistory.WbsDetailHistoryId = SQLHelper.GetNewID(typeof(Model.WbsDetailHistory));
                            detailHistory.ToWbs = detail.ToWbs;
                            detailHistory.ToFlag = detail.ToFlag;
                            detailHistory.Way = detail.Way;
                            detailHistory.Months = detail.Months;
                            detailHistory.PlanValue = detail.PlanValue;
                            detailHistory.PlanValueRate = detail.PlanValueRate;
                            detailHistory.VersionNum = wbsSet.VersionNum;
                            BLL.WbsDetailHistoryService.AddWbsDetailHistory(detailHistory);
                        }
                    }
                }
            }
            string type = this.trWBS.SelectedNode.CommandName;
            if (type == "wbsSet")
            {
                var childWbsSets = BLL.WbsSetService.GetApproveWbsSetsBySuperWbsSetId(this.trWBS.SelectedNodeID);
                if (childWbsSets.Count == 0)   //当前为末级
                {
                    foreach (JObject mergedRow in mergedData)
                    {
                        Model.WbsDetail detail = new Model.WbsDetail();
                        JObject values = mergedRow.Value("values");
                        detail.WbsDetailId = values.Value("WbsDetailId");
                        var oldDetail = BLL.WbsDetailService.GetWbsDetailByWbsDetailId(detail.WbsDetailId);
                        detail.ToWbs = values.Value("ToWbs");
                        detail.ToFlag = Convert.ToInt32(values.Value("ToFlag"));
                        detail.Way = values.Value("Way");
                        detail.Months = Convert.ToDateTime(values.Value("Months"));
                        if (!string.IsNullOrEmpty(values.Value("PlanValue")))
                        {
                            detail.PlanValue = Convert.ToDecimal(values.Value("PlanValue"));
                            oldDetail.PlanValue = Convert.ToDecimal(values.Value("PlanValue"));
                        }
                        else
                        {
                            detail.PlanValue = 0;
                            oldDetail.PlanValue = 0;
                        }
                        if (moneys != 0)
                        {
                            detail.PlanValueRate = decimal.Round(Convert.ToDecimal(detail.PlanValue) / moneys * 100, 2);
                            oldDetail.PlanValueRate = detail.PlanValueRate;
                        }
                        else
                        {
                            detail.PlanValueRate = 0;
                            oldDetail.PlanValueRate = 0;
                        }
                        if (oldDetail == null)
                        {
                            BLL.WbsDetailService.AddWbsDetail(detail);
                        }
                        else
                        {
                            Funs.DB.SubmitChanges();
                        }
                    }
                }
            }
            //BLL.Sys_LogService.AddLog(BLL.Const.System_9, this.CurrUser.LoginProjectId, this.CurrUser.UserId, "设置计划值");
            if (!string.IsNullOrEmpty(Request.Params["ToWbs"]))
            {
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
            }
            else
            {
                if (!string.IsNullOrEmpty(message))
                {
                    ShowNotify("保存成功!", MessageBoxIcon.Success);
                }
            }
        }
        #endregion
        #region 保存方法2(用于展开末级项时的自动保存)
        /// 
        /// 保存方法2(用于展开末级项时的自动保存)
        /// 
        /// 
        private void SaveData2()
        {
            Model.Wbs_WbsSet wbsSet = BLL.WbsSetService.GetWbsSetByWbsSetId(this.trWBS.SelectedNodeID);
            for (int i = 0; i < this.Grid1.Rows.Count; i++)
            {
                string type = this.trWBS.SelectedNode.CommandName;
                if (type == "wbsSet")
                {
                    var childWbsSets = BLL.WbsSetService.GetApproveWbsSetsBySuperWbsSetId(this.trWBS.SelectedNodeID);
                    if (childWbsSets.Count == 0)   //当前为末级
                    {
                        Model.WbsDetail detail = new Model.WbsDetail();
                        detail.WbsDetailId = this.Grid1.Rows[i].Values[4].ToString();
                        detail.ToWbs = this.Grid1.Rows[i].Values[5].ToString();
                        detail.ToFlag = Convert.ToInt32(this.Grid1.Rows[i].Values[6].ToString());
                        detail.Way = this.Grid1.Rows[i].Values[7].ToString();
                        detail.Months = Convert.ToDateTime(this.Grid1.Rows[i].Values[9].ToString());
                        if (!string.IsNullOrEmpty(this.Grid1.Rows[i].Values[1].ToString()) && this.Grid1.Rows[i].Values[10].ToString() != "0")
                        {
                            detail.PlanValue = Convert.ToDecimal(this.Grid1.Rows[i].Values[1].ToString());
                            detail.PlanValueRate = decimal.Round(Convert.ToDecimal(detail.PlanValue) / Convert.ToDecimal(this.Grid1.Rows[i].Values[10].ToString()) * 100, 2);
                        }
                        var oldDetail = BLL.WbsDetailService.GetWbsDetailByWbsDetailId(detail.WbsDetailId);
                        if (oldDetail == null)
                        {
                            BLL.WbsDetailService.AddWbsDetail(detail);
                        }
                    }
                }
            }
            //BLL.Sys_LogService.AddLog(BLL.Const.System_9, this.CurrUser.LoginProjectId, this.CurrUser.UserId, "设置计划值");
        }
        #endregion
        #region  增加分部分项
        /// 
        /// 增加分部分项
        /// 
        /// 
        /// 
        /// 
        /// 
        private void AddWbsParentDetail(DateTime months, decimal? planValue, string parentId, decimal? completeValue, decimal? realValue)
        {
            Model.Wbs_WbsSet parentWbsSet1 = BLL.WbsSetService.GetWbsSetByWbsSetId(parentId);
            if (parentWbsSet1 != null)
            {
                if (parentWbsSet1.WeightsMoney != null)
                {
                    if (parentWbsSet1.SuperWbsSetId != null)   //父节点不是分部节点
                    {
                        Model.WbsDetail detail1 = BLL.WbsDetailService.GetWbsDetailByWbsFlagYearMonth(parentWbsSet1.WbsSetId, 4, months);
                        if (detail1 == null)
                        {
                            Model.WbsDetail newDetail1 = new Model.WbsDetail();
                            newDetail1.WbsDetailId = SQLHelper.GetNewID(typeof(Model.WbsDetail));
                            newDetail1.ToWbs = parentWbsSet1.WbsSetId;
                            newDetail1.ToFlag = 4;
                            newDetail1.Way = parentWbsSet1.Way;
                            newDetail1.Months = months;
                            newDetail1.PlanValue = planValue ?? 0;
                            newDetail1.CompleteValue = completeValue ?? 0;
                            newDetail1.RealValue = realValue ?? 0;
                            if (parentWbsSet1.WeightsMoney != null)
                            {
                                newDetail1.PlanValueRate = decimal.Round(Convert.ToDecimal(newDetail1.PlanValue) / Convert.ToDecimal(parentWbsSet1.WeightsMoney) * 100, 2);
                                newDetail1.CompleteValueRate = decimal.Round(Convert.ToDecimal(newDetail1.CompleteValue) / Convert.ToDecimal(parentWbsSet1.WeightsMoney) * 100, 2);
                                newDetail1.RealValueRate = decimal.Round(Convert.ToDecimal(newDetail1.RealValue) / Convert.ToDecimal(parentWbsSet1.WeightsMoney) * 100, 2);
                            }
                            else
                            {
                                newDetail1.PlanValueRate = 0;
                                newDetail1.CompleteValueRate = 0;
                                newDetail1.RealValueRate = 0;
                            }
                            BLL.WbsDetailService.AddWbsDetail(newDetail1);
                        }
                        else
                        {
                            detail1.PlanValue += planValue ?? 0;
                            detail1.CompleteValue += completeValue ?? 0;
                            detail1.RealValue += realValue ?? 0;
                            if (parentWbsSet1.WeightsMoney != null)
                            {
                                detail1.PlanValueRate = decimal.Round(Convert.ToDecimal(detail1.PlanValue) / Convert.ToDecimal(parentWbsSet1.WeightsMoney) * 100, 2);
                                detail1.CompleteValueRate = decimal.Round(Convert.ToDecimal(detail1.CompleteValue) / Convert.ToDecimal(parentWbsSet1.WeightsMoney) * 100, 2);
                                detail1.RealValueRate = decimal.Round(Convert.ToDecimal(detail1.RealValue) / Convert.ToDecimal(parentWbsSet1.WeightsMoney) * 100, 2);
                            }
                            else
                            {
                                detail1.PlanValueRate = 0;
                                detail1.CompleteValueRate = 0;
                                detail1.RealValueRate = 0;
                            }
                            BLL.WbsDetailService.UpdateWbsDetail(detail1);
                        }
                        AddWbsParentDetail(months, planValue, parentWbsSet1.SuperWbsSetId, completeValue, realValue);
                    }
                    else
                    {
                        Model.WbsDetail detail1 = BLL.WbsDetailService.GetWbsDetailByWbsFlagYearMonth(parentWbsSet1.WbsSetId, 4, months);
                        if (detail1 == null)
                        {
                            Model.WbsDetail newDetail1 = new Model.WbsDetail();
                            newDetail1.WbsDetailId = SQLHelper.GetNewID(typeof(Model.WbsDetail));
                            newDetail1.ToWbs = parentWbsSet1.WbsSetId;
                            newDetail1.ToFlag = 4;
                            newDetail1.Way = parentWbsSet1.Way;
                            newDetail1.Months = months;
                            newDetail1.PlanValue = planValue;
                            newDetail1.CompleteValue = completeValue ?? 0;
                            newDetail1.RealValue = realValue ?? 0;
                            if (parentWbsSet1.WeightsMoney != null)
                            {
                                newDetail1.PlanValueRate = decimal.Round(Convert.ToDecimal(newDetail1.PlanValue) / Convert.ToDecimal(parentWbsSet1.WeightsMoney) * 100, 2);
                                newDetail1.CompleteValueRate = decimal.Round(Convert.ToDecimal(newDetail1.CompleteValue) / Convert.ToDecimal(parentWbsSet1.WeightsMoney) * 100, 2);
                                newDetail1.RealValueRate = decimal.Round(Convert.ToDecimal(newDetail1.RealValue) / Convert.ToDecimal(parentWbsSet1.WeightsMoney) * 100, 2);
                            }
                            else
                            {
                                newDetail1.PlanValueRate = 0;
                                newDetail1.CompleteValueRate = 0;
                                newDetail1.RealValueRate = 0;
                            }
                            BLL.WbsDetailService.AddWbsDetail(newDetail1);
                        }
                        else
                        {
                            detail1.PlanValue += planValue;
                            detail1.CompleteValue += completeValue ?? 0;
                            detail1.RealValue += realValue ?? 0;
                            if (parentWbsSet1.WeightsMoney != null)
                            {
                                detail1.PlanValueRate = decimal.Round(Convert.ToDecimal(detail1.PlanValue) / Convert.ToDecimal(parentWbsSet1.WeightsMoney) * 100, 2);
                                detail1.CompleteValueRate = decimal.Round(Convert.ToDecimal(detail1.CompleteValue) / Convert.ToDecimal(parentWbsSet1.WeightsMoney) * 100, 2);
                                detail1.RealValueRate = decimal.Round(Convert.ToDecimal(detail1.RealValue) / Convert.ToDecimal(parentWbsSet1.WeightsMoney) * 100, 2);
                            }
                            else
                            {
                                detail1.PlanValueRate = 0;
                                detail1.CompleteValueRate = 0;
                                detail1.RealValueRate = 0;
                            }
                            BLL.WbsDetailService.UpdateWbsDetail(detail1);
                        }
                    }
                }
            }
        }
        #endregion
        #region  更新分部分项
        /// 
        /// 更新分部分项
        /// 
        /// 
        /// 
        /// 
        /// 
        private void UpdateWbsParentDetail(DateTime months, decimal? planValue, decimal? oldPlanValue, string parentId)
        {
            Model.Wbs_WbsSet parentWbsSet1 = BLL.WbsSetService.GetWbsSetByWbsSetId(parentId);
            if (parentWbsSet1 != null)
            {
                Model.WbsDetail detail1 = BLL.WbsDetailService.GetWbsDetailByWbsFlagYearMonth(parentWbsSet1.WbsSetId, 4, months);
                if (detail1 != null)
                {
                    if (parentWbsSet1.SuperWbsSetId != null)   //父节点不是分部节点
                    {
                        detail1.PlanValue += ((planValue ?? 0) - (oldPlanValue ?? 0));
                        detail1.PlanValueRate = decimal.Round(Convert.ToDecimal(detail1.PlanValue) / Convert.ToDecimal(parentWbsSet1.WeightsMoney) * 100, 2);
                        BLL.WbsDetailService.UpdateWbsDetail(detail1);
                        UpdateWbsParentDetail(months, planValue, oldPlanValue, parentWbsSet1.SuperWbsSetId);
                    }
                    else
                    {
                        detail1.PlanValue += ((planValue ?? 0) - (oldPlanValue ?? 0));
                        detail1.PlanValueRate = decimal.Round(Convert.ToDecimal(detail1.PlanValue) / Convert.ToDecimal(parentWbsSet1.WeightsMoney) * 100, 2);
                        BLL.WbsDetailService.UpdateWbsDetail(detail1);
                    }
                }
            }
        }
        #endregion
        #region  保存按钮
        /// 
        /// 保存
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.Grid1.Rows.Count > 0)
            {
                SaveData("success");
            }
            else
            {
                Alert.ShowInTop("请至少选择一条记录!", MessageBoxIcon.Warning);
                return;
            }
        }
        #endregion
        #region  查看之前版本
        /// 
        /// 查看之前版本
        /// 
        /// 
        /// 
        protected void btnSee_Click(object sender, EventArgs e)
        {
            PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("SeeDetailHistory.aspx?ToWbs={0}", this.trWBS.SelectedNodeID, "编辑 - ")));
        }
        #endregion
        #region  绑定数据
        /// 
        /// 绑定数据
        /// 
        /// 
        /// 
        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();
        }
        /// 
        /// 加载Grid
        /// 
        private void BindGrid()
        {
            this.btnSave.Hidden = true;
            this.btnSee.Hidden = true;
            DateTime startDate, endDate, startMonth, endMonth, parentStartDate, parentEndDate, parentStartMonth, parentEndMonth;
            List items = new List();
            List months = new List();
            string column0 = string.Empty;
            decimal totalValue = 0, totalRate = 0;
            RenderField column = Grid1.FindColumn("PlanValue") as RenderField;
            column.EnableColumnEdit = false;
            if (this.trWBS.SelectedNode != null)
            {
                #region 分部分项
                if (this.trWBS.SelectedNode.CommandName == "wbsSet")
                {
                    Model.Wbs_WbsSet wbsSet = BLL.WbsSetService.GetWbsSetByWbsSetId(this.trWBS.SelectedNode.NodeID);
                    if (wbsSet != null)
                    {
                        startDate = Convert.ToDateTime(wbsSet.StartDate);
                        endDate = Convert.ToDateTime(wbsSet.EndDate);
                        startMonth = Convert.ToDateTime(startDate.Year + "-" + startDate.Month + "-01");
                        endMonth = Convert.ToDateTime(endDate.Year + "-" + endDate.Month + "-01");
                        do
                        {
                            months.Add(startMonth);
                            startMonth = startMonth.AddMonths(1);
                        } while (startMonth <= endMonth);
                        column0 = "进度计划开始日期:" + string.Format("{0:yyyy-MM-dd}", startDate) + "
进度计划结束日期:" + string.Format("{0:yyyy-MM-dd}", endDate);
                        this.Grid1.Columns[0].HeaderText = column0;
                        var childWbsSets = BLL.WbsSetService.GetApproveWbsSetsBySuperWbsSetId(wbsSet.WbsSetId);
                        if (childWbsSets.Count == 0)   //当前为末级
                        {
                            //获取父级节点的月份集合
                            List parentMonths = new List();   //父级节点的月份集合
                            string parentId = this.trWBS.SelectedNode.ParentNode.NodeID;
                            Model.Wbs_WbsSet parentWbsSet = BLL.WbsSetService.GetWbsSetByWbsSetId(parentId);
                            if (parentWbsSet != null)  //父级为分部
                            {
                                parentStartDate = Convert.ToDateTime(parentWbsSet.StartDate);
                                parentEndDate = Convert.ToDateTime(parentWbsSet.EndDate);
                                parentStartMonth = Convert.ToDateTime(parentStartDate.Year + "-" + parentStartDate.Month + "-01");
                                parentEndMonth = Convert.ToDateTime(parentEndDate.Year + "-" + parentEndDate.Month + "-01");
                                do
                                {
                                    parentMonths.Add(parentStartMonth);
                                    parentStartMonth = parentStartMonth.AddMonths(1);
                                } while (parentStartMonth <= parentEndMonth);
                            }
                            else    //父级为质量行为
                            {
                                Model.Wbs_UnitProject unitProject = BLL.UnitProjectService.GetUnitProjectByUnitProjectId(parentId);
                                if (unitProject != null)
                                {
                                    parentStartDate = Convert.ToDateTime(unitProject.StartDate);
                                    parentEndDate = Convert.ToDateTime(unitProject.EndDate);
                                    parentStartMonth = Convert.ToDateTime(parentStartDate.Year + "-" + parentStartDate.Month + "-01");
                                    parentEndMonth = Convert.ToDateTime(parentEndDate.Year + "-" + parentEndDate.Month + "-01");
                                    do
                                    {
                                        parentMonths.Add(parentStartMonth);
                                        parentStartMonth = parentStartMonth.AddMonths(1);
                                    } while (parentStartMonth <= parentEndMonth);
                                }
                            }
                            RenderField columnPlanValue = Grid1.FindColumn("PlanValue") as RenderField;
                            columnPlanValue.EnableColumnEdit = true;
                            if (wbsSet.IsPlanApprove == true)
                            {
                                btnSave.ConfirmText = String.Format("{0}的进度计划已审核并发布,确定重新设置进度计划并进行流程审批吗?", wbsSet.WbsSetName);
                            }
                            else
                            {
                                btnSave.ConfirmText = string.Empty;
                            }
                            if (BLL.CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.WBSPlanAuditMenuId, Const.BtnSave))
                            {
                                this.btnSave.Hidden = false;
                                this.btnSee.Hidden = false;
                            }
                            for (int i = 0; i < months.Count; i++)
                            {
                                Model.WbsDetail detail = BLL.WbsDetailService.GetWbsDetailByWbsFlagYearMonth(wbsSet.WbsSetId, 4, months[i]);
                                if (detail == null)
                                {
                                    Model.WBSDetailItem item = new Model.WBSDetailItem();
                                    item.WbsDetailId = SQLHelper.GetNewID(typeof(Model.WbsDetail));
                                    item.ToWbs = wbsSet.WbsSetId;
                                    item.ToFlag = 4;   //分部分项
                                    item.Way = wbsSet.Way;
                                    item.Months = months[i];
                                    item.YearsMonthsStr = months[i].Year + "年" + months[i].Month + "月";
                                    item.WeightsMoney = wbsSet.WeightsMoney;
                                    if (wbsSet.WeightsMoney != null)
                                    {
                                        decimal moneys = decimal.Round(Convert.ToDecimal(wbsSet.WeightsMoney), 2);
                                        if (i == months.Count - 1)
                                        {
                                            decimal lastPlanValue = decimal.Round(Convert.ToDecimal(wbsSet.WeightsMoney - totalValue), 2);
                                            if (lastPlanValue >= 0)
                                            {
                                                item.PlanValue = lastPlanValue;
                                            }
                                            else
                                            {
                                                decimal value = decimal.Round(Convert.ToDecimal(totalValue - wbsSet.WeightsMoney), 2);
                                                var detailList = BLL.WbsDetailService.GetTotalWbsDetailsByToWbs(wbsSet.WbsSetId);
                                                while (value > 0)
                                                {
                                                    foreach (var d in detailList)
                                                    {
                                                        if (value > 0 && d.PlanValue >= Convert.ToDecimal(0.01))
                                                        {
                                                            d.PlanValue = Convert.ToDecimal(Convert.ToDecimal(d.PlanValue) - Convert.ToDecimal(0.01));
                                                            d.PlanValueRate = decimal.Round(Convert.ToDecimal(d.PlanValue / wbsSet.WeightsMoney * 100), 2);
                                                            BLL.WbsDetailService.UpdateWbsDetail(d);
                                                            value = Convert.ToDecimal(value - Convert.ToDecimal(0.01));
                                                        }
                                                    }
                                                }
                                                item.PlanValue = 0;
                                            }
                                        }
                                        else
                                        {
                                            item.PlanValue = WbsDetailService.GetMonthPlanValueByMonthCountAndMonthNumAndWeightMonth(months.Count, i + 1, Convert.ToDecimal(wbsSet.WeightsMoney));
                                        }
                                        if (wbsSet.WeightsMoney != 0)
                                        {
                                            item.PlanValueRate = decimal.Round(Convert.ToDecimal(item.PlanValue / wbsSet.WeightsMoney * 100), 2);
                                        }
                                        else
                                        {
                                            item.PlanValueRate = 0;
                                        }
                                        totalValue += decimal.Round(Convert.ToDecimal(item.PlanValue), 2);
                                        totalRate += decimal.Round(Convert.ToDecimal(item.PlanValueRate), 2);
                                        item.PlanValueTotal = totalValue;
                                        item.PlanValueRateTotal = totalRate;
                                        if (i == months.Count - 1)
                                        {
                                            if (wbsSet.WeightsMoney == totalValue)
                                            {
                                                item.PlanValueRateTotal = 100;
                                            }
                                        }
                                    }
                                    items.Add(item);
                                }
                                else
                                {
                                    Model.WBSDetailItem item = new Model.WBSDetailItem();
                                    item.WbsDetailId = detail.WbsDetailId;
                                    item.ToWbs = detail.ToWbs;
                                    item.ToFlag = 4;   //分部分项
                                    item.Way = detail.Way;
                                    item.Months = months[i];
                                    item.YearsMonthsStr = months[i].Year + "年" + months[i].Month + "月";
                                    item.WeightsMoney = wbsSet.WeightsMoney;
                                    if (detail.PlanValue != null)
                                    {
                                        item.PlanValue = decimal.Round(Convert.ToDecimal(detail.PlanValue), 2);
                                        item.PlanValueRate = decimal.Round(Convert.ToDecimal(detail.PlanValueRate), 2);
                                        totalValue += decimal.Round(Convert.ToDecimal(item.PlanValue), 2);
                                        totalRate += decimal.Round(Convert.ToDecimal(item.PlanValueRate), 2);
                                        item.PlanValueTotal = totalValue;
                                        item.PlanValueRateTotal = totalRate;
                                        if (i == months.Count - 1)
                                        {
                                            if (wbsSet.WeightsMoney == totalValue)
                                            {
                                                item.PlanValueRateTotal = 100;
                                            }
                                        }
                                    }
                                    items.Add(item);
                                }
                            }
                            for (int i = 0; i < parentMonths.Count; i++)
                            {
                                if (!months.Contains(parentMonths[i]))
                                {
                                    Model.WbsDetail detail = (from x in Funs.DB.WbsDetail
                                                              where x.ToWbs == wbsSet.WbsSetId && x.Months == parentMonths[i]
                                                              select x).FirstOrDefault();
                                    if (detail == null)
                                    {
                                        Model.WBSDetailItem item = new Model.WBSDetailItem();
                                        item.WbsDetailId = SQLHelper.GetNewID(typeof(Model.WbsDetail));
                                        item.ToWbs = wbsSet.WbsSetId;
                                        item.ToFlag = 4;   //分部分项
                                        item.Way = wbsSet.Way;
                                        item.Months = parentMonths[i];
                                        item.YearsMonthsStr = parentMonths[i].Year + "年" + parentMonths[i].Month + "月";
                                        item.WeightsMoney = wbsSet.WeightsMoney;
                                        item.PlanValue = 0;
                                        item.PlanValueRate = 0;
                                        items.Add(item);
                                    }
                                    else
                                    {
                                        Model.WBSDetailItem item = new Model.WBSDetailItem();
                                        item.WbsDetailId = detail.WbsDetailId;
                                        item.ToWbs = wbsSet.WbsSetId;
                                        item.ToFlag = 4;   //分部分项
                                        item.Way = wbsSet.Way;
                                        item.Months = parentMonths[i];
                                        item.YearsMonthsStr = parentMonths[i].Year + "年" + parentMonths[i].Month + "月";
                                        item.WeightsMoney = wbsSet.WeightsMoney;
                                        item.PlanValue = detail.PlanValue;
                                        item.PlanValueRate = detail.PlanValueRate;
                                        items.Add(item);
                                    }
                                }
                            }
                            items = items.OrderBy(x => x.Months).ToList();
                        }
                        else   //当前不为末级
                        {
                            for (int i = 0; i < months.Count; i++)
                            {
                                Model.WbsDetail detail = BLL.WbsDetailService.GetWbsDetailByWbsFlagYearMonth(wbsSet.WbsSetId, 4, months[i]);
                                if (detail != null)
                                {
                                    Model.WBSDetailItem item = new Model.WBSDetailItem();
                                    item.WbsDetailId = detail.WbsDetailId;
                                    item.ToWbs = detail.ToWbs;
                                    item.ToFlag = 4;   //分部分项
                                    item.Way = detail.Way;
                                    item.Months = months[i];
                                    item.YearsMonthsStr = months[i].Year + "年" + months[i].Month + "月";
                                    item.WeightsMoney = wbsSet.WeightsMoney;
                                    if (detail.PlanValue != null)
                                    {
                                        item.PlanValue = decimal.Round(Convert.ToDecimal(detail.PlanValue), 2);
                                        item.PlanValueRate = decimal.Round(Convert.ToDecimal(detail.PlanValueRate), 2);
                                        totalValue += decimal.Round(Convert.ToDecimal(item.PlanValue), 2);
                                        totalRate += decimal.Round(Convert.ToDecimal(item.PlanValueRate), 2);
                                        item.PlanValueTotal = totalValue;
                                        item.PlanValueRateTotal = totalRate;
                                        if (i == months.Count - 1)
                                        {
                                            if (wbsSet.WeightsMoney == totalValue)
                                            {
                                                item.PlanValueRateTotal = 100;
                                            }
                                        }
                                    }
                                    items.Add(item);
                                }
                            }
                        }
                    }
                }
                #endregion
                #region  单位工程/子单位工程
                else if (this.trWBS.SelectedNode.CommandName == "unitProject" || this.trWBS.SelectedNode.CommandName == "childUnitProject")
                {
                    Model.Wbs_UnitProject unitProject = BLL.UnitProjectService.GetUnitProjectByUnitProjectId(this.trWBS.SelectedNode.NodeID);
                    if (unitProject != null)
                    {
                        startDate = Convert.ToDateTime(unitProject.StartDate);
                        endDate = Convert.ToDateTime(unitProject.EndDate);
                        startMonth = Convert.ToDateTime(startDate.Year + "-" + startDate.Month + "-01");
                        endMonth = Convert.ToDateTime(endDate.Year + "-" + endDate.Month + "-01");
                        do
                        {
                            months.Add(startMonth);
                            startMonth = startMonth.AddMonths(1);
                        } while (startMonth <= endMonth);
                        column0 = "进度计划开始日期:" + string.Format("{0:yyyy-MM-dd}", startDate) + "
进度计划结束日期:" + string.Format("{0:yyyy-MM-dd}", endDate);
                        this.Grid1.Columns[0].HeaderText = column0;
                        for (int i = 0; i < months.Count; i++)
                        {
                            Model.WbsDetail detail = BLL.WbsDetailService.GetWbsDetailByWbsFlagYearMonth(unitProject.UnitProjectId, 3, months[i]);
                            if (detail != null)
                            {
                                Model.WBSDetailItem item = new Model.WBSDetailItem();
                                item.WbsDetailId = detail.WbsDetailId;
                                item.ToWbs = detail.ToWbs;
                                item.ToFlag = 3;   //单位工程/子单位工程
                                item.Way = detail.Way;
                                item.Months = months[i];
                                item.YearsMonthsStr = months[i].Year + "年" + months[i].Month + "月";
                                item.WeightsMoney = unitProject.WeightsMoney;
                                if (detail.PlanValue != null)
                                {
                                    item.PlanValue = decimal.Round(Convert.ToDecimal(detail.PlanValue), 2);
                                    item.PlanValueRate = decimal.Round(Convert.ToDecimal(detail.PlanValueRate), 2);
                                    totalValue += decimal.Round(Convert.ToDecimal(item.PlanValue), 2);
                                    totalRate += decimal.Round(Convert.ToDecimal(item.PlanValueRate), 2);
                                    item.PlanValueTotal = totalValue;
                                    item.PlanValueRateTotal = totalRate;
                                    if (i == months.Count - 1)
                                    {
                                        if (unitProject.WeightsMoney == totalValue)
                                        {
                                            item.PlanValueRateTotal = 100;
                                        }
                                    }
                                }
                                items.Add(item);
                            }
                        }
                    }
                }
                #endregion
                #region  专业
                else if (this.trWBS.SelectedNode.CommandName == "cnProfession")
                {
                    Model.WBS_CnProfession cnProfession = BLL.CnProfessionService.GetCnProfessionByCnProfessionId(this.trWBS.SelectedNode.NodeID);
                    if (cnProfession != null)
                    {
                        startDate = Convert.ToDateTime(cnProfession.StartDate);
                        endDate = Convert.ToDateTime(cnProfession.EndDate);
                        startMonth = Convert.ToDateTime(startDate.Year + "-" + startDate.Month + "-01");
                        endMonth = Convert.ToDateTime(endDate.Year + "-" + endDate.Month + "-01");
                        do
                        {
                            months.Add(startMonth);
                            startMonth = startMonth.AddMonths(1);
                        } while (startMonth <= endMonth);
                        column0 = "进度计划开始日期:" + string.Format("{0:yyyy-MM-dd}", startDate) + "
进度计划结束日期:" + string.Format("{0:yyyy-MM-dd}", endDate);
                        this.Grid1.Columns[0].HeaderText = column0;
                        for (int i = 0; i < months.Count; i++)
                        {
                            Model.WbsDetail detail = BLL.WbsDetailService.GetWbsDetailByWbsFlagYearMonth(cnProfession.CnProfessionId, 2, months[i]);
                            if (detail != null)
                            {
                                Model.WBSDetailItem item = new Model.WBSDetailItem();
                                item.WbsDetailId = detail.WbsDetailId;
                                item.ToWbs = detail.ToWbs;
                                item.ToFlag = 2;   //专业
                                item.Way = detail.Way;
                                item.Months = months[i];
                                item.YearsMonthsStr = months[i].Year + "年" + months[i].Month + "月";
                                item.WeightsMoney = cnProfession.WeightsMoney;
                                if (detail.PlanValue != null)
                                {
                                    item.PlanValue = decimal.Round(Convert.ToDecimal(detail.PlanValue), 2);
                                    item.PlanValueRate = decimal.Round(Convert.ToDecimal(detail.PlanValueRate), 2);
                                    totalValue += decimal.Round(Convert.ToDecimal(item.PlanValue), 2);
                                    totalRate += decimal.Round(Convert.ToDecimal(item.PlanValueRate), 2);
                                    item.PlanValueTotal = totalValue;
                                    item.PlanValueRateTotal = totalRate;
                                    if (i == months.Count - 1)
                                    {
                                        if (cnProfession.WeightsMoney == totalValue)
                                        {
                                            item.PlanValueRateTotal = 100;
                                        }
                                    }
                                }
                                items.Add(item);
                            }
                        }
                    }
                }
                #endregion
                #region  装置
                else if (this.trWBS.SelectedNode.CommandName == "installation")
                {
                    Model.Project_Installation installation = BLL.Project_InstallationService.GetInstallationByInstallationId(this.trWBS.SelectedNode.NodeID);
                    if (installation != null)
                    {
                        startDate = Convert.ToDateTime(installation.StartDate);
                        endDate = Convert.ToDateTime(installation.EndDate);
                        startMonth = Convert.ToDateTime(startDate.Year + "-" + startDate.Month + "-01");
                        endMonth = Convert.ToDateTime(endDate.Year + "-" + endDate.Month + "-01");
                        do
                        {
                            months.Add(startMonth);
                            startMonth = startMonth.AddMonths(1);
                        } while (startMonth <= endMonth);
                        column0 = "进度计划开始日期:" + string.Format("{0:yyyy-MM-dd}", startDate) + "
进度计划结束日期:" + string.Format("{0:yyyy-MM-dd}", endDate);
                        this.Grid1.Columns[0].HeaderText = column0;
                        for (int i = 0; i < months.Count; i++)
                        {
                            Model.WbsDetail detail = BLL.WbsDetailService.GetWbsDetailByWbsFlagYearMonth(installation.InstallationId, 1, months[i]);
                            if (detail != null)
                            {
                                Model.WBSDetailItem item = new Model.WBSDetailItem();
                                item.WbsDetailId = detail.WbsDetailId;
                                item.ToWbs = detail.ToWbs;
                                item.ToFlag = 1;   //装置
                                item.Way = detail.Way;
                                item.Months = months[i];
                                item.YearsMonthsStr = months[i].Year + "年" + months[i].Month + "月";
                                item.WeightsMoney = installation.WeightsMoney;
                                if (detail.PlanValue != null)
                                {
                                    item.PlanValue = decimal.Round(Convert.ToDecimal(detail.PlanValue), 2);
                                    item.PlanValueRate = decimal.Round(Convert.ToDecimal(detail.PlanValueRate), 2);
                                    totalValue += decimal.Round(Convert.ToDecimal(item.PlanValue), 2);
                                    totalRate += decimal.Round(Convert.ToDecimal(item.PlanValueRate), 2);
                                    item.PlanValueTotal = totalValue;
                                    item.PlanValueRateTotal = totalRate;
                                    if (i == months.Count - 1)
                                    {
                                        if (installation.WeightsMoney == totalValue)
                                        {
                                            item.PlanValueRateTotal = 100;
                                        }
                                    }
                                }
                                items.Add(item);
                            }
                        }
                    }
                }
                #endregion
                this.Grid1.DataSource = items;
                this.Grid1.DataBind();
                if (this.trWBS.SelectedNode.CommandName == "wbsSet")
                {
                    Model.Wbs_WbsSet wbsSet = BLL.WbsSetService.GetWbsSetByWbsSetId(this.trWBS.SelectedNode.NodeID);
                    if (wbsSet != null)
                    {
                        var childWbsSets = BLL.WbsSetService.GetApproveWbsSetsBySuperWbsSetId(wbsSet.WbsSetId);
                        if (childWbsSets.Count == 0)   //当前为末级
                        {
                            SaveData2();
                        }
                    }
                }
            }
        }
        #endregion
        #region 判断装置下权重是否都已审核
        /// 
        /// 判断装置下权重是否都已审核
        /// 
        /// 
        /// 
        private bool IsWeightAudit(string ins)
        {
            int num = 0;
            bool isWeightAudit = true;
            var cn = from x in Funs.DB.WBS_CnProfession
                     join y in Funs.DB.Wbs_UnitProject on x.CnProfessionId equals y.CnProfessionId
                     where x.InstallationId == ins && y.IsApprove == true
                     && (x.IsWeightsApprove == false || x.IsWeightsApprove == null)
                     select x;
            num = cn.Count();
            var unit = from x in Funs.DB.Wbs_UnitProject
                       join y in Funs.DB.WBS_CnProfession on x.CnProfessionId equals y.CnProfessionId
                       where y.InstallationId == ins && x.IsApprove == true && (x.IsWeightsApprove == false || x.IsWeightsApprove == null)
                       select x;
            num = num + unit.Count();
            var wbs = from x in Funs.DB.Wbs_WbsSet
                      join y in Funs.DB.WBS_CnProfession on x.CnProfessionId equals y.CnProfessionId
                      where y.InstallationId == ins && x.IsApprove == true && (x.IsWeightsApprove == false || x.IsWeightsApprove == null)
                      select x;
            num = num + wbs.Count();
            if (num > 0)
            {
                isWeightAudit = false;
            }
            return isWeightAudit;
        }
        #endregion
        #region 判断装置下权重是否有负数
        /// 
        /// 判断装置下权重是否有负数
        /// 
        /// 
        /// 
        private bool IsWeightMinus(string ins)
        {
            int num = 0;
            bool isWeightMinus = false;
            var cn = from x in Funs.DB.WBS_CnProfession where x.InstallationId == ins && (x.WeightsMoney <= 0 || x.Weights <= 0) select x;
            num = cn.Count();
            var unit = from x in Funs.DB.Wbs_UnitProject
                       join y in Funs.DB.WBS_CnProfession on x.CnProfessionId equals y.CnProfessionId
                       where y.InstallationId == ins && x.IsApprove == true && (x.WeightsMoney <= 0 || x.Weights <= 0)
                       select x;
            num = num + unit.Count();
            var wbs = from x in Funs.DB.Wbs_WbsSet
                      join y in Funs.DB.WBS_CnProfession on x.CnProfessionId equals y.CnProfessionId
                      where y.InstallationId == ins && x.IsApprove == true && (x.WeightsMoney <= 0 || x.Weights <= 0)
                      select x;
            num = num + wbs.Count();
            if (num > 0)
            {
                isWeightMinus = true;
            }
            return isWeightMinus;
        }
        #endregion
        #region 右键合计计划值方法
        /// 
        /// 右键修改事件
        /// 
        /// 
        /// 
        protected void btnMenuEdit_Click(object sender, EventArgs e)
        {
            if (this.trWBS.SelectedNode != null)
            {
                MergeNextWbsSet(trWBS.SelectedNodeID, trWBS.SelectedNode.CommandName);
                Alert.ShowInTop("计划值合计完成!", MessageBoxIcon.Warning);
            }
        }
        private void MergeNextWbsSet(string id, string type)
        {
            if (type == "installation")
            {
                var q = from x in Funs.DB.WbsDetail
                        join y in Funs.DB.WBS_CnProfession on x.ToWbs equals y.CnProfessionId
                        where y.InstallationId == id
                        group new { x.PlanValue, x.CompleteValue, x.RealValue } by new { x.Months }
                            into g
                        select new
                        {
                            Months = g.Key.Months,
                            PlanValue = g.Sum(x => x.PlanValue ?? 0),
                            CompleteValue = g.Sum(x => x.CompleteValue ?? 0),
                            RealValue = g.Sum(x => x.RealValue ?? 0)
                        };
                if (q.Count() > 0)
                {
                    foreach (var wbsSet in q)
                    {
                        AddWbsSetDetail(id, Convert.ToDateTime(wbsSet.Months), Convert.ToDecimal(wbsSet.PlanValue), Convert.ToDecimal(wbsSet.CompleteValue), Convert.ToDecimal(wbsSet.RealValue), 1);
                    }
                }
            }
            if (type == "cnProfession")
            {
                var q = from x in Funs.DB.WbsDetail
                        join y in Funs.DB.Wbs_UnitProject on x.ToWbs equals y.UnitProjectId
                        where y.CnProfessionId == id && y.IsApprove == true
                        group new { x.PlanValue, x.CompleteValue, x.RealValue } by new { x.Months }
                            into g
                        select new
                        {
                            Months = g.Key.Months,
                            PlanValue = g.Sum(x => x.PlanValue ?? 0),
                            CompleteValue = g.Sum(x => x.CompleteValue ?? 0),
                            RealValue = g.Sum(x => x.RealValue ?? 0)
                        };
                if (q.Count() > 0)
                {
                    foreach (var wbsSet in q)
                    {
                        AddWbsSetDetail(id, Convert.ToDateTime(wbsSet.Months), Convert.ToDecimal(wbsSet.PlanValue), Convert.ToDecimal(wbsSet.CompleteValue), Convert.ToDecimal(wbsSet.RealValue), 2);
                    }
                }
            }
            if (type == "unitProject")
            {
                var q = from x in Funs.DB.WbsDetail
                        join y in Funs.DB.Wbs_WbsSet on x.ToWbs equals y.WbsSetId
                        where y.UnitProjectId == id && y.Flag == 1 && y.IsApprove == true
                        group new { x.PlanValue, x.CompleteValue, x.RealValue } by new { x.Months }
                            into g
                        select new
                        {
                            Months = g.Key.Months,
                            PlanValue = g.Sum(x => x.PlanValue ?? 0),
                            CompleteValue = g.Sum(x => x.CompleteValue ?? 0),
                            RealValue = g.Sum(x => x.RealValue ?? 0)
                        };
                //  质量行为
                var z = from x in Funs.DB.WbsDetail
                        join y in Funs.DB.Wbs_WbsSet on x.ToWbs equals y.WbsSetId
                        where y.UnitProjectId == id && y.Flag == 3 && y.IsApprove == true
                        group new { x.PlanValue, x.CompleteValue, x.RealValue } by new { x.Months }
                            into g
                        select new
                        {
                            Months = g.Key.Months,
                            PlanValue = g.Sum(x => x.PlanValue ?? 0),
                            CompleteValue = g.Sum(x => x.CompleteValue ?? 0),
                            RealValue = g.Sum(x => x.RealValue ?? 0)
                        };
                // 子单位工程
                var c = from x in Funs.DB.WbsDetail
                        join y in Funs.DB.Wbs_UnitProject on x.ToWbs equals y.UnitProjectId
                        where y.SuperUnitProjectId == id && y.IsApprove == true
                        group new { x.PlanValue, x.CompleteValue, x.RealValue } by new { y.SuperUnitProjectId, x.Months }
                            into g
                        select new
                        {
                            ToWbs = g.Key.SuperUnitProjectId,
                            Months = g.Key.Months,
                            PlanValue = g.Sum(x => x.PlanValue ?? 0),
                            CompleteValue = g.Sum(x => x.CompleteValue ?? 0),
                            RealValue = g.Sum(x => x.RealValue ?? 0)
                        };
                if (q.Count() > 0)
                {
                    foreach (var wbsSet in q)
                    {
                        AddWbsSetDetail(id, Convert.ToDateTime(wbsSet.Months), Convert.ToDecimal(wbsSet.PlanValue), Convert.ToDecimal(wbsSet.CompleteValue), Convert.ToDecimal(wbsSet.RealValue), 3);
                    }
                }
                if (z.Count() > 0)
                {
                    foreach (var wbsSet in z)
                    {
                        AddWbsSetDetail(id, Convert.ToDateTime(wbsSet.Months), Convert.ToDecimal(wbsSet.PlanValue), Convert.ToDecimal(wbsSet.CompleteValue), Convert.ToDecimal(wbsSet.RealValue), 3);
                    }
                }
                if (c.Count() > 0)
                {
                    foreach (var wbsSet in c)
                    {
                        AddWbsSetDetail(id, Convert.ToDateTime(wbsSet.Months), Convert.ToDecimal(wbsSet.PlanValue), Convert.ToDecimal(wbsSet.CompleteValue), Convert.ToDecimal(wbsSet.RealValue), 3);
                    }
                }
            }
            if (type == "childUnitProject")
            {
                var q = from x in Funs.DB.WbsDetail
                        join y in Funs.DB.Wbs_WbsSet on x.ToWbs equals y.WbsSetId
                        where y.UnitProjectId == id && y.Flag == 1 && y.IsApprove == true
                        group new { x.PlanValue, x.CompleteValue, x.RealValue } by new { x.Months }
                            into g
                        select new
                        {
                            Months = g.Key.Months,
                            PlanValue = g.Sum(x => x.PlanValue ?? 0),
                            CompleteValue = g.Sum(x => x.CompleteValue ?? 0),
                            RealValue = g.Sum(x => x.RealValue ?? 0)
                        };
                if (q.Count() > 0)
                {
                    foreach (var wbsSet in q)
                    {
                        AddWbsSetDetail(id, Convert.ToDateTime(wbsSet.Months), Convert.ToDecimal(wbsSet.PlanValue), Convert.ToDecimal(wbsSet.CompleteValue), Convert.ToDecimal(wbsSet.RealValue), 3);
                    }
                }
            }
            if (type == "wbsSet")
            {
                var q = from x in Funs.DB.WbsDetail
                        join y in Funs.DB.Wbs_WbsSet on x.ToWbs equals y.WbsSetId
                        where y.SuperWbsSetId == id && y.IsApprove == true
                        group new { x.PlanValue, x.CompleteValue, x.RealValue } by new { x.Months }
                            into g
                        select new
                        {
                            Months = g.Key.Months,
                            PlanValue = g.Sum(x => x.PlanValue ?? 0),
                            CompleteValue = g.Sum(x => x.CompleteValue ?? 0),
                            RealValue = g.Sum(x => x.RealValue ?? 0)
                        };
                if (q.Count() > 0)
                {
                    foreach (var wbsSet in q)
                    {
                        AddWbsSetDetail(id, Convert.ToDateTime(wbsSet.Months), Convert.ToDecimal(wbsSet.PlanValue), Convert.ToDecimal(wbsSet.CompleteValue), Convert.ToDecimal(wbsSet.RealValue), 4);
                    }
                }
            }
        }
        #endregion
        #region  自动审核设置装置末级分项计划值
        /// 
        /// 自动审核设置装置末级分项计划值
        /// 
        /// 
        /// 
        protected void btnAuditPlan_Click(object sender, EventArgs e)
        {
            if (this.trWBS.SelectedNode != null)
            {
                if (this.trWBS.SelectedNode.CommandName == "installation")
                {
                    if (!IsWeightAudit(this.trWBS.SelectedNodeID))
                    {
                        Alert.ShowInTop("该装置下有未审核的权重设置!", MessageBoxIcon.Warning);
                        return;
                    }
                    if (!IsWeightAudit(this.trWBS.SelectedNodeID))
                    {
                        Alert.ShowInTop("该装置下有权重设置为0或负数!", MessageBoxIcon.Warning);
                        return;
                    }
                    var cnProfessions = from x in Funs.DB.WBS_CnProfession where x.InstallationId == this.trWBS.SelectedNodeID orderby x.OldId select x;
                    foreach (var cnProfession in cnProfessions)
                    {
                        var wbsSets = from x in Funs.DB.Wbs_WbsSet where x.IsApprove == true && x.CnProfessionId == cnProfession.CnProfessionId select x;
                        foreach (var wbsSet in wbsSets)
                        {
                            DateTime startDate, endDate, startMonth, endMonth, parentStartDate, parentEndDate, parentStartMonth, parentEndMonth;
                            List months = new List();
                            string column0 = string.Empty;
                            decimal totalValue = 0, totalRate = 0;
                            var childWbsSets = BLL.WbsSetService.GetApproveWbsSetsBySuperWbsSetId(wbsSet.WbsSetId);
                            if (childWbsSets.Count() == 0)   //当前为末级
                            {
                                BLL.WbsSetService.UpdateWbsSetIsPlanApprove(wbsSet, true);
                                startDate = Convert.ToDateTime(wbsSet.StartDate);
                                endDate = Convert.ToDateTime(wbsSet.EndDate);
                                startMonth = Convert.ToDateTime(startDate.Year + "-" + startDate.Month + "-01");
                                endMonth = Convert.ToDateTime(endDate.Year + "-" + endDate.Month + "-01");
                                do
                                {
                                    months.Add(startMonth);
                                    startMonth = startMonth.AddMonths(1);
                                }
                                while (startMonth <= endMonth);
                                //获取父级节点的月份集合
                                List parentMonths = new List();   //父级节点的月份集合
                                string parentId = wbsSet.SuperWbsSetId;
                                Model.Wbs_WbsSet parentWbsSet = BLL.WbsSetService.GetWbsSetByWbsSetId(parentId);
                                if (parentWbsSet != null)  //父级为分部
                                {
                                    parentStartDate = Convert.ToDateTime(parentWbsSet.StartDate);
                                    parentEndDate = Convert.ToDateTime(parentWbsSet.EndDate);
                                    parentStartMonth = Convert.ToDateTime(parentStartDate.Year + "-" + parentStartDate.Month + "-01");
                                    parentEndMonth = Convert.ToDateTime(parentEndDate.Year + "-" + parentEndDate.Month + "-01");
                                    do
                                    {
                                        parentMonths.Add(parentStartMonth);
                                        parentStartMonth = parentStartMonth.AddMonths(1);
                                    }
                                    while (parentStartMonth <= parentEndMonth);
                                }
                                else    //父级为质量行为
                                {
                                    Model.Wbs_UnitProject unitProject = BLL.UnitProjectService.GetUnitProjectByUnitProjectId(parentId);
                                    if (unitProject != null)
                                    {
                                        parentStartDate = Convert.ToDateTime(unitProject.StartDate);
                                        parentEndDate = Convert.ToDateTime(unitProject.EndDate);
                                        parentStartMonth = Convert.ToDateTime(parentStartDate.Year + "-" + parentStartDate.Month + "-01");
                                        parentEndMonth = Convert.ToDateTime(parentEndDate.Year + "-" + parentEndDate.Month + "-01");
                                        do
                                        {
                                            parentMonths.Add(parentStartMonth);
                                            parentStartMonth = parentStartMonth.AddMonths(1);
                                        }
                                        while (parentStartMonth <= parentEndMonth);
                                    }
                                }
                                for (int i = 0; i < months.Count; i++)
                                {
                                    Model.WbsDetail detail = BLL.WbsDetailService.GetWbsDetailByWbsFlagYearMonth(wbsSet.WbsSetId, 4, months[i]);
                                    if (detail == null)
                                    {
                                        Model.WbsDetail item = new Model.WbsDetail();
                                        item.WbsDetailId = SQLHelper.GetNewID(typeof(Model.WbsDetail));
                                        item.ToWbs = wbsSet.WbsSetId;
                                        item.ToFlag = 4;   //分部分项
                                        item.Way = wbsSet.Way;
                                        item.Months = months[i];
                                        if (wbsSet.WeightsMoney != null)
                                        {
                                            if (i == months.Count - 1)
                                            {
                                                decimal lastPlanValue = decimal.Round(Convert.ToDecimal(wbsSet.WeightsMoney - totalValue), 2);
                                                if (lastPlanValue >= 0)
                                                {
                                                    item.PlanValue = lastPlanValue;
                                                }
                                            }
                                            else
                                            {
                                                item.PlanValue = WbsDetailService.GetMonthPlanValueByMonthCountAndMonthNumAndWeightMonth(months.Count, i + 1, Convert.ToDecimal(wbsSet.WeightsMoney));
                                            }
                                            if (wbsSet.WeightsMoney != 0)
                                            {
                                                item.PlanValueRate = decimal.Round(Convert.ToDecimal(item.PlanValue / wbsSet.WeightsMoney * 100), 2);
                                            }
                                            else
                                            {
                                                item.PlanValueRate = 0;
                                            }
                                            totalValue += item.PlanValue != null ? item.PlanValue.Value : 0;
                                            totalRate += item.PlanValueRate != null ? item.PlanValueRate.Value : 0;
                                            BLL.WbsDetailService.AddWbsDetail(item);
                                        }
                                    }
                                }
                                for (int i = 0; i < parentMonths.Count; i++)
                                {
                                    if (!months.Contains(parentMonths[i]))
                                    {
                                        Model.WbsDetail detail = (from x in Funs.DB.WbsDetail
                                                                  where x.ToWbs == wbsSet.WbsSetId && x.Months == parentMonths[i]
                                                                  select x).FirstOrDefault();
                                        if (detail == null)
                                        {
                                            Model.WbsDetail item = new Model.WbsDetail();
                                            item.WbsDetailId = SQLHelper.GetNewID(typeof(Model.WbsDetail));
                                            item.ToWbs = wbsSet.WbsSetId;
                                            item.ToFlag = 4;   //分部分项
                                            item.Way = wbsSet.Way;
                                            item.Months = parentMonths[i];
                                            item.PlanValue = 0;
                                            item.PlanValueRate = 0;
                                            BLL.WbsDetailService.AddWbsDetail(item);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    Alert.ShowInTop("自动分配计划值完成!", MessageBoxIcon.Warning);
                }
                else
                {
                    Alert.ShowInTop("请选择一个装置!", MessageBoxIcon.Warning);
                    return;
                }
            }
            else
            {
                Alert.ShowInTop("请选择装置!", MessageBoxIcon.Warning);
                return;
            }
        }
        #endregion
        #region 逐级向上合并计划明细
        /// 
        /// 逐级向上合并计划明细
        /// 
        /// 
        private void MergeWbsSetDetail(string cnProfessionId)
        {
            Model.SGGLDB db = Funs.DB;
            // 分部分项
            var wbsSetList4 = from x in db.WbsDetail
                              join y in db.Wbs_WbsSet on x.ToWbs equals y.WbsSetId
                              where y.CnProfessionId == cnProfessionId && y.Flag == 4 && y.IsApprove == true && y.SuperWbsSetId != null
                              group new { x.PlanValue, x.CompleteValue, x.RealValue } by new { y.SuperWbsSetId, x.Months }
                                  into g
                              select new
                              {
                                  ToWbs = g.Key.SuperWbsSetId,
                                  Months = g.Key.Months,
                                  PlanValue = g.Sum(x => x.PlanValue ?? 0),
                                  CompleteValue = g.Sum(x => x.CompleteValue ?? 0),
                                  RealValue = g.Sum(x => x.RealValue ?? 0)
                              };
            if (wbsSetList4.Count() > 0)
            {
                foreach (var wbsSet in wbsSetList4)
                {
                    AddWbsSetDetail(wbsSet.ToWbs, Convert.ToDateTime(wbsSet.Months), Convert.ToDecimal(wbsSet.PlanValue), Convert.ToDecimal(wbsSet.CompleteValue), Convert.ToDecimal(wbsSet.RealValue), 4);
                }
            }
            var wbsSetList3 = from x in db.WbsDetail
                              join y in db.Wbs_WbsSet on x.ToWbs equals y.WbsSetId
                              where y.CnProfessionId == cnProfessionId && y.Flag == 3 && y.IsApprove == true && y.SuperWbsSetId != null
                              group new { x.PlanValue, x.CompleteValue, x.RealValue } by new { y.SuperWbsSetId, x.Months }
                                  into g
                              select new
                              {
                                  ToWbs = g.Key.SuperWbsSetId,
                                  Months = g.Key.Months,
                                  PlanValue = g.Sum(x => x.PlanValue ?? 0),
                                  CompleteValue = g.Sum(x => x.CompleteValue ?? 0),
                                  RealValue = g.Sum(x => x.RealValue ?? 0)
                              };
            if (wbsSetList3.Count() > 0)
            {
                foreach (var wbsSet in wbsSetList3)
                {
                    AddWbsSetDetail(wbsSet.ToWbs, Convert.ToDateTime(wbsSet.Months), Convert.ToDecimal(wbsSet.PlanValue), Convert.ToDecimal(wbsSet.CompleteValue), Convert.ToDecimal(wbsSet.RealValue), 4);
                }
            }
            var wbsSetList2 = from x in db.WbsDetail
                              join y in db.Wbs_WbsSet on x.ToWbs equals y.WbsSetId
                              where y.CnProfessionId == cnProfessionId && y.Flag == 2 && y.IsApprove == true && y.SuperWbsSetId != null
                              group new { x.PlanValue, x.CompleteValue, x.RealValue } by new { y.SuperWbsSetId, x.Months }
                                  into g
                              select new
                              {
                                  ToWbs = g.Key.SuperWbsSetId,
                                  Months = g.Key.Months,
                                  PlanValue = g.Sum(x => x.PlanValue ?? 0),
                                  CompleteValue = g.Sum(x => x.CompleteValue ?? 0),
                                  RealValue = g.Sum(x => x.RealValue ?? 0)
                              };
            if (wbsSetList2.Count() > 0)
            {
                foreach (var wbsSet in wbsSetList2)
                {
                    AddWbsSetDetail(wbsSet.ToWbs, Convert.ToDateTime(wbsSet.Months), Convert.ToDecimal(wbsSet.PlanValue), Convert.ToDecimal(wbsSet.CompleteValue), Convert.ToDecimal(wbsSet.RealValue), 4);
                }
            }
            // 单位工程及子单位工程
            //质量行为
            var wbsSetList = from x in db.WbsDetail
                             join y in db.Wbs_WbsSet on x.ToWbs equals y.WbsSetId
                             join z in db.Wbs_UnitProject on y.UnitProjectId equals z.UnitProjectId
                             where y.CnProfessionId == cnProfessionId && y.IsApprove == true && y.Flag == 3 && y.SuperWbsSetId == null
                             group new { x.PlanValue, x.CompleteValue, x.RealValue } by new { z.UnitProjectId, x.Months }
                                 into g
                             select new
                             {
                                 ToWbs = g.Key.UnitProjectId,
                                 Months = g.Key.Months,
                                 PlanValue = g.Sum(x => x.PlanValue ?? 0),
                                 CompleteValue = g.Sum(x => x.CompleteValue ?? 0),
                                 RealValue = g.Sum(x => x.RealValue ?? 0)
                             };
            if (wbsSetList.Count() > 0)
            {
                foreach (var wbsSet in wbsSetList)
                {
                    AddWbsSetDetail(wbsSet.ToWbs, Convert.ToDateTime(wbsSet.Months), Convert.ToDecimal(wbsSet.PlanValue), Convert.ToDecimal(wbsSet.CompleteValue), Convert.ToDecimal(wbsSet.RealValue), 3);
                }
            }
            // 单位工程及子单位工程
            var wbsSetList1 = from x in db.WbsDetail
                              join y in db.Wbs_WbsSet on x.ToWbs equals y.WbsSetId
                              join z in db.Wbs_UnitProject on y.UnitProjectId equals z.UnitProjectId
                              where y.CnProfessionId == cnProfessionId && y.IsApprove == true && z.IsApprove == true && y.Flag == 1
                              group new { x.PlanValue, x.CompleteValue, x.RealValue } by new { z.UnitProjectId, x.Months }
                                  into g
                              select new
                              {
                                  ToWbs = g.Key.UnitProjectId,
                                  Months = g.Key.Months,
                                  PlanValue = g.Sum(x => x.PlanValue ?? 0),
                                  CompleteValue = g.Sum(x => x.CompleteValue ?? 0),
                                  RealValue = g.Sum(x => x.RealValue ?? 0)
                              };
            if (wbsSetList1.Count() > 0)
            {
                foreach (var wbsSet in wbsSetList1)
                {
                    AddWbsSetDetail(wbsSet.ToWbs, Convert.ToDateTime(wbsSet.Months), Convert.ToDecimal(wbsSet.PlanValue), Convert.ToDecimal(wbsSet.CompleteValue), Convert.ToDecimal(wbsSet.RealValue), 3);
                }
            }
            var wbsSetList0 = from x in db.WbsDetail
                              join y in db.Wbs_UnitProject on x.ToWbs equals y.UnitProjectId
                              //join z in db.Wbs_UnitProject on y.SuperUnitProjectId equals z.UnitProjectId
                              where y.CnProfessionId == cnProfessionId && y.IsApprove == true && y.SuperUnitProjectId != null
                              group new { x.PlanValue, x.CompleteValue, x.RealValue } by new { y.SuperUnitProjectId, x.Months }
                                  into g
                              select new
                              {
                                  ToWbs = g.Key.SuperUnitProjectId,
                                  Months = g.Key.Months,
                                  PlanValue = g.Sum(x => x.PlanValue ?? 0),
                                  CompleteValue = g.Sum(x => x.CompleteValue ?? 0),
                                  RealValue = g.Sum(x => x.RealValue ?? 0)
                              };
            if (wbsSetList0.Count() > 0)
            {
                foreach (var wbsSet in wbsSetList0)
                {
                    AddWbsSetDetail(wbsSet.ToWbs, Convert.ToDateTime(wbsSet.Months), Convert.ToDecimal(wbsSet.PlanValue), Convert.ToDecimal(wbsSet.CompleteValue), Convert.ToDecimal(wbsSet.RealValue), 3);
                }
            }
            // 专业
            var wbsSetList5 = from x in db.WbsDetail
                              join y in db.Wbs_UnitProject on x.ToWbs equals y.UnitProjectId
                              join z in db.WBS_CnProfession on y.CnProfessionId equals z.CnProfessionId
                              where y.CnProfessionId == cnProfessionId && y.IsApprove == true && y.SuperUnitProjectId == null
                              group new { x.PlanValue, x.CompleteValue, x.RealValue } by new { z.CnProfessionId, x.Months }
                                  into g
                              select new
                              {
                                  ToWbs = g.Key.CnProfessionId,
                                  Months = g.Key.Months,
                                  PlanValue = g.Sum(x => x.PlanValue ?? 0),
                                  CompleteValue = g.Sum(x => x.CompleteValue ?? 0),
                                  RealValue = g.Sum(x => x.RealValue ?? 0)
                              };
            if (wbsSetList5.Count() > 0)
            {
                foreach (var wbsSet in wbsSetList5)
                {
                    AddWbsSetDetail(wbsSet.ToWbs, Convert.ToDateTime(wbsSet.Months), Convert.ToDecimal(wbsSet.PlanValue), Convert.ToDecimal(wbsSet.CompleteValue), Convert.ToDecimal(wbsSet.RealValue), 2);
                }
            }
        }
        private void AddWbsSetDetail(string toWbs, DateTime months, decimal planValue, decimal completeValue, decimal realValue, int toFlag)
        {
            Model.SGGLDB db = Funs.DB;
            decimal? weight = null;
            if (toFlag == 4)
            {
                weight = BLL.WbsSetService.GetWbsSetByWbsSetId(toWbs).WeightsMoney;
            }
            if (toFlag == 3)
            {
                weight = BLL.UnitProjectService.GetUnitProjectByUnitProjectId(toWbs).WeightsMoney;
            }
            if (toFlag == 2)
            {
                weight = BLL.CnProfessionService.GetCnProfessionByCnProfessionId(toWbs).WeightsMoney;
            }
            if (toFlag == 1)
            {
                weight = BLL.Project_InstallationService.GetInstallationByInstallationId(toWbs).WeightsMoney;
            }
            if (BLL.WbsDetailService.IsExitWbsDetailByToWbsOrMonth(toWbs, toFlag, months))
            {
                Model.WbsDetail updateDetail = (from x in db.WbsDetail where x.ToWbs == toWbs && x.Months == months && x.ToFlag == toFlag select x).FirstOrDefault();
                updateDetail.PlanValue = planValue;
                updateDetail.CompleteValue = completeValue;
                updateDetail.RealValue = realValue;
                if (weight != null && weight != 0)
                {
                    updateDetail.PlanValueRate = decimal.Round(Convert.ToDecimal(planValue) / Convert.ToDecimal(weight) * 100, 2);
                    updateDetail.CompleteValueRate = decimal.Round(Convert.ToDecimal(completeValue) / Convert.ToDecimal(weight) * 100, 2);
                    updateDetail.RealValueRate = decimal.Round(Convert.ToDecimal(realValue) / Convert.ToDecimal(weight) * 100, 2);
                }
                db.SubmitChanges();
            }
            else
            {
                Model.WbsDetail newDetail = new Model.WbsDetail();
                newDetail.WbsDetailId = SQLHelper.GetNewID(typeof(Model.WbsDetail));
                newDetail.ToWbs = toWbs;
                newDetail.ToFlag = toFlag;
                newDetail.Way = "A";
                newDetail.Months = months;
                newDetail.PlanValue = planValue;
                if (weight != null && weight != 0)
                {
                    newDetail.PlanValueRate = decimal.Round(Convert.ToDecimal(planValue) / Convert.ToDecimal(weight) * 100, 2);
                }
                newDetail.CompleteValue = 0;
                newDetail.RealValue = 0;
                db.WbsDetail.InsertOnSubmit(newDetail);
                db.SubmitChanges();
            }
        }
        #endregion
        #region 一键保存
        /// 
        /// 一键保存
        /// 
        /// 
        /// 
        protected void btnSaveInstrallion_Click(object sender, EventArgs e)
        {
            if (this.trWBS.SelectedNode != null)
            {
                if (this.trWBS.SelectedNode.CommandName == "installation")
                {
                    string installationId = this.trWBS.SelectedNodeID;
                    if (!IsWeightAudit(installationId))
                    {
                        Alert.ShowInTop("该装置下有未审核的权重设置!", MessageBoxIcon.Warning);
                        return;
                    }
                    //专业
                    var profession = BLL.CnProfessionService.GetCnProfessionByInstallation(installationId);
                    foreach (var pro in profession)
                    {
                        MergeWbsSetDetail(pro.CnProfessionId);
                    }
                    var insList = from x in Funs.DB.WbsDetail
                                  join y in Funs.DB.WBS_CnProfession on x.ToWbs equals y.CnProfessionId
                                  where y.InstallationId == installationId
                                  group new { x.PlanValue, x.CompleteValue, x.RealValue } by new { y.InstallationId, x.Months }
                                      into g
                                  select new
                                  {
                                      ToWbs = g.Key.InstallationId,
                                      Months = g.Key.Months,
                                      PlanValue = g.Sum(x => x.PlanValue ?? 0),
                                      CompleteValue = g.Sum(x => x.CompleteValue ?? 0),
                                      RealValue = g.Sum(x => x.RealValue ?? 0)
                                  };
                    if (insList.Count() > 0)
                    {
                        foreach (var ins in insList)
                        {
                            AddWbsSetDetail(ins.ToWbs, Convert.ToDateTime(ins.Months), Convert.ToDecimal(ins.PlanValue), Convert.ToDecimal(ins.CompleteValue), Convert.ToDecimal(ins.RealValue), 1);
                        }
                    }
                    Alert.ShowInTop("一键保存成功!", MessageBoxIcon.Warning);
                }
                else
                {
                    Alert.ShowInTop("请选择一个装置!", MessageBoxIcon.Warning);
                    return;
                }
            }
            else
            {
                Alert.ShowInTop("请选择装置!", MessageBoxIcon.Warning);
                return;
            }
        }
        #endregion
    }
}