1. 指标分析添加数据库查询计算方式

topo_dev
xueqingkun 9 months ago
parent 8f51b964f8
commit be3946dbfd

@ -35,6 +35,7 @@ public enum ResultStatusEnum {
USER_DOES_NOT_EXIST(507, "用户不存在,操作失败!"), USER_DOES_NOT_EXIST(507, "用户不存在,操作失败!"),
ILLEGAL_ARGUMENT(508, "参数校验失败!"), ILLEGAL_ARGUMENT(508, "参数校验失败!"),
RUNTIME_EXCEPTION(509, "程序运行异常!"),
IMPORT_COMPANY_FORMAT_ERROR(521,"Excel表格格式错误"), IMPORT_COMPANY_FORMAT_ERROR(521,"Excel表格格式错误"),
IMPORT_COMPANY_FAIL(522,"部分数据导入失败"), IMPORT_COMPANY_FAIL(522,"部分数据导入失败"),
INSERT_FAIL(600,"新增失败"), INSERT_FAIL(600,"新增失败"),

@ -32,5 +32,11 @@ public class ExceptionHandlerConfig {
return R.fail(ResultStatusEnum.ILLEGAL_ARGUMENT,exception.getMessage()); return R.fail(ResultStatusEnum.ILLEGAL_ARGUMENT,exception.getMessage());
} }
@ExceptionHandler(RuntimeException.class)
public R<?> manualValidationExceptionResponse(RuntimeException exception) {
log.error("=========运行异常=========>>>");
log.error(exception.getMessage(), exception);
log.error("<<<=========运行异常=========");
return R.fail(ResultStatusEnum.RUNTIME_EXCEPTION,exception.getMessage());
}
} }

