Browse Source

指标计算逻辑执行sql

ZhangYanJie 2 months ago
parent
commit
d63b1cdd2f

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

@@ -131,7 +131,7 @@ public class DataSetCalcUnit extends CalcUnit {
         Map<String,Object> resultMap = new HashMap<>();
         // 合并数据集
         sourceResults.forEach((calcUnit,result)-> {
-            resultMap.putAll((Map<String, Object>)result.get(calcUnit.getCalcCode()));
+            resultMap.putAll(result);
         });
         thisResult.put(this.getCalcCode(),resultMap);
 

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

@@ -13,6 +13,7 @@ import com.sundata.internalevaluation.configuration.service.SysInterfaceService;
 import com.sundata.internalevaluation.script.TemplateUtil;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.stream.Collectors;
@@ -85,7 +86,10 @@ public class DataSourcesCalcUnit extends CalcUnit {
         for ( String s : interfaceNos) {
             sysInterfaceModelList.add(sysInterfaceService.getById(s));
         }
-        return sysInterfaceModelList.stream().map(interfaceModel -> new InterfaceCalcUnit(interfaceModel.getId(),interfaceModel.getInterfaceName(),CalcType.INTERFACE,Map.of(),interfaceModel)).collect(Collectors.toList());
+        List<CalcUnit> interFaceList = sysInterfaceModelList.stream().map(interfaceModel -> new InterfaceCalcUnit(interfaceModel.getId(),interfaceModel.getInterfaceName(),CalcType.INTERFACE,Map.of(),interfaceModel)).collect(Collectors.toList());
+        List<CalcUnit> queryList = dataSourcesModel.getQueryLogic().stream().map(queryParam -> new QueryLogicCalcUnit(queryParam.getDataItemName(),queryParam.getScriptDescription(),CalcType.QUERYLOGIC,Map.of(),queryParam)).collect(Collectors.toList());
+        interFaceList.addAll(queryList);
+        return interFaceList;
 
         // 取得个配置信息
 //        List<DataSourcesModel> dataList = sourcesService.getDataList(new DataSourcesModel());
@@ -136,28 +140,24 @@ public class DataSourcesCalcUnit extends CalcUnit {
                 if (calcUnit instanceof InterfaceCalcUnit ) {
                     // 获取json
                     String json = result.get(calcUnit.getCalcCode()) instanceof String ? (String) result.get(calcUnit.getCalcCode()) : "";
-                    Map<Object, Object> objectObjectMap1 = JsonUtil.jsonToMap(json);
-                    Map<Object, Object> objectObjectMap = objectObjectMap1
-                            .entrySet().stream().collect(Collectors.toMap(entry ->"interface_"+entry.getKey(), Map.Entry::getValue));
-
-//                    objectObjectMap.forEach((key, value)->{
-//                        objectObjectMap.put("interface_"+key,value);
-//                        objectObjectMap.remove(key, value);
-//                    });
-
-                    thisResult.put(this.getCalcCode(), objectObjectMap);
+                    List<Object>  parseList = JsonUtil.jsonToList(json);
+                    Map<Object, Object> objectObjectMap = new HashMap<>();
+//                    Map<Object, Object> objectObjectMap = objectObjectMap1
+//                            .entrySet().stream().collect(Collectors.toMap(entry ->"interface_"+entry.getKey(), Map.Entry::getValue));
+                    // 结果封装
+                    thisResult.put(this.getCalcCode(), parseList);
                 }
             } else if ("JDBC".equals(dataSourcesModel.getDataSourcesType())) {
                 // 数据来源类型为jdbc时处理逻辑
                 if (calcUnit instanceof QueryLogicCalcUnit queryLogicCalcUnit) {
+                    // 取得查询逻辑脚本
                     String sqlTemplate = queryLogicCalcUnit.queryLogicModel.getSelectSqlScript();
+                    // 执行模板
                     String sql = TemplateUtil.execute(calcUnit.getCalcCode(), sqlTemplate, result);
+                    // 查询本地数据库
                     Map<String, Object> stringObjectMap = DBExecutor.doQueryMap(sql);
-                    stringObjectMap.forEach((key, value)->{
-                        stringObjectMap.put("jdbc_"+key,value);
-                        stringObjectMap.remove(key, value);
-                    });
-                    thisResult.put(this.getCalcCode(), stringObjectMap);
+                    // 结果封装,数据项名做key
+                    thisResult.put(queryLogicCalcUnit.queryLogicModel.getDataItemName(), stringObjectMap);
                 }
             }
         });

