KnowledgeService.cs 9.9 KB

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