@ -1,6 +1,7 @@
package com.supervision.police.dto; package com.supervision.police.dto;
import com.supervision.police.domain.ModelAtomicIndex; import com.supervision.police.domain.ModelAtomicIndex;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data; import lombok.Data;
import java.util.List; import java.util.List;
@ -23,6 +24,10 @@ public class AnalyseCaseDTO {
*/ */
private String lawParty; private String lawParty;
@Schema(description = "证据名称")
private String evidenceName;
/** /**
* *
*/ */

@ -0,0 +1,197 @@
package com.supervision.police.mybatis;
import cn.hutool.crypto.digest.MD5;
import org.apache.ibatis.builder.StaticSqlSource;
import org.apache.ibatis.mapping.*;
import org.apache.ibatis.scripting.LanguageDriver;
import org.apache.ibatis.session.Configuration;
import java.util.ArrayList;
import java.util.Map;
/**
* mybatisMappedStatement
*/
public class RowMapperStatementBuilder {
private final Configuration configuration;
private final LanguageDriver languageDriver;
public RowMapperStatementBuilder(Configuration configuration) {
this.configuration = configuration;
this.languageDriver = configuration.getDefaultScriptingLanguageInstance();
}
/**
* MSID
*
* @param sql sql
* @param sql sqlCommandType
* @return
*/
private String generateMappedStatementId(String sql, SqlCommandType sqlCommandType) {
return sqlCommandType.toString() + "." + MD5.create().digestHex(sql);
}
/**
* ID
*
* @param msId
* @return
*/
private boolean hasMappedStatement(String msId) {
return configuration.hasStatement(msId, false);
}
/**
* MS
*
* @param msId
* @param sqlSource sqlSource
* @param resultType
*/
private void cacheSelectMappedStatement(String msId, SqlSource sqlSource, final Class<?> resultType) {
if (configuration.hasStatement(msId,false)){
return;
}
MappedStatement ms = new MappedStatement.Builder(configuration, msId, sqlSource, SqlCommandType.SELECT)
.resultMaps(new ArrayList<ResultMap>() {
{
add(new ResultMap.Builder(configuration, "defaultResultMap", resultType, new ArrayList<ResultMapping>(0)).build());
}
})
.build();
//缓存MappedStatement
configuration.addMappedStatement(ms);
}
/**
* MS
*
* @param msId
* @param sqlSource sqlSource
* @param sqlCommandType sqlCommandType
*/
private void newUpdateMappedStatement(String msId, SqlSource sqlSource, SqlCommandType sqlCommandType) {
MappedStatement ms = new MappedStatement.Builder(configuration, msId, sqlSource, sqlCommandType)
.resultMaps(new ArrayList<ResultMap>() {
{
add(new ResultMap.Builder(configuration, "defaultResultMap", int.class, new ArrayList<ResultMapping>(0)).build());
}
})
.build();
//缓存
configuration.addMappedStatement(ms);
}
public String select(String sql) {
String msId = generateMappedStatementId(sql, SqlCommandType.SELECT);
if (hasMappedStatement(msId)) {
return msId;
}
StaticSqlSource sqlSource = new StaticSqlSource(configuration, sql);
cacheSelectMappedStatement(msId, sqlSource, Map.class);
return msId;
}
public MappedStatement selectMappedStatement(String sql, Class<?> resultType) {
String msId = generateMappedStatementId(sql, SqlCommandType.SELECT);
if (hasMappedStatement(msId)) {
return configuration.getMappedStatement(msId, false);
}
StaticSqlSource sqlSource = new StaticSqlSource(configuration, sql);
cacheSelectMappedStatement(msId, sqlSource, resultType);
return configuration.getMappedStatement(msId, false);
}
public String selectDynamic(String sql, Class<?> parameterType) {
String msId = generateMappedStatementId(sql , SqlCommandType.SELECT);
if (hasMappedStatement(msId)) {
return msId;
}
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, parameterType);
cacheSelectMappedStatement(msId, sqlSource, Map.class);
return msId;
}
public String select(String sql, Class<?> resultType) {
String msId = generateMappedStatementId(resultType + sql, SqlCommandType.SELECT);
if (hasMappedStatement(msId)) {
return msId;
}
StaticSqlSource sqlSource = new StaticSqlSource(configuration, sql);
cacheSelectMappedStatement(msId, sqlSource, resultType);
return msId;
}
public String selectDynamic(String sql, Class<?> parameterType, Class<?> resultType) {
String msId = generateMappedStatementId(resultType + sql + parameterType, SqlCommandType.SELECT);
if (hasMappedStatement(msId)) {
return msId;
}
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, parameterType);
cacheSelectMappedStatement(msId, sqlSource, resultType);
return msId;
}
public String insert(String sql) {
String msId = generateMappedStatementId(sql, SqlCommandType.INSERT);
if (hasMappedStatement(msId)) {
return msId;
}
StaticSqlSource sqlSource = new StaticSqlSource(configuration, sql);
newUpdateMappedStatement(msId, sqlSource, SqlCommandType.INSERT);
return msId;
}
public String insertDynamic(String sql, Class<?> parameterType) {
String msId = generateMappedStatementId(sql + parameterType, SqlCommandType.INSERT);
if (hasMappedStatement(msId)) {
return msId;
}
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, parameterType);
newUpdateMappedStatement(msId, sqlSource, SqlCommandType.INSERT);
return msId;
}
public String update(String sql) {
String msId = generateMappedStatementId(sql, SqlCommandType.UPDATE);
if (hasMappedStatement(msId)) {
return msId;
}
StaticSqlSource sqlSource = new StaticSqlSource(configuration, sql);
newUpdateMappedStatement(msId, sqlSource, SqlCommandType.UPDATE);
return msId;
}
public String updateDynamic(String sql, Class<?> parameterType) {
String msId = generateMappedStatementId(sql + parameterType, SqlCommandType.UPDATE);
if (hasMappedStatement(msId)) {
return msId;
}
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, parameterType);
newUpdateMappedStatement(msId, sqlSource, SqlCommandType.UPDATE);
return msId;
}
public String delete(String sql) {
String msId = generateMappedStatementId(sql, SqlCommandType.DELETE);
if (hasMappedStatement(msId)) {
return msId;
}
StaticSqlSource sqlSource = new StaticSqlSource(configuration, sql);
newUpdateMappedStatement(msId, sqlSource, SqlCommandType.DELETE);
return msId;
}
public String deleteDynamic(String sql, Class<?> parameterType) {
String msId = generateMappedStatementId(sql + parameterType, SqlCommandType.DELETE);
if (hasMappedStatement(msId)) {
return msId;
}
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, parameterType);
newUpdateMappedStatement(msId, sqlSource, SqlCommandType.DELETE);
return msId;
}
}

