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.SDK.Services
{
public static class SyllabusService
{
///
/// 根据id查询列表串联id pid的新的关系列表
///
///
///
///
///
public static HashSet GetNewNode(List nodes, string pid, HashSet 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 TreeToList(List trees, List nodes,long now)
{
int index = 0;
foreach (SyllabusTree tr in trees)
{
tr.order = index;
index++;
}
trees = trees.OrderBy(x => x.order).ToList();
List list = new List();
//var list = trees.ToJsonString().ToObject>();
trees.ForEach(x=> {
List cids = new List();
if (x.children.IsNotEmpty()) {
x.children.ForEach(y => cids.Add(y.id));
}
var node = new Tnode
{
title = x.title,
id = x.id,
pid = x.pid,
order = x.order,
rnodes = x.rnodes,
cids= cids,
creatorId=x.creatorId,
creatorName=x.creatorName,
updateTime= now
};
list.Add(node);
});
nodes.AddRange(list);
foreach (SyllabusTree tree in trees)
{
if (null != tree.children && tree.children.Count > 0)
{
TreeToList(tree.children, nodes,now);
}
}
return nodes;
}
public static List ListToTree(List noes)
{
List list = noes.ToJsonString().ToObject>();
//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 blockDict = new Dictionary();
foreach (var s in res)
{
blockDict.TryAdd(s.First().id, s.First());
}
return GetChild(list, blockDict);
}
private static List GetChild(List list, Dictionary dict)
{
// list = list.OrderBy(m => m.Order).ToList();
List trees = new List();
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;
}
#region 开放平台使用
public static List OListToTree(List noes)
{
List list = noes.ToJsonString().ToObject>();
//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 blockDict = new Dictionary();
foreach (var s in res)
{
blockDict.TryAdd(s.First().id, s.First());
}
return GetChildO(list, blockDict);
}
private static List GetChildO(List list, Dictionary dict)
{
// list = list.OrderBy(m => m.Order).ToList();
List trees = new List();
trees = trees.OrderBy(x => x.order).ToList();
foreach (OSyllabusTree node in list)
{
bool flag = dict.TryGetValue(node.pid, out OSyllabusTree syllabus);
if (flag && syllabus != null)
{
syllabus.children.Add(node);
}
else
{
trees.Add(node);
}
}
return trees;
}
#endregion
}
}