+ 21 - 53
src/main/java/com/sundata/internalevaluation/calc/calcUnit/IndexCalcUnit.java

@@ -118,19 +118,17 @@ public class IndexCalcUnit extends CalcUnit {
             dataSetCalcList = dataSetList.stream().map(dataSetModel -> new DataSetCalcUnit
                     (dataSetModel.getDataSetNo(),dataSetModel.getDataSetName(),Map.of(),dataSetModel)).collect(Collectors.toList());
         }
-//        selectList.stream().map(sourceModel -> new IndexSourceCalcUnit
-//                (sourceModel.getOtherIndexNo(),"",CalcType.INDEX,Map.of(),sourceModel));
+
         // 结果封装
         if (null != indexCalcList) {
             resultCalcMap.addAll(indexCalcList);
         }
+
         if (null !=  dataSetCalcList) {
             resultCalcMap.addAll(dataSetCalcList);
         }
-
-
+        // 返回所有源头节点
         return resultCalcMap;
-//        return ConfigImages.indexCalcUnitListMap.get(this);
     }
 
 
@@ -174,55 +172,56 @@ public class IndexCalcUnit extends CalcUnit {
         Map<String, Object> indexResult = new HashMap<>();
         // 声明只存放所有数据集源头节点变量
         Map<String, Object> dataSetResult = new HashMap<>();
-
+        // 遍历结果集
         sourceResults.forEach((calcUnit,result) -> {
             // 数据集
             if ( calcUnit instanceof DataSetCalcUnit dataSetCalcUnit){
                 if(dataSetCalcUnit.getCalcType() == CalcType.DATASET){
+                    // 记录记过集次数
                     dataSetNumber.getAndIncrement();
-                    dataSetResult.putAll((Map) result.get(calcUnit.getCalcCode()));
+                    // 将源头节点的返回值取出
+                    dataSetResult.putAll(result);
                 }
             }
             // 其他指标
             if( calcUnit instanceof IndexCalcUnit indexCalcUnit){
                 if(indexCalcUnit.getCalcType() == CalcType.INDEX){
+                    // 记录指标出现次数
                     indexNumber.getAndIncrement();
+                    // 记录指标节点的返回值
                     indexResult.putAll(result);
                 }
             }
         });
 
-
-        // TODO
-        // TODO
-
-
+        // TODO 计算过程开始
         if (dataSetNumber.get() > 0 && indexNumber.get() == 0){
-
+            // 如果数据集的数量大于0
+            // 将数据集返回结果转化成json
             String json = JsonUtil.toJSONString(dataSetResult);
+            // 实例化组建域对象
             JsonToCalciteExample jsonUtil = new JsonToCalciteExample();
-
-
-            // 如果数据集的数量大于0
             // 获取指标逻辑
             String logic = indexConfigModel.getIndexLogic();
+            // 获取域名
 
-            Object resValue = jsonUtil.jsonTotable(this.getCalcCode(),json,logic);
-
-            // 执行sql,并放进结果集内
+            // 执行sql
+            Object resValue = jsonUtil.jsonTotable(json,logic);
 
-            // TODO 计算逻辑
+            // 执行结果放进结果集内
             thisResult.put(this.getCalcCode(),resValue);
         }
+
         if(indexNumber.get() > 0 && dataSetNumber.get() == 0){
             // 如果指标的数量大于0
             // 获取指标逻辑
             String logic = indexConfigModel.getIndexLogic();
             // 执行公式,结果放入结果集
             Object result = ScriptUtil.executeScript(indexConfigModel.getIndexNo(),logic,indexResult);
-            // JsonToCalciteExample.jsonTotable()
+            // 指标执行公式放入结果集
             thisResult.put(this.getCalcCode(),result);
         }
+
         if (dataSetNumber.get() > 0 && indexNumber.get() >0) {
             // 同时具有指标和数据集
             JsonToCalciteExample jsonUtil = new JsonToCalciteExample();
@@ -233,43 +232,12 @@ public class IndexCalcUnit extends CalcUnit {
             // 将数据集得到的数据转json
             String json = JsonUtil.toJSONString(dataSetResult);
             // 执行拿结果
-            Object resValue = jsonUtil.jsonTotable(this.indexConfigModel.getIndexNo(),json,editSql);
+            Object resValue = jsonUtil.jsonTotable(json,editSql);
             // 放结果
             thisResult.put(this.getCalcCode(),resValue);
         }
     }
 
 
-    /**
-     *
-     * @param calciteConnection
-     * @param sql
-     * @return
-     */
-    public Object getConnectionTable (CalciteConnection calciteConnection,String sql) {
-        //获取域
-        SchemaPlus rootSchemafter = calciteConnection.getRootSchema();
-        //根域中获取子域
-        SchemaPlus childrenSchema=rootSchemafter.getSubSchema(this.getCalcCode());
-        //子域名
-        String childrenName = childrenSchema.getName();
-        System.out.println("childrenName: " + childrenName);
-        //子域中表名
-        tableSet = childrenSchema.getTableNames();
-
-        String value = "";
 
-        try {
-            Statement statement = calciteConnection.createStatement();
-            ResultSet resultSet = statement.executeQuery(sql);
-            if (resultSet.next()){
-                 //value = resultSet.getString(1);
-                return resultSet.getObject(1);
-//                System.out.println();
-            }
-        } catch (SQLException e) {
-            throw new RuntimeException(e);
-        }
-        return null;
-    }
 }

