From 38ea4c2d1b6dba9d0369b0fd3bc587442d696b72 Mon Sep 17 00:00:00 2001
From: liu <liujiatong112@163.com>
Date: Fri, 26 Jul 2024 16:25:52 +0800
Subject: [PATCH] =?UTF-8?q?=E5=8E=9F=E5=AD=90=E6=8C=87=E6=A0=87=E9=85=8D?=
 =?UTF-8?q?=E7=BD=AE=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../service/ModelAtomicIndexService.java      |   3 +-
 .../service/impl/ModelIndexServiceImpl.java   | 112 ++++++++++--------
 2 files changed, 63 insertions(+), 52 deletions(-)

diff --git a/src/main/java/com/supervision/police/service/ModelAtomicIndexService.java b/src/main/java/com/supervision/police/service/ModelAtomicIndexService.java
index df079e2..13b8e3c 100644
--- a/src/main/java/com/supervision/police/service/ModelAtomicIndexService.java
+++ b/src/main/java/com/supervision/police/service/ModelAtomicIndexService.java
@@ -1,12 +1,13 @@
 package com.supervision.police.service;
 
 import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.service.IService;
 import com.supervision.police.domain.ModelAtomicIndex;
 import com.supervision.police.mapper.ModelAtomicIndexMapper;
 
 import java.util.List;
 
