糊涂包
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.26</version>
</dependency>
切点注解
/**
* aop数据字典拦截
* Author:daWang
* Date:2024/3/4 16:24
*/
public @interface DicAop {
}
属性注解
/**
* Author:daWang
* Date:2024/3/4 16:32
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DicData {
//数据字典分类
String dicType() default "";
}
翻译类
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.extra.cglib.CglibUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crmzf.cloud.api.annotation.DicData;
import com.crmzf.cloud.common.util.Result;
import com.crmzf.cloud.system.entity.SysDictDataEntity;
import com.crmzf.cloud.system.util.DictUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;
/**
* 数据字典统一处理
* Author:daWang
* Date:2024/3/4 16:20
*/
@Slf4j
@Aspect
@Component
public class DicAopAspect {
@Pointcut("@annotation(com.crmzf.cloud.api.aspectj.DicAop)")
public void pointCut() {
}
@AfterReturning(pointcut = "pointCut()", returning = "jsonResult")
public void doAfterReturning(JoinPoint joinPoint, Object jsonResult) {
if (jsonResult instanceof Result) {
Result result = (Result) jsonResult;
Object data = result.getData();
if (data instanceof Page) {
Page page = (Page) data;
List records = page.getRecords();
if (CollUtil.isNotEmpty(records)) {
Object o = records.stream().findFirst().get();
Field[] declaredFields = ClassUtil.getDeclaredFields(o.getClass());
List<Field> collect = Arrays.stream(declaredFields).filter(item -> {
DicData annotation = item.getAnnotation(DicData.class);
return Objects.nonNull(annotation) && StringUtils.isNotBlank(annotation.dicType()) ? true : false;
}).collect(Collectors.toList());
if (CollUtil.isNotEmpty(collect)) {
//准备数据字典
HashMap<Field, List<SysDictDataEntity>> map = new HashMap<>();
for (Field field : collect) {
List<SysDictDataEntity> dictCache = DictUtils.getDictCache(field.getAnnotation(DicData.class).dicType());
map.put(field, dictCache);
}
//遍历对象封装字典翻译
Object transBean = records.stream().map(item -> {
Map<String, Object> bean = BeanUtil.beanToMap(item);
for (Map.Entry<Field, List<SysDictDataEntity>> fieldListEntry : map.entrySet()) {
Field key = fieldListEntry.getKey();
String dicType = key.getAnnotation(DicData.class).dicType();
List<SysDictDataEntity> value = fieldListEntry.getValue();
String name = key.getName();
Object o1 = "";//字典值
try {
Field field = ReflectUtil.getField(item.getClass(), name);
field.setAccessible(true);
o1 = field.get(item);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
for (SysDictDataEntity entity : value) {
String dictLabel = entity.getDictLabel();
String dictValue = entity.getDictValue();
if (o1.toString().equals(dictValue)) {
String label = name + "DictLabel";
bean.put(label, dictLabel);
log.debug("字典分类:{},字典值:{},字典翻译:{},", dicType, dictValue, label);
}
}
}
return bean;
}).collect(Collectors.toList());
page.setRecords((List) transBean);
}
}
}
}
}
}
Bean
Controlle
接口返回