FormAuthoryGroup.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using DevExpress.XtraEditors;
  11. using ProjectBase.Controls;
  12. using SIMDP.BLL;
  13. using SIMDP.Model;
  14. using ProjectBase.Data.Logs;
  15. using ProjectBase.Data.BaseDAL;
  16. using System.Data.Common;
  17. using ProjectBase.Util;
  18. namespace SIMDP.View
  19. {
  20. public partial class FormAuthoryGroup : BaseDock
  21. {
  22. //BlAuthoryGroup blGroup = new BlAuthoryGroup();
  23. //BlAuthoryUser blUser = new BlAuthoryUser();
  24. //BlAuthoryRole blRole = new BlAuthoryRole();
  25. DbTransaction trans;
  26. bool flag = true;
  27. public FormAuthoryGroup()
  28. {
  29. InitializeComponent();
  30. InitTree_Group();
  31. InitLookUpGroup();
  32. }
  33. /// <summary>
  34. /// 初始化树--组织
  35. /// </summary>
  36. private void InitTree_Group()
  37. {
  38. this.tree_Group.BeginUpdate();
  39. this.tree_Group.Nodes.Clear();
  40. this.tree_Group.ImageList = this.imageList1;
  41. List<MoAuthoryGroup> list = BLLFactory<BlAuthoryGroup>.Instance.GetAll();
  42. List<MoAuthoryGroup> rootList = new List<MoAuthoryGroup>();
  43. foreach (MoAuthoryGroup item in list)
  44. {
  45. if (item.ParentGroupId == "")
  46. {
  47. rootList.Add(item);
  48. }
  49. }
  50. foreach (MoAuthoryGroup root in rootList)
  51. {
  52. if (root != null)
  53. {
  54. TreeNode rootNode = new TreeNode(); //根节点
  55. rootNode.Text = root.GroupName;
  56. rootNode.Name = root.GroupId;
  57. rootNode.ImageIndex = 0;
  58. rootNode.SelectedImageIndex = 0;
  59. this.tree_Group.Nodes.Add(rootNode);
  60. List<MoAuthoryGroup> subList = BLLFactory<BlAuthoryGroup>.Instance.Find(string.Format(" parent_group_id = '{0}'", root.GroupId));
  61. AddNode_Group(subList, rootNode);
  62. }
  63. }
  64. this.tree_Group.ExpandAll();
  65. this.tree_Group.EndUpdate();
  66. this.tree_Group.SelectedNode = this.tree_Group.Nodes[0];
  67. }
  68. /// <summary>
  69. /// 添加树节点--组织
  70. /// </summary>
  71. /// <param name="list"></param>
  72. /// <param name="parentNode"></param>
  73. private void AddNode_Group(List<MoAuthoryGroup> list, TreeNode parentNode)
  74. {
  75. if (list == null)
  76. {
  77. return;
  78. }
  79. foreach (MoAuthoryGroup item in list)
  80. {
  81. TreeNode node = new TreeNode(); //根节点
  82. node.Text = item.GroupName;
  83. node.Name = item.GroupId;
  84. string index = item.GroupId.Substring(0,1);
  85. node.ImageIndex = Convert.ToInt32(index) - 1;
  86. node.SelectedImageIndex = Convert.ToInt32(index) - 1;
  87. //topnode.ImageIndex = Portal.gc.GetImageIndex(groupInfo.Category);
  88. //topnode.SelectedImageIndex = Portal.gc.GetImageIndex(groupInfo.Category);
  89. parentNode.Nodes.Add(node);
  90. List<MoAuthoryGroup> subList = BLLFactory<BlAuthoryGroup>.Instance.Find(string.Format(" parent_group_id = '{0}'", item.GroupId));
  91. AddNode_Group(subList, node);
  92. }
  93. }
  94. private void tree_Group_AfterSelect(object sender, TreeViewEventArgs e)
  95. {
  96. if (e.Node != null)
  97. {
  98. MoAuthoryGroup group = new MoAuthoryGroup();
  99. group = BLLFactory<BlAuthoryGroup>.Instance.FindByID(e.Node.Name);
  100. this.txt_GroupId.Text = group.GroupId;
  101. this.txt_GroupName.Text = group.GroupName;
  102. if (!string.IsNullOrEmpty(group.ParentGroupId))
  103. {
  104. this.treelookUp_PGroup.EditValue = group.ParentGroupId;
  105. this.txt_GroupId.Enabled = true;
  106. this.treelookUp_PGroup.Enabled = true;
  107. }
  108. else
  109. {
  110. this.txt_GroupId.Enabled = false;
  111. this.treelookUp_PGroup.Enabled = false;
  112. }
  113. RefreshRole(group.GroupId);
  114. RefreshUser(group.GroupId);
  115. }
  116. }
  117. /// <summary>
  118. /// 初始化上层组织控件
  119. /// </summary>
  120. private void InitLookUpGroup()
  121. {
  122. DataTable dt = new DataTable();
  123. dt.Columns.Add("ImageIndex", typeof(int));
  124. dt.Columns.Add("GroupID", typeof(string));
  125. dt.Columns.Add("GroupPID", typeof(string));
  126. dt.Columns.Add("GroupName", typeof(string));
  127. List<MoAuthoryGroup> list = BLLFactory<BlAuthoryGroup>.Instance.GetAll();
  128. DataRow dr = null;
  129. foreach (MoAuthoryGroup info in list)
  130. {
  131. dr = dt.NewRow();
  132. dr["ImageIndex"] = 0;
  133. dr["GroupID"] = info.GroupId;
  134. dr["GroupPID"] = info.ParentGroupId;
  135. dr["GroupName"] = info.GroupName;
  136. dt.Rows.Add(dr);
  137. }
  138. //增加一行空的
  139. dr = dt.NewRow();
  140. dr["GroupID"] = "0"; //使用0代替-1,避免出现节点的嵌套显示,因为-1已经作为了一般节点的顶级标识
  141. dr["GroupPID"] = "-1";
  142. dr["GroupName"] = "无";
  143. dt.Rows.InsertAt(dr, 0);
  144. //设置图形序号
  145. //this.treeListLookUpEdit1TreeList.SelectImageList = this.imageList2;
  146. //this.treeListLookUpEdit1TreeList.StateImageList = this.imageList2;
  147. this.treelookUp_PGroup.Properties.TreeList.KeyFieldName = "GroupID";
  148. this.treelookUp_PGroup.Properties.TreeList.ParentFieldName = "GroupPID";
  149. this.treelookUp_PGroup.Properties.DataSource = dt;
  150. this.treelookUp_PGroup.Properties.ValueMember = "GroupID";
  151. this.treelookUp_PGroup.Properties.DisplayMember = "GroupName";
  152. this.treelookUp_PGroup.EditValue = "0";
  153. }
  154. private void menu_Add_Click(object sender, EventArgs e)
  155. {
  156. TreeNode choiceNode = this.tree_Group.SelectedNode;
  157. //if (BLLFactory<BlAuthoryGroup>.Instance.GetRecordCount() <= 0)
  158. //{
  159. // DevExpress.XtraEditors.XtraMessageBox.Show("请先添加组织信息", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
  160. // return;
  161. //}
  162. if (choiceNode == null)
  163. {
  164. this.txt_GroupName.Text = null;
  165. this.txt_GroupId.Text = null;
  166. this.treelookUp_PGroup.EditValue = "0";
  167. this.txt_GroupId.Enabled = true;
  168. this.treelookUp_PGroup.Enabled = true;
  169. }
  170. if (choiceNode.Level == 0)
  171. {
  172. this.txt_GroupName.Text = null;
  173. this.txt_GroupId.Text = null;
  174. this.treelookUp_PGroup.EditValue = choiceNode.Name;
  175. this.txt_GroupId.Enabled = true;
  176. this.treelookUp_PGroup.Enabled = true;
  177. }
  178. else
  179. {
  180. this.txt_GroupName.Text = null;
  181. this.txt_GroupId.Text = null;
  182. this.treelookUp_PGroup.EditValue = choiceNode.Name;
  183. }
  184. }
  185. private void menu_Delete_Click(object sender, EventArgs e)
  186. {
  187. TreeNode node = this.tree_Group.SelectedNode;
  188. if (node == null)
  189. {
  190. DevExpress.XtraEditors.XtraMessageBox.Show("请选择要删除的记录", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
  191. return;
  192. }
  193. if (DevExpress.XtraEditors.XtraMessageBox.Show("您确定删除选定的记录么?", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No)
  194. {
  195. return;
  196. }
  197. if (node.Level == 0)
  198. {
  199. DevExpress.XtraEditors.XtraMessageBox.Show("不能删除集团,请重新选择", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
  200. return;
  201. }
  202. List<string> groupId = new List<string>();
  203. FetchNode(node,ref groupId);
  204. using (trans = BLLFactory<BlAuthoryUser>.Instance.CreateTransaction())
  205. {
  206. try
  207. {
  208. foreach (string id in groupId)
  209. {
  210. if (flag)
  211. {
  212. flag = BLLFactory<BlAuthoryGroup>.Instance.Delete(id, SysEnvironment.CurrentLoginID, trans);
  213. }
  214. else
  215. {
  216. break;
  217. }
  218. }
  219. trans.Commit();
  220. }
  221. catch (Exception ex)
  222. {
  223. trans.Rollback();
  224. DevExpress.XtraEditors.XtraMessageBox.Show("删除失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  225. LogHelper.log.Error(string.Format("删除数据库authory_group出现错误:{0}", ex));
  226. }
  227. }
  228. trans.Dispose();
  229. if (flag)
  230. {
  231. DevExpress.XtraEditors.XtraMessageBox.Show("删除成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  232. InitTree_Group();
  233. InitLookUpGroup();
  234. }
  235. }
  236. /// <summary>
  237. /// 遍历一个节点下的所有子节点
  238. /// </summary>
  239. /// <param name="node"> 要遍历的节点</param>
  240. /// <param name="groupId"> 该节点与所有子节点名字的集合</param>
  241. private void FetchNode(TreeNode node,ref List<string> groupId)
  242. {
  243. groupId.Add(node.Name);
  244. for (int i = 0; i < node.Nodes.Count; i++)
  245. {
  246. FetchNode(node.Nodes[i],ref groupId);
  247. }
  248. }
  249. private void menu_Expand_Click(object sender, EventArgs e)
  250. {
  251. this.tree_Group.ExpandAll();
  252. }
  253. private void menu_Collapse_Click(object sender, EventArgs e)
  254. {
  255. this.tree_Group.CollapseAll();
  256. }
  257. private void menu_Refresh_Click(object sender, EventArgs e)
  258. {
  259. InitTree_Group();
  260. }
  261. /// <summary>
  262. /// 刷新包含用户
  263. /// </summary>
  264. /// <param name="id">组ID</param>
  265. private void RefreshUser(string id)
  266. {
  267. this.listBox_User.BeginUpdate();
  268. this.listBox_User.Items.Clear();
  269. List<MoAuthoryUser> list = BLLFactory<BlAuthoryUser>.Instance.Find(string.Format(" group_id = '{0}'",id));
  270. foreach (MoAuthoryUser info in list)
  271. {
  272. string name = string.Format("{0}({1})", info.UserName, info.LoginAccount);
  273. this.listBox_User.Items.Add(name);
  274. }
  275. if (this.listBox_User.Items.Count > 0)
  276. {
  277. this.listBox_User.SelectedIndex = 0;
  278. }
  279. this.listBox_User.EndUpdate();
  280. }
  281. /// <summary>
  282. /// 刷新所属角色
  283. /// </summary>
  284. /// <param name="id">组ID</param>
  285. private void RefreshRole(string id)
  286. {
  287. this.listBox_Role.BeginUpdate();
  288. this.listBox_Role.Items.Clear();
  289. string sql = " role_id IN (SELECT b.role_id FROM group_role b WHERE b.group_id = '"+ id +"')";
  290. List<MoAuthoryRole> list = BLLFactory<BlAuthoryRole>.Instance.Find(sql);
  291. foreach (MoAuthoryRole info in list)
  292. {
  293. string name = string.Format("{0}({1})", info.RoleName, info.RoleId);
  294. this.listBox_Role.Items.Add(name);
  295. }
  296. if (this.listBox_Role.Items.Count > 0)
  297. {
  298. this.listBox_Role.SelectedIndex = 0;
  299. }
  300. this.listBox_Role.EndUpdate();
  301. }
  302. private void btn_Save_Click(object sender, EventArgs e)
  303. {
  304. if (string.IsNullOrEmpty(txt_GroupId.Text))
  305. {
  306. DevExpress.XtraEditors.XtraMessageBox.Show("请输入组织名称", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  307. return;
  308. }
  309. if (string.IsNullOrEmpty(txt_GroupId.Text))
  310. {
  311. DevExpress.XtraEditors.XtraMessageBox.Show("请输入组织编号", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  312. return;
  313. }
  314. try
  315. {
  316. MoAuthoryGroup group = new MoAuthoryGroup();
  317. group.GroupName = txt_GroupName.Text;
  318. group.GroupId = txt_GroupId.Text;
  319. group.GroupTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  320. if (this.treelookUp_PGroup.EditValue.ToString() != "0")
  321. {
  322. group.ParentGroupId = treelookUp_PGroup.EditValue.ToString();
  323. }
  324. bool flag = BLLFactory<BlAuthoryGroup>.Instance.InsertUpdate(group, group.GroupId);
  325. if (flag)
  326. {
  327. DevExpress.XtraEditors.XtraMessageBox.Show("保存成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  328. InitTree_Group();
  329. InitLookUpGroup();
  330. }
  331. else
  332. {
  333. DevExpress.XtraEditors.XtraMessageBox.Show("保存失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  334. }
  335. }
  336. catch (Exception ex)
  337. {
  338. DevExpress.XtraEditors.XtraMessageBox.Show("保存失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  339. LogHelper.log.Error(string.Format("添加或修改数据库authory_group出现错误:{0}", ex));
  340. }
  341. }
  342. private void tree_Group_MouseDown(object sender, MouseEventArgs e)
  343. {
  344. if (e.Button == MouseButtons.Right)
  345. {
  346. TreeNode node = tree_Group.GetNodeAt(e.X, e.Y);
  347. if (node != null)
  348. {
  349. tree_Group.SelectedNode = node;
  350. }
  351. }
  352. base.OnMouseDown(e);
  353. }
  354. }
  355. }