Forráskód Böngészése

fix: 增加查询数据-接口

liuc 7 hónapja
szülő
commit
830862c759

+ 25 - 0
bound-link-api/sa-flow-api/src/main/java/com/cloud/sa/api/SettingEngineController.java

@@ -1,7 +1,11 @@
 package com.cloud.sa.api;
 
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.cloud.sa.flow.commom.aspect.EventProgram;
 import com.cloud.sa.flow.commom.common.model.StResult;
 import com.cloud.sa.flow.commom.common.system.StUserInfo;
+import com.cloud.sa.flow.commom.common.utils.StConvert;
 import com.cloud.sa.flow.commom.common.utils.StString;
 import com.cloud.sa.flow.commom.domain.entity.*;
 import com.cloud.sa.flow.commom.domain.virentity.TreeNode;
@@ -14,6 +18,7 @@ import com.cloud.sa.flow.commom.domain.entity.setting.StEventMain;
 import com.cloud.sa.flow.commom.domain.entity.setting.StQueryMain;
 import com.cloud.sa.flow.commom.service.setting.StTaskAgentService;
 import com.cloud.sa.flow.engine.service.StFlowService;
+import com.wx.blink.base.common.annoation.NoNeedLogin;
 import com.wx.blink.base.common.domain.ResponseDTO;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
@@ -308,4 +313,24 @@ public class SettingEngineController {
     public StResult updatePrint(@RequestBody StPrint print) {
         return settingService.updatePrint(print.getFlowCode(), print.getContent());
     }
+    @NoNeedLogin
+    @EventProgram
+    @GetMapping("/queryTest")
+    public JSONArray queryTest(String tt) {
+        if(StString.isNotEmpty(tt)){
+            return null;
+        }
+        JSONArray jsonArray = new JSONArray();
+        for (int i = 0; i < 10; i++) {
+            JSONObject jsonObject = new JSONObject();
+            jsonArray.add(jsonObject);
+            //所有值必须是字符串,否则前端无法获取
+            jsonObject.put("code", StConvert.toStr(i));
+            jsonObject.put("name", i + "name");
+            jsonObject.put("address",  LocalDateTime.now().toString());
+            jsonObject.put("address3", i + "address");
+            jsonObject.put("address2", null);
+        }
+        return jsonArray;
+    }
 }

+ 1 - 1
bound-link-api/sa-flow-api/src/main/java/com/cloud/sa/service/FlowUniversalService.java

@@ -11,7 +11,7 @@ import com.cloud.sa.flow.commom.domain.virentity.ParamAdJustActivate;
 import com.cloud.sa.flow.engine.service.StFlowService;
 import com.cloud.sa.flow.engine.service.StOperationService;
 import com.cloud.sa.flow.engine.service.form.StFormService;
-import com.cloud.sa.flow.engine.subs.StHttpRequest;
+import com.cloud.sa.flow.commom.common.Request.StHttpRequest;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;

BIN
bound-link-api/sa-flow-api/src/main/resources/lib/sa-flow-engine-3.0.0.jar


+ 72 - 0
bound-link-api/sa-flow-common/src/main/java/com/cloud/sa/flow/commom/common/Request/StHttpRequest.java

