Jelajahi Sumber

1、local数据源更改
2、树形接口数据编辑更新

yuchengwei 5 bulan lalu
induk
melakukan
9b3cd6fff3

+ 17 - 0
src/main/java/com/qizhen/healsphere/config/JacksonConfig.java

@@ -0,0 +1,17 @@
+package com.qizhen.healsphere.config;
+
+import com.fasterxml.jackson.databind.MapperFeature;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+public class JacksonConfig {
+    
+    @Bean
+    public ObjectMapper objectMapper() {
+        ObjectMapper mapper = new ObjectMapper();
+        mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
+        return mapper;
+    }
+} 

+ 44 - 7
src/main/java/com/qizhen/healsphere/repository/neo4j/BaseNodeRepository.java

@@ -203,9 +203,6 @@ public class BaseNodeRepository {
                 throw new RuntimeException("One or both nodes not found");
             }
             
-            Node a = aResult.single().get("a").asNode();
-            Node b = bResult.single().get("b").asNode();
-
             // 创建新节点
             Map<String, Object> newNodeProps = new HashMap<>();
             newNodeProps.put("name", mergeNodeParam.getNewName());
@@ -217,21 +214,36 @@ public class BaseNodeRepository {
             long newNodeId = newNode.id();
 
             // 合并关系 - 修改这部分代码
+            // 合并关系 - 保持原始方向
             String firstRelationQuery = String.format(
-                "MATCH (a:`%s`)-[r]-(other) WHERE id(a) = $firstId " +
+                "MATCH (a:`%s`)-[r]->(other) WHERE id(a) = $firstId " +  // 出向关系
+                "WITH r, other, type(r) as relType, properties(r) as props " +
+                "MATCH (n) WHERE id(n) = $newNodeId " +
+                            "MERGE (n)-[newRel:relType]->(other) " +  // 创建出向关系
+                "SET newRel += props " +
+                "UNION ALL " +
+                "MATCH (a:`%s`)<-[r]-(other) WHERE id(a) = $firstId " +  // 入向关系
                 "WITH r, other, type(r) as relType, properties(r) as props " +
                 "MATCH (n) WHERE id(n) = $newNodeId " +
-                "MERGE (n)-[newRel:`${type(r)}`]->(other) " +
+                            "MERGE (n)<-[newRel:relType]-(other) " +  // 创建入向关系
                 "SET newRel += props", 
+                mergeNodeParam.getFirstLabel(),
                 mergeNodeParam.getFirstLabel()
             );
             
             String secondRelationQuery = String.format(
-                "MATCH (b:`%s`)-[r]-(other) WHERE id(b) = $secondId " +
+                "MATCH (b:`%s`)-[r]->(other) WHERE id(b) = $secondId " +  // 出向关系
                 "WITH r, other, type(r) as relType, properties(r) as props " +
                 "MATCH (n) WHERE id(n) = $newNodeId " +
-                "MERGE (n)-[newRel:`${type(r)}`]->(other) " +
+                            "MERGE (n)-[newRel:relType]->(other) " +  // 创建出向关系
+                "SET newRel += props " +
+                "UNION ALL " +
+                "MATCH (b:`%s`)<-[r]-(other) WHERE id(b) = $secondId " +  // 入向关系
+                "WITH r, other, type(r) as relType, properties(r) as props " +
+                "MATCH (n) WHERE id(n) = $newNodeId " +
+                            "MERGE (n)<-[newRel:relType]-(other) " +  // 创建入向关系
                 "SET newRel += props",
+                mergeNodeParam.getSecondLabel(),
                 mergeNodeParam.getSecondLabel()
             );
 
@@ -311,4 +323,29 @@ public class BaseNodeRepository {
         baseEntity.setName(node.get("name").asString());
         return baseEntity;
     }
+
+    /**
+     * 获取所有科室及其对应的疾病树形结构
+     * @return 所有科室-疾病树形结构的列表
+     */
+    public List<Map<String, Object>> getAllDepartmentDiseaseTrees() {
+        String query = 
+            "MATCH (d:就诊科室)-[r:疾病相关就诊科室]-(disease:疾病) " +
+            "WHERE (d.is_deleted = 'N' or NOT exists(d.is_deleted)) " +
+            "AND (disease.is_deleted = 'N' or NOT exists(disease.is_deleted)) " +
+            "WITH d.name as department, collect({name: disease.name, sNode: []}) as diseases " +
+            "RETURN {name: department, sNode: diseases} as tree";
+        
+        try(Session session = neo4jUtil.getSession()) {
+            StatementResult result = session.run(query);
+            
+            List<Map<String, Object>> trees = new ArrayList<>();
+            while(result.hasNext()) {
+                Record record = result.next();
+                Map<String, Object> tree = record.get("tree").asMap();
+                trees.add(tree);
+            }
+            return trees;
+        }
+    }
 }

