@ -272,6 +272,7 @@ public class Neo4jServiceImpl implements Neo4jService {
Map < String , Object > map = new HashMap < > ( ) ;
List < WebRelDTO > list = new ArrayList < > ( ) ;
List < Map < String , String > > nodes = new ArrayList < > ( ) ;
List < Map < String , String > > labelColorMap = graphReqVO . getNodeLabelColorMap ( ) ;
try {
Session session = driver . session ( ) ;
StringBuilder relQuery = new StringBuilder ( "MATCH (n)-[rel]->(m) " +
@ -293,11 +294,17 @@ public class Neo4jServiceImpl implements Neo4jService {
params . put ( "relTypes" , graphReqVO . getRelTypes ( ) ) ;
relQuery . append ( "AND ANY(type IN $relTypes WHERE type = type(rel)) " ) ;
}
relQuery . append ( "RETURN id(rel) as id, n.name as source, id(n) as sourceId, n.name as sourceName, type(rel) as relName, m.name as target, id(m) as targetId, m.name as targetName ") ;
relQuery . append ( "RETURN id(rel) as id, n.name as source, id(n) as sourceId, n.name as sourceName, type(rel) as relName, m.name as target, id(m) as targetId, m.name as targetName , labels(n) as sourceLabels, labels(m) as targetLabels ") ;
log . info ( "relQuery:{}" , relQuery ) ;
Result run = session . run ( relQuery . toString ( ) , params ) ;
while ( run . hasNext ( ) ) {
Record record = run . next ( ) ;
List < String > sourceLabels = record . get ( "sourceLabels" ) . asList ( Value : : asString ) ;
List < String > targetLabels = record . get ( "targetLabels" ) . asList ( Value : : asString ) ;
if ( sourceLabels = = null | | sourceLabels . isEmpty ( ) | | targetLabels = = null | | targetLabels . isEmpty ( ) ) {
log . info ( "节点标签为空,跳过" ) ;
continue ;
}
// 组织边
long sourceId = record . get ( "sourceId" ) . asLong ( ) ;
String relName = record . get ( "relName" ) . asString ( ) ;
@ -307,12 +314,10 @@ public class Neo4jServiceImpl implements Neo4jService {
// 组织节点
Map < String , String > sourceNodeMap = new HashMap < > ( ) ;
sourceNodeMap . put ( "name" , record . get ( "sourceName" ) . asString ( ) ) ;
sourceNodeMap . put ( "id" , String . valueOf ( sourceId ) ) ;
nodes . add ( sourceNodeMap ) ;
setNodeIdAndColor ( nodes , labelColorMap , sourceLabels , sourceId , sourceNodeMap , graphReqVO ) ;
Map < String , String > targetNodeMap = new HashMap < > ( ) ;
targetNodeMap . put ( "name" , record . get ( "targetName" ) . asString ( ) ) ;
targetNodeMap . put ( "id" , String . valueOf ( targetId ) ) ;
nodes . add ( targetNodeMap ) ;
setNodeIdAndColor ( nodes , labelColorMap , targetLabels , targetId , targetNodeMap , graphReqVO ) ;
}
} catch ( Exception e ) {
log . error ( "查询失败" , e ) ;
@ -328,20 +333,50 @@ public class Neo4jServiceImpl implements Neo4jService {
// 节点和关系合并
Pair < List < WebRelDTO > , List < Map < String , String > > > pair = mergeRecord ( distinctNodes , list ) ;
// 将返回结果List<Map<String, String>>转为Map<String, Object>, 并将其中key为color的键值对包一层itemStyle, 将键值对放在里面
List < Map < String , Object > > nodesList = new ArrayList < > ( ) ;
for ( Map < String , String > node : pair . getValue ( ) ) {
Map < String , String > itemStyle = new HashMap < > ( ) ;
itemStyle . put ( "color" , node . get ( "color" ) ) ;
Map < String , Object > nodeMap = new HashMap < > ( ) ;
nodeMap . put ( "name" , node . get ( "name" ) ) ;
nodeMap . put ( "id" , node . get ( "id" ) ) ;
nodeMap . put ( "itemStyle" , itemStyle ) ;
nodesList . add ( nodeMap ) ;
}
map . put ( "list" , pair . getKey ( ) ) ;
map . put ( "nodes" , pair . getValue ( ) ) ;
map . put ( "nodes" , nodesList ) ;
return R . ok ( map ) ;
}
private void setNodeIdAndColor ( List < Map < String , String > > nodes , List < Map < String , String > > labelColorMap , List < String > labels , long id , Map < String , String > nodeMap , GraphReqVO graphReqVO ) {
nodeMap . put ( "id" , String . valueOf ( id ) ) ;
for ( Map < String , String > labelColor : labelColorMap ) {
if ( labels . contains ( labelColor . get ( "name" ) ) ) {
if ( graphReqVO . getNodeLabels ( ) . isEmpty ( ) & & graphReqVO . getRelTypes ( ) . isEmpty ( ) & & StringUtils . isEmpty ( graphReqVO . getQueryStr ( ) ) ) {
nodeMap . put ( "color" , labelColor . get ( "lightColor" ) ) ;
} else {
nodeMap . put ( "color" , labelColor . get ( "color" ) ) ;
}
break ;
}
}
if ( ! nodeMap . containsKey ( "color" ) ) {
log . error ( "节点【{}】没有颜色,随机生成" , nodeMap . get ( "name" ) ) ;
nodeMap . put ( "color" , Neo4jUtils . getRandomColorPair ( ) [ 0 ] ) ;
}
nodes . add ( nodeMap ) ;
}
public Pair < List < WebRelDTO > , List < Map < String , String > > > mergeRecord ( List < Map < String , String > > nodes , List < WebRelDTO > relDTOS ) {
Map < String , NodeMapRecord > nodeRecordMap = electNodeRecord ( nodes ) ;
return Pair . of ( mergerWebRel ( relDTOS , nodeRecordMap ) , mergeNode ( nodes , nodeRecordMap ) ) ;
return Pair . of ( mergerWebRel ( relDTOS , nodeRecordMap ) , mergeNode ( nodes , nodeRecordMap ) ) ;
}
@Override
public R < ? > getNodeAndRelationListByCaseId ( String picType , String caseId ) {
Map < String , Set < String > > map = new HashMap < > ( ) ;
Map < String , Object > map = new HashMap < > ( ) ;
Set < String > nodeLabels = new HashSet < > ( ) ;
Set < String > relTypes = new HashSet < > ( ) ;
try ( Session session = driver . session ( ) ) {
@ -368,7 +403,18 @@ public class Neo4jServiceImpl implements Neo4jService {
}
log . info ( "查询到的节点类型{}个:{}" , nodeLabels . size ( ) , nodeLabels ) ;
log . info ( "查询到的关系类型{}个:{}" , relTypes . size ( ) , relTypes ) ;
map . put ( "nodeLabels" , nodeLabels ) ;
// 将nodeLabels重新组装成List<Map<String, Object>>, nodeLabels中的值作为key:name的值, 另外创建一个key:itemStyle, 值是Map<String, String>, 一个键值对是color+随机颜色, 另一个是lightColor+随机颜色
List < Map < String , Object > > nodeLabelsList = nodeLabels . stream ( ) . map ( label - > {
Map < String , Object > nodeLabelMap = new HashMap < > ( ) ;
Map < String , String > itemStyle = new HashMap < > ( ) ;
String [ ] colors = Neo4jUtils . getRandomColorPair ( ) ;
itemStyle . put ( "color" , colors [ 0 ] ) ;
itemStyle . put ( "lightColor" , colors [ 1 ] ) ;
nodeLabelMap . put ( "name" , label ) ;
nodeLabelMap . put ( "itemStyle" , itemStyle ) ;
return nodeLabelMap ;
} ) . toList ( ) ;
map . put ( "nodeLabels" , nodeLabelsList ) ;
map . put ( "relTypes" , relTypes ) ;
return R . ok ( map ) ;
}
@ -415,6 +461,7 @@ public class Neo4jServiceImpl implements Neo4jService {
return nodes . stream ( ) . map ( map - > {
Map < String , String > nodeMap = new HashMap < > ( ) ;
nodeMap . put ( "name" , map . get ( "name" ) ) ;
nodeMap . put ( "color" , map . get ( "color" ) ) ;
NodeMapRecord nodeMapRecord = nodeRecordMap . get ( map . get ( "name" ) ) ;
if ( null = = nodeMapRecord ) {
log . warn ( "mergeNode:节点信息异常, nodeRecordMap中不存在节点名称为: {}的NodeMapRecord" , map . get ( "name" ) ) ;
@ -583,7 +630,7 @@ public class Neo4jServiceImpl implements Neo4jService {
}
@Override
public List < Record > executeCypher ( String cypher , Map < String , Object > parameters ) {
public List < Record > executeCypher ( String cypher , Map < String , Object > parameters ) {
return this . driver . session ( ) . run ( cypher , parameters ) . list ( ) ;
}
}