diff --git a/src/main/java/com/supervision/neo4j/service/impl/Neo4jServiceImpl.java b/src/main/java/com/supervision/neo4j/service/impl/Neo4jServiceImpl.java index 362a7a2..1a9b564 100644 --- a/src/main/java/com/supervision/neo4j/service/impl/Neo4jServiceImpl.java +++ b/src/main/java/com/supervision/neo4j/service/impl/Neo4jServiceImpl.java @@ -272,6 +272,7 @@ public class Neo4jServiceImpl implements Neo4jService { Map map = new HashMap<>(); List list = new ArrayList<>(); List> nodes = new ArrayList<>(); + List> 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 sourceLabels = record.get("sourceLabels").asList(Value::asString); + List 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 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); Map 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); } } catch (Exception e) { log.error("查询失败", e); @@ -328,20 +333,46 @@ public class Neo4jServiceImpl implements Neo4jService { // 节点和关系合并 Pair, List>> pair = mergeRecord(distinctNodes, list); + // 将返回结果List>转为Map,并将其中key为color的键值对包一层itemStyle,将键值对放在里面 + List> nodesList = new ArrayList<>(); + for (Map node : pair.getValue()) { + Map itemStyle = new HashMap<>(); + itemStyle.put("color", node.get("color")); + Map 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> nodes, List> labelColorMap, List labels, long id, Map nodeMap) { + nodeMap.put("id", String.valueOf(id)); + for (Map labelColor : labelColorMap) { + if (labels.contains(labelColor.get("name"))) { + 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>> mergeRecord(List> nodes, List relDTOS) { Map 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> map = new HashMap<>(); + Map map = new HashMap<>(); Set nodeLabels = new HashSet<>(); Set relTypes = new HashSet<>(); try (Session session = driver.session()) { @@ -368,7 +399,17 @@ public class Neo4jServiceImpl implements Neo4jService { } log.info("查询到的节点类型{}个:{}", nodeLabels.size(), nodeLabels); log.info("查询到的关系类型{}个:{}", relTypes.size(), relTypes); - map.put("nodeLabels", nodeLabels); + // 将nodeLabels重新组装成List>,nodeLabels中的值作为key:name的值,另外创建一个key:itemStyle,值是Map,一个键值对是color+随机颜色,另一个是lightColor+随机颜色 + List> nodeLabelsList = nodeLabels.stream().map(label -> { + Map nodeLabelMap = new HashMap<>(); + Map itemStyle = new HashMap<>(); + itemStyle.put("color", Neo4jUtils.getRandomColorPair()[0]); + itemStyle.put("lightColor", Neo4jUtils.getRandomColorPair()[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 +456,7 @@ public class Neo4jServiceImpl implements Neo4jService { return nodes.stream().map(map -> { Map 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 +625,7 @@ public class Neo4jServiceImpl implements Neo4jService { } @Override - public List executeCypher(String cypher,Map parameters) { + public List executeCypher(String cypher, Map parameters) { return this.driver.session().run(cypher, parameters).list(); } } diff --git a/src/main/java/com/supervision/neo4j/utils/Neo4jUtils.java b/src/main/java/com/supervision/neo4j/utils/Neo4jUtils.java index 2e371dd..0514cd9 100644 --- a/src/main/java/com/supervision/neo4j/utils/Neo4jUtils.java +++ b/src/main/java/com/supervision/neo4j/utils/Neo4jUtils.java @@ -14,11 +14,26 @@ import org.neo4j.driver.internal.value.StringValue; import java.util.ArrayList; import java.util.List; import java.util.Optional; +import java.util.Random; public class Neo4jUtils { public static final String NODE_RETURN = " RETURN id(n) as id, n.name as name, n.nodeType as nodeType, n.recordId as recordId, n.caseId as caseId, n.picType as picType"; public static final String REL_RETURN = " RETURN id(rel) as id, a.name as source, id(a) as sourceId, type(rel) as name, b.name as target, id(b) as targetId"; + private static final String[][] COLORS = { + {"#0083C7", "#7FCOE2"}, + {"#09C6C0", "#83E2DF"}, + {"#2BCCFF", "#94E5FF"}, + {"#3763FF", "#9BB1FF"}, + {"#4274AF", "#A0B9D6"}, + {"#469C3D", "#A2CD9D"}, + {"#55CBAD", "#A9E4D5"}, + {"#75A0C8", "#B9CFE3"}, + {"#8D8BFF", "#C5C4FF"}, + {"#C76BFF", "#E2B5FF"}, + {"#D2955D", "#E8C9AD"}, + {"#FF8F51", "#FFC6A7"} + }; public static CaseNode getOneNode(Result run) { CaseNode node = null; @@ -117,4 +132,15 @@ public class Neo4jUtils { } + /** + * 随机返回一组颜色(深色和浅色) + * @return 一个字符串数组,包含深色和浅色 [深色, 浅色] + */ + public static String[] getRandomColorPair() { + Random random = new Random(); + // 随机选择一个颜色组 + int index = random.nextInt(COLORS.length); + return COLORS[index]; + } + } diff --git a/src/main/java/com/supervision/police/controller/ModelController.java b/src/main/java/com/supervision/police/controller/ModelController.java index bd54706..d635431 100644 --- a/src/main/java/com/supervision/police/controller/ModelController.java +++ b/src/main/java/com/supervision/police/controller/ModelController.java @@ -21,17 +21,11 @@ public class ModelController { private final ModelService modelService; - @Value("${fu-hsi-config.case-analysis-method}") - private String caseAnalysisMethod; @PostMapping("/analyseCase") @Operation(summary = "分析指标") public R analyseCase(@RequestBody AnalyseCaseDTO analyseCaseDTO) { - if (METHOD_NEW.equals(caseAnalysisMethod)) { - return modelService.analyseCaseNew(analyseCaseDTO); - } else { - return modelService.analyseCase(analyseCaseDTO); - } + return modelService.analyseCase(analyseCaseDTO); } @GetMapping("/caseScoreDetail") diff --git a/src/main/java/com/supervision/police/service/ModelService.java b/src/main/java/com/supervision/police/service/ModelService.java index f96900d..b160fe1 100644 --- a/src/main/java/com/supervision/police/service/ModelService.java +++ b/src/main/java/com/supervision/police/service/ModelService.java @@ -11,7 +11,6 @@ public interface ModelService { R analyseCase(AnalyseCaseDTO analyseCaseDTO); - R analyseCaseNew(AnalyseCaseDTO analyseCaseDTO); /** * 获取案件详情 diff --git a/src/main/java/com/supervision/police/service/impl/ModelServiceImpl.java b/src/main/java/com/supervision/police/service/impl/ModelServiceImpl.java index bb4b51d..c482ad2 100644 --- a/src/main/java/com/supervision/police/service/impl/ModelServiceImpl.java +++ b/src/main/java/com/supervision/police/service/impl/ModelServiceImpl.java @@ -97,86 +97,7 @@ public class ModelServiceImpl implements ModelService { private final CaseEvidencePropertyService caseEvidencePropertyService; @Override - @Transactional(transactionManager = "dataSourceTransactionManager", rollbackFor = Exception.class) public R analyseCase(AnalyseCaseDTO analyseCaseDTO) { - - ModelCase modelCase = modelCaseMapper.selectById(analyseCaseDTO.getCaseId()); - // 获取案件行为人ID - CasePerson casePerson = casePersonMapper.selectOne(new LambdaQueryWrapper() - .eq(CasePerson::getCaseId, analyseCaseDTO.getCaseId()) - .eq(CasePerson::getCaseActorFlag, 1) - .eq(CasePerson::getRoleCode, "1") - .eq(StrUtil.isNotEmpty(analyseCaseDTO.getLawActorName()), CasePerson::getName, analyseCaseDTO.getLawActorName())); - if (ObjectUtil.isEmpty(casePerson)) { - throw new RuntimeException("未找到的行为人" + analyseCaseDTO.getLawActorName()); - } - // 首先将原先的值进行赋值,设置到pre_result - modelIndexResultMapper.updatePreResult(analyseCaseDTO.getCaseId()); - //原子指标 - List atomicIndices = modelAtomicIndexMapper.selectByCaseType(modelCase.getCaseType()); - // 存放原子指标的结果,key:原子指标ID,value:(key大指标ID,value:结果) - Map> atomicResultMap = new HashMap<>(); - for (ModelAtomicIndex atomicIndex : atomicIndices) { - //原子指标结果 - ModelAtomicResult result = new ModelAtomicResult(); - result.setCasePersonId(casePerson.getId()); - result.setCaseId(analyseCaseDTO.getCaseId()); - result.setAtomicId(atomicIndex.getId()); - //查询语句 - String ql = atomicIndex.getQueryLang(); - - //原子指标结果表 - try { - //查询图谱 index_source: 1人工定义 2数据库查询 3图谱生成 4大模型 - if ("1".equals(atomicIndex.getIndexSource())) { - // list - manuallyDefinedCase(analyseCaseDTO, result, atomicIndex); - } else if ("2".equals(atomicIndex.getIndexSource()) && StringUtils.isNotEmpty(ql)) { - // - analyseDataBaseCase(analyseCaseDTO, result, ql, casePerson.getName()); - } else if ("3".equals(atomicIndex.getIndexSource()) && StringUtils.isNotEmpty(ql)) { - // 使用知识图谱进行计算 - analyseGraphCase(analyseCaseDTO, result, ql, casePerson.getName()); - } else if ("4".equals(atomicIndex.getIndexSource())) { - // - } else if ("5".equals(atomicIndex.getIndexSource())) { - // todo: 结构化推理 - //analyseStructuredInference(analyseCaseDTO, result); - } - } catch (Exception e) { - log.error(e.getMessage(), e); - } - // 根据原子指标ID查model_index_atomic_relation表所有的指标ID - List relationList = modelIndexAtomicRelationService.lambdaQuery().eq(ModelIndexAtomicRelation::getAtomicIndexId, atomicIndex.getId()).list(); - for (ModelIndexAtomicRelation relation : relationList) { - // 保存或更新原子指标结果表 - ModelAtomicResult exist = modelAtomicResultMapper.selectByCaseIdAndAtomicId(analyseCaseDTO.getCaseId(), casePerson.getId(), relation.getModelIndexId(), atomicIndex.getId()); - result.setIndexId(relation.getModelIndexId()); - if (exist == null) { - result.setId(null); - modelAtomicResultMapper.insert(result); - } else { - result.setId(exist.getId()); - modelAtomicResultMapper.updateById(result); - } - Map indexMap = atomicResultMap.computeIfAbsent( - result.getAtomicId(), - k -> new HashMap<>() // 如果不存在则创建一个新的 HashMap - ); - indexMap.put(relation.getModelIndexId(), result.getAtomicResult()); - } - - } - // 最终计算得分 - calculateFinalScore(analyseCaseDTO, modelCase, atomicResultMap); - caseStatusManageService.whenAnalyseCaseSuccess(analyseCaseDTO.getCaseId(), modelCase.getTotalScore()); - // 计算完成之后,把所有的笔录上传到模型 - noteRecordService.uploadFileToLangChainChat(analyseCaseDTO.getCaseId()); - return R.ok(); - } - - @Override - public R analyseCaseNew(AnalyseCaseDTO analyseCaseDTO) { log.info("数据准备阶段"); String caseId = analyseCaseDTO.getCaseId(); Map typeScoreMap = new HashMap<>(); diff --git a/src/main/java/com/supervision/police/vo/GraphReqVO.java b/src/main/java/com/supervision/police/vo/GraphReqVO.java index 9bf968e..e329426 100644 --- a/src/main/java/com/supervision/police/vo/GraphReqVO.java +++ b/src/main/java/com/supervision/police/vo/GraphReqVO.java @@ -2,7 +2,9 @@ package com.supervision.police.vo; import lombok.Data; +import java.util.ArrayList; import java.util.List; +import java.util.Map; /** * 图谱查询请求参数 @@ -29,4 +31,6 @@ public class GraphReqVO { * 关系类型(如:购买) */ private List relTypes; + + private List> nodeLabelColorMap = new ArrayList<>(); }