public class Tree:Entity
{
public string id { get; set; }
public string text { get; set; }
public bool leaf { get; set; }
public bool expanded { get; set; }
public string path { get; set; }
public List<Tree> children { get; set; }
}
public Tree GetDeptListTree(string orgname)
{
Tree data = new Tree();
List<Department> list = IDepartmentDao.GetDeptList();
if (!string.IsNullOrEmpty(orgname))
{
List<Department> OrgList = new List<Department>();
string[] ids = list.Where(x => x.ORG_NAME.Contains(orgname)).Select(a => a.ORG_PATH.Trim()).Distinct().ToArray();
string orgids = string.Join(".", ids);
List<Department> filteredlist = list.Where(x => orgids.Contains(x.ORG_ID.ToString()) || x.ORG_NAME.Contains(orgname)).ToList<Department>();
OrgList = OrgList.Union(filteredlist).ToList();
string[] OrgPaths = list.Where(x => x.ORG_NAME.Contains(orgname)).Select(a => (a.ORG_PATH.Trim() + "." + a.ORG_ID)).Distinct().ToArray();
foreach (var OrgPath in OrgPaths)
{
List<Department> LeafOrglist = list.Where(x => x.ORG_PATH.Contains(OrgPath)).ToList<Department>();
OrgList = OrgList.Union(LeafOrglist).ToList();
}
GetTreeChild(OrgList, "0", data, true, true);
}
else
{
GetTreeChild(list, "0", data, true, false);
}
return data;
}
/// <summary>
/// 生成树json数据
/// </summary>
/// <param name="list">数据集</param>
/// <param name="rootid">根id</param>
/// <param name="tree">实力类</param>
/// <param name="leaf">叶子标识</param>
/// <param name="expanded">是否展开</param>
private void GetTreeChild(List<Department> list, string rootid, Tree tree, bool leaf, bool expanded)
{
tree.children = new List<Tree>();
if (list.Count == 0)
return;
List<Tree> treelist = new List<Tree>();
foreach (var item in list)
{
Tree treechild = new Tree();
if (item.ORG_PARENT_ID.ToString() == rootid)
{
treechild.id = item.ORG_CODE.ToString();
treechild.text = item.ORG_NAME.ToString();
treechild.path = item.ORG_PATH.ToString();
treechild.leaf = true;
treechild.expanded = item.ORG_PARENT_ID == 0 ? true : expanded;
tree.children.Add(treechild);
GetTreeChild(list, item.ORG_ID.ToString(), tree.children[tree.children.Count - 1], tree.leaf = false, expanded);
}
}
}