浏览代码

Merge remote-tracking branch 'refs/remotes/origin/master'

# Conflicts:
#	src/main/java/com/sundata/internalevaluation/calc/calcUnit/DataSetCalcUnit.java
#	src/main/java/com/sundata/internalevaluation/calc/calcUnit/DataSourcesCalcUnit.java
#	src/main/java/com/sundata/internalevaluation/calc/calcUnit/IndexCalcUnit.java
#	src/main/java/com/sundata/internalevaluation/calc/calcUnit/InterfaceCalcUnit.java
#	src/main/java/com/sundata/internalevaluation/calc/calcUnit/RuleCalcUnit.java
#	src/main/java/com/sundata/internalevaluation/configuration/mybatis/IndexConfigMapper.xml
#	src/main/java/com/sundata/internalevaluation/configuration/service/DataSourcesService.java
JoeLazy 1 月之前
父节点
当前提交
d87742dae9

+ 19 - 10
src/main/java/com/sundata/internalevaluation/calc/calcUnit/DataSetCalcUnit.java

@@ -7,9 +7,12 @@ import com.sundata.internalevaluation.calc.model.CalcException;
 import com.sundata.internalevaluation.calc.model.CalcResult;
 import com.sundata.internalevaluation.calc.model.CalcUnit;
 import com.sundata.internalevaluation.calc.model.finals.CalcType;
-import com.sundata.internalevaluation.configuration.model.DataSetModel;
-import com.sundata.internalevaluation.configuration.model.DataSourceModel;
+import com.sundata.internalevaluation.calc.util.CalciteUtil;
+import com.sundata.internalevaluation.configuration.model.DataSetConfigModel;
+import com.sundata.internalevaluation.configuration.model.DataSourcesModel;
 import com.sundata.internalevaluation.configuration.service.DataSourcesService;
