public IActionResult GetOrgStructure(OrganisationalStructureLookup reference) { List <OrgStructure> data = new List <OrgStructure>(); OrgStructure rootTree = null; List <AssetNodeVM> assetNodes = new AssetNodeService(_context, _contextUsers).GetAssetNodeVMs(reference); AssetNodeVM assetNodeVM = assetNodes.FirstOrDefault(id => id.Height == 1); if (assetNodeVM == null) { if (assetNodes.Count > 0) { int firstHeight = assetNodes[0].Height - 1; List <AssetNodeVM> assetNodesTemp = new List <AssetNodeVM>(); int flyRoot = assetNodes[0].AssetNodeId; foreach (var an in assetNodes) { AssetNodeVM assetNodeVM1 = new AssetNodeVM(); assetNodeVM1 = an; assetNodeVM1.Height = assetNodeVM1.Height - firstHeight; assetNodeVM1.RootAssetNodeId = flyRoot; assetNodesTemp.Add(assetNodeVM1); } assetNodes = new List <AssetNodeVM>(); assetNodes = assetNodesTemp; } } foreach (var asd in assetNodes.Where(a => a.Height == 1)) { rootTree = new OrgStructure(); rootTree.Name = asd.Name; rootTree.NodeId = asd.AssetNodeId; rootTree.Subordinates = new List <OrgStructure>(); rootTree.Designation = "(" + asd.Size + ")"; data.Add(rootTree); var allChildrens = assetNodes.Where(a => a.RootAssetNodeId == asd.AssetNodeId) .OrderBy(p => p.Height).OrderBy(p => p.ParentAssetNodeId).ToList(); if (allChildrens.Count > 0) { var maxHeight = allChildrens.Max(a => a.Height); OrgStructure currntParent = rootTree; List <OrgStructure> parents = new List <OrgStructure>(); for (int i = 2; i <= maxHeight; i++) { OrgStructure treeBranch = null; OrgStructure currentParent = null; var t = allChildrens.OrderBy(h => h.Height).ToList(); var parentIds = t.Where(a => a.Height == i).Select(a => a.ParentAssetNodeId).Distinct().ToList(); foreach (var id in parentIds) { foreach (var child in t.Where(a => a.Height == i && a.ParentAssetNodeId == id)) { treeBranch = new OrgStructure(); treeBranch.Name = child.Name; treeBranch.NodeId = child.NodeId; treeBranch.Subordinates = new List <OrgStructure>(); treeBranch.Designation = "(" + child.Size + ")"; parents.Add(treeBranch); if (child.ParentAssetNodeId != rootTree.NodeId) { currentParent = parents.FirstOrDefault(a => a.NodeId == child.ParentAssetNodeId); } if (currentParent != null) { currentParent.Subordinates.Add(treeBranch); } else { rootTree.Subordinates.Add(treeBranch); } } if (currentParent != null) { rootTree = currentParent; } else { rootTree = treeBranch; } } } } } return(Ok(data)); }
public IActionResult GetAssetNodeTree(OrganisationalStructureLookup reference) { List <Tree1> data = new List <Tree1>(); Tree1 rootTree = null; List <AssetNodeVM> assetNodes = new AssetNodeService(_context, _contextUsers).GetAssetNodeVMs(reference); AssetNodeVM assetNodeVM = assetNodes.FirstOrDefault(id => id.Height == 1); if (assetNodeVM == null) { if (assetNodes.Count > 0) { int firstHeight = assetNodes[0].Height - 1; List <AssetNodeVM> assetNodesTemp = new List <AssetNodeVM>(); int flyRoot = assetNodes[0].AssetNodeId; foreach (var an in assetNodes) { AssetNodeVM assetNodeVM1 = new AssetNodeVM(); assetNodeVM1 = an; assetNodeVM1.Height = assetNodeVM1.Height - firstHeight; assetNodeVM1.RootAssetNodeId = flyRoot; assetNodesTemp.Add(assetNodeVM1); } assetNodes = new List <AssetNodeVM>(); assetNodes = assetNodesTemp; } } foreach (var asd in assetNodes.Where(a => a.Height == 1)) { rootTree = new Tree1(); rootTree.Name = string.Format("{0} ({1})", asd.Name, asd.Size); rootTree.Children = new List <Tree1>(); rootTree.NodeId = asd.AssetNodeId; rootTree.ParentNodeId = asd.ParentAssetNodeId; //rootTree.NodeType = asd.NodeType; rootTree.Checked = false; data.Add(rootTree); var allChildrens = assetNodes.Where(a => a.RootAssetNodeId == asd.AssetNodeId) .OrderBy(p => p.Height).OrderBy(p => p.ParentAssetNodeId).ToList(); if (allChildrens.Count > 0) { var maxHeight = allChildrens.Max(a => a.Height); Tree1 currntParent = rootTree; List <Tree1> parents = new List <Tree1>(); for (int i = 2; i <= maxHeight; i++) { Tree1 treeBranch = null; Tree1 currentParent = null; var t = allChildrens.OrderBy(h => h.Height).ToList(); var parentIds = t.Where(a => a.Height == i).Select(a => a.ParentAssetNodeId).Distinct().ToList(); foreach (var id in parentIds) { foreach (var child in t.Where(a => a.Height == i && a.ParentAssetNodeId == id)) { treeBranch = new Tree1(); treeBranch.NodeId = child.AssetNodeId; treeBranch.ParentNodeId = child.ParentAssetNodeId; //treeBranch.NodeType = child.NodeType; treeBranch.Name = string.Format("{0} ({1})", child.Name, child.Size); treeBranch.Children = new List <Tree1>(); treeBranch.Checked = false; parents.Add(treeBranch); if (child.ParentAssetNodeId != rootTree.NodeId) { currentParent = parents.FirstOrDefault(a => a.NodeId == child.ParentAssetNodeId); } if (currentParent != null) { currentParent.Children.Add(treeBranch); } else { rootTree.Children.Add(treeBranch); } } if (currentParent != null) { rootTree = currentParent; } else { rootTree = treeBranch; } } } } } return(Ok(data)); }