FormAuthoryRole.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. using DevExpress.XtraTreeList.Columns;
  2. using DevExpress.XtraTreeList.Data;
  3. using DevExpress.XtraTreeList.Nodes;
  4. using ProjectBase.Data.BaseDAL;
  5. using ProjectBase.Data.Logs;
  6. using SIMDP.BLL;
  7. using SIMDP.Model;
  8. using ProjectBase.Util;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.ComponentModel;
  12. using System.Data;
  13. using System.Data.Common;
  14. using System.Drawing;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using System.Windows.Forms;
  19. namespace SIMDP.View
  20. {
  21. public partial class FormAuthoryRole : DevExpress.XtraEditors.XtraForm
  22. {
  23. //private BlAuthoryGroup blGroup = new BlAuthoryGroup();
  24. private string selectTreeNode = ""; // 选中节点
  25. private int treeLevel = 0; // 树形结构的层级
  26. private int nodeType = 0; // 选中节点类型,0,代表组织,1,代表角色
  27. public FormAuthoryRole()
  28. {
  29. InitializeComponent();
  30. }
  31. private void FormAuthoryRole_Load(object sender, EventArgs e)
  32. {
  33. InitTree();
  34. InitGrid();
  35. }
  36. /// <summary>
  37. /// 初始化树
  38. /// </summary>
  39. private void InitTree()
  40. {
  41. this.treeViewRole.BeginUpdate();
  42. this.treeViewRole.Nodes.Clear();
  43. this.treeViewRole.ImageList = this.imageList1;
  44. string condition = string.Format(" parent_group_id =''");
  45. List<MoAuthoryGroup> list = BLLFactory<BlAuthoryGroup>.Instance.Find(condition);
  46. List<MoAuthoryGroup> rootList = new List<MoAuthoryGroup>();
  47. foreach (MoAuthoryGroup item in list)
  48. {
  49. rootList.Add(item);
  50. treeLevel = 1;
  51. }
  52. foreach (MoAuthoryGroup root in rootList)
  53. {
  54. if (root != null)
  55. {
  56. TreeNode rootNode = new TreeNode(); //根节点
  57. rootNode.Text = root.GroupName;
  58. rootNode.Name = root.GroupId;
  59. rootNode.ImageIndex = 0;
  60. rootNode.SelectedImageIndex = 0;
  61. AddChildRole(rootNode);
  62. if (root.GroupId.Substring(0, 1).Equals("1"))
  63. {
  64. List<MoAuthoryGroup> subList = BLLFactory<BlAuthoryGroup>.Instance.Find(string.Format(" parent_group_id = '{0}'", root.GroupId));
  65. AddChildNode(subList, rootNode);
  66. treeLevel = 2;
  67. }
  68. this.treeViewRole.Nodes.Add(rootNode);
  69. }
  70. }
  71. this.treeViewRole.ExpandAll();
  72. this.treeViewRole.EndUpdate();
  73. }
  74. /// <summary>
  75. /// 添加公司级子节点
  76. /// </summary>
  77. /// <param name="list"></param>
  78. /// <param name="parentNode"></param>
  79. private void AddChildNode(List<MoAuthoryGroup> list, TreeNode parentNode)
  80. {
  81. if (list == null)
  82. {
  83. return;
  84. }
  85. foreach (MoAuthoryGroup item in list)
  86. {
  87. TreeNode node = new TreeNode(); //公司级子节点
  88. node.Text = item.GroupName;
  89. node.Name = item.GroupId;
  90. node.ImageIndex = 1;
  91. node.SelectedImageIndex = 1;
  92. parentNode.Nodes.Add(node);
  93. AddChildRole(node);
  94. }
  95. }
  96. /// <summary>
  97. /// 添加角色节点
  98. /// </summary>
  99. /// <param name="parentNode"></param>
  100. private void AddChildRole(TreeNode parentNode)
  101. {
  102. string sql =" role_id in (SELECT b.role_id from group_role b where b.group_id = '" + parentNode.Name + "')";
  103. //BlAuthoryRole blRole = new BlAuthoryRole();
  104. List<MoAuthoryRole> roleList = new List<MoAuthoryRole>();
  105. roleList = BLLFactory<BlAuthoryRole>.Instance.Find(sql);
  106. foreach (MoAuthoryRole item in roleList)
  107. {
  108. TreeNode node = new TreeNode();
  109. node.Text = item.RoleName;
  110. node.Name = item.RoleId;
  111. node.ImageIndex = 2;
  112. node.SelectedImageIndex = 2;
  113. parentNode.Nodes.Add(node);
  114. }
  115. }
  116. /// <summary>
  117. /// 初始化表格控件
  118. /// </summary>
  119. private void InitGrid()
  120. {
  121. //entity
  122. this.winGridViewPager1.OnPageChanged += new EventHandler(winGridViewPager1_OnPageChanged);
  123. this.winGridViewPager1.OnStartExport += new EventHandler(winGridViewPager1_OnStartExport);
  124. this.winGridViewPager1.OnEditSelected += new EventHandler(winGridViewPager1_OnEditSelected);
  125. this.winGridViewPager1.OnAddNew += new EventHandler(winGridViewPager1_OnAddNew);
  126. this.winGridViewPager1.OnDeleteSelected += new EventHandler(winGridViewPager1_OnDeleteSelected);
  127. this.winGridViewPager1.OnRefresh += new EventHandler(winGridViewPager1_OnRefresh);
  128. this.winGridViewPager1.gridView1.SelectionChanged += new DevExpress.Data.SelectionChangedEventHandler(winGridViewPager1_OnSelect);
  129. //this.winGridViewPager1.AppendedMenu = this.contextMenuStrip1;
  130. this.winGridViewPager1.ShowLineNumber = true;
  131. this.winGridViewPager1.BestFitColumnWith = true;
  132. this.winGridViewPager1.DisplayColumns = "RoleId,RoleName,RoleTime,RoleDescription";
  133. #region 添加别名解析
  134. this.winGridViewPager1.AddColumnAlias("RoleId", "角色编码");
  135. this.winGridViewPager1.AddColumnAlias("RoleName", "角色名称");
  136. this.winGridViewPager1.AddColumnAlias("RoleTime", "角色生成时间");
  137. this.winGridViewPager1.AddColumnAlias("RoleDescription", "角色描述");
  138. #endregion
  139. this.winGridViewPager1.PrintTitle = "角色信息报表";
  140. this.winGridViewPager1.gridView1.Appearance.HeaderPanel.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
  141. for (int i = 0; i < this.winGridViewPager1.gridView1.Columns.Count; i++) //设置每列内容居中显示
  142. {
  143. this.winGridViewPager1.gridView1.Columns[i].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
  144. }
  145. }
  146. /// <summary>
  147. /// 绑定数据
  148. /// </summary>
  149. /// <param name="condition">组编码</param>
  150. private void BindData(string condition, int nodeType)
  151. {
  152. try
  153. {
  154. //BlAuthoryRole blRole = new BlAuthoryRole();
  155. if (!string.IsNullOrEmpty(condition))
  156. {
  157. string sql = "";
  158. if (nodeType == 0)
  159. {
  160. sql = " role_id in (SELECT b.role_id from group_role b where b.group_id = '" + condition + "')";
  161. }
  162. else
  163. {
  164. sql = " role_id = '" + condition + "'";
  165. }
  166. this.winGridViewPager1.DataSource = BLLFactory<BlAuthoryRole>.Instance.FindWithPager(sql, this.winGridViewPager1.PagerInfo);
  167. }
  168. }
  169. catch (Exception ex)
  170. {
  171. DevExpress.XtraEditors.XtraMessageBox.Show("查询角色信息失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  172. LogHelper.log.Error(string.Format("查询角色信息失败,失败原因为:{0}", ex));
  173. }
  174. }
  175. /// <summary>
  176. /// 选中事件
  177. /// </summary>
  178. /// <param name="sender"></param>
  179. /// <param name="e"></param>
  180. private void winGridViewPager1_OnSelect(object sender, EventArgs e)
  181. {
  182. MoAuthoryRole selectRow = this.winGridViewPager1.gridView1.GetFocusedRow() as MoAuthoryRole;
  183. InitTreeList();
  184. //BlRoleRight blRole = new BlRoleRight();
  185. List<MoRoleRight> roleRightList = BLLFactory<BlRoleRight>.Instance.FindByIDs(selectRow.RoleId);
  186. foreach (TreeListNode node in this.treeListRight.Nodes)
  187. {
  188. SetTreeListChecked(node, roleRightList);
  189. }
  190. }
  191. private void SetTreeListChecked(TreeListNode parentNode, List<MoRoleRight> roleRightList)
  192. {
  193. MoAuthoryRight data = this.treeListRight.GetDataRecordByNode(parentNode) as MoAuthoryRight;
  194. for (int i = 0; i < roleRightList.Count; i++)
  195. {
  196. if (roleRightList[i].RightId == data.RightId)
  197. parentNode.Checked = true;
  198. }
  199. if (parentNode.Nodes.Count == 0)
  200. return;
  201. foreach (TreeListNode node in parentNode.Nodes)
  202. {
  203. SetTreeListChecked(node, roleRightList);
  204. }
  205. }
  206. private void InitTreeList()
  207. {
  208. #region 添加别名解析
  209. Dictionary<string, string> columnNameAlias = new Dictionary<string, string>();//Treelist中字段别名字典集合
  210. this.treeListRight.Columns.Clear();
  211. columnNameAlias.Clear();
  212. //columnNameAlias.Add("Check", "选中状态");
  213. columnNameAlias.Add("RightName", "权限名称");
  214. foreach (var item in columnNameAlias)
  215. {
  216. TreeListColumn col = new TreeListColumn();
  217. col.Caption = item.Value;
  218. col.FieldName = item.Key;
  219. col.Visible = true;
  220. col.Name = item.Key;
  221. this.treeListRight.Columns.Add(col);
  222. }
  223. this.treeListRight.KeyFieldName = "RightId";
  224. this.treeListRight.ParentFieldName = "ParentRightId";
  225. this.treeListRight.LookAndFeel.UseDefaultLookAndFeel = false; // 设置“+” ,“-”
  226. this.treeListRight.LookAndFeel.UseWindowsXPTheme = true;
  227. #endregion
  228. List<MoAuthoryRight> rightList = new List<MoAuthoryRight>();
  229. //BlAuthoryRight bl = new BlAuthoryRight();
  230. rightList = BLLFactory<BlAuthoryRight>.Instance.GetAll();
  231. this.treeListRight.DataSource = rightList;
  232. this.treeListRight.RefreshDataSource();
  233. }
  234. /// <summary>
  235. /// 添加列序号
  236. /// </summary>
  237. /// <param name="sender"></param>
  238. /// <param name="e"></param>
  239. private void treeListRight_CustomDrawNodeIndicator(object sender, DevExpress.XtraTreeList.CustomDrawNodeIndicatorEventArgs e)
  240. {
  241. DevExpress.XtraTreeList.TreeList tmpTree = sender as DevExpress.XtraTreeList.TreeList;
  242. DevExpress.Utils.Drawing.IndicatorObjectInfoArgs args = e.ObjectArgs as DevExpress.Utils.Drawing.IndicatorObjectInfoArgs;
  243. if (args != null)
  244. {
  245. int rowNum = tmpTree.GetVisibleIndexByNode(e.Node) + 1;
  246. this.treeListRight.IndicatorWidth = 30;
  247. args.DisplayText = rowNum.ToString();
  248. }
  249. }
  250. /// <summary>
  251. /// 分页控件翻页的操作
  252. /// </summary>
  253. private void winGridViewPager1_OnPageChanged(object sender, EventArgs e)
  254. {
  255. BindData(selectTreeNode, nodeType);
  256. }
  257. /// <summary>
  258. /// 分页控件全部导出操作前的操作
  259. /// </summary>
  260. private void winGridViewPager1_OnStartExport(object sender, EventArgs e)
  261. {
  262. //this.winGridViewPager1.AllToExport = blMenu.Find(condition);
  263. }
  264. /// <summary>
  265. /// 分页控件编辑项操作
  266. /// </summary>
  267. private void winGridViewPager1_OnEditSelected(object sender, EventArgs e)
  268. {
  269. MoAuthoryRole selectRow = this.winGridViewPager1.gridView1.GetFocusedRow() as MoAuthoryRole;
  270. if (selectRow == null)
  271. {
  272. DevExpress.XtraEditors.XtraMessageBox.Show("请选择一行数据!");
  273. return;
  274. }
  275. FormEditAuthoryRole editAuthoryRole = new FormEditAuthoryRole(selectRow);
  276. editAuthoryRole.StartPosition = FormStartPosition.CenterScreen;
  277. editAuthoryRole.ShowDialog();
  278. BindData(selectTreeNode, nodeType);
  279. InitTree();
  280. //BlRoleRight blRole = new BlRoleRight();
  281. List<MoRoleRight> roleRightList = BLLFactory<BlRoleRight>.Instance.FindByIDs(selectRow.RoleId);
  282. foreach (TreeListNode node in this.treeListRight.Nodes)
  283. {
  284. SetTreeListChecked(node, roleRightList);
  285. }
  286. }
  287. ///// <summary>
  288. ///// 分页控件新增操作
  289. ///// </summary>
  290. private void winGridViewPager1_OnAddNew(object sender, EventArgs e)
  291. {
  292. FormEditAuthoryRole editAuthoryRole = new FormEditAuthoryRole(null);
  293. editAuthoryRole.StartPosition = FormStartPosition.CenterScreen;
  294. editAuthoryRole.ShowDialog();
  295. InitTree();
  296. InitTreeList();
  297. BindData(selectTreeNode, nodeType);
  298. }
  299. /// <summary>
  300. /// 分页控件删除操作
  301. /// </summary>
  302. /// <param name="sender"></param>
  303. /// <param name="e"></param>
  304. private void winGridViewPager1_OnDeleteSelected(object sender, EventArgs e)
  305. {
  306. bool bOK = false;
  307. DbTransaction trans = null;
  308. try
  309. {
  310. if (DevExpress.XtraEditors.XtraMessageBox.Show("您确定删除选定的记录么?", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No)
  311. {
  312. return;
  313. }
  314. MoAuthoryRole selectRow = this.winGridViewPager1.gridView1.GetFocusedRow() as MoAuthoryRole;
  315. trans = BLLFactory<BlAuthoryGroup>.Instance.CreateTransaction();
  316. //BlAuthoryRole blRole = new BlAuthoryRole();
  317. bOK = BLLFactory<BlAuthoryRole>.Instance.Delete(selectRow.RoleId, SysEnvironment.CurrentLoginID, trans);
  318. //BlRoleRight blRight = new BlRoleRight();
  319. if (bOK)
  320. {
  321. bOK = BLLFactory<BlRoleRight>.Instance.Delete(selectRow.RoleId, SysEnvironment.CurrentLoginID, trans);
  322. }
  323. //BlGroupRole blGroupRole = new BlGroupRole();
  324. if (bOK)
  325. {
  326. string condition = string.Format(" role_id = '{0}'", selectRow.RoleId);
  327. bOK = BLLFactory<BlGroupRole>.Instance.DeleteByCondition(condition, trans);
  328. }
  329. if (!bOK)
  330. {
  331. trans.Rollback();
  332. DevExpress.XtraEditors.XtraMessageBox.Show("删除角色信息失败!");
  333. }
  334. else
  335. {
  336. trans.Commit();
  337. DevExpress.XtraEditors.XtraMessageBox.Show("删除角色信息成功!");
  338. InitTree();
  339. InitTreeList();
  340. BindData(selectTreeNode, nodeType);
  341. }
  342. }
  343. catch (Exception ex)
  344. {
  345. trans.Rollback();
  346. DevExpress.XtraEditors.XtraMessageBox.Show("删除失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  347. LogHelper.log.Error(string.Format("删除数据库角色关联数据出现错误:{0}", ex));
  348. }
  349. finally
  350. {
  351. trans.Dispose();
  352. BindData(selectTreeNode, nodeType);
  353. }
  354. }
  355. /// <summary>
  356. /// 分页控件刷新操作
  357. /// </summary>
  358. private void winGridViewPager1_OnRefresh(object sender, EventArgs e)
  359. {
  360. BindData(selectTreeNode, nodeType);
  361. }
  362. /// <summary>
  363. /// 选中节点响应事件
  364. /// </summary>
  365. /// <param name="sender"></param>
  366. /// <param name="e"></param>
  367. private void TreeNode_AfterSelect(object sender, TreeViewEventArgs e)
  368. {
  369. if (e.Node.Level == 0)
  370. {
  371. if (treeLevel == 2)
  372. return;
  373. else
  374. {
  375. selectTreeNode = e.Node.Name;
  376. nodeType = 0;
  377. BindData(selectTreeNode, nodeType);
  378. }
  379. }
  380. else if (e.Node.Level == 1)
  381. {
  382. if (treeLevel == 2)
  383. {
  384. selectTreeNode = e.Node.Name;
  385. nodeType = 0;
  386. BindData(selectTreeNode, nodeType);
  387. }
  388. else
  389. {
  390. selectTreeNode = e.Node.Name;
  391. nodeType = 1;
  392. BindData(selectTreeNode, nodeType);
  393. }
  394. }
  395. else
  396. {
  397. selectTreeNode = e.Node.Name;
  398. nodeType = 1;
  399. BindData(selectTreeNode, nodeType);
  400. }
  401. }
  402. private void menu_Add_Click(object sender, EventArgs e)
  403. {
  404. winGridViewPager1_OnAddNew(sender, e);
  405. }
  406. private void menu_Expand_Click(object sender, EventArgs e)
  407. {
  408. this.treeViewRole.ExpandAll();
  409. }
  410. private void menu_Collapse_Click(object sender, EventArgs e)
  411. {
  412. this.treeViewRole.CollapseAll();
  413. }
  414. private void menu_Refresh_Click(object sender, EventArgs e)
  415. {
  416. InitTree();
  417. }
  418. }
  419. }