123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462 |
- using DevExpress.XtraTreeList.Columns;
- using DevExpress.XtraTreeList.Data;
- using DevExpress.XtraTreeList.Nodes;
- using ProjectBase.Data.BaseDAL;
- using ProjectBase.Data.Logs;
- using SIMDP.BLL;
- using SIMDP.Model;
- using ProjectBase.Util;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Data.Common;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace SIMDP.View
- {
- public partial class FormAuthoryRole : DevExpress.XtraEditors.XtraForm
- {
- //private BlAuthoryGroup blGroup = new BlAuthoryGroup();
- private string selectTreeNode = ""; // 选中节点
- private int treeLevel = 0; // 树形结构的层级
- private int nodeType = 0; // 选中节点类型,0,代表组织,1,代表角色
- public FormAuthoryRole()
- {
- InitializeComponent();
- }
- private void FormAuthoryRole_Load(object sender, EventArgs e)
- {
- InitTree();
- InitGrid();
- }
- /// <summary>
- /// 初始化树
- /// </summary>
- private void InitTree()
- {
- this.treeViewRole.BeginUpdate();
- this.treeViewRole.Nodes.Clear();
- this.treeViewRole.ImageList = this.imageList1;
- string condition = string.Format(" parent_group_id =''");
- List<MoAuthoryGroup> list = BLLFactory<BlAuthoryGroup>.Instance.Find(condition);
- List<MoAuthoryGroup> rootList = new List<MoAuthoryGroup>();
- foreach (MoAuthoryGroup item in list)
- {
- rootList.Add(item);
- treeLevel = 1;
- }
- foreach (MoAuthoryGroup root in rootList)
- {
- if (root != null)
- {
- TreeNode rootNode = new TreeNode(); //根节点
- rootNode.Text = root.GroupName;
- rootNode.Name = root.GroupId;
- rootNode.ImageIndex = 0;
- rootNode.SelectedImageIndex = 0;
- AddChildRole(rootNode);
- if (root.GroupId.Substring(0, 1).Equals("1"))
- {
- List<MoAuthoryGroup> subList = BLLFactory<BlAuthoryGroup>.Instance.Find(string.Format(" parent_group_id = '{0}'", root.GroupId));
- AddChildNode(subList, rootNode);
- treeLevel = 2;
- }
- this.treeViewRole.Nodes.Add(rootNode);
- }
- }
- this.treeViewRole.ExpandAll();
- this.treeViewRole.EndUpdate();
- }
- /// <summary>
- /// 添加公司级子节点
- /// </summary>
- /// <param name="list"></param>
- /// <param name="parentNode"></param>
- private void AddChildNode(List<MoAuthoryGroup> list, TreeNode parentNode)
- {
- if (list == null)
- {
- return;
- }
- foreach (MoAuthoryGroup item in list)
- {
- TreeNode node = new TreeNode(); //公司级子节点
- node.Text = item.GroupName;
- node.Name = item.GroupId;
- node.ImageIndex = 1;
- node.SelectedImageIndex = 1;
- parentNode.Nodes.Add(node);
- AddChildRole(node);
- }
- }
-
- /// <summary>
- /// 添加角色节点
- /// </summary>
- /// <param name="parentNode"></param>
- private void AddChildRole(TreeNode parentNode)
- {
- string sql =" role_id in (SELECT b.role_id from group_role b where b.group_id = '" + parentNode.Name + "')";
- //BlAuthoryRole blRole = new BlAuthoryRole();
- List<MoAuthoryRole> roleList = new List<MoAuthoryRole>();
- roleList = BLLFactory<BlAuthoryRole>.Instance.Find(sql);
- foreach (MoAuthoryRole item in roleList)
- {
- TreeNode node = new TreeNode();
- node.Text = item.RoleName;
- node.Name = item.RoleId;
- node.ImageIndex = 2;
- node.SelectedImageIndex = 2;
- parentNode.Nodes.Add(node);
- }
- }
- /// <summary>
- /// 初始化表格控件
- /// </summary>
- private void InitGrid()
- {
- //entity
- this.winGridViewPager1.OnPageChanged += new EventHandler(winGridViewPager1_OnPageChanged);
- this.winGridViewPager1.OnStartExport += new EventHandler(winGridViewPager1_OnStartExport);
- this.winGridViewPager1.OnEditSelected += new EventHandler(winGridViewPager1_OnEditSelected);
- this.winGridViewPager1.OnAddNew += new EventHandler(winGridViewPager1_OnAddNew);
- this.winGridViewPager1.OnDeleteSelected += new EventHandler(winGridViewPager1_OnDeleteSelected);
- this.winGridViewPager1.OnRefresh += new EventHandler(winGridViewPager1_OnRefresh);
- this.winGridViewPager1.gridView1.SelectionChanged += new DevExpress.Data.SelectionChangedEventHandler(winGridViewPager1_OnSelect);
- //this.winGridViewPager1.AppendedMenu = this.contextMenuStrip1;
- this.winGridViewPager1.ShowLineNumber = true;
- this.winGridViewPager1.BestFitColumnWith = true;
- this.winGridViewPager1.DisplayColumns = "RoleId,RoleName,RoleTime,RoleDescription";
- #region 添加别名解析
- this.winGridViewPager1.AddColumnAlias("RoleId", "角色编码");
- this.winGridViewPager1.AddColumnAlias("RoleName", "角色名称");
- this.winGridViewPager1.AddColumnAlias("RoleTime", "角色生成时间");
- this.winGridViewPager1.AddColumnAlias("RoleDescription", "角色描述");
- #endregion
- this.winGridViewPager1.PrintTitle = "角色信息报表";
- this.winGridViewPager1.gridView1.Appearance.HeaderPanel.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
- for (int i = 0; i < this.winGridViewPager1.gridView1.Columns.Count; i++) //设置每列内容居中显示
- {
- this.winGridViewPager1.gridView1.Columns[i].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
- }
- }
- /// <summary>
- /// 绑定数据
- /// </summary>
- /// <param name="condition">组编码</param>
- private void BindData(string condition, int nodeType)
- {
- try
- {
- //BlAuthoryRole blRole = new BlAuthoryRole();
- if (!string.IsNullOrEmpty(condition))
- {
- string sql = "";
- if (nodeType == 0)
- {
- sql = " role_id in (SELECT b.role_id from group_role b where b.group_id = '" + condition + "')";
- }
- else
- {
- sql = " role_id = '" + condition + "'";
- }
-
- this.winGridViewPager1.DataSource = BLLFactory<BlAuthoryRole>.Instance.FindWithPager(sql, this.winGridViewPager1.PagerInfo);
- }
-
- }
- catch (Exception ex)
- {
- DevExpress.XtraEditors.XtraMessageBox.Show("查询角色信息失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- LogHelper.log.Error(string.Format("查询角色信息失败,失败原因为:{0}", ex));
- }
- }
- /// <summary>
- /// 选中事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void winGridViewPager1_OnSelect(object sender, EventArgs e)
- {
- MoAuthoryRole selectRow = this.winGridViewPager1.gridView1.GetFocusedRow() as MoAuthoryRole;
- InitTreeList();
- //BlRoleRight blRole = new BlRoleRight();
- List<MoRoleRight> roleRightList = BLLFactory<BlRoleRight>.Instance.FindByIDs(selectRow.RoleId);
- foreach (TreeListNode node in this.treeListRight.Nodes)
- {
- SetTreeListChecked(node, roleRightList);
- }
- }
- private void SetTreeListChecked(TreeListNode parentNode, List<MoRoleRight> roleRightList)
- {
- MoAuthoryRight data = this.treeListRight.GetDataRecordByNode(parentNode) as MoAuthoryRight;
- for (int i = 0; i < roleRightList.Count; i++)
- {
- if (roleRightList[i].RightId == data.RightId)
- parentNode.Checked = true;
- }
- if (parentNode.Nodes.Count == 0)
- return;
- foreach (TreeListNode node in parentNode.Nodes)
- {
- SetTreeListChecked(node, roleRightList);
- }
- }
- private void InitTreeList()
- {
- #region 添加别名解析
- Dictionary<string, string> columnNameAlias = new Dictionary<string, string>();//Treelist中字段别名字典集合
- this.treeListRight.Columns.Clear();
- columnNameAlias.Clear();
- //columnNameAlias.Add("Check", "选中状态");
- columnNameAlias.Add("RightName", "权限名称");
- foreach (var item in columnNameAlias)
- {
- TreeListColumn col = new TreeListColumn();
- col.Caption = item.Value;
- col.FieldName = item.Key;
- col.Visible = true;
- col.Name = item.Key;
- this.treeListRight.Columns.Add(col);
- }
- this.treeListRight.KeyFieldName = "RightId";
- this.treeListRight.ParentFieldName = "ParentRightId";
- this.treeListRight.LookAndFeel.UseDefaultLookAndFeel = false; // 设置“+” ,“-”
- this.treeListRight.LookAndFeel.UseWindowsXPTheme = true;
- #endregion
- List<MoAuthoryRight> rightList = new List<MoAuthoryRight>();
- //BlAuthoryRight bl = new BlAuthoryRight();
- rightList = BLLFactory<BlAuthoryRight>.Instance.GetAll();
- this.treeListRight.DataSource = rightList;
- this.treeListRight.RefreshDataSource();
- }
- /// <summary>
- /// 添加列序号
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void treeListRight_CustomDrawNodeIndicator(object sender, DevExpress.XtraTreeList.CustomDrawNodeIndicatorEventArgs e)
- {
- DevExpress.XtraTreeList.TreeList tmpTree = sender as DevExpress.XtraTreeList.TreeList;
- DevExpress.Utils.Drawing.IndicatorObjectInfoArgs args = e.ObjectArgs as DevExpress.Utils.Drawing.IndicatorObjectInfoArgs;
- if (args != null)
- {
- int rowNum = tmpTree.GetVisibleIndexByNode(e.Node) + 1;
- this.treeListRight.IndicatorWidth = 30;
- args.DisplayText = rowNum.ToString();
- }
- }
- /// <summary>
- /// 分页控件翻页的操作
- /// </summary>
- private void winGridViewPager1_OnPageChanged(object sender, EventArgs e)
- {
- BindData(selectTreeNode, nodeType);
- }
- /// <summary>
- /// 分页控件全部导出操作前的操作
- /// </summary>
- private void winGridViewPager1_OnStartExport(object sender, EventArgs e)
- {
- //this.winGridViewPager1.AllToExport = blMenu.Find(condition);
- }
- /// <summary>
- /// 分页控件编辑项操作
- /// </summary>
- private void winGridViewPager1_OnEditSelected(object sender, EventArgs e)
- {
- MoAuthoryRole selectRow = this.winGridViewPager1.gridView1.GetFocusedRow() as MoAuthoryRole;
- if (selectRow == null)
- {
- DevExpress.XtraEditors.XtraMessageBox.Show("请选择一行数据!");
- return;
- }
- FormEditAuthoryRole editAuthoryRole = new FormEditAuthoryRole(selectRow);
- editAuthoryRole.StartPosition = FormStartPosition.CenterScreen;
- editAuthoryRole.ShowDialog();
- BindData(selectTreeNode, nodeType);
- InitTree();
- //BlRoleRight blRole = new BlRoleRight();
- List<MoRoleRight> roleRightList = BLLFactory<BlRoleRight>.Instance.FindByIDs(selectRow.RoleId);
- foreach (TreeListNode node in this.treeListRight.Nodes)
- {
- SetTreeListChecked(node, roleRightList);
- }
- }
- ///// <summary>
- ///// 分页控件新增操作
- ///// </summary>
- private void winGridViewPager1_OnAddNew(object sender, EventArgs e)
- {
- FormEditAuthoryRole editAuthoryRole = new FormEditAuthoryRole(null);
- editAuthoryRole.StartPosition = FormStartPosition.CenterScreen;
- editAuthoryRole.ShowDialog();
- InitTree();
- InitTreeList();
- BindData(selectTreeNode, nodeType);
- }
- /// <summary>
- /// 分页控件删除操作
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void winGridViewPager1_OnDeleteSelected(object sender, EventArgs e)
- {
- bool bOK = false;
- DbTransaction trans = null;
- try
- {
- if (DevExpress.XtraEditors.XtraMessageBox.Show("您确定删除选定的记录么?", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No)
- {
- return;
- }
- MoAuthoryRole selectRow = this.winGridViewPager1.gridView1.GetFocusedRow() as MoAuthoryRole;
- trans = BLLFactory<BlAuthoryGroup>.Instance.CreateTransaction();
- //BlAuthoryRole blRole = new BlAuthoryRole();
- bOK = BLLFactory<BlAuthoryRole>.Instance.Delete(selectRow.RoleId, SysEnvironment.CurrentLoginID, trans);
- //BlRoleRight blRight = new BlRoleRight();
- if (bOK)
- {
- bOK = BLLFactory<BlRoleRight>.Instance.Delete(selectRow.RoleId, SysEnvironment.CurrentLoginID, trans);
- }
- //BlGroupRole blGroupRole = new BlGroupRole();
- if (bOK)
- {
- string condition = string.Format(" role_id = '{0}'", selectRow.RoleId);
- bOK = BLLFactory<BlGroupRole>.Instance.DeleteByCondition(condition, trans);
- }
- if (!bOK)
- {
- trans.Rollback();
- DevExpress.XtraEditors.XtraMessageBox.Show("删除角色信息失败!");
- }
- else
- {
- trans.Commit();
- DevExpress.XtraEditors.XtraMessageBox.Show("删除角色信息成功!");
- InitTree();
- InitTreeList();
- BindData(selectTreeNode, nodeType);
- }
- }
- catch (Exception ex)
- {
- trans.Rollback();
- DevExpress.XtraEditors.XtraMessageBox.Show("删除失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- LogHelper.log.Error(string.Format("删除数据库角色关联数据出现错误:{0}", ex));
- }
- finally
- {
- trans.Dispose();
- BindData(selectTreeNode, nodeType);
- }
- }
- /// <summary>
- /// 分页控件刷新操作
- /// </summary>
- private void winGridViewPager1_OnRefresh(object sender, EventArgs e)
- {
- BindData(selectTreeNode, nodeType);
- }
-
-
- /// <summary>
- /// 选中节点响应事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void TreeNode_AfterSelect(object sender, TreeViewEventArgs e)
- {
- if (e.Node.Level == 0)
- {
- if (treeLevel == 2)
- return;
- else
- {
- selectTreeNode = e.Node.Name;
- nodeType = 0;
- BindData(selectTreeNode, nodeType);
- }
- }
- else if (e.Node.Level == 1)
- {
- if (treeLevel == 2)
- {
- selectTreeNode = e.Node.Name;
- nodeType = 0;
- BindData(selectTreeNode, nodeType);
- }
- else
- {
- selectTreeNode = e.Node.Name;
- nodeType = 1;
- BindData(selectTreeNode, nodeType);
- }
- }
- else
- {
- selectTreeNode = e.Node.Name;
- nodeType = 1;
- BindData(selectTreeNode, nodeType);
- }
- }
- private void menu_Add_Click(object sender, EventArgs e)
- {
- winGridViewPager1_OnAddNew(sender, e);
- }
- private void menu_Expand_Click(object sender, EventArgs e)
- {
- this.treeViewRole.ExpandAll();
- }
- private void menu_Collapse_Click(object sender, EventArgs e)
- {
- this.treeViewRole.CollapseAll();
- }
- private void menu_Refresh_Click(object sender, EventArgs e)
- {
- InitTree();
- }
- }
- }
|