@ -0,0 +1,239 @@
package com.supervision.police.mybatis;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.exceptions.TooManyResultsException;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
/**
* mybatissql
*/
@Slf4j
@Component
public class RowSqlMapper {
private final RowMapperStatementBuilder mapperStatementBuilder;
private final SqlSession sqlSession;
/**
* MappedStatement
*
* @param sqlSession
*/
public RowSqlMapper(SqlSession sqlSession) {
this.sqlSession = sqlSession;
this.mapperStatementBuilder = new RowMapperStatementBuilder(sqlSession.getConfiguration());
}
/**
* List
*
* @param list List
* @param <T>
* @return
*/
private <T> T getOne(List<T> list) {
if (list.size() == 1) {
return list.get(0);
} else if (list.size() > 1) {
throw new TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found: " + list.size());
} else {
return null;
}
}
/**
*
*
* @param sql sql
* @return
*/
public Map<String, Object> selectOne(String sql) {
List<Map<String, Object>> list = selectList(sql);
return getOne(list);
}
/**
*
*
* @param sql sql
* @param value
* @return
*/
public Map<String, Object> selectOne(String sql, Object value) {
List<Map<String, Object>> list = selectList(sql, value);
return getOne(list);
}
/**
*
*
* @param sql sql
* @param resultType
* @param <T>
* @return
*/
public <T> T selectOne(String sql, Class<T> resultType) {
List<T> list = selectList(sql, resultType);
return getOne(list);
}
/**
*
*
* @param sql sql
* @param value
* @param resultType
* @param <T>
* @return
*/
public <T> T selectOne(String sql, Object value, Class<T> resultType) {
List<T> list = selectList(sql, value, resultType);
return getOne(list);
}
/**
* List<Map<String, Object>>
*
* @param sql sql
* @return
*/
public List<Map<String, Object>> selectList(String sql) {
String msId = mapperStatementBuilder.select(sql);
return sqlSession.selectList(msId);
}
/**
* List<Map<String, Object>>
*
* @param sql sql
* @param value
* @return
*/
public List<Map<String, Object>> selectList(String sql, Object value) {
log.info("selectList sql:{},params:{}", sql, JSONUtil.toJsonStr(value));
Class<?> parameterType = value != null ? value.getClass() : null;
String msId = mapperStatementBuilder.selectDynamic(sql, parameterType);
return sqlSession.selectList(msId, value);
}
/**
*
*
* @param sql sql
* @param resultType
* @param <T>
* @return
*/
public <T> List<T> selectList(String sql, Class<T> resultType) {
String msId;
if (resultType == null) {
msId = mapperStatementBuilder.select(sql);
} else {
msId = mapperStatementBuilder.select(sql, resultType);
}
return sqlSession.selectList(msId);
}
/**
*
*
* @param sql sql
* @param value
* @param resultType
* @param <T>
* @return
*/
public <T> List<T> selectList(String sql, Object value, Class<T> resultType) {
String msId;
Class<?> parameterType = value != null ? value.getClass() : null;
if (resultType == null) {
msId = mapperStatementBuilder.selectDynamic(sql, parameterType);
} else {
msId = mapperStatementBuilder.selectDynamic(sql, parameterType, resultType);
}
return sqlSession.selectList(msId, value);
}
/**
*
*
* @param sql sql
* @return
*/
public int insert(String sql) {
String msId = mapperStatementBuilder.insert(sql);
return sqlSession.insert(msId);
}
/**
*
*
* @param sql sql
* @param value
* @return
*/
public int insert(String sql, Object value) {
Class<?> parameterType = value != null ? value.getClass() : null;
String msId = mapperStatementBuilder.insertDynamic(sql, parameterType);
return sqlSession.insert(msId, value);
}
/**
*
*
* @param sql sql
* @return
*/
public int update(String sql) {
String msId = mapperStatementBuilder.update(sql);
return sqlSession.update(msId);
}
/**
*
*
* @param sql sql
* @param value
* @return
*/
public int update(String sql, Object value) {
Class<?> parameterType = value != null ? value.getClass() : null;
String msId = mapperStatementBuilder.updateDynamic(sql, parameterType);
return sqlSession.update(msId, value);
}
/**
*
*
* @param sql sql
* @return
*/
public int delete(String sql) {
String msId = mapperStatementBuilder.delete(sql);
return sqlSession.delete(msId);
}
/**
*
*
* @param sql sql
* @param value
* @return
*/
public int delete(String sql, Object value) {
Class<?> parameterType = value != null ? value.getClass() : null;
String msId = mapperStatementBuilder.deleteDynamic(sql, parameterType);
return sqlSession.delete(msId, value);
}
public MappedStatement selectMappedStatement(String sql, Class<?> resultType) {
return mapperStatementBuilder.selectMappedStatement(sql, resultType);
}
}

