KnowledgeService.cs 9.7 KB

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