FormAuthoryUser.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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.Data.Logs;
  12. using SIMDP.Model;
  13. using SIMDP.BLL;
  14. using System.Data.Common;
  15. using ProjectBase.Data.BaseDAL;
  16. using ProjectBase.Util;
  17. namespace SIMDP.View
  18. {
  19. public partial class FormAuthoryUser : DevExpress.XtraEditors.XtraForm
  20. {
  21. //BlAuthoryGroup blGroup = new BlAuthoryGroup();
  22. //BlAuthoryUser blUser = new BlAuthoryUser();
  23. //BlUserRole blUserRole = new BlUserRole();
  24. string condition = null;
  25. DbTransaction trans;
  26. bool flag;
  27. //private string selectTreeNode = ""; // 选中节点
  28. private int treeLevel = 0; // 树形结构的层级-- 角色树
  29. //private int nodeType = 0; // 选中节点类型,0,代表组织,1,代表角色--角色树
  30. /// <summary>
  31. /// 性别的字典表
  32. /// </summary>
  33. private Dictionary<string, string> Dic_Sex
  34. {
  35. get
  36. {
  37. Dictionary<string, string> sex = new Dictionary<string, string>();
  38. sex.Add("-1","全部");
  39. sex.Add("1", "男");
  40. sex.Add("0", "女");
  41. return sex;
  42. }
  43. }
  44. /// <summary>
  45. /// 状态的字典表
  46. /// </summary>
  47. private Dictionary<string, string> Dic_Status
  48. {
  49. get
  50. {
  51. Dictionary<string, string> status = new Dictionary<string, string>();
  52. status.Add("-1", "全部");
  53. status.Add("0", "停用");
  54. status.Add("1", "启用");
  55. return status;
  56. }
  57. }
  58. public FormAuthoryUser()
  59. {
  60. InitializeComponent();
  61. //下拉框--性别
  62. BindingSource bsSex = new BindingSource();
  63. bsSex.DataSource = Dic_Sex;
  64. this.lookUp_Sex.Properties.DataSource = bsSex;
  65. this.lookUp_Sex.Properties.ValueMember = "Key";
  66. this.lookUp_Sex.Properties.DisplayMember = "Value";
  67. this.lookUp_Sex.EditValue = -1;
  68. //下拉框--状态
  69. BindingSource bsStatus = new BindingSource();
  70. bsStatus.DataSource = Dic_Status;
  71. this.lookUp_Status.Properties.DataSource = bsStatus;
  72. this.lookUp_Status.Properties.ValueMember = "Key";
  73. this.lookUp_Status.Properties.DisplayMember = "Value";
  74. this.lookUp_Status.EditValue = -1;
  75. InitTree_Dept();
  76. InitTree_Role();
  77. InitGrid();
  78. }
  79. /// <summary>
  80. /// 初始化树--组织
  81. /// </summary>
  82. private void InitTree_Dept()
  83. {
  84. this.tree_Dept.BeginUpdate();
  85. this.tree_Dept.Nodes.Clear();
  86. this.tree_Dept.ImageList = imageList1;
  87. List<MoAuthoryGroup> list = BLLFactory<BlAuthoryGroup>.Instance.GetAll();
  88. List<MoAuthoryGroup> rootList = new List<MoAuthoryGroup>();
  89. foreach (MoAuthoryGroup item in list)
  90. {
  91. if (item.ParentGroupId == "")
  92. {
  93. rootList.Add(item);
  94. }
  95. }
  96. foreach (MoAuthoryGroup root in rootList)
  97. {
  98. if (root != null)
  99. {
  100. TreeNode rootNode = new TreeNode(); //根节点
  101. rootNode.Text = root.GroupName;
  102. rootNode.Name = root.GroupId;
  103. rootNode.Tag = string.Format(" group_id = '{0}'", root.GroupId);
  104. rootNode.ImageIndex = 0;
  105. rootNode.SelectedImageIndex = 0;
  106. this.tree_Dept.Nodes.Add(rootNode);
  107. List<MoAuthoryGroup> subList = BLLFactory<BlAuthoryGroup>.Instance.Find(string.Format(" parent_group_id = '{0}'",root.GroupId));
  108. AddNode_Dept(subList,rootNode);
  109. }
  110. }
  111. this.tree_Dept.ExpandAll();
  112. this.tree_Dept.EndUpdate();
  113. this.tree_Dept.SelectedNode = null;
  114. }
  115. /// <summary>
  116. /// 添加树节点--组织
  117. /// </summary>
  118. /// <param name="list"></param>
  119. /// <param name="parentNode"></param>
  120. private void AddNode_Dept(List<MoAuthoryGroup> list ,TreeNode parentNode)
  121. {
  122. if (list == null)
  123. {
  124. return;
  125. }
  126. foreach (MoAuthoryGroup item in list)
  127. {
  128. TreeNode node = new TreeNode(); //根节点
  129. node.Text = item.GroupName;
  130. node.Name = item.GroupId;
  131. node.Tag = string.Format(" group_id = '{0}'",item.GroupId);
  132. string index = item.GroupId.Substring(0, 1);
  133. node.ImageIndex = Convert.ToInt32(index) - 1;
  134. node.SelectedImageIndex = Convert.ToInt32(index) - 1;
  135. parentNode.Nodes.Add(node);
  136. List<MoAuthoryGroup> subList = BLLFactory<BlAuthoryGroup>.Instance.Find(string.Format(" parent_group_id = '{0}'", item.GroupId));
  137. AddNode_Dept(subList, node);
  138. }
  139. }
  140. /// <summary>
  141. /// 初始化树--角色
  142. /// </summary>
  143. private void InitTree_Role()
  144. {
  145. this.tree_Role.BeginUpdate();
  146. this.tree_Role.Nodes.Clear();
  147. this.tree_Role.ImageList = imageList2;
  148. string condition = string.Format(" parent_group_id =''");
  149. List<MoAuthoryGroup> list = BLLFactory<BlAuthoryGroup>.Instance.Find(condition);
  150. List<MoAuthoryGroup> rootList = new List<MoAuthoryGroup>();
  151. foreach (MoAuthoryGroup item in list)
  152. {
  153. rootList.Add(item);
  154. treeLevel = 1;
  155. }
  156. foreach (MoAuthoryGroup root in rootList)
  157. {
  158. if (root != null)
  159. {
  160. TreeNode rootNode = new TreeNode(); //根节点
  161. rootNode.Text = root.GroupName;
  162. rootNode.Name = root.GroupId;
  163. rootNode.ImageIndex = 0;
  164. rootNode.SelectedImageIndex = 0;
  165. rootNode.Tag = " group_id = '" + root.GroupId + "'";
  166. AddChildRole(rootNode);
  167. if (root.GroupId.Substring(0, 1).Equals("1"))
  168. {
  169. List<MoAuthoryGroup> subList = BLLFactory<BlAuthoryGroup>.Instance.Find(string.Format(" parent_group_id = '{0}'", root.GroupId));
  170. AddChildNode(subList, rootNode);
  171. treeLevel = 2; //根节点为集团
  172. }
  173. this.tree_Role.Nodes.Add(rootNode);
  174. }
  175. }
  176. this.tree_Role.ExpandAll();
  177. this.tree_Role.EndUpdate();
  178. }
  179. /// <summary>
  180. /// 添加公司级子节点--角色树
  181. /// </summary>
  182. /// <param name="list"></param>
  183. /// <param name="parentNode"></param>
  184. private void AddChildNode(List<MoAuthoryGroup> list, TreeNode parentNode)
  185. {
  186. if (list == null)
  187. {
  188. return;
  189. }
  190. foreach (MoAuthoryGroup item in list)
  191. {
  192. TreeNode node = new TreeNode(); //公司级子节点
  193. node.Text = item.GroupName;
  194. node.Name = item.GroupId;
  195. node.ImageIndex = 1;
  196. node.SelectedImageIndex = 1;
  197. node.Tag = " group_id = '" + item.GroupId + "'";
  198. parentNode.Nodes.Add(node);
  199. AddChildRole(node);
  200. }
  201. }
  202. /// <summary>
  203. /// 添加角色节点--角色树
  204. /// </summary>
  205. /// <param name="parentNode"></param>
  206. private void AddChildRole(TreeNode parentNode)
  207. {
  208. string sql = " role_id in (SELECT b.role_id from group_role b where b.group_id = '" + parentNode.Name + "')";
  209. //BlAuthoryRole blRole = new BlAuthoryRole();
  210. List<MoAuthoryRole> roleList = new List<MoAuthoryRole>();
  211. roleList = BLLFactory<BlAuthoryRole>.Instance.Find(sql);
  212. foreach (MoAuthoryRole item in roleList)
  213. {
  214. TreeNode node = new TreeNode();
  215. node.Text = item.RoleName;
  216. node.Name = item.RoleId;
  217. node.ImageIndex = 2;
  218. node.SelectedImageIndex = 2;
  219. if (treeLevel == 1)
  220. {
  221. node.Tag = " login_account in (SELECT b.user_id from user_role b where b.role_id = '" + item.RoleId + "') AND group_id = '" + parentNode.Name + "'";
  222. }
  223. else if (treeLevel == 2)
  224. {
  225. node.Tag = " group_id = '" + parentNode.Name + "'";
  226. }
  227. parentNode.Nodes.Add(node);
  228. }
  229. }
  230. /// <summary>
  231. /// 初始化表格控件
  232. /// </summary>
  233. private void InitGrid()
  234. {
  235. //entity
  236. this.winGridViewPager1.OnPageChanged += new EventHandler(winGridViewPager1_OnPageChanged);
  237. this.winGridViewPager1.OnStartExport += new EventHandler(winGridViewPager1_OnStartExport);
  238. this.winGridViewPager1.OnEditSelected += new EventHandler(winGridViewPager1_OnEditSelected);
  239. this.winGridViewPager1.OnAddNew += new EventHandler(winGridViewPager1_OnAddNew);
  240. this.winGridViewPager1.OnDeleteSelected += new EventHandler(winGridViewPager1_OnDeleteSelected);
  241. this.winGridViewPager1.OnRefresh += new EventHandler(winGridViewPager1_OnRefresh);
  242. this.winGridViewPager1.gridView1.CustomColumnDisplayText += new DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventHandler(gridView1_CustomColumnDisplayText);
  243. //this.winGridViewPager1.AppendedMenu = this.contextMenuStrip1;
  244. this.winGridViewPager1.ShowLineNumber = true;
  245. this.winGridViewPager1.BestFitColumnWith = true;
  246. this.winGridViewPager1.DisplayColumns = "LoginAccount,UserName,UserMobile,UserEmail,UserTime,GroupId,CompanyId,UserStatus,UserSex,CardId";
  247. #region 添加别名解析
  248. this.winGridViewPager1.AddColumnAlias("LoginAccount", "登录账号");
  249. this.winGridViewPager1.AddColumnAlias("LoginPasswd", "");
  250. this.winGridViewPager1.AddColumnAlias("UserName", "用户名");
  251. this.winGridViewPager1.AddColumnAlias("UserMobile", "用户手机号");
  252. this.winGridViewPager1.AddColumnAlias("UserEmail", "用户邮箱");
  253. this.winGridViewPager1.AddColumnAlias("UserTime", "用户创建时间");
  254. this.winGridViewPager1.AddColumnAlias("GroupId", "用户组");
  255. this.winGridViewPager1.AddColumnAlias("CompanyId", "所属公司");
  256. this.winGridViewPager1.AddColumnAlias("UserStatus", "用户状态");
  257. this.winGridViewPager1.AddColumnAlias("UserSex", "用户性别");
  258. this.winGridViewPager1.AddColumnAlias("CardId", "员工号");
  259. #endregion
  260. this.winGridViewPager1.PrintTitle = "用户信息报表";
  261. this.winGridViewPager1.gridView1.Appearance.HeaderPanel.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
  262. BindData(null);
  263. }
  264. /// <summary>
  265. /// 绑定数据
  266. /// </summary>
  267. private void BindData(string condition)
  268. {
  269. if (string.IsNullOrEmpty(condition))
  270. {
  271. this.winGridViewPager1.DataSource = BLLFactory<BlAuthoryUser>.Instance.GetAll(this.winGridViewPager1.PagerInfo);
  272. for (int i = 0; i < this.winGridViewPager1.gridView1.Columns.Count; i++) //设置每列内容居中显示
  273. {
  274. this.winGridViewPager1.gridView1.Columns[i].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
  275. }
  276. return;
  277. }
  278. this.winGridViewPager1.DataSource = BLLFactory<BlAuthoryUser>.Instance.FindWithPager(condition, this.winGridViewPager1.PagerInfo);
  279. for (int i = 0; i < this.winGridViewPager1.gridView1.Columns.Count; i++) //设置每列内容居中显示
  280. {
  281. this.winGridViewPager1.gridView1.Columns[i].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
  282. }
  283. }
  284. /// <summary>
  285. /// 分页控件翻页的操作
  286. /// </summary>
  287. private void winGridViewPager1_OnPageChanged(object sender, EventArgs e)
  288. {
  289. BindData(condition);
  290. }
  291. /// <summary>
  292. /// 分页控件全部导出操作前的操作
  293. /// </summary>
  294. private void winGridViewPager1_OnStartExport(object sender, EventArgs e)
  295. {
  296. this.winGridViewPager1.AllToExport = BLLFactory<BlAuthoryUser>.Instance.Find(condition);
  297. }
  298. /// <summary>
  299. /// 分页控件编辑项操作
  300. /// </summary>
  301. private void winGridViewPager1_OnEditSelected(object sender, EventArgs e)
  302. {
  303. MoAuthoryUser selectRow = this.winGridViewPager1.gridView1.GetFocusedRow() as MoAuthoryUser;
  304. FormEditAuthoryUser editUser = new FormEditAuthoryUser(selectRow);
  305. editUser.saveData += new FormEditAuthoryUser.save(DataSaved);
  306. editUser.ShowDialog();
  307. }
  308. ///// <summary>
  309. ///// 分页控件新增操作
  310. ///// </summary>
  311. private void winGridViewPager1_OnAddNew(object sender, EventArgs e)
  312. {
  313. menuDept_Add_Click(null, null);
  314. }
  315. /// <summary>
  316. /// 分页控件删除操作
  317. /// </summary>
  318. /// <param name="sender"></param>
  319. /// <param name="e"></param>
  320. private void winGridViewPager1_OnDeleteSelected(object sender, EventArgs e)
  321. {
  322. if (DevExpress.XtraEditors.XtraMessageBox.Show("您确定删除选定的记录么?", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No)
  323. {
  324. return;
  325. }
  326. MoAuthoryUser selectRow = this.winGridViewPager1.gridView1.GetFocusedRow() as MoAuthoryUser;
  327. string id = selectRow.LoginAccount;
  328. using (trans = BLLFactory<BlAuthoryUser>.Instance.CreateTransaction())
  329. {
  330. try
  331. {
  332. flag = BLLFactory<BlAuthoryUser>.Instance.Delete(id, SysEnvironment.CurrentLoginID, trans);
  333. if (flag)
  334. {
  335. flag = BLLFactory<BlUserRole>.Instance.Delete(id, SysEnvironment.CurrentLoginID, trans);
  336. }
  337. trans.Commit();
  338. }
  339. catch (Exception ex)
  340. {
  341. trans.Rollback();
  342. DevExpress.XtraEditors.XtraMessageBox.Show("删除失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  343. LogHelper.log.Error(string.Format("删除数据库authory_user和user_role出现错误:{0}", ex));
  344. }
  345. }
  346. trans.Dispose();
  347. if (flag)
  348. {
  349. DevExpress.XtraEditors.XtraMessageBox.Show("删除成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  350. BindData(condition);
  351. }
  352. }
  353. /// <summary>
  354. /// 分页控件刷新操作
  355. /// </summary>
  356. private void winGridViewPager1_OnRefresh(object sender, EventArgs e)
  357. {
  358. BindData(condition);
  359. }
  360. void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
  361. {
  362. if (e.Column.ColumnType == typeof(DateTime))
  363. {
  364. string columnName = e.Column.FieldName;
  365. if (e.Value != null)
  366. {
  367. if (Convert.ToDateTime(e.Value) <= Convert.ToDateTime("1900-1-1"))
  368. {
  369. e.DisplayText = "";
  370. }
  371. else
  372. {
  373. e.DisplayText = Convert.ToDateTime(e.Value).ToString("yyyy-MM-dd HH:mm");//yyyy-MM-dd
  374. }
  375. }
  376. }
  377. if (e.Column.FieldName == "UserSex")
  378. {
  379. e.DisplayText = Dic_Sex[(e.Value).ToString()];
  380. }
  381. if (e.Column.FieldName == "UserStatus")
  382. {
  383. e.DisplayText = Dic_Status[(e.Value).ToString()];
  384. }
  385. }
  386. private void tree_Dept_AfterSelect(object sender, TreeViewEventArgs e)
  387. {
  388. if (e.Node != null && e.Node.Tag != null)
  389. {
  390. condition = e.Node.Tag.ToString();
  391. BindData(condition);
  392. }
  393. else
  394. {
  395. condition = "";
  396. BindData(condition);
  397. }
  398. }
  399. private void menuDept_Add_Click(object sender, EventArgs e)
  400. {
  401. FormEditAuthoryUser editUser = new FormEditAuthoryUser(null);
  402. editUser.saveData += new FormEditAuthoryUser.save(DataSaved);
  403. editUser.ShowDialog();
  404. }
  405. private void menuDept_Expand_Click(object sender, EventArgs e)
  406. {
  407. this.tree_Dept.ExpandAll();
  408. }
  409. private void menuDept_Collapse_Click(object sender, EventArgs e)
  410. {
  411. this.tree_Dept.CollapseAll();
  412. }
  413. private void menuDept_Refresh_Click(object sender, EventArgs e)
  414. {
  415. InitTree_Dept();
  416. }
  417. private void menuRole_Add_Click(object sender, EventArgs e)
  418. {
  419. menuDept_Add_Click(null,null);
  420. }
  421. private void menuRole_Expand_Click(object sender, EventArgs e)
  422. {
  423. this.tree_Role.ExpandAll();
  424. }
  425. private void menuRole_Collapse_Click(object sender, EventArgs e)
  426. {
  427. this.tree_Role.CollapseAll();
  428. }
  429. private void menuRole_Refresh_Click(object sender, EventArgs e)
  430. {
  431. InitTree_Role();
  432. }
  433. private void btn_Query_Click(object sender, EventArgs e)
  434. {
  435. if (string.IsNullOrEmpty(txt_Account.Text) && string.IsNullOrEmpty(txt_CardId.Text) && string.IsNullOrEmpty(txt_Email.Text) && string.IsNullOrEmpty(txt_Mobile.Text)
  436. && string.IsNullOrEmpty(txt_UserName.Text) && lookUp_Sex.Text == "全部" && lookUp_Status.Text == "全部")
  437. {
  438. BindData(null);
  439. condition = null;
  440. return;
  441. }
  442. string sql = " 1=1";
  443. if (!string.IsNullOrEmpty(txt_Account.Text))
  444. {
  445. sql += string.Format(" AND login_account = '{0}'", txt_Account.Text);
  446. }
  447. if (!string.IsNullOrEmpty(txt_CardId.Text))
  448. {
  449. sql += string.Format(" AND card_id = '{0}'", txt_CardId.Text);
  450. }
  451. if (!string.IsNullOrEmpty(txt_Email.Text))
  452. {
  453. sql += string.Format(" AND user_email = '{0}'", txt_Email.Text);
  454. }
  455. if (!string.IsNullOrEmpty(txt_Mobile.Text))
  456. {
  457. sql += string.Format(" AND user_mobile = '{0}'", txt_Mobile.Text);
  458. }
  459. if (!string.IsNullOrEmpty(txt_UserName.Text))
  460. {
  461. sql += string.Format(" AND user_name = '{0}'", txt_UserName.Text);
  462. }
  463. if (lookUp_Sex.Text != "全部")
  464. {
  465. sql += string.Format(" AND user__sex = {0}", lookUp_Sex.EditValue);
  466. }
  467. if (lookUp_Status.Text != "全部")
  468. {
  469. sql += string.Format(" AND user_status = {0}", lookUp_Status.EditValue);
  470. }
  471. BindData(sql);
  472. condition = sql;
  473. }
  474. /// <summary>
  475. /// 保存数据成功后调用
  476. /// </summary>
  477. private void DataSaved()
  478. {
  479. InitTree_Dept();
  480. InitTree_Role();
  481. BindData(condition);
  482. }
  483. private void tree_Role_AfterSelect(object sender, TreeViewEventArgs e)
  484. {
  485. //if (e.Node.Level == 0)
  486. //{
  487. // if (treeLevel == 2)
  488. // return;
  489. // else
  490. // {
  491. // string sql = " group_id = '" + e.Node.Name + "'";
  492. // BindData(sql);
  493. // condition = sql;
  494. // }
  495. //}
  496. //else if (e.Node.Level == 1)
  497. //{
  498. // if (treeLevel == 2)
  499. // {
  500. // string sql = " group_id = '" + e.Node.Name + "'";
  501. // BindData(sql);
  502. // condition = sql;
  503. // }
  504. // else
  505. // {
  506. // string sql = " login_account in (SELECT b.user_id from user_role b where b.role_id = '" + e.Node.Name + "') AND group_id = '"+ e.Node.Parent.Name +"'";
  507. // BindData(sql);
  508. // condition = sql;
  509. // }
  510. //}
  511. //else
  512. //{
  513. // string sql = " login_account in (SELECT b.user_id from user_role b where b.role_id = '" + e.Node.Name + "') AND group_id = '" + e.Node.Parent.Name + "'";
  514. // BindData(sql);
  515. // condition = sql;
  516. //}
  517. if (e.Node != null && e.Node.Tag != null)
  518. {
  519. condition = e.Node.Tag.ToString();
  520. BindData(condition);
  521. }
  522. else
  523. {
  524. condition = "";
  525. BindData(condition);
  526. }
  527. }
  528. }
  529. }