1. 工具类转换
- 定义装换枚举转换类型
public enum UnitConvertType {
ACCURACY,
TEN_THOUSAND_YUAN,
PERCENTAGE,
PERMIL;
}
- 创建转换工具类,
1. 通过反射去除字段,
2.对照传入map标记的字段需要转换的类型转换
3. 重新赋值
@Slf4j
public class UnitConvertUtil {
public static <T> void unitMapConvert(List<T> list, Map<String, UnitConvertType> propertyMap){
list.forEach(item -> {
Field[] fields = item.getClass().getDeclaredFields();
for (Field field : fields) {
if(propertyMap.containsKey(field.getName())){
try {
field.setAccessible(true);
Object value = field.get(item);
UnitConvertType unitConvertType = propertyMap.get(field.getName());
if(value != null){
BigDecimal bigDecimal;
if(unitConvertType == UnitConvertType.PERCENTAGE){
bigDecimal = ((BigDecimal) value).multiply(BigDecimal.valueOf(100));
field.set(item,bigDecimal);
}else if(unitConvertType == UnitConvertType.PERMIL){
bigDecimal = ((BigDecimal) value).multiply(BigDecimal.valueOf(1000));
field.set(item,bigDecimal);
}else if(unitConvertType == UnitConvertType.TEN_THOUSAND_YUAN){
bigDecimal = ((BigDecimal) value).divide(BigDecimal.valueOf(10000)).setScale(2, RoundingMode.HALF_UP);
field.set(item,bigDecimal);
}else if(unitConvertType == UnitConvertType.ACCURACY){
bigDecimal = ((BigDecimal) value).setScale(2, RoundingMode.HALF_UP);
field.set(item,bigDecimal);
}else{
log.error("未知的单位转换类型");
}
}
}catch (Exception e){
log.error("属性转换失败",e);
}
}
}
});
}
public static void main(String[] args) {
SumReport sumReport = new SumReport();
sumReport.setPayTotalAmount(new BigDecimal(2390000));
sumReport.setJcAmountPer(BigDecimal.valueOf(0.885));
sumReport.setJcCountPer(BigDecimal.valueOf(0.2394));
sumReport.setLength(BigDecimal.valueOf(1700.64003));
List<SumReport> list = new ArrayList<>();
list.add(sumReport);
Map<String, UnitConvertType> map =new HashMap<>();
map.put("payTotalAmount", UnitConvertType.TEN_THOUSAND_YUAN);
map.put("jcAmountPer", UnitConvertType.PERCENTAGE);
map.put("jcCountPer", UnitConvertType.PERMIL);
map.put("length", UnitConvertType.ACCURACY);
unitMapConvert(list,map);
System.out.println(list);
}
}
2. 注解转换
- 定义注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface UnitBigDecConvert {
UnitConvertType name();
}
- 解析注解
public static <T> void unitAnnotationConvert(List<T> list){
list.forEach(item -> {
Field[] fields = item.getClass().getDeclaredFields();
for (Field field : fields) {
try {
UnitBigDecConvert annotation = field.getAnnotation(UnitBigDecConvert.class);
if(annotation == null){
continue;
}
UnitConvertType name = annotation.name();
field.setAccessible(true);
Object o = field.get(item);
if(o != null){
if(name == UnitConvertType.PERCENTAGE){
BigDecimal bigDecimal = ((BigDecimal) o).multiply(BigDecimal.valueOf(100));
field.set(item,bigDecimal);
}else if(name == UnitConvertType.PERMIL){
BigDecimal bigDecimal = ((BigDecimal) o).multiply(BigDecimal.valueOf(1000));
field.set(item,bigDecimal);
}else if(name == UnitConvertType.TEN_THOUSAND_YUAN){
BigDecimal bigDecimal = ((BigDecimal) o).divide(BigDecimal.valueOf(10000)).setScale(2, RoundingMode.HALF_UP);
field.set(item,bigDecimal);
}else if (name == UnitConvertType.ACCURACY){
BigDecimal bigDecimal = ((BigDecimal) o).setScale(2, RoundingMode.HALF_UP);
field.set(item,bigDecimal);
}else {
log.error("未知的单位转换类型");
}
}
} catch (IllegalAccessException e) {
log.error("装换失败",e);
}
}
});
}
public static void main(String[] args) {
SumReport sumReport = new SumReport();
sumReport.setPayTotalAmount(new BigDecimal(2390000));
sumReport.setJcAmountPer(BigDecimal.valueOf(0.885));
sumReport.setJcCountPer(BigDecimal.valueOf(0.2394));
sumReport.setLength(BigDecimal.valueOf(1700.64003));
List<SumReport> list = new ArrayList<>();
list.add(sumReport);
unitAnnotationConvert(list);
System.out.println(list);
}