123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- using MathNet.Numerics.Distributions;
- using System.Collections.Generic;
- using System.Linq;
- using TEAMModelOS.SDK.Extension;
- namespace TEAMModelOS.SDK.Models.Service
- {
- public sealed class KnowledgeService
- {
- public static KnowledgeTreeDto KnowledgeTranslate(Knowledge knowledge)
- {
- HashSet<string> points = new HashSet<string>();
- List<Block> blocks = new List<Block>();
- blocks.AddRange(knowledge.blocks);
- var pidNull= knowledge.nodes.FindAll(x => string.IsNullOrWhiteSpace(x.pid));
- if (pidNull.IsNotEmpty()) {
- foreach (var isnull in pidNull) {
- isnull.pid= knowledge.id;
- }
- }
- List<PointTree> tree = KnowledgeService.ListToTree(knowledge.nodes);
- foreach (var node in tree)
- {
- points.Add(node.name);
- var blck = blocks.Find(x => x.name.Equals(node.name));
- if (blck!= null)
- {
- blck.points.AddRange(node.children.Select(x => x.name));
- blck.points= blck.points.Distinct().ToList();
- }
- else
- {
- blocks.Add(new Block { name=node.name, source=2, points= node.children.Select(x => x.name).Distinct().ToList() });
- }
- node.pid=knowledge.id;
- node.allcids= KnowledgeService.GetChildIds(node, points, blocks);
- }
-
- knowledge.points.AddRange(points);
- knowledge.points=knowledge.points.Distinct().ToList();
- KnowledgeTreeDto knowledgeTrees = new KnowledgeTreeDto
- {
- id= knowledge.id,
- owner=knowledge.owner,
- scope=knowledge.scope,
- periodId=knowledge.periodId,
- subjectId=knowledge.subjectId,
- name=knowledge.name,
- tree=tree,
- points=knowledge.points,
- pointsFromTree=points.ToList(),
- blocks=blocks,
- };
- return knowledgeTrees;
- }
- public static List<KnowledgeTreeDto> KnowledgeTranslate(List<Knowledge> knowledges)
- {
- List<KnowledgeTreeDto> knowledgeTrees = new List<KnowledgeTreeDto>();
- foreach (var knowledge in knowledges)
- {
- knowledgeTrees.Add(KnowledgeTranslate(knowledge));
- }
- return knowledgeTrees;
- }
- public static List<PointNode> TreeToList(List<PointTree> trees, List<PointNode> nodes, List<Block> blocks,HashSet<string> points)
- {
- List<PointNode> list = new List<PointNode>();
- trees.ForEach(x => {
- List<string> cids = new List<string>();
- if (x.children.IsNotEmpty())
- {
- x.children.ForEach(y => { cids.Add(y.id); /*points.Add(y.name); */y.pid=x.id; });
- }
- var node = new PointNode
- {
- name = x.name,
- id = x.id,
- pid = x.pid,
- cids= cids,
- used =x.used,
- link = x.link,
- level = x.level,
- };
- list.Add(node);
- blocks.Add(new Block { name= x.name, points=x.children.Select(x => x.name).ToList() });
- points.Add(x.name);
- });
-
- nodes.AddRange(list);
- foreach (PointTree tree in trees)
- {
- if (null != tree.children && tree.children.Count > 0)
- {
- TreeToList(tree.children, nodes, blocks, points);
- }
- }
- return nodes;
- }
- public static List<PointTree> ListToTree(List<PointNode> noes)
- {
- List<PointTree> list = noes.ToJsonString().ToObject<List<PointTree>>();
- var res = from r in list group r by r.id into g select g;
- Dictionary<string, PointTree> blockDict = new Dictionary<string, PointTree>();
- foreach (var s in res)
- {
- blockDict.TryAdd(s.First().id, s.First());
- }
- return GetChild(list, blockDict);
- }
- private static List<PointTree> GetChild(List<PointTree> list, Dictionary<string, PointTree> dict)
- {
- // list = list.OrderBy(m => m.Order).ToList();
- List<PointTree> trees = new List<PointTree>();
- foreach (PointTree node in list)
- {
- bool flag = dict.TryGetValue(node.pid, out PointTree syllabus);
- if (flag && syllabus != null)
- {
- syllabus.children.Add(node);
- }
- else
- {
- trees.Add(node);
- }
- }
- return trees;
- }
- static public List<string> GetChildIds(PointTree node,HashSet<string> points, List<Block> blocks)
- {
- List<string> childIds = new List<string>();
- if (node.children != null)
- {
- foreach (PointTree child in node.children)
- {
- childIds.Add(child.id);
- var allcids = GetChildIds(child, points, blocks);
- childIds.AddRange(allcids);
- child.allcids=allcids.Count>100 ? child.children.Select(x => x.id).ToList() : allcids;
- points.Add(child.name);
- if (child.children.IsNotEmpty())
- {
- var blck = blocks.Find(x => x.name.Equals(child.name));
- if (blck!= null)
- {
- blck.points.AddRange(child.children.Select(x => x.name));
- blck.points= blck.points.Distinct().ToList();
- }
- else {
-
- }
- blocks.Add(new Block { name=child.name, source=2, points= child.children.Select(x => x.name).Distinct().ToList() });
- }
- }
- }
- return childIds;
- }
- public static PointTree BuildTree(List<MoofenKnowledgePointDto> pointDtos)
- {
- var root = new PointTree();
- var stack = new Stack<PointTree>();
- stack.Push(root);
- foreach (var pointDto in pointDtos)
- {
- var node = new PointTree
- {
- id = pointDto.id,
- tid = pointDto.id,
- name = pointDto.name,
- level = pointDto.level
- };
- while (stack.Count > 0 && stack.Peek().level >= node.level)
- {
- stack.Pop();
- }
- if (stack.Count > 0)
- {
- stack.Peek().children.Add(node);
- }
- stack.Push(node);
- }
- if (root.level==0 && root.children.IsNotEmpty())
- {
- foreach (var node in root.children)
- {
- node.subcids=node.children.Select(x => x.tid).ToList();
- var cids = GetChildIds(node);
- node.allcids=cids.Count>100 ? new List<string>() : cids;
- }
- }
- return root;
- }
- static List<string> GetChildIds(PointTree node)
- {
- List<string> childIds = new List<string>();
- if (node.children != null)
- {
- foreach (PointTree child in node.children)
- {
- childIds.Add(child.tid);
- var cids = GetChildIds(child);
- childIds.AddRange(cids);
- child.allcids=cids.Count>100 ? new List<string>() : cids;
- child.subcids=child.children.Select(x => x.tid).ToList();
- }
- }
- return childIds;
- }
- }
- //public class TreeNode
- //{
- // public string name { get; set; }
- // public string id { get; set; }
- // /// <summary>
- // /// 第三方id
- // /// </summary>
- // public string tid { get; set; }
- // public int level { get; set; }
- // /// <summary>
- // /// 直接子节点
- // /// </summary>
- // public List<string> subcids { get; set; } = new List<string>();
- // /// <summary>
- // /// 所有子节点
- // /// </summary>
- // public List<string> allcids { get; set; } = new List<string>();
- // public List<PointTree> children { get; set; } = new List<PointTree>();
- //}
- public class MoofenKnowledgePointDto
- {
- public string id { get; set; }
- public string name { get; set; }
- public int level { get; set; }
- public string kp1 { get; set; }
- public string kp2 { get; set; }
- public string kp3 { get; set; }
- public string kp4 { get; set; }
- }
- public class MoofenQuestion
- {
- public int difficulty { get; set; }
- public string questionCode { get; set; }
- public MoofenScope scope { get; set; }
- public List<MoofenOption> options { get; set; }
- public MoofenType @type { get; set; }
- public string trunk { get; set; }
- public List<MoofenQuestion> questions { get; set; }
- /// <summary>
- /// 填空题的答案
- /// </summary>
- public List<MoofenFill> items { get; set; }
- /// <summary>
- /// 问答题的答案
- /// </summary>
- public string content { get; set; }
- public List<MoofenKnowledgePoint> kps { get; set; }
- }
- public class MoofenFill
- {
- public string content { get; set; }
- }
- public class MoofenScope
- {
- public MoofenGrade grade { get; set; }
- }
- public class MoofenGrade
- {
- public string code { get; set; }
- public string name { get; set; }
- }
- public class MoofenOption
- {
- public bool correct { get; set; }
- public string label { get; set; }
- public string content { get; set; }
- }
- public class MoofenType
- {
- public string code { get; set; }
- public string name { get; set; }
- }
- public class MoofenKnowledgePoint
- {
- public string kpCode { get; set; }
- public string kpId { get; set; }
- public string kpSetId { get; set; }
- public string kpName { get; set; }
- public string subCode { get; set; }
- public int kpLevel { get; set; }
- public string id { get; set; }
- public string parKpId { get; set; }
- }
- }
|