KgFacade.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package com.diagbot.facade;
  2. import com.diagbot.dto.BaseNodeRSDTO;
  3. import com.diagbot.dto.Categories;
  4. import com.diagbot.dto.GraDTO;
  5. import com.diagbot.dto.GraphDTO;
  6. import com.diagbot.dto.GraphLabelDTO;
  7. import com.diagbot.dto.Link;
  8. import com.diagbot.dto.Node;
  9. import com.diagbot.dto.TreeDTO;
  10. import com.diagbot.dto.Tu;
  11. import com.diagbot.exception.CommonErrorCode;
  12. import com.diagbot.exception.CommonException;
  13. import com.diagbot.service.impl.KgServiceImpl;
  14. import com.diagbot.util.EntityUtil;
  15. import com.diagbot.util.FastJsonUtils;
  16. import com.diagbot.util.ListUtil;
  17. import com.diagbot.vo.KgQueryVO;
  18. import com.diagbot.vo.KgTreeVO;
  19. import org.springframework.stereotype.Component;
  20. import java.util.ArrayList;
  21. import java.util.LinkedHashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. /**
  25. * @Description: 用户日志业务层
  26. * @author: gaodm
  27. * @time: 2018/8/6 9:11
  28. */
  29. @Component
  30. public class KgFacade extends KgServiceImpl {
  31. public GraphLabelDTO getGraphFac(KgQueryVO kgQueryVO) {
  32. GraphLabelDTO graphLabelDTO = new GraphLabelDTO();
  33. List<GraphDTO> res = this.getGraph(kgQueryVO);
  34. if (ListUtil.isEmpty(res)) {
  35. throw new CommonException(CommonErrorCode.NOT_EXISTS);
  36. } else {
  37. List<String> allRs = new ArrayList<>();
  38. for (GraphDTO graphDTO : res) {
  39. if (ListUtil.isNotEmpty(graphDTO.getENodeRSDTOS())) {
  40. for (BaseNodeRSDTO baseNodeRSDTO : graphDTO.getENodeRSDTOS()) {
  41. if (!allRs.contains(baseNodeRSDTO.getRType())) {
  42. allRs.add(baseNodeRSDTO.getRType());
  43. }
  44. }
  45. }
  46. }
  47. graphLabelDTO.setAllRs(allRs);
  48. graphLabelDTO.setGraphDTOS(res);
  49. }
  50. return graphLabelDTO;
  51. }
  52. public TreeDTO getTreeFac(KgTreeVO kgTreeVO) {
  53. TreeDTO treeDTO = new TreeDTO();
  54. if (kgTreeVO.getType() == 1 && kgTreeVO.getSubType() == 1) {
  55. treeDTO = FastJsonUtils.getJsonToBean(jsonStr, TreeDTO.class);
  56. } else {
  57. throw new CommonException(CommonErrorCode.PARAM_IS_ERROR);
  58. }
  59. return treeDTO;
  60. }
  61. private static final String jsonStr = "{\n" +
  62. " \"icdCode\": \"1\",\n" +
  63. " \"label\": null,\n" +
  64. " \"name\": \"疾病\",\n" +
  65. " \"snode\": [\n" +
  66. " {\n" +
  67. "\t\t\"icdCode\": \"A\",\n" +
  68. "\t\t\"label\": \"\",\n" +
  69. "\t\t\"name\": \"A\",\n" +
  70. "\t\t\"snode\": [\n" +
  71. "\t\t {\n" +
  72. "\t\t\t\"icdCode\": \"A1\",\n" +
  73. "\t\t\t\"label\": \"\",\n" +
  74. "\t\t\t\"name\": \"A1\",\n" +
  75. "\t\t\t\"snode\": [\n" +
  76. "\t\t\t null\n" +
  77. "\t\t\t]\n" +
  78. "\t\t },\n" +
  79. "\t\t {\n" +
  80. "\t\t\t\"icdCode\": \"A2\",\n" +
  81. "\t\t\t\"label\": \"\",\n" +
  82. "\t\t\t\"name\": \"A2\",\n" +
  83. "\t\t\t\"snode\": [\n" +
  84. "\t\t\t null\n" +
  85. "\t\t\t]\n" +
  86. "\t\t }\n" +
  87. "\t\t]\n" +
  88. "\t },\n" +
  89. "\t {\n" +
  90. "\t\t\"icdCode\": \"B\",\n" +
  91. "\t\t\"label\": \"\",\n" +
  92. "\t\t\"name\": \"B\",\n" +
  93. "\t\t\"snode\": [\n" +
  94. "\t\t null\n" +
  95. "\t\t]\n" +
  96. "\t }\n" +
  97. " ]\n" +
  98. "}";
  99. private void addNode(TreeDTO treeDTO) {
  100. }
  101. public GraDTO getTuFac(KgQueryVO kgQueryVO) {
  102. GraDTO graDTO = new GraDTO();
  103. List<Tu> tu = this.getTu(kgQueryVO);
  104. Map<String, List<Tu>> map = EntityUtil.makeEntityListMap(tu, "rel");
  105. List<Categories> categoriesList = new ArrayList<>();
  106. List<Node> nodeList = new ArrayList<>();
  107. List<Link> linksList = new ArrayList<>();
  108. // 添加第一个节点:中心节点
  109. Node node = new Node(0, kgQueryVO.getInputStr(), 0L, 50);
  110. nodeList.add(node);
  111. // 设置categories
  112. Map<String, Integer> relMap = new LinkedHashMap<>();
  113. int i = 0; // 设置category
  114. Categories c1 = new Categories("中心词");
  115. categoriesList.add(c1);
  116. relMap.put("中心词", i++);
  117. Categories c2 = new Categories("关系");
  118. categoriesList.add(c2);
  119. relMap.put("关系", i++);
  120. Long j = 1L; // 设置节点编号
  121. for (String key : map.keySet()) {
  122. Categories bean = new Categories(key);
  123. categoriesList.add(bean);
  124. relMap.put(key, i);
  125. // 添加关系节点
  126. Node nodeRel = new Node(1, "", j, 10);
  127. nodeList.add(nodeRel);
  128. // 添加中心节点与关系的关联
  129. Link linkRel = new Link(0L, j, key);
  130. linksList.add(linkRel);
  131. List<Tu> inner = map.get(key);
  132. for (Tu t : inner) {
  133. // 添加关系下的节点
  134. Node nodeLabel = new Node(i, t.getSubName(), ++j, 28);
  135. nodeList.add(nodeLabel);
  136. // 添加关系和节点的关联
  137. Link linkLabel = new Link(linkRel.getTarget(), j, "");
  138. linksList.add(linkLabel);
  139. }
  140. i++;
  141. j++;
  142. }
  143. graDTO.setCategoriesList(categoriesList);
  144. graDTO.setNodeList(nodeList);
  145. graDTO.setLinksList(linksList);
  146. return graDTO;
  147. }
  148. }