feat: 字典翻译支持深层嵌套翻译
This commit is contained in:
+54
-28
@@ -3,6 +3,7 @@ package com.jero.common.aspect;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.jero.common.api.CommonAPI;
|
||||
@@ -22,10 +23,7 @@ import org.springframework.util.StringUtils;
|
||||
import javax.annotation.Resource;
|
||||
import java.lang.reflect.Field;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -99,47 +97,75 @@ public class TranslationAspect {
|
||||
}
|
||||
}
|
||||
|
||||
private Object translateObject(Object record,List<SysDictItemCore> listDict){
|
||||
private Object translateObject(Object record, List<SysDictItemCore> listDict) {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
String json;
|
||||
try {
|
||||
//解决@JsonFormat注解解析不了的问题详见SysAnnouncement类的@JsonFormat
|
||||
json = mapper.writeValueAsString(record);
|
||||
} catch (JsonProcessingException e) {
|
||||
log.error("json解析失败" + e.getMessage(), e);
|
||||
return record;
|
||||
}
|
||||
JSONObject item;
|
||||
try {
|
||||
item = JSONObject.parseObject(json);
|
||||
//update-begin--Author:scott -- Date:20190603 ----for:解决继承实体字段无法翻译问题------
|
||||
//for (Field field : record.getClass().getDeclaredFields()) {
|
||||
|
||||
for (Field field : oConvertUtils.getAllFields(record)) {
|
||||
//update-end--Author:scott -- Date:20190603 ----for:解决继承实体字段无法翻译问题------
|
||||
if (field.getAnnotation(Dict.class) != null) {
|
||||
String code = field.getAnnotation(Dict.class).dicCode();
|
||||
String text = field.getAnnotation(Dict.class).dicText();
|
||||
String table = field.getAnnotation(Dict.class).dictTable();
|
||||
String key = String.valueOf(item.get(field.getName()));
|
||||
JSONObject item = JSONObject.parseObject(json);
|
||||
|
||||
//翻译字典值对应的txt
|
||||
String textValue = translateDictValue(code, text, table, key, listDict);
|
||||
item.put(field.getName() + CommonConstant.DICT_TEXT_SUFFIX, textValue);
|
||||
for (Field field : oConvertUtils.getAllFields(record)) {
|
||||
field.setAccessible(true);
|
||||
|
||||
// 如果字段是复杂对象类型或者是List,进行递归翻译
|
||||
if (isComplexObject(field.getType()) || List.class.isAssignableFrom(field.getType())) {
|
||||
Object nestedRecord;
|
||||
try {
|
||||
nestedRecord = field.get(record);
|
||||
} catch (IllegalAccessException e) {
|
||||
continue;
|
||||
}
|
||||
//date类型默认转换string格式化日期
|
||||
if (Objects.equals(field.getType().getName(),"java.util.Date") && field.getAnnotation(JsonFormat.class) == null && item.get(field.getName()) != null) {
|
||||
SimpleDateFormat aDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
item.put(field.getName(), aDate.format(new Date((Long) item.get(field.getName()))));
|
||||
|
||||
if (nestedRecord != null) {
|
||||
JsonProperty jsonProperty = field.getAnnotation(JsonProperty.class);
|
||||
String jsonFieldName = (jsonProperty != null) ? jsonProperty.value() : field.getName();
|
||||
|
||||
if (List.class.isAssignableFrom(field.getType())) {
|
||||
List<?> objList = (List<?>) nestedRecord;
|
||||
List<Object> translatedList = new ArrayList<>();
|
||||
for (Object obj : objList) {
|
||||
translatedList.add(translateObject(obj, listDict));
|
||||
}
|
||||
item.put(jsonFieldName, translatedList);
|
||||
} else {
|
||||
Object translatedNestedRecord = translateObject(nestedRecord, listDict);
|
||||
item.put(jsonFieldName, translatedNestedRecord);
|
||||
}
|
||||
}
|
||||
}
|
||||
}catch (Exception e){
|
||||
log.info("########################此返回类型不支持字典翻译########################");
|
||||
return record;
|
||||
|
||||
if (field.getAnnotation(Dict.class) != null) {
|
||||
String code = field.getAnnotation(Dict.class).dicCode();
|
||||
String text = field.getAnnotation(Dict.class).dicText();
|
||||
String table = field.getAnnotation(Dict.class).dictTable();
|
||||
String key = String.valueOf(item.get(field.getName()));
|
||||
String textValue = translateDictValue(code, text, table, key, listDict);
|
||||
item.put(field.getName() + CommonConstant.DICT_TEXT_SUFFIX, textValue);
|
||||
}
|
||||
|
||||
if (Objects.equals(field.getType().getName(), "java.util.Date") && field.getAnnotation(JsonFormat.class) == null && item.get(field.getName()) != null) {
|
||||
SimpleDateFormat aDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
item.put(field.getName(), aDate.format(new Date((Long) item.get(field.getName()))));
|
||||
}
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
private boolean isComplexObject(Class<?> type) {
|
||||
return !(
|
||||
type.isPrimitive() ||
|
||||
type.equals(String.class) ||
|
||||
Number.class.isAssignableFrom(type) ||
|
||||
Date.class.isAssignableFrom(type)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 翻译字典文本
|
||||
* @param code 编码
|
||||
|
||||
Reference in New Issue
Block a user