新建一个自定义注解SysCodeAnnotation、SysCodeShowTypeEnum
SysCodeAnnotation
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface SysCodeAnnotation {
/**
* codeType
*
* @return 返还字典类型
*/
String type() default "";
AjSysCodeShowTypeEnum showType() default AjSysCodeShowTypeEnum.SINGLE;
/**
* 是否混合类型
*/
boolean includeMultipleType() default false;
/**
* 多种类型
*
* @return 多种类型
*/
String[] multipleType() default {};
}
SysCodeShowTypeEnum
public enum SysCodeShowTypeEnum {
ALL,
SINGLE
;
}
新建一个工具类SysCodeUtils
@Component
public class SysCodeUtils {
@Autowired
private ISysCodeService sysCodeService;
@SneakyThrows
public <T> void convertCodeValue2CodeName(List<T> tList) {
if (CollectionUtils.isEmpty(tList)) {
return new ArrayList<>();
}
tList.forEach(this::convertCodeValue2CodeName);
}
/**
* 单个对象转换
* @param t
* @param <T>
*/
@SneakyThrows
public <T> void convertCodeValue2CodeName(T t) {
Map<String, Map<String, Object>> typeValueNameMap = new HashMap<>();
Map<String, Map<String, String>> typeValuePvalueMap = new HashMap<>();
Class aClazz = t.getClass();
Field[] declaredFields = aClazz.getDeclaredFields();
for (Field declaredField : declaredFields) {
declaredField.setAccessible(true);
SysCodeAnnotation sysCodeAnno = declaredField.getAnnotation(SysCodeAnnotation.class);
if (ObjectUtils.isEmpty(sysCodeAnno)) {
continue;
}
String type = sysCodeAnno.type();
Map<String, Object> map = typeValueNameMap.get(type);
Map<String, String> pMap = typeValuePvalueMap.getOrDefault(type, new HashMap<>());
if (ObjectUtils.isEmpty(map)) {
List<String> codeTypeList = new ArrayList<>();
codeTypeList.add(type);
if (sysCodeAnno.includeMultipleType()) {
String[] multipleType = sysCodeAnno.multipleType();
if (ObjectUtils.isNotEmpty(multipleType)) {
List<String> multipleTypeList = Arrays.stream(multipleType).collect(Collectors.toList());
if (!CollectionUtils.isEmpty(multipleTypeList)) {
codeTypeList.addAll(multipleTypeList);
}
}
}
List<SysCodeEntity> sysCodeEntityList = sysCodeService.list(new QueryWrapper<SysCodeEntity>().in("code_type", codeTypeList));
Map<String, String> valuePvalueMap = sysCodeEntityList.stream().filter(sysCodeEntity -> StringUtils.isNoneBlank(sysCodeEntity.getPCodeValue()) && StringUtils.isNoneBlank(sysCodeEntity.getCodeValue())).collect(Collectors.toMap(SysCodeEntity::getCodeValue, SysCodeEntity::getPCodeValue));
Map<String, Object> valueNameMap = sysCodeEntityList.stream().collect(Collectors.toMap(SysCodeEntity::getCodeValue, SysCodeEntity::getCodeName));
typeValueNameMap.put(type, valueNameMap);
typeValuePvalueMap.put(type, valuePvalueMap);
map = valueNameMap;
pMap = valuePvalueMap;
}
Object codeValue = declaredField.get(t);
if (!ObjectUtils.isEmpty(codeValue)) {
Object codeName = map.get(codeValue.toString());
if (sysCodeAnno.showType() == AjSysCodeShowTypeEnum.ALL) {
while (ObjectUtils.isNotEmpty(codeValue) && StringUtils.isNoneBlank(pMap.get(codeValue.toString()))) {
codeValue = pMap.get(codeValue.toString());
codeName = map.get(codeValue) + "/" + codeName;
}
}
Field codeNameField = aClazz.getDeclaredField(declaredField.getName() + "Name");
codeNameField.setAccessible(true);
if (!ObjectUtils.isEmpty(codeNameField)) {
codeNameField.set(t, codeName);
}
}
}
}
}
测试
实体类中stableStatus的值是字典码的code值,需设置stableStatusName的值;
administrativeArea对应的是字典码administrative_area、p_administrative_area两个code中的值,设置administrativeAreaName的值
@Data
public class User {
@ApiModelProperty(value = "稳定状态-STABLE_STATUS")
@AjSysCode(type = "STABLE_STATUS")
private String stableStatus;
@ApiModelProperty(value = "稳定状态名称")
private String stableStatusName;
@ApiModelProperty("行政区域(字典码)")
@AjSysCode(type = "administrative_area", includeMultipleType = true, multipleType = {"administrative_area", "p_administrative_area"})
private String administrativeArea;
@ApiModelProperty(value = "行政区域名称")
private String administrativeAreaName;
}
使用SysCodeUtils工具类进行转换
User user = new User();
SysCodeUtils.convertCodeValue2CodeName(user)