KnowledgeService.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. using MathNet.Numerics.Distributions;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using TEAMModelOS.SDK.Extension;
  5. namespace TEAMModelOS.SDK.Models.Service
  6. {
  7. public sealed class KnowledgeService
  8. {
  9. public static KnowledgeTreeDto KnowledgeTranslate(Knowledge knowledge)
  10. {
  11. HashSet<string> points = new HashSet<string>();
  12. List<Block> blocks = new List<Block>();
  13. blocks.AddRange(knowledge.blocks);
  14. var pidNull= knowledge.nodes.FindAll(x => string.IsNullOrWhiteSpace(x.pid));
  15. if (pidNull.IsNotEmpty()) {
  16. foreach (var isnull in pidNull) {
  17. isnull.pid= knowledge.id;
  18. }
  19. }
  20. List<PointTree> tree = KnowledgeService.ListToTree(knowledge.nodes);
  21. foreach (var node in tree)
  22. {
  23. points.Add(node.name);
  24. var blck = blocks.Find(x => x.name.Equals(node.name));
  25. if (blck!= null)
  26. {
  27. blck.points.AddRange(node.children.Select(x => x.name));
  28. blck.points= blck.points.Distinct().ToList();
  29. }
  30. else
  31. {
  32. blocks.Add(new Block { name=node.name, source=2, points= node.children.Select(x => x.name).Distinct().ToList() });
  33. }
  34. node.pid=knowledge.id;
  35. node.allcids= KnowledgeService.GetChildIds(node, points, blocks);
  36. }
  37. knowledge.points.AddRange(points);
  38. knowledge.points=knowledge.points.Distinct().ToList();
  39. KnowledgeTreeDto knowledgeTrees = new KnowledgeTreeDto
  40. {
  41. id= knowledge.id,
  42. owner=knowledge.owner,
  43. scope=knowledge.scope,
  44. periodId=knowledge.periodId,
  45. subjectId=knowledge.subjectId,
  46. name=knowledge.name,
  47. tree=tree,
  48. points=knowledge.points,
  49. pointsFromTree=points.ToList(),
  50. blocks=blocks,
  51. };
  52. return knowledgeTrees;
  53. }
  54. public static List<KnowledgeTreeDto> KnowledgeTranslate(List<Knowledge> knowledges)
  55. {
  56. List<KnowledgeTreeDto> knowledgeTrees = new List<KnowledgeTreeDto>();
  57. foreach (var knowledge in knowledges)
  58. {
  59. knowledgeTrees.Add(KnowledgeTranslate(knowledge));
  60. }
  61. return knowledgeTrees;
  62. }
  63. public static List<PointNode> TreeToList(List<PointTree> trees, List<PointNode> nodes, List<Block> blocks,HashSet<string> points)
  64. {
  65. List<PointNode> list = new List<PointNode>();
  66. trees.ForEach(x => {
  67. List<string> cids = new List<string>();
  68. if (x.children.IsNotEmpty())
  69. {
  70. x.children.ForEach(y => { cids.Add(y.id); /*points.Add(y.name); */y.pid=x.id; });
  71. }
  72. var node = new PointNode
  73. {
  74. name = x.name,
  75. id = x.id,
  76. pid = x.pid,
  77. cids= cids,
  78. used =x.used,
  79. link = x.link,
  80. level = x.level,
  81. };
  82. list.Add(node);
  83. blocks.Add(new Block { name= x.name, points=x.children.Select(x => x.name).ToList() });
  84. points.Add(x.name);
  85. });
  86. nodes.AddRange(list);
  87. foreach (PointTree tree in trees)
  88. {
  89. if (null != tree.children && tree.children.Count > 0)
  90. {
  91. TreeToList(tree.children, nodes, blocks, points);
  92. }
  93. }
  94. return nodes;
  95. }
  96. public static List<PointTree> ListToTree(List<PointNode> noes)
  97. {
  98. List<PointTree> list = noes.ToJsonString().ToObject<List<PointTree>>();
  99. var res = from r in list group r by r.id into g select g;
  100. Dictionary<string, PointTree> blockDict = new Dictionary<string, PointTree>();
  101. foreach (var s in res)
  102. {
  103. blockDict.TryAdd(s.First().id, s.First());
  104. }
  105. return GetChild(list, blockDict);
  106. }
  107. private static List<PointTree> GetChild(List<PointTree> list, Dictionary<string, PointTree> dict)
  108. {
  109. // list = list.OrderBy(m => m.Order).ToList();
  110. List<PointTree> trees = new List<PointTree>();
  111. foreach (PointTree node in list)
  112. {
  113. bool flag = dict.TryGetValue(node.pid, out PointTree syllabus);
  114. if (flag && syllabus != null)
  115. {
  116. syllabus.children.Add(node);
  117. }
  118. else
  119. {
  120. trees.Add(node);
  121. }
  122. }
  123. return trees;
  124. }
  125. static public List<string> GetChildIds(PointTree node,HashSet<string> points, List<Block> blocks)
  126. {
  127. List<string> childIds = new List<string>();
  128. if (node.children != null)
  129. {
  130. foreach (PointTree child in node.children)
  131. {
  132. childIds.Add(child.id);
  133. var allcids = GetChildIds(child, points, blocks);
  134. childIds.AddRange(allcids);
  135. child.allcids=allcids.Count>100 ? child.children.Select(x => x.id).ToList() : allcids;
  136. points.Add(child.name);
  137. if (child.children.IsNotEmpty())
  138. {
  139. var blck = blocks.Find(x => x.name.Equals(child.name));
  140. if (blck!= null)
  141. {
  142. blck.points.AddRange(child.children.Select(x => x.name));
  143. blck.points= blck.points.Distinct().ToList();
  144. }
  145. else {
  146. }
  147. blocks.Add(new Block { name=child.name, source=2, points= child.children.Select(x => x.name).Distinct().ToList() });
  148. }
  149. }
  150. }
  151. return childIds;
  152. }
  153. public static PointTree BuildTree(List<MoofenKnowledgePointDto> pointDtos)
  154. {
  155. var root = new PointTree();
  156. var stack = new Stack<PointTree>();
  157. stack.Push(root);
  158. foreach (var pointDto in pointDtos)
  159. {
  160. var node = new PointTree
  161. {
  162. id = pointDto.id,
  163. tid = pointDto.id,
  164. name = pointDto.name,
  165. level = pointDto.level
  166. };
  167. while (stack.Count > 0 && stack.Peek().level >= node.level)
  168. {
  169. stack.Pop();
  170. }
  171. if (stack.Count > 0)
  172. {
  173. stack.Peek().children.Add(node);
  174. }
  175. stack.Push(node);
  176. }
  177. if (root.level==0 && root.children.IsNotEmpty())
  178. {
  179. foreach (var node in root.children)
  180. {
  181. node.subcids=node.children.Select(x => x.tid).ToList();
  182. var cids = GetChildIds(node);
  183. node.allcids=cids.Count>100 ? new List<string>() : cids;
  184. }
  185. }
  186. return root;
  187. }
  188. static List<string> GetChildIds(PointTree node)
  189. {
  190. List<string> childIds = new List<string>();
  191. if (node.children != null)
  192. {
  193. foreach (PointTree child in node.children)
  194. {
  195. childIds.Add(child.tid);
  196. var cids = GetChildIds(child);
  197. childIds.AddRange(cids);
  198. child.allcids=cids.Count>100 ? new List<string>() : cids;
  199. child.subcids=child.children.Select(x => x.tid).ToList();
  200. }
  201. }
  202. return childIds;
  203. }
  204. }
  205. //public class TreeNode
  206. //{
  207. // public string name { get; set; }
  208. // public string id { get; set; }
  209. // /// <summary>
  210. // /// 第三方id
  211. // /// </summary>
  212. // public string tid { get; set; }
  213. // public int level { get; set; }
  214. // /// <summary>
  215. // /// 直接子节点
  216. // /// </summary>
  217. // public List<string> subcids { get; set; } = new List<string>();
  218. // /// <summary>
  219. // /// 所有子节点
  220. // /// </summary>
  221. // public List<string> allcids { get; set; } = new List<string>();
  222. // public List<PointTree> children { get; set; } = new List<PointTree>();
  223. //}
  224. public class MoofenKnowledgePointDto
  225. {
  226. public string id { get; set; }
  227. public string name { get; set; }
  228. public int level { get; set; }
  229. public string kp1 { get; set; }
  230. public string kp2 { get; set; }
  231. public string kp3 { get; set; }
  232. public string kp4 { get; set; }
  233. }
  234. public class MoofenQuestion
  235. {
  236. public int difficulty { get; set; }
  237. public string questionCode { get; set; }
  238. public MoofenScope scope { get; set; }
  239. public List<MoofenOption> options { get; set; }
  240. public MoofenType @type { get; set; }
  241. public string trunk { get; set; }
  242. public List<MoofenQuestion> questions { get; set; }
  243. /// <summary>
  244. /// 填空题的答案
  245. /// </summary>
  246. public List<MoofenFill> items { get; set; }
  247. /// <summary>
  248. /// 问答题的答案
  249. /// </summary>
  250. public string content { get; set; }
  251. public List<MoofenKnowledgePoint> kps { get; set; }
  252. }
  253. public class MoofenFill
  254. {
  255. public string content { get; set; }
  256. }
  257. public class MoofenScope
  258. {
  259. public MoofenGrade grade { get; set; }
  260. }
  261. public class MoofenGrade
  262. {
  263. public string code { get; set; }
  264. public string name { get; set; }
  265. }
  266. public class MoofenOption
  267. {
  268. public bool correct { get; set; }
  269. public string label { get; set; }
  270. public string content { get; set; }
  271. }
  272. public class MoofenType
  273. {
  274. public string code { get; set; }
  275. public string name { get; set; }
  276. }
  277. public class MoofenKnowledgePoint
  278. {
  279. public string kpCode { get; set; }
  280. public string kpId { get; set; }
  281. public string kpSetId { get; set; }
  282. public string kpName { get; set; }
  283. public string subCode { get; set; }
  284. public int kpLevel { get; set; }
  285. public string id { get; set; }
  286. public string parKpId { get; set; }
  287. }
  288. }