using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; using DevExpress.XtraEditors; using SIMDP.Model; using SIMDP.BLL; using DevExpress.XtraTreeList.Nodes; using ProjectBase.Data.Logs; using System.Data.Common; using ProjectBase.Data.Encrypt; using ProjectBase.Data.BaseDAL; using ProjectBase.Util; namespace SIMDP.View { public partial class FormEditAuthoryUser : DevExpress.XtraEditors.XtraForm { //BlAuthoryGroup blGroup = new BlAuthoryGroup(); //BlAuthoryRole blRole = new BlAuthoryRole(); //BlUserRole blUserRole = new BlUserRole(); //BlAuthoryUser blUser = new BlAuthoryUser(); List roleList = new List(); DbTransaction trans; bool flag; private MoAuthoryUser authoryUser = new MoAuthoryUser(); private List listCheckedID = new List();//用户角色控件选择局ID集合 private List listCheckedName = new List();//用户角色控件选择局Name集合 private string displayText = null; //用户角色控件显示的文本 /// /// 定义一个委托 /// public delegate void save(); /// /// 定义一个事件,数据添加和修改成功后刷新 /// public event save saveData; /// /// 性别的字典表 /// private Dictionary Dic_Sex { get { Dictionary sex = new Dictionary(); sex.Add("1", "男"); sex.Add("0", "女"); return sex; } } /// /// 状态的字典表 /// private Dictionary Dic_Status { get { Dictionary status = new Dictionary(); status.Add("0", "停用"); status.Add("1", "启用"); return status; } } /// /// 构造函数 /// /// 为null时表示添加操作,不为null时表示修改操作 public FormEditAuthoryUser(MoAuthoryUser moAuthory) { InitializeComponent(); authoryUser = moAuthory; InitControls(moAuthory); } private void InitControls(MoAuthoryUser moAuthory) { //文本框--密码 //this.txt_PassWord.Properties.NullValuePromptShowForEmptyValue = true; //this.txt_PassWord.Properties.NullValuePrompt = "请输入密码"; this.txt_PassWord.Enabled = false; //下拉框--性别 BindingSource bsSex = new BindingSource(); bsSex.DataSource = Dic_Sex; this.lookUp_Sex.Properties.DataSource = bsSex; this.lookUp_Sex.Properties.ValueMember = "Key"; this.lookUp_Sex.Properties.DisplayMember = "Value"; //下拉框--状态 BindingSource bsStatus = new BindingSource(); bsStatus.DataSource = Dic_Status; this.lookUp_Status.Properties.DataSource = bsStatus; this.lookUp_Status.Properties.ValueMember = "Key"; this.lookUp_Status.Properties.DisplayMember = "Value"; #region 初始化用户组控件 DataTable dt = new DataTable(); dt.Columns.Add("ImageIndex", typeof(int)); dt.Columns.Add("GroupID", typeof(string)); dt.Columns.Add("GroupPID", typeof(string)); dt.Columns.Add("GroupName", typeof(string)); List list = BLLFactory.Instance.GetAll(); DataRow dr = null; foreach (MoAuthoryGroup info in list) { if (info.GroupId.Substring(0, 1).Equals("1") || info.GroupId.Substring(0, 1).Equals("2")) { dr = dt.NewRow(); dr["ImageIndex"] = 0; dr["GroupID"] = info.GroupId; dr["GroupPID"] = info.ParentGroupId; dr["GroupName"] = info.GroupName; dt.Rows.Add(dr); } } //设置图形序号 //this.treeListLookUpEdit1TreeList.SelectImageList = this.imageList2; //this.treeListLookUpEdit1TreeList.StateImageList = this.imageList2; this.treeLookUp_Group.Properties.TreeList.KeyFieldName = "GroupID"; this.treeLookUp_Group.Properties.TreeList.ParentFieldName = "GroupPID"; this.treeLookUp_Group.Properties.DataSource = dt; this.treeLookUp_Group.Properties.ValueMember = "GroupID"; this.treeLookUp_Group.Properties.DisplayMember = "GroupName"; this.treeLookUp_Group.Properties.NullValuePrompt = "请选择公司"; this.treeLookUp_Group.Properties.NullValuePromptShowForEmptyValue = true; this.treeLookUp_Group.EditValueChanged += new EventHandler(treeLookUp_Group_EditValueChanged); #endregion if (moAuthory == null) { this.btn_Save.Enabled = false; this.lookUp_Sex.Properties.NullText = "请您选择"; this.lookUp_Status.Properties.NullText = "请您选择"; this.treeLookUp_Group.EditValue = "0"; this.treeLookUp_Role.EditValue = "0"; this.treeLookUp_Part.EditValue = "0"; this.txt_PassWord.Text = "000000"; return; } this.btn_Add.Enabled = false; this.lookUp_Sex.EditValue = moAuthory.UserSex; this.lookUp_Status.EditValue = moAuthory.UserStatus; if (moAuthory.GroupId.Substring(0,1).Equals("1") || moAuthory.GroupId.Substring(0, 1).Equals("2")) { this.treeLookUp_Group.EditValue = moAuthory.GroupId; } else { MoAuthoryGroup group = new MoAuthoryGroup(); group = BLLFactory.Instance.FindByID(moAuthory.GroupId); this.treeLookUp_Group.EditValue = group.ParentGroupId; this.treeLookUp_Part.EditValue = moAuthory.GroupId; } this.txt_Account.Text = moAuthory.LoginAccount; this.txt_CardId.Text = moAuthory.CardId; this.txt_Email.Text = moAuthory.UserEmail; this.txt_Mobile.Text = moAuthory.UserMobile; this.txt_Name.Text = moAuthory.UserName; this.txt_PassWord.Text = EncodeHelper.DecryptString(moAuthory.LoginPasswd, true); List userRoleList = BLLFactory.Instance.Find(string.Format(" user_id = '{0}'", txt_Account.Text)); DefaultChecked(userRoleList); } /// /// 用户角色控件复选框默认选中情况 /// /// private void DefaultChecked(List role) { this.listCheckedID.Clear(); this.listCheckedName.Clear(); foreach (MoUserRole item in role) { listCheckedID.Add(item.RoleId); foreach (MoAuthoryRole mo in roleList) { if (mo.RoleId == item.RoleId) { listCheckedName.Add(mo.RoleName); } } } if (this.treeLookUp_Role.Properties.TreeList.Nodes.Count > 0) { foreach (TreeListNode nd in treeLookUp_Role.Properties.TreeList.Nodes) { for (int i = 0; i < listCheckedID.Count; i++) { string checkedkeyid = listCheckedID[i]; if (treeLookUp_Role.Properties.TreeList.FindNodeByKeyID(checkedkeyid) != null) { treeLookUp_Role.Properties.TreeList.FindNodeByKeyID(checkedkeyid).Checked = true; } } } } displayText = null; foreach (string name in listCheckedName) { displayText += name + ","; } this.treeLookUp_Role.Text = displayText; } /// /// 获取用户角色控件复选框已经勾选的ID和Name集合 /// private void GetSelectedRoleIDandName() { this.listCheckedID.Clear(); this.listCheckedName.Clear(); if (this.treeLookUp_Role.Properties.TreeList.Nodes.Count > 0) { foreach (TreeListNode root in this.treeLookUp_Role.Properties.TreeList.Nodes) { GetCheckedKeyID(root); } } displayText = null; foreach (string name in listCheckedName) { displayText += name + ","; } } /// /// 获取用户角色控件复选框已经勾选的ID和Name集合 /// /// 父级节点 private void GetCheckedKeyID(TreeListNode parentNode) { //if (parentNode.Nodes.Count == 0) //本项目只有根节点,此部分不需要 //{ // return;//递归终止 //} if (parentNode.CheckState != CheckState.Unchecked) { DataRowView drv = this.treeLookUp_Role.Properties.TreeList.GetDataRecordByNode(parentNode) as DataRowView; if (drv != null) { string KeyFieldName = drv["RoleId"].ToString(); string DisplayMember = drv["RoleName"].ToString(); if (!listCheckedID.Contains(KeyFieldName)) { listCheckedID.Add(KeyFieldName); } if (!listCheckedName.Contains(DisplayMember)) { listCheckedName.Add(DisplayMember); } } } //foreach (TreeListNode node in parentNode.Nodes) //本项目只有根节点,此部分不需要 //{ // if (node.CheckState != CheckState.Unchecked) // { // DataRowView drv = this.treeLookUp_Role.Properties.TreeList.GetDataRecordByNode(node) as DataRowView;//关键代码,就是不知道是这样获取数据而纠结了很久(鬼知道可以转换为DataRowView啊) // if (drv != null) // { // string KeyFieldName = drv["RoleId"].ToString(); // string DisplayMember = drv["RoleName"].ToString(); // listCheckedID.Add(KeyFieldName); // listCheckedName.Add(DisplayMember); // } // } // GetCheckedKeyID(node); //} } private void btn_Cancel_Click(object sender, EventArgs e) { this.Close(); } private void treeLookUp_Role_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e) { this.treeLookUp_Role.Properties.NullText = displayText; } private void treeLookUp_Role_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e) { e.DisplayText = displayText; } private void btn_Add_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txt_Account.Text)) { DevExpress.XtraEditors.XtraMessageBox.Show("请输入登录账号", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (string.IsNullOrEmpty(txt_Name.Text)) { DevExpress.XtraEditors.XtraMessageBox.Show("请输入用户名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (string.IsNullOrEmpty(txt_PassWord.Text)) { DevExpress.XtraEditors.XtraMessageBox.Show("请输入登录密码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } //if (txt_PassWord.Text.Trim().Length != 6) //{ // DevExpress.XtraEditors.XtraMessageBox.Show("请输入六位登录密码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); // return; //} if (string.IsNullOrEmpty(treeLookUp_Group.Text)) { DevExpress.XtraEditors.XtraMessageBox.Show("请输入所属公司", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (treeLookUp_Group.Text == treeLookUp_Part.Text) { DevExpress.XtraEditors.XtraMessageBox.Show("请正确选择公司和部门", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (string.IsNullOrEmpty(txt_Mobile.Text)) { DevExpress.XtraEditors.XtraMessageBox.Show("请输入手机号", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (string.IsNullOrEmpty(txt_Email.Text)) { DevExpress.XtraEditors.XtraMessageBox.Show("请输入邮箱", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (string.IsNullOrEmpty(treeLookUp_Role.Text)) { DevExpress.XtraEditors.XtraMessageBox.Show("请输入角色", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (lookUp_Status.Text == "请您选择") { DevExpress.XtraEditors.XtraMessageBox.Show("请输入状态", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } MoAuthoryUser user = new MoAuthoryUser(); user.LoginAccount = txt_Account.Text; user.LoginPasswd = EncodeHelper.EncryptString(txt_PassWord.Text); user.UserName = txt_Name.Text; user.UserMobile = txt_Mobile.Text; user.UserEmail = txt_Email.Text; user.UserTime = DateTime.Now; if (string.IsNullOrEmpty(this.treeLookUp_Part.Text) || this.treeLookUp_Part.Text == "无") { user.GroupId = treeLookUp_Group.EditValue.ToString(); } else { user.GroupId = treeLookUp_Part.EditValue.ToString(); } user.UserStatus = Convert.ToInt32(lookUp_Status.EditValue); if (lookUp_Sex.Text != "请您选择") { user.UserSex = Convert.ToInt32(lookUp_Sex.EditValue); } //user.UserSex = Convert.ToInt32(lookUp_Status.EditValue); user.CardId = txt_CardId.Text; List insert = new List(); foreach (string item in listCheckedID) { insert.Add(new MoUserRole { UserId = txt_Account.Text, RoleId = item }); } using (trans = BLLFactory.Instance.CreateTransaction()) { try { flag = BLLFactory.Instance.Insert(user, trans); foreach (MoUserRole item in insert) { if (flag) { flag = BLLFactory.Instance.Insert(item, trans); } } trans.Commit(); } catch (Exception ex) { trans.Rollback(); DevExpress.XtraEditors.XtraMessageBox.Show("添加失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); LogHelper.log.Error(string.Format("添加数据库authory_user和user_role出现错误:{0}", ex)); } } trans.Dispose(); if (flag) { DevExpress.XtraEditors.XtraMessageBox.Show("添加成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); saveData(); this.Close(); } } private void btn_Save_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txt_Account.Text)) { DevExpress.XtraEditors.XtraMessageBox.Show("请输入登录账号", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (string.IsNullOrEmpty(txt_Name.Text)) { DevExpress.XtraEditors.XtraMessageBox.Show("请输入用户名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (string.IsNullOrEmpty(txt_PassWord.Text)) { DevExpress.XtraEditors.XtraMessageBox.Show("请输入登录密码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } //20220307 取消位数限制 //if (txt_PassWord.Text.Trim().Length != 6) //{ // DevExpress.XtraEditors.XtraMessageBox.Show("请输入六位登录密码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); // return; //} if (string.IsNullOrEmpty(treeLookUp_Group.Text)) { DevExpress.XtraEditors.XtraMessageBox.Show("请输入所属公司", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (treeLookUp_Group.Text == treeLookUp_Part.Text) { DevExpress.XtraEditors.XtraMessageBox.Show("请正确选择公司和部门", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (string.IsNullOrEmpty(txt_Mobile.Text)) { DevExpress.XtraEditors.XtraMessageBox.Show("请输入手机号", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (string.IsNullOrEmpty(txt_Email.Text)) { DevExpress.XtraEditors.XtraMessageBox.Show("请输入邮箱", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (string.IsNullOrEmpty(treeLookUp_Role.Text)) { DevExpress.XtraEditors.XtraMessageBox.Show("请输入角色", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (lookUp_Status.Text == "请您选择") { DevExpress.XtraEditors.XtraMessageBox.Show("请输入状态", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } authoryUser.LoginAccount = txt_Account.Text; authoryUser.LoginPasswd = EncodeHelper.EncryptString(txt_PassWord.Text); authoryUser.UserName = txt_Name.Text; authoryUser.UserMobile = txt_Mobile.Text; authoryUser.UserEmail = txt_Email.Text; if (string.IsNullOrEmpty(this.treeLookUp_Part.Text) || this.treeLookUp_Part.Text == "无") { authoryUser.GroupId = treeLookUp_Group.EditValue.ToString(); } else { authoryUser.GroupId = treeLookUp_Part.EditValue.ToString(); } authoryUser.UserStatus = Convert.ToInt32(lookUp_Status.EditValue) ; authoryUser.UserSex = Convert.ToInt32(lookUp_Sex.EditValue); authoryUser.CardId = txt_CardId.Text; List upDate = new List(); foreach (string item in listCheckedID) { upDate.Add(new MoUserRole { UserId = txt_Account.Text, RoleId = item }); } using (trans = BLLFactory.Instance.CreateTransaction()) { try { flag = BLLFactory.Instance.Update(authoryUser, authoryUser.LoginAccount, trans); if (flag) { flag = BLLFactory.Instance.Delete(authoryUser.LoginAccount, SysEnvironment.CurrentLoginID, trans); } foreach (MoUserRole item in upDate) { if (flag) { flag = BLLFactory.Instance.Insert(item,trans); } } trans.Commit(); } catch (Exception ex) { trans.Rollback(); DevExpress.XtraEditors.XtraMessageBox.Show("修改失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); LogHelper.log.Error(string.Format("修改数据库authory_user和user_role出现错误:{0}", ex)); } } trans.Dispose(); if (flag) { DevExpress.XtraEditors.XtraMessageBox.Show("修改成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); saveData(); this.Close(); } } private void treeLookUp_Group_EditValueChanged(object sender, EventArgs e) { if (!string.IsNullOrEmpty(this.treeLookUp_Group.EditValue.ToString())) { if (this.treeLookUp_Group.EditValue.ToString() == "1000" || this.treeLookUp_Group.EditValue.ToString() == "0") { InitTreePart(null); InitTreeRole(null); return; } InitTreePart(this.treeLookUp_Group.EditValue.ToString()); InitTreeRole(this.treeLookUp_Group.EditValue.ToString()); } } /// /// 部门控件 /// /// 所选的组织id private void InitTreePart(string id) { DataTable dt = new DataTable(); dt.Columns.Add("ImageIndex", typeof(int)); dt.Columns.Add("GroupID", typeof(string)); dt.Columns.Add("GroupPID", typeof(string)); dt.Columns.Add("GroupName", typeof(string)); List list = new List(); if (id == null) { list = BLLFactory.Instance.GetAll(); } else { list = BLLFactory.Instance.Find(string.Format(" group_id = '{0}' OR parent_group_id = '{1}'",id,id)); } DataRow dr = null; foreach (MoAuthoryGroup info in list) { dr = dt.NewRow(); dr["ImageIndex"] = 0; dr["GroupID"] = info.GroupId; dr["GroupPID"] = info.ParentGroupId; dr["GroupName"] = info.GroupName; dt.Rows.Add(dr); } //增加一行空的 dr = dt.NewRow(); dr["GroupID"] = "0"; //使用0代替-1,避免出现节点的嵌套显示,因为-1已经作为了一般节点的顶级标识 dr["GroupPID"] = "-1"; dr["GroupName"] = "无"; dt.Rows.InsertAt(dr, 0); //设置图形序号 //this.treeListLookUpEdit1TreeList.SelectImageList = this.imageList2; //this.treeListLookUpEdit1TreeList.StateImageList = this.imageList2; this.treeLookUp_Part.Properties.TreeList.KeyFieldName = "GroupID"; this.treeLookUp_Part.Properties.TreeList.ParentFieldName = "GroupPID"; this.treeLookUp_Part.Properties.DataSource = dt; this.treeLookUp_Part.Properties.ValueMember = "GroupID"; this.treeLookUp_Part.Properties.DisplayMember = "GroupName"; this.treeLookUp_Part.Text = ""; this.treeLookUp_Part.Properties.NullText = ""; this.treeLookUp_Part.Properties.NullValuePrompt = "请选择部门"; this.treeLookUp_Part.Properties.NullValuePromptShowForEmptyValue = true; } /// /// 角色控件 /// /// 所选的组织id private void InitTreeRole(string id) { DataTable data = new DataTable(); data.Columns.Add("ImageIndex", typeof(int)); data.Columns.Add("RoleId", typeof(string)); data.Columns.Add("ParentRoleId", typeof(string)); data.Columns.Add("RoleName", typeof(string)); if (id == null) { roleList = BLLFactory.Instance.GetAll(); } else { string sql = " role_id in (SELECT b.role_id from group_role b where group_id = '"+ id +"')"; roleList = BLLFactory.Instance.Find(sql); } DataRow dataRow = null; foreach (MoAuthoryRole info in roleList) { dataRow = data.NewRow(); dataRow["ImageIndex"] = 0; dataRow["RoleId"] = info.RoleId; //dataRow["ParentRoleId"] = null; dataRow["RoleName"] = info.RoleName; data.Rows.Add(dataRow); } //设置图形序号 //this.treeListLookUpEdit1TreeList.SelectImageList = this.imageList2; //this.treeListLookUpEdit1TreeList.StateImageList = this.imageList2; this.treeLookUp_Role.Properties.TreeList.OptionsView.ShowCheckBoxes = true; //设置带有复选框 //this.treeLookUp_Role.Properties.TreeList.OptionsBehavior.AllowIndeterminateCheckState = true; //设置复选框有三个状态 this.treeLookUp_Role.Properties.TreeList.KeyFieldName = "RoleId"; //this.treeLookUp_Role.Properties.TreeList.ParentFieldName = "ParentRoleId"; this.treeLookUp_Role.Properties.DataSource = data; this.treeLookUp_Role.Properties.ValueMember = "RoleId"; this.treeLookUp_Role.Properties.DisplayMember = "RoleName"; this.treeLookUp_Role.Text = ""; this.treeLookUp_Part.Properties.NullText = ""; this.treeLookUp_Role.Properties.NullValuePrompt = "请选择角色"; this.treeLookUp_Role.Properties.NullValuePromptShowForEmptyValue = true; displayText = null; this.treeLookUp_Role.Properties.TreeList.AfterCheckNode += (s, a) => { a.Node.Selected = true; // txtRole.RefreshEditValue(); // txtRole.ForceClosePopup(); GetSelectedRoleIDandName(); }; } } }