diff --git a/src/main/java/weaver/bokang/xiao/xhny_report/mapper/SourceTrackingMapper.java b/src/main/java/weaver/bokang/xiao/xhny_report/mapper/SourceTrackingMapper.java index bfddab0..7097012 100644 --- a/src/main/java/weaver/bokang/xiao/xhny_report/mapper/SourceTrackingMapper.java +++ b/src/main/java/weaver/bokang/xiao/xhny_report/mapper/SourceTrackingMapper.java @@ -67,6 +67,8 @@ public interface SourceTrackingMapper { @CollectionMethod(3) List queryQuotaList(@ParamMapper("sourceTackingCode")String sourceTackingCode); + + /** *

删除报表数据

* @param ids 主键id数组 diff --git a/src/main/java/weaver/xiao/commons/config/service/DealWithMapping.java b/src/main/java/weaver/xiao/commons/config/service/DealWithMapping.java index 7b1f579..cbbb8fb 100644 --- a/src/main/java/weaver/xiao/commons/config/service/DealWithMapping.java +++ b/src/main/java/weaver/xiao/commons/config/service/DealWithMapping.java @@ -62,7 +62,7 @@ public class DealWithMapping extends ToolUtil { private DealWithMapper mapper = null; - private final Logger logger = aiyh.utils.Util.getLogger("json-util"); + private final Logger logger = aiyh.utils.Util.getLogger("json_util"); { try { diff --git a/src/test/java/bokang/xiao/NormalTest.java b/src/test/java/bokang/xiao/NormalTest.java index 3957a7b..ee14f66 100644 --- a/src/test/java/bokang/xiao/NormalTest.java +++ b/src/test/java/bokang/xiao/NormalTest.java @@ -9,6 +9,7 @@ import basetest.BaseTest; import bokang.xiao.entity.CourseEntity; import bokang.xiao.entity.StudentEntity; import bokang.xiao.entity.TeacherEntity; +import bokang.xiao.response_deal.ResponseMappingDeal; import bokang.xiao.util.XmlParser; import cn.afterturn.easypoi.excel.ExcelExportUtil; import cn.afterturn.easypoi.excel.entity.ExportParams; @@ -62,6 +63,7 @@ import weaver.interfaces.datasource.DataSource; import weaver.workflow.workflow.WfForceOver; import weaver.workflow.workflow.WorkflowVersion; import weaver.xiao.commons.utils.DocImageFileUtil; +import youhong.ai.utiltest.resultjsonparase.ParseTest; import java.io.*; import java.lang.reflect.Constructor; @@ -120,6 +122,15 @@ public class NormalTest extends BaseTest { examineSchedule.runCode(); } + @Test + public void testResponseSync(){ + ParseTest parseTest = new ParseTest(); + Map resultData = parseTest.getResultData(); + System.out.println(JSON.toJSONString(resultData)); + ResponseMappingDeal responseMappingDeal = new ResponseMappingDeal(); + responseMappingDeal.doResponseSync("test1",resultData); + } + @Test public void paramGet(){ Map param = new HashMap<>(); diff --git a/src/test/java/bokang/xiao/response_deal/ResponseMappingDeal.java b/src/test/java/bokang/xiao/response_deal/ResponseMappingDeal.java index 87548b6..6d36c07 100644 --- a/src/test/java/bokang/xiao/response_deal/ResponseMappingDeal.java +++ b/src/test/java/bokang/xiao/response_deal/ResponseMappingDeal.java @@ -1,19 +1,24 @@ package bokang.xiao.response_deal; -import aiyh.utils.ScriptUtil; import aiyh.utils.Util; -import aiyh.utils.excention.CustomerException; +import aiyh.utils.function.Bi3Function; import aiyh.utils.httpUtil.ResponeVo; -import aiyh.utils.tool.cn.hutool.core.collection.CollectionUtil; -import aiyh.utils.tool.cn.hutool.core.util.StrUtil; import bokang.xiao.exception.ResponseException; -import bokang.xiao.response_deal.entity.ResponseConfig; -import bokang.xiao.response_deal.entity.ResponseConfigAlias; -import bokang.xiao.response_deal.entity.ResponseConfigValueChange; +import bokang.xiao.response_deal.constant.ResponseConfigConstant; +import bokang.xiao.response_deal.entity.*; +import bokang.xiao.response_deal.intfaces.FieldDefinitionCallback; +import bokang.xiao.response_deal.intfaces.RowDefinitionCallback; +import bokang.xiao.response_deal.intfaces.TableDefinitionCallback; import bokang.xiao.response_deal.mapper.ConfigMapper; +import bokang.xiao.response_deal.process.DataChangeProcess; +import bokang.xiao.response_deal.process.TypeChangeProcess; +import bokang.xiao.response_deal.util.ResponseUtil; +import lombok.Setter; import org.apache.log4j.Logger; import java.util.*; +import java.util.function.BiFunction; +import java.util.function.Function; import java.util.stream.Collectors; /** @@ -22,68 +27,28 @@ import java.util.stream.Collectors; * @Date 2023/7/4 17:42 * @Description

响应信息处理类

**/ +@Setter public class ResponseMappingDeal { - private final Logger logger = Util.getLogger("json-util"); + private final Logger logger = Util.getLogger("json_util"); private final ConfigMapper configMapper = Util.getMapper(ConfigMapper.class); - - - public void parseConfig(String uniqueCode, ResponeVo responeVo) { - if (responeVo.getCode() != 200) { - throw new CustomerException("你妈没成功"); - } - - List responseConfigs = configMapper.queryResponseConfigByUnique(uniqueCode); - - if (CollectionUtil.isEmpty(responseConfigs)) { - throw new CustomerException("sb"); - } - for (ResponseConfig responseConfig : responseConfigs) { - parseConfigOne(responseConfig, responeVo); - } - } - - private void parseConfigOne(ResponseConfig responseConfig, ResponeVo responeVo) { - List responseConfigAliasList = responseConfig.getResponseConfigAliasList(); - String successExp = responseConfig.getSuccessExp(); + + private TableDefinitionCallback tableCallback; + + private RowDefinitionCallback rowCallback; + + private FieldDefinitionCallback fieldDefinitionCallback; + + public void doResponseSync(String uniqueCode, ResponeVo responeVo) { Map responseMap = responeVo.getResponseMap(); - List> resultList = new ArrayList<>(); - if (CollectionUtil.isEmpty(responseMap)) { - // 他是一个list,尝试获取一下 - List result = responeVo.getResult(); - if (CollectionUtil.isEmpty(result)) { - throw new CustomerException("没有响应结果,你同步个J8"); - } else { - for (Map map : result) { - Map tempMap = new HashMap<>(); - for (Object entry : map.entrySet()) { - - } - resultList.add(tempMap); - } - } - } else { - resultList.add(responseMap); - } - if (StrUtil.isNotBlank(successExp)) { - for (Map map : resultList) { - boolean flag = (boolean) ScriptUtil.invokeScript(successExp, map); - if (!flag) { - throw new CustomerException("失败了!"); - } - } - } - - + ResponseUtil.parameterJudgment(responseMap,"接口返回都为空 你处理个勾"); + this.doResponseSync(uniqueCode,responseMap); } - - - public void getConfig(String uniqueCode, ResponeVo responeVo) { + + public void doResponseSync(String uniqueCode, Map responseMap) { List responseConfigList = configMapper.queryResponseConfigByUnique(uniqueCode); - if (Objects.isNull(responseConfigList) || responseConfigList.isEmpty()) { - throw new ResponseException("response config is empty please check!!! "); - } + ResponseUtil.parameterJudgment(responseConfigList,"response config is empty please check!!! "); Map tableNameConfig; try { tableNameConfig = responseConfigList.stream().collect(Collectors.toMap(ResponseConfig::getModelTableName, v -> v)); @@ -91,51 +56,187 @@ public class ResponseMappingDeal { logger.error("response config error please check!!! " + Util.getErrString(e)); throw new ResponseException("response config error please check!!! "); } - tableNameConfig.entrySet().stream().forEach(item -> { - dealConfig(item.getValue(), responeVo); - }); + ResponseUtil.parameterJudgment(responseMap,"接口返回都为空 你处理个勾"); + tableNameConfig.forEach((key, value) -> doResponseSync(value, responseMap)); } - - public void dealConfig(ResponseConfig responseConfig, ResponeVo responeVo) { + + public void doResponseSync(ResponseConfig responseConfig, Map responseMap){ + List responseConfigAliasList = responseConfig.getResponseConfigAliasList(); + ResponseUtil.parameterJudgment(responseConfigAliasList,"responseConfigAliasList config is empty please check!!!"); List valueChangeList = responseConfig.getValueChangeList(); - if (Objects.isNull(valueChangeList) || valueChangeList.isEmpty()) { - throw new ResponseException("valueChangeList config is empty please check!!!"); + ResponseUtil.parameterJudgment(valueChangeList,"valueChangeList config is empty please check!!!"); + //数据信息按是否主表分组 + Map> aliasMap = responseConfigAliasList.stream().collect(Collectors.groupingBy(ResponseConfigAlias::getTableType)); + List mainConfigList = aliasMap.get(ResponseConfigConstant.MAIN_TABLE); + Map> mainOrDetail = valueChangeList.stream().collect(Collectors.groupingBy(ResponseConfigValueChange::getTableType)); + List mainValueChangeList = mainOrDetail.get(ResponseConfigConstant.MAIN_TABLE); + TableDefinition tableDefinition; + if(ResponseUtil.parameterIsNotNull(mainConfigList)) { + tableDefinition = this.parsingJsonToTable(responseConfig.getModelTableName(), responseConfig, mainConfigList, mainValueChangeList, responseMap, true, this::detailTableDeal); + }else{ + Map detailTable = new HashMap<>(); + this.detailTableDeal(responseConfig,responseMap,detailTable); + tableDefinition = TableDefinition.builder() + .detailTableMap(detailTable) + .tableType(responseConfig.getTableType()) + .assignType(ResponseConfigConstant.NO_HANDLE) + .assignTable(responseConfig.getModelTableName()).build(); } - Map> tableTypeMap = valueChangeList.stream().collect(Collectors.groupingBy(ResponseConfigValueChange::getTableType)); - List mainConfig = tableTypeMap.get(0); - Map> collect = mainConfig.stream().collect(Collectors.groupingBy(ResponseConfigValueChange::getAssignTable)); - if (collect.size() > 1) { - throw new ResponseException("The primary table cannot have details please check!!! "); + //做数据处理 + tableDefinition.setDefaultState(); + tableDefinition.dataProcess(); + } + + public TableDefinition parsingJsonToTable( + String tableName, + ResponseConfig responseConfig, + List responseConfigAliasList, + List valueChangeList, + Map responseMap, + boolean isMainTable, + Bi3Function,Map,String> detailCallBack) { + + List mainAliasList = responseConfigAliasList.stream().filter(item -> item.getMainData() == ResponseConfigConstant.MAIN_DATA).collect(Collectors.toList()); + if(responseConfigAliasList.size() > 1) { + throw new ResponseException("The master data cannot be multiple please check !!! "); } - // Map responseMap = responeVo.getResponseMap(); - // Map> mainFieldMap = new HashMap<>(); - // for (ResponseConfigValueChange responseConfigValueChange : mainConfig) { - // String valuePath = responseConfigValueChange.getValuePath(); - // Util.ValueOrList valueOrListByKeyStr = Util.getValueOrListByKeyStr(valuePath, responseMap); - // if(valueOrListByKeyStr.isArray() && !mainFieldMap.isEmpty() && !mainFieldMap.containsKey(valueOrListByKeyStr)){ - // throw new ResponseException("The primary table cannot contain multiple List data please check!!!"); - // }else if(valueOrListByKeyStr.isArray() && mainFieldMap.isEmpty()){ - // List> listValue = (List>) valueOrListByKeyStr.getValue(); - // List fieldDefinitions = listValue.stream().map(item -> FieldDefinition.builder().build()).collect(Collectors.toList()); - // mainFieldMap.put(valueOrListByKeyStr,fieldDefinitions); - // }else if(valueOrListByKeyStr.isArray()){ - // List fieldDefinitions = mainFieldMap.get(valueOrListByKeyStr); - // } - //} - // mainConfig.stream().map(item ->{ - // - //}) - List detail = tableTypeMap.get(1); - Map> tableValueMap = valueChangeList.stream().collect(Collectors.groupingBy(ResponseConfigValueChange::getAssignTable)); - - + if(responseConfigAliasList.isEmpty()){ + throw new ResponseException("The configuration is missing master data please check !!! "); + } + Map aliasData = new HashMap<>(responseMap); + //处理别名数据 + responseConfigAliasList.forEach(item -> { + String dataPath = item.getDataPath(); + String dataAlias = item.getDataAlias(); + Object parsingData = Util.getValueByKeyStr(dataPath, responseMap); + 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!!!"); + //获取主表的主数据信息 + 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) + .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); + for (Map mainItem : mainList) { + aliasData.put(mainDataAlias.getDataAlias(),mainItem); + //处理配置信息 + List fieldDefinitions = parsingJsonToConfig(valueChangeList, aliasData); + RowDefinition rowDefinition = this.buildRowDefinition(fieldDefinitions,tableName, mainDataAlias.getAssignType(), mainDataAlias.getConditionScript(), mainDataAlias.getJudgmentScript()); + rowDefinition.setMainOrDetail(ResponseConfigConstant.DETAIL_TABLE); + if(isMainTable) { + rowDefinition.setMainOrDetail(ResponseConfigConstant.MAIN_TABLE); + 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); + //处理配置信息 + List fieldDefinitions = parsingJsonToConfig(valueChangeList, aliasData); + RowDefinition rowDefinition = this.buildRowDefinition(fieldDefinitions,tableName,mainDataAlias.getAssignType(), mainDataAlias.getConditionScript(), mainDataAlias.getJudgmentScript()); + rowDefinition.setMainOrDetail(ResponseConfigConstant.DETAIL_TABLE); + if(isMainTable) { + rowDefinition.setMainOrDetail(ResponseConfigConstant.MAIN_TABLE); + detailCallBack.apply(responseConfig,aliasData,detailTable); + rowDefinition.setDetailTableMap(detailTable); + } + rowDefinitionList.add(rowDefinition); + }else { + throw new ResponseException("please set the primary data type !!!"); + } + //设置表的行信息 + tableDefinition.setRowDefinitionList(rowDefinitionList); + return tableDefinition; } - - public void responseToEntity(ResponseConfigValueChange responseConfigValueChange, Map param) { - String valuePath = responseConfigValueChange.getValuePath(); - Util.ValueOrList valueOrListByKeyStr = Util.getValueOrListByKeyStr(valuePath, param); - + + private RowDefinition buildRowDefinition(List fieldDefinitionList, + 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()); + } + }); + return RowDefinition.builder().assignType(assignType) + .conditionScript(conditionScript) + .updateParam(updateParam) + .assignTable(tableName) + .whereParam(wherePram) + .fieldDefinitionList(fieldDefinitionList) + .judgmentScript(judgmentScript).build(); } - - + + 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)){ + return ""; + } + Map> detailAliasMap = detailAliases.stream().collect(Collectors.groupingBy(ResponseConfigAlias::getDetailTableName)); + Map> detailValueChangeMap = valueChangeList.stream().collect(Collectors.groupingBy(ResponseConfigValueChange::getAssignTable)); + detailAliasMap.forEach((key,value) ->{ + List detailValueChangeList = detailValueChangeMap.get(key); + TableDefinition tableDefinition = parsingJsonToTable(key, responseConfig, value, detailValueChangeList, param, false, this::detailTableDeal); + detailTable.put(key,tableDefinition); + }); + return ""; + } + + /** + * 将json参数解析成对应的字段描述信息 + * @param responseConfigValueChangeList 配置信息 + * @param parsingData json信息 + * @return 字段描述信息 + */ + public List parsingJsonToConfig(List responseConfigValueChangeList, + Map parsingData){ + Map param = new HashMap<>(); + responseConfigValueChangeList.forEach(item ->{ + Object value = item.getDataChange() == ResponseConfigConstant.DEFAULT_VALUE + ? null + : Util.getValueByKeyStr(Util.null2String(item.getValuePath()), parsingData); + item.setJsonData(value); + param.put(item.getAssignFieldName(),value); + }); + 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); + Function typeChangeFunction = TypeChangeProcess.MODE_METHOD_MAP.get(item.getTypeChange()); + 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()) + .fieldValue(targetValue).build(); + }).collect(Collectors.toList()); + } + } diff --git a/src/test/java/bokang/xiao/response_deal/constant/ResponseConfigConstant.java b/src/test/java/bokang/xiao/response_deal/constant/ResponseConfigConstant.java new file mode 100644 index 0000000..fd3bd99 --- /dev/null +++ b/src/test/java/bokang/xiao/response_deal/constant/ResponseConfigConstant.java @@ -0,0 +1,56 @@ +package bokang.xiao.response_deal.constant; + +/** + * @ClassName ResponseConfigConstant + * @Author 肖博亢 + * @Date 2023/7/7 10:23 + * @Description

+ **/ +public class ResponseConfigConstant { + + /** 主数据 */ + public static int MAIN_DATA = 0; + + /** 主表 */ + public static int MAIN_TABLE = 0; + + /** 明细表 */ + public static int DETAIL_TABLE = 1; + + /** json数组 */ + public static int JSON_ARRAY = 0; + + /** json对象 */ + public static int JSON_OBJECT = 1; + + /** 默认值 */ + public static int DEFAULT_VALUE = 0; + + /** 表数据无需处理 */ + public static int NO_HANDLE = -1; + + /** 表数据新增 */ + public static int INSERT = 0; + + /** 表数据更新 */ + public static int UPDATE = 1; + + /** 表数据新增或更新 */ + public static int INSERT_OR_UPDATE = 2; + + /** 流程表 */ + public static int WORKFLOW_TABLE = 1; + + /** 建模表 */ + public static int MODEL_TABLE = 0; + + /** 自定义表 */ + public static int CUS_TABLE = 2; + + /** 更新字段 */ + public static int UPDATE_FIELD = 0; + + /** 条件字段 */ + public static int WHERE_FIELD = 1; + +} diff --git a/src/test/java/bokang/xiao/response_deal/entity/FieldDefinition.java b/src/test/java/bokang/xiao/response_deal/entity/FieldDefinition.java new file mode 100644 index 0000000..70561ae --- /dev/null +++ b/src/test/java/bokang/xiao/response_deal/entity/FieldDefinition.java @@ -0,0 +1,38 @@ +package bokang.xiao.response_deal.entity; + +import bokang.xiao.response_deal.intfaces.FieldDefinitionCallback; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * @ClassName FieldDefinition + * @Author 肖博亢 + * @Date 2023/7/7 14:05 + * @Description

+ **/ +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class FieldDefinition { + + /** 字段名 */ + private String fieldName; + + /** 字段类型 */ + private int fieldType; + + /** 字段值 */ + private Object fieldValue; + + /** 所属表名 */ + private String tableName; + + /** 表类型 */ + private int tableType; + + private FieldDefinitionCallback fieldDefinitionCallback; + +} diff --git a/src/test/java/bokang/xiao/response_deal/entity/ResponseConfig.java b/src/test/java/bokang/xiao/response_deal/entity/ResponseConfig.java index fb00a49..9c0b572 100644 --- a/src/test/java/bokang/xiao/response_deal/entity/ResponseConfig.java +++ b/src/test/java/bokang/xiao/response_deal/entity/ResponseConfig.java @@ -5,7 +5,6 @@ import aiyh.utils.annotation.recordset.SqlOracleDbFieldAnn; import java.util.List; - /** * @ClassName ResponseConfigMain * @Author 肖博亢 @@ -17,26 +16,30 @@ import java.util.List; public class ResponseConfig { /** 请求唯一标识 */ - @SqlOracleDbFieldAnn("request_unique") + @SqlOracleDbFieldAnn("REQUEST_UNIQUE") private String requestUnique; /** 配置描述 */ - @SqlOracleDbFieldAnn("config_desc") + @SqlOracleDbFieldAnn("CONFIG_DESC") private String configDesc; /** 表类型 */ - @SqlOracleDbFieldAnn("table_type") - private String tableType; + @SqlOracleDbFieldAnn("TABLE_TYPE") + private int tableType; /** 模型表 */ - @SqlOracleDbFieldAnn("model_table") + @SqlOracleDbFieldAnn("MODEL_TABLE") private String modelTable; /** 模型表名称 */ - @SqlOracleDbFieldAnn("model_table_name") + @SqlOracleDbFieldAnn("MODEL_TABLE_NAME") private String modelTableName; - - private String successExp; + + /** 数据根路径 */ + @SqlOracleDbFieldAnn("ROOT_PATH") + private String rootPath; + + private String successExp; private List responseConfigAliasList; diff --git a/src/test/java/bokang/xiao/response_deal/entity/ResponseConfigAlias.java b/src/test/java/bokang/xiao/response_deal/entity/ResponseConfigAlias.java index 33dfd58..b0a90d4 100644 --- a/src/test/java/bokang/xiao/response_deal/entity/ResponseConfigAlias.java +++ b/src/test/java/bokang/xiao/response_deal/entity/ResponseConfigAlias.java @@ -3,6 +3,9 @@ package bokang.xiao.response_deal.entity; import lombok.Data; import aiyh.utils.annotation.recordset.SqlOracleDbFieldAnn; +import java.util.HashMap; +import java.util.Map; + /** * @ClassName ResponseConfigDtAlias * @Author 肖博亢 @@ -13,26 +16,49 @@ import aiyh.utils.annotation.recordset.SqlOracleDbFieldAnn; public class ResponseConfigAlias { /** 数据路径 */ - @SqlOracleDbFieldAnn("data_path") + @SqlOracleDbFieldAnn("DATA_PATH") private String dataPath; /** 数据类型 */ - @SqlOracleDbFieldAnn("data_type") - private String dataType; + @SqlOracleDbFieldAnn("DATA_TYPE") + private int dataType; /** 数据别名 */ - @SqlOracleDbFieldAnn("data_alias") + @SqlOracleDbFieldAnn("DATA_ALIAS") private String dataAlias; - /** 数据变更 */ - @SqlOracleDbFieldAnn("data_change") - private String dataChange; + /** 表类型 */ + @SqlOracleDbFieldAnn("TABLE_TYPE") + private int tableType; - /** 自定义文本 */ - @SqlOracleDbFieldAnn("cus_text") - private String cusText; + /** 明细表 */ + @SqlOracleDbFieldAnn("DETAIL_TABLE") + private String detailTable; + + /** 明细表名 */ + @SqlOracleDbFieldAnn("DETAIL_TABLE_NAME") + private String detailTableName; + + /** 是否主数据 */ + @SqlOracleDbFieldAnn("MAIN_DATA") + private int mainData; + + /** 是否为拆分 */ + @SqlOracleDbFieldAnn("SPLIT_FLAG") + private int splitFlag; + + /** 判断表达式 */ + @SqlOracleDbFieldAnn("JUDGMENT_SCRIPT") + private String judgmentScript; + + /** 条件表达式 */ + @SqlOracleDbFieldAnn("CONDITION_SCRIPT") + private String conditionScript; + + /** 数据写入方式 */ + @SqlOracleDbFieldAnn("ASSIGN_TYPE") + private int assignType; + + private Map parsingData = new HashMap<>(); - /** 类型变更 */ - @SqlOracleDbFieldAnn("type_change") - private String typeChange; } diff --git a/src/test/java/bokang/xiao/response_deal/entity/ResponseConfigValueChange.java b/src/test/java/bokang/xiao/response_deal/entity/ResponseConfigValueChange.java index 2f1a7ca..f0bedfe 100644 --- a/src/test/java/bokang/xiao/response_deal/entity/ResponseConfigValueChange.java +++ b/src/test/java/bokang/xiao/response_deal/entity/ResponseConfigValueChange.java @@ -16,56 +16,47 @@ import java.util.regex.Pattern; public class ResponseConfigValueChange { /** 值路径 */ - @SqlOracleDbFieldAnn("value_path") + @SqlOracleDbFieldAnn("VALUE_PATH") private String valuePath; /** 字段类型 */ - @SqlOracleDbFieldAnn("field_type") + @SqlOracleDbFieldAnn("FIELD_TYPE") private int fieldType; /** 分配字段 */ - @SqlOracleDbFieldAnn("assign_field") + @SqlOracleDbFieldAnn("ASSIGN_FIELD") private String assignField; /** 分配表 */ - @SqlOracleDbFieldAnn("assign_table") + @SqlOracleDbFieldAnn("ASSIGN_TABLE") private String assignTable; /** 表类型 */ - @SqlOracleDbFieldAnn("table_type") + @SqlOracleDbFieldAnn("TABLE_TYPE") private int tableType; - public void setTableType(int tableType){ - this.tableType = tableType == -1 ? this.judgmentIsDetail(this.assignTable) : tableType; - } - /** 分配字段名 */ - @SqlOracleDbFieldAnn("assign_field_name") + @SqlOracleDbFieldAnn("ASSIGN_FIELD_NAME") private String assignFieldName; /** 分配类型 */ - @SqlOracleDbFieldAnn("assign_type") + @SqlOracleDbFieldAnn("ASSIGN_TYPE") private int assignType; /** 数据变更 */ - @SqlOracleDbFieldAnn("data_change") + @SqlOracleDbFieldAnn("DATA_CHANGE") private int dataChange; /** 类型变更 */ - @SqlOracleDbFieldAnn("type_change") + @SqlOracleDbFieldAnn("TYPE_CHANGE") private int typeChange; /** 自定义文本 */ - @SqlOracleDbFieldAnn("cus_text") + @SqlOracleDbFieldAnn("CUS_TEXT") private String cusText; - /** 判断脚本 */ - @SqlOracleDbFieldAnn("judgment_script") - private String judgmentScript; - - /** 条件脚本 */ - @SqlOracleDbFieldAnn("condition_script") - private String conditionScript; + /** 接口响应值 */ + private Object jsonData; public int judgmentIsDetail(String tableName){ Pattern compiledPattern = Pattern.compile("dt_\\d+$"); diff --git a/src/test/java/bokang/xiao/response_deal/entity/RowDefinition.java b/src/test/java/bokang/xiao/response_deal/entity/RowDefinition.java new file mode 100644 index 0000000..59aa019 --- /dev/null +++ b/src/test/java/bokang/xiao/response_deal/entity/RowDefinition.java @@ -0,0 +1,51 @@ +package bokang.xiao.response_deal.entity; + +import bokang.xiao.response_deal.intfaces.RowDefinitionCallback; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; +import java.util.Map; + +/** + * @ClassName RowDefinition + * @Author 肖博亢 + * @Date 2023/7/8 16:54 + * @Description

+ **/ +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class RowDefinition { + + /** 数据ID执行sql后返回 */ + private int dataId; + + /** 赋值表名 **/ + private String assignTable; + + /** 主表行还是明细行 */ + private int mainOrDetail; + + /** 判断表达式 */ + private String judgmentScript; + + /** 条件表达式 */ + private String conditionScript; + + /** 数据写入方式 */ + private int assignType; + + private RowDefinitionCallback rowDefinitionCallback; + + private Map updateParam; + + private Map whereParam; + + Map detailTableMap; + + private List fieldDefinitionList; +} diff --git a/src/test/java/bokang/xiao/response_deal/entity/TableDefinition.java b/src/test/java/bokang/xiao/response_deal/entity/TableDefinition.java new file mode 100644 index 0000000..d8d8e39 --- /dev/null +++ b/src/test/java/bokang/xiao/response_deal/entity/TableDefinition.java @@ -0,0 +1,66 @@ +package bokang.xiao.response_deal.entity; + +import bokang.xiao.response_deal.intfaces.TableDefinitionCallback; +import bokang.xiao.response_deal.state.State; +import bokang.xiao.response_deal.state.StateFactory; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; +import java.util.Map; + +/** + * @ClassName TableDefinition + * @Author 肖博亢 + * @Date 2023/7/7 14:30 + * @Description

+ **/ +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class TableDefinition { + + /** 赋值表名 **/ + private String assignTable; + + /** 表类型 0:建模表;1:流程表;2:自定义表 */ + private int tableType; + + /** 主表还是明细表 */ + private int mainOrDetail; + + /** 判断表达式 */ + private String judgmentScript; + + /** 条件表达式 */ + private String conditionScript; + + /** 数据写入方式 */ + private int assignType; + + /** 数据状态 */ + private State state; + + private TableDefinitionCallback tableDefinitionCallback; + + private List rowDefinitionList; + + private Map detailTableMap; + + public void setState(State state) { + this.state = state; + this.state.setContext(this); + } + + public void setDefaultState() { + this.state = StateFactory.createState(this.getAssignType()); + this.state.setContext(this); + } + + public void dataProcess(){ + this.state.handle(); + } +} diff --git a/src/test/java/bokang/xiao/response_deal/intfaces/DataChangeInterface.java b/src/test/java/bokang/xiao/response_deal/intfaces/DataChangeInterface.java new file mode 100644 index 0000000..24260c9 --- /dev/null +++ b/src/test/java/bokang/xiao/response_deal/intfaces/DataChangeInterface.java @@ -0,0 +1,23 @@ +package bokang.xiao.response_deal.intfaces; + +import bokang.xiao.response_deal.entity.ResponseConfigValueChange; + +import java.util.Map; + +/** + * @ClassName DataChangeInterface + * @Author 肖博亢 + * @Date 2023/7/7 12:10 + * @Description

+ **/ +public interface DataChangeInterface { + + /** + * 自定义转换接口 + * @param responseConfigValueChange 配置信息 + * @param param 参数信息 + * @param pathParam 路径参数 + * @return 转换后的值 + */ + Object change(ResponseConfigValueChange responseConfigValueChange, Map param,Map pathParam); +} diff --git a/src/test/java/bokang/xiao/response_deal/intfaces/FieldDefinitionCallback.java b/src/test/java/bokang/xiao/response_deal/intfaces/FieldDefinitionCallback.java new file mode 100644 index 0000000..e096b3e --- /dev/null +++ b/src/test/java/bokang/xiao/response_deal/intfaces/FieldDefinitionCallback.java @@ -0,0 +1,14 @@ +package bokang.xiao.response_deal.intfaces; + +import bokang.xiao.response_deal.entity.FieldDefinition; + +/** + * @ClassName FieldDefinitionCallback + * @Author 肖博亢 + * @Date 2023/7/10 13:38 + * @Description

+ **/ +public interface FieldDefinitionCallback { + + void fieldHandle(FieldDefinition fieldDefinition); +} diff --git a/src/test/java/bokang/xiao/response_deal/intfaces/RowDefinitionCallback.java b/src/test/java/bokang/xiao/response_deal/intfaces/RowDefinitionCallback.java new file mode 100644 index 0000000..7c44848 --- /dev/null +++ b/src/test/java/bokang/xiao/response_deal/intfaces/RowDefinitionCallback.java @@ -0,0 +1,16 @@ +package bokang.xiao.response_deal.intfaces; + +import bokang.xiao.response_deal.entity.RowDefinition; + +/** + * @ClassName RowDefinitionCallback + * @Author 肖博亢 + * @Date 2023/7/10 13:36 + * @Description

+ **/ +public interface RowDefinitionCallback { + + void rowHandle(RowDefinition rowDefinition); + + boolean judgmentRepetition(RowDefinition rowDefinition); +} diff --git a/src/test/java/bokang/xiao/response_deal/intfaces/TableDefinitionCallback.java b/src/test/java/bokang/xiao/response_deal/intfaces/TableDefinitionCallback.java new file mode 100644 index 0000000..4b1dc00 --- /dev/null +++ b/src/test/java/bokang/xiao/response_deal/intfaces/TableDefinitionCallback.java @@ -0,0 +1,15 @@ +package bokang.xiao.response_deal.intfaces; + +import bokang.xiao.response_deal.entity.TableDefinition; + +/** + * @ClassName DataCallbackInterface + * @Author 肖博亢 + * @Date 2023/7/10 13:04 + * @Description

+ **/ +public interface TableDefinitionCallback { + + void tableHandle(TableDefinition tableDefinition); + +} diff --git a/src/test/java/bokang/xiao/response_deal/mapper/ConfigMapper.java b/src/test/java/bokang/xiao/response_deal/mapper/ConfigMapper.java index 5327489..24b80bb 100644 --- a/src/test/java/bokang/xiao/response_deal/mapper/ConfigMapper.java +++ b/src/test/java/bokang/xiao/response_deal/mapper/ConfigMapper.java @@ -6,6 +6,7 @@ import bokang.xiao.response_deal.entity.ResponseConfigAlias; import bokang.xiao.response_deal.entity.ResponseConfigValueChange; import java.util.List; +import java.util.Map; /** * @ClassName ConfigMapper @@ -48,14 +49,56 @@ public interface ConfigMapper { @CollectionMethod(1) List queryResponseConfigAlias(@ParamMapper("mainId")int mainId); - /** *

查询响应数据转换配置信息

* @param mainId 主数据id * @return 响应数据转换配置信息列表 */ - @Select("select * from uf_response_config_dt2 where mainid = #{mainId}") + @Select("select * from uf_response_config_dt3 where mainid = #{mainId}") @CollectionMethod(2) List queryResponseConfigValueChange(@ParamMapper("mainId")int mainId); + /** + *

执行自定义查询sql

+ * @param cusSql 自定义sql + * @param param 参数信息 + * @return 查询结果 + */ + @Select(custom = true) + String executeCusQuerySql(@SqlString String cusSql,@ParamMapper("whereItem") Map whereParam,@ParamMapper("param")Map param); + + /** + *

执行自定义更新sql

+ * @param cusSql 自定义sql + * @param updateParam 更新参数信息 + * @param whereParam 条件参数信息 + * @return 查询结果 + */ + @Update(custom = true) + boolean executeUpdateCusSql(@SqlString String cusSql, + @ParamMapper("updateParam")Map updateParam, + @ParamMapper("whereParam")Map whereParam); + + /** + *

执行自定义批量新增sql

+ * @param cusSql 自定义sql + * @param insertList 新镇参数信息 + * @return 处理结果 + */ + @BatchInsert(custom = true) + boolean executeInsertBatchCusSql(@SqlString String cusSql, + @BatchSqlArgs List> insertList); + + /** + *

执行自定义批量更新sql

+ * @param cusSql 自定义sql + * @param updateList 更新参数信息 + * @param whereList 条件参数信息 + * @return 处理结果 + */ + @BatchInsert(custom = true) + boolean executeUpdateBatchCusSql(@SqlString String cusSql, + @BatchSqlArgs("upItem") List> updateList, + @BatchSqlArgs("whereItem") List> whereList); + } diff --git a/src/test/java/bokang/xiao/response_deal/process/DataChangeProcess.java b/src/test/java/bokang/xiao/response_deal/process/DataChangeProcess.java new file mode 100644 index 0000000..7ec2df5 --- /dev/null +++ b/src/test/java/bokang/xiao/response_deal/process/DataChangeProcess.java @@ -0,0 +1,205 @@ +package bokang.xiao.response_deal.process; + +import aiyh.utils.Util; +import aiyh.utils.annotation.MethodRuleNo; +import aiyh.utils.excention.CustomerException; +import bokang.xiao.exception.ResponseException; +import bokang.xiao.response_deal.entity.ResponseConfigValueChange; +import bokang.xiao.response_deal.intfaces.DataChangeInterface; +import bokang.xiao.response_deal.mapper.ConfigMapper; +import org.apache.commons.lang3.StringUtils; +import org.apache.log4j.Logger; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.HashMap; +import java.util.Map; +import java.util.function.BiFunction; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * @ClassName DataChangeProcess + * @Author 肖博亢 + * @Date 2023/7/7 10:55 + * @Description

+ **/ +public class DataChangeProcess { + + public static final Map,String>> MODE_METHOD_MAP = new HashMap<>(); + + private static final ConfigMapper CONFIG_MAPPER = Util.getMapper(ConfigMapper.class); + + public static final Logger logger = Util.getLogger(); + + static { + try { + Class dataChangeProcessClass = DataChangeProcess.class; + Method[] methods = dataChangeProcessClass.getDeclaredMethods(); + for (Method method : methods) { + if (method.isAnnotationPresent(MethodRuleNo.class)) { + MethodRuleNo annotation = method.getAnnotation(MethodRuleNo.class); + int value = annotation.value(); + MODE_METHOD_MAP.put(value, (responseConfigValueChange,param) -> { + try { + return (String) method.invoke(null,responseConfigValueChange,param); + } catch (IllegalAccessException | InvocationTargetException e) { + throw new RuntimeException(e); + } + }); + } + } + }catch (Exception exception){ + logger.error("DataChangeProcess init error !!! "+Util.getErrString(exception)); + } + } + + /** + * 默认值处理方式 + * @param responseConfigValueChange 赋值转换配置信息 + * @param param 参数 + * @return 结果 + */ + @MethodRuleNo(value = 0, desc = "默认值") + public static String defaultValue(ResponseConfigValueChange responseConfigValueChange,Map param){ + String cusText = responseConfigValueChange.getCusText(); + cusText = Util.dbc2sbcCase(cusText); + responseConfigValueChange.setJsonData(cusText); + return ""; + } + + /** + * sql转换处理方式 + * @param responseConfigValueChange 赋值转换配置信息 + * @param param 参数 + * @return 结果 + */ + @MethodRuleNo(value = 1, desc = "sql转换") + public static String sqlChange(ResponseConfigValueChange responseConfigValueChange,Map param){ + String cusText = responseConfigValueChange.getCusText(); + cusText = Util.dbc2sbcCase(cusText); + if(StringUtils.isBlank(cusText)){ + throw new CustomerException("when selecting Custom sql, the sql string cannot be empty !!!"); + } + if(!cusText.startsWith("select ")){ + throw new CustomerException("When you select custom sql, you are not allowed to perform dangerous operations other than SELECT !!!"); + } + String tempValue = CONFIG_MAPPER.executeCusQuerySql(cusText, param,new HashMap<>()); + responseConfigValueChange.setJsonData(tempValue); + return ""; + } + + /** + * 日期转换处理方式 + * @param responseConfigValueChange 赋值转换配置信息 + * @param param 参数 + * @return 结果 + */ + @MethodRuleNo(value = 2, desc = "日期转换") + public static String dateChange(ResponseConfigValueChange responseConfigValueChange,Map param){ + String cusText = responseConfigValueChange.getCusText(); + Object jsonData = responseConfigValueChange.getJsonData(); + cusText = Util.dbc2sbcCase(cusText); + String fromStr = getDateFormat(cusText,"from:\\{(.*?)\\}"); + String toStr = getDateFormat(cusText,"to:\\{(.*?)\\}"); + responseConfigValueChange.setJsonData(formatDateTime(Util.null2String(jsonData),fromStr,toStr)); + return ""; + } + + /** + * 自定义接口处理方式 + * @param responseConfigValueChange 赋值转换配置信息 + * @param param 参数 + * @return 结果 + */ + @MethodRuleNo(value = 3, desc = "自定义接口") + public static String cusInterface(ResponseConfigValueChange responseConfigValueChange,Map param){ + String cusText = responseConfigValueChange.getCusText(); + cusText = Util.dbc2sbcCase(cusText); + if(StringUtils.isBlank(cusText)){ + throw new CustomerException("The operation full path cannot be empty !!!"); + } + Map pathParam = new HashMap<>(); + try { + DataChangeInterface cusInterfaceObj = getCusInterfaceObj(cusText, DataChangeInterface.class, pathParam); + Object change = cusInterfaceObj.change(responseConfigValueChange, param, pathParam); + responseConfigValueChange.setJsonData(change); + }catch (Exception e){ + logger.error("自定义处理类执行异常 ==>"+Util.getErrString(e)); + throw new ResponseException(e); + } + return ""; + } + + /** + * 日期转换处理方式 + * @param responseConfigValueChange 赋值转换配置信息 + * @param param 参数 + * @return 结果 + */ + @MethodRuleNo(value = 4, desc = "无需转换") + public static String noChange(ResponseConfigValueChange responseConfigValueChange,Map param){ + return ""; + } + + /** + *

+ * @param path 全路径类名 + * @param clazz 父类接口 + * @param pathParamMap 路径参数 + * @return 接口实现类 + * @param 泛型 + */ + public static T getCusInterfaceObj(String path, Class clazz, Map pathParamMap) { + + path = Util.sbc2dbcCase(path); + String[] split = path.split("\\?"); + String classPath = split[0]; + Class aClass; + try { + aClass = Class.forName(classPath); + } catch (ClassNotFoundException e) { + throw new IllegalArgumentException("未能找到自定义接口:" + classPath); + } + if (!clazz.isAssignableFrom(aClass)) { + throw new IllegalArgumentException("自定义接口:" + classPath + " 不是" + + clazz.getName() + "的子类或实现类!"); + } + Constructor constructor; + try { + constructor = aClass.getConstructor(); + } catch (NoSuchMethodException e) { + throw new IllegalArgumentException(classPath + "没有空参构造方法,无法获取构造方法对象!"); + } + T o; + try { + o = (T) constructor.newInstance(); + } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { + throw new IllegalArgumentException("无法构造" + classPath + "对象!"); + } + pathParamMap.putAll(Util.parseCusInterfacePathParam(path)); + return o; + } + + private static String getDateFormat(String cusText,String regex){ + String formatStr = ""; + Pattern fromPattern = Pattern.compile(regex); + Matcher fromMatcher = fromPattern.matcher(cusText); + if (fromMatcher.find()) { + formatStr = fromMatcher.group(1); + } + return formatStr; + } + + public static String formatDateTime(String dateTimeString, String originalFormat, String targetFormat) { + DateTimeFormatter originalFormatter = DateTimeFormatter.ofPattern(originalFormat); + DateTimeFormatter targetFormatter = DateTimeFormatter.ofPattern(targetFormat); + + LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, originalFormatter); + + return dateTime.format(targetFormatter); + } +} diff --git a/src/test/java/bokang/xiao/response_deal/process/TypeChangeProcess.java b/src/test/java/bokang/xiao/response_deal/process/TypeChangeProcess.java new file mode 100644 index 0000000..7bd8e52 --- /dev/null +++ b/src/test/java/bokang/xiao/response_deal/process/TypeChangeProcess.java @@ -0,0 +1,88 @@ +package bokang.xiao.response_deal.process; + +import aiyh.utils.Util; +import aiyh.utils.annotation.MethodRuleNo; +import org.apache.log4j.Logger; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; +import java.util.function.Function; + +/** + * @ClassName TypeChangeProcess + * @Author 肖博亢 + * @Date 2023/7/7 13:30 + * @Description

+ **/ +public class TypeChangeProcess { + + public static final Map> MODE_METHOD_MAP = new HashMap<>(); + + public static final Logger logger = Util.getLogger(); + + static { + try { + Class typeChangeProcessClass = TypeChangeProcess.class; + Method[] methods = typeChangeProcessClass.getDeclaredMethods(); + for (Method method : methods) { + if (method.isAnnotationPresent(MethodRuleNo.class)) { + MethodRuleNo annotation = method.getAnnotation(MethodRuleNo.class); + int value = annotation.value(); + MODE_METHOD_MAP.put(value, (sourceValue) -> { + try { + return method.invoke(null,sourceValue); + } catch (IllegalAccessException | InvocationTargetException e) { + throw new RuntimeException(e); + } + }); + } + } + }catch (Exception exception){ + logger.error("typeChangeProcessClass init error !!! "+ Util.getErrString(exception)); + } + } + + /** + * String类型 + * @param sourceValue 原值 + * @return 结果 + */ + @MethodRuleNo(value = 0, desc = "String类型") + public static Object changString(Object sourceValue){ + return Util.null2String(sourceValue); + } + + /** + * int类型 + * @param sourceValue 原值 + * @return 结果 + */ + @MethodRuleNo(value = 1, desc = "int类型") + public static Object changInt(Object sourceValue){ + return Util.getIntValue(Util.null2String(sourceValue)); + } + + /** + * boolean类型 + * @param sourceValue 原值 + * @return 结果 + */ + @MethodRuleNo(value = 2, desc = "boolean类型") + public static Object changBoolean(Object sourceValue){ + return sourceValue; + } + + /** + * double类型 + * @param sourceValue 原值 + * @return 结果 + */ + @MethodRuleNo(value = 3, desc = "double类型") + public static Object changDouble(Object sourceValue){ + return Util.getDoubleValue(Util.null2String(sourceValue)); + } + + +} diff --git a/src/test/java/bokang/xiao/response_deal/state/InsertOrUpdateState.java b/src/test/java/bokang/xiao/response_deal/state/InsertOrUpdateState.java new file mode 100644 index 0000000..1852a60 --- /dev/null +++ b/src/test/java/bokang/xiao/response_deal/state/InsertOrUpdateState.java @@ -0,0 +1,68 @@ +package bokang.xiao.response_deal.state; + +import aiyh.utils.tool.cn.hutool.core.bean.BeanUtil; +import aiyh.utils.tool.cn.hutool.core.util.StrUtil; +import bokang.xiao.response_deal.entity.RowDefinition; +import bokang.xiao.response_deal.entity.TableDefinition; +import bokang.xiao.response_deal.intfaces.RowDefinitionCallback; +import bokang.xiao.response_deal.util.ResponseUtil; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +/** + * @ClassName InsertOrUpdateState + * @Author 肖博亢 + * @Date 2023/7/9 23:34 + * @Description

+ **/ +public class InsertOrUpdateState extends State{ + @Override + public void handle() { + if(ResponseUtil.parameterIsNotNull(this.context.getTableDefinitionCallback())){ + this.context.getTableDefinitionCallback().tableHandle(this.context); + } + List rowDefinitionList = this.context.getRowDefinitionList(); + List updateRowList = new ArrayList<>(); + List insertRowList = new ArrayList<>(); + for (RowDefinition rowDefinition : rowDefinitionList) { + boolean isRepetition = false; + if(ResponseUtil.parameterIsNotNull(rowDefinition.getRowDefinitionCallback())){ + RowDefinitionCallback rowDefinitionCallback = rowDefinition.getRowDefinitionCallback(); + isRepetition = rowDefinitionCallback.judgmentRepetition(rowDefinition); + }else { + isRepetition = this.judgmentRepetition(rowDefinition); + } + if(isRepetition){ + updateRowList.add(rowDefinition); + }else { + insertRowList.add(rowDefinition); + } + } + if(ResponseUtil.parameterIsNotNull(updateRowList)){ + this.tableProcess(1,updateRowList); + } + if(ResponseUtil.parameterIsNotNull(insertRowList)){ + this.tableProcess(0,insertRowList); + } + } + + private void tableProcess(int assignType,List rowDefinitionList){ + TableDefinition tableDefinition = new TableDefinition(); + BeanUtil.copyProperties(this.context,tableDefinition); + tableDefinition.setRowDefinitionList(rowDefinitionList); + tableDefinition.setTableDefinitionCallback(null); + tableDefinition.setAssignType(assignType); + tableDefinition.setDefaultState(); + tableDefinition.dataProcess(); + } + + private boolean judgmentRepetition(RowDefinition rowDefinition){ + String conditionScript = rowDefinition.getConditionScript(); + String cusQuerySql = "select id from " + rowDefinition.getAssignTable() + " where " + conditionScript; + String dataId = this.configMapper.executeCusQuerySql(cusQuerySql, rowDefinition.getWhereParam(),new HashMap<>()); + return StrUtil.isNotBlank(dataId); + } + +} diff --git a/src/test/java/bokang/xiao/response_deal/state/InsertSate.java b/src/test/java/bokang/xiao/response_deal/state/InsertSate.java new file mode 100644 index 0000000..5055875 --- /dev/null +++ b/src/test/java/bokang/xiao/response_deal/state/InsertSate.java @@ -0,0 +1,98 @@ +package bokang.xiao.response_deal.state; + +import aiyh.utils.Util; +import aiyh.utils.recordset.MapperBuilderSql; +import bokang.xiao.response_deal.constant.ResponseConfigConstant; +import bokang.xiao.response_deal.entity.RowDefinition; +import bokang.xiao.response_deal.entity.TableDefinition; +import bokang.xiao.response_deal.intfaces.RowDefinitionCallback; +import bokang.xiao.response_deal.util.ResponseUtil; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * @ClassName InsertSate + * @Author 肖博亢 + * @Date 2023/7/9 23:32 + * @Description

+ **/ +public class InsertSate extends State{ + @Override + public void handle() { + int tableType = this.context.getTableType(); + int mainOrDetail = this.context.getMainOrDetail(); + List rowDefinitionList = this.context.getRowDefinitionList(); + Map detailTableMap = ResponseUtil.parameterIsNull(this.context.getDetailTableMap()) ? + new HashMap<>() : this.context.getDetailTableMap(); + String assignTable = this.context.getAssignTable(); + if(ResponseUtil.parameterIsNull(rowDefinitionList)){ + logger.info(String.format("表:%s 没有数据需要新增",assignTable)); + return; + } + if(ResponseUtil.parameterIsNotNull(this.context.getTableDefinitionCallback())){ + this.context.getTableDefinitionCallback().tableHandle(this.context); + } + rowDefinitionList.forEach(item ->{ + if(ResponseUtil.parameterIsNotNull(item.getRowDefinitionCallback())){ + RowDefinitionCallback rowDefinitionCallback = item.getRowDefinitionCallback(); + rowDefinitionCallback.rowHandle(item); + } + }); + RowDefinition tempRowDefinition = rowDefinitionList.get(0); + if(tableType == ResponseConfigConstant.MODEL_TABLE && mainOrDetail == ResponseConfigConstant.MAIN_TABLE){ + List> updateList = new ArrayList<>(); + List> whereList = new ArrayList<>(); + List dataIdList = new ArrayList<>(); + for (RowDefinition rowDefinition : rowDefinitionList) { + int dataId = Util.getModeDataId(assignTable, 1); + rowDefinition.getWhereParam().put("id",dataId); + updateList.add(rowDefinition.getUpdateParam()); + whereList.add(rowDefinition.getWhereParam()); + rowDefinition.setDataId(dataId); + if(mainOrDetail == ResponseConfigConstant.MAIN_TABLE){ + detailTableDeal(rowDefinition,detailTableMap); + } + dataIdList.add(dataId); + } + String updateSql = MapperBuilderSql.builderUpdateSql(assignTable, tempRowDefinition.getUpdateParam(),"upItem"); + updateSql = updateSql + " where id = #{whereItem.id}"; + boolean flag = this.configMapper.executeUpdateBatchCusSql(updateSql, updateList, whereList); + String modeId = Util.getModeIdByTableName(assignTable); + Util.rebuildModeDataShareByAsyncList(1,modeId,dataIdList); + if(ResponseUtil.parameterIsNotNull(detailTableMap) && flag){ + detailTableMap.forEach((key,value)->{ + value.setDefaultState(); + value.dataProcess(); + }); + } + }else { + String insertSql = MapperBuilderSql.builderInsertSql(assignTable, tempRowDefinition.getUpdateParam(),"item"); + List> insertList = rowDefinitionList.stream().map(RowDefinition::getUpdateParam).collect(Collectors.toList()); + boolean flag = this.configMapper.executeInsertBatchCusSql(insertSql, insertList); + } + } + + private void detailTableDeal(RowDefinition rowDefinition,Map mainDetailMap){ + int mainId = rowDefinition.getDataId(); + Map detailTableMap = rowDefinition.getDetailTableMap(); + detailTableMap.forEach((key,value) ->{ + List rowDefinitionList = value.getRowDefinitionList(); + if(ResponseUtil.parameterIsNull(rowDefinitionList)){ + return; + } + rowDefinitionList.forEach(item ->{ + item.getUpdateParam().put("mainid",mainId); + }); + if(mainDetailMap.containsKey(key)){ + TableDefinition tableDefinition = mainDetailMap.get(key); + tableDefinition.getRowDefinitionList().addAll(rowDefinitionList); + }else { + mainDetailMap.put(key,value); + } + }); + } +} diff --git a/src/test/java/bokang/xiao/response_deal/state/State.java b/src/test/java/bokang/xiao/response_deal/state/State.java new file mode 100644 index 0000000..a8dba38 --- /dev/null +++ b/src/test/java/bokang/xiao/response_deal/state/State.java @@ -0,0 +1,33 @@ +package bokang.xiao.response_deal.state; + +import aiyh.utils.Util; +import bokang.xiao.response_deal.entity.TableDefinition; +import bokang.xiao.response_deal.mapper.ConfigMapper; +import org.apache.log4j.Logger; + +/** + * @ClassName State + * @Author 肖博亢 + * @Date 2023/7/9 23:27 + * @Description

+ **/ +public abstract class State { + + protected final ConfigMapper configMapper = Util.getMapper(ConfigMapper.class); + + /** 日志对象 */ + protected final Logger logger = Util.getLogger(); + + /** 上下文对象 */ + protected TableDefinition context; + + public void setContext(TableDefinition context) { + this.context = context; + } + + /** + * 抽象处理方法 + */ + public abstract void handle(); + +} diff --git a/src/test/java/bokang/xiao/response_deal/state/StateFactory.java b/src/test/java/bokang/xiao/response_deal/state/StateFactory.java new file mode 100644 index 0000000..cfe08a2 --- /dev/null +++ b/src/test/java/bokang/xiao/response_deal/state/StateFactory.java @@ -0,0 +1,22 @@ +package bokang.xiao.response_deal.state; + +import org.docx4j.wml.U; + +/** + * @ClassName StateFactory + * @Author 肖博亢 + * @Date 2023/7/9 23:34 + * @Description

+ **/ +public class StateFactory { + + public static State createState(int assignType){ + if(assignType == 0){ + return new InsertSate(); + }else if(assignType == 1){ + return new UpdateState(); + }else { + return new InsertOrUpdateState(); + } + } +} diff --git a/src/test/java/bokang/xiao/response_deal/state/UpdateState.java b/src/test/java/bokang/xiao/response_deal/state/UpdateState.java new file mode 100644 index 0000000..62c78ec --- /dev/null +++ b/src/test/java/bokang/xiao/response_deal/state/UpdateState.java @@ -0,0 +1,90 @@ +package bokang.xiao.response_deal.state; + +import aiyh.utils.ScriptUtil; +import aiyh.utils.recordset.MapperBuilderSql; +import bokang.xiao.exception.ResponseException; +import bokang.xiao.response_deal.constant.ResponseConfigConstant; +import bokang.xiao.response_deal.entity.RowDefinition; +import bokang.xiao.response_deal.entity.TableDefinition; +import bokang.xiao.response_deal.util.ResponseUtil; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @ClassName UpdateState + * @Author 肖博亢 + * @Date 2023/7/9 23:33 + * @Description

+ **/ +public class UpdateState extends State{ + @Override + public void handle() { + if(ResponseUtil.parameterIsNotNull(this.context.getTableDefinitionCallback())){ + this.context.getTableDefinitionCallback().tableHandle(this.context); + } + int mainOrDetail = this.context.getMainOrDetail(); + Map detailTableMap = ResponseUtil.parameterIsNull(this.context.getDetailTableMap()) ? + new HashMap<>() : this.context.getDetailTableMap(); + List rowDefinitionList = this.context.getRowDefinitionList(); + String assignTable = this.context.getAssignTable(); + String conditionScript = this.context.getConditionScript(); + if(ResponseUtil.parameterIsNull(conditionScript)){ + throw new ResponseException(String.format("table:%s Unconditional updates are not allowed !!!",assignTable)); + } + try { + boolean res = (boolean) ScriptUtil.invokeScript(conditionScript, new HashMap<>()); + if(res){ + throw new ResponseException(String.format("table:%s The expression cannot be an identity !!!",assignTable)); + } + }catch (Exception e){ + logger.info("表达式不为恒等式"); + } + if(ResponseUtil.parameterIsNull(rowDefinitionList)){ + logger.info(String.format("表:%s 没有数据需要新增",assignTable)); + } + List> updateList = new ArrayList<>(); + List> whereList = new ArrayList<>(); + for (RowDefinition definition : rowDefinitionList) { + if(ResponseUtil.parameterIsNotNull(definition.getRowDefinitionCallback())){ + definition.getRowDefinitionCallback().rowHandle(definition); + } + updateList.add(definition.getUpdateParam()); + updateList.add(definition.getWhereParam()); + } + RowDefinition rowDefinition = rowDefinitionList.get(0); + String updateSql = MapperBuilderSql.builderUpdateSql(assignTable, rowDefinition.getUpdateParam(),"upItem"); + updateSql = updateSql + " " + conditionScript; + boolean flag = this.configMapper.executeUpdateBatchCusSql(updateSql, updateList, whereList); + logger.info("批量更新处理结果 ==>"+flag); + //如果为主表则处理明细数据 + if(mainOrDetail == ResponseConfigConstant.MAIN_TABLE){ + detailTableDeal(detailTableMap); + if(ResponseUtil.parameterIsNotNull(detailTableMap)){ + detailTableMap.forEach((key,value)->{ + value.setDefaultState(); + value.dataProcess(); + }); + } + } + } + + private void detailTableDeal(Map mainDetailMap){ + List rowDefinitionList = this.context.getRowDefinitionList(); + rowDefinitionList.forEach(item ->{ + Map detailTableMap = item.getDetailTableMap(); + if(ResponseUtil.parameterIsNull(detailTableMap)){ + return; + } + detailTableMap.forEach((key,value) ->{ + if(mainDetailMap.containsKey(key)){ + mainDetailMap.get(key).getRowDefinitionList().addAll(value.getRowDefinitionList()); + }else { + mainDetailMap.put(key,value); + } + }); + }); + } +} diff --git a/src/test/java/bokang/xiao/response_deal/store/ResponseStore.java b/src/test/java/bokang/xiao/response_deal/store/ResponseStore.java index f992a69..079fae8 100644 --- a/src/test/java/bokang/xiao/response_deal/store/ResponseStore.java +++ b/src/test/java/bokang/xiao/response_deal/store/ResponseStore.java @@ -11,7 +11,7 @@ import java.util.*; import java.util.stream.Collectors; /** - * @ClassName VendorStore + * @ClassName ResponseStore * @Author 肖博亢 * @Date 2023/3/17 18:31 * @Description

diff --git a/src/test/java/bokang/xiao/response_deal/util/ResponseUtil.java b/src/test/java/bokang/xiao/response_deal/util/ResponseUtil.java new file mode 100644 index 0000000..972fb06 --- /dev/null +++ b/src/test/java/bokang/xiao/response_deal/util/ResponseUtil.java @@ -0,0 +1,39 @@ +package bokang.xiao.response_deal.util; + +import bokang.xiao.exception.ResponseException; + +import java.util.List; +import java.util.Map; + +/** + * @ClassName ResponseUtil + * @Author 肖博亢 + * @Date 2023/7/6 18:08 + * @Description

+ **/ +public class ResponseUtil { + + public static void parameterJudgment(Object o,String message){ + if(parameterIsNull(o)) {throw new ResponseException(message);} + } + + public static boolean parameterIsNull(Object o){ + if(o == null){ + return true; + } + if(o instanceof List){ + List list = (List) o; + return list.isEmpty(); + } + if(o instanceof Map){ + Map map = (Map) o; + return map.isEmpty(); + } + return false; + } + + public static boolean parameterIsNotNull(Object o){ + return !parameterIsNull(o); + } + +}