/// <summary>
        /// 添加一项菜单
        /// </summary>
        /// <param name="menu">菜单实体</param>
        /// <returns>1:添加成功;0:添加失败</returns>
        public int Add(SYS_MenuEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Sys_Menu(");
            strSql.Append("MenuID,MenuName,MenuUrl,FunctionID,IsLeaf,MenuLevel,RootID,ParentsID,DisplayOrder,IcValue,IsPop,MenuSeq)");
            strSql.Append(" values (");
            strSql.Append("@MenuID,@MenuName,@MenuUrl,@FunctionID,@IsLeaf,@MenuLevel,@RootID,@ParentsID,@DisplayOrder,@IcValue,@IsPop,@MenuSeq)");
            SqlParameter[] parameters = {
                    new SqlParameter("@MenuID", SqlDbType.NVarChar,20),
                    new SqlParameter("@MenuName", SqlDbType.NVarChar,50),
                    new SqlParameter("@MenuUrl", SqlDbType.VarChar,300),
                    new SqlParameter("@FunctionID", SqlDbType.NVarChar,100),
                    new SqlParameter("@IsLeaf", SqlDbType.VarChar,1),
                    new SqlParameter("@MenuLevel", SqlDbType.VarChar,1),
                    new SqlParameter("@RootID", SqlDbType.VarChar,32),
                    new SqlParameter("@ParentsID", SqlDbType.VarChar,32),
                    new SqlParameter("@DisplayOrder", SqlDbType.Decimal,5),
                    new SqlParameter("@IcValue", SqlDbType.VarChar,200),
                    new SqlParameter("@IsPop", SqlDbType.VarChar,1),
                    new SqlParameter("@MenuSeq", SqlDbType.VarChar,300)};
            parameters[0].Value = model.MenuID;
            parameters[1].Value = model.MenuName;
            parameters[2].Value = model.MenuUrl;
            parameters[3].Value = model.FunctionID;
            parameters[4].Value = model.IsLeaf;
            parameters[5].Value = model.MenuLevel;
            parameters[6].Value = model.RootID;
            parameters[7].Value = model.ParentsID;
            parameters[8].Value = model.DisplayOrder;
            parameters[9].Value = model.IcValue;
            parameters[10].Value = model.IsPop;
            parameters[11].Value = model.MenuSeq;

            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
    protected void SetModelToControl(string menUserCode)
    {
        //菜单项实体
        IndustryPlatform.Model.SYS_MenuEntity menu = menuBll.GetModel(menUserCode);

        this.txtDisplayOrder.Text = menu.DisplayOrder.ToString();         //排序编号
        this.txtFunctionID.Text   = menu.FunctionID.ToString();           //功能编号
        this.txtICValue.Text      = menu.IcValue.ToString();              //图片地址
        this.txtMenuAction.Text   = menu.MenuUrl.ToString();              //菜单项活动
        //this.txtMenuLabel.Text = menu.MenuLabel.ToString();                  //菜单项标签

        this.txtMenuName.Text  = menu.MenuName.ToString();                   //菜单名称
        this.chkIsLeaf.Checked = menu.IsLeaf.Trim() == "0" ? false : true;   //是否为叶子节点
        this.chkIsPop.Checked  = menu.IsPop.Trim() == "0" ? false : true;    //是否填出

        this.ddlParents.SelectedValue = menu.ParentsID;                      //父菜单项编号
    }
        /// <summary>
        /// 由菜单项编号获取该菜单项实体
        /// </summary>
        /// <param name="menuID">菜单项编号</param>
        /// <returns>菜单项实体;如果不存在该菜单项返回NULL</returns>
        public SYS_MenuEntity GetModel(string MenuID)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select  top 1* from Sys_Menu ");
            strSql.Append(" where MenuID=@MenuID ");
            SqlParameter[] parameters =
            {
                new SqlParameter("@MenuID", SqlDbType.NVarChar, 50)
            };
            parameters[0].Value = MenuID;

            IndustryPlatform.Model.SYS_MenuEntity model = new IndustryPlatform.Model.SYS_MenuEntity();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                model.MenuID     = ds.Tables[0].Rows[0]["MenuID"].ToString();
                model.MenuName   = ds.Tables[0].Rows[0]["MenuName"].ToString();
                model.MenuUrl    = ds.Tables[0].Rows[0]["MenuUrl"].ToString();
                model.FunctionID = ds.Tables[0].Rows[0]["FunctionID"].ToString();
                model.IsLeaf     = ds.Tables[0].Rows[0]["IsLeaf"].ToString();
                model.MenuLevel  = ds.Tables[0].Rows[0]["MenuLevel"].ToString();
                model.RootID     = ds.Tables[0].Rows[0]["RootID"].ToString();
                model.ParentsID  = ds.Tables[0].Rows[0]["ParentsID"].ToString();
                if (ds.Tables[0].Rows[0]["DisplayOrder"].ToString() != "")
                {
                    model.DisplayOrder = decimal.Parse(ds.Tables[0].Rows[0]["DisplayOrder"].ToString());
                }
                model.IcValue = ds.Tables[0].Rows[0]["IcValue"].ToString();
                model.IsPop   = ds.Tables[0].Rows[0]["IsPop"].ToString();
                model.MenuSeq = ds.Tables[0].Rows[0]["MenuSeq"].ToString();
                return(model);
            }
            else
            {
                return(null);
            }
        }
    protected void btnSave_Click(object sender, ImageClickEventArgs e)
    {
        if (this.ddlParents.SelectedIndex == 0)
        {
            MessageBox.Show(this, "请选择父节点!");
            return;
        }
        if (Request.QueryString["menuID"] != null)
        {
            if (this.chkIsLeaf.Checked)
            {
                if (DbHelperSQL.Exists("Select Count(0) from Sys_Menu where ParentsID='" + Request.QueryString["menuID"].ToString() + "'"))
                {
                    MessageBox.Show(this, "您修改的节点下面还有子节点,不能修改成叶子节点!");
                    return;
                }
            }
        }
        BuildModel();

        if (string.IsNullOrEmpty(Request.QueryString["menuID"]))
        {
            string strparentsID = "0";
            if (this.ddlParents.SelectedIndex != 0)
            {
                strparentsID = this.ddlParents.SelectedValue;
            }
            DataTable dtExist = menuBll.GetList(" menuName='" + this.txtMenuName.Text.Trim().Replace("'", "''") + "' and parentsID='" + strparentsID + "'").Tables[0];
            if (dtExist.Rows.Count > 0)
            {
                MessageBox.Show(this, "同父级别下的菜单名称不能重复,请重新输入!");
                return;
            }
            if (menuBll.Add(menuModel) == 1)
            {
                #region 数据同步
                if (ConfigurationManager.AppSettings["IsSync"] == "1")
                {
                    try
                    {
                        //添加成功,数据同步到各个磅房
                        IndustryPlatform.DBUtility.MsmqManage msm = MsmqManage.GetMsmq();
                        string strSQL = "INSERT INTO Sys_Menu ( " +
                                        "[MenuID] ," +
                                        "[MenuName] ," +
                                        "[MenuUrl] ," +
                                        "[FunctionID] ," +
                                        "[IsLeaf] ," +
                                        "[MenuLevel] ," +
                                        "[RootID] ," +
                                        "[ParentsID] ," +
                                        "[DisplayOrder] ," +
                                        "[IcValue] ," +
                                        "[IsPop] ," +
                                        "[MenuSeq] ) VALUES ('" + menuModel.MenuID + "','" + CommonMethod.RepChar(menuModel.MenuName) +
                                        "','" + CommonMethod.RepChar(menuModel.MenuUrl) + "','" + menuModel.FunctionID +
                                        "','" + menuModel.IsLeaf + "','" + menuModel.MenuLevel +
                                        "','" + menuModel.RootID + "','" + menuModel.ParentsID +
                                        "'," + menuModel.DisplayOrder + ",'" + menuModel.IcValue +
                                        "','" + menuModel.IsPop + "','" + menuModel.MenuSeq + "')";
                        strSQL = msm.AllStation + msm.Prefix + "Sys_Menu" + msm.Prefix + msm.AddFlg + msm.Prefix + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + msm.Prefix + strSQL;
                        msm.AddMsmq(strSQL);
                        //List<string> iplist = ControlBindHelper.GetAllRoomIP();
                        //for (int i = 0; i < iplist.Count; i++)
                        //{
                        //    if (iplist[i] != "")
                        //    {
                        //        EndpointAddress ep = new EndpointAddress("net.msmq://" + iplist[i] + "/private/STOCMessagingQueue");
                        //        NetMsmqBinding et = new NetMsmqBinding();
                        //        et.ExactlyOnce = false;
                        //        et.Security.Mode = System.ServiceModel.NetMsmqSecurityMode.None;
                        //        IDataPublish proxy = ChannelFactory<IDataPublish>.CreateChannel(et, ep);
                        //        proxy.IndustryPlatform_Menu_Add(menuModel);
                        //    }
                        //}
                    }
                    catch
                    { }
                }
                #endregion

                MessageBox.Show(this, "添加成功!");
                SetText();
                menuBll.BindDdl(ddlParents, PositionCodes);
            }
            else
            {
                MessageBox.Show(this, "添加失败!");
            }
        }
        else//更新操作
        {
            menuModel.MenuID = Request.QueryString["MenuID"];
            string strparentsID = "0";
            if (this.ddlParents.SelectedIndex != 0)
            {
                strparentsID = this.ddlParents.SelectedValue;
            }
            DataTable dtExist = menuBll.GetList(" menuName='" + this.txtMenuName.Text.Trim().Replace("'", "''") + "' and MenuID<>'" + menuModel.MenuID + "' and ParentsID='" + strparentsID + "'").Tables[0];
            if (dtExist.Rows.Count > 0)
            {
                MessageBox.Show(this, "同父级别下的菜单名称不能重复,请重新输入!");
                return;
            }

            if (menuBll.IsChildren(menuModel.MenuID, ddlParents.SelectedValue))
            {
                MessageBox.Show(this, "不要选择该菜单项下的子菜单项作为父菜单!");
                //菜单项实体
                IndustryPlatform.Model.SYS_MenuEntity menu = menuBll.GetModel(menuModel.MenuID);

                ddlParents.SelectedValue = menu.ParentsID;
            }
            else
            {
                if (menuBll.Update(menuModel) == 1)
                {
                    #region 数据同步
                    if (ConfigurationManager.AppSettings["IsSync"] == "1")
                    {
                        try
                        {
                            //添加成功,数据同步到各个磅房
                            if (menuModel.DisplayOrder == null)
                            {
                                menuModel.DisplayOrder = 0;
                            }
                            //添加成功,数据同步到各个磅房
                            IndustryPlatform.DBUtility.MsmqManage msm = MsmqManage.GetMsmq();
                            string strSQL = "Delete From Sys_Menu Where MenuID='" + menuModel.MenuID + "'; ";

                            strSQL += "INSERT INTO Sys_Menu ( " +
                                      "[MenuID] ," +
                                      "[MenuName] ," +
                                      "[MenuUrl] ," +
                                      "[FunctionID] ," +
                                      "[IsLeaf] ," +
                                      "[MenuLevel] ," +
                                      "[RootID] ," +
                                      "[ParentsID] ," +
                                      "[DisplayOrder] ," +
                                      "[IcValue] ," +
                                      "[IsPop] ," +
                                      "[MenuSeq] ) VALUES ('" + menuModel.MenuID + "','" + CommonMethod.RepChar(menuModel.MenuName) +
                                      "','" + CommonMethod.RepChar(menuModel.MenuUrl) + "','" + menuModel.FunctionID +
                                      "','" + menuModel.IsLeaf + "','" + menuModel.MenuLevel +
                                      "','" + menuModel.RootID + "','" + menuModel.ParentsID +
                                      "'," + menuModel.DisplayOrder + ",'" + menuModel.IcValue +
                                      "','" + menuModel.IsPop + "','" + menuModel.MenuSeq + "')";
                            strSQL = msm.AllStation + msm.Prefix + "Sys_Menu" + msm.Prefix + msm.AddFlg + msm.Prefix + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + msm.Prefix + strSQL;
                            msm.AddMsmq(strSQL);
                        }
                        catch
                        { }
                    }
                    #endregion

                    ClientScript.RegisterStartupScript(Page.GetType(), "", "this.top.currForm.close();", true);
                }
                else
                {
                    MessageBox.Show(this, "修改失败!");
                }
            }
        }
    }
 /// <summary>
 /// 添加一项菜单
 /// </summary>
 /// <param name="menu">菜单实体</param>
 /// <returns>1:添加成功;0:添加失败</returns>
 public int Add(SYS_MenuEntity menu)
 {
     return dal.Add(menu);
 }
 /// <summary>
 /// 更新一项菜单
 /// </summary>
 /// <param name="menu">菜单实体</param>
 /// <returns>1:添加成功;0:添加失败</returns>
 public int Update(SYS_MenuEntity menu)
 {
     return dal.Update(menu);
 }
        /// <summary>
        /// 更新一项菜单
        /// </summary>
        /// <param name="menu">菜单实体</param>
        /// <returns>1:添加成功;0:添加失败</returns>
        public int Update(SYS_MenuEntity menu)
        {
            try
            {
                string originalMenuSEQ = GetParentSEQ(menu.MenuID).TrimEnd(menu.MenuID.ToCharArray());

                #region Update the menuSEQ
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("update SYS_MENU set menuSEQ='{0}'", menu.MenuID.TrimEnd(menu.MenuID.ToCharArray()));
                sb.AppendFormat("+replace(menuSEQ,'{0}','')", originalMenuSEQ);
                sb.AppendFormat(" where menuSEQ like '{0}%'", originalMenuSEQ + menu.MenuID);

                DbHelperSQL.ExecuteSql(sb.ToString());
                #endregion Update the menuSEQ

                StringBuilder sbLevel = new StringBuilder();
                sbLevel.Append("update SYS_MENU set menuLevel = len(menuSEQ)-len(replace(menuSEQ,'.' , ''))");
                sbLevel.AppendFormat(" where menuSEQ like '{0}%'", originalMenuSEQ + menu.MenuID);

                DbHelperSQL.ExecuteSql(sbLevel.ToString());

                StringBuilder strSql = new StringBuilder();
                strSql.Append("update Sys_Menu set ");
                strSql.Append("MenuName=@MenuName,");
                strSql.Append("MenuUrl=@MenuUrl,");
                strSql.Append("FunctionID=@FunctionID,");
                strSql.Append("IsLeaf=@IsLeaf,");
                strSql.Append("MenuLevel=@MenuLevel,");
                strSql.Append("RootID=@RootID,");
                strSql.Append("ParentsID=@ParentsID,");
                strSql.Append("DisplayOrder=@DisplayOrder,");
                strSql.Append("IcValue=@IcValue,");
                strSql.Append("IsPop=@IsPop,");
                strSql.Append("MenuSeq=@MenuSeq");
                strSql.Append(" where MenuID=@MenuID ");
                SqlParameter[] parameters = {
                    new SqlParameter("@MenuID", SqlDbType.NVarChar,20),
                    new SqlParameter("@MenuName", SqlDbType.NVarChar,50),
                    new SqlParameter("@MenuUrl", SqlDbType.VarChar,300),
                    new SqlParameter("@FunctionID", SqlDbType.NVarChar,100),
                    new SqlParameter("@IsLeaf", SqlDbType.VarChar,1),
                    new SqlParameter("@MenuLevel", SqlDbType.VarChar,1),
                    new SqlParameter("@RootID", SqlDbType.VarChar,32),
                    new SqlParameter("@ParentsID", SqlDbType.VarChar,32),
                    new SqlParameter("@DisplayOrder", SqlDbType.Decimal,5),
                    new SqlParameter("@IcValue", SqlDbType.VarChar,200),
                    new SqlParameter("@IsPop", SqlDbType.VarChar,1),
                    new SqlParameter("@MenuSeq", SqlDbType.VarChar,300)};
                parameters[0].Value = menu.MenuID;
                parameters[1].Value = menu.MenuName;
                parameters[2].Value = menu.MenuUrl;
                parameters[3].Value = menu.FunctionID;
                parameters[4].Value = menu.IsLeaf;
                parameters[5].Value = menu.MenuLevel;
                parameters[6].Value = menu.RootID;
                parameters[7].Value = menu.ParentsID;
                parameters[8].Value = menu.DisplayOrder;
                parameters[9].Value = menu.IcValue;
                parameters[10].Value = menu.IsPop;
                parameters[11].Value = menu.MenuSeq;

                DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
                return 1;
            }
            catch
            {
                return 0;
            }
        }
        /// <summary>
        /// 由菜单项编号获取该菜单项实体
        /// </summary>
        /// <param name="menuID">菜单项编号</param>
        /// <returns>菜单项实体;如果不存在该菜单项返回NULL</returns>
        public SYS_MenuEntity GetModel(string MenuID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1* from Sys_Menu ");
            strSql.Append(" where MenuID=@MenuID ");
            SqlParameter[] parameters = {
                    new SqlParameter("@MenuID", SqlDbType.NVarChar,50)};
            parameters[0].Value = MenuID;

            IndustryPlatform.Model.SYS_MenuEntity model = new IndustryPlatform.Model.SYS_MenuEntity();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.MenuID = ds.Tables[0].Rows[0]["MenuID"].ToString();
                model.MenuName = ds.Tables[0].Rows[0]["MenuName"].ToString();
                model.MenuUrl = ds.Tables[0].Rows[0]["MenuUrl"].ToString();
                model.FunctionID = ds.Tables[0].Rows[0]["FunctionID"].ToString();
                model.IsLeaf = ds.Tables[0].Rows[0]["IsLeaf"].ToString();
                model.MenuLevel = ds.Tables[0].Rows[0]["MenuLevel"].ToString();
                model.RootID = ds.Tables[0].Rows[0]["RootID"].ToString();
                model.ParentsID = ds.Tables[0].Rows[0]["ParentsID"].ToString();
                if (ds.Tables[0].Rows[0]["DisplayOrder"].ToString() != "")
                {
                    model.DisplayOrder = decimal.Parse(ds.Tables[0].Rows[0]["DisplayOrder"].ToString());
                }
                model.IcValue = ds.Tables[0].Rows[0]["IcValue"].ToString();
                model.IsPop = ds.Tables[0].Rows[0]["IsPop"].ToString();
                model.MenuSeq = ds.Tables[0].Rows[0]["MenuSeq"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }