@ -1,9 +1,6 @@
package com.supervision.neo4j.service.impl ;
import cn.hutool.core.collection.CollUtil ;
import cn.hutool.core.util.ObjectUtil ;
import cn.hutool.json.JSON ;
import cn.hutool.json.JSONUtil ;
import cn.hutool.core.util.StrUtil ;
import cn.hutool.poi.excel.ExcelReader ;
import cn.hutool.poi.excel.ExcelUtil ;
import com.supervision.common.domain.R ;
@ -15,16 +12,16 @@ import com.supervision.neo4j.service.Neo4jService;
import com.supervision.neo4j.utils.Neo4jUtils ;
import lombok.Data ;
import lombok.extern.slf4j.Slf4j ;
import org.neo4j.cypherdsl.core.Case ;
import org.neo4j.driver.* ;
import org.neo4j.driver.Record ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.stereotype.Service ;
import java.util.* ;
import java.util.concurrent.CountDownLatch ;
import java.util.concurrent.ExecutorService ;
import java.util.concurrent.Executors ;
import java.util.concurrent.ConcurrentHashMap ;
import java.util.function.Function ;
import java.util.function.Predicate ;
import java.util.stream.Collectors ;
/ * *
* @author qmy
@ -312,31 +309,112 @@ public class Neo4jServiceImpl implements Neo4jService {
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
// 节点和关系合并
Map < String , NodeMapRecord > nodeRecordMap = electNodeRecord ( nodes ) ;
list = mergerWebRel ( list , nodeRecordMap ) ;
nodes = mergeNode ( nodes , nodeRecordMap ) ;
map . put ( "list" , list ) ;
map . put ( "nodes" , nodes ) ;
return R . ok ( map ) ;
}
// @Override
// public R<?> test() {
// Session session = driver.session();
// Map<String, Object> params = new HashMap<>();
// params.put("lawActor", "行为人");
// params.put("lawParty", "aaaaaa");
// Result run = session.run("MATCH (m:LawActor), (n:FictionalOrgan) where m.name=$lawActor OPTIONAL MATCH (m)-[r:`冒充`]->(n) RETURN id(m) as startId, id(n) as endId, id(r) as relId, m.recordId as recordId, m.recordsId as recordsId", params);
// while (run.hasNext()) {
// Record record = run.next();
//
// String id = Neo4jUtils.valueTransportString(record.get("startId"));
// String endId = Neo4jUtils.valueTransportString(record.get("endId"));
// String relId = Neo4jUtils.valueTransportString(record.get("relId"));
// System.out.println("************" + id);
// System.out.println("************" + endId);
// System.out.println("************" + relId);
// }
// return R.ok("222");
// }
record NodeMapRecord ( String name , String id , Set < String > idSet ) {
}
/ * *
* 推 选 出 代 表 节 点 信 息
* @param nodes key : name , entityName , id 节 点 信 息
* @return
* /
private Map < String , NodeMapRecord > electNodeRecord ( List < Map < String , String > > nodes ) {
Map < String , NodeMapRecord > nodeRecordMap = new HashMap < > ( ) ;
for ( Map < String , String > node : nodes ) {
String name = node . get ( "name" ) ;
String id = node . get ( "id" ) ;
NodeMapRecord nodeMapRecord = nodeRecordMap . get ( name ) ;
if ( nodeMapRecord = = null ) {
Set < String > idSet = new HashSet < > ( ) ;
idSet . add ( id ) ;
nodeRecordMap . put ( name , new NodeMapRecord ( name , id , idSet ) ) ;
} else {
nodeMapRecord . idSet . add ( id ) ;
}
}
return nodeRecordMap ;
}
/ * *
* 合 并 节 点 信 息
* 合 并 依 据 :
* name 为 唯 一 标 识
* @param nodes key : name , entityName , id
* @param nodeRecordMap 代 表 节 点 信 息
* @return
* /
private List < Map < String , String > > mergeNode ( List < Map < String , String > > nodes , Map < String , NodeMapRecord > nodeRecordMap ) {
return nodes . stream ( ) . map ( map - > {
Map < String , String > nodeMap = new HashMap < > ( ) ;
nodeMap . put ( "name" , map . get ( "name" ) ) ;
nodeMap . put ( "entityName" , map . get ( "entityName" ) ) ;
NodeMapRecord nodeMapRecord = nodeRecordMap . get ( map . get ( "name" ) ) ;
if ( null = = nodeMapRecord ) {
log . warn ( "mergeNode:节点信息异常, nodeRecordMap中不存在节点名称为: {}的NodeMapRecord" , map . get ( "name" ) ) ;
return nodeMap ;
}
if ( ! nodeMapRecord . idSet . contains ( map . get ( "id" ) ) ) {
log . warn ( "mergeNode:节点信息异常, nodeMapRecord.idSet中不包含节点id:{},节点名称为:{}" , map . get ( "id" ) , map . get ( "name" ) ) ;
return nodeMap ;
}
nodeMap . put ( "id" , nodeMapRecord . id ) ;
return nodeMap ;
} ) . filter ( map - > StrUtil . isNotEmpty ( map . get ( "id" ) ) )
. filter ( distinctPredicate ( m - > m . get ( "id" ) ) ) . collect ( Collectors . toList ( ) ) ;
}
/ * *
* 合 并 关 系 信 息
* @param webRelDTOList 关 系 信 息
* @param nodeRecordMap 代 表 节 点 信 息
* @return
* /
private List < WebRelDTO > mergerWebRel ( List < WebRelDTO > webRelDTOList , Map < String , NodeMapRecord > nodeRecordMap ) {
Map < String , NodeMapRecord > idNodeRecordMap = nodeRecordMap . entrySet ( ) . stream ( )
. collect ( Collectors . toMap ( entry - > entry . getValue ( ) . id , Map . Entry : : getValue ) ) ;
return webRelDTOList . stream ( ) . map ( webRelDTO - > {
String target = webRelDTO . getTarget ( ) ;
String source = webRelDTO . getSource ( ) ;
String name = webRelDTO . getName ( ) ;
String sourceNew = idNodeRecordMap . entrySet ( ) . stream ( )
. filter ( entry - > entry . getValue ( ) . idSet . contains ( source ) )
. findAny ( ) . map ( Map . Entry : : getKey ) . orElse ( "" ) ;
String targetNew = idNodeRecordMap . entrySet ( ) . stream ( )
. filter ( entry - > entry . getValue ( ) . idSet . contains ( target ) )
. findAny ( ) . map ( Map . Entry : : getKey ) . orElse ( "" ) ;
if ( StrUtil . isEmpty ( sourceNew ) | | StrUtil . isEmpty ( targetNew ) ) {
log . warn ( "mergerWebRel:关系信息异常, nodeRecordMap中不存在节点id:{}或节点id:{}信息,节点名称为:{}" , source , target , name ) ;
}
return new WebRelDTO ( sourceNew , targetNew , name ) ;
} ) . filter ( webRelDTO - > StrUtil . isNotEmpty ( webRelDTO . getSource ( ) ) & & StrUtil . isNotEmpty ( webRelDTO . getTarget ( ) ) )
. filter ( distinctPredicate ( rel - > rel . getSource ( ) + rel . getTarget ( ) ) ) . toList ( ) ;
}
private < K > Predicate < K > distinctPredicate ( Function < K , Object > function ) {
ConcurrentHashMap < Object , Boolean > map = new ConcurrentHashMap < > ( ) ;
return ( t ) - > null = = map . putIfAbsent ( function . apply ( t ) , true ) ;
}
@Override
public void createAbstractGraph ( String path , String sheetName ) {