houzhongjian
2024-07-23 a6de490948278991e47952e90671ddba4555e9a2
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
//package com.iailab.common.aspect;
//
//import com.alibaba.fastjson.JSONObject;
//import com.alibaba.fastjson.parser.Feature;
//import com.iailab.common.annotation.Dict;
//import com.iailab.framework.common.page.PageData;
//import com.iailab.framework.common.util.object.ConvertUtils;
//import com.iailab.framework.common.pojo.CommonResult;
//import com.iailab.entity.SysDictItem;
//import com.iailab.module.infra.api.config.ConfigApi;
//import lombok.extern.slf4j.Slf4j;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.Around;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Pointcut;
//import javax.annotation.Resource;
//import org.springframework.stereotype.Component;
//import org.springframework.util.CollectionUtils;
//
//import java.beans.IntrospectionException;
//import java.beans.PropertyDescriptor;
//import java.lang.reflect.Field;
//import java.lang.reflect.InvocationTargetException;
//import java.lang.reflect.Method;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
///**
// * @author PanZhibao
// * @Description
// * @createTime 2022年05月21日 09:57:00
// */
//@Aspect
//@Component
//@Slf4j
//public class DictAspect {
//
//    private final String PAGE_CODE = "page";
//
//    private final String DATA_CODE = "data";
//
//    @Resource
//    private ConfigApi configApi;
//
//    /**
//     * 定义切点Pointcut
//     */
//    @Pointcut("@annotation(com.iailab.common.annotation.AutoDict)")
//    public void excudeService() {
//    }
//
//    @Around("excudeService()")
//    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
//        Object result = pjp.proceed();
//        try {
//            this.parseDictText(result);
//        } catch (Exception ex) {
//            log.info(ex.getMessage());
//        }
//        return result;
//    }
//
//    /**
//     * parseDictText
//     *
//     * @param result
//     * @throws IntrospectionException
//     * @throws InvocationTargetException
//     * @throws IllegalAccessException
//     */
//    private void parseDictText(Object result) throws IntrospectionException, InvocationTargetException, IllegalAccessException {
//        Map<String, List<SysDictItem>> mapList = configApi.queryAllDictItems();
//        Map<String,Map<String, String>> map = new HashMap<>();
//        if (!CollectionUtils.isEmpty(mapList)) {
//            mapList.forEach((k , v) -> {
//                Map<String, String> dictItems = new HashMap<>();
//                if (!CollectionUtils.isEmpty(v)) {
//                    v.forEach(item -> {
//                        dictItems.put(item.getItemValue(), item.getItemText());
//                    });
//                }
//                map.put(k, dictItems);
//            });
//        }
//
//        if(result instanceof CommonResult) {
//            if(((CommonResult) result).getData() != null) {
//                Object data = ((CommonResult) result).getData();
//                if (data instanceof List) {
//                    for (Object record : (List)data) {
//                        this.setDictText(record, map);
//                    }
//                } else if(data instanceof PageData) {
//                    Object list = ((PageData)data).getList();
//                    for (Object record : (List)list) {
//                        this.setDictText(record, map);
//                    }
//                } else {
//                    this.setDictText(data, map);
//                }
//            }
//        } else if (result instanceof List){
//            for (Object record : (List)result) {
//                this.setDictText(record, map);
//            }
//        }
//    }
//
//    /**
//     * setDictText
//     *
//     * @param record
//     * @param map
//     * @throws IntrospectionException
//     * @throws InvocationTargetException
//     * @throws IllegalAccessException
//     */
//    private void setDictText(Object record, Map<String,Map<String, String>> map) throws IntrospectionException, InvocationTargetException, IllegalAccessException {
//        JSONObject item = JSONObject.parseObject(JSONObject.toJSONString(record), Feature.OrderedField);
//        for (Field field : ConvertUtils.getAllFields(record)) {
//            if (!field.isAnnotationPresent(Dict.class)) {
//                continue;
//            }
//            String dictCode = field.getAnnotation(Dict.class).dicCode();
//            String itemValue = field.getAnnotation(Dict.class).itemValue();
//            String value = item.getString(itemValue);
//            Method method = new PropertyDescriptor(field.getName(), record.getClass()).getWriteMethod();
//            method.invoke(record, this.getDictText(dictCode, value, map));
//        }
//    }
//
//    /**
//     * getDictText
//     *
//     * @param dictCode
//     * @param itemValue
//     * @param map
//     * @return
//     */
//    private String getDictText(String dictCode, String itemValue, Map<String,Map<String, String>> map) {
//        String dictText = "";
//        try{
//            if (map != null || map.containsKey(dictCode)) {
//                dictText = map.get(dictCode).get(itemValue);
//            }
//        } catch (Exception ex) {
//            log.info("字典异常:" + dictCode );
//        }
//        return dictText;
//    }
//}