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.Controls; using SIMDP.BLL; using SIMDP.Model; using ProjectBase.Data.Logs; using ProjectBase.Data.BaseDAL; using System.Data.Common; using ProjectBase.Util; namespace SIMDP.View { public partial class FormAuthoryGroup : BaseDock { //BlAuthoryGroup blGroup = new BlAuthoryGroup(); //BlAuthoryUser blUser = new BlAuthoryUser(); //BlAuthoryRole blRole = new BlAuthoryRole(); DbTransaction trans; bool flag = true; public FormAuthoryGroup() { InitializeComponent(); InitTree_Group(); InitLookUpGroup(); } /// /// 初始化树--组织 /// private void InitTree_Group() { this.tree_Group.BeginUpdate(); this.tree_Group.Nodes.Clear(); this.tree_Group.ImageList = this.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.ImageIndex = 0; rootNode.SelectedImageIndex = 0; this.tree_Group.Nodes.Add(rootNode); List subList = BLLFactory.Instance.Find(string.Format(" parent_group_id = '{0}'", root.GroupId)); AddNode_Group(subList, rootNode); } } this.tree_Group.ExpandAll(); this.tree_Group.EndUpdate(); this.tree_Group.SelectedNode = this.tree_Group.Nodes[0]; } /// /// 添加树节点--组织 /// /// /// private void AddNode_Group(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; string index = item.GroupId.Substring(0,1); node.ImageIndex = Convert.ToInt32(index) - 1; node.SelectedImageIndex = Convert.ToInt32(index) - 1; //topnode.ImageIndex = Portal.gc.GetImageIndex(groupInfo.Category); //topnode.SelectedImageIndex = Portal.gc.GetImageIndex(groupInfo.Category); parentNode.Nodes.Add(node); List subList = BLLFactory.Instance.Find(string.Format(" parent_group_id = '{0}'", item.GroupId)); AddNode_Group(subList, node); } } private void tree_Group_AfterSelect(object sender, TreeViewEventArgs e) { if (e.Node != null) { MoAuthoryGroup group = new MoAuthoryGroup(); group = BLLFactory.Instance.FindByID(e.Node.Name); this.txt_GroupId.Text = group.GroupId; this.txt_GroupName.Text = group.GroupName; if (!string.IsNullOrEmpty(group.ParentGroupId)) { this.treelookUp_PGroup.EditValue = group.ParentGroupId; this.txt_GroupId.Enabled = true; this.treelookUp_PGroup.Enabled = true; } else { this.txt_GroupId.Enabled = false; this.treelookUp_PGroup.Enabled = false; } RefreshRole(group.GroupId); RefreshUser(group.GroupId); } } /// /// 初始化上层组织控件 /// private void InitLookUpGroup() { 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) { 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_PGroup.Properties.TreeList.KeyFieldName = "GroupID"; this.treelookUp_PGroup.Properties.TreeList.ParentFieldName = "GroupPID"; this.treelookUp_PGroup.Properties.DataSource = dt; this.treelookUp_PGroup.Properties.ValueMember = "GroupID"; this.treelookUp_PGroup.Properties.DisplayMember = "GroupName"; this.treelookUp_PGroup.EditValue = "0"; } private void menu_Add_Click(object sender, EventArgs e) { TreeNode choiceNode = this.tree_Group.SelectedNode; //if (BLLFactory.Instance.GetRecordCount() <= 0) //{ // DevExpress.XtraEditors.XtraMessageBox.Show("请先添加组织信息", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); // return; //} if (choiceNode == null) { this.txt_GroupName.Text = null; this.txt_GroupId.Text = null; this.treelookUp_PGroup.EditValue = "0"; this.txt_GroupId.Enabled = true; this.treelookUp_PGroup.Enabled = true; } if (choiceNode.Level == 0) { this.txt_GroupName.Text = null; this.txt_GroupId.Text = null; this.treelookUp_PGroup.EditValue = choiceNode.Name; this.txt_GroupId.Enabled = true; this.treelookUp_PGroup.Enabled = true; } else { this.txt_GroupName.Text = null; this.txt_GroupId.Text = null; this.treelookUp_PGroup.EditValue = choiceNode.Name; } } private void menu_Delete_Click(object sender, EventArgs e) { TreeNode node = this.tree_Group.SelectedNode; if (node == null) { DevExpress.XtraEditors.XtraMessageBox.Show("请选择要删除的记录", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (DevExpress.XtraEditors.XtraMessageBox.Show("您确定删除选定的记录么?", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No) { return; } if (node.Level == 0) { DevExpress.XtraEditors.XtraMessageBox.Show("不能删除集团,请重新选择", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } List groupId = new List(); FetchNode(node,ref groupId); using (trans = BLLFactory.Instance.CreateTransaction()) { try { foreach (string id in groupId) { if (flag) { flag = BLLFactory.Instance.Delete(id, SysEnvironment.CurrentLoginID, trans); } else { break; } } trans.Commit(); } catch (Exception ex) { trans.Rollback(); DevExpress.XtraEditors.XtraMessageBox.Show("删除失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); LogHelper.log.Error(string.Format("删除数据库authory_group出现错误:{0}", ex)); } } trans.Dispose(); if (flag) { DevExpress.XtraEditors.XtraMessageBox.Show("删除成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); InitTree_Group(); InitLookUpGroup(); } } /// /// 遍历一个节点下的所有子节点 /// /// 要遍历的节点 /// 该节点与所有子节点名字的集合 private void FetchNode(TreeNode node,ref List groupId) { groupId.Add(node.Name); for (int i = 0; i < node.Nodes.Count; i++) { FetchNode(node.Nodes[i],ref groupId); } } private void menu_Expand_Click(object sender, EventArgs e) { this.tree_Group.ExpandAll(); } private void menu_Collapse_Click(object sender, EventArgs e) { this.tree_Group.CollapseAll(); } private void menu_Refresh_Click(object sender, EventArgs e) { InitTree_Group(); } /// /// 刷新包含用户 /// /// 组ID private void RefreshUser(string id) { this.listBox_User.BeginUpdate(); this.listBox_User.Items.Clear(); List list = BLLFactory.Instance.Find(string.Format(" group_id = '{0}'",id)); foreach (MoAuthoryUser info in list) { string name = string.Format("{0}({1})", info.UserName, info.LoginAccount); this.listBox_User.Items.Add(name); } if (this.listBox_User.Items.Count > 0) { this.listBox_User.SelectedIndex = 0; } this.listBox_User.EndUpdate(); } /// /// 刷新所属角色 /// /// 组ID private void RefreshRole(string id) { this.listBox_Role.BeginUpdate(); this.listBox_Role.Items.Clear(); string sql = " role_id IN (SELECT b.role_id FROM group_role b WHERE b.group_id = '"+ id +"')"; List list = BLLFactory.Instance.Find(sql); foreach (MoAuthoryRole info in list) { string name = string.Format("{0}({1})", info.RoleName, info.RoleId); this.listBox_Role.Items.Add(name); } if (this.listBox_Role.Items.Count > 0) { this.listBox_Role.SelectedIndex = 0; } this.listBox_Role.EndUpdate(); } private void btn_Save_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txt_GroupId.Text)) { DevExpress.XtraEditors.XtraMessageBox.Show("请输入组织名称", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (string.IsNullOrEmpty(txt_GroupId.Text)) { DevExpress.XtraEditors.XtraMessageBox.Show("请输入组织编号", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } try { MoAuthoryGroup group = new MoAuthoryGroup(); group.GroupName = txt_GroupName.Text; group.GroupId = txt_GroupId.Text; group.GroupTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); if (this.treelookUp_PGroup.EditValue.ToString() != "0") { group.ParentGroupId = treelookUp_PGroup.EditValue.ToString(); } bool flag = BLLFactory.Instance.InsertUpdate(group, group.GroupId); if (flag) { DevExpress.XtraEditors.XtraMessageBox.Show("保存成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); InitTree_Group(); InitLookUpGroup(); } else { DevExpress.XtraEditors.XtraMessageBox.Show("保存失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ex) { DevExpress.XtraEditors.XtraMessageBox.Show("保存失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); LogHelper.log.Error(string.Format("添加或修改数据库authory_group出现错误:{0}", ex)); } } private void tree_Group_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { TreeNode node = tree_Group.GetNodeAt(e.X, e.Y); if (node != null) { tree_Group.SelectedNode = node; } } base.OnMouseDown(e); } } }