html 字典翻译,字典翻译@Dict

1、编写翻译字典@Dict

/**

* 数据字典翻译注解

*/

@Target(ElementType.METHOD)

@Retention(RetentionPolicy.RUNTIME)

public @interface Dict {

public static final String SOURCE_NAME = "Id";

public static final String TARGET_NAME = "Name";

}

2、编写翻译字段注解@DictItem

@Target({ FIELD })

@Retention(RUNTIME)

public @interface DictItem {

CategoryEnum value();

}

3、编写切面实现翻译

@Aspect

@Component

@Slf4j

public class DictAspect {

@Reference

private SysDictionaryDubboService sysDictionaryDubboService;

@Pointcut("execution(* com.bgy.order.controller..*(..))")

public void packagePointCut(){}

/**

* 返回增强:目标方法正常执行完毕时执行

*

* @param joinPoint

* @param result

*/

@AfterReturning(pointcut = "packagePointCut() && @annotation(dict)", returning = "result")

public void afterReturningMethod(JoinPoint joinPoint, Dict dict, Object result) {

processData(result);

}

private void processData(Object result) {

List needProcessFieldList = new ArrayList<>();

Map fieldMap = new HashMap<>();

if (result instanceof Page) {

for(Object object : ((Page>)result).getRecords()){

getProcessFieldList(object, needProcessFieldList, fieldMap);

processSingleData(object, needProcessFieldList, fieldMap);

}

} else if(result instanceof List) {

for(Object object : (List>) result) {

getProcessFieldList(object, needProcessFieldList, fieldMap);

processSingleData(object, needProcessFieldList, fieldMap);

}

} else if(result instanceof Map){

for(Object obj : ((Map,?>)result).values()){

processData(obj);

}

} else if(result instanceof Serializable){

getProcessFieldList(result, needProcessFieldList,fieldMap);

processSingleData(result, needProcessFieldList,fieldMap);

}

}

private void getProcessFieldList(Object result, List needProcessFieldList, Map fieldMap) {

if(!(result instanceof Serializable)){

return;

}

List declaredFields = getAllField(result.getClass());

if (CollectionUtils.isEmpty(declaredFields)) {

return;

}

for(Field field : declaredFields){

fieldMap.put(field.getName(), field);

DictItem dictItem = field.getAnnotation(DictItem.class);

if(dictItem != null){

needProcessFieldList.add(field.getName());

}

}

}

private List getAllField(Class> clz){

List fieldList = new ArrayList<>() ;

Class> tempClass = clz;

while (tempClass != null) {//当父类为null的时候说明到达了最上层的父类(Object类).

fieldList.addAll(Arrays.asList(tempClass.getDeclaredFields()));

tempClass = tempClass.getSuperclass(); //得到父类,然后赋给自己

}

return fieldList;

}

private void processSingleData(Object data, List needProcessFieldList,Map fieldMap){

for(String fieldName : needProcessFieldList){

setDataDesc(data, fieldMap, fieldName);

}

}

private void setDataDesc(Object data, Map fieldMap, String fieldName) {

try {

Field field = fieldMap.get(fieldName);

Class> type = fieldMap.get(fieldName).getType();

if(!type.equals(Integer.class)){

return;

}

field.setAccessible(true);

Integer dictKey = (Integer)field.get(data);

DictItem dictItem = field.getAnnotation(DictItem.class);

CategoryEnum categoryEnum = dictItem.value();

if(!fieldName.endsWith(Dict.SOURCE_NAME)){

return;

}

String targetFiledName = fieldName.replace(Dict.SOURCE_NAME,Dict.TARGET_NAME);

Field findField = fieldMap.get(targetFiledName);

if (findField == null || !findField.getType().equals(String.class)) {

return ;

}

findField.setAccessible(true);

String dictValue = sysDictionaryDubboService.getDictName(categoryEnum, dictKey);

findField.set(data, dictValue);

} catch (Exception e) {

log.error("注入字典名称报错", e);

}

}

4、测试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值