package com.supervision.police.service.impl; import cn.hutool.json.JSONUtil; import com.supervision.common.domain.R; import com.supervision.common.utils.StringUtils; import com.supervision.neo4j.dto.ResultDTO; import com.supervision.neo4j.utils.Neo4jUtils; import com.supervision.police.domain.*; import com.supervision.police.dto.AnalyseCaseDTO; import com.supervision.police.dto.AtomicData; import com.supervision.police.dto.JudgeLogic; import com.supervision.police.mapper.*; import com.supervision.police.service.ModelService; import org.neo4j.driver.Driver; import org.neo4j.driver.Result; import org.neo4j.driver.Session; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.*; @Service public class ModelServiceImpl implements ModelService { @Autowired private ModelCaseMapper modelCaseMapper; @Autowired private ModelAtomicIndexMapper modelAtomicIndexMapper; private final Driver driver; @Autowired private ModelServiceImpl(Driver driver) { this.driver = driver; } @Autowired private ModelAtomicResultMapper modelAtomicResultMapper; @Autowired private ModelIndexMapper modelIndexMapper; @Autowired private ModelIndexResultMapper modelIndexResultMapper; @Override public R analyseCase(AnalyseCaseDTO analyseCaseDTO) { ModelCase modelCase = modelCaseMapper.selectById(analyseCaseDTO.getCaseId()); //原子指标 List atomicIndices = modelAtomicIndexMapper.selectByCaseType(modelCase.getCaseType()); Map atomic = new HashMap<>(); for (ModelAtomicIndex atomicIndex : atomicIndices) { //原子指标结果 ModelAtomicResult result = new ModelAtomicResult(); result.setCaseId(analyseCaseDTO.getCaseId()); result.setAtomicId(atomicIndex.getId()); //查询语句 String ql = atomicIndex.getQueryLang(); Map params = new HashMap<>(); params.put("lawActor", analyseCaseDTO.getLawActor()); params.put("caseId", analyseCaseDTO.getCaseId()); //原子指标结果表 try { // index_source==1 List list = analyseCaseDTO.getAtomicIndexList(); //index_source==3 Session session = driver.session(); //查询图谱 index_source: 1人工定义 2数据库查询 3图谱生成 4大模型 if ("1".endsWith(atomicIndex.getIndexSource())) { // list } else if ("2".endsWith(atomicIndex.getIndexSource()) && StringUtils.isNotEmpty(ql)) { // } else if ("3".endsWith(atomicIndex.getIndexSource()) && StringUtils.isNotEmpty(ql)) { //图谱 int i = 1; String lawPartys = analyseCaseDTO.getLawParty(); String[] split = StringUtils.isEmpty(lawPartys) ? new String[0] : lawPartys.split(","); if (ql.contains("$lawParty") && split.length > 1) { i = split.length; } for (int j = 0; j < i; j++) { if (split.length > 0) { params.put("lawParty", split[j]); } Result run = session.run(ql, params); List res = Neo4jUtils.getResultDTOList(run); if (res.isEmpty()) { result.setAtomicResult("-1"); } else { ResultDTO resultDTO = res.get(0); if (StringUtils.isNotEmpty(resultDTO.getRelId())) { //存在关系 result.setAtomicResult("1"); result.setRecordId(resultDTO.getRecordId()); result.setRecordsId(resultDTO.getRecordsId()); break; } else { result.setAtomicResult("0"); } } } } else if ("4".endsWith(atomicIndex.getIndexSource())) { // } } catch (Exception e) { e.printStackTrace(); } //保存原子指标结果表 ModelAtomicResult exist = modelAtomicResultMapper.selectByCaseIdAndAtomicId(analyseCaseDTO.getCaseId(), atomicIndex.getId()); if (exist == null) { modelAtomicResultMapper.insert(result); } else { result.setId(exist.getId()); modelAtomicResultMapper.updateById(result); } // 所有原子指标id atomic.put(result.getAtomicId(), "1".equals(result.getAtomicResult())); } // 计算指标结果 int score = 0; List modelIndices = modelIndexMapper.selectByCaseType(modelCase.getCaseType()); for (ModelIndex modelIndex : modelIndices) { ModelIndexResult result = new ModelIndexResult(); result.setCaseId(analyseCaseDTO.getCaseId()); result.setIndexId(modelIndex.getId()); Set atomicIds = new HashSet<>(); if (StringUtils.isNotEmpty(modelIndex.getJudgeLogic())) { List judgeLogics = JSONUtil.toList(modelIndex.getJudgeLogic(), JudgeLogic.class); boolean res = false; for (int i = 0; i < judgeLogics.size(); i++) { boolean group = false; JudgeLogic logic = judgeLogics.get(i); String rowLogic = logic.getRowLogic(); List atomicData = logic.getAtomicData(); for (int j = 0; j < atomicData.size(); j++) { AtomicData data = atomicData.get(j); atomicIds.add(data.getAtomicIndex()); Boolean ato = atomic.get(data.getAtomicIndex()); String relationalSymbol = data.getRelationalSymbol(); if ("2".equals(relationalSymbol) || "4".equals(relationalSymbol) || "5".equals(relationalSymbol)) { ato = !ato; } if (j == 0) { group = ato; } else { if ("1".equals(rowLogic)) { group = group && ato; } else if ("2".equals(rowLogic)) { group = group || ato; } } } String groupLogic = logic.getGroupLogic(); if (i == 0) { res = group; } else { if ("1".equals(groupLogic)) { res = res && group; } else if ("2".equals(groupLogic)) { res = res || group; } } } result.setIndexResult(res? "true" : "false"); result.setAtomicIds(StringUtils.join(atomicIds, ",")); } ModelIndexResult exist = modelIndexResultMapper.selectByCaseIdAndIndexId(analyseCaseDTO.getCaseId(), modelIndex.getId()); if (exist == null) { modelIndexResultMapper.insert(result); } else { result.setId(exist.getId()); modelIndexResultMapper.updateById(result); } if ("true".equals(result.getIndexResult())) { score = score + modelIndex.getIndexScore(); } } modelCase.setTotalScore(score); modelCaseMapper.updateById(modelCase); return R.ok(); } }