@@ -0,0 +1,72 @@
+package com.cloud.sa.flow.commom.common.Request;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
+import org.springframework.stereotype.Component;
+
+import java.io.IOException;
+
+/**
+ * @author liuchao
+ * @date 2024/6/20 19:16
+ */
+public class StHttpRequest {
+    public static String postHttpClient(String path, String json) throws Exception {
+        CloseableHttpClient httpClient = HttpClients.createDefault();
+        String uri = path;
+        try {
+            HttpPost httpPost = new HttpPost(uri); // 替换为你的目标URL
+            // 设置POST请求的参数(如果需要)
+            StringEntity entity = new StringEntity(json, "UTF-8");
+            entity.setContentType("application/json"); // 设置请求的内容类型
+            httpPost.setEntity(entity);
+            // 添加请求头
+            httpPost.addHeader("eventId", "3988bfd0-67f0-11ef-9ea9-71058a057db9"); // 添加eventId请求头
+            // 执行请求并获取响应
+            CloseableHttpResponse response = httpClient.execute(httpPost);
+            try {
+                // 处理响应
+                HttpEntity responseEntity = response.getEntity();
+                if (responseEntity != null) {
+                    String responseString = EntityUtils.toString(responseEntity, "UTF-8");
+                    return responseString;
+                }
+            } finally {
+                response.close();
+            }
+        } finally {
+            httpClient.close();
+        }
+        return null;
+    }
+    public static String getHttpClient(String path) throws Exception {
+        CloseableHttpClient httpClient = HttpClients.createDefault();
+        String uri = path;
+        try {
+            HttpGet httpGet = new HttpGet(uri); // 替换为你的目标URL
+            // 添加请求头
+            httpGet.addHeader("eventId", "3988bfd0-67f0-11ef-9ea9-71058a057db9"); // 添加eventId请求头
+            // 执行请求并获取响应
+            CloseableHttpResponse response = httpClient.execute(httpGet);
+            try {
+                // 处理响应
+                HttpEntity responseEntity = response.getEntity();
+                if (responseEntity != null) {
+                    String responseString = EntityUtils.toString(responseEntity, "UTF-8");
+                    return responseString;
+                }
+            } finally {
+                response.close();
+            }
+        } finally {
+            httpClient.close();
+        }
+        return null;
+    }
+}

+ 9 - 0
bound-link-api/sa-flow-common/src/main/java/com/cloud/sa/flow/commom/domain/entity/setting/StQueryMain.java

