/**
* 迭代处理对象属性值替换
* @param vo
* @param attriMap
* @param unchangeNameSet
*/
private void changeBaseInfo(Object vo,Map attriMap,Set<String> unchangeNameSet) {
try {
if(vo == null || attriMap == null || attriMap.size() == 0) return;
//map 数据中内容替换
if (vo instanceof Map) {
Map map = (Map) vo;
if (map != null && map.size() > 0) {
Set set = map.keySet();
Iterator iterator = set.iterator();
//获取map中所有value值
while (iterator.hasNext()) {
Object key = iterator.next();
if(key == null) continue;
Object value = map.get(key);
//map中String类型内容可直接替换
if (value != null && value instanceof String) {
ZhuanquDataMultilingualDTO dtoObjB = (ZhuanquDataMultilingualDTO) attriMap.get(value.toString().toLowerCase());
if (dtoObjB != null) {
String otherB = dtoObjB.getTextValue();
if (StringUtils.isNotBlank(otherB)) {
//不需要替换内容校验
if(unchangeNameSet.contains(key.toString())) continue;
map.put(key, otherB);
}
}
}
//其他类型需要迭代类型处理
else {
//迭代分析
changeBaseInfo(value, attriMap,unchangeNameSet);
}
}
}
}
//列表中数据替换
else if (vo instanceof List) {
List list = (List) vo;
if (!CollectionUtils.isEmpty(list)) {
for (Object value : list) {
//迭代分析(字符类型暂不做处理,只处理对象类型)
changeBaseInfo(value, attriMap,unchangeNameSet);
}
}
}
//对象数据替换
else {
Field[] declaredFields = vo.getClass().getDeclaredFields();
//只处理有属性值得对象数据
if (declaredFields != null && declaredFields.length > 0) {
for (Field field : declaredFields) {
//过滤掉配置中不需要修改的值
if(field == null && unchangeNameSet.contains(field.getName())){
continue;
}
//对象中字符类型数据使用get\set方法替换
if (field.getType() == String.class) {
String methodName = com.dhgate.mobile.util.StringUtils.toUpperCaseFirstOne(field.getName());
Method getMethod = vo.getClass().getMethod("get" + methodName);
if (getMethod != null) {
Object value = getMethod.invoke(vo);
if (value != null && attriMap != null) {
ZhuanquDataMultilingualDTO dtoObjB = (ZhuanquDataMultilingualDTO) attriMap.get(value.toString().toLowerCase());
if (dtoObjB != null) {
String otherB = dtoObjB.getTextValue();
if (StringUtils.isNotBlank(otherB)) {
Method setMethod = vo.getClass().getMethod("set" + methodName, String.class);
setMethod.invoke(vo, otherB);
}
}
}
}
}
//对象中map和list对象继续迭代处理
else if(field.getType() == Map.class || field.getType() == List.class){
String methodName = com.dhgate.mobile.util.StringUtils.toUpperCaseFirstOne(field.getName());
Method getMethod = vo.getClass().getMethod("get" + methodName);
Object value = getMethod.invoke(vo);
if (value != null) {
//迭代分析
changeBaseInfo(value, attriMap,unchangeNameSet);
}
}
}
}
}
} catch (Exception e) {
logger.error("changeBaseInfo error ",e);
}
}