123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584 |
- 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,代表角色--角色树
- /// <summary>
- /// 性别的字典表
- /// </summary>
- private Dictionary<string, string> Dic_Sex
- {
- get
- {
- Dictionary<string, string> sex = new Dictionary<string, string>();
- sex.Add("-1","全部");
- sex.Add("1", "男");
- sex.Add("0", "女");
- return sex;
- }
- }
- /// <summary>
- /// 状态的字典表
- /// </summary>
- private Dictionary<string, string> Dic_Status
- {
- get
- {
- Dictionary<string, string> status = new Dictionary<string, string>();
- 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();
- }
- /// <summary>
- /// 初始化树--组织
- /// </summary>
- private void InitTree_Dept()
- {
- this.tree_Dept.BeginUpdate();
- this.tree_Dept.Nodes.Clear();
- this.tree_Dept.ImageList = imageList1;
- List<MoAuthoryGroup> list = BLLFactory<BlAuthoryGroup>.Instance.GetAll();
- List<MoAuthoryGroup> rootList = new List<MoAuthoryGroup>();
- 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<MoAuthoryGroup> subList = BLLFactory<BlAuthoryGroup>.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;
- }
- /// <summary>
- /// 添加树节点--组织
- /// </summary>
- /// <param name="list"></param>
- /// <param name="parentNode"></param>
- private void AddNode_Dept(List<MoAuthoryGroup> 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<MoAuthoryGroup> subList = BLLFactory<BlAuthoryGroup>.Instance.Find(string.Format(" parent_group_id = '{0}'", item.GroupId));
- AddNode_Dept(subList, node);
- }
- }
- /// <summary>
- /// 初始化树--角色
- /// </summary>
- 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<MoAuthoryGroup> list = BLLFactory<BlAuthoryGroup>.Instance.Find(condition);
- List<MoAuthoryGroup> rootList = new List<MoAuthoryGroup>();
- 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<MoAuthoryGroup> subList = BLLFactory<BlAuthoryGroup>.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();
- }
- /// <summary>
- /// 添加公司级子节点--角色树
- /// </summary>
- /// <param name="list"></param>
- /// <param name="parentNode"></param>
- private void AddChildNode(List<MoAuthoryGroup> 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);
- }
- }
- /// <summary>
- /// 添加角色节点--角色树
- /// </summary>
- /// <param name="parentNode"></param>
- 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<MoAuthoryRole> roleList = new List<MoAuthoryRole>();
- roleList = BLLFactory<BlAuthoryRole>.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);
- }
- }
- /// <summary>
- /// 初始化表格控件
- /// </summary>
- 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);
- }
- /// <summary>
- /// 绑定数据
- /// </summary>
- private void BindData(string condition)
- {
- if (string.IsNullOrEmpty(condition))
- {
- this.winGridViewPager1.DataSource = BLLFactory<BlAuthoryUser>.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<BlAuthoryUser>.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;
- }
- }
- /// <summary>
- /// 分页控件翻页的操作
- /// </summary>
- private void winGridViewPager1_OnPageChanged(object sender, EventArgs e)
- {
- BindData(condition);
- }
- /// <summary>
- /// 分页控件全部导出操作前的操作
- /// </summary>
- private void winGridViewPager1_OnStartExport(object sender, EventArgs e)
- {
- this.winGridViewPager1.AllToExport = BLLFactory<BlAuthoryUser>.Instance.Find(condition);
- }
- /// <summary>
- /// 分页控件编辑项操作
- /// </summary>
- 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();
- }
- ///// <summary>
- ///// 分页控件新增操作
- ///// </summary>
- private void winGridViewPager1_OnAddNew(object sender, EventArgs e)
- {
- menuDept_Add_Click(null, null);
- }
- /// <summary>
- /// 分页控件删除操作
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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<BlAuthoryUser>.Instance.CreateTransaction())
- {
- try
- {
- flag = BLLFactory<BlAuthoryUser>.Instance.Delete(id, SysEnvironment.CurrentLoginID, trans);
- if (flag)
- {
- flag = BLLFactory<BlUserRole>.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);
- }
- }
- /// <summary>
- /// 分页控件刷新操作
- /// </summary>
- 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;
- }
- /// <summary>
- /// 保存数据成功后调用
- /// </summary>
- 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);
- }
- }
- }
- }
|