+ 31 - 0
src/main/java/com/qizhen/healsphere/service/impl/DepartmentServiceImpl.java

@@ -0,0 +1,31 @@
+package com.qizhen.healsphere.service.impl;
+
+import com.qizhen.healsphere.repository.neo4j.BaseNodeRepository;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+import java.util.Map;
+import java.util.HashMap;
+
+@Service
+@Slf4j
+public class DepartmentServiceImpl {
+    
+    @Autowired
+    private BaseNodeRepository baseNodeRepository;
+    
+    public Map<String, Object> getAllDepartmentDiseaseTrees() {
+        try {
+            List<Map<String, Object>> trees = baseNodeRepository.getAllDepartmentDiseaseTrees();
+            Map<String, Object> result = new HashMap<>();
+            result.put("name", "疾病");
+            result.put("sNode", trees);
+            return result;
+        } catch (Exception e) {
+            log.error("获取所有科室疾病树失败", e);
+            throw new RuntimeException("获取所有科室疾病树失败", e);
+        }
+    }
+} 

+ 156 - 0
src/main/java/com/qizhen/healsphere/service/impl/IcdDataProcessor.java

@@ -0,0 +1,156 @@
+package com.qizhen.healsphere.service.impl;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Service;
+
+import java.io.*;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.*;
+import java.util.stream.Collectors;
+
+@Service
+@Slf4j
+public class IcdDataProcessor {
+
+    @Data
+    private static class TreeNode {
+        String name;
+        @JsonProperty("sNode")
+        List<TreeNode> sNode;
+    }
+
+    public void processIcdData() throws IOException {
+        try {
+            // 1. 读取当前疾病列表
+            Set<String> currentDiseases = readCurrentDiseases();
+            log.info("读取到 {} 个疾病名称", currentDiseases.size());
+
+            // 2. 读取ICD10数据并建立映射关系
+            Map<String, String> diseaseToIcdMap = readIcd10Data();
+            log.info("读取到 {} 个ICD10映射关系", diseaseToIcdMap.size());
+
+            // 3. 读取原JSON数据
+            ObjectMapper mapper = new ObjectMapper();
+            TreeNode rootNode;
+            try (InputStream is = Files.newInputStream(Paths.get("src/main/resources/old_json.txt"))) {
+                rootNode = mapper.readValue(is, TreeNode.class);
+            }
+
+            // 4. 按ICD编码分组当前疾病
+            Map<String, List<String>> icdGroupedDiseases = new HashMap<>();
+            for (String disease : currentDiseases) {
+                String icdCode = diseaseToIcdMap.get(disease);
+                if (icdCode != null && icdCode.length() >= 3) {
+                    String icdPrefix = icdCode.substring(0, 3);
+                    icdGroupedDiseases.computeIfAbsent(icdPrefix, k -> new ArrayList<>())
+                            .add(disease);
+                }
+            }
+
+            // 5. 更新JSON树
+            updateTreeNodes(rootNode, icdGroupedDiseases);
+
+            // 6. 写入新文件
+            try (Writer writer = new FileWriter("src/main/resources/new_icd_json.json")) {
+                mapper.writerWithDefaultPrettyPrinter().writeValue(writer, rootNode);
+                log.info("成功生成新的JSON文件");
+            }
+        } catch (Exception e) {
+            log.error("处理疾病数据失败", e);
+            e.printStackTrace();
+        }
+    }
+    
+    private Set<String> readCurrentDiseases() throws IOException {
+        Set<String> diseases = new LinkedHashSet<>();
+        try (BufferedReader reader = new BufferedReader(
+                new InputStreamReader(
+                        Files.newInputStream(Paths.get("src/main/resources/current_disease_new.csv")),
+                    StandardCharsets.UTF_8
+                ))) {
+            String line;
+            while ((line = reader.readLine()) != null) {
+                String disease = line.trim();
+                if (!disease.isEmpty()) {
+                    diseases.add(disease);
+                }
+            }
+        }
+        return diseases;
+    }
+    
+    private Map<String, String> readIcd10Data() throws IOException {
+        Map<String, String> diseaseToIcdMap = new HashMap<>();
+        try (BufferedReader reader = new BufferedReader(
+                new InputStreamReader(
+                        Files.newInputStream(Paths.get("src/main/resources/new_icd10_data.csv")),
+                    StandardCharsets.UTF_8
+                ))) {
+            String line;
+            while ((line = reader.readLine()) != null) {
+                String[] parts = line.split(",");
+                if (parts.length >= 2) {
+                    String disease = parts[0].trim();
+                    String icdCode = parts[1].trim();
+                    if (!disease.isEmpty() && !icdCode.isEmpty()) {
+                        diseaseToIcdMap.put(disease, icdCode);
+                    }
+                }
+            }
+        }
+        return diseaseToIcdMap;
+    }
+    
+    private void updateTreeNodes(TreeNode node, Map<String, List<String>> icdGroupedDiseases) {
+        if (node == null) return;
+        
+        // 如果当前节点的name是3位ICD码
+        if (node.name != null && node.name.length() == 3 && icdGroupedDiseases.containsKey(node.name)) {
+            // 替换sNode为新的疾病名称列表
+            node.sNode = icdGroupedDiseases.get(node.name).stream()
+                    .map(name -> {
+                        TreeNode newNode = new TreeNode();
+                        newNode.name = name;
+                        newNode.sNode = new ArrayList<>();
+                        return newNode;
+                    })
+                    .collect(Collectors.toList());
+        } else if (node.sNode != null) {
+            // 递归处理子节点
+            List<TreeNode> newChildren = new ArrayList<>();
+            for (TreeNode child : node.sNode) {
+                // 只处理和添加包含匹配疾病的节点
+                if (hasMatchingDiseases(child, icdGroupedDiseases)) {
+                    updateTreeNodes(child, icdGroupedDiseases);
+                    newChildren.add(child);
+                }
+            }
+            node.sNode = newChildren;
+        }
+    }
+    
+    private boolean hasMatchingDiseases(TreeNode node, Map<String, List<String>> icdGroupedDiseases) {
+        if (node == null) return false;
+        
+        // 如果是ICD码节点,直接检查是否有匹配的疾病
+        if (node.name != null && node.name.length() == 3) {
+            return icdGroupedDiseases.containsKey(node.name);
+        }
+        
+        // 对于非ICD码节点,检查其子节点
+        if (node.sNode != null) {
+            for (TreeNode child : node.sNode) {
+                if (hasMatchingDiseases(child, icdGroupedDiseases)) {
+                    return true;
+                }
+            }
+        }
+        
+        return false;
+    }
+} 