@@ -15,6 +15,7 @@ public class StQueryMain extends StBaseEntity {
     public String queryName;
     public String queryCode;
     public String querySql;
+    public String queryType;
     public String queryColumns;
     public List<QueryColumn> queryColumnList;
     public void initList() {
@@ -62,4 +63,12 @@ public class StQueryMain extends StBaseEntity {
     public void setQueryColumns(String queryColumns) {
         this.queryColumns = queryColumns;
     }
+
+    public String getQueryType() {
+        return queryType;
+    }
+
+    public void setQueryType(String queryType) {
+        this.queryType = queryType;
+    }
 }

+ 98 - 31
bound-link-api/sa-flow-common/src/main/java/com/cloud/sa/flow/commom/service/setting/StSettingService.java

@@ -1,10 +1,12 @@
 package com.cloud.sa.flow.commom.service.setting;
 
+import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.cloud.sa.flow.commom.common.Request.StHttpRequest;
 import com.cloud.sa.flow.commom.common.model.StResult;
 import com.cloud.sa.flow.commom.common.redis.StRedisCache;
 import com.cloud.sa.flow.commom.common.system.StUserInfo;
@@ -24,9 +26,11 @@ import com.cloud.sa.flow.commom.domain.entity.setting.StSequence;
 import com.cloud.sa.flow.commom.domain.mapper.*;
 import com.cloud.sa.flow.commom.domain.virentity.StQueryMainDTO;
 import com.cloud.sa.flow.commom.service.genSql.SqlGeneratorService;
+import com.sun.org.apache.bcel.internal.generic.NEW;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.exception.ExceptionUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -34,8 +38,10 @@ import java.io.PrintWriter;
 import java.io.StringWriter;
 import java.time.LocalDateTime;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.stream.Collectors;
 
 /**
  * @author lc
@@ -65,6 +71,8 @@ public class StSettingService {
     StPrintMapper printMapper;
     @Autowired
     StPrintHistoryMapper printHistoryMapper;
+    @Value("${server.port}")
+    private String port;
 
 
     //region 数据查询
@@ -99,43 +107,102 @@ public class StSettingService {
     }
 
     private List<Map<String, String>> getQuerySData(StQueryMain singleQueryMain, String query, String queryMapField) {
-        String sql = sqlGenerator.getQueryConcatSql(singleQueryMain.getQuerySql().replaceAll("\\s+$", "").replaceAll(";+$", ""), query);
-        //将所有连续的空白字符替换为一个空格
-        sql = sql.replaceAll("\\s{2,}", " ");
-        if (StString.isNotEmpty(queryMapField)) {
-            JSONArray jsonArray = JSONArray.parseArray(queryMapField);
-            for (Object item : jsonArray) {
-                JSONObject jsonObject = (JSONObject) item;
-                String key = jsonObject.getString("key");
-                String value = jsonObject.getString("value");
-                if (StGlobalConstant.CurrentLoginUser.equals(value)) {
-                    //(1)当前登录人
-                    value = StUserInfo.getUserCode();
+        if ("1".equals(singleQueryMain.getQueryType())) {
+            try {
+                String url = "http://localhost:" + port + singleQueryMain.getQuerySql();
+                if (StString.isNotEmpty(queryMapField)) {
+                    JSONArray jsonArray = JSONArray.parseArray(queryMapField);
+                    for (Object item : jsonArray) {
+                        JSONObject jsonObject = (JSONObject) item;
+                        if (jsonObject.containsKey("key") && jsonObject.containsKey("value")) {
+                            String key = jsonObject.getString("key");
+                            String value = jsonObject.getString("value");
+                            if (StString.isNotEmpty(key) && StString.isNotEmpty(value)) {
+                                if (StGlobalConstant.CurrentLoginUser.equals(value)) {
+                                    //(1)当前登录人
+                                    value = StUserInfo.getUserCode();
+                                }
+                                if (url.contains("?")) {
+                                    url += "&" + key + "=" + value;
+                                } else {
+                                    url += "?" + key + "=" + value;
+                                }
+                            }
+                        }
+                    }
                 }
-                if (value == null) {
-                    //value可能非必填,需要进行设置
-                    String fieldCode = key.replace("$", "").replace("{", "").replace("}", "");
-                    if (sql.contains("'%" + key + "%'") || sql.contains("'" + key + "%'") || sql.contains("'%" + key + "'")) {
-                        //like的情况
-                        value = "";
-                        sql = sql.replace(key, value);
-                    } else {
-                        if (sql.contains("'" + key + "'")) {
-                            key = "'" + key + "'";
+                String httpClient = StHttpRequest.getHttpClient(url);
+                List<Map<String, String>> resultList= new ArrayList<>();
+                if(httpClient!=null  ){
+                    JSONArray jsonArray = JSONArray.parseArray(httpClient);
+                    resultList = jsonArray.stream()
+                            .map(obj -> {
+                                JSONObject jsonObject = (JSONObject) obj;
+                                Map<String, String> map = new HashMap<>();
+                                jsonObject.forEach((key, value) -> {
+                                    if (value instanceof String) {
+                                        map.put(key, StConvert.toStr(value));
+                                    }
+                                });
+                                return map;
+                            })
+                            .collect(Collectors.toList());
+                    if (query != null && query.length() > 5) {
+                        JSONArray queryArray = JSONArray.parseArray(query);
+                        for (int i = 0; i < queryArray.size(); i++) {
+                            JSONObject queryObj = (JSONObject) queryArray.get(i);
+                            if (StString.isNotEmpty(queryObj.getString("value"))) {
+                                resultList = resultList.stream().filter(f ->
+                                                f.get(queryObj.getString("field")) != null &&
+                                                        f.get(queryObj.getString("field")).contains(queryObj.getString("value")))
+                                        .collect(Collectors.toList());
+                            }
+                        }
+                    }
+                }
+                return resultList;
+            } catch (Exception e) {
+                throw new RuntimeException(e);
+            }
+        } else {
+            String sql = sqlGenerator.getQueryConcatSql(singleQueryMain.getQuerySql().replaceAll("\\s+$", "").replaceAll(";+$", ""), query);
+            //将所有连续的空白字符替换为一个空格
+            sql = sql.replaceAll("\\s{2,}", " ");
+            if (StString.isNotEmpty(queryMapField)) {
+                JSONArray jsonArray = JSONArray.parseArray(queryMapField);
+                for (Object item : jsonArray) {
+                    JSONObject jsonObject = (JSONObject) item;
+                    String key = jsonObject.getString("key");
+                    String value = jsonObject.getString("value");
+                    if (StGlobalConstant.CurrentLoginUser.equals(value)) {
+                        //(1)当前登录人
+                        value = StUserInfo.getUserCode();
+                    }
+                    if (value == null) {
+                        //value可能非必填,需要进行设置
+                        String fieldCode = key.replace("$", "").replace("{", "").replace("}", "");
+                        if (sql.contains("'%" + key + "%'") || sql.contains("'" + key + "%'") || sql.contains("'%" + key + "'")) {
+                            //like的情况
+                            value = "";
+                            sql = sql.replace(key, value);
+                        } else {
+                            if (sql.contains("'" + key + "'")) {
+                                key = "'" + key + "'";
+                            }
+                            sql = sql.replace(key, fieldCode);
+                            sql = sql.replace(fieldCode + "=" + fieldCode, "1=1")
+                                    .replace(fieldCode + " = " + fieldCode, "1=1")
+                                    .replace(fieldCode + " =" + fieldCode, "1=1")
+                                    .replace(fieldCode + "= " + fieldCode, "1=1");
                         }
-                        sql = sql.replace(key, fieldCode);
-                        sql = sql.replace(fieldCode + "=" + fieldCode, "1=1")
-                                .replace(fieldCode + " = " + fieldCode, "1=1")
-                                .replace(fieldCode + " =" + fieldCode, "1=1")
-                                .replace(fieldCode + "= " + fieldCode, "1=1");
+                    } else {
+                        //有值,直接替换
+                        sql = sql.replace(key, value);
                     }
-                } else {
-                    //有值,直接替换
-                    sql = sql.replace(key, value);
                 }
             }
+            return commonMapper.getQueryDataList(sql);
         }
-        return commonMapper.getQueryDataList(sql);
     }
 
     public void updateStQueryMain(StQueryMain queryMain) {

+ 6 - 2
bound-link-api/sa-flow-common/src/main/resources/mapper/StSettingMapper.xml

@@ -4,13 +4,14 @@
 
 
     <insert id="istQueryMain">
-        INSERT INTO st_query_main(id,query_name,query_code,query_sql,query_columns,create_by,create_time)
-        VALUES (#{id},#{queryName},#{queryCode},#{querySql},#{queryColumns},#{createBy},NOW())
+        INSERT INTO st_query_main(id,query_name,query_code,query_type,query_sql,query_columns,create_by,create_time)
+        VALUES (#{id},#{queryName},#{queryCode},#{queryType},#{querySql},#{queryColumns},#{createBy},NOW())
     </insert>
     <update id="updateStQueryMain">
         UPDATE st_query_main
         SET query_name  = #{queryName},
             query_code  = #{queryCode},
+            query_type   = #{queryType},
             query_sql   = #{querySql},
             query_columns   = #{queryColumns},
             update_by   = #{updateBy},
@@ -21,6 +22,7 @@
         SELECT id,
         query_name queryName,
         query_code queryCode,
+        query_type queryType,
         query_sql querySql,
         query_columns queryColumns,
         create_by createBy,
@@ -59,6 +61,7 @@
         SELECT id,
                query_name    queryName,
                query_code    queryCode,
+               query_type    queryType,
                query_sql     querySql,
                query_columns queryColumns,
                create_by     createBy,
@@ -72,6 +75,7 @@
         SELECT id,
                query_name    queryName,
                query_code    queryCode,
+               query_type    queryType,
                query_sql     querySql,
                query_columns queryColumns,
                create_by     createBy,