@ -41,6 +41,7 @@ import org.neo4j.driver.types.Node;
import org.springframework.beans.BeanUtils ;
import org.springframework.beans.BeanUtils ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.data.neo4j.core.Neo4jClient ;
import org.springframework.data.neo4j.core.Neo4jClient ;
import org.springframework.data.neo4j.core.Neo4jTemplate ;
import org.springframework.stereotype.Service ;
import org.springframework.stereotype.Service ;
import org.springframework.transaction.annotation.Transactional ;
import org.springframework.transaction.annotation.Transactional ;
@ -113,7 +114,7 @@ public class knowServiceImpl implements KnowService {
Node node1 = ( Node ) element . get ( "p" ) ;
Node node1 = ( Node ) element . get ( "p" ) ;
Long group = ( Long ) element . get ( "d" ) ;
Long group = ( Long ) element . get ( "d" ) ;
knowVO . setColor ( colorChoose . get ( group ) ) ;
// knowVO.setColor(colorChoose.get(group));
Long id1 = node1 . id ( ) ;
Long id1 = node1 . id ( ) ;
String name1 = node1 . get ( "name" ) . asString ( ) ;
String name1 = node1 . get ( "name" ) . asString ( ) ;
@ -168,8 +169,8 @@ public class knowServiceImpl implements KnowService {
knowVO = new KnowVO ( ) ;
knowVO = new KnowVO ( ) ;
Node node1 = ( Node ) element . get ( "p" ) ;
Node node1 = ( Node ) element . get ( "p" ) ;
Long group = ( Long ) element . get ( "d" ) ;
// Long group = (Long) element.get("d");
knowVO . setColor ( colorChoose . get ( group ) ) ;
// knowVO.setColor(colorChoose.get(group));
Long id1 = node1 . id ( ) ;
Long id1 = node1 . id ( ) ;
String name1 = node1 . get ( "name" ) . asString ( ) ;
String name1 = node1 . get ( "name" ) . asString ( ) ;
@ -225,7 +226,7 @@ public class knowServiceImpl implements KnowService {
Node node1 = ( Node ) element . get ( "p" ) ;
Node node1 = ( Node ) element . get ( "p" ) ;
Long group = ( Long ) element . get ( "d" ) ;
Long group = ( Long ) element . get ( "d" ) ;
knowVO . setColor ( colorChoose . get ( group ) ) ;
// knowVO.setColor(colorChoose.get(group));
Long id1 = node1 . id ( ) ;
Long id1 = node1 . id ( ) ;
String name1 = node1 . get ( "name" ) . asString ( ) ;
String name1 = node1 . get ( "name" ) . asString ( ) ;
@ -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串生成 图谱 保证事务
//todo 根据json串生成 图谱 保证事务
private BaseResponse < String > generateGraph ( String jsonNodeCourse , String jsonNodeChapter , String jsonNodeKnow , String jsonEdges ) {
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 ) ;
JSONArray jsonArrayEdges = JSONUtil . parseArray ( jsonEdges ) ;
for ( Object obj : jsonArrayEdges ) {
for ( Object obj : jsonArrayEdges ) {
JSONObject jsonObject = ( JSONObject ) obj ;
JSONObject jsonObject = ( JSONObject ) obj ;
@ -476,43 +547,14 @@ public class knowServiceImpl implements KnowService {
String target = jsonObject . getStr ( "target" ) ;
String target = jsonObject . getStr ( "target" ) ;
String type = jsonObject . getStr ( "type" ) ;
String type = jsonObject . getStr ( "type" ) ;
Integer sta = jsonObject . getInt ( "sta" ) ;
Integer sta = jsonObject . getInt ( "sta" ) ;
//在图数据库中插入
try {
if ( sta ! = null & & sta = = 1 ) {
opResult ( source , target , type , sta ) ;
Long target1 = Long . parseLong ( target ) ;
} catch ( Exception e ) {
int res = knowRepository . addCourseRelationship ( source , target1 , type ) ;
e . printStackTrace ( ) ;
if ( res < = 0 ) {
return ResultUtils . error ( ErrorCode . SYSTEM_ERROR , "添加节点关系失败" ) ;
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 错误" ) ;
}
}
}
return ResultUtils . success ( "添加节点关系成功" ) ;
return ResultUtils . success ( "添加节点关系成功" ) ;
}
}
@ -748,7 +790,10 @@ public class knowServiceImpl implements KnowService {
@Override
@Override
public BaseResponse < BaseKnowReturn > getAllKnowByCourseId ( String id ) {
public BaseResponse < BaseKnowReturn > getAllKnowByCourseId ( String id ) {
Collection < Map < String , Object > > all =
Collection < Map < String , Object > > all =
neo4jClient . query ( "match(n:Know)-[r:contain*0..4]->(p:Know) where n.courseId = '" + id + "' return n as `n`,r as `r`,p as `p`,length(r) as `d`" ) . fetch ( ) . all ( ) ;
neo4jClient . query ( "match(n:Know)-[r:contain*0..]->(p:Know) where n.courseId = '" + id + "' return n as `n`,r as `r`,p as `p`,length(r) as `d`" ) . fetch ( ) . all ( ) ;
Collection < Map < String , Object > > all2 =
neo4jClient . query ( "match(n:Know)-[r*]->(p:Know) where n.courseId = '" + id + "' return r as `r`" ) . fetch ( ) . all ( ) ;
Map < Long , String > colorChoose = new HashMap < > ( ) ;
Map < Long , String > colorChoose = new HashMap < > ( ) ;
String color [ ] = new String [ 10 ] ;
String color [ ] = new String [ 10 ] ;
@ -758,6 +803,7 @@ public class knowServiceImpl implements KnowService {
}
}
Iterator < Map < String , Object > > iterator = all . iterator ( ) ;
Iterator < Map < String , Object > > iterator = all . iterator ( ) ;
Iterator < Map < String , Object > > iterator2 = all2 . iterator ( ) ;
Set < KnowVO > knowList = new HashSet < > ( ) ;
Set < KnowVO > knowList = new HashSet < > ( ) ;
Set < Links > linksList = new HashSet < > ( ) ;
Set < Links > linksList = new HashSet < > ( ) ;
KnowVO knowVO ;
KnowVO knowVO ;
@ -768,9 +814,8 @@ public class knowServiceImpl implements KnowService {
Map < String , Object > element = iterator . next ( ) ;
Map < String , Object > element = iterator . next ( ) ;
knowVO = new KnowVO ( ) ;
knowVO = new KnowVO ( ) ;
Node node1 = ( Node ) element . get ( "p" ) ;
Node node1 = ( Node ) element . get ( "p" ) ;
Long group = ( Long ) element . get ( "d" ) ;
Long group = ( Long ) element . get ( "d" ) ;
// knowVO.setColor(colorChoose.get(group));
knowVO . setColor ( colorChoose . get ( group ) ) ;
if ( node1 ! = null ) {
if ( node1 ! = null ) {
Long id1 = node1 . id ( ) ;
Long id1 = node1 . id ( ) ;
String name1 = node1 . get ( "name" ) . asString ( ) ;
String name1 = node1 . get ( "name" ) . asString ( ) ;
@ -778,9 +823,29 @@ public class knowServiceImpl implements KnowService {
knowVO . setLabel ( name1 ) ;
knowVO . setLabel ( name1 ) ;
knowList . add ( knowVO ) ;
knowList . add ( knowVO ) ;
knowVO . setColor ( colorChoose . get ( group ) ) ;
knowVO . setColor ( colorChoose . get ( group ) ) ;
}
node2 = ( List ) element . get ( "r" ) ;
if ( ! node2 . isEmpty ( ) ) {
for ( int i = 0 ; i < node2 . size ( ) ; i + + ) {
InternalRelationship e = ( InternalRelationship ) node2 . get ( i ) ;
links = new Links ( ) ;
links . setId ( e . id ( ) ) ;
links . setSource ( e . startNodeId ( ) ) ;
links . setTarget ( e . endNodeId ( ) ) ;
if ( "contain" . equals ( e . type ( ) ) ) {
links . setLabel ( "包含" ) ;
} else if ( "order" . equals ( e . type ( ) ) ) {
links . setLabel ( "顺序" ) ;
}
linksList . add ( links ) ;
}
}
}
}
while ( iterator2 . hasNext ( ) ) {
Map < String , Object > element = iterator2 . next ( ) ;
node2 = ( List ) element . get ( "r" ) ;
node2 = ( List ) element . get ( "r" ) ;
if ( ! node2 . isEmpty ( ) ) {
if ( ! node2 . isEmpty ( ) ) {
for ( int i = 0 ; i < node2 . size ( ) ; i + + ) {
for ( int i = 0 ; i < node2 . size ( ) ; i + + ) {