+ 26 - 0
src/main/java/com/qizhen/healsphere/web/DepartmentController.java

@@ -0,0 +1,26 @@
+package com.qizhen.healsphere.web;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.qizhen.healsphere.service.impl.DepartmentServiceImpl;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.Map;
+
+@Controller
+@RequestMapping("/api/department")
+@Slf4j
+@JsonInclude(JsonInclude.Include.NON_NULL)
+public class DepartmentController {
+    
+    @Autowired
+    private DepartmentServiceImpl departmentService;
+    
+    @RequestMapping(value = "/disease-tree", method = RequestMethod.GET)
+    @ResponseBody
+    public Map<String, Object> getDepartmentDiseaseTree(@RequestParam(required = false) String departmentLabel) {
+        return departmentService.getAllDepartmentDiseaseTrees();
+    }
+} 

+ 27 - 0
src/main/java/com/qizhen/healsphere/web/IcdController.java

@@ -0,0 +1,27 @@
+package com.qizhen.healsphere.web;
+
+import com.qizhen.healsphere.service.impl.IcdDataProcessor;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+@Controller
+@RequestMapping("/api/icd")
+public class IcdController {
+    
+    @Autowired
+    private IcdDataProcessor icdDataProcessor;
+    
+    @RequestMapping(value = "/process",method = RequestMethod.POST)
+    @ResponseBody
+    public String processIcdData() {
+        try {
+            icdDataProcessor.processIcdData();
+            return "数据处理成功";
+        } catch (Exception e) {
+            return "处理失败:" + e.getMessage();
+        }
+    }
+} 

+ 2 - 2
src/main/resources/application-local.yml

@@ -4,10 +4,10 @@ swagger:
 spring:
   datasource:
     hikari:
-      jdbc-url: jdbc:postgresql://172.16.8.59:5433/postgres
+      jdbc-url: jdbc:postgresql://172.16.8.64:5432/postgres
       driver-class-name: org.postgresql.Driver
       username: postgres
-      password: 1Qaz@wsx
+      password: 12345678
 #      jdbc-url: jdbc:mysql://173.18.12.194:3306/sys-ltkg?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&useSSL=false
 #      driver-class-name: com.mysql.cj.jdbc.Driver
 #      username: root

+ 3 - 0
src/main/resources/application.yml

@@ -3,6 +3,9 @@ spring:
     active: local
   application:
     name: healsphere
+  jackson:
+    serialization:
+      indent-output: false
 
 server:
   servlet: