KnowledgeService.cs 9.4 KB

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