1.添加文件
2.DimName注解,实体使用
package annotation;
import MateTypeEnum;
import java.lang.annotation.*;
/**
* 字典翻译注解
*
* @author pw
*/
@Documented
@Target(ElementType.FIELD)// 可用在方法名上
@Retention(RetentionPolicy.RUNTIME)// 运行时有效
public @interface DimName {
/**
* 该字段代表字典类型的编码
*/
//Class clazz() default this.clazz();
Class<? extends Enum<?>> clazz() default MateTypeEnum.class;
String type() default "";//DIM-维度 ""-常量
/**
* 所映射字段名,没有则覆盖当前字段值
*/
String fieldName() default "";
}
3.DimVerify注解
package annotation;
import java.lang.annotation.*;
/**
* @author pw
*/
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DimVerify {
}
3.DimVerifyAspect,aop切面
package aop;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
@Aspect
@Component
@Slf4j
public class DimVerifyAspect {
@AfterReturning(value = "@annotation( com.mdfp.dim.sdk.annotation.DimVerify)", returning = "obj")
public void doAround(Object obj) throws Throwable {
this.getDictValue(obj);
}
private void getDictValue(Object data) throws Throwable {
if (null == data) {
return;
}
List<Object> list;
if (data instanceof List) {
list = (List) data;
} else if (data instanceof Pagination) {
list = ((Pagination) data).getData();
} else if (data instanceof IPage) {
list = ((IPage) data).getRecords();
} else {
list = new ArrayList<>();
list.add(data);
}
if (null == list || list.size() == 0) {
return;
}
//反射获取返回date中的属性
Class<?> clazz = list.get(0).getClass();
Field[] fieldArr = clazz.getDeclaredFields();
// 获得父类的字节码对象
for (Object object : list) {
for (Field field : fieldArr) {
//获取属性类型
if (field.isAnnotationPresent(DimName.class)) {
DimName dimName = field.getAnnotation(DimName.class);
String methodeName = dimName.fieldName();//默认字段名
if (ObjectUtil.isEmpty(methodeName)) {
methodeName = field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1);
}
Object fileldDataO = ReflectionUtils.invokeMethod(object, "get" + methodeName,
new Class[]{}, new Object[]{});//获取值
if (ObjectUtil.isNotEmpty(fileldDataO)) {
Class<Enum> dimNameClazz = (Class<Enum>) Class.forName(dimName.clazz().getName());
String type = dimName.type();//DIM-维度 ""-常量
if (type.equals("DIM")) {
MdfpBaDimObjectDTO mdfpBaDimObjectDTO = DimUtils.getBaDimObjectById(fileldDataO.toString());
if (mdfpBaDimObjectDTO != null) {
setDimNameData(object, methodeName, mdfpBaDimObjectDTO.getObjectName());
}
} else {
Enum[] objects = dimNameClazz.getEnumConstants();
try {
Method getCode = dimNameClazz.getMethod("getCode");
Method getAlias = dimNameClazz.getMethod("getAlias");
for (Enum e : objects) {
Object eCode = getCode.invoke(e);
String eName = e.name();
if ((eName.contains(methodeName) || eName.contains(DataUtils.camelToUnderline(methodeName))) && ObjectUtil.isNotEmpty(eCode)) {
if (eCode.toString().equals(fileldDataO.toString())) {
setDimNameData(object, methodeName, getAlias.invoke(e));
}
}
}
} catch (Exception e) {
log.error("枚举类型没有getCode或getAlias方法");
}
}
}
}
}
}
}
public void setDimNameData(Object object, String methodeName, Object data) {
Method method = null;
try {
method = object.getClass().getMethod("set" + methodeName + "Name", String.class);
if (ObjectUtil.isNotEmpty(data)) {
method.invoke(object, data.toString());
}
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
4.实体使用
@DimName(clazz = AbDiApiEnum.class)
private String interfaceType;//接口类型
@DimName()
private String state;//状态
5.aop使用
@Override
@DimVerify
public IPage<MdfpAbDiApiDTO> selectMaxApiPage(MdfpAbDiApiQueryVO mdfpAbDiApiQueryVO, Integer pageIndex, Integer pageRows) {
return mdfpAbDiApiMapper.selectMaxApiPage(new Page(pageIndex, pageRows), mdfpAbDiApiQueryVO);
}
@ApiOperation(value = "查询最新版本接口API分页列表", notes = "查询最新版本接口API分页列表", httpMethod = "GET")
@ApiResponses(@ApiResponse(code = 800, message = "ok"))
@PostMapping(value = "/selectMaxApiPage")
@DimVerify
public ResponseData<Pagination<MdfpAbDiApiDTO>> selectMaxApiPage(@RequestBody PaginationRequestData<MdfpAbDiApiQueryVO> requestData) {
Pagination<MdfpAbDiApiDTO> mdfpAbDiApiDTOPage = mdfpAbDiApiPolyService.selectMaxApiPage(requestData.getParams(), requestData.getPageIndex(), requestData.getPageRows());
return new ResponseData<>(StatusCodeEnum.SUCCESS_CODE.getStatusCode(), mdfpAbDiApiDTOPage, "查询成功", LanguageEnum.GENERAL_QUERY_SUCCESS.getCode(), new Object[]{});
}