Treeview

using DBHelper;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Linq;
using System.Text;

namespace MES
{
public partial class RolePower : UIForm
{
MyDB DataConn = new DBHelper.MyDB();
RoleDAL dal = new RoleDAL();
public int RoleId = 0;


public RolePower(int Id)
{
InitializeComponent();
RoleId = Id;
}

public RolePower()
{
InitializeComponent();
}

 


private void btncancel_Click(object sender, EventArgs e)
{
this.Close();
}


/// <summary>
/// 确定
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnDetermine_Click(object sender, EventArgs e)
{
try
{
dal.nodeDelete(RoleId);
RoleMenuEntity Rolenode = new RoleMenuEntity();
foreach (TreeNode node in TreeViews.Nodes)
{
if (node.Checked)
{
Rolenode.MenuId = Convert.ToInt32(node.Name);
Rolenode.RoleId = RoleId;
int result = dal.RoleMenuAdd(Rolenode);
// 递归处理子节点
GetCheckedChildNodes(node, null);
}
else
{
TreeNode rootNode = node; // 获取根节点
List<TreeNodeData> allNodesData = GetAllNodesData(rootNode); // 获取所有节点数据
}
}
this.Close();
}
catch { }
}


private void GetCheckedChildNodes(TreeNode parentNode, TreeNode pNode)
{
try
{
RoleMenuButtonEntity Data = new RoleMenuButtonEntity();
List<TreeNode> uncheckedNodes = new List<TreeNode>();

foreach (TreeNode childNode in parentNode.Nodes)
{
if (childNode.Checked == true)
{
List<MenuEntity> list = dal.NodeExist(childNode.Name, childNode.Text);
if (list.Count > 0)
{
// 递归处理孙子节点(如果存在)
GetCheckedChildNodes(childNode, null);

// 递归处理孙子节点(如果存在)
GetCheckedChildNodes(childNode, null);
Data.MenuId = Convert.ToInt32(childNode.Name);
Data.ButtonId = Convert.ToInt32(childNode.Name);
Data.RoleId = RoleId;
//判断子节点是否保存过,避免重复保存数据
List<RoleMenuButtonEntity> Reult = dal.NodeExist(Data);
if (Reult.Count == 0)
{
//保存节点到数据库
int result = dal.MenuButtonAdd(Data);
}
}
else
{
TreeNode thirdLevelNode = childNode; // 获取或创建一个三级节点
if (thirdLevelNode.Parent != null) // 检查是否存在父节点
{
// 递归处理孙子节点(如果存在)
GetCheckedChildNodes(childNode, null);
Data.MenuId = Convert.ToInt32(thirdLevelNode.Parent.Name);
Data.ButtonId = Convert.ToInt32(childNode.Name);
Data.RoleId = RoleId;
//判断子节点是否保存过,避免重复保存数据
List<RoleMenuButtonEntity> Reult = dal.NodeExist(Data);
if (Reult.Count == 0)
{
//保存节点到数据库
int result = dal.MenuButtonAdd(Data);
}
}
}
}
}
}
catch (Exception ex) { Console.WriteLine(ex); }
}

public List<TreeNodeData> GetAllNodesData(TreeNode node)
{
RoleMenuButtonEntity Data = new RoleMenuButtonEntity();
List<TreeNodeData> nodesData = new List<TreeNodeData>();
if (node != null)
{
foreach (TreeNode child in node.Nodes)
{
nodesData.AddRange(GetAllNodesData(child)); // 递归调用,获取子节点数据
}
if (node.Checked == true)
{
Data.MenuId = Convert.ToInt32(node.Parent.Name);
Data.ButtonId = Convert.ToInt32(node.Name);
Data.RoleId = RoleId;
List<RoleMenuButtonEntity> Reult = dal.NodeExist(Data);
if (Reult.Count == 0)
{
// node.Checked=false 保存节点到数据库
int result = dal.MenuButtonAdd(Data);

//查询按钮的根节点
List<RoleMenuButtonEntity> Reult03= dal.NodeSALL(Data);
if (Reult03.Count > 0)
{
Data.MenuId = Convert.ToInt32(Reult03[0].ParentId);

List<RoleMenuButtonEntity> Reult04 = dal.NodeEXitSALL(Data);
if (Reult04.Count== 0)
{
int result03 = dal.MenuNodeAdd(Data);
}
}
}
}
}
return nodesData;
}

public class TreeNodeData
{
public TreeNode Node { get; set; }
public bool Checked { get; set; }
}


private void btncancel_Click_1(object sender, EventArgs e)
{
this.Close();
}

private void RolePower_Load(object sender, EventArgs e)
{
Treeview();
}

 

 

/// <summary>
/// 查询节点方法
/// </summary>
public void Treeview()
{
TreeViews.Nodes.Clear();//清除所有节点
//1.查询所有节点
string sql = string.Format(" select m.Id menuid, m.Name menuname, m.ParentId parentid, m.Icon menuicon, mb.ButtonId buttonid, b.Name buttonname,rmb.MenuId, ");
sql += string.Format(" b.Icon buttonicon,rmb.RoleId roleid,case when isnull(rmb.ButtonId , 0) = 0 then 'false' else 'true' end checked ");
sql += string.Format(" from tbMenu m left join tbMenuButton mb on m.Id=mb.MenuId ");
sql += string.Format(" left join tbButton b on mb.ButtonId=b.Id ");
sql += string.Format(" left join tbRoleMenuButton rmb on(mb.MenuId=rmb.MenuId and mb.ButtonId=rmb.ButtonId and rmb.RoleId ='{0}')", RoleId);
sql += string.Format(" order by m.ParentId,m.Sort,b.Sort");
DataTable dt = Functions.gettable(sql);

DataView dataView = new DataView(dt);
//distinct取不重复的子节点
DataTable dtDistinct = dataView.ToTable(true, new string[] { "menuname", "menuid", "parentid" });
CreateNode(dtDistinct, null, 0,dt);
}

//2.添加节点(递归)
private void CreateNode(DataTable dtDistinct, TreeNode pNode, int parentId, DataTable dt)
{
//1.获取数据
DataTable ds = dt;
//2.获取要创建的节点数据
DataRow[] rows = dtDistinct.Select("ParentId=" + parentId);
DataRow[] rowx = ds.Select("ParentId=" + parentId);
if (rows.Length > 0)
{

foreach (DataRow r in rows)
{
//3.新建子节点
TreeNode node = new TreeNode();
node.Name = r["menuid"].ToString();
node.Text = r["menuname"].ToString();


if (dt.Rows.Count > 0 && dt != null)
{
var buttons = dt.AsEnumerable().Where(p => Convert.ToString(p["MenuId"]) == node.Name);
if (buttons != null && buttons.Count() > 0)
{
List<String> lsButton = buttons.Select(p => Convert.ToString(p["ButtonId"])).ToList();
String sql = string.Format(" select b.*,mb.MenuId,case when isnull(rmb.ButtonId , 0) = 0 then 'false' else 'true' end checked");
sql += string.Format(" from tbMenu m left join tbMenuButton mb on m.Id=mb.MenuId ");
sql += string.Format(" left join tbButton b on mb.ButtonId=b.Id left join tbRoleMenuButton rmb on(mb.MenuId=rmb.MenuId and mb.ButtonId=rmb.ButtonId and rmb.RoleId ='{0}')",RoleId);
sql += string.Format(" where b.Id IN ('{0}') and mb.MenuId='{1}' order by m.ParentId,m.Sort,b.Sort", string.Join("','", lsButton), node.Name);
DataTable dtButton = Functions.gettable(sql);
if (dtButton != null && dtButton.Rows.Count > 0)
{
foreach (DataRow dr in dtButton.Rows)
{
TreeNode buttonNode = new TreeNode();
buttonNode.Name = dr["Id"].ToString();
buttonNode.Text = dr["Name"].ToString();
node.Nodes.Add(buttonNode);
if (Convert.ToBoolean(dr["checked"]) == true)
{
buttonNode.Checked = true;
}
else
{
buttonNode.Checked = false;
}
}
}
}
}


//4.直接添加到TreeView Nodes还是添加指定节点
if (pNode != null)
{
pNode.Nodes.Add(node);
}
else
{
TreeViews.Nodes.Add(node);
}


// 设置Checked属性
bool isNodeInDs = false;
foreach (DataRow dr in rowx)
{
if (dr["menuid"].ToString() == node.Name && Convert.ToBoolean(dr["checked"])==true)
{
isNodeInDs = true;
break;
}
}
node.Checked = isNodeInDs;

//5.判断当前节点下有没有子节点 递归
CreateNode(dtDistinct, node, int.Parse(node.Name), dt);
}
}
}

 

 

}
}

posted @ 2024-01-30 08:47  林新i  阅读(13)  评论(0编辑  收藏  举报