|
@@ -3,20 +3,22 @@ package com.sundata.internalevaluation.calc.calcUnit;
|
|
|
import cn.hutool.http.HttpRequest;
|
|
|
import cn.hutool.http.HttpResponse;
|
|
|
import cn.hutool.http.HttpUtil;
|
|
|
+import com.sundata.common.util.DBExecutor;
|
|
|
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.InvokeUtil;
|
|
|
import com.sundata.internalevaluation.configuration.model.SysInterface;
|
|
|
import com.sundata.internalevaluation.configuration.model.SysInterfaceParam;
|
|
|
+import com.sundata.internalevaluation.script.TemplateUtil;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
-import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* Created by IntelliJ IDEA.
|
|
@@ -75,11 +77,13 @@ 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;
|
|
|
+// 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();
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -116,7 +120,7 @@ public class InterfaceCalcUnit extends CalcUnit {
|
|
|
Map<String, Object> context,
|
|
|
Map<CalcUnit, CalcResult<String, Object>> sourceResults) {
|
|
|
|
|
|
- log.debug("计算{}:编号:{},名称:{},流水号:{}", getCalcType().getName(),getCalcCode(),getCalcName(),calculateInstanceNumber);
|
|
|
+ log.debug("计算{}:编号:{},名称:{},流水号:{}", getCalcType().getName(), getCalcCode(), getCalcName(), calculateInstanceNumber);
|
|
|
|
|
|
|
|
|
// 接口内容类型
|
|
@@ -124,37 +128,130 @@ public class InterfaceCalcUnit extends CalcUnit {
|
|
|
// 接口请求类型
|
|
|
String interfaceTypeCode = this.sysInterface.getInterfaceTypeCode();
|
|
|
|
|
|
+ // 请求类型不为 http或者 https 或者 请求体不为 json
|
|
|
+ if (!interfaceTypeCode.startsWith("http") || !"json".equals(reqContentTypeCode)) {
|
|
|
+ // TODO Socket 请求
|
|
|
+ throw new CalcException("目前仅支持http和https的请求和json请求体");
|
|
|
+ }
|
|
|
|
|
|
- // http请求或者 https请求
|
|
|
- if (interfaceTypeCode.startsWith("http") && "json".equals(reqContentTypeCode)) {
|
|
|
- Map<String, String> headersMap = new HashMap<>();
|
|
|
- Map<String, Object> paramMap = new HashMap<>();
|
|
|
-
|
|
|
- sourceResults.forEach((key, value) -> {
|
|
|
- String calcCode = key.getCalcCode();
|
|
|
- Object res = value.get(calcCode);
|
|
|
- SysInterfaceParam entity = (SysInterfaceParam) value.get("entity");
|
|
|
-
|
|
|
- // 判断是不是请求头
|
|
|
- if ("true".equals(entity.getIsHeader())) {
|
|
|
- headersMap.put(entity.getParamObjName(), res.toString());
|
|
|
- } else {
|
|
|
- paramMap.put(entity.getParamObjName(), res);
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 计算参数
|
|
|
+ */
|
|
|
+ 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);
|
|
|
+ String isHeader = param.getIsHeader();
|
|
|
+ if ("true".equals(isHeader)) {
|
|
|
+ headersMap.put(param.getParamObjName(), JsonUtil.toJSONString(result));
|
|
|
+ } else {
|
|
|
+ paramMap.put(param.getParamObjName(), result);
|
|
|
+ }
|
|
|
}
|
|
|
- });
|
|
|
+ );
|
|
|
+
|
|
|
+
|
|
|
+ // http请求或者 https请求
|
|
|
+ if (interfaceTypeCode.startsWith("http")) {
|
|
|
String url = interfaceTypeCode + "://" + sysInterface.getInterfaceIp() + ":" + sysInterface.getInterfacePort() + sysInterface.getInterfaceUrl();
|
|
|
HttpRequest postRequest = HttpUtil.createPost(url);
|
|
|
postRequest.addHeaders(headersMap);
|
|
|
- postRequest.body(JsonUtil.toJSONString(paramMap));
|
|
|
- try (HttpResponse httpResponse = postRequest.execute()){
|
|
|
+ String jsonString = JsonUtil.toJSONString(paramMap);
|
|
|
+ log.warn("请求参数json:{}", jsonString);
|
|
|
+ postRequest.body(jsonString);
|
|
|
+ try (HttpResponse httpResponse = postRequest.execute()) {
|
|
|
String responseStr = httpResponse.body();
|
|
|
-// thisResult.put(getCalcCode(), JsonUtil.jsonToMap(responseStr));
|
|
|
- thisResult.put(getCalcCode(), (responseStr));
|
|
|
+ log.debug("请求接口【{}】的结果是:{}", url, responseStr);
|
|
|
+ thisResult.put(getCalcCode(), responseStr);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private Object getResultByInterfaceParam(SysInterfaceParam sysInterfaceParam, Map<String, Object> context) {
|
|
|
+ // 参数获取类型
|
|
|
+ String paramRetrievalTypeCode = sysInterfaceParam.getParamRetrievalTypeCode();
|
|
|
+
|
|
|
+ // 参数获取内容
|
|
|
+ String paramRetrievalTypeCont = sysInterfaceParam.getParamRetrievalTypeCont();
|
|
|
+
|
|
|
+
|
|
|
+ Object result = null;
|
|
|
+
|
|
|
+ // 获取参数值
|
|
|
+ switch (paramRetrievalTypeCode) {
|
|
|
+ // 固定值
|
|
|
+ case "fixed":
|
|
|
+ result = paramRetrievalTypeCont;
|
|
|
+ break;
|
|
|
+ // java
|
|
|
+ // 获取参数内容
|
|
|
+ case "java":
|
|
|
+ try {
|
|
|
+ result = InvokeUtil.invokeMethod(paramRetrievalTypeCont);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ // SQL-Map
|
|
|
+ case "sqlmap":
|
|
|
+ String execute = TemplateUtil.execute("interfaceParam-temp-sqlmap-" + getCalcCode(), paramRetrievalTypeCont, context);
|
|
|
+ log.warn("sql语句为:{}",execute);
|
|
|
+ result = DBExecutor.doQuery(execute);
|
|
|
+ break;
|
|
|
+ // SQL-List
|
|
|
+ case "sqllist":
|
|
|
+ String execute1 = TemplateUtil.execute("interfaceParam-temp-sqllist-" + getCalcCode(), paramRetrievalTypeCont, context);
|
|
|
+ log.warn("sql语句为:{}",execute1);
|
|
|
+ result = DBExecutor.doQueryMapList(execute1);
|
|
|
+ break;
|
|
|
+ // SQL-String
|
|
|
+ case "sqlstring":
|
|
|
+ String execute2 = TemplateUtil.execute("interfaceParam-temp-sqlstring-" + getCalcCode(), paramRetrievalTypeCont, context);
|
|
|
+ log.warn("sql语句为:{}",execute2);
|
|
|
+ result = DBExecutor.doQuery(execute2);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ String paramTypeCode = sysInterfaceParam.getParamTypeCode();
|
|
|
+ if ("string".equals(paramTypeCode)) {
|
|
|
+ if (result instanceof String) {
|
|
|
+
|
|
|
+ } else {
|
|
|
+ result = JsonUtil.toJSONString(result);
|
|
|
+ }
|
|
|
+ } else if ("number".equals(paramTypeCode)) {
|
|
|
+ if (result instanceof Number) {
|
|
|
+ if (result instanceof Integer) {
|
|
|
+ result = ((Number) result).intValue();
|
|
|
+ } else if (result instanceof Long) {
|
|
|
+ result = ((Number) result).longValue();
|
|
|
+ } else if (result instanceof Float) {
|
|
|
+ result = ((Number) result).floatValue();
|
|
|
+ } else if (result instanceof Double) {
|
|
|
+ result = ((Number) result).doubleValue();
|
|
|
+ } else if (result instanceof Short) {
|
|
|
+ result = ((Number) result).shortValue();
|
|
|
+ } else if (result instanceof Byte) {
|
|
|
+ result = ((Number) result).byteValue();
|
|
|
+ } else {
|
|
|
+ throw new RuntimeException("数据类型不匹配:计算机过不为'数字'");
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+ throw new RuntimeException("数据类型不匹配:计算机过不为'数字'");
|
|
|
}
|
|
|
} else {
|
|
|
- // TODO Socket 请求
|
|
|
- throw new CalcException("目前仅支持http和https的请求");
|
|
|
-// thisResult.put(getCalcCode(), "非http、https请求,并且参数不是json格式");
|
|
|
+ throw new CalcException("返回值类型有无,目前仅有 数字和字符串");
|
|
|
}
|
|
|
+ return result;
|
|
|
}
|
|
|
}
|