FormEditAuthoryUser.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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 SIMDP.Model;
  12. using SIMDP.BLL;
  13. using DevExpress.XtraTreeList.Nodes;
  14. using ProjectBase.Data.Logs;
  15. using System.Data.Common;
  16. using ProjectBase.Data.Encrypt;
  17. using ProjectBase.Data.BaseDAL;
  18. using SIMDP.Util;
  19. namespace SIMDP
  20. {
  21. public partial class FormEditAuthoryUser : DevExpress.XtraEditors.XtraForm
  22. {
  23. //BlAuthoryGroup blGroup = new BlAuthoryGroup();
  24. //BlAuthoryRole blRole = new BlAuthoryRole();
  25. //BlUserRole blUserRole = new BlUserRole();
  26. //BlAuthoryUser blUser = new BlAuthoryUser();
  27. List<MoAuthoryRole> roleList = new List<MoAuthoryRole>();
  28. DbTransaction trans;
  29. bool flag;
  30. private MoAuthoryUser authoryUser = new MoAuthoryUser();
  31. private List<string> listCheckedID = new List<string>();//用户角色控件选择局ID集合
  32. private List<string> listCheckedName = new List<string>();//用户角色控件选择局Name集合
  33. private string displayText = null; //用户角色控件显示的文本
  34. /// <summary>
  35. /// 定义一个委托
  36. /// </summary>
  37. public delegate void save();
  38. /// <summary>
  39. /// 定义一个事件,数据添加和修改成功后刷新
  40. /// </summary>
  41. public event save saveData;
  42. /// <summary>
  43. /// 性别的字典表
  44. /// </summary>
  45. private Dictionary<string, string> Dic_Sex
  46. {
  47. get
  48. {
  49. Dictionary<string, string> sex = new Dictionary<string, string>();
  50. sex.Add("1", "男");
  51. sex.Add("0", "女");
  52. return sex;
  53. }
  54. }
  55. /// <summary>
  56. /// 状态的字典表
  57. /// </summary>
  58. private Dictionary<string, string> Dic_Status
  59. {
  60. get
  61. {
  62. Dictionary<string, string> status = new Dictionary<string, string>();
  63. status.Add("0", "停用");
  64. status.Add("1", "启用");
  65. return status;
  66. }
  67. }
  68. /// <summary>
  69. /// 构造函数
  70. /// </summary>
  71. /// <param name="moAuthory">为null时表示添加操作,不为null时表示修改操作</param>
  72. public FormEditAuthoryUser(MoAuthoryUser moAuthory)
  73. {
  74. InitializeComponent();
  75. authoryUser = moAuthory;
  76. InitControls(moAuthory);
  77. }
  78. private void InitControls(MoAuthoryUser moAuthory)
  79. {
  80. //文本框--密码
  81. //this.txt_PassWord.Properties.NullValuePromptShowForEmptyValue = true;
  82. //this.txt_PassWord.Properties.NullValuePrompt = "请输入密码";
  83. this.txt_PassWord.Enabled = false;
  84. //下拉框--性别
  85. BindingSource bsSex = new BindingSource();
  86. bsSex.DataSource = Dic_Sex;
  87. this.lookUp_Sex.Properties.DataSource = bsSex;
  88. this.lookUp_Sex.Properties.ValueMember = "Key";
  89. this.lookUp_Sex.Properties.DisplayMember = "Value";
  90. //下拉框--状态
  91. BindingSource bsStatus = new BindingSource();
  92. bsStatus.DataSource = Dic_Status;
  93. this.lookUp_Status.Properties.DataSource = bsStatus;
  94. this.lookUp_Status.Properties.ValueMember = "Key";
  95. this.lookUp_Status.Properties.DisplayMember = "Value";
  96. #region 初始化用户组控件
  97. DataTable dt = new DataTable();
  98. dt.Columns.Add("ImageIndex", typeof(int));
  99. dt.Columns.Add("GroupID", typeof(string));
  100. dt.Columns.Add("GroupPID", typeof(string));
  101. dt.Columns.Add("GroupName", typeof(string));
  102. List<MoAuthoryGroup> list = BLLFactory<BlAuthoryGroup>.Instance.GetAll();
  103. DataRow dr = null;
  104. foreach (MoAuthoryGroup info in list)
  105. {
  106. if (info.GroupId.Substring(0, 1).Equals("1") || info.GroupId.Substring(0, 1).Equals("2"))
  107. {
  108. dr = dt.NewRow();
  109. dr["ImageIndex"] = 0;
  110. dr["GroupID"] = info.GroupId;
  111. dr["GroupPID"] = info.ParentGroupId;
  112. dr["GroupName"] = info.GroupName;
  113. dt.Rows.Add(dr);
  114. }
  115. }
  116. //设置图形序号
  117. //this.treeListLookUpEdit1TreeList.SelectImageList = this.imageList2;
  118. //this.treeListLookUpEdit1TreeList.StateImageList = this.imageList2;
  119. this.treeLookUp_Group.Properties.TreeList.KeyFieldName = "GroupID";
  120. this.treeLookUp_Group.Properties.TreeList.ParentFieldName = "GroupPID";
  121. this.treeLookUp_Group.Properties.DataSource = dt;
  122. this.treeLookUp_Group.Properties.ValueMember = "GroupID";
  123. this.treeLookUp_Group.Properties.DisplayMember = "GroupName";
  124. this.treeLookUp_Group.Properties.NullValuePrompt = "请选择公司";
  125. this.treeLookUp_Group.Properties.NullValuePromptShowForEmptyValue = true;
  126. this.treeLookUp_Group.EditValueChanged += new EventHandler(treeLookUp_Group_EditValueChanged);
  127. #endregion
  128. if (moAuthory == null)
  129. {
  130. this.btn_Save.Enabled = false;
  131. this.lookUp_Sex.Properties.NullText = "请您选择";
  132. this.lookUp_Status.Properties.NullText = "请您选择";
  133. this.treeLookUp_Group.EditValue = "0";
  134. this.treeLookUp_Role.EditValue = "0";
  135. this.treeLookUp_Part.EditValue = "0";
  136. this.txt_PassWord.Text = "000000";
  137. return;
  138. }
  139. this.btn_Add.Enabled = false;
  140. this.lookUp_Sex.EditValue = moAuthory.UserSex;
  141. this.lookUp_Status.EditValue = moAuthory.UserStatus;
  142. if (moAuthory.GroupId.Substring(0,1).Equals("1") || moAuthory.GroupId.Substring(0, 1).Equals("2"))
  143. {
  144. this.treeLookUp_Group.EditValue = moAuthory.GroupId;
  145. }
  146. else
  147. {
  148. MoAuthoryGroup group = new MoAuthoryGroup();
  149. group = BLLFactory<BlAuthoryGroup>.Instance.FindByID(moAuthory.GroupId);
  150. this.treeLookUp_Group.EditValue = group.ParentGroupId;
  151. this.treeLookUp_Part.EditValue = moAuthory.GroupId;
  152. }
  153. this.txt_Account.Text = moAuthory.LoginAccount;
  154. this.txt_CardId.Text = moAuthory.CardId;
  155. this.txt_Email.Text = moAuthory.UserEmail;
  156. this.txt_Mobile.Text = moAuthory.UserMobile;
  157. this.txt_Name.Text = moAuthory.UserName;
  158. this.txt_PassWord.Text = EncodeHelper.DecryptString(moAuthory.LoginPasswd, true);
  159. List<MoUserRole> userRoleList = BLLFactory<BlUserRole>.Instance.Find(string.Format(" user_id = '{0}'", txt_Account.Text));
  160. DefaultChecked(userRoleList);
  161. }
  162. /// <summary>
  163. /// 用户角色控件复选框默认选中情况
  164. /// </summary>
  165. /// <param name="rid"></param>
  166. private void DefaultChecked(List<MoUserRole> role)
  167. {
  168. this.listCheckedID.Clear();
  169. this.listCheckedName.Clear();
  170. foreach (MoUserRole item in role)
  171. {
  172. listCheckedID.Add(item.RoleId);
  173. foreach (MoAuthoryRole mo in roleList)
  174. {
  175. if (mo.RoleId == item.RoleId)
  176. {
  177. listCheckedName.Add(mo.RoleName);
  178. }
  179. }
  180. }
  181. if (this.treeLookUp_Role.Properties.TreeList.Nodes.Count > 0)
  182. {
  183. foreach (TreeListNode nd in treeLookUp_Role.Properties.TreeList.Nodes)
  184. {
  185. for (int i = 0; i < listCheckedID.Count; i++)
  186. {
  187. string checkedkeyid = listCheckedID[i];
  188. if (treeLookUp_Role.Properties.TreeList.FindNodeByKeyID(checkedkeyid) != null)
  189. {
  190. treeLookUp_Role.Properties.TreeList.FindNodeByKeyID(checkedkeyid).Checked = true;
  191. }
  192. }
  193. }
  194. }
  195. displayText = null;
  196. foreach (string name in listCheckedName)
  197. {
  198. displayText += name + ",";
  199. }
  200. this.treeLookUp_Role.Text = displayText;
  201. }
  202. /// <summary>
  203. /// 获取用户角色控件复选框已经勾选的ID和Name集合
  204. /// </summary>
  205. private void GetSelectedRoleIDandName()
  206. {
  207. this.listCheckedID.Clear();
  208. this.listCheckedName.Clear();
  209. if (this.treeLookUp_Role.Properties.TreeList.Nodes.Count > 0)
  210. {
  211. foreach (TreeListNode root in this.treeLookUp_Role.Properties.TreeList.Nodes)
  212. {
  213. GetCheckedKeyID(root);
  214. }
  215. }
  216. displayText = null;
  217. foreach (string name in listCheckedName)
  218. {
  219. displayText += name + ",";
  220. }
  221. }
  222. /// <summary>
  223. /// 获取用户角色控件复选框已经勾选的ID和Name集合
  224. /// </summary>
  225. /// <param name="parentNode">父级节点</param>
  226. private void GetCheckedKeyID(TreeListNode parentNode)
  227. {
  228. //if (parentNode.Nodes.Count == 0) //本项目只有根节点,此部分不需要
  229. //{
  230. // return;//递归终止
  231. //}
  232. if (parentNode.CheckState != CheckState.Unchecked)
  233. {
  234. DataRowView drv = this.treeLookUp_Role.Properties.TreeList.GetDataRecordByNode(parentNode) as DataRowView;
  235. if (drv != null)
  236. {
  237. string KeyFieldName = drv["RoleId"].ToString();
  238. string DisplayMember = drv["RoleName"].ToString();
  239. if (!listCheckedID.Contains(KeyFieldName))
  240. {
  241. listCheckedID.Add(KeyFieldName);
  242. }
  243. if (!listCheckedName.Contains(DisplayMember))
  244. {
  245. listCheckedName.Add(DisplayMember);
  246. }
  247. }
  248. }
  249. //foreach (TreeListNode node in parentNode.Nodes) //本项目只有根节点,此部分不需要
  250. //{
  251. // if (node.CheckState != CheckState.Unchecked)
  252. // {
  253. // DataRowView drv = this.treeLookUp_Role.Properties.TreeList.GetDataRecordByNode(node) as DataRowView;//关键代码,就是不知道是这样获取数据而纠结了很久(鬼知道可以转换为DataRowView啊)
  254. // if (drv != null)
  255. // {
  256. // string KeyFieldName = drv["RoleId"].ToString();
  257. // string DisplayMember = drv["RoleName"].ToString();
  258. // listCheckedID.Add(KeyFieldName);
  259. // listCheckedName.Add(DisplayMember);
  260. // }
  261. // }
  262. // GetCheckedKeyID(node);
  263. //}
  264. }
  265. private void btn_Cancel_Click(object sender, EventArgs e)
  266. {
  267. this.Close();
  268. }
  269. private void treeLookUp_Role_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
  270. {
  271. this.treeLookUp_Role.Properties.NullText = displayText;
  272. }
  273. private void treeLookUp_Role_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
  274. {
  275. e.DisplayText = displayText;
  276. }
  277. private void btn_Add_Click(object sender, EventArgs e)
  278. {
  279. if (string.IsNullOrEmpty(txt_Account.Text))
  280. {
  281. DevExpress.XtraEditors.XtraMessageBox.Show("请输入登录账号", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  282. return;
  283. }
  284. if (string.IsNullOrEmpty(txt_Name.Text))
  285. {
  286. DevExpress.XtraEditors.XtraMessageBox.Show("请输入用户名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  287. return;
  288. }
  289. if (string.IsNullOrEmpty(txt_PassWord.Text))
  290. {
  291. DevExpress.XtraEditors.XtraMessageBox.Show("请输入登录密码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  292. return;
  293. }
  294. //if (txt_PassWord.Text.Trim().Length != 6)
  295. //{
  296. // DevExpress.XtraEditors.XtraMessageBox.Show("请输入六位登录密码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  297. // return;
  298. //}
  299. if (string.IsNullOrEmpty(treeLookUp_Group.Text))
  300. {
  301. DevExpress.XtraEditors.XtraMessageBox.Show("请输入所属公司", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  302. return;
  303. }
  304. if (treeLookUp_Group.Text == treeLookUp_Part.Text)
  305. {
  306. DevExpress.XtraEditors.XtraMessageBox.Show("请正确选择公司和部门", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  307. return;
  308. }
  309. if (string.IsNullOrEmpty(txt_Mobile.Text))
  310. {
  311. DevExpress.XtraEditors.XtraMessageBox.Show("请输入手机号", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  312. return;
  313. }
  314. if (string.IsNullOrEmpty(txt_Email.Text))
  315. {
  316. DevExpress.XtraEditors.XtraMessageBox.Show("请输入邮箱", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  317. return;
  318. }
  319. if (string.IsNullOrEmpty(treeLookUp_Role.Text))
  320. {
  321. DevExpress.XtraEditors.XtraMessageBox.Show("请输入角色", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  322. return;
  323. }
  324. if (lookUp_Status.Text == "请您选择")
  325. {
  326. DevExpress.XtraEditors.XtraMessageBox.Show("请输入状态", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  327. return;
  328. }
  329. MoAuthoryUser user = new MoAuthoryUser();
  330. user.LoginAccount = txt_Account.Text;
  331. user.LoginPasswd = EncodeHelper.EncryptString(txt_PassWord.Text);
  332. user.UserName = txt_Name.Text;
  333. user.UserMobile = txt_Mobile.Text;
  334. user.UserEmail = txt_Email.Text;
  335. user.UserTime = DateTime.Now;
  336. if (string.IsNullOrEmpty(this.treeLookUp_Part.Text) || this.treeLookUp_Part.Text == "无")
  337. {
  338. user.GroupId = treeLookUp_Group.EditValue.ToString();
  339. }
  340. else
  341. {
  342. user.GroupId = treeLookUp_Part.EditValue.ToString();
  343. }
  344. user.UserStatus = Convert.ToInt32(lookUp_Status.EditValue);
  345. if (lookUp_Sex.Text != "请您选择")
  346. {
  347. user.UserSex = Convert.ToInt32(lookUp_Sex.EditValue);
  348. }
  349. //user.UserSex = Convert.ToInt32(lookUp_Status.EditValue);
  350. user.CardId = txt_CardId.Text;
  351. List<MoUserRole> insert = new List<MoUserRole>();
  352. foreach (string item in listCheckedID)
  353. {
  354. insert.Add(new MoUserRole { UserId = txt_Account.Text, RoleId = item });
  355. }
  356. using (trans = BLLFactory<BlAuthoryUser>.Instance.CreateTransaction())
  357. {
  358. try
  359. {
  360. flag = BLLFactory<BlAuthoryUser>.Instance.Insert(user, trans);
  361. foreach (MoUserRole item in insert)
  362. {
  363. if (flag)
  364. {
  365. flag = BLLFactory<BlUserRole>.Instance.Insert(item, trans);
  366. }
  367. }
  368. trans.Commit();
  369. }
  370. catch (Exception ex)
  371. {
  372. trans.Rollback();
  373. DevExpress.XtraEditors.XtraMessageBox.Show("添加失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  374. LogHelper.log.Error(string.Format("添加数据库authory_user和user_role出现错误:{0}", ex));
  375. }
  376. }
  377. trans.Dispose();
  378. if (flag)
  379. {
  380. DevExpress.XtraEditors.XtraMessageBox.Show("添加成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  381. saveData();
  382. this.Close();
  383. }
  384. }
  385. private void btn_Save_Click(object sender, EventArgs e)
  386. {
  387. if (string.IsNullOrEmpty(txt_Account.Text))
  388. {
  389. DevExpress.XtraEditors.XtraMessageBox.Show("请输入登录账号", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  390. return;
  391. }
  392. if (string.IsNullOrEmpty(txt_Name.Text))
  393. {
  394. DevExpress.XtraEditors.XtraMessageBox.Show("请输入用户名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  395. return;
  396. }
  397. if (string.IsNullOrEmpty(txt_PassWord.Text))
  398. {
  399. DevExpress.XtraEditors.XtraMessageBox.Show("请输入登录密码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  400. return;
  401. }
  402. if (txt_PassWord.Text.Trim().Length != 6)
  403. {
  404. DevExpress.XtraEditors.XtraMessageBox.Show("请输入六位登录密码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  405. return;
  406. }
  407. if (string.IsNullOrEmpty(treeLookUp_Group.Text))
  408. {
  409. DevExpress.XtraEditors.XtraMessageBox.Show("请输入所属公司", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  410. return;
  411. }
  412. if (treeLookUp_Group.Text == treeLookUp_Part.Text)
  413. {
  414. DevExpress.XtraEditors.XtraMessageBox.Show("请正确选择公司和部门", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  415. return;
  416. }
  417. if (string.IsNullOrEmpty(txt_Mobile.Text))
  418. {
  419. DevExpress.XtraEditors.XtraMessageBox.Show("请输入手机号", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  420. return;
  421. }
  422. if (string.IsNullOrEmpty(txt_Email.Text))
  423. {
  424. DevExpress.XtraEditors.XtraMessageBox.Show("请输入邮箱", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  425. return;
  426. }
  427. if (string.IsNullOrEmpty(treeLookUp_Role.Text))
  428. {
  429. DevExpress.XtraEditors.XtraMessageBox.Show("请输入角色", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  430. return;
  431. }
  432. if (lookUp_Status.Text == "请您选择")
  433. {
  434. DevExpress.XtraEditors.XtraMessageBox.Show("请输入状态", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  435. return;
  436. }
  437. authoryUser.LoginAccount = txt_Account.Text;
  438. authoryUser.LoginPasswd = EncodeHelper.EncryptString(txt_PassWord.Text);
  439. authoryUser.UserName = txt_Name.Text;
  440. authoryUser.UserMobile = txt_Mobile.Text;
  441. authoryUser.UserEmail = txt_Email.Text;
  442. if (string.IsNullOrEmpty(this.treeLookUp_Part.Text) || this.treeLookUp_Part.Text == "无")
  443. {
  444. authoryUser.GroupId = treeLookUp_Group.EditValue.ToString();
  445. }
  446. else
  447. {
  448. authoryUser.GroupId = treeLookUp_Part.EditValue.ToString();
  449. }
  450. authoryUser.UserStatus = Convert.ToInt32(lookUp_Status.EditValue) ;
  451. authoryUser.UserSex = Convert.ToInt32(lookUp_Sex.EditValue);
  452. authoryUser.CardId = txt_CardId.Text;
  453. List<MoUserRole> upDate = new List<MoUserRole>();
  454. foreach (string item in listCheckedID)
  455. {
  456. upDate.Add(new MoUserRole { UserId = txt_Account.Text, RoleId = item });
  457. }
  458. using (trans = BLLFactory<BlAuthoryUser>.Instance.CreateTransaction())
  459. {
  460. try
  461. {
  462. flag = BLLFactory<BlAuthoryUser>.Instance.Update(authoryUser, authoryUser.LoginAccount, trans);
  463. if (flag)
  464. {
  465. flag = BLLFactory<BlUserRole>.Instance.Delete(authoryUser.LoginAccount, SysEnvironment.CurrentLoginID, trans);
  466. }
  467. foreach (MoUserRole item in upDate)
  468. {
  469. if (flag)
  470. {
  471. flag = BLLFactory<BlUserRole>.Instance.Insert(item,trans);
  472. }
  473. }
  474. trans.Commit();
  475. }
  476. catch (Exception ex)
  477. {
  478. trans.Rollback();
  479. DevExpress.XtraEditors.XtraMessageBox.Show("修改失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  480. LogHelper.log.Error(string.Format("修改数据库authory_user和user_role出现错误:{0}", ex));
  481. }
  482. }
  483. trans.Dispose();
  484. if (flag)
  485. {
  486. DevExpress.XtraEditors.XtraMessageBox.Show("修改成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  487. saveData();
  488. this.Close();
  489. }
  490. }
  491. private void treeLookUp_Group_EditValueChanged(object sender, EventArgs e)
  492. {
  493. if (!string.IsNullOrEmpty(this.treeLookUp_Group.EditValue.ToString()))
  494. {
  495. if (this.treeLookUp_Group.EditValue.ToString() == "1000" || this.treeLookUp_Group.EditValue.ToString() == "0")
  496. {
  497. InitTreePart(null);
  498. InitTreeRole(null);
  499. return;
  500. }
  501. InitTreePart(this.treeLookUp_Group.EditValue.ToString());
  502. InitTreeRole(this.treeLookUp_Group.EditValue.ToString());
  503. }
  504. }
  505. /// <summary>
  506. /// 部门控件
  507. /// </summary>
  508. /// <param name="id">所选的组织id</param>
  509. private void InitTreePart(string id)
  510. {
  511. DataTable dt = new DataTable();
  512. dt.Columns.Add("ImageIndex", typeof(int));
  513. dt.Columns.Add("GroupID", typeof(string));
  514. dt.Columns.Add("GroupPID", typeof(string));
  515. dt.Columns.Add("GroupName", typeof(string));
  516. List<MoAuthoryGroup> list = new List<MoAuthoryGroup>();
  517. if (id == null)
  518. {
  519. list = BLLFactory<BlAuthoryGroup>.Instance.GetAll();
  520. }
  521. else
  522. {
  523. list = BLLFactory<BlAuthoryGroup>.Instance.Find(string.Format(" group_id = '{0}' OR parent_group_id = '{1}'",id,id));
  524. }
  525. DataRow dr = null;
  526. foreach (MoAuthoryGroup info in list)
  527. {
  528. dr = dt.NewRow();
  529. dr["ImageIndex"] = 0;
  530. dr["GroupID"] = info.GroupId;
  531. dr["GroupPID"] = info.ParentGroupId;
  532. dr["GroupName"] = info.GroupName;
  533. dt.Rows.Add(dr);
  534. }
  535. //增加一行空的
  536. dr = dt.NewRow();
  537. dr["GroupID"] = "0"; //使用0代替-1,避免出现节点的嵌套显示,因为-1已经作为了一般节点的顶级标识
  538. dr["GroupPID"] = "-1";
  539. dr["GroupName"] = "无";
  540. dt.Rows.InsertAt(dr, 0);
  541. //设置图形序号
  542. //this.treeListLookUpEdit1TreeList.SelectImageList = this.imageList2;
  543. //this.treeListLookUpEdit1TreeList.StateImageList = this.imageList2;
  544. this.treeLookUp_Part.Properties.TreeList.KeyFieldName = "GroupID";
  545. this.treeLookUp_Part.Properties.TreeList.ParentFieldName = "GroupPID";
  546. this.treeLookUp_Part.Properties.DataSource = dt;
  547. this.treeLookUp_Part.Properties.ValueMember = "GroupID";
  548. this.treeLookUp_Part.Properties.DisplayMember = "GroupName";
  549. this.treeLookUp_Part.Text = "";
  550. this.treeLookUp_Part.Properties.NullText = "";
  551. this.treeLookUp_Part.Properties.NullValuePrompt = "请选择部门";
  552. this.treeLookUp_Part.Properties.NullValuePromptShowForEmptyValue = true;
  553. }
  554. /// <summary>
  555. /// 角色控件
  556. /// </summary>
  557. /// <param name="id">所选的组织id</param>
  558. private void InitTreeRole(string id)
  559. {
  560. DataTable data = new DataTable();
  561. data.Columns.Add("ImageIndex", typeof(int));
  562. data.Columns.Add("RoleId", typeof(string));
  563. data.Columns.Add("ParentRoleId", typeof(string));
  564. data.Columns.Add("RoleName", typeof(string));
  565. if (id == null)
  566. {
  567. roleList = BLLFactory<BlAuthoryRole>.Instance.GetAll();
  568. }
  569. else
  570. {
  571. string sql = " role_id in (SELECT b.role_id from group_role b where group_id = '"+ id +"')";
  572. roleList = BLLFactory<BlAuthoryRole>.Instance.Find(sql);
  573. }
  574. DataRow dataRow = null;
  575. foreach (MoAuthoryRole info in roleList)
  576. {
  577. dataRow = data.NewRow();
  578. dataRow["ImageIndex"] = 0;
  579. dataRow["RoleId"] = info.RoleId;
  580. //dataRow["ParentRoleId"] = null;
  581. dataRow["RoleName"] = info.RoleName;
  582. data.Rows.Add(dataRow);
  583. }
  584. //设置图形序号
  585. //this.treeListLookUpEdit1TreeList.SelectImageList = this.imageList2;
  586. //this.treeListLookUpEdit1TreeList.StateImageList = this.imageList2;
  587. this.treeLookUp_Role.Properties.TreeList.OptionsView.ShowCheckBoxes = true; //设置带有复选框
  588. //this.treeLookUp_Role.Properties.TreeList.OptionsBehavior.AllowIndeterminateCheckState = true; //设置复选框有三个状态
  589. this.treeLookUp_Role.Properties.TreeList.KeyFieldName = "RoleId";
  590. //this.treeLookUp_Role.Properties.TreeList.ParentFieldName = "ParentRoleId";
  591. this.treeLookUp_Role.Properties.DataSource = data;
  592. this.treeLookUp_Role.Properties.ValueMember = "RoleId";
  593. this.treeLookUp_Role.Properties.DisplayMember = "RoleName";
  594. this.treeLookUp_Role.Text = "";
  595. this.treeLookUp_Part.Properties.NullText = "";
  596. this.treeLookUp_Role.Properties.NullValuePrompt = "请选择角色";
  597. this.treeLookUp_Role.Properties.NullValuePromptShowForEmptyValue = true;
  598. displayText = null;
  599. this.treeLookUp_Role.Properties.TreeList.AfterCheckNode += (s, a) =>
  600. {
  601. a.Node.Selected = true;
  602. // txtRole.RefreshEditValue();
  603. // txtRole.ForceClosePopup();
  604. GetSelectedRoleIDandName();
  605. };
  606. }
  607. }
  608. }