潘志宝
2024-08-21 4d3533b6e75e6afa5af325288b03915715add4b6
提交 | 用户 | 时间
a6de49 1 //package com.iailab.common.aspect;
H 2 //
3 //import com.alibaba.fastjson.JSONObject;
4 //import com.alibaba.fastjson.parser.Feature;
5 //import com.iailab.common.annotation.Dict;
6 //import com.iailab.framework.common.page.PageData;
7 //import com.iailab.framework.common.util.object.ConvertUtils;
8 //import com.iailab.common.utils.PageUtils;
9 //import com.iailab.common.utils.R;
10 //import com.iailab.common.utils.CommonResult;
11 //import com.iailab.entity.SysDictItem;
12 //import com.iailab.feign.IFeignSystemController;
13 //import lombok.extern.slf4j.Slf4j;
14 //import org.aspectj.lang.ProceedingJoinPoint;
15 //import org.aspectj.lang.annotation.Around;
16 //import org.aspectj.lang.annotation.Aspect;
17 //import org.aspectj.lang.annotation.Pointcut;
18 //import javax.annotation.Resource;
19 //import org.springframework.stereotype.Component;
20 //import org.springframework.util.CollectionUtils;
21 //
22 //import java.beans.IntrospectionException;
23 //import java.beans.PropertyDescriptor;
24 //import java.lang.reflect.Field;
25 //import java.lang.reflect.InvocationTargetException;
26 //import java.lang.reflect.Method;
27 //import java.util.HashMap;
28 //import java.util.List;
29 //import java.util.Map;
30 //
31 ///**
32 // * @author PanZhibao
33 // * @Description
34 // * @createTime 2022年05月21日 09:57:00
35 // */
36 //@Aspect
37 //@Component
38 //@Slf4j
39 //public class DictAspect {
40 //
41 //    private final String PAGE_CODE = "page";
42 //
43 //    private final String DATA_CODE = "data";
44 //
45 //    @Resource
46 //    private IFeignSystemController feignSystemController;
47 //
48 //    /**
49 //     * 定义切点Pointcut
50 //     */
51 //    @Pointcut("@annotation(com.iailab.common.annotation.AutoDict)")
52 //    public void excudeService() {
53 //    }
54 //
55 //    @Around("excudeService()")
56 //    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
57 //        Object result = pjp.proceed();
58 //        try {
59 //            this.parseDictText(result);
60 //        } catch (Exception ex) {
61 //            log.info(ex.getMessage());
62 //        }
63 //        return result;
64 //    }
65 //
66 //    /**
67 //     * parseDictText
68 //     *
69 //     * @param result
70 //     * @throws IntrospectionException
71 //     * @throws InvocationTargetException
72 //     * @throws IllegalAccessException
73 //     */
74 //    private void parseDictText(Object result) throws IntrospectionException, InvocationTargetException, IllegalAccessException {
75 //        Map<String, List<SysDictItem>> mapList = feignSystemController.queryAllDictItems();
76 //        Map<String,Map<String, String>> map = new HashMap<>();
77 //        if (!CollectionUtils.isEmpty(mapList)) {
78 //            mapList.forEach((k , v) -> {
79 //                Map<String, String> dictItems = new HashMap<>();
80 //                if (!CollectionUtils.isEmpty(v)) {
81 //                    v.forEach(item -> {
82 //                        dictItems.put(item.getItemValue(), item.getItemText());
83 //                    });
84 //                }
85 //                map.put(k, dictItems);
86 //            });
87 //        }
88 //
89 //        if (result instanceof R) {
90 //            if (((R) result).get(PAGE_CODE) instanceof PageUtils) {
91 //                PageUtils page =(PageUtils)((R) result).get(PAGE_CODE);
92 //                for (Object record : page.getList()) {
93 //                    this.setDictText(record, map);
94 //                }
95 //            } else if(((R) result).get(DATA_CODE) instanceof PageUtils) {
96 //                PageUtils page =(PageUtils)((R) result).get(DATA_CODE);
97 //                for (Object record : page.getList()) {
98 //                    this.setDictText(record, map);
99 //                }
100 //            }
101 //        } else if(result instanceof CommonResult) {
102 //            if(((CommonResult) result).getData() != null) {
103 //                Object data = ((CommonResult) result).getData();
104 //                if (data instanceof List) {
105 //                    for (Object record : (List)data) {
106 //                        this.setDictText(record, map);
107 //                    }
108 //                } else if(data instanceof PageData) {
109 //                    Object list = ((PageData)data).getList();
110 //                    for (Object record : (List)list) {
111 //                        this.setDictText(record, map);
112 //                    }
113 //                } else {
114 //                    this.setDictText(data, map);
115 //                }
116 //            }
117 //        } else if (result instanceof List){
118 //            for (Object record : (List)result) {
119 //                this.setDictText(record, map);
120 //            }
121 //        }
122 //    }
123 //
124 //    /**
125 //     * setDictText
126 //     *
127 //     * @param record
128 //     * @param map
129 //     * @throws IntrospectionException
130 //     * @throws InvocationTargetException
131 //     * @throws IllegalAccessException
132 //     */
133 //    private void setDictText(Object record, Map<String,Map<String, String>> map) throws IntrospectionException, InvocationTargetException, IllegalAccessException {
134 //        JSONObject item = JSONObject.parseObject(JSONObject.toJSONString(record), Feature.OrderedField);
135 //        for (Field field : ConvertUtils.getAllFields(record)) {
136 //            if (!field.isAnnotationPresent(Dict.class)) {
137 //                continue;
138 //            }
139 //            String dictCode = field.getAnnotation(Dict.class).dicCode();
140 //            String itemValue = field.getAnnotation(Dict.class).itemValue();
141 //            String value = item.getString(itemValue);
142 //            Method method = new PropertyDescriptor(field.getName(), record.getClass()).getWriteMethod();
143 //            method.invoke(record, this.getDictText(dictCode, value, map));
144 //        }
145 //    }
146 //
147 //    /**
148 //     * getDictText
149 //     *
150 //     * @param dictCode
151 //     * @param itemValue
152 //     * @param map
153 //     * @return
154 //     */
155 //    private String getDictText(String dictCode, String itemValue, Map<String,Map<String, String>> map) {
156 //        String dictText = "";
157 //        try{
158 //            if (map != null || map.containsKey(dictCode)) {
159 //                dictText = map.get(dictCode).get(itemValue);
160 //            }
161 //        } catch (Exception ex) {
162 //            log.info("字典异常:" + dictCode );
163 //        }
164 //        return dictText;
165 //    }
166 //}