利用Java反射实现对象实体类中的字典码code字段设置对应的name字段

新建一个自定义注解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)
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七月中cc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值