|
@@ -0,0 +1,154 @@
|
|
|
+package com.qizhen.healsphere;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.qizhen.healsphere.common.ai.BaidubceUtil;
|
|
|
+import com.qizhen.healsphere.common.ai.Knowlege;
|
|
|
+import com.qizhen.healsphere.repository.neo4j.entity.BaseEntity;
|
|
|
+import com.qizhen.healsphere.service.EntityService;
|
|
|
+import com.qizhen.healsphere.service.RelationshipService;
|
|
|
+import com.qizhen.healsphere.web.vo.CreateEntityVO;
|
|
|
+import com.qizhen.healsphere.web.vo.RelationshipVO;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFRow;
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFSheet;
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
+import org.apache.poi.ss.usermodel.Row;
|
|
|
+import org.apache.poi.ss.usermodel.Sheet;
|
|
|
+import org.apache.poi.ss.usermodel.Workbook;
|
|
|
+import org.apache.poi.ss.usermodel.WorkbookFactory;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
+import org.junit.Test;
|
|
|
+import org.junit.runner.RunWith;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.boot.test.context.SpringBootTest;
|
|
|
+import org.springframework.context.annotation.ComponentScan;
|
|
|
+import org.springframework.test.context.junit4.SpringRunner;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 是否空腹及归大类
|
|
|
+ */
|
|
|
+@RunWith(SpringRunner.class)
|
|
|
+@ComponentScan(basePackages = {"com.qizhen.healsphere.model", "com.qizhen.healsphere.repository"})
|
|
|
+@SpringBootTest
|
|
|
+public class DataWriteFromExcelTest {
|
|
|
+ @Autowired
|
|
|
+ RelationshipService relationshipService;
|
|
|
+ @Autowired
|
|
|
+ EntityService entityService;
|
|
|
+ private static int maxCount= -1;
|
|
|
+ private static String urlExcelPath = "C:\\Users\\17664\\Desktop\\检查目的1736793723427.xlsx";
|
|
|
+ static HSSFWorkbook workbook = new HSSFWorkbook();//这里也可以设置sheet的Name;
|
|
|
+ static String startLabel = "辅助检查";
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void WriteFromExcel() throws Exception {
|
|
|
+
|
|
|
+ List<Knowlege> knowleges = getDataFromExecl(0);
|
|
|
+ saveNeo4j(knowleges,"疾病");
|
|
|
+ knowleges = getDataFromExecl(2);
|
|
|
+ saveNeo4j(knowleges,startLabel);
|
|
|
+ knowleges = getDataFromExecl(3);
|
|
|
+ saveNeo4j(knowleges,"疾病");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private static List<Knowlege> getDataFromExecl(int index) throws Exception {
|
|
|
+ int curCount = 0;
|
|
|
+
|
|
|
+ List<Knowlege> result = new ArrayList<>();
|
|
|
+ InputStream fis = new FileInputStream(urlExcelPath);
|
|
|
+ Workbook urlWorkbook = WorkbookFactory.create(fis);
|
|
|
+ Sheet urlSheet = urlWorkbook.getSheetAt(index);
|
|
|
+
|
|
|
+ for (int rowNum = 0; rowNum <= urlSheet.getLastRowNum(); rowNum++) {
|
|
|
+ try {
|
|
|
+ Row row = urlSheet.getRow(rowNum);
|
|
|
+ String name = row.getCell(0).getStringCellValue();
|
|
|
+ if (StringUtils.isEmpty(name)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ String property = row.getCell(1).getStringCellValue();
|
|
|
+ if(StringUtils.isEmpty(property)){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ String answer = row.getCell(2).getStringCellValue();
|
|
|
+ if(StringUtils.isEmpty(answer)){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ JSONArray jsonArray = JSONArray.parseArray(answer);
|
|
|
+
|
|
|
+ Knowlege knowlege = new Knowlege();
|
|
|
+ knowlege.setEntity(name.trim());
|
|
|
+
|
|
|
+ knowlege.setProperty(property.trim());
|
|
|
+ knowlege.setValue(jsonArray.toJSONString());
|
|
|
+
|
|
|
+ result.add(knowlege);
|
|
|
+ curCount++;
|
|
|
+ if (maxCount > 0) {
|
|
|
+ if (curCount >= maxCount) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void saveNeo4j(List<Knowlege> knowleges,String endLabel) throws Exception {
|
|
|
+ for (Knowlege temp: knowleges) {
|
|
|
+ String value = temp.getValue();
|
|
|
+ if(StringUtils.isBlank(value)){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ try{
|
|
|
+ JSONArray jsonArray = JSONArray.parseArray(value);
|
|
|
+ BaseEntity startEntity = createNoExists(startLabel, temp.getEntity());
|
|
|
+ long startId = startEntity.getId();
|
|
|
+ List<RelationshipVO> relationshipList = new ArrayList<>();
|
|
|
+ for(int i=0;i<jsonArray.size();i++){
|
|
|
+ String name = jsonArray.getString(i);
|
|
|
+ if(StringUtils.isEmpty(name)){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ String property = temp.getProperty();
|
|
|
+ if(StringUtils.isEmpty(endLabel)) {
|
|
|
+ endLabel = startLabel + property;
|
|
|
+ }
|
|
|
+ BaseEntity endEntity = createNoExists(endLabel, name);
|
|
|
+ Long endId = endEntity.getId();
|
|
|
+ RelationshipVO relationshipVO = new RelationshipVO();
|
|
|
+ relationshipVO.setStartId(startId);
|
|
|
+ relationshipVO.setEndId(endId);
|
|
|
+ relationshipVO.setStartLabel(startLabel);
|
|
|
+ relationshipVO.setEndLabel(endLabel);
|
|
|
+ relationshipVO.setRelationshipType(startLabel+"相关"+ property);
|
|
|
+ relationshipList.add(relationshipVO);
|
|
|
+ }
|
|
|
+ if(!CollectionUtils.isEmpty(relationshipList)) {
|
|
|
+ System.out.println(relationshipService.createRelationship(relationshipList));
|
|
|
+ }
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private BaseEntity createNoExists(String labelName, String name) {
|
|
|
+ BaseEntity nodeByName = entityService.findNodeByName(labelName, name);
|
|
|
+ if(Objects.nonNull(nodeByName)){//节点不存在
|
|
|
+ return nodeByName;
|
|
|
+ }
|
|
|
+ CreateEntityVO createEntity = new CreateEntityVO();
|
|
|
+ createEntity.setName(name);
|
|
|
+ createEntity.setLabel(labelName);
|
|
|
+ return entityService.create(createEntity);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|