KnowledgeService.cs 9.8 KB

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