|
|
|
@ -403,90 +403,86 @@ public class ModelRecordTypeServiceImpl extends ServiceImpl<ModelRecordTypeMappe
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String addNeo4j(List<String> ids) {
|
|
|
|
|
if (CollUtil.isEmpty(ids)) {
|
|
|
|
|
return "成功插入0条信息";
|
|
|
|
|
public String addNeo4j(List<String> ids, String recordId) {
|
|
|
|
|
if (StrUtil.isBlank(recordId)){
|
|
|
|
|
throw new BusinessException("记录ID不能为空");
|
|
|
|
|
}
|
|
|
|
|
List<TripleInfo> tripleInfos = tripleInfoService.listByIds(ids);
|
|
|
|
|
int i = 0;
|
|
|
|
|
for (TripleInfo tripleInfo : tripleInfos) {
|
|
|
|
|
try {
|
|
|
|
|
//开始节点
|
|
|
|
|
String start = tripleInfo.getStartNode();
|
|
|
|
|
// 首先看是否已经存在了,如果已经存在了,就不添加了
|
|
|
|
|
CaseNode startNode = neo4jService.findOneByName(tripleInfo.getCaseId(), tripleInfo.getRecordId(), tripleInfo.getStartNodeType(), start, "1");
|
|
|
|
|
if (startNode == null) {
|
|
|
|
|
startNode = new CaseNode(start, tripleInfo.getStartNodeType(), tripleInfo.getRecordSplitId(), tripleInfo.getRecordId(), tripleInfo.getCaseId(), "1");
|
|
|
|
|
CaseNode save = neo4jService.save(startNode);
|
|
|
|
|
startNode.setId(save.getId());
|
|
|
|
|
}
|
|
|
|
|
//结束节点
|
|
|
|
|
String end = tripleInfo.getEndNode();
|
|
|
|
|
CaseNode endNode = neo4jService.findOneByName(tripleInfo.getCaseId(), tripleInfo.getRecordId(), tripleInfo.getEndNodeType(), end, "1");
|
|
|
|
|
if (endNode == null) {
|
|
|
|
|
endNode = new CaseNode(end, tripleInfo.getEndNodeType(), tripleInfo.getRecordSplitId(), tripleInfo.getRecordId(), tripleInfo.getCaseId(), "1");
|
|
|
|
|
CaseNode save = neo4jService.save(endNode);
|
|
|
|
|
endNode.setId(save.getId());
|
|
|
|
|
}
|
|
|
|
|
//关系
|
|
|
|
|
Rel rel = new Rel(startNode.getId(), tripleInfo.getRelation(), endNode.getId());
|
|
|
|
|
Rel relation = neo4jService.findRelation(rel);
|
|
|
|
|
if (relation == null) {
|
|
|
|
|
relation = neo4jService.saveRelation(rel);
|
|
|
|
|
}
|
|
|
|
|
tripleInfo.setAddNeo4j("1");
|
|
|
|
|
tripleInfo.setStartNodeGraphId(startNode.getId());
|
|
|
|
|
tripleInfo.setRelGraphId(relation.getId());
|
|
|
|
|
tripleInfo.setEndNodeGraphId(endNode.getId());
|
|
|
|
|
boolean updateResult = tripleInfoService.updateById(tripleInfo);
|
|
|
|
|
if (updateResult) {
|
|
|
|
|
i++;
|
|
|
|
|
if (CollUtil.isNotEmpty(ids)) {
|
|
|
|
|
List<TripleInfo> tripleInfos = tripleInfoService.listByIds(ids);
|
|
|
|
|
int i = 0;
|
|
|
|
|
for (TripleInfo tripleInfo : tripleInfos) {
|
|
|
|
|
try {
|
|
|
|
|
//开始节点
|
|
|
|
|
String start = tripleInfo.getStartNode();
|
|
|
|
|
// 首先看是否已经存在了,如果已经存在了,就不添加了
|
|
|
|
|
CaseNode startNode = neo4jService.findOneByName(tripleInfo.getCaseId(), tripleInfo.getRecordId(), tripleInfo.getStartNodeType(), start, "1");
|
|
|
|
|
if (startNode == null) {
|
|
|
|
|
startNode = new CaseNode(start, tripleInfo.getStartNodeType(), tripleInfo.getRecordSplitId(), tripleInfo.getRecordId(), tripleInfo.getCaseId(), "1");
|
|
|
|
|
CaseNode save = neo4jService.save(startNode);
|
|
|
|
|
startNode.setId(save.getId());
|
|
|
|
|
}
|
|
|
|
|
//结束节点
|
|
|
|
|
String end = tripleInfo.getEndNode();
|
|
|
|
|
CaseNode endNode = neo4jService.findOneByName(tripleInfo.getCaseId(), tripleInfo.getRecordId(), tripleInfo.getEndNodeType(), end, "1");
|
|
|
|
|
if (endNode == null) {
|
|
|
|
|
endNode = new CaseNode(end, tripleInfo.getEndNodeType(), tripleInfo.getRecordSplitId(), tripleInfo.getRecordId(), tripleInfo.getCaseId(), "1");
|
|
|
|
|
CaseNode save = neo4jService.save(endNode);
|
|
|
|
|
endNode.setId(save.getId());
|
|
|
|
|
}
|
|
|
|
|
//关系
|
|
|
|
|
Rel rel = new Rel(startNode.getId(), tripleInfo.getRelation(), endNode.getId());
|
|
|
|
|
Rel relation = neo4jService.findRelation(rel);
|
|
|
|
|
if (relation == null) {
|
|
|
|
|
relation = neo4jService.saveRelation(rel);
|
|
|
|
|
}
|
|
|
|
|
tripleInfo.setAddNeo4j("1");
|
|
|
|
|
tripleInfo.setStartNodeGraphId(startNode.getId());
|
|
|
|
|
tripleInfo.setRelGraphId(relation.getId());
|
|
|
|
|
tripleInfo.setEndNodeGraphId(endNode.getId());
|
|
|
|
|
boolean updateResult = tripleInfoService.updateById(tripleInfo);
|
|
|
|
|
if (updateResult) {
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error(e.getMessage(), e);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error(e.getMessage(), e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 删除的要进行移除
|
|
|
|
|
Optional<TripleInfo> any = tripleInfos.stream().findAny();
|
|
|
|
|
if (any.isPresent()) {
|
|
|
|
|
TripleInfo anyTripleInfo = any.get();
|
|
|
|
|
String recordId = anyTripleInfo.getRecordId();
|
|
|
|
|
List<TripleInfo> existNeoList = tripleInfoService.lambdaQuery().eq(TripleInfo::getRecordId, recordId).eq(TripleInfo::getAddNeo4j, "1").list();
|
|
|
|
|
Map<String, TripleInfo> existNeoMap = existNeoList.stream().collect(Collectors.toMap(TripleInfo::getId, Function.identity()));
|
|
|
|
|
// 去掉已经添加的
|
|
|
|
|
Set<String> existNeoIdSet = existNeoMap.keySet();
|
|
|
|
|
ids.forEach(existNeoIdSet::remove);
|
|
|
|
|
// 剩下的就是需要删除的
|
|
|
|
|
for (String needDeleteId : existNeoIdSet) {
|
|
|
|
|
TripleInfo tripleInfo = existNeoMap.get(needDeleteId);
|
|
|
|
|
if (ObjectUtil.isNotEmpty(tripleInfo.getRelGraphId())) {
|
|
|
|
|
// 先移除关系
|
|
|
|
|
neo4jService.deleteRel(tripleInfo.getRelGraphId());
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (ObjectUtil.isNotEmpty(tripleInfo.getEndNodeGraphId())) {
|
|
|
|
|
// 再移除尾节点
|
|
|
|
|
neo4jService.delNode(tripleInfo.getEndNodeGraphId());
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (ObjectUtil.isNotEmpty(tripleInfo.getStartNodeGraphId())) {
|
|
|
|
|
// 尝试删除头节点(只有头节点不存在任何关系的时候才进行删除)
|
|
|
|
|
neo4jService.deleteNoRelationNode(tripleInfo.getStartNodeGraphId());
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
// 更新状态
|
|
|
|
|
tripleInfoService.lambdaUpdate().set(TripleInfo::getAddNeo4j, "0")
|
|
|
|
|
.set(TripleInfo::getEndNodeGraphId, null)
|
|
|
|
|
.set(TripleInfo::getRelGraphId, null)
|
|
|
|
|
.set(TripleInfo::getStartNodeGraphId, null)
|
|
|
|
|
.eq(TripleInfo::getId, needDeleteId).update();
|
|
|
|
|
List<TripleInfo> existNeoList = tripleInfoService.lambdaQuery().eq(TripleInfo::getRecordId, recordId).eq(TripleInfo::getAddNeo4j, "1").list();
|
|
|
|
|
Map<String, TripleInfo> existNeoMap = existNeoList.stream().collect(Collectors.toMap(TripleInfo::getId, Function.identity()));
|
|
|
|
|
// 去掉已经添加的
|
|
|
|
|
Set<String> existNeoIdSet = existNeoMap.keySet();
|
|
|
|
|
ids.forEach(existNeoIdSet::remove);
|
|
|
|
|
// 剩下的就是需要删除的
|
|
|
|
|
for (String needDeleteId : existNeoIdSet) {
|
|
|
|
|
TripleInfo tripleInfo = existNeoMap.get(needDeleteId);
|
|
|
|
|
if (ObjectUtil.isNotEmpty(tripleInfo.getRelGraphId())) {
|
|
|
|
|
// 先移除关系
|
|
|
|
|
neo4jService.deleteRel(tripleInfo.getRelGraphId());
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (ObjectUtil.isNotEmpty(tripleInfo.getEndNodeGraphId())) {
|
|
|
|
|
// 再移除尾节点
|
|
|
|
|
neo4jService.delNode(tripleInfo.getEndNodeGraphId());
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (ObjectUtil.isNotEmpty(tripleInfo.getStartNodeGraphId())) {
|
|
|
|
|
// 尝试删除头节点(只有头节点不存在任何关系的时候才进行删除)
|
|
|
|
|
neo4jService.deleteNoRelationNode(tripleInfo.getStartNodeGraphId());
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
// 更新状态
|
|
|
|
|
tripleInfoService.lambdaUpdate().set(TripleInfo::getAddNeo4j, "0")
|
|
|
|
|
.set(TripleInfo::getEndNodeGraphId, null)
|
|
|
|
|
.set(TripleInfo::getRelGraphId, null)
|
|
|
|
|
.set(TripleInfo::getStartNodeGraphId, null)
|
|
|
|
|
.eq(TripleInfo::getId, needDeleteId).update();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ("成功插入" + i + "条信息");
|
|
|
|
|
return "保存三元组成功";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|