using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BLL;
using System.IO;
using System.Data.SqlClient;
using Newtonsoft.Json.Linq;
using AspNet = System.Web.UI.WebControls;
using Model;

namespace FineUIPro.Web.CQMS.WBS
{
    public partial class ProjectControlPoint : PageBase
    {
        /// <summary>
        /// 被选择项列表
        /// </summary>
        public List<string> SelectedList
        {
            get
            {
                return (List<string>)ViewState["SelectedList"];
            }
            set
            {
                ViewState["SelectedList"] = value;
            }
        }

        /// <summary>
        /// 未被选择项列表
        /// </summary>
        public List<string> NoSelectedList
        {
            get
            {
                return (List<string>)ViewState["NoSelectedList"];
            }
            set
            {
                ViewState["NoSelectedList"] = value;
            }
        }

        #region  页面加载
        /// <summary>
        /// 页面加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.SelectedList = new List<string>();
                this.NoSelectedList = new List<string>();
                GetButtonPower();
                InitTreeMenu();
                this.Grid1.Columns[2].Hidden = false;
                this.Grid1.Columns[3].Hidden = true;
                this.Grid1.Columns[4].Hidden = true;
                this.Grid1.Columns[5].Hidden = false;
                this.Grid1.Columns[7].Hidden = false;
                this.Grid1.Columns[8].Hidden = false;
                this.Grid1.Columns[9].Hidden = false;
                this.Grid1.Columns[10].Hidden = false;
                this.Grid1.Columns[11].Hidden = false;
            }
        }
        #endregion

        #region  加载树
        /// <summary>
        /// 加载树
        /// </summary>
        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 unitWorks = from x in Funs.DB.WBS_UnitWork where x.ProjectId == this.CurrUser.LoginProjectId orderby x.UnitWorkCode select x;
            foreach (var q in unitWorks)
            {
                TreeNode newNode = new TreeNode();
                newNode.Text = q.UnitWorkName;
                newNode.NodeID = q.UnitWorkId;
                newNode.CommandName = "UnitWork";
                newNode.EnableExpandEvent = true;
                newNode.EnableClickEvent = true;
                this.trWBS.Nodes.Add(newNode);
                TreeNode emptyNode = new TreeNode();
                emptyNode.Text = "";
                emptyNode.NodeID = "";
                newNode.Nodes.Add(emptyNode);
            }
        }
        #endregion

        #region  展开树
        /// <summary>
        /// 展开树
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void trWBS_NodeExpand(object sender, TreeNodeEventArgs e)
        {
            e.Node.Nodes.Clear();
            if (e.Node.CommandName == "UnitWork")   //展开工程类型
            {
                TreeNode newNode1 = new TreeNode();
                newNode1.Text = "建筑工程";
                newNode1.NodeID = e.NodeID + "|" + "1";
                newNode1.CommandName = "ProjectType";
                newNode1.EnableExpandEvent = true;
                newNode1.EnableClickEvent = true;
                e.Node.Nodes.Add(newNode1);
                TreeNode tempNode1 = new TreeNode();
                tempNode1.NodeID = "";
                tempNode1.Text = "";
                tempNode1.EnableExpandEvent = true;
                tempNode1.EnableClickEvent = true;
                newNode1.Nodes.Add(tempNode1);
                TreeNode newNode2 = new TreeNode();
                newNode2.Text = "安装工程";
                newNode2.NodeID = e.NodeID + "|" + "2";
                newNode2.CommandName = "ProjectType";
                newNode2.EnableExpandEvent = true;
                newNode2.EnableClickEvent = true;
                e.Node.Nodes.Add(newNode2);
                TreeNode tempNode2 = new TreeNode();
                tempNode2.NodeID = "";
                tempNode2.Text = "";
                tempNode2.EnableExpandEvent = true;
                tempNode2.EnableClickEvent = true;
                newNode2.Nodes.Add(tempNode2);
            }
            else if (e.Node.CommandName == "ProjectType")   //展开工程类型
            {
                if (e.NodeID.Split('|')[1] == "1")
                {
                    var cNProfessional = (from x in BLL.Funs.DB.Base_CNProfessional where x.CNProfessionalId == Const.CNProfessionalCVId orderby x.SortIndex select x).ToList();
                    foreach (var c in cNProfessional)
                    {
                        TreeNode newCNProfessionalNode = new TreeNode();
                        newCNProfessionalNode.Text = c.ProfessionalName;
                        newCNProfessionalNode.NodeID = e.NodeID.Split('|')[0] + "|" + c.CNProfessionalId;
                        newCNProfessionalNode.CommandName = "CNProfessional";
                        newCNProfessionalNode.EnableExpandEvent = true;
                        newCNProfessionalNode.EnableClickEvent = true;
                        e.Node.Nodes.Add(newCNProfessionalNode);
                        TreeNode tempNode = new TreeNode();
                        tempNode.NodeID = "";
                        tempNode.Text = "";
                        tempNode.EnableExpandEvent = true;
                        tempNode.EnableClickEvent = true;
                        newCNProfessionalNode.Nodes.Add(tempNode);
                    }
                }
                else
                {
                    var cNProfessional = (from x in BLL.Funs.DB.Base_CNProfessional where x.CNProfessionalId != Const.CNProfessionalConstructId && x.CNProfessionalId != Const.CNProfessionalCVId orderby x.SortIndex select x).ToList();
                    foreach (var c in cNProfessional)
                    {
                        TreeNode newCNProfessionalNode = new TreeNode();
                        newCNProfessionalNode.Text = c.ProfessionalName;
                        newCNProfessionalNode.NodeID = e.NodeID.Split('|')[0] + "|" + c.CNProfessionalId;
                        newCNProfessionalNode.CommandName = "CNProfessional";
                        newCNProfessionalNode.EnableExpandEvent = true;
                        newCNProfessionalNode.EnableClickEvent = true;
                        e.Node.Nodes.Add(newCNProfessionalNode);
                        TreeNode tempNode = new TreeNode();
                        tempNode.NodeID = "";
                        tempNode.Text = "";
                        tempNode.EnableExpandEvent = true;
                        tempNode.EnableClickEvent = true;
                        newCNProfessionalNode.Nodes.Add(tempNode);
                    }
                }
            }
            else if (e.Node.CommandName == "CNProfessional")   //展开专业
            {
                string unitWorkId = e.Node.ParentNode.NodeID;
                if (string.IsNullOrEmpty(unitWorkId))
                {
                    unitWorkId = e.Node.ParentNode.ParentNode.NodeID;
                }
                Model.WBS_UnitWork unitWork1 = BLL.UnitWorkService.GetUnitWorkByUnitWorkId(unitWorkId);
                if (unitWork1 == null)
                {
                    unitWorkId = e.Node.ParentNode.ParentNode.NodeID;
                }
                string cNProfessionalId = e.NodeID;
                if (e.NodeID.Contains("|"))
                {
                    cNProfessionalId = e.NodeID.Split('|')[1];
                }
                if (cNProfessionalId == BLL.Const.CNProfessionalConstructId)
                {
                    var divisionsCV = (from x in BLL.Funs.DB.WBS_DivisionProject
                                       where x.CNProfessionalId == BLL.Const.CNProfessionalCVId && x.ProjectId == this.CurrUser.LoginProjectId && x.SuperDivisionId == null && x.UnitWorkId == unitWorkId && x.IsSelected == true
                                       orderby x.SortIndex
                                       select x).ToList();
                    if (divisionsCV.Count > 0)   //建筑工程下存在土建内容
                    {
                        TreeNode newNode = new TreeNode();
                        newNode.Text = "土建";
                        newNode.NodeID = BLL.Const.CNProfessionalCVId;
                        newNode.CommandName = "CNProfessional";
                        newNode.EnableExpandEvent = true;
                        newNode.EnableClickEvent = true;
                        e.Node.Nodes.Add(newNode);
                        TreeNode tempNode = new TreeNode();
                        tempNode.NodeID = "";
                        tempNode.Text = "";
                        tempNode.EnableExpandEvent = true;
                        tempNode.EnableClickEvent = true;
                        newNode.Nodes.Add(tempNode);
                    }
                    else
                    {
                        var divisions = (from x in BLL.Funs.DB.WBS_DivisionProject
                                         where x.CNProfessionalId == cNProfessionalId && x.ProjectId == this.CurrUser.LoginProjectId && x.SuperDivisionId == null && x.UnitWorkId == unitWorkId
                                         orderby x.SortIndex
                                         select x).ToList();
                        foreach (var q in divisions)
                        {
                            TreeNode newNode = new TreeNode();
                            newNode.Text = q.DivisionName;
                            newNode.NodeID = q.DivisionProjectId;
                            newNode.CommandName = "DivisionProject";
                            newNode.EnableExpandEvent = true;
                            newNode.EnableClickEvent = true;
                            newNode.EnableCheckEvent = true;
                            e.Node.Nodes.Add(newNode);
                            if (q.IsSelected == true)
                            {
                                newNode.Checked = true;
                            }
                            var list = (from x in Funs.DB.WBS_DivisionProject
                                        where x.SuperDivisionId == q.DivisionProjectId && x.ProjectId == this.CurrUser.LoginProjectId
                                        orderby x.SortIndex
                                        select x).ToList();
                            if (list.Count > 0)
                            {
                                TreeNode tempNode = new TreeNode();
                                tempNode.NodeID = "";
                                tempNode.Text = "";
                                newNode.Nodes.Add(tempNode);
                            }

                        }
                    }
                }
                else
                {
                    var divisions = (from x in BLL.Funs.DB.WBS_DivisionProject
                                     where x.CNProfessionalId == cNProfessionalId && x.ProjectId == this.CurrUser.LoginProjectId && x.SuperDivisionId == null && x.UnitWorkId == unitWorkId
                                     orderby x.SortIndex
                                     select x).ToList();
                    foreach (var q in divisions)
                    {
                        TreeNode newNode = new TreeNode();
                        newNode.Text = q.DivisionName;
                        newNode.NodeID = q.DivisionProjectId;
                        newNode.CommandName = "DivisionProject";
                        newNode.EnableExpandEvent = true;
                        newNode.EnableClickEvent = true;
                        newNode.EnableCheckBox = true;
                        newNode.EnableCheckEvent = true;
                        e.Node.Nodes.Add(newNode);
                        if (q.IsSelected == true)
                        {
                            newNode.Checked = true;
                        }
                        var list = (from x in Funs.DB.WBS_DivisionProject
                                    where x.SuperDivisionId == q.DivisionProjectId && x.ProjectId == this.CurrUser.LoginProjectId
                                    orderby x.SortIndex
                                    select x).ToList();
                        if (list.Count > 0)
                        {
                            TreeNode tempNode = new TreeNode();
                            tempNode.NodeID = "";
                            tempNode.Text = "";
                            newNode.Nodes.Add(tempNode);
                        }

                    }
                }
            }
            else if (e.Node.CommandName == "DivisionProject")   //展开分部节点
            {
                var childDivisions = (from x in BLL.Funs.DB.WBS_DivisionProject
                                      where x.SuperDivisionId == e.Node.NodeID && x.ProjectId == this.CurrUser.LoginProjectId
                                      orderby x.SortIndex
                                      select x).ToList();
                foreach (var q in childDivisions)
                {
                    TreeNode newNode = new TreeNode();
                    newNode.Text = q.DivisionName;
                    newNode.NodeID = q.DivisionProjectId;
                    newNode.CommandName = "DivisionProject";
                    newNode.EnableExpandEvent = true;
                    newNode.EnableClickEvent = true;
                    newNode.EnableCheckBox = true;
                    newNode.EnableCheckEvent = true;
                    e.Node.Nodes.Add(newNode);
                    if (q.IsSelected == true)
                    {
                        newNode.Checked = true;
                    }
                    var list = (from x in Funs.DB.WBS_DivisionProject
                                where x.SuperDivisionId == q.DivisionProjectId && x.ProjectId == this.CurrUser.LoginProjectId
                                orderby x.SortIndex
                                select x).ToList();
                    if (list.Count > 0)
                    {
                        TreeNode tempNode = new TreeNode();
                        tempNode.NodeID = "";
                        tempNode.Text = "";
                        newNode.Nodes.Add(tempNode);
                    }
                }
            }
        }
        #endregion

        #region  树节点复选框勾选事件
        /// <summary>
        /// 树节点复选框勾选事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void trWBS_NodeCheck(object sender, FineUIPro.TreeCheckEventArgs e)
        {
            if (BLL.CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.ProjectControlPointMenuId, BLL.Const.BtnSave))
            {
                Model.WBS_DivisionProject divisionProject = BLL.DivisionProjectService.GetDivisionProjectById(e.NodeID);
                divisionProject.IsSelected = e.Checked;
                BLL.DivisionProjectService.UpdateDivisionProject(divisionProject);
                this.UpdateParentDivision(divisionProject.SuperDivisionId, e.Checked);
                BindGrid();
            }
            else
            {
                ShowNotify("您没有保存(勾选)这个权限,请与管理员联系!", MessageBoxIcon.Warning);
            }
        }

        /// <summary>
        /// 更新分部的选择状态
        /// </summary>
        /// <param name="superDivisionId"></param>
        private void UpdateParentDivision(string superDivisionId, bool b)
        {
            Model.WBS_DivisionProject superDivisionProject = BLL.DivisionProjectService.GetDivisionProjectById(superDivisionId);
            if (superDivisionProject != null)
            {
                if (string.IsNullOrEmpty(superDivisionProject.SuperDivisionId))
                {
                //    if (!string.IsNullOrEmpty(superDivisionProject.CNProfessionalId))
                //{
                    superDivisionProject.IsSelected = b;
                    BLL.DivisionProjectService.UpdateDivisionProject(superDivisionProject);
                }
                else
                {
                    superDivisionProject.IsSelected = b;
                    BLL.DivisionProjectService.UpdateDivisionProject(superDivisionProject);
                    this.UpdateParentDivision(superDivisionProject.SuperDivisionId, b);
                }
            }
        }
        #endregion

        #region  Tree点击事件
        /// <summary>
        /// Tree点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void trWBS_NodeCommand(object sender, TreeCommandEventArgs e)
        {
            for (int i = 0; i < this.Grid1.Rows.Count; i++)
            {
				if (this.Grid1.SelectedRowIDArray.Contains(this.Grid1.Rows[i].RowID))
				{
					if (!SelectedList.Contains(this.Grid1.Rows[i].RowID))
					{
						SelectedList.Add(this.Grid1.Rows[i].RowID);
					}
				}
				else
				{
					if (!NoSelectedList.Contains(this.Grid1.Rows[i].RowID))
					{
						NoSelectedList.Add(this.Grid1.Rows[i].RowID);
					}
				}
            }
            string divisionProjectId = this.trWBS.SelectedNode.NodeID;
            var temp = BLL.DivisionProjectService.GetDivisionProjectById(divisionProjectId);
            if (temp == null)
            {
                return;
            }
            if (temp.CNProfessionalId != null && temp.CNProfessionalId == Const.CNProfessionalConstructId)
            {
                this.Grid1.Columns[2].Hidden = false;
                this.Grid1.Columns[3].Hidden = true;
                this.Grid1.Columns[4].Hidden = true;
                this.Grid1.Columns[5].Hidden = false;
                this.Grid1.Columns[7].Hidden = false;
                this.Grid1.Columns[8].Hidden = false;
                this.Grid1.Columns[9].Hidden = false;
                this.Grid1.Columns[10].Hidden = false;
                this.Grid1.Columns[11].Hidden = false;
                this.Grid1.Columns[12].Hidden = false;
            }
            else
            {
                this.Grid1.Columns[2].Hidden = false;
                this.Grid1.Columns[3].Hidden = false;
                this.Grid1.Columns[4].Hidden = false;
                this.Grid1.Columns[5].Hidden = true;
                this.Grid1.Columns[7].Hidden = true;
                this.Grid1.Columns[8].Hidden = true;
                this.Grid1.Columns[9].Hidden = true;
                this.Grid1.Columns[10].Hidden = true;
                this.Grid1.Columns[11].Hidden = true;
                this.Grid1.Columns[12].Hidden = true;
            }
            BindGrid();
        }
        #endregion

        #region  修改关闭窗口
        /// <summary>
        /// 关闭窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Window1_Close(object sender, WindowCloseEventArgs e)
        {
            ShowNotify("修改成功!", MessageBoxIcon.Success);

            GetSelectTreeNode();
        }
        #endregion

        #region  增加关闭窗口
        /// <summary>
        /// 增加关闭窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Window2_Close(object sender, WindowCloseEventArgs e)
        {
            ShowNotify("增加成功!", MessageBoxIcon.Success);

            GetSelectTreeNode();
        }
        #endregion

        #region  关闭窗口
        /// <summary>
        /// 关闭窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Window3_Close(object sender, WindowCloseEventArgs e)
        {
            ShowNotify("保存成功!", MessageBoxIcon.Success);
            BindGrid();
        }
        #endregion

        #region 右键增加、修改、删除方法
        /// <summary>
        /// 右键修改事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnMenuEdit_Click(object sender, EventArgs e)
        {
            if (this.trWBS.SelectedNode != null)
            {
                if (BLL.CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.ProjectControlPointMenuId, BLL.Const.BtnModify))
                {
                    if (this.trWBS.SelectedNode.CommandName != "ProjectType" && this.trWBS.SelectedNode.CommandName != "UnitWork" && this.trWBS.SelectedNode.CommandName != "CNProfessional")   //非工程类型和单位工程、专业节点可以修改
                    {
                        this.hdSelectId.Text = this.trWBS.SelectedNode.NodeID;
                        string unitWorkId = this.GetUnitWorkId(this.trWBS.SelectedNode);
                        string openUrl = String.Format("EditDivisionProject.aspx?type=modify&selectedCode={0}&unitWorkId={1}", this.trWBS.SelectedNode.NodeID, unitWorkId, "编辑 - ");
                        PageContext.RegisterStartupScript(Window1.GetSaveStateReference(hdSelectId.ClientID)
                                    + Window1.GetShowReference(openUrl));
                    }
                    else
                    {
                        Alert.ShowInTop("单位工程、工程类型和专业节点无法修改!", MessageBoxIcon.Warning);
                    }
                }
                else
                {
                    Alert.ShowInTop("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
                }
            }
            else
            {
                Alert.ShowInTop("请选择树节点!", MessageBoxIcon.Warning);
            }
        }

        /// <summary>
        /// 根据选择节点获取单位工程Id
        /// </summary>
        /// <param name="node"></param>
        private string GetUnitWorkId(TreeNode node)
        {
            string id = string.Empty;
            TreeNode parentNode = node.ParentNode;
            if (parentNode != null)
            {
                Model.WBS_UnitWork i = BLL.UnitWorkService.GetUnitWorkByUnitWorkId(parentNode.NodeID);
                if (i != null)
                {
                    id = i.UnitWorkId;
                }
                else
                {
                    id = this.GetUnitWorkId(parentNode);
                }
            }
            return id;
        }

        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnMenuAdd_Click(object sender, EventArgs e)
        {
            if (this.trWBS.SelectedNode != null)
            {
                if (BLL.CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.ProjectControlPointMenuId, BLL.Const.BtnAdd))
                {
                    if (this.trWBS.SelectedNode.CommandName != "ProjectType" && this.trWBS.SelectedNode.CommandName != "UnitWork")   //非工程类型和单位工程节点可以增加
                    {
                        string unitWorkId = this.GetUnitWorkId(this.trWBS.SelectedNode);
                        string openUrl = String.Format("EditDivisionProject.aspx?type=add&selectedCode={0}&unitWorkId={1}", this.trWBS.SelectedNode.NodeID, unitWorkId, "增加 - ");
                        PageContext.RegisterStartupScript(Window2.GetSaveStateReference(hdSelectId.ClientID)
                                + Window2.GetShowReference(openUrl));
                    }
                    else
                    {
                        ShowNotify("单位工程、工程类型节点无法增加!", MessageBoxIcon.Warning);
                    }
                }
                else
                {
                    ShowNotify("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
                }
            }
            else
            {
                ShowNotify("请选择树节点!", MessageBoxIcon.Warning);
            }
        }

        /// <summary>
        /// 右键删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnMenuDelete_Click(object sender, EventArgs e)
        {
            if (this.trWBS.SelectedNode != null)
            {
                if (BLL.CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.ProjectControlPointMenuId, BLL.Const.BtnDelete))
                {
                    if (this.trWBS.SelectedNode.CommandName != "ProjectType" && this.trWBS.SelectedNode.CommandName != "UnitWork" && this.trWBS.SelectedNode.CommandName != "CNProfessional")   //非工程类型和单位工程、专业节点可以删除
                    {
                        string id = this.trWBS.SelectedNodeID;
                        //var workPackage = Funs.DB.WBS_WorkPackage.FirstOrDefault(x => x.ProjectId == this.CurrUser.LoginProjectId && x.InitWorkPackageCode == this.trWBS.SelectedNodeID);
                        //if (workPackage != null)
                        //{
                        //    ShowNotify("WBS定制中已使用该数据,无法删除!", MessageBoxIcon.Warning);
                        //}
                        //else
                        //{
                        DeleteData();
                        //}
                    }
                    else
                    {
                        ShowNotify("单位工程、工程类型和专业节点无法删除!", MessageBoxIcon.Warning);
                    }
                }
                else
                {
                    ShowNotify("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
                }
            }
            else
            {
                ShowNotify("请选择树节点!", MessageBoxIcon.Warning);
            }
        }

        /// <summary>
        /// 删除方法
        /// </summary>
        private void DeleteData()
        {
            string id = this.trWBS.SelectedNodeID;
            this.hdSelectId.Text = this.trWBS.SelectedNode.ParentNode.NodeID;
            if (this.trWBS.SelectedNode.CommandName == "DivisionProject")
            {
                string divisionProjectId = this.trWBS.SelectedNode.NodeID;
                Model.WBS_DivisionProject divisionProject = BLL.DivisionProjectService.GetDivisionProjectById(divisionProjectId);
                this.hdUnitWorkId.Text = divisionProject.UnitWorkId;
                if (divisionProject != null)
                {
                    if (!string.IsNullOrEmpty(divisionProject.CNProfessionalId))    //删除分部
                    {
                        var q = from x in Funs.DB.WBS_DivisionProject where x.SuperDivisionId == divisionProjectId select x;
                        if (q.Count() > 0)     //分部含有子分部
                        {
                            foreach (var d in q)
                            {
                                var q2 = from x in Funs.DB.WBS_DivisionProject where x.SuperDivisionId == d.DivisionProjectId select x;
                                if (q2.Count() > 0)     //子分部还有子级
                                {
                                    foreach (var d2 in q2)
                                    {
                                        BLL.BreakdownProjectService.DeleteAllBreakdown(d2.DivisionProjectId);
                                        BLL.DivisionProjectService.DeleteDivisionProject(d2.DivisionProjectId);
                                    }
                                }
                                BLL.BreakdownProjectService.DeleteAllBreakdown(d.DivisionProjectId);
                                BLL.DivisionProjectService.DeleteDivisionProject(d.DivisionProjectId);
                            }
                        }
                        else          //分部不包含子分部
                        {
                            BLL.BreakdownProjectService.DeleteAllBreakdown(divisionProjectId);
                        }
                        BLL.DivisionProjectService.DeleteDivisionProject(divisionProjectId);
                    }
                    else   //删除子分部
                    {
                        var q = from x in Funs.DB.WBS_DivisionProject where x.SuperDivisionId == divisionProjectId select x;
                        if (q.Count() > 0)     //子分部含有子级
                        {
                            foreach (var d in q)
                            {
                                BLL.BreakdownProjectService.DeleteAllBreakdown(d.DivisionProjectId);
                                BLL.DivisionProjectService.DeleteDivisionProject(d.DivisionProjectId);
                            }
                        }
                        else          //子分部不包含子级
                        {
                            BLL.BreakdownProjectService.DeleteAllBreakdown(divisionProjectId);
                        }
                        BLL.DivisionProjectService.DeleteDivisionProject(divisionProjectId);
                    }
                }
                BLL.LogService.AddSys_Log(this.CurrUser, id, id, BLL.Const.ProjectControlPointMenuId, "删除分部或子分部工程及其下所有内容!");
            }
            ShowNotify("删除成功!", MessageBoxIcon.Success);
            GetSelectTreeNode();
        }
        #endregion

        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnNew_Click(object sender, EventArgs e)
        {
            if (this.trWBS.SelectedNode != null)
            {
                //if (this.trWBS.SelectedNode.Nodes.Count == 0)   //末级节点
                if (this.trWBS.SelectedNode.CommandName == "DivisionProject")
                {
                    PageContext.RegisterStartupScript(Window3.GetShowReference(String.Format("EditBreakdownProject.aspx?type=add&divisionProjectId={0}", this.trWBS.SelectedNode.NodeID, "新增 - ")));
                }
                else
                {
                    Alert.ShowInTop("不是末级,无法添加分项!", MessageBoxIcon.Warning);
                }
            }
            else
            {
                Alert.ShowInTop("请选择树节点!", MessageBoxIcon.Warning);
            }
        }

        #region Grid双击事件
        /// <summary>
        /// Grid行双击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e)
        {
            btnMenuModify_Click(null, null);
        }
        #endregion
        #region 编辑
        /// <summary>
        /// 编辑按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnMenuModify_Click(object sender, EventArgs e)
        {
            if (Grid1.SelectedRowIndexArray.Length == 0)
            {
                Alert.ShowInTop("请至少选择一条记录!", MessageBoxIcon.Warning);
                return;
            }
            PageContext.RegisterStartupScript(Window3.GetShowReference(String.Format("EditBreakdownProject.aspx?type=modify&breakdownProjectId={0}&divisionProjectId={1}", this.Grid1.SelectedRowID, this.trWBS.SelectedNodeID, "新增 - ")));
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnMenuDel_Click(object sender, EventArgs e)
        {
            if (Grid1.SelectedRowIndexArray.Length == 0)
            {
                Alert.ShowInTop("请至少选择一条记录!", MessageBoxIcon.Warning);
                return;
            }
            //var controlItemAndCycle = Funs.DB.WBS_ControlItemAndCycle.FirstOrDefault(x => x.ProjectId == this.CurrUser.LoginProjectId && x.InitControlItemCode == Grid1.SelectedRowID);
            //if (controlItemAndCycle != null)
            //{
            //    ShowNotify("WBS定制中已使用该数据,无法删除!", MessageBoxIcon.Warning);
            //}
            //else
            //{
            BLL.BreakdownProjectService.DeleteBreakdown(Grid1.SelectedRowID);
            BLL.LogService.AddSys_Log(this.CurrUser, Grid1.SelectedRowID, Grid1.SelectedRowID, BLL.Const.ProjectControlPointMenuId, "删除分项");
            Grid1.DataBind();
            BindGrid();
            Alert.ShowInTop("删除数据成功!", MessageBoxIcon.Success);
            //}
        }
        #endregion

        #region  绑定数据
        /// <summary>
        /// 绑定数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Grid1_FilterChange(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }

        /// <summary>
        /// Grid1排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            Grid1.SortDirection = e.SortDirection;
            Grid1.SortField = e.SortField;
            BindGrid();
        }

        /// <summary>
        /// 分页下拉选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
            BindGrid();
        }

        /// <summary>
        /// 加载Grid
        /// </summary>
        private void BindGrid()
        {
			 
			string strSql = @"SELECT BreakdownProjectId,BreakdownCode,BreakdownName,Basis,CheckPoints,RecordAndCode,Class,FenBao,WuHuan,JianLi,YeZhu,Remark,ModelURL,SortIndex"
					 + @" FROM WBS_BreakdownProject ";
            List<SqlParameter> listStr = new List<SqlParameter>();
            strSql += " where DivisionProjectId = @DivisionProjectId and ProjectId=@ProjectId";
            listStr.Add(new SqlParameter("@DivisionProjectId", this.trWBS.SelectedNodeID));
            listStr.Add(new SqlParameter("@ProjectId", this.CurrUser.LoginProjectId));
            SqlParameter[] parameter = listStr.ToArray();
            DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);

            Grid1.RecordCount = tb.Rows.Count;
            tb = GetFilteredTable(Grid1.FilteredData, tb);
            var table = this.GetPagedDataTable(Grid1, tb);
            Grid1.DataSource = table;
            Grid1.DataBind();
            var selectIds = (from x in Funs.DB.WBS_BreakdownProject where x.DivisionProjectId == this.trWBS.SelectedNodeID && x.IsSelected == true select x.BreakdownProjectId).ToList();
            if (selectIds.Count() > 0)
            {
                this.Grid1.SelectedRowIDArray = selectIds.ToArray();
            }
        }
        #endregion

        #region 根据所给Id定位到对应分部分项
        /// <summary>
        /// 根据所给Id定位到对应具体的工程类型、单位工程、子单位工程、分部、子分部、分项、子分项
        /// </summary>
        private void GetSelectTreeNode()
        {
            string projectType = string.Empty;
            string cNProfessionalId = string.Empty;
            string projectTypeId = string.Empty;
            string unitWorkId = string.Empty;
            List<string> list = new List<string>();
            Model.WBS_DivisionProject divisionProject = BLL.DivisionProjectService.GetDivisionProjectById(this.hdSelectId.Text);
            if (divisionProject != null)
            {
                list = GetDivisionProjectIds(divisionProject);
                list.Add(this.hdSelectId.Text);
                cNProfessionalId = GetCNProfessionalId(divisionProject);
                if (cNProfessionalId == Const.CNProfessionalConstructId || cNProfessionalId == Const.CNProfessionalCVId)
                {
                    projectTypeId = Const.CNProfessionalConstructId;
                }
                else
                {
                    projectTypeId = "2";
                }
                list.Add(cNProfessionalId);
                unitWorkId = divisionProject.UnitWorkId;
            }
            else
            {
                cNProfessionalId = this.hdSelectId.Text;
                if (cNProfessionalId == Const.CNProfessionalConstructId || cNProfessionalId == Const.CNProfessionalCVId)
                {
                    projectTypeId = Const.CNProfessionalConstructId;
                }
                else
                {
                    projectTypeId = "2";
                }
                unitWorkId = this.hdUnitWorkId.Text;
            }

            InitTreeMenu();
            for (int i = 0; i < trWBS.Nodes.Count; i++)
            {
                if (trWBS.Nodes[i].NodeID == unitWorkId)
                {
                    trWBS.Nodes[i].Expanded = true;
                    trWBS.Nodes[i].Nodes.Clear();
                    TreeNode newNode1 = new TreeNode();
                    newNode1.Text = "建筑工程";
                    newNode1.NodeID = Const.CNProfessionalConstructId;
                    newNode1.CommandName = "CNProfessional";
                    newNode1.EnableExpandEvent = true;
                    newNode1.EnableClickEvent = true;
                    trWBS.Nodes[i].Nodes.Add(newNode1);
                    TreeNode newNode2 = new TreeNode();
                    newNode2.Text = "安装工程";
                    newNode2.NodeID = "2";
                    newNode2.CommandName = "ProjectType";
                    newNode2.EnableExpandEvent = true;
                    newNode2.EnableClickEvent = true;
                    trWBS.Nodes[i].Nodes.Add(newNode2);
                    if (!string.IsNullOrEmpty(projectTypeId))
                    {
                        for (int j = 0; j < trWBS.Nodes[i].Nodes.Count; j++)
                        {
                            if (trWBS.Nodes[i].Nodes[j].NodeID == projectTypeId)
                            {
                                trWBS.Nodes[i].Nodes[j].Expanded = true;
                                if (trWBS.Nodes[i].Nodes[j].NodeID == BLL.Const.CNProfessionalConstructId)  //建筑工程
                                {
                                    var divisionsCV = (from x in BLL.Funs.DB.WBS_DivisionProject
                                                       where x.CNProfessionalId == BLL.Const.CNProfessionalCVId && x.ProjectId == this.CurrUser.LoginProjectId && x.SuperDivisionId == null && x.UnitWorkId == unitWorkId && x.IsSelected == true
                                                       orderby x.SortIndex
                                                       select x).ToList();
                                    if (divisionsCV.Count > 0)   //建筑工程下存在土建内容
                                    {
                                        TreeNode newNode3 = new TreeNode();
                                        newNode3.Text = "土建";
                                        newNode3.NodeID = BLL.Const.CNProfessionalCVId;
                                        newNode3.CommandName = "CNProfessional";
                                        newNode3.EnableExpandEvent = true;
                                        newNode3.EnableClickEvent = true;
                                        trWBS.Nodes[i].Nodes[j].Nodes.Add(newNode3);
                                        newNode3.Expanded = true;
                                        var divisions = (from x in BLL.Funs.DB.WBS_DivisionProject
                                                         where x.CNProfessionalId == BLL.Const.CNProfessionalCVId && x.ProjectId == this.CurrUser.LoginProjectId && x.SuperDivisionId == null && x.UnitWorkId == unitWorkId
                                                         orderby x.SortIndex
                                                         select x).ToList();
                                        foreach (var q in divisions)
                                        {
                                            TreeNode newNode4 = new TreeNode();
                                            newNode4.Text = q.DivisionName;
                                            newNode4.NodeID = q.DivisionProjectId;
                                            newNode4.CommandName = "DivisionProject";
                                            newNode4.EnableExpandEvent = true;
                                            newNode4.EnableClickEvent = true;
                                            if (q.IsSelected == true)
                                            {
                                                newNode4.Checked = true;
                                            }
                                            newNode3.Nodes.Add(newNode4);
                                            var division1s = (from x in BLL.Funs.DB.WBS_DivisionProject
                                                              where x.ProjectId == this.CurrUser.LoginProjectId && x.SuperDivisionId == q.DivisionProjectId
                                                              orderby x.SortIndex
                                                              select x).ToList();
                                            if (list.Contains(q.DivisionProjectId))
                                            {
                                                newNode4.Expanded = true;
                                                foreach (var division1 in division1s)
                                                {
                                                    TreeNode newNode5 = new TreeNode();
                                                    newNode5.Text = division1.DivisionName;
                                                    newNode5.NodeID = division1.DivisionProjectId;
                                                    newNode5.CommandName = "DivisionProject";
                                                    newNode5.EnableExpandEvent = true;
                                                    newNode5.EnableClickEvent = true;
                                                    newNode4.Nodes.Add(newNode5);
                                                    var division2s = (from x in BLL.Funs.DB.WBS_DivisionProject
                                                                      where x.ProjectId == this.CurrUser.LoginProjectId && x.SuperDivisionId == division1.DivisionProjectId
                                                                      orderby x.SortIndex
                                                                      select x).ToList();
                                                    if (list.Contains(division1.DivisionProjectId))
                                                    {
                                                        newNode5.Expanded = true;
                                                        foreach (var division2 in division2s)
                                                        {
                                                            TreeNode newNode6 = new TreeNode();
                                                            newNode6.Text = division2.DivisionName;
                                                            newNode6.NodeID = division2.DivisionProjectId;
                                                            newNode6.CommandName = "DivisionProject";
                                                            newNode6.EnableExpandEvent = true;
                                                            newNode6.EnableClickEvent = true;
                                                            newNode5.Nodes.Add(newNode6);
                                                            var division3s = (from x in BLL.Funs.DB.WBS_DivisionProject
                                                                              where x.ProjectId == this.CurrUser.LoginProjectId && x.SuperDivisionId == division2.DivisionProjectId
                                                                              orderby x.SortIndex
                                                                              select x).ToList();
                                                            if (list.Contains(division2.DivisionProjectId))
                                                            {
                                                                newNode6.Expanded = true;
                                                            }
                                                            else
                                                            {
                                                                if (division3s.Count > 0)
                                                                {
                                                                    TreeNode tempNode2 = new TreeNode();
                                                                    tempNode2.NodeID = "";
                                                                    tempNode2.Text = "";
                                                                    newNode6.Nodes.Add(tempNode2);
                                                                }
                                                            }
                                                        }
                                                    }
                                                    else
                                                    {
                                                        if (division2s.Count > 0)
                                                        {
                                                            TreeNode tempNode2 = new TreeNode();
                                                            tempNode2.NodeID = "";
                                                            tempNode2.Text = "";
                                                            newNode5.Nodes.Add(tempNode2);
                                                        }
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                if (division1s.Count > 0)
                                                {
                                                    TreeNode tempNode2 = new TreeNode();
                                                    tempNode2.NodeID = "";
                                                    tempNode2.Text = "";
                                                    newNode4.Nodes.Add(tempNode2);
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {
                                        var divisions = (from x in BLL.Funs.DB.WBS_DivisionProject
                                                         where x.CNProfessionalId == BLL.Const.CNProfessionalConstructId && x.ProjectId == this.CurrUser.LoginProjectId && x.SuperDivisionId == null && x.UnitWorkId == unitWorkId
                                                         orderby x.SortIndex
                                                         select x).ToList();
                                        foreach (var q in divisions)
                                        {
                                            TreeNode newNode4 = new TreeNode();
                                            newNode4.Text = q.DivisionName;
                                            newNode4.NodeID = q.DivisionProjectId;
                                            newNode4.CommandName = "DivisionProject";
                                            newNode4.EnableExpandEvent = true;
                                            newNode4.EnableClickEvent = true;
                                            trWBS.Nodes[i].Nodes[j].Nodes.Add(newNode4);
                                            if (q.IsSelected == true)
                                            {
                                                newNode4.Checked = true;
                                            }
                                            var division1s = (from x in BLL.Funs.DB.WBS_DivisionProject
                                                              where x.ProjectId == this.CurrUser.LoginProjectId && x.SuperDivisionId == q.DivisionProjectId
                                                              orderby x.SortIndex
                                                              select x).ToList();
                                            if (list.Contains(q.DivisionProjectId))
                                            {
                                                newNode4.Expanded = true;
                                                foreach (var division1 in division1s)
                                                {
                                                    TreeNode newNode5 = new TreeNode();
                                                    newNode5.Text = division1.DivisionName;
                                                    newNode5.NodeID = division1.DivisionProjectId;
                                                    newNode5.CommandName = "DivisionProject";
                                                    newNode5.EnableExpandEvent = true;
                                                    newNode5.EnableClickEvent = true;
                                                    newNode4.Nodes.Add(newNode5);
                                                    var division2s = (from x in BLL.Funs.DB.WBS_DivisionProject
                                                                      where x.ProjectId == this.CurrUser.LoginProjectId && x.SuperDivisionId == division1.DivisionProjectId
                                                                      orderby x.SortIndex
                                                                      select x).ToList();
                                                    if (list.Contains(division1.DivisionProjectId))
                                                    {
                                                        newNode5.Expanded = true;
                                                        foreach (var division2 in division2s)
                                                        {
                                                            TreeNode newNode6 = new TreeNode();
                                                            newNode6.Text = division2.DivisionName;
                                                            newNode6.NodeID = division2.DivisionProjectId;
                                                            newNode6.CommandName = "DivisionProject";
                                                            newNode6.EnableExpandEvent = true;
                                                            newNode6.EnableClickEvent = true;
                                                            newNode5.Nodes.Add(newNode6);
                                                            var division3s = (from x in BLL.Funs.DB.WBS_DivisionProject
                                                                              where x.ProjectId == this.CurrUser.LoginProjectId && x.SuperDivisionId == division2.DivisionProjectId
                                                                              orderby x.SortIndex
                                                                              select x).ToList();
                                                            if (list.Contains(division2.DivisionProjectId))
                                                            {
                                                                newNode6.Expanded = true;
                                                            }
                                                            else
                                                            {
                                                                if (division3s.Count > 0)
                                                                {
                                                                    TreeNode tempNode2 = new TreeNode();
                                                                    tempNode2.NodeID = "";
                                                                    tempNode2.Text = "";
                                                                    newNode6.Nodes.Add(tempNode2);
                                                                }
                                                            }
                                                        }
                                                    }
                                                    else
                                                    {
                                                        if (division2s.Count > 0)
                                                        {
                                                            TreeNode tempNode2 = new TreeNode();
                                                            tempNode2.NodeID = "";
                                                            tempNode2.Text = "";
                                                            newNode5.Nodes.Add(tempNode2);
                                                        }
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                if (division1s.Count > 0)
                                                {
                                                    TreeNode tempNode2 = new TreeNode();
                                                    tempNode2.NodeID = "";
                                                    tempNode2.Text = "";
                                                    newNode4.Nodes.Add(tempNode2);
                                                }
                                            }
                                        }
                                    }
                                }
                                else   //安装工程
                                {
                                    var cNProfessional = (from x in BLL.Funs.DB.Base_CNProfessional where x.CNProfessionalId != Const.CNProfessionalConstructId && x.CNProfessionalId != Const.CNProfessionalCVId orderby x.SortIndex select x).ToList();
                                    foreach (var c in cNProfessional)
                                    {
                                        TreeNode newCNProfessionalNode = new TreeNode();
                                        newCNProfessionalNode.Text = c.ProfessionalName;
                                        newCNProfessionalNode.NodeID = c.CNProfessionalId;
                                        newCNProfessionalNode.CommandName = "CNProfessional";
                                        newCNProfessionalNode.EnableExpandEvent = true;
                                        newCNProfessionalNode.EnableClickEvent = true;
                                        trWBS.Nodes[i].Nodes[j].Nodes.Add(newCNProfessionalNode);
                                        if (c.CNProfessionalId == cNProfessionalId)
                                        {
                                            newCNProfessionalNode.Expanded = true;
                                            var divisions = (from x in BLL.Funs.DB.WBS_DivisionProject
                                                             where x.CNProfessionalId == c.CNProfessionalId && x.ProjectId == this.CurrUser.LoginProjectId && x.SuperDivisionId == null && x.UnitWorkId == unitWorkId
                                                             orderby x.SortIndex
                                                             select x).ToList();
                                            foreach (var q in divisions)
                                            {
                                                TreeNode newNode4 = new TreeNode();
                                                newNode4.Text = q.DivisionName;
                                                newNode4.NodeID = q.DivisionProjectId;
                                                newNode4.CommandName = "DivisionProject";
                                                newNode4.EnableExpandEvent = true;
                                                newNode4.EnableClickEvent = true;
                                                newCNProfessionalNode.Nodes.Add(newNode4);
                                                if (q.IsSelected == true)
                                                {
                                                    newNode4.Checked = true;
                                                }
                                                var division1s = (from x in BLL.Funs.DB.WBS_DivisionProject
                                                                  where x.ProjectId == this.CurrUser.LoginProjectId && x.SuperDivisionId == q.DivisionProjectId
                                                                  orderby x.SortIndex
                                                                  select x).ToList();
                                                if (list.Contains(q.DivisionProjectId))
                                                {
                                                    newNode4.Expanded = true;
                                                    foreach (var division1 in division1s)
                                                    {
                                                        TreeNode newNode5 = new TreeNode();
                                                        newNode5.Text = division1.DivisionName;
                                                        newNode5.NodeID = division1.DivisionProjectId;
                                                        newNode5.CommandName = "DivisionProject";
                                                        newNode5.EnableExpandEvent = true;
                                                        newNode5.EnableClickEvent = true;
                                                        newNode4.Nodes.Add(newNode5);
                                                        var division2s = (from x in BLL.Funs.DB.WBS_DivisionProject
                                                                          where x.ProjectId == this.CurrUser.LoginProjectId && x.SuperDivisionId == division1.DivisionProjectId
                                                                          orderby x.SortIndex
                                                                          select x).ToList();
                                                        if (list.Contains(division1.DivisionProjectId))
                                                        {
                                                            newNode5.Expanded = true;
                                                            foreach (var division2 in division2s)
                                                            {
                                                                TreeNode newNode6 = new TreeNode();
                                                                newNode6.Text = division2.DivisionName;
                                                                newNode6.NodeID = division2.DivisionProjectId;
                                                                newNode6.CommandName = "DivisionProject";
                                                                newNode6.EnableExpandEvent = true;
                                                                newNode6.EnableClickEvent = true;
                                                                newNode5.Nodes.Add(newNode6);
                                                                var division3s = (from x in BLL.Funs.DB.WBS_DivisionProject
                                                                                  where x.ProjectId == this.CurrUser.LoginProjectId && x.SuperDivisionId == division2.DivisionProjectId
                                                                                  orderby x.SortIndex
                                                                                  select x).ToList();
                                                                if (list.Contains(division2.DivisionProjectId))
                                                                {
                                                                    newNode6.Expanded = true;
                                                                }
                                                                else
                                                                {
                                                                    if (division3s.Count > 0)
                                                                    {
                                                                        TreeNode tempNode2 = new TreeNode();
                                                                        tempNode2.NodeID = "";
                                                                        tempNode2.Text = "";
                                                                        newNode6.Nodes.Add(tempNode2);
                                                                    }
                                                                }
                                                            }
                                                        }
                                                        else
                                                        {
                                                            if (division2s.Count > 0)
                                                            {
                                                                TreeNode tempNode2 = new TreeNode();
                                                                tempNode2.NodeID = "";
                                                                tempNode2.Text = "";
                                                                newNode5.Nodes.Add(tempNode2);
                                                            }
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    if (division1s.Count > 0)
                                                    {
                                                        TreeNode tempNode2 = new TreeNode();
                                                        tempNode2.NodeID = "";
                                                        tempNode2.Text = "";
                                                        newNode4.Nodes.Add(tempNode2);
                                                    }
                                                }
                                            }
                                        }
                                        else
                                        {
                                            TreeNode tempNode2 = new TreeNode();
                                            tempNode2.NodeID = "";
                                            tempNode2.Text = "";
                                            newCNProfessionalNode.Nodes.Add(tempNode2);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                TreeNode tempNode2 = new TreeNode();
                                tempNode2.NodeID = "";
                                tempNode2.Text = "";
                                trWBS.Nodes[i].Nodes[j].Nodes.Add(tempNode2);
                            }
                        }
                    }
                }
                else
                {
                    TreeNode tempNode1 = new TreeNode();
                    tempNode1.NodeID = "";
                    tempNode1.Text = "";
                    trWBS.Nodes[i].Nodes.Add(tempNode1);
                }
            }
            this.trWBS.SelectedNodeID = this.hdSelectId.Text;
            BindGrid();
        }

        private string GetCNProfessionalId(Model.WBS_DivisionProject divisionProject)
        {
            string id = string.Empty;
            if (divisionProject != null && !string.IsNullOrEmpty(divisionProject.CNProfessionalId))
            {
                id = divisionProject.CNProfessionalId;
            }
            else
            {
                Model.WBS_DivisionProject superDivisionProject = BLL.DivisionProjectService.GetDivisionProjectById(divisionProject.SuperDivisionId);
                id = GetCNProfessionalId(superDivisionProject);
            }
            return id;
        }

        private List<string> GetDivisionProjectIds(Model.WBS_DivisionProject divisionProject)
        {
            List<string> list = new List<string>();
            if (divisionProject != null && !string.IsNullOrEmpty(divisionProject.SuperDivisionId))
            {
                list.Add(divisionProject.SuperDivisionId);
                Model.WBS_DivisionProject superDivisionProject = BLL.DivisionProjectService.GetDivisionProjectById(divisionProject.SuperDivisionId);
                list.AddRange(GetDivisionProjectIds(superDivisionProject));
            }
            return list;
        }
        #endregion

        #region  行点击事件
        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            string id = e.RowID;
            if (e.CommandName.Equals("download"))
            {
                string menuId = Const.ProjectControlPointMenuId;
                PageContext.RegisterStartupScript(Windowtt.GetShowReference(
                 String.Format("../../AttachFile/webuploader.aspx?type=-1&source=1&toKeyId={0}&path=FileUpload/BreakdownProject&menuId={1}", id, menuId)));
            }
        }
        #endregion

        #region  保存事件
        protected void btnSave_Click(object sender, EventArgs e)
        {
			var gridIds = this.Grid1.Rows.Select(x => x.RowID).ToList<string>();
			SelectedList.RemoveAll(x=>gridIds.Contains(x));

			for (int i = 0; i < this.Grid1.Rows.Count; i++)
			{
				if (this.Grid1.SelectedRowIDArray.Contains(this.Grid1.Rows[i].RowID))
				{
					if (!SelectedList.Contains(this.Grid1.Rows[i].RowID))
					{
						SelectedList.Add(this.Grid1.Rows[i].RowID);
					}
				}
				else
				{
					if (!NoSelectedList.Contains(this.Grid1.Rows[i].RowID))
					{
						NoSelectedList.Add(this.Grid1.Rows[i].RowID);
					}
				}
			} 

            NoSelectedList = NoSelectedList.Distinct().ToList();
            var q = NoSelectedList.Distinct().ToList();
            foreach (var item in q)
            {
                foreach (var i in SelectedList.Distinct())
                {
                    if (item == i)
                    {
                        NoSelectedList.Remove(item);
                    }
                }
            }
            foreach (var item in NoSelectedList)
            {
                Model.WBS_BreakdownProject breakdownProject = BLL.BreakdownProjectService.GetBreakdownProjectById(item);
                breakdownProject.IsSelected = null;
                BLL.BreakdownProjectService.UpdateBreakdownProject(breakdownProject);
				Model.WBS_DivisionProject divisionProject = BLL.DivisionProjectService.GetDivisionProjectById(breakdownProject.DivisionProjectId);
				divisionProject.IsSelected = false; 
				checknode(this.trWBS.Nodes.ToArray(), breakdownProject.DivisionProjectId, false);
				BLL.DivisionProjectService.UpdateDivisionProject(divisionProject);
				this.UpdateParentDivision(divisionProject.SuperDivisionId, false);
			}


			foreach (var item in SelectedList.Distinct())
			{
				Model.WBS_BreakdownProject breakdownProject = BLL.BreakdownProjectService.GetBreakdownProjectById(item);
				breakdownProject.IsSelected = true;
				BLL.BreakdownProjectService.UpdateBreakdownProject(breakdownProject);
				Model.WBS_DivisionProject divisionProject = BLL.DivisionProjectService.GetDivisionProjectById(breakdownProject.DivisionProjectId);
				checknode(this.trWBS.Nodes.ToArray(), breakdownProject.DivisionProjectId, true);
				divisionProject.IsSelected = true;
				BLL.DivisionProjectService.UpdateDivisionProject(divisionProject);
				this.UpdateParentDivision(divisionProject.SuperDivisionId, true);
			}


			Alert.ShowInTop("保存成功!", MessageBoxIcon.Success);
        }
		#endregion
		private void checknode(TreeNode[] nodes, string nodeId, bool check)
		{
			foreach (var node in nodes)
			{
				if (node.NodeID == nodeId)
				{
					node.Checked = check;
				}
				if (node.Nodes.Count > 0)
				{
					checknode(node.Nodes.ToArray(), nodeId, check);
				}
			}
		}
        #region 获取按钮权限
        /// <summary>
        /// 获取按钮权限
        /// </summary>
        /// <param name="button"></param>
        /// <returns></returns>
        private void GetButtonPower()
        {
            if (Request.Params["value"] == "0")
            {
                return;
            }
            var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.ProjectControlPointMenuId);
            if (buttonList.Count() > 0)
            {
                if (buttonList.Contains(BLL.Const.BtnAdd))
                {
                    this.btnNew.Hidden = false;
                    this.btnMenuAdd.Hidden = false;
                }
                if (buttonList.Contains(BLL.Const.BtnModify))
                {
                    this.btnMenuEdit.Hidden = false;
                    this.btnMenuModify.Hidden = false;
                }
                if (buttonList.Contains(BLL.Const.BtnSave))
                {
                    this.btnSave.Hidden = false;
                }
                if (buttonList.Contains(BLL.Const.BtnDelete))
                {
                    this.btnMenuDelete.Hidden = false;
                    this.btnMenuDel.Hidden = false;
                }
            }
        }
        #endregion
    }
}