回写数据时支持配置层级数据

dev
wangxuanran 2023-07-10 17:03:50 +08:00
parent a78f69bead
commit fcd819131c
3 changed files with 232 additions and 7 deletions

View File

@ -1,6 +1,12 @@
package weaver.xiao.commons.config.service;
import aiyh.utils.excention.CustomerException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.google.common.base.Strings;
import lombok.AllArgsConstructor;
import lombok.Data;
@ -57,7 +63,7 @@ public class DealWithMapping extends ToolUtil {
private List<String> fileNames = new ArrayList<>();
private List<InputStream> fileInputStreams = new ArrayList<>();
private List<MultipartFile> multipartFileList = new ArrayList<>();
private static final ObjectMapper objectMapper = new ObjectMapper();
private RecordSet tempRs;
private DealWithMapper mapper = null;
@ -1451,7 +1457,10 @@ public class DealWithMapping extends ToolUtil {
List<ResponseMapping> detailResponseList = new ArrayList<>();
for (ResponseMapping responseMapping : responseMappingList) {
if (responseMapping.getMainOrDetail() == 0) {
String resVal = Util.null2String(requestRes.get(responseMapping.getResponseFieldName()));
// 解析层级数据
String fieldName = responseMapping.getResponseFieldName();
Object result = parseCommon(requestRes, fieldName, Object.class);
String resVal = Util.null2String(result);
mainUpdateMap.put(responseMapping.getWorkflowFieldName(), resVal);
} else {
detailResponseList.add(responseMapping);
@ -1475,6 +1484,100 @@ public class DealWithMapping extends ToolUtil {
logger.info("回写信息tableUpdateMap==> " + tableUpdateMap);
return tableUpdateMap;
}
/**
* <h1></h1>
* @author xuanran.wang
* @dateTime 2023/7/7 17:18
* @param map map
* @param dataKey
* @param clazz
* @return
**/
public <T> T parseCommon(Map<String, Object> map, String dataKey, Class<T> clazz) {
if (MapUtils.isEmpty(map)) {
throw new CustomerException("map can not be null!");
}
Object object = getDataObject(map, dataKey);
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.convertValue(object, clazz);
}
/**
* <h1></h1>
* @author xuanran.wang
* @dateTime 2023/7/7 17:18
* @param map map
* @param dataKey
* @param clazz
* @return
**/
public <T> T parseObj(Map<String, Object> map, String dataKey, Class<?> clazz) {
if (MapUtils.isEmpty(map)) {
throw new CustomerException("map can not be null!");
}
Object dataObject = getDataObject(map, dataKey);
if (Objects.isNull(dataObject)) {
throw new CustomerException("parse object error!");
}
if (dataObject instanceof Map) {
return (T) JSONObject.parseObject(JSONObject.toJSONString(dataObject), clazz);
}
return null;
}
/**
* <h1></h1>
* @author xuanran.wang
* @dateTime 2023/7/7 17:18
* @param map map
* @param dataKey
* @param clazz
* @return
**/
public <T> List<T> parseList(Map<String, Object> map, String dataKey, Class<?> clazz) {
if (MapUtils.isEmpty(map)) {
throw new CustomerException("map can not be null!");
}
Object dataObject = getDataObject(map, dataKey);
if (Objects.isNull(dataObject)) {
throw new CustomerException("parse object error!");
}
if (dataObject instanceof List) {
JavaType listType = TypeFactory.defaultInstance().constructParametricType(List.class, clazz);
try {
String jsonString = objectMapper.writeValueAsString(dataObject);
return objectMapper.readValue(jsonString, listType);
} catch (JsonProcessingException e) {
throw new CustomerException("JSON processing error!", e);
}
}
return null;
}
/**
* <h1></h1>
* @author xuanran.wang
* @dateTime 2023/7/7 17:19
* @param map map
* @param dataKey
* @return
**/
private Object getDataObject(Map<String, Object> map, String dataKey) {
if(StringUtils.isBlank(dataKey) || dataKey.split("\\.").length == 0){
return map;
}
Object dataObject = map;
String[] keys = dataKey.split("\\.");
for (String key : keys) {
if (dataObject instanceof Map) {
dataObject = ((Map<?, ?>) dataObject).get(key);
} else {
return dataObject;
}
}
return dataObject;
}
/**
*

View File

@ -3,6 +3,7 @@ package xuanran.wang;
import aiyh.utils.GenerateFileUtil;
import basetest.BaseTest;
import org.junit.Test;
import weaver.xuanran.wang.ajx.work_flow_split.action.ChildWorkFlowSplitAction;
import weaver.xuanran.wang.eighty_five_degreec.sap.action.WorkflowToSap;
import weaver.xuanran.wang.immc.WorkFlowToVmsAndMQ;
@ -17,6 +18,6 @@ public class NormalTest extends BaseTest {
@Test
public void testWord(){
GenerateFileUtil.createActionDocument(WorkFlowToVmsAndMQ.class);
GenerateFileUtil.createActionDocument(ChildWorkFlowSplitAction.class);
}
}

View File

@ -4,17 +4,19 @@ import aiyh.utils.Util;
import aiyh.utils.excention.CustomerException;
import basetest.BaseTest;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import weaver.bokang.xiao.common.mapper.WorkflowMapper;
import weaver.xuanran.wang.ajx.work_flow_split.mapper.ChildWorkFlowSplitMapper;
import weaver.xuanran.wang.common.mapper.CommonMapper;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;
/**
@ -27,6 +29,7 @@ public class TestA extends BaseTest {
private final CommonMapper workflowMapper = Util.getMapper(CommonMapper.class);
private final ChildWorkFlowSplitMapper mapper = Util.getMapper(ChildWorkFlowSplitMapper.class);
private static final ObjectMapper objectMapper = new ObjectMapper();
private String insertAccountNumberField = "";
@ -82,4 +85,122 @@ public class TestA extends BaseTest {
throw new CustomerException("批量插入明细数据失败!");
}
}
@Test
public void testB(){
String json = "{\n" +
" \"code\": 200,\n" +
" \"msg\": \"接口返回成功\",\n" +
" \"status\": true,\n" +
" \"data\": {\n" +
" \"singleTenant\": \"false\",\n" +
" \"urlTitle\": 100,\n" +
" \"changeTenant\": \"true\",\n" +
" \"realUri\": \"http://www.e-cology.com.cn/papi/app/baseserver/info/getUrlInfo?service=https%3A%2F%2Fwww.e-cology.com.cn%2Fblog%2Ftheirblogs%3FcusMenuId%3D5115968866800923639%26urlPageTitle%3D5LuW5Lq65pel5oql\",\n" +
" \"onlineDomianUrl\": \"https://online.e-cology.com.cn\",\n" +
" \"weappUrl\": \"https://www.e-cology.com.cn\",\n" +
" \"passportUrl\": \"https://www.e-cology.com.cn\",\n" +
" \"deployEnv\": \"private\",\n" +
" \"createTenant\": \"false\"\n" +
" },\n" +
" \"fail\": false\n" +
"}";
Map map = JSONObject.parseObject(json, Map.class);
Object o = parseCommon(map, "data.urlTitle", Object.class);
System.out.println(Util.null2String(o));
}
/**
* <h1></h1>
* @author xuanran.wang
* @dateTime 2023/7/7 17:18
* @param map map
* @param dataKey
* @param clazz
* @return
**/
public <T> T parseCommon(Map<String, Object> map, String dataKey, Class<T> clazz) {
if (MapUtils.isEmpty(map)) {
throw new CustomerException("map can not be null!");
}
Object object = getDataObject(map, dataKey);
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.convertValue(object, clazz);
}
/**
* <h1></h1>
* @author xuanran.wang
* @dateTime 2023/7/7 17:18
* @param map map
* @param dataKey
* @param clazz
* @return
**/
public <T> T parseObj(Map<String, Object> map, String dataKey, Class<?> clazz) {
if (MapUtils.isEmpty(map)) {
throw new CustomerException("map can not be null!");
}
Object dataObject = getDataObject(map, dataKey);
if (Objects.isNull(dataObject)) {
throw new CustomerException("parse object error!");
}
if (dataObject instanceof Map) {
return (T) JSONObject.parseObject(JSONObject.toJSONString(dataObject), clazz);
}
return null;
}
/**
* <h1></h1>
* @author xuanran.wang
* @dateTime 2023/7/7 17:18
* @param map map
* @param dataKey
* @param clazz
* @return
**/
public <T> List<T> parseList(Map<String, Object> map, String dataKey, Class<?> clazz) {
if (MapUtils.isEmpty(map)) {
throw new CustomerException("map can not be null!");
}
Object dataObject = getDataObject(map, dataKey);
if (Objects.isNull(dataObject)) {
throw new CustomerException("parse object error!");
}
if (dataObject instanceof List) {
JavaType listType = TypeFactory.defaultInstance().constructParametricType(List.class, clazz);
try {
String jsonString = objectMapper.writeValueAsString(dataObject);
return objectMapper.readValue(jsonString, listType);
} catch (JsonProcessingException e) {
throw new CustomerException("JSON processing error!", e);
}
}
return null;
}
/**
* <h1></h1>
* @author xuanran.wang
* @dateTime 2023/7/7 17:19
* @param map map
* @param dataKey
* @return
**/
private Object getDataObject(Map<String, Object> map, String dataKey) {
if(StringUtils.isBlank(dataKey) || dataKey.split("\\.").length == 0){
return map;
}
Object dataObject = map;
String[] keys = dataKey.split("\\.");
for (String key : keys) {
if (dataObject instanceof Map) {
dataObject = ((Map<?, ?>) dataObject).get(key);
} else {
return dataObject;
}
}
return dataObject;
}
}