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.View { public partial class FormEditAuthoryRole : DevExpress.XtraEditors.XtraForm { private MoAuthoryRole m_Role = null; public List groupList = new List(); 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.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 moRoleRightList = new List(); moRoleRightList = BLLFactory.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.Instance.FindSingle(string.Format(" role_id = '{0}'", m_Role.RoleId)); this.treeListLookUpEditGroup.EditValue = mo.GroupId; } private void SetTreeListChecked(TreeListNode parentNode, List 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); } } /// /// 选中后状态 /// /// /// private void treeList1_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e) { SetCheckedChildNodes(e.Node, e.Node.CheckState); SetCheckedParentNodes(e.Node, e.Node.CheckState); } /// /// 选中前状态 /// /// /// private void treeList1_BeforeCheckNode(object sender, DevExpress.XtraTreeList.CheckNodeEventArgs e) { e.State = (e.PrevState == CheckState.Checked ? CheckState.Unchecked : CheckState.Checked); } /// /// 设置子节点的状态 /// /// /// 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); } } /// /// 设置父节点的状态 /// /// /// 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 columnNameAlias = new Dictionary();//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 rightList = new List(); //BlAuthoryRight bl = new BlAuthoryRight(); //BlRoleRight blRole = new BlRoleRight(); rightList = BLLFactory.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.Instance.CreateTransaction(); bool bOK = BLLFactory.Instance.InsertUpdate(role,role.RoleId, trans); if (bOK) { string condition = string.Format(" role_id = '{0}'", role.RoleId); if (m_Role !=null && BLLFactory.Instance.IsExistRecord(condition, trans)) { bOK = BLLFactory.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.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.Instance.IsExistRecord(condition, trans)) bOK = BLLFactory.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(); } } /// /// 获取treeList选中节点递归实现 /// /// /// /// 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.Instance.Insert(roleRight, trans); } bOK = GetTreeListCheckedkey(node, trans); } return bOK; } } }