123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379 |
- 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();
- }
- /// <summary>
- /// 初始化树--组织
- /// </summary>
- private void InitTree_Group()
- {
- this.tree_Group.BeginUpdate();
- this.tree_Group.Nodes.Clear();
- this.tree_Group.ImageList = this.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.ImageIndex = 0;
- rootNode.SelectedImageIndex = 0;
- this.tree_Group.Nodes.Add(rootNode);
- List<MoAuthoryGroup> subList = BLLFactory<BlAuthoryGroup>.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];
- }
- /// <summary>
- /// 添加树节点--组织
- /// </summary>
- /// <param name="list"></param>
- /// <param name="parentNode"></param>
- private void AddNode_Group(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;
- 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<MoAuthoryGroup> subList = BLLFactory<BlAuthoryGroup>.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<BlAuthoryGroup>.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);
- }
- }
- /// <summary>
- /// 初始化上层组织控件
- /// </summary>
- 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<MoAuthoryGroup> list = BLLFactory<BlAuthoryGroup>.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<BlAuthoryGroup>.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<string> groupId = new List<string>();
- FetchNode(node,ref groupId);
- using (trans = BLLFactory<BlAuthoryUser>.Instance.CreateTransaction())
- {
- try
- {
- foreach (string id in groupId)
- {
- if (flag)
- {
- flag = BLLFactory<BlAuthoryGroup>.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();
- }
- }
- /// <summary>
- /// 遍历一个节点下的所有子节点
- /// </summary>
- /// <param name="node"> 要遍历的节点</param>
- /// <param name="groupId"> 该节点与所有子节点名字的集合</param>
- private void FetchNode(TreeNode node,ref List<string> 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();
- }
- /// <summary>
- /// 刷新包含用户
- /// </summary>
- /// <param name="id">组ID</param>
- private void RefreshUser(string id)
- {
- this.listBox_User.BeginUpdate();
- this.listBox_User.Items.Clear();
- List<MoAuthoryUser> list = BLLFactory<BlAuthoryUser>.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();
- }
- /// <summary>
- /// 刷新所属角色
- /// </summary>
- /// <param name="id">组ID</param>
- 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<MoAuthoryRole> list = BLLFactory<BlAuthoryRole>.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<BlAuthoryGroup>.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);
- }
- }
- }
|