+ 55 - 53
src/main/java/com/sundata/internalevaluation/conf/JsonToCalciteExample.java

@@ -1,5 +1,6 @@
 package com.sundata.internalevaluation.conf;
 
+import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.sundata.internalevaluation.calc.calcUnit.IndexCalcUnit;
@@ -18,83 +19,52 @@ import org.apache.calcite.sql.type.SqlTypeName;
 import org.apache.calcite.util.Pair;
 import org.apache.calcite.util.Sources;
 
-import java.sql.Connection;
-import java.sql.DriverManager;
+import java.sql.*;
 import java.util.*;
 
 public class JsonToCalciteExample {
 
-    public  Object jsonTotable(String schemaName, String jsonDate, String sql)  {
+    public  Object jsonTotable(String jsonDate, String sql)  {
         // 1. JSON 数据
         Object resValue = null;
         try{
             // 2. 使用 Jackson 解析 JSON 数据
             ObjectMapper objectMapper = new ObjectMapper();
             JsonNode rootNode = objectMapper.readTree(jsonDate);
-
             // 3. 创建 Calcite Schema
             Connection connection = DriverManager.getConnection("jdbc:calcite:");
             CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
-
             SchemaPlus rootSchema = calciteConnection.getRootSchema();
 
-            SchemaPlus schema = rootSchema.add(schemaName,new AbstractSchema());
+
 
             // 注册 JSON 数据为表
             rootNode.fields().forEachRemaining(entry -> {
-                String tableName = entry.getKey();
-                JsonNode tableData = entry.getValue();
-                schema.add(tableName, new JsonScannableTable(Sources.of(tableData.toString())));
+                String schename =entry.getKey();
+                String schenameData="";
+                JsonNode schenameData1=null;
+                try {
+                    schenameData = String.valueOf(entry.getValue());
+                    schenameData1 =objectMapper.readTree(schenameData);
+                } catch (JsonProcessingException e) {
+                    throw new RuntimeException(e);
+                }
+                SchemaPlus schema = rootSchema.add(schename,new AbstractSchema());
+                schenameData1.fields().forEachRemaining(entrynext -> {
+                    String tableName =entrynext.getKey();
+                    JsonNode tableData =entrynext.getValue();
+                    schema.add(tableName, new JsonScannableTable(Sources.of(tableData.toString())));
+                });
             });
-            // 4. 执行 SQL 查询
-//            Statement statement = connection.createStatement();
-//            String sql = """
-//            SELECT "test"."input"."method"   FROM "test"."input"
-//        """;
-//
-//            ResultSet resultSet = statement.executeQuery(sql);
-//            while (resultSet.next()){
-//                String method=resultSet.getString("method");
-//                System.out.println(method);
-//            }
-            // 打印查询结果
-           /*while (resultSet.next()) {
-            System.out.printf("Order ID: %d, Amount: %.2f, Name: %s%n",
-                    resultSet.getInt("user_id"),
-                    resultSet.getDouble("amount"),
-                    resultSet.getString("name"));
-
-           }
-
-            // 关闭连接
-            /*resultSet.close();
-            statement.close();*/
-            //获取域
-//            SchemaPlus rootSchemafter = calciteConnection.getRootSchema();
-//            //根域中获取子域
-//            SchemaPlus childrenSchema=rootSchemafter.getSubSchema("test");
-//            //子域名
-//            String childrenName = childrenSchema.getName();
-//            System.out.println("childrenName: " + childrenName);
-//            //子域中表名
-//            Set<String> tableNames = childrenSchema.getTableNames();
-//            for (String tableName : tableNames) {
-//                System.out.println("Table: " + tableName);
-//            }
-
-            //调用张艳杰接收连接方法,传参  calciteConnection
-            //方法示例
-            IndexCalcUnit model = new IndexCalcUnit(schemaName,"",Map.of(),new IndexConfigModel());
-            resValue = model.getConnectionTable(calciteConnection,sql);
+
+            //调用张艳杰接收连接方法
+            resValue = getConnectionTable(calciteConnection,sql);
 
             connection.close();
         } catch (Exception e) {
             throw new RuntimeException(e);
         }
-        finally {
-            return resValue;
-        }
-
+        return resValue;
     }
 
     // 自定义表类实现
@@ -140,4 +110,36 @@ public class JsonToCalciteExample {
             return Linq4j.asEnumerable(result);
         }
     }
+
+    /**
+     *
+     * @param calciteConnection
+     * @param sql
+     * @return
+     */
+    public Object getConnectionTable (CalciteConnection calciteConnection,String sql) {
+        //获取域
+        SchemaPlus rootSchemafter = calciteConnection.getRootSchema();
+        //根域中获取子域
+        SchemaPlus childrenSchema=rootSchemafter.getSubSchema(calciteConnection.getRootSchema().getName());
+        Object childrebnameobj=rootSchemafter.getSubSchemaNames();
+        //子域名
+//        String childrenName = childrenSchema.getName();
+//        System.out.println("childrenName: " + childrenName);
+//        //子域中表名
+//        Set<String> tableSet = childrenSchema.getTableNames();
+        // 定义返回结果
+        Object value = null;
+        try {
+            Statement statement = calciteConnection.createStatement();
+            ResultSet resultSet = statement.executeQuery(sql);
+            if (resultSet.next()){
+                value = resultSet.getObject(1);
+            }
+        } catch (SQLException e) {
+            e.printStackTrace();
+            throw new RuntimeException(e);
+        }
+        return value;
+    }
 }

+ 2 - 1
src/main/java/com/sundata/internalevaluation/configuration/controller/SysReqRulesController.java

@@ -78,7 +78,8 @@ public class SysReqRulesController extends BaseAction {
 
     @RequestMapping("testRules")
     public Object testRules(@RequestBody SysReqRules sysReqRules) {
-        return CalcUtil.calcRules("rules"+System.currentTimeMillis(), sysReqRules.getRulesNo(), Map.of("name","zhangsan","custNo","1"));
+        return CalcUtil.calcRules("rules"+System.currentTimeMillis(), sysReqRules.getRulesNo(),
+                Map.of("name","zhangsan","custNo","1"));
     }
     
 }