DictionaryAspect.java 3.95 KB
package cn.com.glsx.entmicro.config;

import cn.com.glsx.entmicro.annotation.Dictionary;
import cn.com.glsx.entmicro.model.response.DictDataResponse;
import cn.com.glsx.entmicro.model.response.UserResponse;
import cn.com.glsx.entmicro.service.MerchantService;
import com.github.pagehelper.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author zhouhaibao
 * @version 1.0
 * @description: 字典统一处理
 * @date 2021/09/07
 */
@Slf4j
@Aspect
@Component
public class DictionaryAspect {

    @Autowired
    private MerchantService merchantService;

    @Pointcut("@annotation(cn.com.glsx.entmicro.annotation.DictionarySupport)")
    public void logPointCut() {
    }

    @Before("logPointCut()")
    public void webpre(JoinPoint joinPoint) {

    }

    /**
     * 方法之后调用
     *
     * @param jp
     * @param returnValue 方法返回值
     */
    @AfterReturning(pointcut = "logPointCut()", returning = "returnValue")
    public void doAfterReturning(JoinPoint jp, Object returnValue) {
        Class<?> childClass = returnValue.getClass();
        List fieldValue = new ArrayList();
        if (returnValue instanceof Object) {
            if (returnValue instanceof Page) {
                fieldValue = (List) returnValue;
            } else {
                fieldValue.add(returnValue);
            }
        } else {
            fieldValue = (List) returnValue;
        }
        // 设置字典值
        List<DictDataResponse> allByDictType = merchantService.findAllByDictType("");
        fieldValue.stream().forEach(obj -> {
            Class clazz = obj.getClass();
            Stream<Field> stream = Arrays.stream(getAllFields(clazz));
            stream.filter(field1 ->
                    field1.getAnnotation(Dictionary.class) != null
            ).forEach(field2 -> {
                try {
                    Dictionary annotation = field2.getAnnotation(Dictionary.class);
                    field2.setAccessible(true);
                    Object oldValue = field2.get(obj);
                    if (annotation != null && oldValue != null) {
                        String dicttype = annotation.dicttype();
                        // 设置参数的值
                        String dictvalue = annotation.dictvalue();
                        //3.2 获取field的值
                        String newValue = null;
                        if (CollectionUtils.isNotEmpty(allByDictType)) {
                            List<String> collect = allByDictType.stream().filter(index -> index.getDictValue().equals(oldValue) && index.getDictType().equals(dicttype)).map(DictDataResponse::getDictLabel).collect(Collectors.toList());
                            if (CollectionUtils.isNotEmpty(collect)) {
                                newValue = collect.get(0);
                            }
                        }
                        Field declaredField = clazz.getDeclaredField(dictvalue);
                        declaredField.setAccessible(true);
                        declaredField.set(obj, newValue);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        });

    }

    /**
     * 获取本类的属性的方法
     *
     * @param clazz 当前类对象
     * @return 字段数组
     */

    private static Field[] getAllFields(Class<?> clazz) {
        Field[] declaredFields = clazz.getDeclaredFields();
        return declaredFields;
    }

}