• 字典翻译@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<String> needProcessFieldList = new ArrayList<>();
            Map<String,Field> 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<String> needProcessFieldList, Map<String,Field> fieldMap) {
            if(!(result instanceof Serializable)){
               return;
            }
            
            List<Field> 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<Field> getAllField(Class<?> clz){
            List<Field> 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<String> needProcessFieldList,Map<String,Field> fieldMap){
            for(String fieldName : needProcessFieldList){
                setDataDesc(data, fieldMap, fieldName);
            }
    
        }
    
        private void setDataDesc(Object data, Map<String, Field> 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、测试

     

  • 相关阅读:
    Intellij IDEA 打开文件tab数量限制的调整
    Mysql处理中文乱码的问题
    MIT算法导论笔记
    算法导论-排序(一)-插入排序、归并排序
    leetcode题解:Search for a Range (已排序数组范围查找)
    leetcode 题解:Merge Sorted Array(两个已排序数组归并)
    leetcode题解:Construct Binary Tree from Inorder and Postorder Traversal(根据中序和后序遍历构造二叉树)
    leetcode题解:Construct Binary Tree from Preorder and Inorder Traversal (根据前序和中序遍历构造二叉树)
    c++11 std::prev、std::next、std::advance与auto 使用
    (转)指针的引用(*&)与指针的指针(**)
  • 原文地址:https://www.cnblogs.com/yun965861480/p/13524377.html
Copyright © 2020-2023  润新知