|
|
|
@ -41,6 +41,7 @@ import org.neo4j.driver.types.Node; |
|
|
|
|
import org.springframework.beans.BeanUtils; |
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
import org.springframework.data.neo4j.core.Neo4jClient; |
|
|
|
|
import org.springframework.data.neo4j.core.Neo4jTemplate; |
|
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
|
|
|
|
@ -420,6 +421,77 @@ public class knowServiceImpl implements KnowService { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Long opResult( String source, String target, String type, Integer sta){ |
|
|
|
|
Long res = -1L; |
|
|
|
|
String weight = null; |
|
|
|
|
if("contain".equals(type)){ |
|
|
|
|
weight = " {weight:5}"; |
|
|
|
|
}else if("dependent".equals(type)){ |
|
|
|
|
weight = " {weight:9}"; |
|
|
|
|
}else if("order".equals(type)){ |
|
|
|
|
weight = " {weight: 1}"; |
|
|
|
|
} |
|
|
|
|
if(sta != null && sta == 1){ |
|
|
|
|
res = -1L; |
|
|
|
|
Long target1 = Long.parseLong(target); |
|
|
|
|
res = knowRepository.addCourseRelationship(source,target1); |
|
|
|
|
if (res< 0L) { |
|
|
|
|
throw new RuntimeException("添加课程-章节 包含 失败"); |
|
|
|
|
} |
|
|
|
|
return res; |
|
|
|
|
} |
|
|
|
|
else if (sta != null && sta == 2) { |
|
|
|
|
Long source1 = Long.parseLong(source); |
|
|
|
|
Long target1 = Long.parseLong(target); |
|
|
|
|
String str = "MATCH (p:Know), (j:Know) WHERE p.chapterId= "+source1+" AND j.chapterId= "+target1+" CREATE (p)-[r: "+type+weight+" ]->(j) return count(r) as `count`;"; |
|
|
|
|
Collection<Map<String, Object>> result = neo4jClient.query(str).fetch().all(); |
|
|
|
|
Iterator<Map<String, Object>> iterator = result.iterator(); |
|
|
|
|
res = -1L; |
|
|
|
|
while (iterator.hasNext()){ |
|
|
|
|
Map<String, Object> element = iterator.next(); |
|
|
|
|
res = (Long) element.get("count"); |
|
|
|
|
} |
|
|
|
|
if (res<=0L) { |
|
|
|
|
throw new RuntimeException("添加章节-章节"+type+"失败"); |
|
|
|
|
} |
|
|
|
|
return res; |
|
|
|
|
|
|
|
|
|
} else if (sta != null && sta == 3) { |
|
|
|
|
Long source1 = Long.parseLong(source); |
|
|
|
|
Long target1 = Long.parseLong(target); |
|
|
|
|
String str = "MATCH (p:Know), (j:Know) WHERE p.chapterId= "+source1+" AND j.knowId= "+target1+" CREATE (p)-[r: "+type+weight+" ]->(j) return count(r) as `count`;"; |
|
|
|
|
Collection<Map<String, Object>> result = neo4jClient.query(str).fetch().all(); |
|
|
|
|
Iterator<Map<String, Object>> iterator = result.iterator(); |
|
|
|
|
res = -1L; |
|
|
|
|
while (iterator.hasNext()){ |
|
|
|
|
Map<String, Object> element = iterator.next(); |
|
|
|
|
res = (Long) element.get("count"); |
|
|
|
|
} |
|
|
|
|
if (res < 0) { |
|
|
|
|
throw new RuntimeException("添加章节-知识点"+type+"失败"); |
|
|
|
|
} |
|
|
|
|
return res; |
|
|
|
|
} else if (sta != null && sta == 4) { |
|
|
|
|
Long source1 = Long.parseLong(source); |
|
|
|
|
Long target1 = Long.parseLong(target); |
|
|
|
|
String str = "MATCH (p:Know), (j:Know) WHERE p.chapterId= "+source1+" AND j.knowId= "+target1+" CREATE (p)-[r: "+type+weight+" ]->(j) return count(r) as `count`;"; |
|
|
|
|
Collection<Map<String, Object>> result = neo4jClient.query(str).fetch().all(); |
|
|
|
|
Iterator<Map<String, Object>> iterator = result.iterator(); |
|
|
|
|
res = -1L; |
|
|
|
|
while (iterator.hasNext()){ |
|
|
|
|
Map<String, Object> element = iterator.next(); |
|
|
|
|
res = (Long) element.get("count"); |
|
|
|
|
} |
|
|
|
|
if (res < 0L) { |
|
|
|
|
throw new RuntimeException("添加知识点-知识点 关系"+type+"失败"); |
|
|
|
|
} |
|
|
|
|
return res; |
|
|
|
|
} else { |
|
|
|
|
throw new RuntimeException("执行 错误"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//todo 根据json串生成 图谱 保证事务
|
|
|
|
|
private BaseResponse<String> generateGraph(String jsonNodeCourse, String jsonNodeChapter, String jsonNodeKnow, String jsonEdges) { |
|
|
|
|
//插入图
|
|
|
|
@ -467,8 +539,7 @@ public class knowServiceImpl implements KnowService { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//插入关系
|
|
|
|
|
//插入关系 // 1 课程-章节 2 章节 - 章节 3 章节 - 知识点 4 点 - 点
|
|
|
|
|
JSONArray jsonArrayEdges = JSONUtil.parseArray(jsonEdges); |
|
|
|
|
for (Object obj : jsonArrayEdges) { |
|
|
|
|
JSONObject jsonObject = (JSONObject) obj; |
|
|
|
@ -476,43 +547,14 @@ public class knowServiceImpl implements KnowService { |
|
|
|
|
String target = jsonObject.getStr("target"); |
|
|
|
|
String type = jsonObject.getStr("type"); |
|
|
|
|
Integer sta = jsonObject.getInt("sta"); |
|
|
|
|
//在图数据库中插入
|
|
|
|
|
if (sta != null && sta == 1) { |
|
|
|
|
Long target1 = Long.parseLong(target); |
|
|
|
|
int res = knowRepository.addCourseRelationship(source, target1, type); |
|
|
|
|
if (res <= 0) { |
|
|
|
|
throw new RuntimeException("添加关系 1 失败"); |
|
|
|
|
} |
|
|
|
|
} else if (sta != null && sta == 2) { |
|
|
|
|
Long source1 = Long.parseLong(source); |
|
|
|
|
Long target1 = Long.parseLong(target); |
|
|
|
|
int res = knowRepository.addChapterRelationship(source1, target1, type); |
|
|
|
|
if (res <= 0) { |
|
|
|
|
throw new RuntimeException("添加关系 2 失败"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} else if (sta != null && sta == 3) { |
|
|
|
|
Long source1 = Long.parseLong(source); |
|
|
|
|
Long target1 = Long.parseLong(target); |
|
|
|
|
int res = knowRepository.addChapterToKnowRelationship(source1, target1, type); |
|
|
|
|
if (res <= 0) { |
|
|
|
|
throw new RuntimeException("添加关系 13失败"); |
|
|
|
|
} |
|
|
|
|
} else if (sta != null && sta == 4) { |
|
|
|
|
Long source1 = Long.parseLong(source); |
|
|
|
|
Long target1 = Long.parseLong(target); |
|
|
|
|
int res = knowRepository.addKnowRelationship(source1, target1, type); |
|
|
|
|
if (res <= 0) { |
|
|
|
|
throw new RuntimeException("添加关系 4 失败"); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
throw new RuntimeException("sta 错误"); |
|
|
|
|
try { |
|
|
|
|
opResult(source,target,type,sta); |
|
|
|
|
}catch (Exception e){ |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
return ResultUtils.error(ErrorCode.SYSTEM_ERROR,"添加节点关系失败"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
return ResultUtils.success("添加节点关系成功"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|