|
@@ -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);
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
}
|