改造的是DictAspect类:
原来使用的 parseDictText(Object result)方法,针对返回对象为Result 的IPage的分页列表数据进行动态字典注入,当单个对象查询,列表查询,或者多个数据放到Map中时,就不会自动转化,在web端进行展示的时候就需要连表查询或者手动查询字典值,不方便使用。
于是我改造了parseDictText()方法,不仅针对返回对象为Result时的分页列表,还支持列表、对象以及Map类型的结果。实在Result对象执行setResult()方式时进行自动注入转换。
原方法
代码:
private Object parseDictText(Object result) {
//if (result instanceof Result) {
if (true) {
if (((Result) result).getResult() instanceof IPage) {
List items = new ArrayList<>();
//step.1 筛选出加了 Dict 注解的字段列表
List dictFieldList = new ArrayList<>();
// 字典数据列表, key = 字典code,value=数据列表
Map> dataListMap = new HashMap<>(5);
//取出结果集
List
改造
判断传递参数的类型,是IPage、List、Map、Object,如果是IPage、List、Map,则把对象中的数据拿出来再挨个转换,改造为新的结果数据返回。
private Object parseDictText(Object result) {
if (result instanceof Result) {
List
字典翻译方法:
/**
* 字典翻译
*
* @param record
* @return
*/
private JSONObject dictEscape(Object record) {
ObjectMapper mapper = new ObjectMapper();
String json = "{}";
JSONObject item = null;
try {
//解决@JsonFormat注解解析不了的问题详见SysAnnouncement类的@JsonFormat
json = mapper.writeValueAsString(record);//对象序列化为JSON字符串
} catch (JsonProcessingException e) {
log.error("json解析失败" + e.getMessage(), e);
}
try {
item = JSONObject.parseObject(json);
//update-begin--Author:scott -- Date:20190603 ----for:解决继承实体字段无法翻译问题------
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()));
//翻译字典值对应的txt
String textValue = key;
//非中文时翻译
if (!checkCountName(key)) {
textValue = translateDictValue(code, text, table, key);
}
log.debug(" 字典Val : " + textValue);
log.debug(" __翻译字典字段__ " + field.getName() + CommonConstant.DICT_TEXT_SUFFIX + ": " + textValue);
item.put(field.getName() + CommonConstant.DICT_TEXT_SUFFIX, textValue);
}
//date类型默认转换string格式化日期
if (field.getType().getName().equals("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()))));
}
}
} catch (Exception e) {
log.info("字典翻译异常:" + e.getMessage(), e);
}
return item;
}
检测是否是中文:
/**
* 检测是否是中文
*
* @param countName
* @return
*/
public static boolean checkCountName(String countName) {
Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
Matcher m = p.matcher(countName);
if (m.find()) {
return true;
}
return false;
}
检测是否可转换为JSON字符串
/**
* 检测是否可转换为JSON字符串
*
* @param record
* @return
*/
public static boolean checkIsJsonStr(Object record) {
boolean jsonFlag = false;
try {
String json = new ObjectMapper().writeValueAsString(record);
if (json.startsWith("{")) {
jsonFlag = true;
}
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return jsonFlag;
}