package com.supervision.neo4j.service.impl; import com.supervision.common.domain.R; import com.supervision.common.utils.StringUtils; import com.supervision.neo4j.domain.CaseNode; import com.supervision.neo4j.domain.Rel; import com.supervision.neo4j.service.Neo4jService; import com.supervision.neo4j.utils.Neo4jUtils; import org.neo4j.driver.*; import org.neo4j.driver.Record; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @author qmy * @since 2023-10-26 */ @Service public class Neo4jServiceImpl implements Neo4jService { private final Driver driver; @Autowired private Neo4jServiceImpl(Driver driver) { this.driver = driver; } @Override public R save(CaseNode caseNode) { if (StringUtils.isEmpty(caseNode.getName()) || StringUtils.isEmpty(caseNode.getNodeType())) { return R.fail("未传节点名称或节点类型或图谱类型!"); } List byName = findByName(caseNode.getCaseId(), caseNode.getRecordsId(), caseNode.getNodeType(), caseNode.getName(), caseNode.getPicType()); if (byName != null && !byName.isEmpty()) { return R.fail("名称已存在"); } CaseNode res = null; try { Session session = driver.session(); StringBuffer cql = new StringBuffer(); Map params = new HashMap<>(); cql.append("CREATE (n:").append(caseNode.getNodeType()).append("{name:$name"); params.put("name", caseNode.getName()); if (StringUtils.isNotEmpty(caseNode.getRecordId())) { cql.append(", recordId:$recordId"); params.put("recordId", caseNode.getRecordId()); } if (StringUtils.isNotEmpty(caseNode.getRecordsId())) { cql.append(", recordsId:$recordsId"); params.put("recordsId", caseNode.getRecordsId()); } if (StringUtils.isNotEmpty(caseNode.getCaseId())) { cql.append(", caseId:$caseId"); params.put("caseId", caseNode.getCaseId()); } if (StringUtils.isNotEmpty(caseNode.getPicType())) { cql.append(", picType:$picType"); params.put("picType", caseNode.getPicType()); } cql.append("})").append(Neo4jUtils.NODE_RETURN); Result run = session.run(cql.toString(), params); res = Neo4jUtils.getOneNode(run); } catch (Exception e) { e.printStackTrace(); } return R.ok(res); } @Override public R delNode(Long id) { try { Session session = driver.session(); StringBuffer cql = new StringBuffer(); cql.append("MATCH (n) where id(n) = ").append(id).append(" DELETE n"); Result run = session.run(cql.toString()); } catch (Exception e) { e.printStackTrace(); } return R.ok(); } @Override public CaseNode findById(Long id) { CaseNode node = null; try { Session session = driver.session(); StringBuffer cql = new StringBuffer(); cql.append("MATCH (n) where id(n) = ").append(id).append(Neo4jUtils.NODE_RETURN); Result run = session.run(cql.toString()); node = Neo4jUtils.getOneNode(run); } catch (Exception e) { e.printStackTrace(); } return node; } @Override public List findByName(String caseId, String recordsId, String nodeType, String name, String picType) { List list = new ArrayList<>(); try { Session session = driver.session(); StringBuffer cql = new StringBuffer(); cql.append("MATCH (n"); if (StringUtils.isNotEmpty(nodeType)) { cql.append(":"); cql.append(nodeType); } cql.append(") where 1 = 1"); if (StringUtils.isNotEmpty(caseId)) { cql.append(" and n.caseId = "); cql.append(caseId); } if (StringUtils.isNotEmpty(recordsId)) { cql.append(" and n.recordsId = "); cql.append(recordsId); } if (StringUtils.isNotEmpty(name)) { cql.append(" and n.name = '"); cql.append(name); cql.append("'"); } if (StringUtils.isNotEmpty(picType)) { cql.append(" and n.picType = "); cql.append(picType); } cql.append(Neo4jUtils.NODE_RETURN); Result run = session.run(cql.toString()); list = Neo4jUtils.getNodeList(run); } catch (Exception e) { e.printStackTrace(); } return list; } @Override public CaseNode findOneByName(String caseId, String recordsId, String nodeType, String name, String picType) { CaseNode node = null; try { Session session = driver.session(); StringBuffer cql = new StringBuffer(); Map params = new HashMap<>(); cql.append("MATCH (n"); if (StringUtils.isNotEmpty(nodeType)) { cql.append(":"); cql.append(nodeType); } cql.append(") where 1 = 1"); if (StringUtils.isNotEmpty(caseId)) { cql.append(" and n.caseId = $caseId"); params.put("caseId", caseId); } if (StringUtils.isNotEmpty(recordsId)) { cql.append(" and n.recordsId = $recordsId"); params.put("recordsId", recordsId); } if (StringUtils.isNotEmpty(name)) { cql.append(" and n.name = $name"); params.put("name", name); } if (StringUtils.isNotEmpty(picType)) { cql.append(" and n.picType = $picType"); params.put("picType", picType); } cql.append(Neo4jUtils.NODE_RETURN); Result run = session.run(cql.toString(), params); node = Neo4jUtils.getOneNode(run); } catch (Exception e) { e.printStackTrace(); } return node; } @Override public Rel findRelation(Rel rel) { try { Session session = driver.session(); StringBuffer cql = new StringBuffer(); Map params = new HashMap<>(); cql.append("MATCH (a)-[rel:").append(rel.getName()).append("]->(b) where id(a) = $sourceId and id(b) = $targetId") .append(Neo4jUtils.REL_RETURN); params.put("sourceId", rel.getSourceId()); params.put("targetId", rel.getTargetId()); Result run = session.run(cql.toString(), params); rel = Neo4jUtils.getOneRel(run); } catch (Exception e) { e.printStackTrace(); } if (rel != null && rel.getId() != null) { return rel; } else { return null; } } @Override public R saveRelation(Rel rel) { Rel res = null; try { Session session = driver.session(); StringBuffer cql = new StringBuffer(); Map params = new HashMap<>(); cql.append("MATCH (a), (b) where id(a) = $sourceId and id(b) = $targetId CREATE(a)-[rel:").append(rel.getName()) .append("]->(b) ").append(Neo4jUtils.REL_RETURN); params.put("sourceId", rel.getSourceId()); params.put("targetId", rel.getTargetId()); Result run = session.run(cql.toString(), params); rel = Neo4jUtils.getOneRel(run); } catch (Exception e) { e.printStackTrace(); } if (rel != null) { return R.ok(rel); } else { return R.fail("保存失败"); } } @Override public R getNode(String picType, String caseId) { Map map = new HashMap<>(); List list = new ArrayList<>(); List> nodes = new ArrayList<>(); try { Session session = driver.session(); Map params = new HashMap<>(); params.put("picType", picType); params.put("caseId", caseId); Result run = session.run("MATCH (n)-[rel]->(r) where n.picType = r.picType = $picType and n.caseId = r.caseId = $caseId" + " RETURN id(rel) as id, n.name as source, id(n) as sourceId, type(rel) as name, r.name as target, id(r) as targetId", params); while (run.hasNext()) { Record record = run.next(); long id = record.get("id").asLong(); String source = record.get("source").asString(); long sourceId = record.get("sourceId").asLong(); String name = record.get("name").asString(); String target = record.get("target").asString(); long targetId = record.get("targetId").asLong(); list.add(new Rel(id, source, sourceId, name, target, targetId)); } Result node = session.run("MATCH (n) where n.picType = $picType and n.caseId = $caseId RETURN id(n) as id, n.name as name", params); while (node.hasNext()) { Record record = node.next(); String name = record.get("name").asString(); long idlong = record.get("id").asLong(); Map nodeMap = new HashMap<>(); nodeMap.put("name", name); nodeMap.put("entityName", name); // nodeMap.put("id", idlong + ""); nodes.add(nodeMap); } } catch (Exception e) { e.printStackTrace(); } map.put("list", list); map.put("nodes", nodes); return R.ok(map); } @Override public R test() { Session session = driver.session(); Map 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 = record.get("startId").asLong() + ""; String endId = record.get("endId").asLong() + ""; String relId = record.get("relId").asLong() + ""; System.out.println("************" + id); System.out.println("************" + endId); System.out.println("************" + relId); } return R.ok("222"); } }