123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- using HTEXLib.COMM.Helpers;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using TEAMModelOS.SDK.Extension;
- using TEAMModelOS.SDK.Models;
- using TEAMModelOS.SDK.Models.Cosmos.Common;
- namespace TEAMModelOS.Services.Common
- {
- public static class SyllabusService
- {
- /// <summary>
- /// 根据id查询列表串联id pid的新的关系列表
- /// </summary>
- /// <param name="nodes"></param>
- /// <param name="pid"></param>
- /// <param name="newNodes"></param>
- /// <returns></returns>
- public static HashSet<Tnode> GetNewNode(List<Tnode> nodes, string pid, HashSet<Tnode> newNodes) {
- bool flag = false;
- string spid = "";
- foreach (var node in nodes) {
- if (node.pid == pid) {
- newNodes.Add(node);
- spid = node.id;
- GetNewNode(nodes, spid, newNodes);
- flag = true;
- }
- }
- return newNodes;
- //if (flag)
- //{
- // return GetNewNode(nodes, spid, newNodes);
- //}
- //else {
- // return newNodes;
- //}
- }
- public static List<Tnode> TreeToList(List<SyllabusTree> trees, List<Tnode> nodes)
- {
- int index = 0;
- foreach (SyllabusTree tr in trees)
- {
- tr.order = index;
- index++;
- }
- trees = trees.OrderBy(x => x.order).ToList();
- List<Tnode> list = new List<Tnode>();
- //var list = trees.ToJsonString().ToObject<List<Tnode>>();
- trees.ForEach(x=> {
- List<string> cids = new List<string>();
- if (x.children.IsNotEmpty()) {
- x.children.ForEach(y => cids.Add(y.id));
- }
- var node = new Tnode
- {
- type = x.type,
- title = x.title,
- id = x.id,
- pid = x.pid,
- order = x.order,
- rnodes = x.rnodes,
- cids= cids
- };
- list.Add(node);
- });
- nodes.AddRange(list);
- foreach (SyllabusTree tree in trees)
- {
- if (null != tree.children && tree.children.Count > 0)
- {
- TreeToList(tree.children, nodes);
- }
- }
- return nodes;
- }
- public static List<SyllabusTree> ListToTree(List<Tnode> noes)
- {
- List<SyllabusTree> list = noes.ToJsonString().ToObject<List<SyllabusTree>>();
- //var lookup = list.ToDictionary(n => n.RowKey, n => n);
- var res = from r in list group r by r.id into g select g;
- Dictionary<string, SyllabusTree> blockDict = new Dictionary<string, SyllabusTree>();
- foreach (var s in res)
- {
- blockDict.TryAdd(s.First().id, s.First());
- }
- return GetChild(list, blockDict);
- }
- private static List<SyllabusTree> GetChild(List<SyllabusTree> list, Dictionary<string, SyllabusTree> dict)
- {
- // list = list.OrderBy(m => m.Order).ToList();
- List<SyllabusTree> trees = new List<SyllabusTree>();
- trees = trees.OrderBy(x => x.order).ToList();
- foreach (SyllabusTree node in list)
- {
- bool flag = dict.TryGetValue(node.pid, out SyllabusTree syllabus);
- if (flag && syllabus != null)
- {
- syllabus.children.Add(node);
- }
- else
- {
- trees.Add(node);
- }
- }
- return trees;
- }
- }
- }
|