|
@@ -1,13 +1,13 @@
|
|
|
package com.sundata.internalevaluation.calc.calcUnit;
|
|
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
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.exception.BusinessException;
|
|
|
import com.sundata.common.util.JsonUtil;
|
|
|
import com.sundata.internalevaluation.calc.calcUnit.interfaces.InterfaceRunning;
|
|
|
import com.sundata.internalevaluation.calc.model.CalcException;
|
|
@@ -15,17 +15,15 @@ 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.SysInterface;
|
|
|
-import com.sundata.internalevaluation.configuration.model.SysInterfaceParam;
|
|
|
+import com.sundata.internalevaluation.calc.util.JdbcUtils;
|
|
|
+import com.sundata.internalevaluation.configuration.model.Interface;
|
|
|
+import com.sundata.internalevaluation.configuration.model.InterfaceParam;
|
|
|
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;
|
|
@@ -42,7 +40,7 @@ public class InterfaceCalcUnit extends CalcUnit {
|
|
|
private static final Logger log = LoggerFactory.getLogger(InterfaceCalcUnit.class);
|
|
|
|
|
|
|
|
|
- private final SysInterface sysInterface;
|
|
|
+ private final Interface interfaceModel;
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -53,9 +51,9 @@ public class InterfaceCalcUnit extends CalcUnit {
|
|
|
* @param calcType 计算类型
|
|
|
* @param initContext 计算单元初始化参数
|
|
|
*/
|
|
|
- public InterfaceCalcUnit(String calcCode, String calcName, CalcType calcType, Map<String, Object> initContext, SysInterface sysInterface) {
|
|
|
+ public InterfaceCalcUnit(String calcCode, String calcName, CalcType calcType, Map<String, Object> initContext, Interface interfaceModel) {
|
|
|
super(calcCode, calcName, calcType, initContext);
|
|
|
- this.sysInterface = sysInterface;
|
|
|
+ this.interfaceModel = interfaceModel;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -87,12 +85,6 @@ public class InterfaceCalcUnit extends CalcUnit {
|
|
|
*/
|
|
|
@Override
|
|
|
public List<CalcUnit> getSourceCalcUnits() {
|
|
|
-// 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());
|
|
|
-// return childCalcUnitList;
|
|
|
-
|
|
|
return List.of();
|
|
|
}
|
|
|
|
|
@@ -134,22 +126,22 @@ public class InterfaceCalcUnit extends CalcUnit {
|
|
|
// 获取新流水号,每次请求接口必须不一样
|
|
|
String globalReqNo = AdminUtil.getNextId("cretSeqno");
|
|
|
context.put("globalReqNo",globalReqNo);
|
|
|
- // creditSeqNo 查询正i性能的流水号 如果不存在就重新赋值 只有征信的请求才会用到
|
|
|
+ // creditSeqNo 查询征信的流水号 如果不存在就重新赋值 只有征信的请求才会用到
|
|
|
context.putIfAbsent("creditSeqNo",globalReqNo);
|
|
|
|
|
|
// 接口内容类型
|
|
|
- String reqContentTypeCode = this.sysInterface.getReqContentTypeCode();
|
|
|
+ String reqContentTypeCode = this.interfaceModel.getReqContentTypeCode();
|
|
|
// 接口请求类型
|
|
|
- String interfaceTypeCode = this.sysInterface.getInterfaceTypeCode();
|
|
|
+ String interfaceTypeCode = this.interfaceModel.getInterfaceTypeCode();
|
|
|
|
|
|
// 请求类型不为 http或者 https 或者 请求体不为 json
|
|
|
if (!interfaceTypeCode.startsWith("http") || !"json".equals(reqContentTypeCode)) {
|
|
|
// TODO Socket 请求
|
|
|
- throw new CalcException("目前仅支持http和https的请求和json请求体");
|
|
|
+ throw new BusinessException("目前仅支持http和https的请求和json请求体");
|
|
|
}
|
|
|
|
|
|
// 请求参数实体列表
|
|
|
- List<SysInterfaceParam> paramList = sysInterface.getParamList();
|
|
|
+ List<InterfaceParam> paramList = interfaceModel.getParamList();
|
|
|
Map<String, String> headersMap = new HashMap<>();
|
|
|
Map<String, Object> paramMap = new HashMap<>();
|
|
|
|
|
@@ -171,10 +163,11 @@ public class InterfaceCalcUnit extends CalcUnit {
|
|
|
|
|
|
// http请求或者 https请求
|
|
|
if (interfaceTypeCode.startsWith("http")) {
|
|
|
+ // 拼接url 其中可能存在变量
|
|
|
String url = interfaceTypeCode + "://" +
|
|
|
- sysInterface.getInterfaceIp() + ":" +
|
|
|
- sysInterface.getInterfacePort() +
|
|
|
- sysInterface.getInterfaceUrl();
|
|
|
+ interfaceModel.getInterfaceIp() + ":" +
|
|
|
+ interfaceModel.getInterfacePort() +
|
|
|
+ interfaceModel.getInterfaceUrl();
|
|
|
|
|
|
Map<String,Object> allMap = new HashMap<>(headersMap);
|
|
|
allMap.putAll(paramMap);
|
|
@@ -183,83 +176,105 @@ public class InterfaceCalcUnit extends CalcUnit {
|
|
|
url = sub.replace(url);
|
|
|
|
|
|
// 请求体模板和完整请求体
|
|
|
- String reqBodyTemplate = sysInterface.getReqBodyTemplate();
|
|
|
+ String reqBodyTemplate = interfaceModel.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);
|
|
|
- }
|
|
|
+ // 声明 接口参数处理的对象
|
|
|
+ InterfaceRunning interfaceRunning;
|
|
|
|
|
|
- // 设置请求头和请求体
|
|
|
- httpRequest.addHeaders(headersMap);
|
|
|
- if (jsonString != null && !jsonString.trim().isEmpty()) {
|
|
|
- httpRequest.body(jsonString);
|
|
|
+ // 加密逻辑
|
|
|
+ String encodeLogic = interfaceModel.getReqEncryptionLogic();
|
|
|
+ if (StrUtil.isNotBlank(encodeLogic)) {
|
|
|
+ interfaceRunning = SpringUtil.getBean(encodeLogic);
|
|
|
+ if (interfaceRunning != null) {
|
|
|
+ jsonString = interfaceRunning.encode(jsonString);
|
|
|
}
|
|
|
+ }
|
|
|
+
|
|
|
+ // 声明 HttpRequest 对象
|
|
|
+ HttpRequest httpRequest;
|
|
|
+
|
|
|
+ // 拿到响应体
|
|
|
+ String responseStr;
|
|
|
+ String interfaceNo = interfaceModel.getInterfaceNo();
|
|
|
+ String reqMethod = interfaceModel.getReqMethod();
|
|
|
+ if ("get".equals(reqMethod)) {
|
|
|
+ httpRequest = HttpUtil.createGet(url);
|
|
|
+ } else {
|
|
|
+ httpRequest = HttpUtil.createPost(url);
|
|
|
+ }
|
|
|
|
|
|
- HttpResponse httpResponse = httpRequest.execute();
|
|
|
+ // 设置请求头和请求体
|
|
|
+ httpRequest.addHeaders(headersMap);
|
|
|
+ if (jsonString != null && !jsonString.trim().isEmpty()) {
|
|
|
+ httpRequest.body(jsonString);
|
|
|
+ }
|
|
|
+ log.debug("接口-[{}({})]-请求地址-[{}]-请求body报文:{}",getCalcName(),getCalcCode(),url,jsonString);
|
|
|
+ try (HttpResponse httpResponse = httpRequest.execute()) {
|
|
|
responseStr = httpResponse.body();
|
|
|
+ log.debug("接口-[{}({})]-请求地址-[{}]-响应body报文:{}",getCalcName(),getCalcCode(),url,responseStr);
|
|
|
+ String respParamTypeCode = interfaceModel.getRespParamTypeCode();
|
|
|
+ if ("json".equals(respParamTypeCode)){
|
|
|
+ responseStr = JSONUtil.toJsonPrettyStr(responseStr);
|
|
|
+ }
|
|
|
+ // TODO 可继续增加其他类型
|
|
|
+ else
|
|
|
+ {
|
|
|
+ throw new RuntimeException("接口-目前只支持 json 返回报文");
|
|
|
+ }
|
|
|
|
|
|
- InterfaceRunning interfaceRunning = null;
|
|
|
+ }catch (Exception e){
|
|
|
+ String errMsg = StrUtil.format("接口-[{}({})]-请求地址-[{}] - 请求出现异常:{}", getCalcName(), getCalcCode(), url, e.getMessage());
|
|
|
+ log.error(errMsg);
|
|
|
+ throw new BusinessException(errMsg, e);
|
|
|
+ }
|
|
|
|
|
|
- String decryptionLogic = sysInterface.getDecryptionLogic();
|
|
|
|
|
|
- // 解密逻辑
|
|
|
- if (null != decryptionLogic && !decryptionLogic.trim().isEmpty()) {
|
|
|
- interfaceRunning = SpringUtil.getBean(decryptionLogic);
|
|
|
- if (interfaceRunning != null) {
|
|
|
- responseStr = interfaceRunning.decode(responseStr);
|
|
|
- }
|
|
|
+ // 解密逻辑
|
|
|
+ String decryptionLogic = interfaceModel.getDecryptionLogic();
|
|
|
+ if (null != decryptionLogic && !decryptionLogic.trim().isEmpty()) {
|
|
|
+ interfaceRunning = SpringUtil.getBean(decryptionLogic);
|
|
|
+ if (interfaceRunning != null) {
|
|
|
+ responseStr = interfaceRunning.decode(responseStr);
|
|
|
+ log.debug("接口-[{}({})]-请求地址-[{}]-解密后数据:{}",getCalcName(),getCalcCode(),url,responseStr);
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- // 解密后处理逻辑
|
|
|
- String processLogic = sysInterface.getProcessLogic();
|
|
|
- if (processLogic != null && !processLogic.trim().isEmpty()) {
|
|
|
- interfaceRunning = SpringUtil.getBean(processLogic);
|
|
|
- if (interfaceRunning != null) {
|
|
|
- responseStr = interfaceRunning.afterDecode(interfaceNo,responseStr,calculateInstanceNumber);
|
|
|
- }
|
|
|
+ // 解密后处理逻辑
|
|
|
+ String processLogic = interfaceModel.getProcessLogic();
|
|
|
+ if (processLogic != null && !processLogic.trim().isEmpty()) {
|
|
|
+ interfaceRunning = SpringUtil.getBean(processLogic);
|
|
|
+ if (interfaceRunning != null) {
|
|
|
+ responseStr = interfaceRunning.afterDecode(interfaceNo, responseStr, calculateInstanceNumber);
|
|
|
+ log.debug("接口-[{}({})]-请求地址-[{}]-解析后数据:{}",getCalcName(),getCalcCode(),url,responseStr);
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- thisResult.put(getCalcCode(),responseStr);
|
|
|
+ /*
|
|
|
+ responseStr 一定是如下结果,否则后期计算会报错
|
|
|
+ {
|
|
|
+ "propA":[
|
|
|
+ ]
|
|
|
+ }
|
|
|
|
|
|
|
|
|
- }
|
|
|
|
|
|
-// 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);
|
|
|
-// }
|
|
|
+ */
|
|
|
+ // 保存结果
|
|
|
+ thisResult.put(getCalcCode(), responseStr);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- private Object getParamValue(SysInterfaceParam sysInterfaceParam, Map<String, Object> context) {
|
|
|
+ private Object getParamValue(InterfaceParam sysInterfaceParam, Map<String, Object> context) {
|
|
|
// 参数获取类型
|
|
|
String paramRetrievalTypeCode = sysInterfaceParam.getParamRetrievalTypeCode();
|
|
|
|