FormEditAuthoryRole.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. using DevExpress.XtraTreeList.Columns;
  2. using DevExpress.XtraTreeList.Nodes;
  3. using ProjectBase.Data.BaseDAL;
  4. using ProjectBase.Data.Logs;
  5. using SIMDP.BLL;
  6. using SIMDP.Model;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.ComponentModel;
  10. using System.Data;
  11. using System.Data.Common;
  12. using System.Drawing;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. using System.Windows.Forms;
  17. namespace SIMDP
  18. {
  19. public partial class FormEditAuthoryRole : DevExpress.XtraEditors.XtraForm
  20. {
  21. private MoAuthoryRole m_Role = null;
  22. public List<MoAuthoryGroup> groupList = new List<MoAuthoryGroup>();
  23. public FormEditAuthoryRole(MoAuthoryRole role)
  24. {
  25. InitializeComponent();
  26. m_Role = role;
  27. }
  28. private void FormEditAuthoryRole_Load(object sender, EventArgs e)
  29. {
  30. //BlAuthoryGroup blGroup = new BlAuthoryGroup();
  31. string condition = string.Format(" group_id LIKE '[1,2]%'");
  32. groupList = BLLFactory<BlAuthoryGroup>.Instance.Find(condition);
  33. this.treeListLookUpEditGroup.Properties.DataSource = groupList;
  34. InitTreeList();
  35. if (m_Role == null)
  36. {
  37. return;
  38. }
  39. this.teRoleId.Enabled = false;
  40. this.treeListLookUpEdit1TreeList.Enabled = false;
  41. //BlRoleRight blRoleRight = new BlRoleRight();
  42. List<MoRoleRight> moRoleRightList = new List<MoRoleRight>();
  43. moRoleRightList = BLLFactory<BlRoleRight>.Instance.FindByIDs(m_Role.RoleId);
  44. foreach (TreeListNode node in this.treeList1.Nodes)
  45. {
  46. SetTreeListChecked(node, moRoleRightList);
  47. }
  48. //BlGroupRole blGroupRole = new BlGroupRole();
  49. this.teRoleId.Text = m_Role.RoleId;
  50. this.teRoleName.Text = m_Role.RoleName;
  51. this.tbDescription.Text = m_Role.RoleDescription;
  52. MoGroupRole mo = BLLFactory<BlGroupRole>.Instance.FindSingle(string.Format(" role_id = '{0}'", m_Role.RoleId));
  53. this.treeListLookUpEditGroup.EditValue = mo.GroupId;
  54. }
  55. private void SetTreeListChecked(TreeListNode parentNode, List<MoRoleRight> roleRightList)
  56. {
  57. MoAuthoryRight data = this.treeList1.GetDataRecordByNode(parentNode) as MoAuthoryRight;
  58. for (int i = 0; i < roleRightList.Count; i++)
  59. {
  60. if (roleRightList[i].RightId == data.RightId)
  61. parentNode.Checked = true;
  62. }
  63. if (parentNode.Nodes.Count == 0)
  64. return;
  65. foreach (TreeListNode node in parentNode.Nodes)
  66. {
  67. SetTreeListChecked(node, roleRightList);
  68. }
  69. }
  70. /// <summary>
  71. /// 选中后状态
  72. /// </summary>
  73. /// <param name="sender"></param>
  74. /// <param name="e"></param>
  75. private void treeList1_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
  76. {
  77. SetCheckedChildNodes(e.Node, e.Node.CheckState);
  78. SetCheckedParentNodes(e.Node, e.Node.CheckState);
  79. }
  80. /// <summary>
  81. /// 选中前状态
  82. /// </summary>
  83. /// <param name="sender"></param>
  84. /// <param name="e"></param>
  85. private void treeList1_BeforeCheckNode(object sender, DevExpress.XtraTreeList.CheckNodeEventArgs e)
  86. {
  87. e.State = (e.PrevState == CheckState.Checked ? CheckState.Unchecked : CheckState.Checked);
  88. }
  89. /// <summary>
  90. /// 设置子节点的状态
  91. /// </summary>
  92. /// <param name="node"></param>
  93. /// <param name="check"></param>
  94. private void SetCheckedChildNodes(DevExpress.XtraTreeList.Nodes.TreeListNode node, CheckState check)
  95. {
  96. for (int i = 0; i < node.Nodes.Count; i++)
  97. {
  98. node.Nodes[i].CheckState = check;
  99. SetCheckedChildNodes(node.Nodes[i], check);
  100. }
  101. }
  102. /// <summary>
  103. /// 设置父节点的状态
  104. /// </summary>
  105. /// <param name="node"></param>
  106. /// <param name="check"></param>
  107. private void SetCheckedParentNodes(DevExpress.XtraTreeList.Nodes.TreeListNode node, CheckState check)
  108. {
  109. if (node.ParentNode != null)
  110. {
  111. bool b = false;
  112. CheckState state;
  113. for (int i = 0; i < node.ParentNode.Nodes.Count; i++)
  114. {
  115. state = (CheckState)node.ParentNode.Nodes[i].CheckState;
  116. if (!check.Equals(state))
  117. {
  118. b = !b;
  119. break;
  120. }
  121. }
  122. node.ParentNode.CheckState = b ? CheckState.Indeterminate : check;
  123. SetCheckedParentNodes(node.ParentNode, check);
  124. }
  125. }
  126. private void InitTreeList()
  127. {
  128. #region 添加别名解析
  129. Dictionary<string, string> columnNameAlias = new Dictionary<string, string>();//Treelist中字段别名字典集合
  130. this.treeList1.Columns.Clear();
  131. columnNameAlias.Clear();
  132. columnNameAlias.Add("RightName", "权限名称");
  133. foreach (var item in columnNameAlias)
  134. {
  135. TreeListColumn col = new TreeListColumn();
  136. col.Caption = item.Value;
  137. col.FieldName = item.Key;
  138. col.Visible = true;
  139. this.treeList1.Columns.Add(col);
  140. }
  141. this.treeList1.KeyFieldName = "RightId";
  142. this.treeList1.ParentFieldName = "ParentRightId";
  143. this.treeList1.LookAndFeel.UseDefaultLookAndFeel = false; // 设置“+” ,“-”
  144. this.treeList1.LookAndFeel.UseWindowsXPTheme = true;
  145. #endregion
  146. List<MoAuthoryRight> rightList = new List<MoAuthoryRight>();
  147. //BlAuthoryRight bl = new BlAuthoryRight();
  148. //BlRoleRight blRole = new BlRoleRight();
  149. rightList = BLLFactory<BlAuthoryRight>.Instance.GetAll();
  150. this.treeList1.DataSource = rightList;
  151. this.treeList1.RefreshDataSource();
  152. }
  153. private void sbtnExit_Click(object sender, EventArgs e)
  154. {
  155. this.Close();
  156. }
  157. private void sbtnSave_Click(object sender, EventArgs e)
  158. {
  159. DbTransaction trans = null;
  160. try
  161. {
  162. //BlAuthoryRole blRole = new BlAuthoryRole();
  163. //BlRoleRight blRoleRight = new BlRoleRight();
  164. MoAuthoryRole role = new MoAuthoryRole();
  165. role.RoleId = this.teRoleId.Text;
  166. role.RoleName = this.teRoleName.Text;
  167. role.RoleDescription = this.tbDescription.Text;
  168. trans = BLLFactory<BlAuthoryRole>.Instance.CreateTransaction();
  169. bool bOK = BLLFactory<BlAuthoryRole>.Instance.InsertUpdate(role,role.RoleId, trans);
  170. if (bOK)
  171. {
  172. string condition = string.Format(" role_id = '{0}'", role.RoleId);
  173. if (m_Role !=null && BLLFactory<BlRoleRight>.Instance.IsExistRecord(condition, trans))
  174. {
  175. bOK = BLLFactory<BlRoleRight>.Instance.DeleteByCondition(condition, trans);
  176. }
  177. }
  178. if (bOK)
  179. {
  180. foreach (TreeListNode node in this.treeList1.Nodes)
  181. {
  182. if (node.CheckState == CheckState.Checked || node.CheckState == CheckState.Indeterminate)
  183. {
  184. MoAuthoryRight data = this.treeList1.GetDataRecordByNode(node) as MoAuthoryRight;
  185. MoRoleRight roleRight = new MoRoleRight();
  186. roleRight.RoleId = this.teRoleId.Text;
  187. roleRight.RightId = data.RightId;
  188. bOK = BLLFactory<BlRoleRight>.Instance.Insert(roleRight, trans);
  189. }
  190. if (bOK)
  191. bOK = GetTreeListCheckedkey(node, trans);
  192. }
  193. //BlGroupRole blGroupRole = new BlGroupRole();
  194. MoGroupRole moGroupRole = new MoGroupRole();
  195. moGroupRole.GroupId = this.treeListLookUpEditGroup.EditValue.ToString();
  196. moGroupRole.RoleId = role.RoleId;
  197. string condition = string.Format(" role_id = '{0}'", role.RoleId);
  198. if (!BLLFactory<BlGroupRole>.Instance.IsExistRecord(condition, trans))
  199. bOK = BLLFactory<BlGroupRole>.Instance.Insert(moGroupRole, trans);
  200. }
  201. if (!bOK)
  202. {
  203. trans.Rollback();
  204. DevExpress.XtraEditors.XtraMessageBox.Show("角色信息编辑失败!");
  205. }
  206. else
  207. {
  208. trans.Commit();
  209. DevExpress.XtraEditors.XtraMessageBox.Show("角色信息编辑成功!");
  210. this.Close();
  211. }
  212. }
  213. catch (Exception ex)
  214. {
  215. trans.Rollback();
  216. DevExpress.XtraEditors.XtraMessageBox.Show("保存角色相关信息失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  217. LogHelper.log.Error(string.Format("保存角色相关信息出现错误:{0}", ex));
  218. }
  219. finally
  220. {
  221. trans.Dispose();
  222. }
  223. }
  224. /// <summary>
  225. /// 获取treeList选中节点递归实现
  226. /// </summary>
  227. /// <param name="parentNode"></param>
  228. /// <param name="trans"></param>
  229. /// <returns></returns>
  230. private bool GetTreeListCheckedkey(TreeListNode parentNode, DbTransaction trans)
  231. {
  232. if (parentNode.Nodes.Count == 0)
  233. {
  234. return true;//递归终止
  235. }
  236. bool bOK = false;
  237. //BlRoleRight blRoleRight = new BlRoleRight();
  238. foreach (TreeListNode node in parentNode.Nodes)
  239. {
  240. if (node.CheckState == CheckState.Checked || node.CheckState == CheckState.Indeterminate)
  241. {
  242. MoAuthoryRight data = this.treeList1.GetDataRecordByNode(node) as MoAuthoryRight;
  243. MoRoleRight roleRight = new MoRoleRight();
  244. roleRight.RoleId = this.teRoleId.Text;
  245. roleRight.RightId = data.RightId;
  246. bOK = BLLFactory<BlRoleRight>.Instance.Insert(roleRight, trans);
  247. }
  248. bOK = GetTreeListCheckedkey(node, trans);
  249. }
  250. return bOK;
  251. }
  252. }
  253. }