陶杰
2024-08-22 ee9032d9baf5f33e376d2d2699136e0a7b26bec7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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;
    }
}