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 ProjectBase.Data.Logs; using SIMDP.Model; using SIMDP.BLL; using System.Data.Common; using ProjectBase.Data.BaseDAL; using ProjectBase.Util; namespace SIMDP.View { public partial class FormAuthoryUser : DevExpress.XtraEditors.XtraForm { //BlAuthoryGroup blGroup = new BlAuthoryGroup(); //BlAuthoryUser blUser = new BlAuthoryUser(); //BlUserRole blUserRole = new BlUserRole(); string condition = null; DbTransaction trans; bool flag; //private string selectTreeNode = ""; // 选中节点 private int treeLevel = 0; // 树形结构的层级-- 角色树 //private int nodeType = 0; // 选中节点类型,0,代表组织,1,代表角色--角色树 /// /// 性别的字典表 /// private Dictionary Dic_Sex { get { Dictionary sex = new Dictionary(); sex.Add("-1","全部"); sex.Add("1", "男"); sex.Add("0", "女"); return sex; } } /// /// 状态的字典表 /// private Dictionary Dic_Status { get { Dictionary status = new Dictionary(); status.Add("-1", "全部"); status.Add("0", "停用"); status.Add("1", "启用"); return status; } } public FormAuthoryUser() { InitializeComponent(); //下拉框--性别 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"; this.lookUp_Sex.EditValue = -1; //下拉框--状态 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"; this.lookUp_Status.EditValue = -1; InitTree_Dept(); InitTree_Role(); InitGrid(); } /// /// 初始化树--组织 /// private void InitTree_Dept() { this.tree_Dept.BeginUpdate(); this.tree_Dept.Nodes.Clear(); this.tree_Dept.ImageList = imageList1; List list = BLLFactory.Instance.GetAll(); List rootList = new List(); foreach (MoAuthoryGroup item in list) { if (item.ParentGroupId == "") { rootList.Add(item); } } foreach (MoAuthoryGroup root in rootList) { if (root != null) { TreeNode rootNode = new TreeNode(); //根节点 rootNode.Text = root.GroupName; rootNode.Name = root.GroupId; rootNode.Tag = string.Format(" group_id = '{0}'", root.GroupId); rootNode.ImageIndex = 0; rootNode.SelectedImageIndex = 0; this.tree_Dept.Nodes.Add(rootNode); List subList = BLLFactory.Instance.Find(string.Format(" parent_group_id = '{0}'",root.GroupId)); AddNode_Dept(subList,rootNode); } } this.tree_Dept.ExpandAll(); this.tree_Dept.EndUpdate(); this.tree_Dept.SelectedNode = null; } /// /// 添加树节点--组织 /// /// /// private void AddNode_Dept(List list ,TreeNode parentNode) { if (list == null) { return; } foreach (MoAuthoryGroup item in list) { TreeNode node = new TreeNode(); //根节点 node.Text = item.GroupName; node.Name = item.GroupId; node.Tag = string.Format(" group_id = '{0}'",item.GroupId); string index = item.GroupId.Substring(0, 1); node.ImageIndex = Convert.ToInt32(index) - 1; node.SelectedImageIndex = Convert.ToInt32(index) - 1; parentNode.Nodes.Add(node); List subList = BLLFactory.Instance.Find(string.Format(" parent_group_id = '{0}'", item.GroupId)); AddNode_Dept(subList, node); } } /// /// 初始化树--角色 /// private void InitTree_Role() { this.tree_Role.BeginUpdate(); this.tree_Role.Nodes.Clear(); this.tree_Role.ImageList = imageList2; string condition = string.Format(" parent_group_id =''"); List list = BLLFactory.Instance.Find(condition); List rootList = new List(); foreach (MoAuthoryGroup item in list) { rootList.Add(item); treeLevel = 1; } foreach (MoAuthoryGroup root in rootList) { if (root != null) { TreeNode rootNode = new TreeNode(); //根节点 rootNode.Text = root.GroupName; rootNode.Name = root.GroupId; rootNode.ImageIndex = 0; rootNode.SelectedImageIndex = 0; rootNode.Tag = " group_id = '" + root.GroupId + "'"; AddChildRole(rootNode); if (root.GroupId.Substring(0, 1).Equals("1")) { List subList = BLLFactory.Instance.Find(string.Format(" parent_group_id = '{0}'", root.GroupId)); AddChildNode(subList, rootNode); treeLevel = 2; //根节点为集团 } this.tree_Role.Nodes.Add(rootNode); } } this.tree_Role.ExpandAll(); this.tree_Role.EndUpdate(); } /// /// 添加公司级子节点--角色树 /// /// /// private void AddChildNode(List list, TreeNode parentNode) { if (list == null) { return; } foreach (MoAuthoryGroup item in list) { TreeNode node = new TreeNode(); //公司级子节点 node.Text = item.GroupName; node.Name = item.GroupId; node.ImageIndex = 1; node.SelectedImageIndex = 1; node.Tag = " group_id = '" + item.GroupId + "'"; parentNode.Nodes.Add(node); AddChildRole(node); } } /// /// 添加角色节点--角色树 /// /// private void AddChildRole(TreeNode parentNode) { string sql = " role_id in (SELECT b.role_id from group_role b where b.group_id = '" + parentNode.Name + "')"; //BlAuthoryRole blRole = new BlAuthoryRole(); List roleList = new List(); roleList = BLLFactory.Instance.Find(sql); foreach (MoAuthoryRole item in roleList) { TreeNode node = new TreeNode(); node.Text = item.RoleName; node.Name = item.RoleId; node.ImageIndex = 2; node.SelectedImageIndex = 2; if (treeLevel == 1) { node.Tag = " login_account in (SELECT b.user_id from user_role b where b.role_id = '" + item.RoleId + "') AND group_id = '" + parentNode.Name + "'"; } else if (treeLevel == 2) { node.Tag = " group_id = '" + parentNode.Name + "'"; } parentNode.Nodes.Add(node); } } /// /// 初始化表格控件 /// private void InitGrid() { //entity this.winGridViewPager1.OnPageChanged += new EventHandler(winGridViewPager1_OnPageChanged); this.winGridViewPager1.OnStartExport += new EventHandler(winGridViewPager1_OnStartExport); this.winGridViewPager1.OnEditSelected += new EventHandler(winGridViewPager1_OnEditSelected); this.winGridViewPager1.OnAddNew += new EventHandler(winGridViewPager1_OnAddNew); this.winGridViewPager1.OnDeleteSelected += new EventHandler(winGridViewPager1_OnDeleteSelected); this.winGridViewPager1.OnRefresh += new EventHandler(winGridViewPager1_OnRefresh); this.winGridViewPager1.gridView1.CustomColumnDisplayText += new DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventHandler(gridView1_CustomColumnDisplayText); //this.winGridViewPager1.AppendedMenu = this.contextMenuStrip1; this.winGridViewPager1.ShowLineNumber = true; this.winGridViewPager1.BestFitColumnWith = true; this.winGridViewPager1.DisplayColumns = "LoginAccount,UserName,UserMobile,UserEmail,UserTime,GroupId,CompanyId,UserStatus,UserSex,CardId"; #region 添加别名解析 this.winGridViewPager1.AddColumnAlias("LoginAccount", "登录账号"); this.winGridViewPager1.AddColumnAlias("LoginPasswd", ""); this.winGridViewPager1.AddColumnAlias("UserName", "用户名"); this.winGridViewPager1.AddColumnAlias("UserMobile", "用户手机号"); this.winGridViewPager1.AddColumnAlias("UserEmail", "用户邮箱"); this.winGridViewPager1.AddColumnAlias("UserTime", "用户创建时间"); this.winGridViewPager1.AddColumnAlias("GroupId", "用户组"); this.winGridViewPager1.AddColumnAlias("CompanyId", "所属公司"); this.winGridViewPager1.AddColumnAlias("UserStatus", "用户状态"); this.winGridViewPager1.AddColumnAlias("UserSex", "用户性别"); this.winGridViewPager1.AddColumnAlias("CardId", "员工号"); #endregion this.winGridViewPager1.PrintTitle = "用户信息报表"; this.winGridViewPager1.gridView1.Appearance.HeaderPanel.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center; BindData(null); } /// /// 绑定数据 /// private void BindData(string condition) { if (string.IsNullOrEmpty(condition)) { this.winGridViewPager1.DataSource = BLLFactory.Instance.GetAll(this.winGridViewPager1.PagerInfo); for (int i = 0; i < this.winGridViewPager1.gridView1.Columns.Count; i++) //设置每列内容居中显示 { this.winGridViewPager1.gridView1.Columns[i].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center; } return; } this.winGridViewPager1.DataSource = BLLFactory.Instance.FindWithPager(condition, this.winGridViewPager1.PagerInfo); for (int i = 0; i < this.winGridViewPager1.gridView1.Columns.Count; i++) //设置每列内容居中显示 { this.winGridViewPager1.gridView1.Columns[i].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center; } } /// /// 分页控件翻页的操作 /// private void winGridViewPager1_OnPageChanged(object sender, EventArgs e) { BindData(condition); } /// /// 分页控件全部导出操作前的操作 /// private void winGridViewPager1_OnStartExport(object sender, EventArgs e) { this.winGridViewPager1.AllToExport = BLLFactory.Instance.Find(condition); } /// /// 分页控件编辑项操作 /// private void winGridViewPager1_OnEditSelected(object sender, EventArgs e) { MoAuthoryUser selectRow = this.winGridViewPager1.gridView1.GetFocusedRow() as MoAuthoryUser; FormEditAuthoryUser editUser = new FormEditAuthoryUser(selectRow); editUser.saveData += new FormEditAuthoryUser.save(DataSaved); editUser.ShowDialog(); } ///// ///// 分页控件新增操作 ///// private void winGridViewPager1_OnAddNew(object sender, EventArgs e) { menuDept_Add_Click(null, null); } /// /// 分页控件删除操作 /// /// /// private void winGridViewPager1_OnDeleteSelected(object sender, EventArgs e) { if (DevExpress.XtraEditors.XtraMessageBox.Show("您确定删除选定的记录么?", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No) { return; } MoAuthoryUser selectRow = this.winGridViewPager1.gridView1.GetFocusedRow() as MoAuthoryUser; string id = selectRow.LoginAccount; using (trans = BLLFactory.Instance.CreateTransaction()) { try { flag = BLLFactory.Instance.Delete(id, SysEnvironment.CurrentLoginID, trans); if (flag) { flag = BLLFactory.Instance.Delete(id, SysEnvironment.CurrentLoginID, 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); BindData(condition); } } /// /// 分页控件刷新操作 /// private void winGridViewPager1_OnRefresh(object sender, EventArgs e) { BindData(condition); } void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e) { if (e.Column.ColumnType == typeof(DateTime)) { string columnName = e.Column.FieldName; if (e.Value != null) { if (Convert.ToDateTime(e.Value) <= Convert.ToDateTime("1900-1-1")) { e.DisplayText = ""; } else { e.DisplayText = Convert.ToDateTime(e.Value).ToString("yyyy-MM-dd HH:mm");//yyyy-MM-dd } } } if (e.Column.FieldName == "UserSex") { e.DisplayText = Dic_Sex[(e.Value).ToString()]; } if (e.Column.FieldName == "UserStatus") { e.DisplayText = Dic_Status[(e.Value).ToString()]; } } private void tree_Dept_AfterSelect(object sender, TreeViewEventArgs e) { if (e.Node != null && e.Node.Tag != null) { condition = e.Node.Tag.ToString(); BindData(condition); } else { condition = ""; BindData(condition); } } private void menuDept_Add_Click(object sender, EventArgs e) { FormEditAuthoryUser editUser = new FormEditAuthoryUser(null); editUser.saveData += new FormEditAuthoryUser.save(DataSaved); editUser.ShowDialog(); } private void menuDept_Expand_Click(object sender, EventArgs e) { this.tree_Dept.ExpandAll(); } private void menuDept_Collapse_Click(object sender, EventArgs e) { this.tree_Dept.CollapseAll(); } private void menuDept_Refresh_Click(object sender, EventArgs e) { InitTree_Dept(); } private void menuRole_Add_Click(object sender, EventArgs e) { menuDept_Add_Click(null,null); } private void menuRole_Expand_Click(object sender, EventArgs e) { this.tree_Role.ExpandAll(); } private void menuRole_Collapse_Click(object sender, EventArgs e) { this.tree_Role.CollapseAll(); } private void menuRole_Refresh_Click(object sender, EventArgs e) { InitTree_Role(); } private void btn_Query_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txt_Account.Text) && string.IsNullOrEmpty(txt_CardId.Text) && string.IsNullOrEmpty(txt_Email.Text) && string.IsNullOrEmpty(txt_Mobile.Text) && string.IsNullOrEmpty(txt_UserName.Text) && lookUp_Sex.Text == "全部" && lookUp_Status.Text == "全部") { BindData(null); condition = null; return; } string sql = " 1=1"; if (!string.IsNullOrEmpty(txt_Account.Text)) { sql += string.Format(" AND login_account = '{0}'", txt_Account.Text); } if (!string.IsNullOrEmpty(txt_CardId.Text)) { sql += string.Format(" AND card_id = '{0}'", txt_CardId.Text); } if (!string.IsNullOrEmpty(txt_Email.Text)) { sql += string.Format(" AND user_email = '{0}'", txt_Email.Text); } if (!string.IsNullOrEmpty(txt_Mobile.Text)) { sql += string.Format(" AND user_mobile = '{0}'", txt_Mobile.Text); } if (!string.IsNullOrEmpty(txt_UserName.Text)) { sql += string.Format(" AND user_name = '{0}'", txt_UserName.Text); } if (lookUp_Sex.Text != "全部") { sql += string.Format(" AND user__sex = {0}", lookUp_Sex.EditValue); } if (lookUp_Status.Text != "全部") { sql += string.Format(" AND user_status = {0}", lookUp_Status.EditValue); } BindData(sql); condition = sql; } /// /// 保存数据成功后调用 /// private void DataSaved() { InitTree_Dept(); InitTree_Role(); BindData(condition); } private void tree_Role_AfterSelect(object sender, TreeViewEventArgs e) { //if (e.Node.Level == 0) //{ // if (treeLevel == 2) // return; // else // { // string sql = " group_id = '" + e.Node.Name + "'"; // BindData(sql); // condition = sql; // } //} //else if (e.Node.Level == 1) //{ // if (treeLevel == 2) // { // string sql = " group_id = '" + e.Node.Name + "'"; // BindData(sql); // condition = sql; // } // else // { // 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 +"'"; // BindData(sql); // condition = sql; // } //} //else //{ // 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 + "'"; // BindData(sql); // condition = sql; //} if (e.Node != null && e.Node.Tag != null) { condition = e.Node.Tag.ToString(); BindData(condition); } else { condition = ""; BindData(condition); } } } }