using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CommonTool { public class FunSettingTree { public static FunSettingTree Inatance = new FunSettingTree(); public FunctionTree CreatFunctionTree(List pcFunctionTrees) { FunctionTree loRoot = null; var lcFunctionTrees = pcFunctionTrees.Where(a => a.FatherId == "0").ToList(); if (lcFunctionTrees.Count() == 1) { loRoot = lcFunctionTrees.FirstOrDefault(); if (loRoot != null) { loRoot.ChildNodes = pcFunctionTrees.Where(a => a.FatherId == loRoot.Id && a.FatherId != "0").ToList(); if (loRoot.ChildNodes.Any()) { foreach (FunctionTree tree in loRoot.ChildNodes) { CreatFunctionTree(pcFunctionTrees, tree.Id); } } } } return loRoot; } public FunctionTree CreatFunctionTree(List pcFunctionTrees, string id) { FunctionTree loRoot = null; var lcFunctionTrees = pcFunctionTrees.Where(a => a.Id == id && a.FatherId != "0").ToList(); if (lcFunctionTrees.Count() == 1) { loRoot = lcFunctionTrees.FirstOrDefault(); if (loRoot != null) { loRoot.ChildNodes = pcFunctionTrees.Where(a => a.FatherId == loRoot.Id && a.FatherId != "0").ToList(); if (loRoot.ChildNodes.Any()) { foreach (FunctionTree tree in loRoot.ChildNodes) { CreatFunctionTree(pcFunctionTrees, tree.Id); } } } } return loRoot; } } public class FunctionTree { public string Id { get; set; } public string FatherId { get; set; } public string FunctionName { get; set; } public string ActionName { get; set; } public string ControllerName { get; set; } public string Url { get; set; } //public int Depth { get; set; } //public string IsLeaf { get; set; } //public int Sort { get; set; } public List ChildNodes { get; set; } public string ToJason() { StringBuilder sb = new StringBuilder(); if (ChildNodes.Count > 0) { if (FatherId != "0") { sb.AppendFormat( "
  • {0}
      ", FunctionName); foreach (FunctionTree loChild in ChildNodes) { sb.Append(loChild.ToJason()); } sb.AppendFormat("
  • "); } else { foreach (FunctionTree loChild in ChildNodes) { sb.Append(loChild.ToJason()); } } } else { if (string.IsNullOrEmpty(ActionName)) Url = "#"; sb.AppendFormat("
  • {1}
  • ", Url, FunctionName); } return sb.ToString(); } //public string ToJbson() //{ // StringBuilder sb = new StringBuilder(); // if (ChildNodes != null) // { // foreach (FunctionTree loChild in ChildNodes) // { // sb.Append(loChild.ToJason()); // } // } // else // { // if (string.IsNullOrEmpty(ActionName)) // Url = "#"; // sb.AppendFormat("
  • {1}
  • ", Url, FunctionName); // } // return sb.ToString(); //} } }