|
@@ -2,75 +2,71 @@ package org.bfkj.application;
|
|
|
|
|
|
|
|
|
import org.bfkj.config.AppConfig;
|
|
|
-import org.bfkj.config.ObjectMap;
|
|
|
import org.bfkj.utils.LogUtils;
|
|
|
import org.bfkj.utils.MapTools;
|
|
|
import org.bfkj.utils.MyDbHelper;
|
|
|
import org.bfkj.utils.ScriptEnginePro;
|
|
|
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|
|
|
|
|
import java.util.*;
|
|
|
+import java.util.concurrent.ThreadPoolExecutor;
|
|
|
|
|
|
-
|
|
|
-
|
|
|
- * z
|
|
|
- * 数据收集
|
|
|
- */
|
|
|
-public class DataProcess {
|
|
|
-
|
|
|
-
|
|
|
+public class DataProcess {
|
|
|
private String serviceId;
|
|
|
private long lastActive;
|
|
|
private String errorMessage = null;
|
|
|
- private List<Map<String, Object>> calculationLibraryList;
|
|
|
- private final Map<String, Integer> serviceErrorCount = new HashMap<>();
|
|
|
+ private MyDbHelper baseDbHelper = null;
|
|
|
|
|
|
- private final Map<String, ScriptEnginePro> ScriptEngineProMaps = new HashMap<>();
|
|
|
+ private Map<String, Integer> serviceErrorCount = new HashMap<>();
|
|
|
|
|
|
- private final List<String> baseInfo = new ArrayList<>() {{
|
|
|
- add("serviceId");
|
|
|
- add("event");
|
|
|
- add("page");
|
|
|
- add("pageSize");
|
|
|
- add("dataObjectId");
|
|
|
- }};
|
|
|
+ private List<Map<String, Object>> calcList;
|
|
|
+ private Map<String, ScriptEnginePro> ScriptEngineProMaps = new HashMap<>();
|
|
|
+ private Map<String, MyDbHelper> calcDbHelperMaps = new HashMap<>();
|
|
|
|
|
|
+ private List<String> serviceAuthMap = new ArrayList<>();
|
|
|
|
|
|
public DataProcess(String service_Id) {
|
|
|
lastActive = System.currentTimeMillis();
|
|
|
serviceId = service_Id;
|
|
|
- MyDbHelper myDbHelper = ObjectMap.getordropMyDbHelper(AppConfig.getSystemParams(AppConfig.REMOTE_DB_CONNECT));
|
|
|
- if (Objects.nonNull(myDbHelper.getErrorMessage())) {
|
|
|
- errorMessage = "获取myDbHelper对象异常: " + myDbHelper.getErrorMessage();
|
|
|
- return;
|
|
|
- }
|
|
|
- Map<String, Object> serviceInfoResult = myDbHelper.queryByParamsReturnList("select * from serviceinfo where serviceID = ?", service_Id);
|
|
|
- List<Map<String, Object>> mapList = MapTools.getMapList(serviceInfoResult);
|
|
|
- if (!serviceInfoResult.get("code").equals("0") || Objects.isNull(mapList) || mapList.isEmpty()) {
|
|
|
- errorMessage = !serviceInfoResult.get("code").equals("0") ? ("查询" + serviceId + "服务配置失败:" + serviceInfoResult.get("message")) : ("未配置服务:" + serviceId);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- String calculationListSql = "SELECT CL.*,DI.* FROM calculation_library CL left JOIN datasourceinfo DI ON DI.dataSourceID = CL.datasource_id WHERE CL.service_id =? order by library_sort,library_id";
|
|
|
- Map<String, Object> calculationResult = myDbHelper.queryByParamsReturnList(calculationListSql, service_Id);
|
|
|
- calculationLibraryList = MapTools.getMapList(calculationResult);
|
|
|
- if (!calculationResult.get("code").equals("0")) {
|
|
|
- errorMessage = "查询" + service_Id + "的算法失败:" + calculationResult.get("message");
|
|
|
- }
|
|
|
- if (calculationLibraryList == null || calculationLibraryList.isEmpty()) {
|
|
|
- errorMessage = "服务ID " + service_Id + "没有找到对应的算法";
|
|
|
+ try{
|
|
|
+ baseDbHelper = new MyDbHelper(AppConfig.getSystemParams(AppConfig.REMOTE_DB_CONNECT));
|
|
|
+ if (Objects.nonNull(baseDbHelper.getErrorMessage())) {
|
|
|
+ errorMessage = "获取底座myDbHelper对象异常: ".concat(baseDbHelper.getErrorMessage());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, Object> calculationMap = baseDbHelper.queryByParamsReturnList("SELECT CL.*,DI.* FROM calculation_library CL left JOIN datasourceinfo DI ON DI.dataSourceID = CL.datasource_id WHERE CL.service_id =? and CL.library_type is not null order by library_sort,library_id", service_Id);
|
|
|
+ calcList = Objects.isNull(calculationMap.get("returnData"))?null:(List<Map<String, Object>>) calculationMap.get("returnData");
|
|
|
+ if (!calculationMap.get("code").equals("0") || calcList == null || calcList.isEmpty()) {
|
|
|
+ errorMessage = "查询".concat(serviceId).concat("的算法失败:").concat(calculationMap.get("message").toString());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, Object> serviceAuth = baseDbHelper.queryByParamsReturnList("SELECT app_id FROM appService WHERE serviceID = ?", service_Id);
|
|
|
+ if (!serviceAuth.get("code").equals("0")) {
|
|
|
+ errorMessage = "获取".concat(service_Id).concat("的应用安全等级出错:").concat(serviceAuth.get("message").toString());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Object serviceAuthList = serviceAuth.get("returnData");
|
|
|
+ if (!Objects.isNull(serviceAuthList)) {
|
|
|
+ serviceAuthMap = ((List<Map<String, Object>>) serviceAuthList).stream().map(map -> map.get("app_id").toString()).toList();
|
|
|
+ }
|
|
|
+ }catch (Exception e) {
|
|
|
+ errorMessage = "数据处理对象初始化异常: ".concat(serviceId).concat(";异常信息:").concat(LogUtils.getException(e));
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
- * 关闭的连接信息
|
|
|
- */
|
|
|
+
|
|
|
public void close() {
|
|
|
- for (ScriptEnginePro scriptEnginePro : ScriptEngineProMaps.values()) {
|
|
|
- scriptEnginePro.close();
|
|
|
+ for (ScriptEnginePro scriptEnginePro : ScriptEngineProMaps.values()) {
|
|
|
+ scriptEnginePro.close();
|
|
|
+ }
|
|
|
+ ScriptEngineProMaps.clear();
|
|
|
+ for (MyDbHelper calcMyDbHelper : calcDbHelperMaps.values()) {
|
|
|
+ calcMyDbHelper.close();
|
|
|
}
|
|
|
- ScriptEngineProMaps.clear();
|
|
|
+ calcDbHelperMaps.clear();
|
|
|
+ baseDbHelper.close();
|
|
|
}
|
|
|
-
|
|
|
|
|
|
public Map<String, Object> processSuccess(Object returnData) {
|
|
|
if (returnData instanceof Map) {
|
|
@@ -83,7 +79,6 @@ public class DataProcess {
|
|
|
returnMap.put("returnData", returnData);
|
|
|
return returnMap;
|
|
|
}
|
|
|
-
|
|
|
|
|
|
public Map<String, Object> processFail(String errorMessage, String libraryId) {
|
|
|
Map<String, Object> returnMap = new HashMap<>();
|
|
@@ -94,383 +89,290 @@ public class DataProcess {
|
|
|
}
|
|
|
return returnMap;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
public Map<String, Object> processData(Map<String, Object> inputData, String... user_id) {
|
|
|
-
|
|
|
if (!MapTools.isBlank(errorMessage)) {
|
|
|
- LogUtils.log("processData:1", "-1", null, "服务不可用" + errorMessage, serviceId, AppConfig.WORK_ID, MapTools.jacksonObjToStr(inputData), null, null, null);
|
|
|
- return processFail("服务不可用" + errorMessage, null);
|
|
|
+ LogUtils.log("processData:1", "-1", null, "服务不可用".concat(errorMessage), serviceId, inputData, null, null, null);
|
|
|
+ return processFail("服务不可用".concat(errorMessage), null);
|
|
|
}
|
|
|
if (System.currentTimeMillis() - lastActive > 2000) {
|
|
|
lastActive = System.currentTimeMillis();
|
|
|
- Runtime.getRuntime().gc();
|
|
|
-
|
|
|
- MyDbHelper myDbHelper = ObjectMap.getordropMyDbHelper(AppConfig.getSystemParams(AppConfig.REMOTE_DB_CONNECT));
|
|
|
- if (Objects.nonNull(myDbHelper.getErrorMessage())) {
|
|
|
- LogUtils.log("processData:2", "-1", null, "数据接收错误,获取远程数据库对象: " + myDbHelper.getErrorMessage(), serviceId, AppConfig.WORK_ID, MapTools.jacksonObjToStr(inputData), null, null, null);
|
|
|
- return processFail("数据接收错误,获取远程数据库对象: " + myDbHelper.getErrorMessage(), null);
|
|
|
- }
|
|
|
- myDbHelper.updateByCondition("update serviceinfo set runState = 1 ,lastactive = now() where serviceID =?", null, serviceId);
|
|
|
+ baseDbHelper.updateByCondition("update serviceinfo set runState = ? ,lastactive = ? where serviceID =?", null, "1", lastActive, serviceId);
|
|
|
}
|
|
|
|
|
|
if (!inputData.containsKey("dataObjectId")) {
|
|
|
-
|
|
|
- inputData.put("dataObjectId", createLifeCycleCol(Long.valueOf(AppConfig.WORK_ID), Integer.parseInt(serviceId)));
|
|
|
+ inputData.put("dataObjectId", createLifeCycleCol(Long.valueOf(AppConfig.WORK_ID), Integer.parseInt(serviceId)));
|
|
|
}
|
|
|
- String dataObjectId = (String) inputData.get("dataObjectId");
|
|
|
- Object event = inputData.get("event");
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- if (!event.equals("1") && Objects.nonNull(user_id) && inputData.containsKey("auth_id")) {
|
|
|
- MyDbHelper myDbHelper = ObjectMap.getordropMyDbHelper(AppConfig.getSystemParams(AppConfig.REMOTE_DB_CONNECT));
|
|
|
- if (Objects.nonNull(myDbHelper.getErrorMessage())) {
|
|
|
- LogUtils.log("processData:2", "-1", null, "数据接收错误,获取远程数据库对象: " + myDbHelper.getErrorMessage(), serviceId, AppConfig.WORK_ID, MapTools.jacksonObjToStr(inputData), null, null, null);
|
|
|
- return processFail("数据接收错误,获取远程数据库对象: " + myDbHelper.getErrorMessage(), null);
|
|
|
- }
|
|
|
- Map<String, Object> authServiceMap = myDbHelper.queryByParamsReturnList("SELECT auth_id FROM t_auth WHERE serviceID = ? and auth_id = ? ", serviceId, inputData.get("auth_id"));
|
|
|
- if (authServiceMap.get("code").equals("-1") || (authServiceMap.get("returnData") instanceof List<?> returnDataList && returnDataList.isEmpty())) return authServiceMap;
|
|
|
-
|
|
|
-
|
|
|
- Map<String, Object> userDataAuthMap = myDbHelper.queryByParamsReturnList("SELECT QCS.columnName,TUA.row_auth FROM t_user_group_auth TUA LEFT JOIN querytemplatecolumnset QCS on TUA.queryTemplateColumnSetID = QCS.queryTemplateColumnSetID WHERE TUA.user_id = ? AND TUA.auth_id = ? AND TUA.queryTemplateColumnSetID IS NOT NULL", user_id[0], inputData.get("auth_id"));
|
|
|
- if (userDataAuthMap.get("code").equals("-1")) return userDataAuthMap;
|
|
|
- List<Object> userList = new ArrayList<>();
|
|
|
- ((List<Map<String, Object>>) userDataAuthMap.get("returnData")).stream().filter(map -> Objects.nonNull(map.get("row_auth"))).forEach(tempMap -> {
|
|
|
- String[] row_auths = tempMap.get("row_auth").toString().split(",");
|
|
|
- for (String row_auth : row_auths) {
|
|
|
- if (Objects.isNull(row_auth)) continue;
|
|
|
- HashMap<String, Object> hashMap = new HashMap<>();
|
|
|
- hashMap.put(tempMap.get("columnName").toString(), row_auth.startsWith("!") ? row_auth.substring(1) : row_auth);
|
|
|
- if (row_auth.startsWith("!")) {
|
|
|
- userList.addAll(changeSignFilter(hashMap, "!", "or"));
|
|
|
- } else {
|
|
|
- userList.addAll(changeSignFilter(hashMap, null, "or"));
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
- ((Map<String, Object>) userList.get(0)).put("left", "(");
|
|
|
- ((Map<String, Object>) userList.get(userList.size() - 1)).put("connector", " and ");
|
|
|
- ((Map<String, Object>) userList.get(userList.size() - 1)).put("right", " ) ");
|
|
|
- inputData = initParam(inputData, "0", userList);
|
|
|
- }
|
|
|
-
|
|
|
- List<Map<String, Object>> calculationResult = new ArrayList<>();
|
|
|
- calculationResult.add(inputData);
|
|
|
- Map<String, List<Map<String, Object>>> execResult = execCalultion(calculationResult, null, event, dataObjectId, inputData.get("page"), inputData.get("pageSize"));
|
|
|
- calculationResult = execResult.get("calcData");
|
|
|
- Map<String, Object> lastResult = calculationResult.get(calculationResult.size() - 1);
|
|
|
- String libraryId = lastResult.containsKey("library_id") ? lastResult.get("library_id").toString() : null;
|
|
|
+ String dataObjectId = inputData.get("dataObjectId").toString();
|
|
|
+ inputData = authCheck(inputData, user_id);
|
|
|
+ List<Map<String, Object>> calcData = new ArrayList<>();
|
|
|
+ calcData.add(inputData);
|
|
|
+ Map<String, Object> execResult = execCalultion(calcData, null, dataObjectId);
|
|
|
+ String library_id = execResult.get("library_id").toString();
|
|
|
+ List<Map<String, Object>> preListData = (List<Map<String, Object>>) execResult.get("preData");
|
|
|
+ calcData = (List<Map<String, Object>>) execResult.get("calcData");
|
|
|
+
|
|
|
+ Map<String, Object> lastResult = library_id.endsWith("N")?preListData.get(preListData.size() - 1):calcData.get(calcData.size() - 1);
|
|
|
if (Objects.equals(lastResult.get("code"), "-1")) {
|
|
|
Object message = lastResult.get("message");
|
|
|
- LogUtils.log("processData:3", "-1", libraryId, Objects.nonNull(message) ? message.toString() : null, serviceId, AppConfig.WORK_ID, MapTools.jacksonObjToStr(calculationResult), dataObjectId, MapTools.jacksonObjToStr(execResult.get("calcData")), event);
|
|
|
- setServiceErrorCount(libraryId, serviceId, "-1");
|
|
|
- return processFail(libraryId + "数据计算错误:" + lastResult.get("message"), libraryId);
|
|
|
+ LogUtils.log("processData:3", "-1", library_id, Objects.nonNull(message)?message.toString():null, serviceId, calcData, dataObjectId, preListData, inputData.get("event"));
|
|
|
+ return processFail(library_id.concat("数据计算错误:").concat(lastResult.get("message").toString()), library_id);
|
|
|
}
|
|
|
|
|
|
- LogUtils.log("DataProcess:9999", "0", libraryId, "数据接收后处理成功", serviceId, AppConfig.WORK_ID, MapTools.jacksonObjToStr(calculationResult), dataObjectId, MapTools.jacksonObjToStr(execResult.get("calcData")), event);
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- List<Map<String, Object>> collect = calculationResult.stream().filter(map -> Objects.isNull(map.get("library_id")) || !map.get("library_id").toString().contains("_N")).toList();
|
|
|
- return processSuccess(collect.get(collect.size() - 1));
|
|
|
+ LogUtils.log("DataProcess:9999", "0", library_id, "数据接收后处理成功", serviceId, calcData, dataObjectId, preListData, inputData.get("event"));
|
|
|
+ return processSuccess(lastResult);
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
- private Map<String, Object> initParam(Map<String, Object> inputData, String event, List<Object> authList) {
|
|
|
- Object dataContent = inputData.get("dataContent");
|
|
|
- if (Objects.isNull(dataContent)) {
|
|
|
- Map<String, Object> tempDataContent = new HashMap<>(inputData);
|
|
|
- baseInfo.forEach(tempDataContent.keySet()::remove);
|
|
|
- dataContent = tempDataContent;
|
|
|
- }
|
|
|
- if (dataContent instanceof Map<?, ?> dataContentMap) {
|
|
|
- List<Map<?, ?>> tempDataContent = new ArrayList<>();
|
|
|
- tempDataContent.add(dataContentMap);
|
|
|
- dataContent = tempDataContent;
|
|
|
+
|
|
|
+ public Map<String, Object> authCheck(Map<String, Object> inputData, String... user_id) {
|
|
|
+ Object appid = inputData.get("appid");
|
|
|
+ if (Objects.nonNull(appid) && !serviceAuthMap.contains(appid.toString())) {
|
|
|
+ return processFail("服务未授权", null);
|
|
|
}
|
|
|
- if (dataContent instanceof List dataContentList) {
|
|
|
- List<Map<String, Object>> returnDataContent = new ArrayList<>();
|
|
|
- for (Object signDataContent : dataContentList) {
|
|
|
- if (!(signDataContent instanceof Map)) return inputData;
|
|
|
- returnDataContent.add(createStandardParameters((Map<String, Object>) signDataContent, event, authList));
|
|
|
- }
|
|
|
- dataContent = returnDataContent;
|
|
|
- }
|
|
|
- inputData.put("dataContent", dataContent);
|
|
|
- return inputData;
|
|
|
- }
|
|
|
-
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
- private Map<String, Object> createStandardParameters(Map<String, Object> inDataContent, String event, List<Object> authList) {
|
|
|
- Map<String, Object> returnData = new HashMap<>();
|
|
|
- if ("1,2".contains(event)) {
|
|
|
- Object tempValueObject = inDataContent.get("Value");
|
|
|
- if (Objects.isNull(tempValueObject)) {
|
|
|
- HashMap<String, Object> tempValue = new HashMap<>(inDataContent);
|
|
|
- tempValue.remove("filter");
|
|
|
- tempValueObject = tempValue;
|
|
|
- }
|
|
|
- returnData.put((tempValueObject instanceof Map) ? "Value" : "newValue", tempValueObject);
|
|
|
- }
|
|
|
- if (!("1".equals(event))) {
|
|
|
- Object tempFilterObject = inDataContent.get("filter");
|
|
|
- tempFilterObject = Objects.isNull(tempFilterObject) ? inDataContent : tempFilterObject;
|
|
|
- if (tempFilterObject instanceof Map) {
|
|
|
- authList.addAll(changeSignFilter((Map<String, Object>) tempFilterObject, null, null));
|
|
|
- returnData.put("filter", authList);
|
|
|
- } else {
|
|
|
- if (tempFilterObject instanceof List tempFilterObjectList && tempFilterObjectList.get(0) instanceof Map) {
|
|
|
- List<Object> tempFilterList = new ArrayList<>(authList);
|
|
|
- for (Object o : tempFilterObjectList) {
|
|
|
- tempFilterList.addAll(changeSignFilter((Map<String, Object>) o, null, null));
|
|
|
- }
|
|
|
- returnData.put("filter", tempFilterList);
|
|
|
- } else {
|
|
|
- returnData.put("newFilter", tempFilterObject);
|
|
|
+
|
|
|
+ if (Objects.nonNull(user_id) && inputData.containsKey("authId") && !"1,9999".contains(user_id[0])) {
|
|
|
+ Map<String, Object> userDataAuthMap = baseDbHelper.queryByParamsReturnList("SELECT QCS.columnName,TUA.row_auth FROM t_user_group_auth TUA LEFT JOIN querytemplatecolumnset QCS on TUA.queryTemplateColumnSetID = QCS.queryTemplateColumnSetID WHERE TUA.user_id = ? AND TUA.auth_id = ? AND TUA.queryTemplateColumnSetID IS NOT NULL", user_id[0], inputData.get("authId"));
|
|
|
+ if (userDataAuthMap.get("code").equals("-1") || Objects.isNull(userDataAuthMap.get("returnData")) || ((List<Map<String, Object>>) userDataAuthMap.get("returnData")).isEmpty()) {
|
|
|
+ if (userDataAuthMap.get("code").equals("0")) {
|
|
|
+ userDataAuthMap.put("message", "服务未授任何数据权限");
|
|
|
}
|
|
|
+ return userDataAuthMap;
|
|
|
}
|
|
|
- }
|
|
|
- return returnData;
|
|
|
- }
|
|
|
-
|
|
|
- private List<Map<String, Object>> changeSignFilter(Map<String, Object> filterMap, String compare, String connect) {
|
|
|
- List<Map<String, Object>> objects = new ArrayList<>();
|
|
|
- if (filterMap.containsKey("left")) {
|
|
|
- objects.add(filterMap);
|
|
|
- return objects;
|
|
|
- }
|
|
|
- for (String filterKey : filterMap.keySet()) {
|
|
|
- Map<String, Object> returnMap = new HashMap<>();
|
|
|
- Object keyValues = filterMap.get(filterKey);
|
|
|
- returnMap.put("left", "");
|
|
|
- returnMap.put("column", filterKey);
|
|
|
- returnMap.put("comparator", (Objects.nonNull(compare) && compare.startsWith("!")) ? "!=" : " = ");
|
|
|
- if (Objects.isNull(keyValues)) {
|
|
|
- returnMap.put("comparator", " is null ");
|
|
|
+ List<Map<String, Object>> rowAuth = new ArrayList<>();
|
|
|
+ List<String> authColumn = new ArrayList<>();
|
|
|
+ List<Map<String, Object>> dataAuth = (List<Map<String, Object>>) userDataAuthMap.get("returnData");
|
|
|
+ dataAuth.forEach(dataAuthMap -> {
|
|
|
+ String columnName = dataAuthMap.get("columnName").toString();
|
|
|
+ authColumn.add(columnName);
|
|
|
+ Object rowAuthObj = dataAuthMap.get("row_auth");
|
|
|
+ if (Objects.nonNull(rowAuthObj)) {
|
|
|
+ String tempRowAuth = rowAuthObj.toString();
|
|
|
+ String connect = tempRowAuth.startsWith("!") ? "!=" : "=";
|
|
|
+ tempRowAuth = tempRowAuth.startsWith("!") ? tempRowAuth.substring(1) : tempRowAuth;
|
|
|
+ String[] row_auths = tempRowAuth.split(",");
|
|
|
+ for (String row_auth : row_auths) {
|
|
|
+ if (MapTools.isBlank(row_auth)) continue;
|
|
|
+ Map<String, Object> signRowAuth = new HashMap<>();
|
|
|
+ signRowAuth.put(columnName, row_auth);
|
|
|
+ rowAuth.addAll(baseDbHelper.changeSignFilter(signRowAuth, connect, connect.equals("!=") ? "and" : "or"));
|
|
|
+ }
|
|
|
+ if (rowAuth.size() > 0) {
|
|
|
+ rowAuth.get(0).put("left", "(");
|
|
|
+ rowAuth.get(rowAuth.size() - 1).put("connector", " and ");
|
|
|
+ rowAuth.get(rowAuth.size() - 1).put("right", " ) ");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ if (rowAuth.size() > 0) {
|
|
|
+ rowAuth.get(0).put("left", "(".concat(rowAuth.get(0).get("left").toString()));
|
|
|
+ rowAuth.get(rowAuth.size() - 1).put("connector", " and ");
|
|
|
+ rowAuth.get(rowAuth.size() - 1).put("right", " ) ".concat(rowAuth.get(rowAuth.size() - 1).get("right").toString()));
|
|
|
+ inputData.put("rowAuth", rowAuth);
|
|
|
}
|
|
|
- returnMap.put("value", keyValues);
|
|
|
- returnMap.put("right", "");
|
|
|
- returnMap.put("connector", Objects.nonNull(connect) ? (" " + connect) : " and ");
|
|
|
- objects.add(returnMap);
|
|
|
+ inputData.put("authColumn", authColumn);
|
|
|
}
|
|
|
- return objects;
|
|
|
+ return inputData;
|
|
|
}
|
|
|
-
|
|
|
- public Map<String, List<Map<String, Object>>> execCalultion(List<Map<String, Object>> inData, String beginLibraryId, Object event, String dataObjectId, Object page, Object pageSize) {
|
|
|
- Map<String, List<Map<String, Object>>> returnData = new HashMap<>();
|
|
|
- List<Map<String, Object>> preData = new ArrayList<>();
|
|
|
- List<Map<String, Object>> calcData = Objects.isNull(inData) || inData.isEmpty() ? new ArrayList<>() : inData;
|
|
|
-
|
|
|
- for (Map<String, Object> calculationLibrary : calculationLibraryList) {
|
|
|
-
|
|
|
- String library_id = calculationLibrary.get("library_id").toString();
|
|
|
- if (MapTools.isNotBlank(beginLibraryId) && !beginLibraryId.equals(library_id)) {
|
|
|
+
|
|
|
+ public Map<String, Object> execCalultion(List<Map<String, Object>> inData, String beginLibraryId, String dataObjectId) {
|
|
|
+ Map<String, Object> returnData = new HashMap<>();
|
|
|
+ List<Map<String, Object>> preData = new ArrayList<>();
|
|
|
+ String lastLibraryId = "";
|
|
|
+ List<Map<String, Object>> calcData = Objects.isNull(inData) || inData.isEmpty() ? new ArrayList<>() : inData;
|
|
|
+ Map<String, Object> preCalMap = new HashMap<>();
|
|
|
+ for (Map<String, Object> calculationLibrary : calcList) {
|
|
|
+ lastLibraryId = calculationLibrary.get("library_id").toString();
|
|
|
+ if (MapTools.isNotBlank(beginLibraryId) && !beginLibraryId.equals(lastLibraryId)) {
|
|
|
continue;
|
|
|
}
|
|
|
beginLibraryId = null;
|
|
|
- String library_type = calculationLibrary.get("library_type").toString();
|
|
|
- String service_id = calculationLibrary.get("service_id").toString();
|
|
|
- String computing_expression = MapTools.objectToString(calculationLibrary.get("computing_expression"));
|
|
|
- Object is_exec = calculationLibrary.get("is_exec");
|
|
|
- Map<String, Object> currentResult;
|
|
|
- if (Objects.nonNull(is_exec)) {
|
|
|
- Map<String, Object> preCalMap = new HashMap<>();
|
|
|
- preCalMap.put("library_id", library_id + "_N");
|
|
|
- preCalMap.put("service_id", service_id);
|
|
|
- preCalMap.put("library_type", 2);
|
|
|
- preCalMap.put("computing_expression", is_exec);
|
|
|
- Map<String, Object> preEnginResult = execEngine(preCalMap, calcData, dataObjectId);
|
|
|
- preEnginResult.put("library_id", library_id + "_N");
|
|
|
-
|
|
|
- preData.add(preEnginResult);
|
|
|
- returnData.put("preData", preData);
|
|
|
-
|
|
|
- if ("-1".equals(preEnginResult.get("code")) || Objects.equals("2", preEnginResult.get("returnData"))) {
|
|
|
- return returnData;
|
|
|
+ Object is_exec = calculationLibrary.get("is_exec");
|
|
|
+ if (Objects.nonNull(is_exec) && !MapTools.isBlank(is_exec.toString())) {
|
|
|
+ lastLibraryId = lastLibraryId.concat("_N");
|
|
|
+ preCalMap.put("library_id", lastLibraryId);
|
|
|
+ preCalMap.put("library_type", 2);
|
|
|
+ preCalMap.put("computing_expression", is_exec);
|
|
|
+ Map<String, Object> preEnginResult = execEngine(lastLibraryId, preCalMap, calcData);
|
|
|
+ preData.add(preEnginResult);
|
|
|
+ setServiceErrorCount(lastLibraryId, preEnginResult.get("code").toString());
|
|
|
+ if ("-1".equals(preEnginResult.get("code")) || Objects.equals("2", preEnginResult.get("returnData"))) {
|
|
|
+ break;
|
|
|
}
|
|
|
- setServiceErrorCount(library_id + "_N", serviceId, "0");
|
|
|
if (Objects.equals("1", preEnginResult.get("returnData"))) {
|
|
|
+ calcData.add(null);
|
|
|
continue;
|
|
|
}
|
|
|
+ lastLibraryId = calculationLibrary.get("library_id").toString();
|
|
|
}
|
|
|
- if (library_type.equals("3")) {
|
|
|
- String connectConfig = calculationLibrary.get("connectConfig").toString();
|
|
|
- MyDbHelper myDbHelper = ObjectMap.getordropMyDbHelper(connectConfig);
|
|
|
- if (Objects.nonNull(myDbHelper.getErrorMessage())) {
|
|
|
- calcData.add(processFail("算法获取数据库连接异常:" + myDbHelper.getErrorMessage(), library_id));
|
|
|
- returnData.put("calcData", calcData);
|
|
|
- return returnData;
|
|
|
- }
|
|
|
-
|
|
|
- String tableName = Objects.isNull(calculationLibrary.get("sourceObjectName")) ? null : calculationLibrary.get("sourceObjectName").toString();
|
|
|
- Object isActiveTable = calculationLibrary.get("isActiveTable");
|
|
|
-
|
|
|
- String eventFlag = Objects.isNull(calculationLibrary.get("event")) ? (MapTools.isNotBlank(tableName) ? event.toString() : AppConfig.staticEvent.get(Objects.requireNonNull(computing_expression).trim().toLowerCase().substring(0, 6))) : calculationLibrary.get("event").toString();
|
|
|
- if (null == eventFlag) {
|
|
|
- calcData.add(processFail("事件标志为空", library_id));
|
|
|
- returnData.put("calcData", calcData);
|
|
|
- return returnData;
|
|
|
- }
|
|
|
-
|
|
|
- List<Map<String,Object>> dbPrams = new ArrayList<>();
|
|
|
- Object tempDBPrams = null;
|
|
|
- try {
|
|
|
- if (Objects.isNull(calculationLibrary.get("parmIndex"))) {
|
|
|
- Map<String, Object> tempMap = calcData.get(calcData.size() - 1);
|
|
|
- tempDBPrams = tempMap.containsKey("dataContent") ? tempMap.get("dataContent") : tempMap.get("returnData");
|
|
|
- } else {
|
|
|
- tempDBPrams = calcData;
|
|
|
- String[] parmSplit = calculationLibrary.get("parmIndex").toString().split("\\.");
|
|
|
- for (String item : parmSplit) {
|
|
|
- if (MapTools.isNumber(item) && tempDBPrams instanceof List<?> tempList) {
|
|
|
- tempDBPrams = tempList.get(Integer.parseInt(item));
|
|
|
- } else {
|
|
|
- if (tempDBPrams instanceof Map<?,?> tempMap){
|
|
|
- tempDBPrams = tempMap.get(item);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- if (tempDBPrams instanceof Map tempMap) {
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- dbPrams.add(tempMap);
|
|
|
- } else if (tempDBPrams instanceof List tempList) {
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- dbPrams = tempList;
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- calcData.add(processFail("入参数据格式错误:不是List<Map<String,Object> " + tempDBPrams, library_id));
|
|
|
- returnData.put("calcData", calcData);
|
|
|
- return returnData;
|
|
|
- }
|
|
|
-
|
|
|
- if (eventFlag.equals("6")) {
|
|
|
- currentResult = myDbHelper.insertOrUpdate(tableName, dbPrams);
|
|
|
- } else {
|
|
|
- if (eventFlag.equals("7")){
|
|
|
- currentResult = myDbHelper.bathEvent7(computing_expression, dbPrams);
|
|
|
- }else {
|
|
|
- currentResult = myDbHelper.generalProcess(eventFlag, tableName, computing_expression, null, dbPrams, Objects.nonNull(isActiveTable) && Objects.equals(isActiveTable, "true"), page, pageSize);
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- currentResult = execEngine(calculationLibrary, inData, dataObjectId);
|
|
|
+
|
|
|
+ Map<String, Object> currentResult = calculationLibrary.get("library_type").toString().equals("3") ? execDB(lastLibraryId, calculationLibrary, inData, dataObjectId) : execEngine(lastLibraryId, calculationLibrary, inData);
|
|
|
+ setServiceErrorCount(lastLibraryId, Objects.nonNull(currentResult) && currentResult.containsKey("code") ? currentResult.get("code").toString() : "0");
|
|
|
+ calcData.add(currentResult);
|
|
|
+ if (Objects.nonNull(currentResult) && !currentResult.get("code").equals("0")) {
|
|
|
+ break;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- currentResult.put("library_id", library_id);
|
|
|
- calcData.add(currentResult);
|
|
|
- returnData.put("calcData", calcData);
|
|
|
-
|
|
|
- setServiceErrorCount(library_id, serviceId, "0");
|
|
|
}
|
|
|
-
|
|
|
+ returnData.put("library_id", lastLibraryId);
|
|
|
+ returnData.put("preData", preData);
|
|
|
+ returnData.put("calcData", calcData);
|
|
|
return returnData;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
- * 算法连续错误次数判定
|
|
|
- *
|
|
|
- * @param location
|
|
|
- * @param serviceId
|
|
|
- * @param success
|
|
|
- */
|
|
|
- public void setServiceErrorCount(String location, String serviceId, String success) {
|
|
|
- String key = String.format("processData %s_%s", location, serviceId);
|
|
|
- if (!success.equals("0")) {
|
|
|
- Integer errCount = serviceErrorCount.get(key);
|
|
|
- errCount = (null == errCount) ? 1 : errCount + 1;
|
|
|
- serviceErrorCount.put(key, errCount);
|
|
|
- if (errCount > AppConfig.SERVICE_ERR_MAX) {
|
|
|
- ServiceInputControl.stop(serviceId);
|
|
|
- }
|
|
|
- } else {
|
|
|
- serviceErrorCount.put(key, 0);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private Map<String, Object> execEngine(Map<String, Object> currentCalMap, List<Map<String, Object>> allData, String dataObjectId) {
|
|
|
- String library_id = currentCalMap.get("library_id").toString();
|
|
|
+
|
|
|
+ private Map<String, Object> execEngine(String library_id, Map<String, Object> currentCalMap, List<Map<String, Object>> calcAllData) {
|
|
|
if (!ScriptEngineProMaps.containsKey(library_id)) {
|
|
|
ScriptEngineProMaps.put(library_id, new ScriptEnginePro(currentCalMap));
|
|
|
}
|
|
|
ScriptEnginePro currentEngin = ScriptEngineProMaps.get(library_id);
|
|
|
- if (Objects.nonNull(currentEngin) && Objects.nonNull(currentEngin.getErrorMessage())) {
|
|
|
+ String errMessage = "";
|
|
|
+ if (Objects.nonNull(currentEngin) && Objects.nonNull(currentEngin.getErrorMessage())) {
|
|
|
+ errMessage = currentEngin.getErrorMessage();
|
|
|
currentEngin.close();
|
|
|
currentEngin = null;
|
|
|
+ }
|
|
|
+ if (Objects.isNull(currentEngin)) {
|
|
|
ScriptEngineProMaps.remove(library_id);
|
|
|
+ return processFail("创建引擎失败".concat(errMessage), library_id);
|
|
|
}
|
|
|
- if (Objects.isNull(currentEngin) || Objects.nonNull(currentEngin.getErrorMessage())) {
|
|
|
- return processFail("创建引擎失败" + (Objects.nonNull(currentEngin) ? currentEngin.getErrorMessage() : ""), library_id);
|
|
|
+ Map<String, String> parmaNames = currentEngin.getParmaNames();
|
|
|
+ Map<String, Object> paramValue = new HashMap<>();
|
|
|
+ for (String key : parmaNames.keySet()) {
|
|
|
+ Object currentData = dataSubscription(calcAllData, key, currentCalMap);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if (currentCalMap.get("library_type").toString().equals("2")){
|
|
|
+ paramValue.put(parmaNames.get(key), currentData);
|
|
|
+ }else {
|
|
|
+ paramValue.put(key, currentData);
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
- return currentEngin.execScript(allData);
|
|
|
+ return currentEngin.execScript(paramValue);
|
|
|
}
|
|
|
+
|
|
|
+ private Map<String, Object> execDB(String library_id, Map<String, Object> calculationLibrary, List<Map<String, Object>> calcData, String dataObjectId) {
|
|
|
+ Object connectConfigObj = calculationLibrary.get("connectConfig");
|
|
|
+ if (Objects.isNull(connectConfigObj)) {
|
|
|
+ return processFail("连接信息未配置 ", library_id);
|
|
|
+ }
|
|
|
+ if (!calcDbHelperMaps.containsKey(library_id)) {
|
|
|
+ calcDbHelperMaps.put(library_id, new MyDbHelper(connectConfigObj.toString()));
|
|
|
+ }
|
|
|
+ MyDbHelper myDbHelper = calcDbHelperMaps.get(library_id);
|
|
|
+ String errMessage = "";
|
|
|
+ if (Objects.nonNull(myDbHelper) && MapTools.isNotBlank(errMessage)) {
|
|
|
+ errMessage = myDbHelper.getErrorMessage();
|
|
|
+ myDbHelper.close();
|
|
|
+ myDbHelper = null;
|
|
|
+ }
|
|
|
+ if (Objects.isNull(myDbHelper)) {
|
|
|
+ calcDbHelperMaps.remove(library_id);
|
|
|
+ return processFail("获取业务数据库对象失败".concat(errMessage), library_id);
|
|
|
+ }
|
|
|
+
|
|
|
+ String paramIndex = Objects.isNull(calculationLibrary.get("parmIndex")) ? (String.valueOf(calcData.size() - 1)).concat(".returnData") : calculationLibrary.get("parmIndex").toString();
|
|
|
+ Object tempDBPrams = dataSubscription(calcData, "List.".concat(paramIndex), calculationLibrary);
|
|
|
+
|
|
|
+
|
|
|
+ if("7".equals(calculationLibrary.get("event")) && !Objects.isNull(calculationLibrary.get("tableName")) && !Objects.isNull(calculationLibrary.get("computing_expression"))){
|
|
|
+ Map<String,Object> tempCalcInfo = calculationLibrary;
|
|
|
+ String tableName = tempCalcInfo.get("tableName").toString();
|
|
|
+ String sqlStr = tempCalcInfo.get("computing_expression").toString();
|
|
|
+ tempCalcInfo.put("tableName","");
|
|
|
+ tempDBPrams = myDbHelper.generalProcess(tempCalcInfo, tempDBPrams, dataObjectId, calcData.get(0));
|
|
|
+
|
|
|
+
|
|
|
+ tempCalcInfo.put("tableName",tableName);
|
|
|
+ tempCalcInfo.put("computing_expression","");
|
|
|
+ tempCalcInfo.put("event","6");
|
|
|
+ return myDbHelper.generalProcess(tempCalcInfo, tempDBPrams, dataObjectId, calcData.get(0));
|
|
|
+ }
|
|
|
+ return myDbHelper.generalProcess(calculationLibrary, tempDBPrams, dataObjectId, calcData.get(0));
|
|
|
+ }
|
|
|
+
|
|
|
+ private Object dataSubscription(List<Map<String, Object>> calcAllData, String paramRule, Map<String, Object> calculationLibrary) {
|
|
|
+ String[] itemRule = paramRule.split("\\.");
|
|
|
+ String dataType = itemRule.length > 0 ? itemRule[0] : "List";
|
|
|
+ dataType = dataType.endsWith("]")?dataType.substring(0,dataType.indexOf("[")):dataType;
|
|
|
+ String dataLocation = itemRule.length > 1 ? itemRule[1] : "";
|
|
|
+ if (dataLocation.equals("T")) {
|
|
|
+ Object tempObj = itemRule.length > 2 ? MapTools.isBlank(itemRule[2])?("List".equals(dataType)?new ArrayList<>():("JSON,Map".contains(dataType)?new HashMap<>():null)):itemRule[2]:null;
|
|
|
+ if ("Boolean".equals(dataType)){
|
|
|
+ tempObj = itemRule[2].equals("true");
|
|
|
+ }
|
|
|
+ return tempObj;
|
|
|
|
|
|
-
|
|
|
- * 支持的最大机器id,结果是31 (这个移位算法可以很快计算出几位二进制数所能表示的最大十进制数)
|
|
|
- */
|
|
|
- private final String maxWorkerId = "4";
|
|
|
-
|
|
|
- * 支持的最大数据标识id,结果是31
|
|
|
- */
|
|
|
- private final String maxServiceId = "4";
|
|
|
-
|
|
|
- * 毫秒内序列(0~4095)
|
|
|
- */
|
|
|
- private final Long maxSequence = 999L;
|
|
|
-
|
|
|
- private long sequence = 0L;
|
|
|
-
|
|
|
-
|
|
|
- * 上次生成ID的时间截
|
|
|
- */
|
|
|
- private long lastTimestamp = -1L;
|
|
|
-
|
|
|
-
|
|
|
- * 生命周期ID生成
|
|
|
- *
|
|
|
- * @param workerId
|
|
|
- * @param serviceId
|
|
|
- * @return
|
|
|
- */
|
|
|
- public String createLifeCycleCol(Long workerId, Integer serviceId) {
|
|
|
+ }
|
|
|
+
|
|
|
+ Object returnData = dataLocation.equals("L") ? calculationLibrary : MapTools.isNumber(dataLocation) ? calcAllData.get(Integer.parseInt(dataLocation)) : null;
|
|
|
+ for (int index = 2; index < itemRule.length; index++) {
|
|
|
+ if(Objects.isNull(returnData)) return null;
|
|
|
+ if (returnData instanceof String) {
|
|
|
+ returnData = MapTools.strToObj(returnData.toString());
|
|
|
+ }
|
|
|
+ if (MapTools.isNumber(itemRule[index])) {
|
|
|
+
|
|
|
+ returnData = returnData instanceof List ? ((List<?>) returnData).get(Integer.parseInt(itemRule[index])) : returnData;
|
|
|
+ } else {
|
|
|
+ if(returnData instanceof Map){
|
|
|
+ returnData = ((Map<?, ?>) returnData).get(itemRule[index]);
|
|
|
+ }else{
|
|
|
+ if(returnData instanceof List && ((List<?>) returnData).size() > 0 && ((List<?>) returnData).get(0) instanceof Map){
|
|
|
+ returnData = ((Map<?, ?>) ((List<?>) returnData).get(0)).get(itemRule[index]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (Objects.nonNull(returnData)) {
|
|
|
+ if ("List,Map".contains(dataType) && returnData instanceof String) {
|
|
|
+ returnData = MapTools.strToObj(returnData.toString());
|
|
|
+ }
|
|
|
+ if ("List".equals(dataType) && !(returnData instanceof List<?>)) {
|
|
|
+ List<Object> tempList = new ArrayList<>();
|
|
|
+ tempList.add(returnData);
|
|
|
+ returnData = tempList;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ returnData = "Map".equals(dataType) && !(returnData instanceof Map<?, ?>) ? null
|
|
|
+ : ("String".equals(dataType) ? returnData.toString()
|
|
|
+ : ("Boolean".equals(dataType) ? returnData.toString().equals("true")
|
|
|
+ : ("JSONStr".equals(dataType) ? MapTools.objToJSONStr(returnData)
|
|
|
+ : returnData)));
|
|
|
+ }
|
|
|
+ return returnData;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setServiceErrorCount(String library_id, String success) {
|
|
|
+ if (!success.equals("0")) {
|
|
|
+ Integer errCount = Objects.isNull(serviceErrorCount.get(library_id))?1:serviceErrorCount.get(library_id)+1;
|
|
|
+ serviceErrorCount.put(library_id,errCount);
|
|
|
+ if (errCount > AppConfig.SERVICE_ERR_MAX) {
|
|
|
+ ServiceInputControl.stop(serviceId);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ serviceErrorCount.put(library_id, 0);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ private long sequence = 0L;
|
|
|
+ private long lastTimestamp = -1L;
|
|
|
+ public String createLifeCycleCol(Long workerId, Integer serviceId) {
|
|
|
long timestamp = System.currentTimeMillis();
|
|
|
|
|
|
-
|
|
|
- if (lastTimestamp == timestamp) {
|
|
|
+ if (lastTimestamp == timestamp) {
|
|
|
sequence++;
|
|
|
-
|
|
|
- if (sequence > maxSequence) {
|
|
|
-
|
|
|
+ if (sequence > 999L) {
|
|
|
sequence = 0;
|
|
|
- while (lastTimestamp == System.currentTimeMillis()) {
|
|
|
+ while (lastTimestamp == System.currentTimeMillis()) {
|
|
|
}
|
|
|
timestamp = System.currentTimeMillis();
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- else {
|
|
|
+ } else {
|
|
|
sequence = 0L;
|
|
|
}
|
|
|
-
|
|
|
- lastTimestamp = timestamp;
|
|
|
+ lastTimestamp = timestamp;
|
|
|
|
|
|
- return timestamp + "" + (String.format("%0" + (maxSequence.toString().length()) + "d", sequence)) + (String.format("%0" + maxWorkerId + "d", workerId)) + (String.format("%0" + maxServiceId + "d", serviceId));
|
|
|
+ return String.valueOf(timestamp).concat(String.format("%03d", sequence)).concat(String.format("%04d", workerId)).concat(String.format("%04d", serviceId));
|
|
|
}
|
|
|
|
|
|
public String getServiceId() {
|
|
@@ -481,12 +383,7 @@ public class DataProcess {
|
|
|
return errorMessage;
|
|
|
}
|
|
|
|
|
|
- public long getLastTimestamp() {
|
|
|
- return lastTimestamp;
|
|
|
- }
|
|
|
-
|
|
|
public long getLastActive() {
|
|
|
return lastActive;
|
|
|
}
|
|
|
-
|
|
|
}
|