+import org.apache.calcite.adapter.file.JsonScannableTable;
+import org.apache.calcite.schema.impl.AbstractSchema;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -25,7 +28,7 @@ import java.util.stream.Collectors;
 public class DataSetCalcUnit extends CalcUnit {
     private static final Logger log = LoggerFactory.getLogger(DataSetCalcUnit.class);
 
-    DataSetModel dataSetModel;
+    DataSetConfigModel dataSetConfigModel;
 
     /**
      * 创建数据单元的绝对对象,对象必须包含如下参数
@@ -34,9 +37,9 @@ public class DataSetCalcUnit extends CalcUnit {
      * @param calcName    计算对象名称
      * @param initContext 计算单元初始化参数
      */
-    public DataSetCalcUnit(String calcCode, String calcName, Map<String, Object> initContext, DataSetModel dataSetModel) {
+    public DataSetCalcUnit(String calcCode, String calcName, Map<String, Object> initContext, DataSetConfigModel dataSetConfigModel) {
         super(calcCode, calcName, CalcType.DATASET, initContext);
-        this.dataSetModel = dataSetModel;
+        this.dataSetConfigModel = dataSetConfigModel;
     }
 
     /**
@@ -81,9 +84,9 @@ public class DataSetCalcUnit extends CalcUnit {
         // 获取数据来源service对象
         DataSourcesService sourcesService = SpringUtil.getBean(DataSourcesService.class);
         // 放入该数据集下的所有数据源对象
-        List<DataSourceModel> sourceList = new ArrayList<>();
-        for (String s : dataSetModel.getDataSourcesNos()) {
-            DataSourceModel model = sourcesService.selectDetailData(s);
+        List<DataSourcesModel> sourceList = new ArrayList<>();
+        for (String s :dataSetConfigModel.getDataSourcesNos()) {
+            DataSourcesModel model = sourcesService.selectDetailData(s);
             sourceList.add(model);
         }
 
@@ -128,12 +131,18 @@ public class DataSetCalcUnit extends CalcUnit {
         log.info("当前计算节点为:[{}-{}-{}],计算流水号为:{}", this.getCalcType(), this.getCalcCode(), this.getCalcName(), calculateInstanceNumber);
         // TODO 实际的计算过程
         // 合并后结果集
-        Map<String,Object> resultMap = new HashMap<>();
+        Map<String,JsonScannableTable> resultMap = new HashMap<>();
         // 合并数据集
         sourceResults.forEach((calcUnit,result)-> {
-            resultMap.putAll(result);
+            // resultMap.putAll(result);
+            // 获取构造后的table表
+            Map<String, JsonScannableTable> tableMap = (Map) result.get(calcUnit.getCalcCode());
+            resultMap.putAll(tableMap);
+
         });
         log.debug("合并后结果集结构:{}",resultMap);
+        // 声明域,将表结构放入
+        AbstractSchema schema = CalciteUtil.createSchema(resultMap);
         // 合并数据集结果集放入
         thisResult.put(this.getCalcCode(),resultMap);
 

+ 43 - 23
src/main/java/com/sundata/internalevaluation/calc/calcUnit/DataSourcesCalcUnit.java

@@ -1,18 +1,23 @@
 package com.sundata.internalevaluation.calc.calcUnit;
 
 import cn.hutool.extra.spring.SpringUtil;
+import com.sundata.common.util.DBExecutor;
 import com.sundata.common.util.JsonUtil;
 import com.sundata.internalevaluation.calc.model.CalcResult;
 import com.sundata.internalevaluation.calc.model.CalcUnit;
 import com.sundata.internalevaluation.calc.model.finals.CalcType;
-import com.sundata.internalevaluation.configuration.model.DataSourceModel;
-import com.sundata.internalevaluation.configuration.model.Interface;
+import com.sundata.internalevaluation.calc.util.CalciteUtil;
+import com.sundata.internalevaluation.configuration.model.DataSourcesModel;
+import com.sundata.internalevaluation.configuration.model.SysInterface;
 import com.sundata.internalevaluation.configuration.service.DataSourcesService;
-import com.sundata.internalevaluation.configuration.service.InterfaceService;
+import com.sundata.internalevaluation.configuration.service.SysInterfaceService;
+import com.sundata.internalevaluation.script.TemplateUtil;
+import org.apache.calcite.adapter.file.JsonScannableTable;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.stream.Collectors;
@@ -29,7 +34,7 @@ public class DataSourcesCalcUnit extends CalcUnit {
 
     private static final Logger log = LoggerFactory.getLogger(DataSourcesCalcUnit.class);
 
-    final DataSourceModel dataSourceModel;
+    final DataSourcesModel dataSourcesModel;
 
     /**
     /**
@@ -41,9 +46,9 @@ public class DataSourcesCalcUnit extends CalcUnit {
      * @param initContext 计算单元初始化参数
      */
     public DataSourcesCalcUnit(String calcCode, String calcName,CalcType calcType, Map<String, Object> initContext,
-                               DataSourceModel dataSourceModel) {
+                               DataSourcesModel dataSourcesModel) {
         super(calcCode, calcName, calcType, initContext);
-        this.dataSourceModel = dataSourceModel;
+        this.dataSourcesModel = dataSourcesModel;
 
     }
 
@@ -80,28 +85,29 @@ public class DataSourcesCalcUnit extends CalcUnit {
     public List<CalcUnit> getSourceCalcUnits() {
         // 通过bean获取对象
         DataSourcesService sourcesService = SpringUtil.getBean(DataSourcesService.class);
-        InterfaceService interfaceService = SpringUtil.getBean(InterfaceService.class);
-        DataSourceModel selectModel = sourcesService.selectDetailData(this.dataSourceModel.getDataSourcesNo());
+        SysInterfaceService sysInterfaceService = SpringUtil.getBean(SysInterfaceService.class);
+        DataSourcesModel selectModel = sourcesService.selectDetailData(this.dataSourcesModel.getDataSourcesNo());
 
         // 记录所有源头节点
         List<CalcUnit> allUnitList = new ArrayList<>();
 
-        List<Interface> interfaceModelList = new ArrayList<>();
+        List<SysInterface> sysInterfaceModelList = new ArrayList<>();
 
         // 接口源节点
-        if ("INTERFACE".equals(dataSourceModel.getDataSourcesType())) {
+        if ("INTERFACE".equals(dataSourcesModel.getDataSourcesType())) {
+
 //            List<String> interfaceNos = selectModel.getRequestInterfaces();
-            List<String> interfaceNos = new ArrayList<>();
-            for ( String s : interfaceNos) {
-                interfaceModelList.add(interfaceService.getById(s));
-            }
-            List<CalcUnit> interFaceList = interfaceModelList.stream().map(interfaceModel -> new InterfaceCalcUnit(interfaceModel.getId(),interfaceModel.getInterfaceName(),CalcType.INTERFACE,Map.of(),interfaceModel)).collect(Collectors.toList());
+//            for ( String s : interfaceNos) {
+//                sysInterfaceModelList.add(sysInterfaceService.getById(s));
+//            }
+            sysInterfaceModelList.add(sysInterfaceService.getById(selectModel.getRequestInterface()));
+            List<CalcUnit> interFaceList = sysInterfaceModelList.stream().map(interfaceModel -> new InterfaceCalcUnit(interfaceModel.getId(),interfaceModel.getInterfaceName(),CalcType.INTERFACE,Map.of(),interfaceModel)).collect(Collectors.toList());
             allUnitList.addAll(interFaceList);
         }
 
         // 查询逻辑源节点
-        if ("JDBC".equals(dataSourceModel.getDataSourcesType())) {
-            List<CalcUnit> queryList = dataSourceModel.getQueryLogic().stream().map(queryParam -> new QueryLogicCalcUnit(queryParam.getDataItemName(),queryParam.getScriptDescription(),CalcType.QUERYLOGIC,Map.of(),queryParam)).collect(Collectors.toList());
+        if ("JDBC".equals(dataSourcesModel.getDataSourcesType())) {
+            List<CalcUnit> queryList = dataSourcesModel.getQueryLogic().stream().map(queryParam -> new QueryLogicCalcUnit(queryParam.getDataItemName(),queryParam.getScriptDescription(),CalcType.QUERYLOGIC,Map.of(),queryParam)).collect(Collectors.toList());
             allUnitList.addAll(queryList);
         }
         return allUnitList;
@@ -139,23 +145,37 @@ public class DataSourcesCalcUnit extends CalcUnit {
     public  void calc(CalcResult<String, Object> thisResult, String calculateInstanceNumber, Map<String, Object> context, Map<CalcUnit, CalcResult<String, Object>> sourceResults) {
         log.info("当前计算节点为:[{}-{}-{}],计算流水号为:{}", this.getCalcType(), this.getCalcCode(), this.getCalcName(), calculateInstanceNumber);
         sourceResults.forEach((calcUnit,result)->{
+            // 合并结果集
+            Map<String,JsonScannableTable> allMap = new HashMap<>();
 
-            if ("INTERFACE".equals(dataSourceModel.getDataSourcesType())) {
+            if ("INTERFACE".equals(dataSourcesModel.getDataSourcesType())) {
                 // 数据来源类型为接口时处理逻辑
                 if (calcUnit instanceof InterfaceCalcUnit ) {
                     // 获取json
                     String json = result.get(calcUnit.getCalcCode()) instanceof String ? (String) result.get(calcUnit.getCalcCode()) : "";
-                    List<Object>  parseList = JsonUtil.jsonToList(json);
-                    // 结果封装
-                    thisResult.put(this.getCalcCode(), parseList);
+                    Map<String,Object> objectMap = JsonUtil.jsonToMap(json);
+                    // 调用utils类构建表
+                    Map<String, JsonScannableTable> tableMap = CalciteUtil.createTableMap(objectMap);
+                    allMap.putAll(tableMap);
+//                    List<Object>  parseList = JsonUtil.jsonToList(json);
+//                    // 结果封装
+//                    thisResult.put(this.getCalcCode(), parseList);
                 }
-            } else if ("JDBC".equals(dataSourceModel.getDataSourcesType())) {
+            } else if ("JDBC".equals(dataSourcesModel.getDataSourcesType())) {
                 // 数据来源类型为jdbc时处理逻辑
                 if (calcUnit instanceof QueryLogicCalcUnit queryLogicCalcUnit) {
+                    // 声明本地表数据集合
+                    Map<String,Object> localTable = new HashMap<>();
+                    localTable.put(queryLogicCalcUnit.queryLogicModel.getDataItemName(),result.get(queryLogicCalcUnit.queryLogicModel.getDataItemName()));
+                    // 调用util类构造表
+                    Map<String,JsonScannableTable> tableMap = CalciteUtil.createTableMap(localTable);
+                    allMap.putAll(tableMap);
                     // 结果封装,数据项名做key
-                    thisResult.put(queryLogicCalcUnit.queryLogicModel.getDataItemName(), result.get(queryLogicCalcUnit.getCalcCode()));
+                    // thisResult.put(queryLogicCalcUnit.queryLogicModel.getDataItemName(), result.get(queryLogicCalcUnit.getCalcCode()));
                 }
             }
+            // 将构造好的table表结果封装
+            thisResult.put(this.getCalcCode(),allMap);
         });
     }
 

+ 132 - 52
src/main/java/com/sundata/internalevaluation/calc/calcUnit/IndexCalcUnit.java

@@ -1,28 +1,39 @@
 package com.sundata.internalevaluation.calc.calcUnit;
 
+import cn.hutool.core.convert.Convert;
+import cn.hutool.core.util.StrUtil;
 import cn.hutool.extra.spring.SpringUtil;
+import com.sundata.common.exception.BusinessException;
 import com.sundata.common.util.JsonUtil;
+import com.sundata.internalevaluation.calc.model.CalcException;
 import com.sundata.internalevaluation.calc.model.CalcResult;
 import com.sundata.internalevaluation.calc.model.CalcUnit;
 import com.sundata.internalevaluation.calc.model.finals.CalcType;
+import com.sundata.internalevaluation.calc.util.CalciteUtil;
 import com.sundata.internalevaluation.conf.JsonToCalciteExample;
-import com.sundata.internalevaluation.configuration.model.DataSetModel;
-import com.sundata.internalevaluation.configuration.model.IndexModel;
+import com.sundata.internalevaluation.configuration.model.DataSetConfigModel;
+import com.sundata.internalevaluation.configuration.model.IndexConfigModel;
 import com.sundata.internalevaluation.configuration.model.IndexSourceModel;
-import com.sundata.internalevaluation.configuration.service.DataSetService;
+import com.sundata.internalevaluation.configuration.service.DataSetConfigService;
 import com.sundata.internalevaluation.configuration.service.IndexConfigService;
 import com.sundata.internalevaluation.script.ScriptUtil;
 import com.sundata.internalevaluation.script.TemplateUtil;
+import org.apache.calcite.jdbc.CalciteConnection;
+import org.apache.calcite.schema.SchemaPlus;
+import org.apache.calcite.schema.impl.AbstractSchema;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
 import java.util.*;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.stream.Collectors;
 
 public class IndexCalcUnit extends CalcUnit {
 
-    private final IndexModel indexModel;
+    private final IndexConfigModel indexConfigModel;
 
     private static final Logger log = LoggerFactory.getLogger(IndexCalcUnit.class);
 
@@ -36,9 +47,9 @@ public class IndexCalcUnit extends CalcUnit {
      * @param calcName    计算对象名称
      * @param initContext 计算单元初始化参数
      */
-    public IndexCalcUnit(String calcCode, String calcName, Map<String, Object> initContext, IndexModel indexModel) {
+    public IndexCalcUnit(String calcCode, String calcName, Map<String, Object> initContext, IndexConfigModel indexConfigModel) {
         super(calcCode, calcName, CalcType.INDEX, initContext);
-        this.indexModel = indexModel;
+        this.indexConfigModel = indexConfigModel;
     }
 
     /**
@@ -80,24 +91,24 @@ public class IndexCalcUnit extends CalcUnit {
     @Override
     public List<CalcUnit> getSourceCalcUnits() {
         // TODO 获取源头节点
-        DataSetService dataSetService = SpringUtil.getBean(DataSetService.class);
+        DataSetConfigService dataSetService = SpringUtil.getBean(DataSetConfigService.class);
         IndexConfigService indexConfigService = SpringUtil.getBean(IndexConfigService.class);
-        List<IndexSourceModel> selectList = indexConfigService.getIndexSourceList(this.indexModel);
+        List<IndexSourceModel> selectList = indexConfigService.getIndexSourceList(this.indexConfigModel);
         // 定义存放数据集和其他指标的集合
-        List<IndexModel> indexList = new ArrayList<>();
-        List<DataSetModel> dataSetList = new ArrayList<>();
+        List<IndexConfigModel> indexList = new ArrayList<>();
+        List<DataSetConfigModel> dataSetList = new ArrayList<>();
         // 查询个指标编号和数据集对应的数据
         for ( IndexSourceModel m : selectList) {
             if ("DATASET".equals(m.getDataSourceType())) {
-                DataSetModel conditionModel = new DataSetModel();
+                DataSetConfigModel conditionModel = new DataSetConfigModel();
                 conditionModel.setDataSetNo(m.getDataSetNo());
-                DataSetModel datasetModel =  dataSetService.selectDetailData(conditionModel);
+                DataSetConfigModel datasetModel =  dataSetService.selectDetailData(conditionModel);
                 dataSetList.add(datasetModel);
             } else if ("INDEX".equals(m.getDataSourceType())) {
-                IndexModel conditionModel = new IndexModel();
+                IndexConfigModel conditionModel = new IndexConfigModel();
                 conditionModel.setIndexNo(m.getOtherIndexNo());
-                IndexModel indexModel = indexConfigService.selectDetailData(conditionModel);
-                indexList.add(indexModel);
+                IndexConfigModel indexConfigModel = indexConfigService.selectDetailData(conditionModel);
+                indexList.add(indexConfigModel);
             }
         }
 
@@ -168,7 +179,7 @@ public class IndexCalcUnit extends CalcUnit {
         // 声明只存放指标源头节点变量
         Map<String, Object> indexResult = new HashMap<>();
         // 声明只存放所有数据集源头节点变量
-        Map<String, Object> dataSetResult = new HashMap<>();
+        Map<String, AbstractSchema> dataSetResult = new HashMap<>();
         // 遍历结果集
         sourceResults.forEach((calcUnit,result) -> {
             // 数据集
@@ -177,7 +188,17 @@ public class IndexCalcUnit extends CalcUnit {
                     // 记录记过集次数
                     dataSetNumber.getAndIncrement();
                     // 将源头节点的返回值取出
-                    dataSetResult.putAll(result);
+                    // dataSetResult.putAll(result);
+                    String calcCode = calcUnit.getCalcCode();
+                    Object schemeObj = result.get(calcCode);
+                    if (schemeObj instanceof AbstractSchema schema) {
+                        dataSetResult.put(calcCode,schema);
+                    } else {
+                        String errorMsg = StrUtil.format("数据集返回的计算结果不是AbstractSchema类型",calcUnit.getCalcName(),calcCode);
+                        log.error(errorMsg);
+                        throw new CalcException(errorMsg);
+                    }
+
                 }
             }
             // 其他指标
@@ -192,47 +213,106 @@ public class IndexCalcUnit extends CalcUnit {
         });
 
         // TODO 计算过程开始
-        if (dataSetNumber.get() > 0 && indexNumber.get() == 0){
-            // 如果数据集的数量大于0
-            // 将数据集返回结果转化成json
-            String json = JsonUtil.toJSONString(dataSetResult);
-            // 实例化组建域对象
-            JsonToCalciteExample jsonUtil = new JsonToCalciteExample();
-            // 获取指标逻辑
-            String logic = indexModel.getIndexLogic();
-            // 获取域名
-
-            // 执行sql
-            Object resValue = jsonUtil.jsonTotable(json,logic);
-
-            // 执行结果放进结果集内
-            thisResult.put(this.getCalcCode(),resValue);
-        }
 
-        if(indexNumber.get() > 0 && dataSetNumber.get() == 0){
-            // 如果指标的数量大于0
-            // 获取指标逻辑
-            String logic = indexModel.getIndexLogic();
-            // 执行公式,结果放入结果集
-            Object result = ScriptUtil.executeScript(indexModel.getIndexNo(),logic,indexResult);
-            // 指标执行公式放入结果集
-            thisResult.put(this.getCalcCode(),result);
-        }
+        // 定义指标结果变量
+        Object indexCalcResult = null;
+
+        // 获取指标计算逻辑
+        String calcLogic = indexConfigModel.getIndexLogic();
 
-        if (dataSetNumber.get() > 0 && indexNumber.get() >0) {
+        if (dataSetNumber.get() > 0 && indexNumber.get() == 0){
+            // 如果数据集的数量大于0(只依赖数据集)
+            indexCalcResult = doExecuteSql(dataSetResult,calcLogic);
+        } else if(indexNumber.get() > 0 && dataSetNumber.get() == 0){
+            // 如果指标的数量大于0(只依赖其他指标)
+            // 执行公式,结果放入结果集
+            indexCalcResult = ScriptUtil.executeScript(indexConfigModel.getIndexNo(),calcLogic,indexResult);
+        } else if (dataSetNumber.get() > 0 && indexNumber.get() >0) {
             // 同时具有指标和数据集
-            JsonToCalciteExample jsonUtil = new JsonToCalciteExample();
-            // 获取指标逻辑
-            String logic = indexModel.getIndexLogic();
             // 先将sql中指标公式替换掉,再将得到的sql执行
-            String editSql =  TemplateUtil.execute(indexModel.getIndexNo(),logic,indexResult);
-            // 将数据集得到的数据转json
-            String json = JsonUtil.toJSONString(dataSetResult);
+            String editSql =  TemplateUtil.execute(indexConfigModel.getIndexNo(),calcLogic,indexResult);
             // 执行拿结果
-            Object resValue = jsonUtil.jsonTotable(json,editSql);
-            // 放结果
-            thisResult.put(this.getCalcCode(),resValue);
+            indexCalcResult = doExecuteSql(dataSetResult,editSql);
+        } else {
+            // 既不依赖其他指标,也不依赖数据集
+            // 执行公式,结果放入结果集
+            indexCalcResult = ScriptUtil.executeScript(indexConfigModel.getIndexNo(),calcLogic,indexResult);
+        }
+
+        if (null != indexCalcResult) {
+            thisResult.put(this.getCalcCode(),indexCalcResult);
+        } else {
+            String defaultValueType = this.indexConfigModel.getDefaultValueType();
+            String defaultValue = this.indexConfigModel.getDefaultValue();
+            indexCalcResult = getDefaultValue(defaultValue,defaultValueType);
+            thisResult.put(this.getCalcCode(),indexCalcResult);
+        }
+    }
+
+    /**
+     * 执行sql获取结果
+     * @param dataSetResult
+     * @param logic
+     * @return
+     */
+    private Object doExecuteSql(Map<String,AbstractSchema> dataSetResult,String logic) {
+        // 执行sql的工具类实例
+        CalciteUtil finalInstance;
+        try {
+            // 创建示例
+            CalciteUtil instance = CalciteUtil.getInstance();
+            // 添加域
+            dataSetResult.forEach(instance::addSchema);
+            finalInstance = instance;
+        } catch (SQLException e) {
+            log.error(e.getMessage());
+            throw new BusinessException(e.getMessage(),e);
+        }
+
+        try {
+            return finalInstance.doExecute(logic);
+        } catch (SQLException e) {
+            throw new RuntimeException(e);
+        }
+
+    }
+
+    /**
+     *
+     * @param defaultValue
+     * @param defaultValueType
+     * @return
+     */
+    private Object getDefaultValue(String defaultValue, String defaultValueType) {
+        Object castValue = null;
+        if (StrUtil.isNotBlank(defaultValue) && StrUtil.isNotBlank(defaultValueType)) {
+            try {
+                switch (defaultValueType) {
+                    case "INT":
+                        castValue = Convert.toInt(defaultValue);
+                        break;
+                    case "STRING":
+                        castValue = defaultValue;
+                        break;
+                    case "BOOLEAN":
+                        castValue = Convert.toBool(defaultValue);
+                        break;
+                    case "BIGDECIMAL":
+                        castValue = Convert.toBigDecimal(defaultValue);
+                        break;
+                }
+                if (null ==castValue) {
+                    String error = "转化异常";
+                    log.error(error);
+                    throw new ClassCastException(error);
+                }
+            } catch (ClassCastException e) {
+                throw new BusinessException(e.getMessage());
+            }
+        } else {
+            log.error("为设置默认值与默认类型");
         }
+        return castValue;
     }
 
 

+ 165 - 40
src/main/java/com/sundata/internalevaluation/calc/calcUnit/InterfaceCalcUnit.java

@@ -1,21 +1,31 @@
 package com.sundata.internalevaluation.calc.calcUnit;
 
+import cn.hutool.extra.spring.SpringUtil;
 import cn.hutool.http.HttpRequest;
 import cn.hutool.http.HttpResponse;
 import cn.hutool.http.HttpUtil;
+import cn.hutool.json.JSONObject;
+import cn.hutool.json.JSONUtil;
+import com.sundata.admin.AdminUtil;
 import com.sundata.common.util.DBExecutor;
 import com.sundata.common.util.JsonUtil;
+import com.sundata.internalevaluation.calc.calcUnit.interfaces.InterfaceRunning;
 import com.sundata.internalevaluation.calc.model.CalcException;
 import com.sundata.internalevaluation.calc.model.CalcResult;
 import com.sundata.internalevaluation.calc.model.CalcUnit;
 import com.sundata.internalevaluation.calc.model.finals.CalcType;
 import com.sundata.internalevaluation.calc.util.InvokeUtil;
-import com.sundata.internalevaluation.configuration.model.Interface;
-import com.sundata.internalevaluation.configuration.model.InterfaceParam;
+import com.sundata.internalevaluation.configuration.model.SysInterface;
+import com.sundata.internalevaluation.configuration.model.SysInterfaceParam;
 import com.sundata.internalevaluation.script.TemplateUtil;
+import io.swagger.v3.oas.annotations.responses.ApiResponse;
+import org.apache.commons.text.StringSubstitutor;
+import org.apache.flink.connector.jdbc.utils.JdbcUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.jdbc.core.JdbcTemplate;
 
+import javax.swing.*;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -32,7 +42,7 @@ public class InterfaceCalcUnit extends CalcUnit {
     private static final Logger log = LoggerFactory.getLogger(InterfaceCalcUnit.class);
 
 
-    private final Interface anInterface;
+    private final SysInterface sysInterface;
 
 
     /**
@@ -43,9 +53,9 @@ public class InterfaceCalcUnit extends CalcUnit {
      * @param calcType    计算类型
      * @param initContext 计算单元初始化参数
      */
-    public InterfaceCalcUnit(String calcCode, String calcName, CalcType calcType, Map<String, Object> initContext, Interface anInterface) {
+    public InterfaceCalcUnit(String calcCode, String calcName, CalcType calcType, Map<String, Object> initContext, SysInterface sysInterface) {
         super(calcCode, calcName, calcType, initContext);
-        this.anInterface = anInterface;
+        this.sysInterface = sysInterface;
     }
 
     /**
@@ -77,7 +87,7 @@ public class InterfaceCalcUnit extends CalcUnit {
      */
     @Override
     public List<CalcUnit> getSourceCalcUnits() {
-//        List<InterfaceParam> paramList = anInterface.getParamList();
+//        List<SysInterfaceParam> paramList = sysInterface.getParamList();
 //        List<CalcUnit> childCalcUnitList = paramList.stream()
 //                .map(item -> new InterfaceParamCalcUnit(item.getParamObjName(), item.getParamChineseName(), CalcType.INTERFACEPARAM, Map.of(), item))
 //                .collect(Collectors.toList());
@@ -121,12 +131,16 @@ public class InterfaceCalcUnit extends CalcUnit {
                      Map<CalcUnit, CalcResult<String, Object>> sourceResults) {
 
         log.debug("计算{}:编号:{},名称:{},流水号:{}", getCalcType().getName(), getCalcCode(), getCalcName(), calculateInstanceNumber);
-
+        // 获取新流水号,每次请求接口必须不一样
+        String globalReqNo = AdminUtil.getNextId("cretSeqno");
+        context.put("globalReqNo",globalReqNo);
+        // creditSeqNo 查询正i性能的流水号 如果不存在就重新赋值 只有征信的请求才会用到
+        context.putIfAbsent("creditSeqNo",globalReqNo);
 
         // 接口内容类型
-        String reqContentTypeCode = this.anInterface.getReqContentTypeCode();
+        String reqContentTypeCode = this.sysInterface.getReqContentTypeCode();
         // 接口请求类型
-        String interfaceTypeCode = this.anInterface.getInterfaceTypeCode();
+        String interfaceTypeCode = this.sysInterface.getInterfaceTypeCode();
 
         // 请求类型不为 http或者 https   或者   请求体不为 json
         if (!interfaceTypeCode.startsWith("http") || !"json".equals(reqContentTypeCode)) {
@@ -134,23 +148,21 @@ public class InterfaceCalcUnit extends CalcUnit {
             throw new CalcException("目前仅支持http和https的请求和json请求体");
         }
 
-
-        /**
-         * 计算参数
-         */
-        List<InterfaceParam> paramList = anInterface.getParamList();
-
+        // 请求参数实体列表
+        List<SysInterfaceParam> paramList = sysInterface.getParamList();
         Map<String, String> headersMap = new HashMap<>();
         Map<String, Object> paramMap = new HashMap<>();
 
         // 封装请求参数
         paramList.forEach(
                 param -> {
-                    Object result = getResultByInterfaceParam(param, context);
+                    Object result = getParamValue(param, context);
                     String isHeader = param.getIsHeader();
                     if ("true".equals(isHeader)) {
+                        // 请求头信息
                         headersMap.put(param.getParamObjName(), JsonUtil.toJSONString(result));
                     } else {
+                        // 请求参数
                         paramMap.put(param.getParamObjName(), result);
                     }
                 }
@@ -159,32 +171,105 @@ public class InterfaceCalcUnit extends CalcUnit {
 
         // http请求或者 https请求
         if (interfaceTypeCode.startsWith("http")) {
-            String url = interfaceTypeCode + "://" + anInterface.getInterfaceIp() + ":" + anInterface.getInterfacePort() + anInterface.getInterfaceUrl();
-            HttpRequest postRequest = HttpUtil.createPost(url);
-            postRequest.addHeaders(headersMap);
-            String jsonString = JsonUtil.toJSONString(paramMap);
-            log.warn("请求参数json:{}", jsonString);
-            postRequest.body(jsonString);
-            try (HttpResponse httpResponse = postRequest.execute()) {
-                String responseStr = httpResponse.body();
-                log.debug("请求接口【{}】的结果是:{}", url, responseStr);
-                thisResult.put(getCalcCode(), responseStr);
+            String url = interfaceTypeCode + "://" +
+                    sysInterface.getInterfaceIp() + ":" +
+                    sysInterface.getInterfacePort() +
+                    sysInterface.getInterfaceUrl();
+
+            Map<String,Object> allMap = new HashMap<>(headersMap);
+            allMap.putAll(paramMap);
+            StringSubstitutor sub  = new StringSubstitutor(allMap);
+            // 替换url中的变量
+            url = sub.replace(url);
+
+            // 请求体模板和完整请求体
+            String reqBodyTemplate = sysInterface.getReqBodyTemplate();
+            String jsonString = "";
+            // 替换请求报文模板中的变量
+            if (reqBodyTemplate != null && !reqBodyTemplate.trim().isEmpty()) {
+                jsonString = TemplateUtil.execute("interface-temp-bodyTemp" + getCalcCode(),reqBodyTemplate,allMap);
             }
+
+            // 需要使用sdk发送请求的接口编号
+            List<String> sdkInterfaceNo = List.of("OY15120241127004","IY15120250124001");
+            // 拿到响应体
+            String responseStr = null;
+            String interfaceNo = sysInterface.getId();
+            if (sdkInterfaceNo.contains(interfaceNo)) {
+                HashMap<String,Object> allparam = new HashMap<>(paramMap);
+                allparam.putAll(headersMap);
+                // responseStr = sendRequestBySDKAPI(url,jsonString,allparam);
+            } else {
+                //  创建 HttpRequest 对象
+                HttpRequest httpRequest = null;
+                String reqMethod = sysInterface.getReqMethod();
+                if ("get".equals(reqMethod)) {
+                    httpRequest = HttpUtil.createGet(url);
+                } else {
+                    httpRequest = HttpUtil.createPost(url);
+                }
+
+                // 设置请求头和请求体
+                httpRequest.addHeaders(headersMap);
+                if (jsonString != null && !jsonString.trim().isEmpty()) {
+                    httpRequest.body(jsonString);
+                }
+
+                HttpResponse httpResponse = httpRequest.execute();
+                responseStr = httpResponse.body();
+
+                InterfaceRunning interfaceRunning = null;
+
+                String decryptionLogic = sysInterface.getDecryptionLogic();
+
+                // 解密逻辑
+                if (null != decryptionLogic && !decryptionLogic.trim().isEmpty()) {
+                    interfaceRunning = SpringUtil.getBean(decryptionLogic);
+                    if (interfaceRunning != null) {
+                        responseStr = interfaceRunning.decode(responseStr);
+                    }
+                }
+
+                // 解密后处理逻辑
+                String processLogic = sysInterface.getProcessLogic();
+                if (processLogic != null && !processLogic.trim().isEmpty()) {
+                    interfaceRunning = SpringUtil.getBean(processLogic);
+                    if (interfaceRunning != null) {
+                        responseStr = interfaceRunning.afterDecode(interfaceNo,responseStr,calculateInstanceNumber);
+                    }
+                }
+
+                thisResult.put(getCalcCode(),responseStr);
+
+
+            }
+
+//            HttpRequest postRequest = HttpUtil.createPost(url);
+//            postRequest.addHeaders(headersMap);
+//            String jsonString = JsonUtil.toJSONString(paramMap);
+//            log.warn("请求参数json:{}", jsonString);
+//            postRequest.body(jsonString);
+//            try (HttpResponse httpResponse = postRequest.execute()) {
+//                String responseStr = httpResponse.body();
+//                log.debug("请求接口【{}】的结果是:{}", url, responseStr);
+//                thisResult.put(getCalcCode(), responseStr);
+//            }
         }
     }
 
 
 
-    private Object getResultByInterfaceParam(InterfaceParam interfaceParam, Map<String, Object> context) {
+    private Object getParamValue(SysInterfaceParam sysInterfaceParam, Map<String, Object> context) {
         // 参数获取类型
-        String paramRetrievalTypeCode = interfaceParam.getParamRetrievalTypeCode();
+        String paramRetrievalTypeCode = sysInterfaceParam.getParamRetrievalTypeCode();
 
         // 参数获取内容
-        String paramRetrievalTypeCont = interfaceParam.getParamRetrievalTypeCont();
-
+        String paramRetrievalTypeCont = sysInterfaceParam.getParamRetrievalTypeCont();
 
         Object result = null;
 
+        JdbcTemplate jdbcTemplate = JdbcUtils.getJdbcTemplate();
+
         // 获取参数值
         switch (paramRetrievalTypeCode) {
             // 固定值
@@ -202,26 +287,26 @@ public class InterfaceCalcUnit extends CalcUnit {
                 break;
             // SQL-Map
             case "sqlmap":
-                String execute = TemplateUtil.execute("interfaceParam-temp-sqlmap-" + getCalcCode(), paramRetrievalTypeCont, context);
-                log.warn("sql语句为:{}",execute);
-                result = DBExecutor.doQuery(execute);
+                String sqlMap = TemplateUtil.execute("interfaceParam-temp-sqlmap-" + getCalcCode(), paramRetrievalTypeCont, context);
+                log.warn("sql语句为:{}",sqlMap);
+                result = jdbcTemplate.queryForMap(sqlMap);
                 break;
             // SQL-List
             case "sqllist":
-                String execute1 = TemplateUtil.execute("interfaceParam-temp-sqllist-" + getCalcCode(), paramRetrievalTypeCont, context);
-                log.warn("sql语句为:{}",execute1);
-                result = DBExecutor.doQueryMapList(execute1);
+                String sqlList = TemplateUtil.execute("interfaceParam-temp-sqllist-" + getCalcCode(), paramRetrievalTypeCont, context);
+                log.warn("sql语句为:{}",sqlList);
+                result = jdbcTemplate.queryForList(sqlList);
                 break;
             // SQL-String
             case "sqlstring":
-                String execute2 = TemplateUtil.execute("interfaceParam-temp-sqlstring-" + getCalcCode(), paramRetrievalTypeCont, context);
-                log.warn("sql语句为:{}",execute2);
-                result = DBExecutor.doQuery(execute2);
+                String sqlString = TemplateUtil.execute("interfaceParam-temp-sqlstring-" + getCalcCode(), paramRetrievalTypeCont, context);
+                log.warn("sql语句为:{}",sqlString);
+                result = jdbcTemplate.queryForObject(sqlString, Object.class);
                 break;
         }
 
 
-        String paramTypeCode = interfaceParam.getParamTypeCode();
+        String paramTypeCode = sysInterfaceParam.getParamTypeCode();
         if ("string".equals(paramTypeCode)) {
             if (result instanceof String) {
 
@@ -254,4 +339,44 @@ public class InterfaceCalcUnit extends CalcUnit {
         }
         return result;
     }
+
+
+    /**
+     *
+     * @param url
+     * @param bodyStr
+     * @param allParam
+     * @return
+     */
+//    private String sendRequestBySDKAPI (String url, String bodyStr, HashMap<String,Object> allParam) {
+//        Object publicKey = allParam.get("publicKey");
+//        if (publicKey == null) {
+//            throw new RuntimeException("publicKey 不能为空");
+//        }
+//        Object systemCode = allParam.get("systemCode");
+//        if (systemCode == null) {
+//            throw new RuntimeException("systemCode 不能为空");
+//        }
+//
+//        // 构建发送请求客户端
+//        ApiClient apiClient = ApiClient.builder().apiUrl(url)
+//                .publicKey(publicKey)
+//                .systemCode(systemCode)
+//                .build();
+//
+//        // 构建json反序列化为MyApiRequest 对象
+//        MyApiRequest apiRequest = JSONUtil.toBean(bodyStr,MyApiRequest.class);
+//        String jsonPrettyStr = JSONUtil.toJsonPrettyStr(apiRequest);
+//        // 相应信息
+//        ApiResponse<JSONObject> response = apiRequest.excute(apiClient);
+//        String jsonStr = JSONUtil.toJsonStr(response);
+//        return jsonStr;
+//    }
+//
+////    private static class MyApiRequest extends ApiRequest<HashMap<String,Object>, JSONObject> {
+//        public MyApiRequest() {
+//            super.setResponseDataClass(JSONObject.class);
+//        }
+//    }
+
 }

+ 12 - 2
src/main/java/com/sundata/internalevaluation/calc/calcUnit/QueryLogicCalcUnit.java

@@ -1,14 +1,18 @@
 package com.sundata.internalevaluation.calc.calcUnit;
 
+import cn.hutool.extra.spring.SpringUtil;
 import com.sundata.common.util.DBExecutor;
 import com.sundata.internalevaluation.calc.model.CalcResult;
 import com.sundata.internalevaluation.calc.model.CalcUnit;
 import com.sundata.internalevaluation.calc.model.finals.CalcType;
+import com.sundata.internalevaluation.conf.UpperMapRowMapper;
 import com.sundata.internalevaluation.configuration.model.QueryLogicModel;
 import com.sundata.internalevaluation.script.TemplateUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.jdbc.core.JdbcTemplate;
 
+import javax.sql.DataSource;
 import java.util.List;
 import java.util.Map;
 
@@ -106,8 +110,14 @@ public class QueryLogicCalcUnit extends CalcUnit {
         String sql = TemplateUtil.execute(this.getCalcCode(), sqlTemplate, context);
         log.debug("替换模板后sql:",sql);
         // 查询本地数据库
-        List<Map<String, Object>> maps = DBExecutor.doQueryMapList(sql);
+        // List<Map<String, Object>> maps = DBExecutor.doQueryMapList(sql);
+        JdbcTemplate jdbcTemplate = SpringUtil.getBean(JdbcTemplate.class);
+        if (null == jdbcTemplate) {
+            jdbcTemplate = new JdbcTemplate(SpringUtil.getBean(DataSource.class));
+        }
+        List<Map<String,Object>> mapList = jdbcTemplate.queryForList(sql,new UpperMapRowMapper());
+
         // 结果封装,数据项名做key
-        thisResult.put(this.queryLogicModel.getDataItemName(), maps);
+        thisResult.put(this.queryLogicModel.getDataItemName(), mapList);
     }
 }

+ 27 - 4
src/main/java/com/sundata/internalevaluation/calc/calcUnit/RiskControlStrategyCalcUnit.java

@@ -95,12 +95,35 @@ public class RiskControlStrategyCalcUnit extends CalcUnit {
     @Override
     public void calc(CalcResult<String, Object> thisResult, String calculateInstanceNumber, Map<String, Object> context, Map<CalcUnit, CalcResult<String, Object>> sourceResults) {
 
-        Map<String,Object> resResultMap =new HashMap<>();
+//        Map<String,Object> resResultMap =new HashMap<>();
+//        sourceResults.forEach(
+//                (calcUnit,result) -> {
+//                    resResultMap.putAll(result);
+//        }
+//        );
+//        thisResult.put(this.getCalcCode(),resResultMap);
+        Map<String,Object> rulesMap = new HashMap<>();
+        Map<String,Object> indexListMap = new HashMap<>();
+        rulesMap.put("approResult","1");
+        List<Object> hitRuleList = new ArrayList<>();
         sourceResults.forEach(
                 (calcUnit,result) -> {
-                    resResultMap.putAll(result);
-        }
+                    if (calcUnit instanceof RulesCalcUnit) {
+                        Map<String,Object> rulesResMap = (Map<String,Object>) result.get(calcUnit.getCalcCode());
+                        hitRuleList.addAll((List) rulesResMap.get("hitRule"));
+                        Object approResult = rulesResMap.get("approResult");
+                        if ("2".equals(approResult)) {
+                            rulesMap.put("approResult","2");
+                        }
+                    } else if (calcUnit instanceof IndexListConfigCalcUnit) {
+                        Map<String,Object> indexResMap = (Map<String, Object>) result.get(calcUnit.getCalcCode());
+                        indexListMap.put(calcUnit.getCalcCode(),indexResMap);
+                    }
+                }
         );
-        thisResult.put(this.getCalcCode(),resResultMap);
+        rulesMap.put("hitRule",hitRuleList);
+        thisResult.put("ruleResult",rulesMap);
+        thisResult.put("indexResult",indexListMap);
+
     }
 }

+ 25 - 18
src/main/java/com/sundata/internalevaluation/calc/calcUnit/RuleCalcUnit.java

@@ -4,11 +4,11 @@ import cn.hutool.extra.spring.SpringUtil;
 import com.sundata.internalevaluation.calc.model.CalcResult;
 import com.sundata.internalevaluation.calc.model.CalcUnit;
 import com.sundata.internalevaluation.calc.model.finals.CalcType;
-import com.sundata.internalevaluation.configuration.model.IndexModel;
-import com.sundata.internalevaluation.configuration.model.Rule;
-import com.sundata.internalevaluation.configuration.model.RuleIndex;
+import com.sundata.internalevaluation.configuration.model.IndexConfigModel;
+import com.sundata.internalevaluation.configuration.model.SysReqRule;
+import com.sundata.internalevaluation.configuration.model.SysRuleIndex;
 import com.sundata.internalevaluation.configuration.service.IndexConfigService;
-import com.sundata.internalevaluation.configuration.service.RuleService;
+import com.sundata.internalevaluation.configuration.service.SysReqRuleService;
 import com.sundata.internalevaluation.script.ScriptUtil;
 import com.sundata.internalevaluation.script.TemplateUtil;
 import org.slf4j.Logger;
@@ -18,6 +18,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.stream.Collectors;
+import java.util.concurrent.atomic.AtomicReference;
 
 /**
  * Created by IntelliJ IDEA.
@@ -30,7 +31,7 @@ public class RuleCalcUnit extends CalcUnit {
 
     private static final Logger log = LoggerFactory.getLogger(RuleCalcUnit.class);
 
-    private final Rule reqRule;
+    private final SysReqRule reqRule;
 
     private final IndexConfigService indexConfigService;
 
@@ -42,7 +43,7 @@ public class RuleCalcUnit extends CalcUnit {
      * @param calcType    计算类型
      * @param initContext 计算单元初始化参数
      */
-    public RuleCalcUnit(String calcCode, String calcName, CalcType calcType, Map<String, Object> initContext, Rule reqRule) {
+    public RuleCalcUnit(String calcCode, String calcName, CalcType calcType, Map<String, Object> initContext, SysReqRule reqRule) {
         super(calcCode, calcName, calcType, initContext);
         this.reqRule = reqRule;
         this.indexConfigService = SpringUtil.getBean(IndexConfigService.class);
@@ -82,9 +83,10 @@ public class RuleCalcUnit extends CalcUnit {
      */
     @Override
     public List<CalcUnit> getSourceCalcUnits() {
-        List<IndexModel> indexModels = indexConfigService.selectIndexListByRuleNo(this.reqRule.getRuleNo());
-        return indexModels.stream()
+        List<IndexConfigModel> indexConfigModels = indexConfigService.selectIndexListByRuleNo(this.reqRule.getRuleNo());
+        List<CalcUnit> collect =indexConfigModels.stream()
                 .map(item -> new IndexCalcUnit(item.getIndexNo(), item.getIndexName(), Map.of(), item)).collect(Collectors.toList());
+        return collect;
     }
 
     /**
@@ -122,17 +124,19 @@ public class RuleCalcUnit extends CalcUnit {
                      Map<CalcUnit, CalcResult<String, Object>> sourceResults) {
         log.debug("计算{}:编号:{},名称:{},流水号:{}", getCalcType().getName(),getCalcCode(),getCalcName(),calculateInstanceNumber);
 
-        RuleService ruleService = SpringUtil.getBean(RuleService.class);
-        List<RuleIndex> sysRuleIndices = ruleService.selectRuleIndexListByRuleNo(getCalcCode());
-        Map<String, String> indexParamMap = sysRuleIndices.stream().collect(Collectors.toMap(RuleIndex::getIndexNo, RuleIndex::getRuleLogicParam));
+        SysReqRuleService ruleService = SpringUtil.getBean(SysReqRuleService.class);
+        List<SysRuleIndex> sysRuleIndices = ruleService.selectRuleIndexListByRuleNo(getCalcCode());
+        Map<String, String> indexParamMap = sysRuleIndices.stream().collect(Collectors.toMap(SysRuleIndex::getIndexNo, SysRuleIndex::getRuleLogicParam));
 
         // 所有规则计算的结果
         Map<String, Object> indexResMap = new HashMap<>();
 
+        AtomicReference<Object> indexResult = new AtomicReference<>("");
         sourceResults.forEach((calcUnit, calcResult) -> {
             String key = indexParamMap.get(calcUnit.getCalcCode());
             Object value = calcResult.get(calcUnit.getCalcCode());
             indexResMap.put(key, value);
+            indexResult.set(value);
         });
 
         // 规则的模板
@@ -144,15 +148,18 @@ public class RuleCalcUnit extends CalcUnit {
         // 执行脚本结果
         Object res = ScriptUtil.executeScript("script-rule-" + getCalcCode(), script, indexResMap);
 
-
         Map<String, Object> resMap = new HashMap<>();
 
-        resMap.put("scriptRes",res);
-        resMap.put("ruleNo", getCalcCode());
-        resMap.put("indexRes", indexResMap);
-        resMap.put("resultType", reqRule.getRuleResult());
-        resMap.put("ruleDesc",reqRule.getTemplateDesc());
-        resMap.put("ruleResult",TemplateUtil.execute("template-ruleConclusion-" + getCalcCode(),reqRule.getRuleConclusionTemplate(), indexResMap));
+        resMap.put("resCalc",res);//计算结果 true false
+        resMap.put("ruleNo", getCalcCode());//规则编号
+        resMap.put("indexRes", indexResMap);//指标结果
+        resMap.put("resultType", reqRule.getRuleResult());//规则的结果 0=无 1=拦截 2=预警
+        //resMap.put("ruleDesc",reqRule.getTemplateDesc());//规则描述
+        resMap.put("datasource",reqRule.getRuleDatasour());
+        //规则结论
+        Map<String,Object> tempMap=new HashMap<>()(resMap);
+        tempMap.putAll(indexResMap);
+        resMap.put("ruleResult",TemplateUtil.execute("template-ruleConclusion-" + getCalcCode(),reqRule.getRuleConclusionTemplate(), tempMap));
 
         // 放计算结果
         thisResult.put(this.getCalcCode(), resMap);

+ 17 - 8
src/main/java/com/sundata/internalevaluation/calc/calcUnit/RulesCalcUnit.java

@@ -156,21 +156,30 @@ public class RulesCalcUnit extends CalcUnit {
 
         List<Map<String, Object>> hitRule = list.stream().filter(map -> {
                     boolean isTrigger = false;
-                    Object o = map.get("scriptRes");
+                    Object o = map.get("resCalc");
                     if (o instanceof Boolean) {
                         isTrigger = (Boolean) o;
                     } else {
-                        isTrigger = "true".equals(map.get("scriptRes"));
+                        isTrigger = "true".equals(map.get("resCalc"));
                     }
-                    boolean intercept = "1".equals(map.get("resultType"));
-                    boolean warn = "2".equals(map.get("resultType"));
-                    if (intercept) {
-                        isIntercept.set(true);
+
+                    if(!isTrigger){
+                        return false;
                     }
-                    if (warn) {
+
+                    //触发了
+
+                    Object resultType=map.get("resultType");
+
+                    //规则结果为 无 的情况
+                    if("0".equals(resultType)){
+                        return false;
+                    }else if ("1".equals(resultType)){
+                        isIntercept.set(true);
+                    }else if ("2".equals(resultType)){
                         isWarn.set(true);
                     }
-                    return isTrigger && (intercept || warn);
+                    return true;
                 }
         ).collect(Collectors.toList());
 

+ 37 - 0
src/main/java/com/sundata/internalevaluation/calc/calcUnit/interfaces/InterfaceRunning.java

@@ -0,0 +1,37 @@
+package com.sundata.internalevaluation.calc.calcUnit.interfaces;
+
+public interface InterfaceRunning {
+
+    /**
+     * 加密逻辑
+     * @param interfaceStr
+     * @return 加密后逻辑
+     */
+    String encode(String interfaceStr);
+
+    /**
+     *
+     * @param encodeString
+     * @return 保温内存
+     */
+    String decode(String encodeString);
+
+    /**
+     *
+     * @param interfaceNo
+     * @param interfaceStr
+     * @param calculateInstanceNumber
+     * @return 处理后的字符串报文
+     */
+    String afterDecode(String interfaceNo, String interfaceStr, String calculateInstanceNumber);
+
+    /**
+     * 处理后的报文内容,若需要映射为对象,则对象内容应当具备初始化方法与getset 方法来提供映射逻辑,简而言之,类似于 将json 或 xml 转化为model对象
+     * @param interfaceStr 经过处理后的json 报文或 xml 报文
+     * @param clazz 需要被转化的对象类型
+     * @return 对象
+     * @param <T> 对象实例
+     */
+    <T> T coverObject(String interfaceStr, Class<T> clazz);
+
+}

+ 168 - 49
src/main/java/com/sundata/internalevaluation/calc/util/CalciteUtil.java

@@ -1,12 +1,15 @@
 package com.sundata.internalevaluation.calc.util;
 
+import com.sundata.common.util.JsonUtil;
 import org.apache.calcite.adapter.file.JsonScannableTable;
 import org.apache.calcite.jdbc.CalciteConnection;
 import org.apache.calcite.schema.SchemaPlus;
+import org.apache.calcite.schema.Table;
 import org.apache.calcite.schema.impl.AbstractSchema;
 import org.apache.calcite.util.Sources;
 
 import java.sql.*;
+import java.util.*;
 
 /**
  * Created by IntelliJ IDEA.
@@ -29,6 +32,52 @@ public class CalciteUtil {
     }
 
 
+    /**
+     * 构造所有表结构
+     * @param tablesMap
+     * @return
+     */
+    public static Map<String,JsonScannableTable> createTableMap(Map<String,Object> tablesMap) {
+        Map<String,JsonScannableTable> resMap = new HashMap<>();
+        tablesMap.forEach((k,v)->{
+            JsonScannableTable jsonScannableTable = new JsonScannableTable(Sources.of(JsonUtil.toJSONString(v)));
+            resMap.put(k,jsonScannableTable);
+        });
+        return resMap;
+    }
+
+    /**
+     *
+     * @param tablesMap
+     * @return
+     */
+    public static AbstractSchema createSchema(Map<String,JsonScannableTable> tablesMap) {
+        Map<String, Table> resMap = new HashMap<>(tablesMap);
+        return new CustomSchema(resMap);
+    }
+
+
+
+
+
+    /**
+     *
+     */
+    public static class CustomSchema extends AbstractSchema {
+        private final Map<String,Table> tables;
+
+        public CustomSchema(Map<String,Table> tables) {
+            this.tables = tables;
+        }
+
+        @Override
+        public Map<String,Table> getTableMap() {
+            return tables;
+        }
+
+    }
+
+
     /**
      * 获取工具类对象实例
      * @return CalciteUtil
@@ -41,6 +90,17 @@ public class CalciteUtil {
     }
 
 
+    /**
+     * 创建跟根schema
+     * @throws SQLException
+     */
+    private void createRootSchema(String schemaName, AbstractSchema abstractSchema) throws SQLException {
+        connection = DriverManager.getConnection("jdbc:calcite:");
+        CalciteConnection calcConnection = connection.unwrap(CalciteConnection.class);
+        rootSchema = calcConnection.getRootSchema();
+        addSchema(schemaName,abstractSchema);
+    }
+
     /**
      * 创建跟根schema
      * @throws SQLException
@@ -51,6 +111,15 @@ public class CalciteUtil {
         rootSchema = calcConnection.getRootSchema();
     }
 
+    /**
+     * 向根域中添加域
+     * @param schemaName 域名
+     * @param abstractSchema 表结构
+     */
+    public void addSchema(String schemaName,AbstractSchema abstractSchema) {
+         rootSchema.add(schemaName,abstractSchema);
+    }
+
 
     /**
      * 创建 域 (数据库)
@@ -93,9 +162,59 @@ public class CalciteUtil {
             statement = connection.createStatement();
         }
         resultSet = statement.executeQuery(sql);
-        if (resultSet.next()) {
-            return resultSet.getObject(1);
+
+        // 每一行数据的List
+        List<LinkedHashMap<String,Object>> resList = new ArrayList<>();
+
+        ResultSetMetaData metaData = resultSet.getMetaData();
+
+        // 总列数
+        int columnCount = metaData.getColumnCount();
+        int row = 1;
+
+        String firstColumnName = "";
+
+        // 将所有列名打印输出
+        List<String> columns = new ArrayList<>();
+        StringJoiner sj = new StringJoiner("\t");
+        for (int i = 1; i < columnCount; i++) {
+            String columnName = metaData.getColumnName(i);
+            columns.add(columnName);
+            sj.add(columnName);
         }
+        // 第一个列名,返回单行但列的时候使用
+        firstColumnName = columns.get(0);
+
+        // 打印表头
+        System.out.println("NO"+"\t"+sj);
+        while (resultSet.next()) {
+            sj = new StringJoiner("\t");
+            sj.add(row++ + "");
+            LinkedHashMap<String,Object> resMap = new LinkedHashMap<>();
+            // 遍历,每一列
+            for (int i=1;i<=columnCount;i++) {
+                // 获取列名
+                String columnName = columns.get(i-1);
+                // 取得当前行当前列的数据
+                Object value = resultSet.getObject(columnName);
+                // 存入map
+                resMap.put(columnName,value);
+                sj.add(value== null?"" : value.toString());
+            }
+            // 打印输出每行数据
+            System.out.println(sj);
+            resList.add(resMap);
+        }
+        int size = resList.size();
+        if (size == 1 && columnCount == 1) {
+            // 返回第一行第一列
+            LinkedHashMap<String,Object> linkedHashMap = resList.get(0);
+            return linkedHashMap.get(firstColumnName);
+        } else if (size >=1 && columnCount>1) {
+            return resList;
+        }
+
+
         return null;
     }
 
@@ -117,51 +236,51 @@ public class CalciteUtil {
     }
 
 
-    public static void main(String[] args) throws SQLException {
-        String subject = "[\n" +
-                "    {\n" +
-                "      \"subNo\": \"07cc9eca-4e66-4d1c-9dd1-fe4671c2e18b\",\n" +
-                "      \"subName\": \"Associate Degree in Health Science\"\n" +
-                "    },\n" +
-                "    {\n" +
-                "      \"subNo\": \"cda7b717-bbff-40fd-af90-7e22dcea8457\",\n" +
-                "      \"subName\": \"Associate Degree in Medicine\"\n" +
-                "    },\n" +
-                "    {\n" +
-                "      \"subNo\": \"9cf8534b-37c6-4365-8453-d1454e336992\",\n" +
-                "      \"subName\": \"Master of Biological Science\"\n" +
-                "    },\n" +
-                "    {\n" +
-                "      \"subNo\": \"b21f3d9b-670b-4819-8faa-dbb2324f522a\",\n" +
-                "      \"subName\": \"Bachelor of Criminology\"\n" +
-                "    },\n" +
-                "    {\n" +
-                "      \"subNo\": \"635c0a8a-3701-43f1-a1d6-d359465df3c6\",\n" +
-                "      \"subName\": \"Master of Teaching\"\n" +
-                "    },\n" +
-                "    {\n" +
-                "      \"subNo\": \"2aa9e2d1-5f0e-41c6-8bf5-970b08d47b67\",\n" +
-                "      \"subName\": \"Associate Degree in Creative Arts\"\n" +
-                "    }\n" +
-                "  ]";
-
-
-        CalciteUtil instance = CalciteUtil.getInstance();
-
-        SchemaPlus db1 = instance.createDatabase("db1");
-
-        instance.createTable(db1, "tab1", subject);
-
-        String sql = "";
-        sql = """
-                select "db1"."tab1"."subName" from "db1"."tab1" where "db1"."tab1"."subNo" = '635c0a8a-3701-43f1-a1d6-d359465df3c6'
-                
-                """;
-        // 执行sql
-        Object o = instance.doExecute(sql);
-
-        System.out.println(o);
-        // 关闭资源
-        instance.closeResource();
-    }
+//    public static void main(String[] args) throws SQLException {
+//        String subject = "[\n" +
+//                "    {\n" +
+//                "      \"subNo\": \"07cc9eca-4e66-4d1c-9dd1-fe4671c2e18b\",\n" +
+//                "      \"subName\": \"Associate Degree in Health Science\"\n" +
+//                "    },\n" +
+//                "    {\n" +
+//                "      \"subNo\": \"cda7b717-bbff-40fd-af90-7e22dcea8457\",\n" +
+//                "      \"subName\": \"Associate Degree in Medicine\"\n" +
+//                "    },\n" +
+//                "    {\n" +
+//                "      \"subNo\": \"9cf8534b-37c6-4365-8453-d1454e336992\",\n" +
+//                "      \"subName\": \"Master of Biological Science\"\n" +
+//                "    },\n" +
+//                "    {\n" +
+//                "      \"subNo\": \"b21f3d9b-670b-4819-8faa-dbb2324f522a\",\n" +
+//                "      \"subName\": \"Bachelor of Criminology\"\n" +
+//                "    },\n" +
+//                "    {\n" +
+//                "      \"subNo\": \"635c0a8a-3701-43f1-a1d6-d359465df3c6\",\n" +
+//                "      \"subName\": \"Master of Teaching\"\n" +
+//                "    },\n" +
+//                "    {\n" +
+//                "      \"subNo\": \"2aa9e2d1-5f0e-41c6-8bf5-970b08d47b67\",\n" +
+//                "      \"subName\": \"Associate Degree in Creative Arts\"\n" +
+//                "    }\n" +
+//                "  ]";
+//
+//
+//        CalciteUtil instance = CalciteUtil.getInstance();
+//
+//        SchemaPlus db1 = instance.createDatabase("db1");
+//
+//        instance.createTable(db1, "tab1", subject);
+//
+//        String sql = "";
+//        sql = """
+//                select "db1"."tab1"."subName" from "db1"."tab1" where "db1"."tab1"."subNo" = '635c0a8a-3701-43f1-a1d6-d359465df3c6'
+//
+//                """;
+//        // 执行sql
+//        Object o = instance.doExecute(sql);
+//
+//        System.out.println(o);
+//        // 关闭资源
+//        instance.closeResource();
+//    }
 }

+ 10 - 0
src/main/java/com/sundata/internalevaluation/conf/UpperMapRowMapper.java

@@ -0,0 +1,10 @@
+package com.sundata.internalevaluation.conf;
+
+import org.springframework.jdbc.core.ColumnMapRowMapper;
+
+public class UpperMapRowMapper extends ColumnMapRowMapper {
+
+    protected String getColumnKey(String columnName) {
+        return columnName.toUpperCase();
+    }
+}

+ 20 - 0
src/main/java/com/sundata/internalevaluation/configuration/model/IndexModel.java

@@ -8,6 +8,10 @@ public class IndexModel {
     private String indexNo;
     // 指标名称
     private String indexName;
+    // 默认值
+    private String defaultValue;
+    // 默认值类型
+    private String defaultValueType;
     // 指标逻辑
     private String indexLogic;
     /**
@@ -48,6 +52,22 @@ public class IndexModel {
         this.indexName = indexName;
     }
 
+    public String getDefaultValueType() {
+        return defaultValueType;
+    }
+
+    public void setDefaultValueType(String defaultValueType) {
+        this.defaultValueType = defaultValueType;
+    }
+
+    public String getDefaultValue() {
+        return defaultValue;
+    }
+
+    public void setDefaultValue(String defaultValue) {
+        this.defaultValue = defaultValue;
+    }
+
     public String getIndexLogic() {
         return indexLogic;
     }