Kaynağa Gözat

数据字典翻译改造(支持List和实体类)

LLL 9 ay önce
ebeveyn
işleme
2f33961e5d

+ 101 - 1
jeecg-boot-base-core/src/main/java/org/jeecg/common/aspect/DictAspect.java

@@ -4,6 +4,8 @@ import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.alibaba.fastjson.parser.Feature;
 import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
+import com.fasterxml.jackson.annotation.JsonFormat;
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import lombok.extern.slf4j.Slf4j;
@@ -24,6 +26,7 @@ import org.springframework.stereotype.Component;
 import org.springframework.util.StringUtils;
 
 import java.lang.reflect.Field;
+import java.text.SimpleDateFormat;
 import java.util.*;
 import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
@@ -58,7 +61,7 @@ public class DictAspect {
 
     @Around("excudeService()")
     public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
-    	long time1=System.currentTimeMillis();	
+    	long time1=System.currentTimeMillis();
         Object result = pjp.proceed();
         long time2=System.currentTimeMillis();
         log.debug("获取JSON数据 耗时:"+(time2-time1)+"ms");
@@ -198,6 +201,103 @@ public class DictAspect {
                 }
 
                 ((IPage) ((Result) result).getResult()).setRecords(items);
+            } else if (((Result) result).getResult() instanceof List) {
+                List result1 = (List) ((Result) result).getResult();
+                if (CollectionUtils.isNotEmpty(result1)) {
+                    Class<?> aClass = result1.get(0).getClass();
+                    String className = aClass.getName();
+                    log.info("------- List<T> 泛型 T is {}", className);
+                    //只解析result中list泛型是object的数据, 基本数据类型不解析
+                    /**
+                     * 以下类型不解析
+                     * java.lang.String
+                     * java.lang.long
+                     * java.lang.Integer
+                     */
+                    if (!className.equals("java.lang.String") && !className.equals("java.lang.long") && !className.equals("java.lang.Integer")) {
+                        List<JSONObject> items = new ArrayList<>();
+                        for (Object record : (List) ((Result) result).getResult()) {
+                            ObjectMapper mapper = new ObjectMapper();
+                            String json = "{}";
+                            try {
+                                //解决@JsonFormat注解解析不了的问题详见SysAnnouncement类的@JsonFormat
+                                json = mapper.writeValueAsString(record);
+                            } catch (JsonProcessingException e) {
+                                log.error("json解析失败" + e.getMessage(), e);
+                            }
+                            JSONObject 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()));
+
+                                    //翻译字典值对应的txt
+                                    String 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()))));
+                                }
+                            }
+                            items.add(item);
+                        }
+                        ((Result) result).setResult(items);
+                    }else {
+                        log.info("只解析result中list<T>泛型是实体类的数据, 基本数据类型不解析: T is {}", className);
+                    }
+                }else {
+                    log.info(" result中List<T> 为空,跳过不解析 ");
+                }
+
+            }else {
+                Object record = (Object) ((Result) result).getResult();
+                ObjectMapper mapper = new ObjectMapper();
+                String json = "{}";
+                try {
+                    //解决@JsonFormat注解解析不了的问题详见SysAnnouncement类的@JsonFormat
+                    json = mapper.writeValueAsString(record);
+                } catch (JsonProcessingException e) {
+                    log.error("json解析失败" + e.getMessage(), e);
+                }
+                try {
+                    JSONObject 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()));
+
+                            //翻译字典值对应的txt
+                            String 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()))));
+                        }
+                    }
+                    ((Result) result).setResult(item);
+                } catch (Exception e) {
+                    log.info("########################此返回类型不支持字典翻译########################");
+                }
             }
 
         }