123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- using DevExpress.XtraTreeList.Columns;
- using DevExpress.XtraTreeList.Nodes;
- using ProjectBase.Data.BaseDAL;
- using ProjectBase.Data.Logs;
- using SIMDP.BLL;
- using SIMDP.Model;
- 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
- {
- public partial class FormEditAuthoryRole : DevExpress.XtraEditors.XtraForm
- {
- private MoAuthoryRole m_Role = null;
- public List<MoAuthoryGroup> groupList = new List<MoAuthoryGroup>();
- public FormEditAuthoryRole(MoAuthoryRole role)
- {
- InitializeComponent();
- m_Role = role;
- }
- private void FormEditAuthoryRole_Load(object sender, EventArgs e)
- {
- //BlAuthoryGroup blGroup = new BlAuthoryGroup();
- string condition = string.Format(" group_id LIKE '[1,2]%'");
- groupList = BLLFactory<BlAuthoryGroup>.Instance.Find(condition);
- this.treeListLookUpEditGroup.Properties.DataSource = groupList;
- InitTreeList();
- if (m_Role == null)
- {
- return;
- }
- this.teRoleId.Enabled = false;
- this.treeListLookUpEdit1TreeList.Enabled = false;
- //BlRoleRight blRoleRight = new BlRoleRight();
- List<MoRoleRight> moRoleRightList = new List<MoRoleRight>();
- moRoleRightList = BLLFactory<BlRoleRight>.Instance.FindByIDs(m_Role.RoleId);
- foreach (TreeListNode node in this.treeList1.Nodes)
- {
- SetTreeListChecked(node, moRoleRightList);
- }
- //BlGroupRole blGroupRole = new BlGroupRole();
- this.teRoleId.Text = m_Role.RoleId;
- this.teRoleName.Text = m_Role.RoleName;
- this.tbDescription.Text = m_Role.RoleDescription;
- MoGroupRole mo = BLLFactory<BlGroupRole>.Instance.FindSingle(string.Format(" role_id = '{0}'", m_Role.RoleId));
- this.treeListLookUpEditGroup.EditValue = mo.GroupId;
- }
- private void SetTreeListChecked(TreeListNode parentNode, List<MoRoleRight> roleRightList)
- {
- MoAuthoryRight data = this.treeList1.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);
- }
- }
- /// <summary>
- /// 选中后状态
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void treeList1_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
- {
- SetCheckedChildNodes(e.Node, e.Node.CheckState);
- SetCheckedParentNodes(e.Node, e.Node.CheckState);
- }
- /// <summary>
- /// 选中前状态
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void treeList1_BeforeCheckNode(object sender, DevExpress.XtraTreeList.CheckNodeEventArgs e)
- {
- e.State = (e.PrevState == CheckState.Checked ? CheckState.Unchecked : CheckState.Checked);
- }
- /// <summary>
- /// 设置子节点的状态
- /// </summary>
- /// <param name="node"></param>
- /// <param name="check"></param>
- private void SetCheckedChildNodes(DevExpress.XtraTreeList.Nodes.TreeListNode node, CheckState check)
- {
- for (int i = 0; i < node.Nodes.Count; i++)
- {
- node.Nodes[i].CheckState = check;
- SetCheckedChildNodes(node.Nodes[i], check);
- }
- }
- /// <summary>
- /// 设置父节点的状态
- /// </summary>
- /// <param name="node"></param>
- /// <param name="check"></param>
- private void SetCheckedParentNodes(DevExpress.XtraTreeList.Nodes.TreeListNode node, CheckState check)
- {
- if (node.ParentNode != null)
- {
- bool b = false;
- CheckState state;
- for (int i = 0; i < node.ParentNode.Nodes.Count; i++)
- {
- state = (CheckState)node.ParentNode.Nodes[i].CheckState;
- if (!check.Equals(state))
- {
- b = !b;
- break;
- }
- }
- node.ParentNode.CheckState = b ? CheckState.Indeterminate : check;
- SetCheckedParentNodes(node.ParentNode, check);
- }
- }
- private void InitTreeList()
- {
- #region 添加别名解析
- Dictionary<string, string> columnNameAlias = new Dictionary<string, string>();//Treelist中字段别名字典集合
- this.treeList1.Columns.Clear();
- columnNameAlias.Clear();
- columnNameAlias.Add("RightName", "权限名称");
- foreach (var item in columnNameAlias)
- {
- TreeListColumn col = new TreeListColumn();
- col.Caption = item.Value;
- col.FieldName = item.Key;
- col.Visible = true;
- this.treeList1.Columns.Add(col);
- }
- this.treeList1.KeyFieldName = "RightId";
- this.treeList1.ParentFieldName = "ParentRightId";
- this.treeList1.LookAndFeel.UseDefaultLookAndFeel = false; // 设置“+” ,“-”
- this.treeList1.LookAndFeel.UseWindowsXPTheme = true;
- #endregion
- List<MoAuthoryRight> rightList = new List<MoAuthoryRight>();
- //BlAuthoryRight bl = new BlAuthoryRight();
- //BlRoleRight blRole = new BlRoleRight();
- rightList = BLLFactory<BlAuthoryRight>.Instance.GetAll();
- this.treeList1.DataSource = rightList;
-
- this.treeList1.RefreshDataSource();
- }
- private void sbtnExit_Click(object sender, EventArgs e)
- {
- this.Close();
- }
- private void sbtnSave_Click(object sender, EventArgs e)
- {
- DbTransaction trans = null;
- try
- {
- //BlAuthoryRole blRole = new BlAuthoryRole();
- //BlRoleRight blRoleRight = new BlRoleRight();
- MoAuthoryRole role = new MoAuthoryRole();
- role.RoleId = this.teRoleId.Text;
- role.RoleName = this.teRoleName.Text;
- role.RoleDescription = this.tbDescription.Text;
- trans = BLLFactory<BlAuthoryRole>.Instance.CreateTransaction();
- bool bOK = BLLFactory<BlAuthoryRole>.Instance.InsertUpdate(role,role.RoleId, trans);
- if (bOK)
- {
- string condition = string.Format(" role_id = '{0}'", role.RoleId);
- if (m_Role !=null && BLLFactory<BlRoleRight>.Instance.IsExistRecord(condition, trans))
- {
- bOK = BLLFactory<BlRoleRight>.Instance.DeleteByCondition(condition, trans);
- }
- }
- if (bOK)
- {
- foreach (TreeListNode node in this.treeList1.Nodes)
- {
- if (node.CheckState == CheckState.Checked || node.CheckState == CheckState.Indeterminate)
- {
- MoAuthoryRight data = this.treeList1.GetDataRecordByNode(node) as MoAuthoryRight;
- MoRoleRight roleRight = new MoRoleRight();
- roleRight.RoleId = this.teRoleId.Text;
- roleRight.RightId = data.RightId;
- bOK = BLLFactory<BlRoleRight>.Instance.Insert(roleRight, trans);
- }
- if (bOK)
- bOK = GetTreeListCheckedkey(node, trans);
- }
- //BlGroupRole blGroupRole = new BlGroupRole();
- MoGroupRole moGroupRole = new MoGroupRole();
- moGroupRole.GroupId = this.treeListLookUpEditGroup.EditValue.ToString();
- moGroupRole.RoleId = role.RoleId;
- string condition = string.Format(" role_id = '{0}'", role.RoleId);
- if (!BLLFactory<BlGroupRole>.Instance.IsExistRecord(condition, trans))
- bOK = BLLFactory<BlGroupRole>.Instance.Insert(moGroupRole, trans);
- }
- if (!bOK)
- {
- trans.Rollback();
- DevExpress.XtraEditors.XtraMessageBox.Show("角色信息编辑失败!");
- }
- else
- {
- trans.Commit();
- DevExpress.XtraEditors.XtraMessageBox.Show("角色信息编辑成功!");
- this.Close();
- }
-
- }
- catch (Exception ex)
- {
- trans.Rollback();
- DevExpress.XtraEditors.XtraMessageBox.Show("保存角色相关信息失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- LogHelper.log.Error(string.Format("保存角色相关信息出现错误:{0}", ex));
- }
- finally
- {
- trans.Dispose();
- }
- }
- /// <summary>
- /// 获取treeList选中节点递归实现
- /// </summary>
- /// <param name="parentNode"></param>
- /// <param name="trans"></param>
- /// <returns></returns>
- private bool GetTreeListCheckedkey(TreeListNode parentNode, DbTransaction trans)
- {
- if (parentNode.Nodes.Count == 0)
- {
- return true;//递归终止
- }
- bool bOK = false;
- //BlRoleRight blRoleRight = new BlRoleRight();
- foreach (TreeListNode node in parentNode.Nodes)
- {
- if (node.CheckState == CheckState.Checked || node.CheckState == CheckState.Indeterminate)
- {
- MoAuthoryRight data = this.treeList1.GetDataRecordByNode(node) as MoAuthoryRight;
- MoRoleRight roleRight = new MoRoleRight();
- roleRight.RoleId = this.teRoleId.Text;
- roleRight.RightId = data.RightId;
- bOK = BLLFactory<BlRoleRight>.Instance.Insert(roleRight, trans);
- }
- bOK = GetTreeListCheckedkey(node, trans);
- }
- return bOK;
- }
- }
- }
|