diff --git a/src/main/java/aiyh/utils/response_deal/ResponseMappingDeal.java b/src/main/java/aiyh/utils/response_deal/ResponseMappingDeal.java index db24dd2..ed63911 100644 --- a/src/main/java/aiyh/utils/response_deal/ResponseMappingDeal.java +++ b/src/main/java/aiyh/utils/response_deal/ResponseMappingDeal.java @@ -37,68 +37,105 @@ public class ResponseMappingDeal { private final Logger logger = Util.getLogger("json_util"); private final ConfigMapper configMapper = Util.getMapper(ConfigMapper.class); - + /** 表数据回调 */ private TableDefinitionCallback tableCallback; - + /** 行数据回调 */ private RowDefinitionCallback rowCallback; - + /** 字段数据回调 */ private FieldDefinitionCallback fieldDefinitionCallback; - + /** 状态map的key */ private Class stateClassKey; - + public void doResponseSync(String uniqueCode, ResponeVo responeVo) { Map responseMap = responeVo.getResponseMap(); - ResponseUtil.parameterJudgment(responseMap,"接口返回都为空 你处理个勾"); - this.doResponseSync(uniqueCode,responseMap); + ResponseUtil.parameterJudgment(responseMap, "接口返回都为空 你处理个勾"); + this.doResponseSync(uniqueCode, responseMap); } - + + /** + *

响应同步 note by youhong.ai

+ * + * @param uniqueCode 唯一表示 + * @param responseMap 响应map + */ public void doResponseSync(String uniqueCode, Map responseMap) { + // note by youhong.ai 查询配置表 List responseConfigList = configMapper.queryResponseConfigByUnique(uniqueCode); - logger.info(String.format("%s 相关响应配置信息==> %s",uniqueCode, JSON.toJSONString(responseConfigList))); - ResponseUtil.parameterJudgment(responseConfigList,"response config is empty please check!!! "); + logger.info(String.format("%s 相关响应配置信息==> %s", uniqueCode, JSON.toJSONString(responseConfigList))); + // note by youhong.ai 自定义校验 + ResponseUtil.parameterJudgment(responseConfigList, "response config is empty please check!!! "); Map tableNameConfig; try { + // note by youhong.ai 对查询到的多个配置进行整合,以同步表表名作为key 配置作为value tableNameConfig = responseConfigList.stream().collect(Collectors.toMap(ResponseConfig::getModelTableName, v -> v)); } catch (Exception e) { logger.error("response config error please check!!! " + Util.getErrString(e)); throw new ResponseException("response config error please check!!! "); } - ResponseUtil.parameterJudgment(responseMap,"接口返回都为空 你处理个勾"); + ResponseUtil.parameterJudgment(responseMap, "接口返回都为空 你处理个勾"); + // note by youhong.ai 循环同步每一个表 tableNameConfig.forEach((key, value) -> doResponseSync(value, responseMap)); } - - public void doResponseSync(ResponseConfig responseConfig, Map responseMap){ + + /** + *

note by youhong.ai 同步数据

+ * + * @param responseConfig 配置表 + * @param responseMap 响应结果 + */ + public void doResponseSync(ResponseConfig responseConfig, Map responseMap) { + // note by youhong.ai 获取别名配置明细数据 List responseConfigAliasList = responseConfig.getResponseConfigAliasList(); - ResponseUtil.parameterJudgment(responseConfigAliasList,"responseConfigAliasList config is empty please check!!!"); + ResponseUtil.parameterJudgment(responseConfigAliasList, "responseConfigAliasList config is empty please check!!!"); + // note by youhong.ai 获取转换值配置明细 List valueChangeList = responseConfig.getValueChangeList(); - ResponseUtil.parameterJudgment(valueChangeList,"valueChangeList config is empty please check!!!"); - //数据信息按是否主表分组 + ResponseUtil.parameterJudgment(valueChangeList, "valueChangeList config is empty please check!!!"); + // 数据信息按是否主表分组 Map> aliasMap = responseConfigAliasList.stream().collect(Collectors.groupingBy(ResponseConfigAlias::getTableType)); + // note by youhong.ai 获取主表别名配置数据 List mainConfigList = aliasMap.get(ResponseConfigConstant.MAIN_TABLE); + // note by youhong.ai 按照表类型分组 Map> mainOrDetail = valueChangeList.stream().collect(Collectors.groupingBy(ResponseConfigValueChange::getTableType)); + // note by youhong.ai 获取主表值配置 List mainValueChangeList = mainOrDetail.get(ResponseConfigConstant.MAIN_TABLE); TableDefinition tableDefinition; - if(ResponseUtil.parameterIsNotNull(mainConfigList)) { + // note by youhong.ai 如果有主表配置 + if (ResponseUtil.parameterIsNotNull(mainConfigList)) { + // note by youhong.ai 解析json为表描述对象 tableDefinition = this.parsingJsonToTable(responseConfig.getModelTableName(), responseConfig, mainConfigList, mainValueChangeList, responseMap, true, this::detailTableDeal); - }else{ - Map detailTable = new HashMap<>(); - this.detailTableDeal(responseConfig,responseMap,detailTable); + } else { + // note by youhong.ai 如果没有主表配置,纯配置明细同步 + Map detailTable = new HashMap<>(); + // note by youhong.ai 处理明细表 + this.detailTableDeal(responseConfig, responseMap, detailTable); tableDefinition = TableDefinition.builder() - .detailTableMap(detailTable) - .tableDefinitionCallback(this.tableCallback) - .tableType(responseConfig.getTableType()) - .assignType(ResponseConfigConstant.NO_HANDLE) - .assignTable(responseConfig.getModelTableName()).build(); + .detailTableMap(detailTable) + .tableDefinitionCallback(this.tableCallback) + .tableType(responseConfig.getTableType()) + .assignType(ResponseConfigConstant.NO_HANDLE) + .assignTable(responseConfig.getModelTableName()).build(); } - //做数据处理 + // 做数据处理 tableDefinition.setDefaultState(stateClassKey); tableDefinition.dataProcess(); } - + + /** + *

note by youhon.ai 解析json为表描述对象

+ * + * @param tableName 表名 + * @param responseConfig 响应配置 + * @param responseConfigAliasList 别名配置 + * @param valueChangeList 值同步配置 + * @param responseMap 响应结果 + * @param isMainTable 是否主表 + * @param detailCallBack 明细处理回调 + * @return 表描述 + */ public TableDefinition parsingJsonToTable( String tableName, ResponseConfig responseConfig, @@ -106,152 +143,157 @@ public class ResponseMappingDeal { List valueChangeList, Map responseMap, boolean isMainTable, - Bi3Function,Map,String> detailCallBack) { - + Bi3Function, Map, String> detailCallBack) { + // note by youhong.ai 过滤出主表别名主数据配置 List mainAliasList = responseConfigAliasList.stream().filter(item -> item.getMainData() == ResponseConfigConstant.MAIN_DATA).collect(Collectors.toList()); - if(responseConfigAliasList.size() > 1) { + // note by youhong.ai 主表别名有且只能有一个主数据 + if (responseConfigAliasList.size() > 1) { throw new ResponseException("The master data cannot be multiple please check !!! "); } - if(responseConfigAliasList.isEmpty()){ + if (responseConfigAliasList.isEmpty()) { throw new ResponseException("The configuration is missing master data please check !!! "); } - Map aliasData = new HashMap<>(responseMap); - //处理别名数据 + Map aliasData = new HashMap<>(responseMap); + // 处理别名数据 responseConfigAliasList.forEach(item -> { String dataPath = item.getDataPath(); String dataAlias = item.getDataAlias(); + // node by youhong.ai 通过路径获取对应的值 Object parsingData = Util.getValueByKeyStr(dataPath, responseMap); - aliasData.put(dataAlias,parsingData); + // node by youhong.ai 别名->值 + aliasData.put(dataAlias, parsingData); }); - //Map> mainOrDetail = valueChangeList.stream().collect(Collectors.groupingBy(ResponseConfigValueChange::getTableType)); - //List mainValueChangeList = mainOrDetail.get(ResponseConfigConstant.MAIN_TABLE); - ResponseUtil.parameterJudgment(valueChangeList,"main table valueChangeList config is empty please check!!!"); - //获取主表的主数据信息 + // Map> mainOrDetail = valueChangeList.stream().collect(Collectors.groupingBy(ResponseConfigValueChange::getTableType)); + // List mainValueChangeList = mainOrDetail.get(ResponseConfigConstant.MAIN_TABLE); + ResponseUtil.parameterJudgment(valueChangeList, "main table valueChangeList config is empty please check!!!"); + // 获取主表的主数据信息 ResponseConfigAlias mainDataAlias = mainAliasList.get(0); int tableMainOrDetail = isMainTable ? ResponseConfigConstant.MAIN_TABLE : ResponseConfigConstant.DETAIL_TABLE; - //构建表信息 + // 构建表信息 TableDefinition tableDefinition = TableDefinition.builder().assignTable(tableName) - .conditionScript(mainDataAlias.getConditionScript()) - .judgmentScript(mainDataAlias.getJudgmentScript()) - .tableType(responseConfig.getTableType()) - .mainOrDetail(tableMainOrDetail) - .tableDefinitionCallback(this.tableCallback) - .assignType(mainDataAlias.getAssignType()).build(); + .conditionScript(mainDataAlias.getConditionScript()) + .judgmentScript(mainDataAlias.getJudgmentScript()) + .tableType(responseConfig.getTableType()) + .mainOrDetail(tableMainOrDetail) + .tableDefinitionCallback(this.tableCallback) + .assignType(mainDataAlias.getAssignType()).build(); String dataPath = mainDataAlias.getDataPath(); List rowDefinitionList = new ArrayList<>(); - Map detailTable = isMainTable ? new HashMap<>() : null; - if(mainDataAlias.getDataType() == ResponseConfigConstant.JSON_ARRAY){ - List> mainList = (List>) Util.getValueByKeyStr(dataPath, aliasData); + Map detailTable = isMainTable ? new HashMap<>() : null; + // note by youhong.ai 如果是json数组 + if (mainDataAlias.getDataType() == ResponseConfigConstant.JSON_ARRAY) { + List> mainList = (List>) Util.getValueByKeyStr(dataPath, aliasData); for (Map mainItem : mainList) { - aliasData.put(mainDataAlias.getDataAlias(),mainItem); - //处理配置信息 + aliasData.put(mainDataAlias.getDataAlias(), mainItem); + // 处理配置信息 List fieldDefinitions = parsingJsonToConfig(valueChangeList, aliasData); - RowDefinition rowDefinition = this.buildRowDefinition(fieldDefinitions,tableName, mainDataAlias.getAssignType(), mainDataAlias.getConditionScript(), mainDataAlias.getJudgmentScript()); + RowDefinition rowDefinition = this.buildRowDefinition(fieldDefinitions, tableName, mainDataAlias.getAssignType(), mainDataAlias.getConditionScript(), mainDataAlias.getJudgmentScript()); rowDefinition.setMainOrDetail(ResponseConfigConstant.DETAIL_TABLE); - if(isMainTable) { + if (isMainTable) { rowDefinition.setMainOrDetail(ResponseConfigConstant.MAIN_TABLE); - detailCallBack.apply(responseConfig,aliasData,detailTable); + detailCallBack.apply(responseConfig, aliasData, detailTable); rowDefinition.setDetailTableMap(detailTable); } rowDefinitionList.add(rowDefinition); } - }else if(mainDataAlias.getDataType() == ResponseConfigConstant.JSON_OBJECT){ - Map jsonObj = (Map) Util.getValueByKeyStr(dataPath, aliasData); - aliasData.put(mainDataAlias.getDataAlias(),jsonObj); - //处理配置信息 + } else if (mainDataAlias.getDataType() == ResponseConfigConstant.JSON_OBJECT) { + Map jsonObj = (Map) Util.getValueByKeyStr(dataPath, aliasData); + aliasData.put(mainDataAlias.getDataAlias(), jsonObj); + // 处理配置信息 List fieldDefinitions = parsingJsonToConfig(valueChangeList, aliasData); - RowDefinition rowDefinition = this.buildRowDefinition(fieldDefinitions,tableName,mainDataAlias.getAssignType(), mainDataAlias.getConditionScript(), mainDataAlias.getJudgmentScript()); + RowDefinition rowDefinition = this.buildRowDefinition(fieldDefinitions, tableName, mainDataAlias.getAssignType(), mainDataAlias.getConditionScript(), mainDataAlias.getJudgmentScript()); rowDefinition.setMainOrDetail(ResponseConfigConstant.DETAIL_TABLE); - if(isMainTable) { + if (isMainTable) { rowDefinition.setMainOrDetail(ResponseConfigConstant.MAIN_TABLE); - detailCallBack.apply(responseConfig,aliasData,detailTable); + detailCallBack.apply(responseConfig, aliasData, detailTable); rowDefinition.setDetailTableMap(detailTable); } rowDefinitionList.add(rowDefinition); - }else { + } else { throw new ResponseException("please set the primary data type !!!"); } - //设置表的行信息 + // 设置表的行信息 tableDefinition.setRowDefinitionList(rowDefinitionList); return tableDefinition; } - + private RowDefinition buildRowDefinition(List fieldDefinitionList, - String tableName, - int assignType, - String conditionScript, - String judgmentScript){ - Map updateParam = new HashMap<>(); - Map wherePram = new HashMap<>(); + String tableName, + int assignType, + String conditionScript, + String judgmentScript) { + Map updateParam = new HashMap<>(); + Map wherePram = new HashMap<>(); fieldDefinitionList.forEach(fieldDefinition -> { int fieldType = fieldDefinition.getFieldType(); - if(fieldType == ResponseConfigConstant.WHERE_FIELD){ - wherePram.put(fieldDefinition.getFieldName(),fieldDefinition.getFieldValue()); - }else { - updateParam.put(fieldDefinition.getFieldName(),fieldDefinition.getFieldValue()); - wherePram.put(fieldDefinition.getFieldName(),fieldDefinition.getFieldValue()); + if (fieldType == ResponseConfigConstant.WHERE_FIELD) { + wherePram.put(fieldDefinition.getFieldName(), fieldDefinition.getFieldValue()); + } else { + updateParam.put(fieldDefinition.getFieldName(), fieldDefinition.getFieldValue()); + wherePram.put(fieldDefinition.getFieldName(), fieldDefinition.getFieldValue()); } }); return RowDefinition.builder().assignType(assignType) - .conditionScript(conditionScript) - .updateParam(updateParam) - .assignTable(tableName) - .whereParam(wherePram) - .fieldDefinitionList(fieldDefinitionList) - .rowDefinitionCallback(this.rowCallback) - .judgmentScript(judgmentScript).build(); + .conditionScript(conditionScript) + .updateParam(updateParam) + .assignTable(tableName) + .whereParam(wherePram) + .fieldDefinitionList(fieldDefinitionList) + .rowDefinitionCallback(this.rowCallback) + .judgmentScript(judgmentScript).build(); } - - private String detailTableDeal(ResponseConfig responseConfig,Map param,Map detailTable){ + + private String detailTableDeal(ResponseConfig responseConfig, Map param, Map detailTable) { List responseConfigAliasList = responseConfig.getResponseConfigAliasList(); List valueChangeList = responseConfig.getValueChangeList(); - //数据信息按是否主表分组 + // 数据信息按是否主表分组 Map> aliasMap = responseConfigAliasList.stream().collect(Collectors.groupingBy(ResponseConfigAlias::getTableType)); List detailAliases = aliasMap.get(ResponseConfigConstant.DETAIL_TABLE); - //没有明细相关配置不处理 - if(ResponseUtil.parameterIsNull(detailAliases)){ + // 没有明细相关配置不处理 + if (ResponseUtil.parameterIsNull(detailAliases)) { return ""; } Map> detailAliasMap = detailAliases.stream().collect(Collectors.groupingBy(ResponseConfigAlias::getDetailTableName)); Map> detailValueChangeMap = valueChangeList.stream().collect(Collectors.groupingBy(ResponseConfigValueChange::getAssignTable)); - detailAliasMap.forEach((key,value) ->{ + detailAliasMap.forEach((key, value) -> { List detailValueChangeList = detailValueChangeMap.get(key); TableDefinition tableDefinition = parsingJsonToTable(key, responseConfig, value, detailValueChangeList, param, false, this::detailTableDeal); - detailTable.put(key,tableDefinition); + detailTable.put(key, tableDefinition); }); return ""; } - + /** * 将json参数解析成对应的字段描述信息 + * * @param responseConfigValueChangeList 配置信息 - * @param parsingData json信息 + * @param parsingData json信息 * @return 字段描述信息 */ public List parsingJsonToConfig(List responseConfigValueChangeList, - Map parsingData){ - Map param = new HashMap<>(); - responseConfigValueChangeList.forEach(item ->{ + Map parsingData) { + Map param = new HashMap<>(); + responseConfigValueChangeList.forEach(item -> { Object value = item.getDataChange() == ResponseConfigConstant.DEFAULT_VALUE - ? null - : Util.getValueByKeyStr(Util.null2String(item.getValuePath()), parsingData); + ? null + : Util.getValueByKeyStr(Util.null2String(item.getValuePath()), parsingData); item.setJsonData(value); - param.put(item.getAssignFieldName(),value); + param.put(item.getAssignFieldName(), value); }); - return responseConfigValueChangeList.stream().map(item ->{ + return responseConfigValueChangeList.stream().map(item -> { BiFunction, String> dataChangeFunction = DataChangeProcess.MODE_METHOD_MAP.get(item.getDataChange()); - ResponseUtil.parameterJudgment(dataChangeFunction,"The corresponding data conversion mode processing method is not found !!!"); - dataChangeFunction.apply(item,param); + ResponseUtil.parameterJudgment(dataChangeFunction, "The corresponding data conversion mode processing method is not found !!!"); + dataChangeFunction.apply(item, param); Function typeChangeFunction = TypeChangeProcess.MODE_METHOD_MAP.get(item.getTypeChange()); - ResponseUtil.parameterJudgment(typeChangeFunction,"The corresponding type conversion method could not be found !!!"); + ResponseUtil.parameterJudgment(typeChangeFunction, "The corresponding type conversion method could not be found !!!"); Object targetValue = typeChangeFunction.apply(item.getJsonData()); return FieldDefinition.builder().fieldType(item.getFieldType()) - .tableType(item.getTableType()) - .tableName(item.getAssignTable()) - .fieldName(item.getAssignFieldName()) - .fieldDefinitionCallback(fieldDefinitionCallback) - .fieldValue(targetValue).build(); + .tableType(item.getTableType()) + .tableName(item.getAssignTable()) + .fieldName(item.getAssignFieldName()) + .fieldDefinitionCallback(fieldDefinitionCallback) + .fieldValue(targetValue).build(); }).collect(Collectors.toList()); } - + }