@ -2,6 +2,7 @@ package com.supervision.police.service.impl;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
@ -15,6 +16,7 @@ import com.supervision.police.dto.AtomicData;
import com.supervision.police.dto.JudgeLogic; import com.supervision.police.dto.JudgeLogic;
import com.supervision.police.dto.caseScore.CaseScoreDetailBuilder; import com.supervision.police.dto.caseScore.CaseScoreDetailBuilder;
import com.supervision.police.mapper.*; import com.supervision.police.mapper.*;
import com.supervision.police.mybatis.RowSqlMapper;
import com.supervision.police.service.ModelService; import com.supervision.police.service.ModelService;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -44,6 +46,8 @@ public class ModelServiceImpl implements ModelService {
private final CasePersonMapper casePersonMapper; private final CasePersonMapper casePersonMapper;
private final RowSqlMapper rowSqlMapper;
@Override @Override
public R<?> analyseCase(AnalyseCaseDTO analyseCaseDTO) { public R<?> analyseCase(AnalyseCaseDTO analyseCaseDTO) {
ModelCase modelCase = modelCaseMapper.selectById(analyseCaseDTO.getCaseId()); ModelCase modelCase = modelCaseMapper.selectById(analyseCaseDTO.getCaseId());
@ -76,6 +80,7 @@ public class ModelServiceImpl implements ModelService {
// list // list
} else if ("2".endsWith(atomicIndex.getIndexSource()) && StringUtils.isNotEmpty(ql)) { } else if ("2".endsWith(atomicIndex.getIndexSource()) && StringUtils.isNotEmpty(ql)) {
// //
analyseDataBaseCase(analyseCaseDTO, result, ql);
} else if ("3".endsWith(atomicIndex.getIndexSource()) && StringUtils.isNotEmpty(ql)) { } else if ("3".endsWith(atomicIndex.getIndexSource()) && StringUtils.isNotEmpty(ql)) {
// 使用知识图谱进行计算 // 使用知识图谱进行计算
analyseGraphCase(analyseCaseDTO, result, ql); analyseGraphCase(analyseCaseDTO, result, ql);
@ -272,4 +277,59 @@ public class ModelServiceImpl implements ModelService {
} }
} }
/**
*
* @param analyseCaseDTO
* @param result
* @param sql
*/
private void analyseDataBaseCase(AnalyseCaseDTO analyseCaseDTO, ModelAtomicResult result, String sql) {
Map<String, Object> params = new HashMap<>();
params.put("caseId", result.getCaseId());
params.put("evidenceName", analyseCaseDTO.getEvidenceName());
params.put("provider", null);
params.put("party_a", analyseCaseDTO.getLawActorName());
params.put("party_b", analyseCaseDTO.getLawParty());
/*
todo:sqlsqlselectsql
MappedStatement mappedStatement = rowSqlMapper.selectMappedStatement(sql, Map.class);
BoundSql boundSql = mappedStatement.getBoundSql(params);
String sql1 = boundSql.getSql();
*/
boolean b = parseResult(rowSqlMapper.selectList(sql, params, Map.class));
result.setAtomicResult(b ? "1" : "0");
}
/**
*
* 1. 11=100=0
*2.
* @param mapList
* @return
*/
private boolean parseResult(List<Map> mapList) {
if (CollUtil.isEmpty(mapList)) {
return false;
}
if (CollUtil.size(mapList) > 1){
return true;
}
Map firstRow = CollUtil.getFirst(mapList);
if (firstRow.size() == 1 && CollUtil.size(firstRow.values())==1) {
Object first = CollUtil.getFirst(firstRow.values());
if (NumberUtil.isNumber(first.toString())) {
return NumberUtil.parseInt(first.toString()) > 0;
}
}
return true;
}
} }

Loading…
Cancel
Save