原子指标配置代码优化

topo_dev
liu 10 months ago
parent 485744c598
commit 38ea4c2d1b

@ -1,12 +1,13 @@
package com.supervision.police.service; package com.supervision.police.service;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.supervision.police.domain.ModelAtomicIndex; import com.supervision.police.domain.ModelAtomicIndex;
import com.supervision.police.mapper.ModelAtomicIndexMapper; import com.supervision.police.mapper.ModelAtomicIndexMapper;
import java.util.List; import java.util.List;
public interface ModelAtomicIndexService { public interface ModelAtomicIndexService extends IService<ModelAtomicIndex> {
ModelAtomicIndexMapper getMapper(); ModelAtomicIndexMapper getMapper();

@ -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.ObjectUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.alibaba.druid.sql.ast.SQLStatement; import com.alibaba.druid.sql.ast.SQLStatement;
@ -61,8 +62,9 @@ public class ModelIndexServiceImpl extends ServiceImpl<ModelIndexMapper, ModelIn
@Value("${case.evidence.table}") @Value("${case.evidence.table}")
private List<String> allowedTables; private List<String> allowedTables;
@Override @Override
@Transactional(transactionManager = "dataSourceTransactionManager",rollbackFor = Exception.class) @Transactional(transactionManager = "dataSourceTransactionManager", rollbackFor = Exception.class)
public R<?> selectAll(ModelIndex modelIndex, Integer page, Integer size) { public R<?> selectAll(ModelIndex modelIndex, Integer page, Integer size) {
IPage<ModelIndex> iPage = new Page<>(page, size); IPage<ModelIndex> iPage = new Page<>(page, size);
LambdaQueryWrapper<ModelIndex> wrapper = Wrappers.lambdaQuery(); LambdaQueryWrapper<ModelIndex> wrapper = Wrappers.lambdaQuery();
@ -99,7 +101,7 @@ public class ModelIndexServiceImpl extends ServiceImpl<ModelIndexMapper, ModelIn
} }
@Override @Override
@Transactional(transactionManager = "dataSourceTransactionManager",rollbackFor = Exception.class) @Transactional(transactionManager = "dataSourceTransactionManager", rollbackFor = Exception.class)
public R<?> addOrUpd(ModelIndex modelIndex) { public R<?> addOrUpd(ModelIndex modelIndex) {
int i = 0; int i = 0;
if (StringUtils.isEmpty(modelIndex.getId())) { if (StringUtils.isEmpty(modelIndex.getId())) {
@ -115,7 +117,7 @@ public class ModelIndexServiceImpl extends ServiceImpl<ModelIndexMapper, ModelIn
} }
@Override @Override
@Transactional(transactionManager = "dataSourceTransactionManager",rollbackFor = Exception.class) @Transactional(transactionManager = "dataSourceTransactionManager", rollbackFor = Exception.class)
public R<?> del(String id) { public R<?> del(String id) {
ModelIndex index = modelIndexMapper.selectById(id); ModelIndex index = modelIndexMapper.selectById(id);
index.setDataStatus(StringUtils.getUUID()); index.setDataStatus(StringUtils.getUUID());
@ -128,7 +130,7 @@ public class ModelIndexServiceImpl extends ServiceImpl<ModelIndexMapper, ModelIn
} }
@Override @Override
@Transactional(transactionManager = "dataSourceTransactionManager",rollbackFor = Exception.class) @Transactional(transactionManager = "dataSourceTransactionManager", rollbackFor = Exception.class)
public R<?> selectAllAtomic(ModelAtomicIndex modelAtomicIndex, Integer page, Integer size) { public R<?> selectAllAtomic(ModelAtomicIndex modelAtomicIndex, Integer page, Integer size) {
IPage<ModelAtomicIndex> iPage = new Page<>(page, size); IPage<ModelAtomicIndex> iPage = new Page<>(page, size);
iPage = modelAtomicIndexService.selectAll(iPage, modelAtomicIndex); iPage = modelAtomicIndexService.selectAll(iPage, modelAtomicIndex);
@ -144,14 +146,19 @@ public class ModelIndexServiceImpl extends ServiceImpl<ModelIndexMapper, ModelIn
} }
@Override @Override
@Transactional(transactionManager = "dataSourceTransactionManager",rollbackFor = Exception.class) @Transactional(transactionManager = "dataSourceTransactionManager", rollbackFor = Exception.class)
public R<?> addOrUpdAtomic(ModelAtomicIndex modelAtomicIndex) { public R<?> addOrUpdAtomic(ModelAtomicIndex modelAtomicIndex) {
int i = 0; int i = 0;
if (StringUtils.equals("2", modelAtomicIndex.getIndexSource())){ if (StringUtils.equals("2", modelAtomicIndex.getIndexSource())) {
// 如果查询类型为数据查询,则校验查询语句 // 如果查询类型为数据查询,则校验查询语句
Assert.notEmpty(modelAtomicIndex.getQueryLang(), "查询语言不能为空"); Assert.notEmpty(modelAtomicIndex.getQueryLang(), "查询语言不能为空");
Assert.isTrue(checkSql(modelAtomicIndex.getQueryLang()), "查询语句不合法"); Assert.isTrue(checkSql(modelAtomicIndex.getQueryLang()), "查询语句不合法");
} }
// 校验是否已经存在了相同名称的原子指标
ModelAtomicIndex existIndex = modelAtomicIndexService.lambdaQuery().eq(ModelAtomicIndex::getName, modelAtomicIndex.getName()).last("limit 1").one();
if (ObjectUtil.isNotEmpty(existIndex) && !StringUtils.equals(modelAtomicIndex.getId(), existIndex.getId())){
throw new RuntimeException("已存在相同名称的原子指标");
}
if (StringUtils.isEmpty(modelAtomicIndex.getId())) { if (StringUtils.isEmpty(modelAtomicIndex.getId())) {
i = modelAtomicIndexService.getMapper().insert(modelAtomicIndex); i = modelAtomicIndexService.getMapper().insert(modelAtomicIndex);
} else { } else {
@ -165,7 +172,7 @@ public class ModelIndexServiceImpl extends ServiceImpl<ModelIndexMapper, ModelIn
} }
@Override @Override
@Transactional(transactionManager = "dataSourceTransactionManager",rollbackFor = Exception.class) @Transactional(transactionManager = "dataSourceTransactionManager", rollbackFor = Exception.class)
public R<?> delAtomic(String id) { public R<?> delAtomic(String id) {
ModelAtomicIndex index = modelAtomicIndexService.getMapper().selectById(id); ModelAtomicIndex index = modelAtomicIndexService.getMapper().selectById(id);
index.setDataStatus(StringUtils.getUUID()); index.setDataStatus(StringUtils.getUUID());
@ -178,7 +185,7 @@ public class ModelIndexServiceImpl extends ServiceImpl<ModelIndexMapper, ModelIn
} }
@Override @Override
@Transactional(transactionManager = "dataSourceTransactionManager",rollbackFor = Exception.class) @Transactional(transactionManager = "dataSourceTransactionManager", rollbackFor = Exception.class)
public List<CaseAtomicIndexDTO> listCaseAtomicIndex(String caseId, String indexSource) { public List<CaseAtomicIndexDTO> listCaseAtomicIndex(String caseId, String indexSource) {
Assert.notEmpty(caseId, "案件id不能为空"); Assert.notEmpty(caseId, "案件id不能为空");
@ -194,7 +201,7 @@ public class ModelIndexServiceImpl extends ServiceImpl<ModelIndexMapper, ModelIn
List<ModelIndex> modelIndexList = modelIndexMapper.selectList( List<ModelIndex> modelIndexList = modelIndexMapper.selectList(
Wrappers.lambdaQuery(ModelIndex.class) Wrappers.lambdaQuery(ModelIndex.class)
.eq(ModelIndex::getCaseType, caseType).eq(ModelIndex::getDataStatus, "1")); .eq(ModelIndex::getCaseType, caseType).eq(ModelIndex::getDataStatus, "1"));
if (CollUtil.isEmpty(modelIndexList)){ if (CollUtil.isEmpty(modelIndexList)) {
return new ArrayList<>(1); return new ArrayList<>(1);
} }
@ -203,13 +210,13 @@ public class ModelIndexServiceImpl extends ServiceImpl<ModelIndexMapper, ModelIn
.map(modelIndex -> pickAtomicIndexIds(modelIndex.getJudgeLogic())) .map(modelIndex -> pickAtomicIndexIds(modelIndex.getJudgeLogic()))
.flatMap(Collection::stream).distinct().toList(); .flatMap(Collection::stream).distinct().toList();
if (CollUtil.isEmpty(automicIndexIds)){ if (CollUtil.isEmpty(automicIndexIds)) {
return new ArrayList<>(1); return new ArrayList<>(1);
} }
// 查询原子指标相关信息 // 查询原子指标相关信息
List<ModelAtomicIndex> modelAtomicIndexList = modelAtomicIndexService.listCaseAtomicIndex(automicIndexIds, caseType, indexSource); List<ModelAtomicIndex> modelAtomicIndexList = modelAtomicIndexService.listCaseAtomicIndex(automicIndexIds, caseType, indexSource);
if (CollUtil.isEmpty(modelAtomicIndexList)){ if (CollUtil.isEmpty(modelAtomicIndexList)) {
return new ArrayList<>(1); return new ArrayList<>(1);
} }
Map<String, ModelAtomicIndex> modelAtomicIndexMap = modelAtomicIndexList.stream().collect(Collectors.toMap(ModelAtomicIndex::getId, v -> v, (v1, v2) -> v1)); Map<String, ModelAtomicIndex> modelAtomicIndexMap = modelAtomicIndexList.stream().collect(Collectors.toMap(ModelAtomicIndex::getId, v -> v, (v1, v2) -> v1));
@ -233,7 +240,7 @@ public class ModelIndexServiceImpl extends ServiceImpl<ModelIndexMapper, ModelIn
} }
@Override @Override
@Transactional(transactionManager = "dataSourceTransactionManager",rollbackFor = Exception.class) @Transactional(transactionManager = "dataSourceTransactionManager", rollbackFor = Exception.class)
public Boolean saveCaseAtomicResult(CaseAtomicResultWrapper caseAtomicResultWrapper) { public Boolean saveCaseAtomicResult(CaseAtomicResultWrapper caseAtomicResultWrapper) {
Assert.notEmpty(caseAtomicResultWrapper.getCaseId(), "案件id不能为空"); Assert.notEmpty(caseAtomicResultWrapper.getCaseId(), "案件id不能为空");
@ -247,8 +254,8 @@ public class ModelIndexServiceImpl extends ServiceImpl<ModelIndexMapper, ModelIn
//清空人工评估结果 //清空人工评估结果
removeCaseAtomicResult(caseAtomicResultWrapper.getCaseId(),modelCase.getCaseType(), removeCaseAtomicResult(caseAtomicResultWrapper.getCaseId(), modelCase.getCaseType(),
caseAtomicResultWrapper.getActorId(),"1"); caseAtomicResultWrapper.getActorId(), "1");
// 保存原子评估结果 // 保存原子评估结果
caseAtomicResultWrapper.getCaseAtomicIndexList().stream().map(caseAtomicIndexDTO -> caseAtomicResultWrapper.getCaseAtomicIndexList().stream().map(caseAtomicIndexDTO ->
@ -266,7 +273,7 @@ public class ModelIndexServiceImpl extends ServiceImpl<ModelIndexMapper, ModelIn
if (StringUtils.isEmpty(sql)) { if (StringUtils.isEmpty(sql)) {
return false; return false;
} }
if (CollUtil.isEmpty(this.allowedTables)){ if (CollUtil.isEmpty(this.allowedTables)) {
log.info("checkSql:未配置允许的表"); log.info("checkSql:未配置允许的表");
return false; return false;
} }
@ -282,14 +289,14 @@ public class ModelIndexServiceImpl extends ServiceImpl<ModelIndexMapper, ModelIn
return false; return false;
} }
List<String> tableList = SqlParserUtil.extractTableNames(sqlStatement); List<String> tableList = SqlParserUtil.extractTableNames(sqlStatement);
if (CollUtil.isEmpty(tableList)){ if (CollUtil.isEmpty(tableList)) {
log.warn("checkSql:未检测到表"); log.warn("checkSql:未检测到表");
return false; return false;
} }
long count = tableList.stream().filter(table -> !this.allowedTables.contains(table)).count(); long count = tableList.stream().filter(table -> !this.allowedTables.contains(table)).count();
if (count > 0){ if (count > 0) {
log.warn("checkSql:表{}不在允许的表列表中",tableList); log.warn("checkSql:表{}不在允许的表列表中", tableList);
return false; return false;
} }
return true; return true;
@ -297,21 +304,24 @@ public class ModelIndexServiceImpl extends ServiceImpl<ModelIndexMapper, ModelIn
/** /**
* *
*
* @param caseId id * @param caseId id
* @param caseType * @param caseType
* @param actorId id * @param actorId id
* @param indexSource * @param indexSource
*/ */
private void removeCaseAtomicResult(String caseId,String caseType, String actorId,String indexSource) { private void removeCaseAtomicResult(String caseId, String caseType, String actorId, String indexSource) {
List<ModelAtomicIndex> modelAtomicIndexList = modelAtomicIndexService.listCaseAtomicIndex(null, caseType, indexSource); List<ModelAtomicIndex> modelAtomicIndexList = modelAtomicIndexService.listCaseAtomicIndex(null, caseType, indexSource);
List<String> automicIndexIds = modelAtomicIndexList.stream().map(ModelAtomicIndex::getId).distinct().toList(); List<String> automicIndexIds = modelAtomicIndexList.stream().map(ModelAtomicIndex::getId).distinct().toList();
modelAtomicResultMapper.delete(Wrappers.lambdaQuery(ModelAtomicResult.class) modelAtomicResultMapper.delete(Wrappers.lambdaQuery(ModelAtomicResult.class)
.eq(ModelAtomicResult::getCaseId, caseId) .eq(ModelAtomicResult::getCaseId, caseId)
.eq(ModelAtomicResult::getCasePersonId, actorId) .eq(ModelAtomicResult::getCasePersonId, actorId)
.in(CollUtil.isNotEmpty(automicIndexIds),ModelAtomicResult::getAtomicId, automicIndexIds)); .in(CollUtil.isNotEmpty(automicIndexIds), ModelAtomicResult::getAtomicId, automicIndexIds));
} }
/** /**
* id * id
*
* @param judgeLogic json * @param judgeLogic json
* @return id() * @return id()
*/ */
@ -321,7 +331,7 @@ public class ModelIndexServiceImpl extends ServiceImpl<ModelIndexMapper, ModelIn
for (JudgeLogic judge : logic) { for (JudgeLogic judge : logic) {
List<AtomicData> atomicData = judge.getAtomicData(); List<AtomicData> atomicData = judge.getAtomicData();
for (AtomicData atomic : atomicData) { for (AtomicData atomic : atomicData) {
if (!ids.contains(atomic.getAtomicIndex())){ if (!ids.contains(atomic.getAtomicIndex())) {
ids.add(atomic.getAtomicIndex()); ids.add(atomic.getAtomicIndex());
} }
} }
@ -329,26 +339,26 @@ public class ModelIndexServiceImpl extends ServiceImpl<ModelIndexMapper, ModelIn
return ids; return ids;
} }
private Map<String,List<ModelIndex>> groupModelIndexByAtomicIndexId(List<ModelIndex> modelIndexList) { private Map<String, List<ModelIndex>> groupModelIndexByAtomicIndexId(List<ModelIndex> modelIndexList) {
Map<String, List<ModelIndex>> groupMap = new HashMap<>(); Map<String, List<ModelIndex>> groupMap = new HashMap<>();
if (CollUtil.isEmpty(modelIndexList)){ if (CollUtil.isEmpty(modelIndexList)) {
return groupMap; return groupMap;
} }
for (ModelIndex modelIndex : modelIndexList) { for (ModelIndex modelIndex : modelIndexList) {
if (StrUtil.isEmpty(modelIndex.getJudgeLogic())){ if (StrUtil.isEmpty(modelIndex.getJudgeLogic())) {
continue; continue;
} }
String judgeLogic = modelIndex.getJudgeLogic(); String judgeLogic = modelIndex.getJudgeLogic();
List<JudgeLogic> judgeLogicList = JSONUtil.toList(judgeLogic, JudgeLogic.class); List<JudgeLogic> judgeLogicList = JSONUtil.toList(judgeLogic, JudgeLogic.class);
for (JudgeLogic logic : judgeLogicList) { for (JudgeLogic logic : judgeLogicList) {
List<AtomicData> atomicData = logic.getAtomicData(); List<AtomicData> atomicData = logic.getAtomicData();
if (CollUtil.isEmpty(atomicData)){ if (CollUtil.isEmpty(atomicData)) {
continue; continue;
} }
for (AtomicData atomic : atomicData) { for (AtomicData atomic : atomicData) {
List<ModelIndex> modelIndexs = groupMap.getOrDefault(atomic.getAtomicIndex(), new ArrayList<>()); List<ModelIndex> modelIndexs = groupMap.getOrDefault(atomic.getAtomicIndex(), new ArrayList<>());
modelIndexs.add(modelIndex); modelIndexs.add(modelIndex);
groupMap.put(atomic.getAtomicIndex(),modelIndexs); groupMap.put(atomic.getAtomicIndex(), modelIndexs);
} }
} }
} }

Loading…
Cancel
Save