-public interface ModelAtomicIndexService {
+public interface ModelAtomicIndexService extends IService<ModelAtomicIndex> {
 
     ModelAtomicIndexMapper getMapper();
 
diff --git a/src/main/java/com/supervision/police/service/impl/ModelIndexServiceImpl.java b/src/main/java/com/supervision/police/service/impl/ModelIndexServiceImpl.java
index d49307a..97eed52 100644
--- a/src/main/java/com/supervision/police/service/impl/ModelIndexServiceImpl.java
+++ b/src/main/java/com/supervision/police/service/impl/ModelIndexServiceImpl.java
@@ -2,6 +2,7 @@ package com.supervision.police.service.impl;
 
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.lang.Assert;
+import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.json.JSONUtil;
 import com.alibaba.druid.sql.ast.SQLStatement;
@@ -61,8 +62,9 @@ public class ModelIndexServiceImpl extends ServiceImpl<ModelIndexMapper, ModelIn
 
     @Value("${case.evidence.table}")
     private List<String> allowedTables;
+
     @Override
-    @Transactional(transactionManager = "dataSourceTransactionManager",rollbackFor = Exception.class)
+    @Transactional(transactionManager = "dataSourceTransactionManager", rollbackFor = Exception.class)
     public R<?> selectAll(ModelIndex modelIndex, Integer page, Integer size) {
         IPage<ModelIndex> iPage = new Page<>(page, size);
         LambdaQueryWrapper<ModelIndex> wrapper = Wrappers.lambdaQuery();
@@ -72,7 +74,7 @@ public class ModelIndexServiceImpl extends ServiceImpl<ModelIndexMapper, ModelIn
                 .eq(StringUtils.isNotEmpty(modelIndex.getIndexType()), ModelIndex::getIndexType, modelIndex.getIndexType())
                 .eq(StringUtils.isNotEmpty(modelIndex.getCaseType()), ModelIndex::getCaseType, modelIndex.getCaseType())
                 .eq(ModelIndex::getDataStatus, "1");
-        iPage  = modelIndexMapper.selectPage(iPage, wrapper);
+        iPage = modelIndexMapper.selectPage(iPage, wrapper);
         List<ModelIndex> records = iPage.getRecords();
         List<ComDictionary> dicts = comDictionaryService.list();
         for (ModelIndex index : records) {
@@ -99,7 +101,7 @@ public class ModelIndexServiceImpl extends ServiceImpl<ModelIndexMapper, ModelIn
     }
 
     @Override
-    @Transactional(transactionManager = "dataSourceTransactionManager",rollbackFor = Exception.class)
+    @Transactional(transactionManager = "dataSourceTransactionManager", rollbackFor = Exception.class)
     public R<?> addOrUpd(ModelIndex modelIndex) {
         int i = 0;
         if (StringUtils.isEmpty(modelIndex.getId())) {
@@ -115,7 +117,7 @@ public class ModelIndexServiceImpl extends ServiceImpl<ModelIndexMapper, ModelIn
     }
 
     @Override
-    @Transactional(transactionManager = "dataSourceTransactionManager",rollbackFor = Exception.class)
+    @Transactional(transactionManager = "dataSourceTransactionManager", rollbackFor = Exception.class)
     public R<?> del(String id) {
         ModelIndex index = modelIndexMapper.selectById(id);
         index.setDataStatus(StringUtils.getUUID());
@@ -128,10 +130,10 @@ public class ModelIndexServiceImpl extends ServiceImpl<ModelIndexMapper, ModelIn
     }
 
     @Override
-    @Transactional(transactionManager = "dataSourceTransactionManager",rollbackFor = Exception.class)
+    @Transactional(transactionManager = "dataSourceTransactionManager", rollbackFor = Exception.class)
     public R<?> selectAllAtomic(ModelAtomicIndex modelAtomicIndex, Integer page, Integer size) {
         IPage<ModelAtomicIndex> iPage = new Page<>(page, size);
-        iPage  = modelAtomicIndexService.selectAll(iPage, modelAtomicIndex);
+        iPage = modelAtomicIndexService.selectAll(iPage, modelAtomicIndex);
         List<ModelAtomicIndex> records = iPage.getRecords();
         List<ComDictionary> dicts = comDictionaryService.list();
         for (ModelAtomicIndex index : records) {
@@ -144,14 +146,19 @@ public class ModelIndexServiceImpl extends ServiceImpl<ModelIndexMapper, ModelIn
     }
 
     @Override
-    @Transactional(transactionManager = "dataSourceTransactionManager",rollbackFor = Exception.class)
+    @Transactional(transactionManager = "dataSourceTransactionManager", rollbackFor = Exception.class)
     public R<?> addOrUpdAtomic(ModelAtomicIndex modelAtomicIndex) {
         int i = 0;
-        if (StringUtils.equals("2", modelAtomicIndex.getIndexSource())){
+        if (StringUtils.equals("2", modelAtomicIndex.getIndexSource())) {
             // 如果查询类型为数据查询,则校验查询语句
             Assert.notEmpty(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())) {
             i = modelAtomicIndexService.getMapper().insert(modelAtomicIndex);
         } else {
@@ -165,7 +172,7 @@ public class ModelIndexServiceImpl extends ServiceImpl<ModelIndexMapper, ModelIn
     }
 
     @Override
-    @Transactional(transactionManager = "dataSourceTransactionManager",rollbackFor = Exception.class)
+    @Transactional(transactionManager = "dataSourceTransactionManager", rollbackFor = Exception.class)
     public R<?> delAtomic(String id) {
         ModelAtomicIndex index = modelAtomicIndexService.getMapper().selectById(id);
         index.setDataStatus(StringUtils.getUUID());
@@ -178,7 +185,7 @@ public class ModelIndexServiceImpl extends ServiceImpl<ModelIndexMapper, ModelIn
     }
 
     @Override
-    @Transactional(transactionManager = "dataSourceTransactionManager",rollbackFor = Exception.class)
+    @Transactional(transactionManager = "dataSourceTransactionManager", rollbackFor = Exception.class)
     public List<CaseAtomicIndexDTO> listCaseAtomicIndex(String caseId, String indexSource) {
 
         Assert.notEmpty(caseId, "案件id不能为空");
@@ -194,7 +201,7 @@ public class ModelIndexServiceImpl extends ServiceImpl<ModelIndexMapper, ModelIn
         List<ModelIndex> modelIndexList = modelIndexMapper.selectList(
                 Wrappers.lambdaQuery(ModelIndex.class)
                         .eq(ModelIndex::getCaseType, caseType).eq(ModelIndex::getDataStatus, "1"));
-        if (CollUtil.isEmpty(modelIndexList)){
+        if (CollUtil.isEmpty(modelIndexList)) {
             return new ArrayList<>(1);
         }
 
@@ -203,13 +210,13 @@ public class ModelIndexServiceImpl extends ServiceImpl<ModelIndexMapper, ModelIn
                 .map(modelIndex -> pickAtomicIndexIds(modelIndex.getJudgeLogic()))
                 .flatMap(Collection::stream).distinct().toList();
 
-        if (CollUtil.isEmpty(automicIndexIds)){
+        if (CollUtil.isEmpty(automicIndexIds)) {
             return new ArrayList<>(1);
         }
 
         // 查询原子指标相关信息
         List<ModelAtomicIndex> modelAtomicIndexList = modelAtomicIndexService.listCaseAtomicIndex(automicIndexIds, caseType, indexSource);
-        if (CollUtil.isEmpty(modelAtomicIndexList)){
+        if (CollUtil.isEmpty(modelAtomicIndexList)) {
             return new ArrayList<>(1);
         }
         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
-    @Transactional(transactionManager = "dataSourceTransactionManager",rollbackFor = Exception.class)
+    @Transactional(transactionManager = "dataSourceTransactionManager", rollbackFor = Exception.class)
     public Boolean saveCaseAtomicResult(CaseAtomicResultWrapper caseAtomicResultWrapper) {
 
         Assert.notEmpty(caseAtomicResultWrapper.getCaseId(), "案件id不能为空!");
@@ -247,13 +254,13 @@ public class ModelIndexServiceImpl extends ServiceImpl<ModelIndexMapper, ModelIn
 
 
         //清空人工评估结果
-        removeCaseAtomicResult(caseAtomicResultWrapper.getCaseId(),modelCase.getCaseType(),
-                caseAtomicResultWrapper.getActorId(),"1");
+        removeCaseAtomicResult(caseAtomicResultWrapper.getCaseId(), modelCase.getCaseType(),
+                caseAtomicResultWrapper.getActorId(), "1");
 
         // 保存原子评估结果
         caseAtomicResultWrapper.getCaseAtomicIndexList().stream().map(caseAtomicIndexDTO ->
-                caseAtomicIndexDTO.toModelAtomicResult(
-                        caseAtomicResultWrapper.getCaseId(), caseAtomicResultWrapper.getActorId())).toList()
+                        caseAtomicIndexDTO.toModelAtomicResult(
+                                caseAtomicResultWrapper.getCaseId(), caseAtomicResultWrapper.getActorId())).toList()
                 .forEach(modelAtomicResultMapper::insert);
 
         return true;
@@ -266,7 +273,7 @@ public class ModelIndexServiceImpl extends ServiceImpl<ModelIndexMapper, ModelIn
         if (StringUtils.isEmpty(sql)) {
             return false;
         }
-        if (CollUtil.isEmpty(this.allowedTables)){
+        if (CollUtil.isEmpty(this.allowedTables)) {
             log.info("checkSql:未配置允许的表");
             return false;
         }
@@ -282,14 +289,14 @@ public class ModelIndexServiceImpl extends ServiceImpl<ModelIndexMapper, ModelIn
             return false;
         }
         List<String> tableList = SqlParserUtil.extractTableNames(sqlStatement);
-        if (CollUtil.isEmpty(tableList)){
+        if (CollUtil.isEmpty(tableList)) {
             log.warn("checkSql:未检测到表");
             return false;
         }
 
         long count = tableList.stream().filter(table -> !this.allowedTables.contains(table)).count();
-        if (count > 0){
-            log.warn("checkSql:表{}不在允许的表列表中",tableList);
+        if (count > 0) {
+            log.warn("checkSql:表{}不在允许的表列表中", tableList);
             return false;
         }
         return true;
@@ -297,21 +304,24 @@ public class ModelIndexServiceImpl extends ServiceImpl<ModelIndexMapper, ModelIn
 
     /**
      * 清空案件下的评估结果
-     * @param caseId 案件id
-     * @param caseType 案件类型
-     * @param actorId 行为人id
+     *
+     * @param caseId      案件id
+     * @param caseType    案件类型
+     * @param actorId     行为人id
      * @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<String> automicIndexIds = modelAtomicIndexList.stream().map(ModelAtomicIndex::getId).distinct().toList();
         modelAtomicResultMapper.delete(Wrappers.lambdaQuery(ModelAtomicResult.class)
                 .eq(ModelAtomicResult::getCaseId, caseId)
                 .eq(ModelAtomicResult::getCasePersonId, actorId)
-                .in(CollUtil.isNotEmpty(automicIndexIds),ModelAtomicResult::getAtomicId, automicIndexIds));
+                .in(CollUtil.isNotEmpty(automicIndexIds), ModelAtomicResult::getAtomicId, automicIndexIds));
     }
+
     /**
      * 从逻辑中获取原子指标id
+     *
      * @param judgeLogic 判断逻辑json字符串
      * @return 原子指标id(不重复)
      */
@@ -321,38 +331,38 @@ public class ModelIndexServiceImpl extends ServiceImpl<ModelIndexMapper, ModelIn
         for (JudgeLogic judge : logic) {
             List<AtomicData> atomicData = judge.getAtomicData();
             for (AtomicData atomic : atomicData) {
-                if (!ids.contains(atomic.getAtomicIndex())){
+                if (!ids.contains(atomic.getAtomicIndex())) {
                     ids.add(atomic.getAtomicIndex());
                 }
             }
         }
         return ids;
-   }
+    }
 
-   private Map<String,List<ModelIndex>> groupModelIndexByAtomicIndexId(List<ModelIndex> modelIndexList) {
-       Map<String, List<ModelIndex>> groupMap = new HashMap<>();
-       if (CollUtil.isEmpty(modelIndexList)){
+    private Map<String, List<ModelIndex>> groupModelIndexByAtomicIndexId(List<ModelIndex> modelIndexList) {
+        Map<String, List<ModelIndex>> groupMap = new HashMap<>();
+        if (CollUtil.isEmpty(modelIndexList)) {
             return groupMap;
         }
-       for (ModelIndex modelIndex : modelIndexList) {
-           if (StrUtil.isEmpty(modelIndex.getJudgeLogic())){
-               continue;
-           }
-           String judgeLogic = modelIndex.getJudgeLogic();
-           List<JudgeLogic> judgeLogicList = JSONUtil.toList(judgeLogic, JudgeLogic.class);
-           for (JudgeLogic logic : judgeLogicList) {
-               List<AtomicData> atomicData = logic.getAtomicData();
-               if (CollUtil.isEmpty(atomicData)){
-                   continue;
-               }
-               for (AtomicData atomic : atomicData) {
-                   List<ModelIndex> modelIndexs = groupMap.getOrDefault(atomic.getAtomicIndex(), new ArrayList<>());
+        for (ModelIndex modelIndex : modelIndexList) {
+            if (StrUtil.isEmpty(modelIndex.getJudgeLogic())) {
+                continue;
+            }
+            String judgeLogic = modelIndex.getJudgeLogic();
+            List<JudgeLogic> judgeLogicList = JSONUtil.toList(judgeLogic, JudgeLogic.class);
+            for (JudgeLogic logic : judgeLogicList) {
+                List<AtomicData> atomicData = logic.getAtomicData();
+                if (CollUtil.isEmpty(atomicData)) {
+                    continue;
+                }
+                for (AtomicData atomic : atomicData) {
+                    List<ModelIndex> modelIndexs = groupMap.getOrDefault(atomic.getAtomicIndex(), new ArrayList<>());
                     modelIndexs.add(modelIndex);
-                   groupMap.put(atomic.getAtomicIndex(),modelIndexs);
-               }
-           }
-       }
-       return groupMap;
-   }
+                    groupMap.put(atomic.getAtomicIndex(), modelIndexs);
+                }
+            }
+        }
+        return groupMap;
+    }
 }