using DevExpress.XtraEditors; using DevExpress.XtraGrid.Columns; using Lskj.Business.Impl; using Lskj.Control; using Lskj.Control.Model; using Lskj.Core; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Lskj.PubUserTomodule { public partial class ModuleAssociationRole : UserControl { //子菜单下拉框 public ComboBoxEdit comboBox { get { return this.PrimaryDirectory; } }//下拉框 (从外面赋值) //选中的菜单id public string Menuid; /// /// 点击删除事件,刷新主页面的权限 /// public event EventHandler onClickDeleteButton; public ModuleAssociationRole() { InitializeComponent(); InitControls(); this.PrimaryDirectory.SelectedValueChanged += new EventHandler(PrimaryDirectoryValueChanged);//选中子目录,加载二级目录 this.gridLeft.GridView.Click += new EventHandler(OnGridLeftGridViewClick);//选中二级目录,加载全部模块 this.AllModules.GridView.RowClick += GridView_RowClick;//单击模块,加载有权限的角色和人员 } //生成页面 public void InitControls() { //this.AllModules.GridView.OptionsView.ColumnAutoWidth = true; GridColumn treeListColumnName = new GridColumn(); treeListColumnName.Caption = "系统名称"; treeListColumnName.FieldName = "MenuCaption"; treeListColumnName.MinWidth = 38; treeListColumnName.Visible = true; treeListColumnName.VisibleIndex = 0; treeListColumnName.Width = 300; GridColumn treeListColumnMenuId = new GridColumn(); treeListColumnMenuId.Caption = "菜单id"; treeListColumnMenuId.FieldName = "MenuId"; treeListColumnMenuId.Visible = true; treeListColumnMenuId.VisibleIndex = 1; this.AllModules.GridView.Columns.AddRange(new GridColumn[] { treeListColumnMenuId, treeListColumnName }); GridColumn MenuIdColumn = new GridColumn(); MenuIdColumn.Caption = "编号"; MenuIdColumn.FieldName = "MenuId"; MenuIdColumn.VisibleIndex = 1; GridColumn MenuStructColumn = new GridColumn(); MenuStructColumn.Caption = "菜单号"; MenuStructColumn.FieldName = "MenuStruct"; MenuStructColumn.VisibleIndex = 2; GridColumn MenuCaptionColumn = new GridColumn(); MenuCaptionColumn.Caption = "菜单名称"; MenuCaptionColumn.FieldName = "MenuCaption"; MenuCaptionColumn.VisibleIndex = 3; this.gridLeft.GridView.Columns.AddRange(new GridColumn[] { MenuIdColumn, MenuStructColumn, MenuCaptionColumn }); GridColumn RoleIdColumn = new GridColumn(); RoleIdColumn.Caption = "编号"; RoleIdColumn.FieldName = "id"; RoleIdColumn.VisibleIndex = 1; GridColumn RoleRolenameColumn = new GridColumn(); RoleRolenameColumn.Caption = "角色"; RoleRolenameColumn.FieldName = "rolename"; RoleRolenameColumn.VisibleIndex = 2; this.RoleControlEx.GridView.Columns.AddRange(new GridColumn[] { RoleIdColumn, RoleRolenameColumn, }); GridColumn personnelIdColumn = new GridColumn(); personnelIdColumn.Caption = "编号"; personnelIdColumn.FieldName = "id"; personnelIdColumn.VisibleIndex = 1; GridColumn personneloperatornameColumn = new GridColumn(); personneloperatornameColumn.Caption = "角色"; personneloperatornameColumn.FieldName = "operatorname"; personneloperatornameColumn.VisibleIndex = 2; this.PersonnelControlEx.GridView.Columns.AddRange(new GridColumn[] { personnelIdColumn, personneloperatornameColumn, }); } /// /// 单击模块,加载有权限的角色和人员 /// /// /// public void GridView_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e) { DataRow mSelectRow = this.AllModules.GridView.GetFocusedDataRow(); if (mSelectRow != null) { Menuid = mSelectRow["menuid"] + ""; // 角色表 string RoleSql = string.Format(@"select a.id,a.rolename from p_systemRoleSetTab a with(nolock) where charindex(','+cast({0} as varchar)+',',','+isnull(a.EditPurview,'')+',')>0", Menuid); DataTable RoleTable = SqlHelper.ExecuteDataTable(RoleSql); this.RoleControlEx.gridControl.DataSource = RoleTable; // 人员 string personnelSql = string.Format(@"select aa.id,aa.operatorname from p_systemRoleSetTab a with(nolock) join p_systemRoleOperSetTab aa with(nolock) on a.id=aa.roleid where charindex(','+cast({0} as varchar)+',',','+isnull(a.EditPurview,'')+',')>0", Menuid); DataTable personnelTable = SqlHelper.ExecuteDataTable(personnelSql); this.PersonnelControlEx.gridControl.DataSource = personnelTable; } else { Menuid = string.Empty; this.RoleControlEx.gridControl.DataSource = null; this.PersonnelControlEx.gridControl.DataSource = null; } } /// /// 改变一级目录,加载对应的二级目录 /// /// /// private void PrimaryDirectoryValueChanged(object sender, EventArgs e) { Menuid = string.Empty; string text = (this.PrimaryDirectory.EditValue + "").Split('.')[0]; DataTable dt = SqlHelper.ExecuteDataTable(string.Format("select * from P_FormMenuConfigTab where len(menustruct)=2 and useFlag=1 and subsysid='{0}' order by menustruct", text)); if (dt.Rows.Count > 0) { this.gridLeft.gridControl.DataSource = dt; this.gridLeft.GridView.SelectRowHandler(0); OnGridLeftGridViewClick(null, null); } else { this.gridLeft.gridControl.DataSource = null; this.AllModules.gridControl.DataSource = null; } } /// /// 点击二级目录,加载具体模块 /// /// /// private void OnGridLeftGridViewClick(object sender, EventArgs e) { Menuid = string.Empty; DataRow mSelectRow = this.gridLeft.GridView.GetFocusedDataRow(); if (mSelectRow != null) { string sql = string.Format("select MenuId,MenuCaption, '0' as Editflag ,'0' as Readflag from P_FormMenuConfigTab where subsysid='{0}' and left(menustruct,2)='{1}' and menustruct!='{1}' and useFlag='1' order by menustruct ", mSelectRow["subsysid"] + "", mSelectRow["menustruct"] + ""); DataTable Alldt = SqlHelper.ExecuteDataTable(sql); this.AllModules.gridControl.DataSource = Alldt; this.AllModules.GridView.SelectRowHandler(0); GridView_RowClick(null, null); } } /// /// 单击删除 /// /// /// private void SetRemovePermissions_Click(object sender, EventArgs e) { //判断当前选中的是角色还是人员表 if (xtc_container.SelectedTabPage == xtraTabPage1) { //角色 DataRow Role = this.RoleControlEx.GridView.GetFocusedDataRow(); if (Role != null) { string RoleId = Role["Id"] + ""; if (string.IsNullOrWhiteSpace(RoleId)) { MessageUtil.Show("角色id为空,删除失败"); return; } string sql = string.Format(@"update p_systemRoleSetTab set EditPurview=REPLACE(EditPurview,cast({0} as varchar)+',','') where id='{1}'", Menuid, RoleId); int i=SqlHelper.ExecuteNonQuery(sql); if (i > 0) { int rValue = saveRoledata(int.Parse(RoleId)); if (rValue == 0) { //在拥有权限的模块中生成权限分类 Editdate readdate MessageUtil.Show("同步人员权限成功!"); } else { MessageUtil.Show("同步人员权限失败!"); return; } } else { MessageUtil.Show("删除失败"); } //刷新当前模块 GridView_RowClick(null, null); //刷新主页面 if (onClickDeleteButton != null) onClickDeleteButton(null, null); } else { MessageUtil.Show("请选择需要删除的角色"); } } } /// /// 执行保存角色权限时候的存储过程 /// /// /// /// /// public int saveRoledata(int roleid) { SqlParameter pMsg = new SqlParameter("@msg", SqlDbType.VarChar, 2000); pMsg.Direction = ParameterDirection.Output; SqlParameter returnValue = new SqlParameter("@return", SqlDbType.Int, 4); returnValue.Direction = ParameterDirection.ReturnValue; SqlParameter[] param = { new SqlParameter("@roleid ",SqlDbType.VarChar,20), pMsg, returnValue }; param[0].Value = roleid; BaseImpl.ExecProcedure("p_SystemSetRoleUserPurview", param); return Convert.ToInt32(returnValue.Value.ToString()); } } }