| package com.mzl.flower.base.interceptor; | 
|   | 
| import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | 
| import com.mzl.flower.base.AbstractTransDTO; | 
| import com.mzl.flower.base.ReturnDataDTO; | 
| import com.mzl.flower.base.annotation.DictTrans; | 
| import com.mzl.flower.base.cache.DictCacheClient; | 
| import com.google.common.collect.ImmutableList; | 
| import lombok.extern.slf4j.Slf4j; | 
| import org.apache.commons.lang3.ArrayUtils; | 
| import org.apache.commons.lang3.StringUtils; | 
| import org.apache.commons.lang3.reflect.FieldUtils; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.context.i18n.LocaleContextHolder; | 
| import org.springframework.stereotype.Component; | 
|   | 
| import java.lang.reflect.Field; | 
| import java.lang.reflect.ParameterizedType; | 
| import java.util.*; | 
| import java.util.stream.Collectors; | 
|   | 
| @Component | 
| @Slf4j | 
| public class CodeDecorator { | 
|     private static final String DELIMITER = ","; | 
|   | 
|     @Autowired | 
|     private DictCacheClient dictCacheClient; | 
|   | 
|     public <T> T decorate(T object) { | 
|         if (object instanceof ReturnDataDTO) { | 
|             Object data = ((ReturnDataDTO) object).getData(); | 
|             if (data instanceof Page) { | 
|                 for (Object o : ((Page) data).getRecords()) { | 
|                     if (o instanceof AbstractTransDTO) { | 
|                         initCodeValue(o); | 
|                     } | 
|                 } | 
|             } else if (data instanceof AbstractTransDTO) { | 
|                 initCodeValue(data); | 
|             } else if (data instanceof List) { | 
|                 checkList((List) data); | 
|             } | 
|         } else if (object instanceof AbstractTransDTO) { | 
|             initCodeValue(object); | 
|         } else if (object instanceof List) { | 
|             checkList((List) object); | 
|         } | 
|         return object; | 
|     } | 
|   | 
|     private void checkList(List list) { | 
|         for (Object o : list) { | 
|             if (o instanceof AbstractTransDTO) { | 
|                 initCodeValue(o); | 
|             } | 
|         } | 
|     } | 
|   | 
|     private void initCodeValue(Object content) { | 
|         if (null == content) { | 
|             return; | 
|         } | 
|         try { | 
|             Field[] fields = FieldUtils.getAllFields(content.getClass()); | 
|             for (Field field : fields) { | 
|                 field.setAccessible(true); | 
|                 if (AbstractTransDTO.class.isAssignableFrom(field.getType())) { | 
|                     initCodeValue(field.get(content)); | 
|                 } else if (Collection.class.isAssignableFrom(field.getType()) && | 
|                         field.getGenericType() instanceof ParameterizedType && | 
|                         ArrayUtils.isNotEmpty(((ParameterizedType) field.getGenericType()).getActualTypeArguments()) && | 
|                         AbstractTransDTO.class.isAssignableFrom((Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0])) { | 
|                     if (null != field.get(content)) { | 
|                         Optional.of((Collection) field.get(content)).ifPresent(objects -> objects.forEach(this::initCodeValue)); | 
|                     } | 
|                 } else { | 
|                      if (field.isAnnotationPresent(DictTrans.class)) { | 
|   | 
|                         field.setAccessible(true); | 
|                         DictTrans dictTrans = field.getAnnotation(DictTrans.class); | 
|                         Object value = field.get(content); | 
|                         if(value == null){ | 
|                             continue; | 
|                         } | 
|                         String codeType = dictTrans.codeType(); | 
|                         if (codeType.trim().length() == 0 && StringUtils.isNotEmpty(dictTrans.cascadeField())) { | 
|                             codeType = String.valueOf(FieldUtils.getField(content.getClass(), dictTrans.cascadeField(), true).get(content)); | 
|                         } | 
|                         String finalCodeType = codeType; | 
|   | 
|                         String stringValue = null; | 
|                         if (value instanceof String || value instanceof Number) { | 
|                             stringValue = String.valueOf(value); | 
|                         } else if (value instanceof Enum) { | 
|                             stringValue = ((Enum) value).name(); | 
|                         } else if (value instanceof List) { | 
|                             if(((List)value).size() > 0){ | 
|                                 stringValue = ""; | 
|                                 int i = 0; | 
|                                 for(Object o : ((List)value)){ | 
|                                     if(i > 0){ | 
|                                         stringValue += ","; | 
|                                     } | 
|                                     stringValue += o; | 
|                                     i++; | 
|                                 } | 
|                             } | 
|                         } | 
|                         if (stringValue != null) { | 
|                             String dictValue = getDictValue(stringValue, finalCodeType); | 
|                             Field targetField = FieldUtils.getField(content.getClass(), dictTrans.target(), true); | 
|                             if (dictValue != null) { | 
|                                 targetField.set(content, dictValue); | 
|                             } else { | 
|                                 targetField.set(content, stringValue); | 
|                             } | 
|                         } | 
|                     } | 
|                 } | 
|             } | 
|         } catch (Exception e) { | 
|             log.error(e.getMessage()); | 
|         } | 
|     } | 
|   | 
|     private String getDictValue(String stringValue, String finalCodeType){ | 
|         String iCodeType = null; | 
|         Locale locale = LocaleContextHolder.getLocale(); | 
|         if(!locale.getLanguage().equals(Locale.CHINA.getLanguage())){ | 
|             iCodeType = finalCodeType + "_" + locale.getLanguage(); | 
|         } | 
|   | 
|         String dictValue; | 
|         if (stringValue.contains(DELIMITER)) { | 
|             List<String> vv = new ArrayList<>(); | 
|             for(String s : stringValue.split(DELIMITER)){ | 
|                 String ss = s.trim(); | 
|                 if(StringUtils.isEmpty(ss)){ | 
|                     continue; | 
|                 } | 
|                 String value = ""; | 
|                 if(iCodeType != null){ | 
|                     value = dictCacheClient.getDict(iCodeType, ss); | 
|                 } | 
|                 if(StringUtils.isEmpty(value)){ | 
|                     value = dictCacheClient.getDict(finalCodeType, ss); | 
|                 } | 
|                 vv.add(value); | 
|             } | 
|             dictValue = StringUtils.join(vv, DELIMITER); | 
|         } else { | 
|             dictValue = dictCacheClient.getDict(finalCodeType, stringValue); | 
|         } | 
|   | 
|         return dictValue; | 
|     } | 
| } |