项目中金额格式化,码值翻译

Posted 诗萧尘

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了项目中金额格式化,码值翻译相关的知识,希望对你有一定的参考价值。

为了解决项目中金额格式化
码值翻译
处理的对象包括

ResultDto
List
实体类

常量的定义

/**
 *  参考
 */
public static class FORMATTER_AMO 
    public static final int FOR_W = 10000;
    public static final int FOR_Y = FOR_W * FOR_W;
    public static final int SCALE = 2;

 

1、编写注解处理金额的注解

/**
 * @author zhangcl
  格式化的位数,以及降为的都可以自己传
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AmoFormatt 
    int formatt() default CommonUserService.FORMATTER_AMO.FOR_W;
    int scale() default CommonUserService.FORMATTER_AMO.SCALE;

2 第二个注解
/**
 * @author zhangcl
 * 配置字典英文即可,多个可以使用逗号分隔。 XXX_LLL_MM,XX_MM_JJ
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CodeValueConversion 
    String value() default "";


2、编写切点注解

/**
 * @author zhangcl
 */
@Target(value = ElementType.METHOD )
@Retention(value = RetentionPolicy.RUNTIME)
public @interface ProcessResult 



3、编写切面类

@Component
@Aspect
@Slf4j
public class AmoFormattAspect 

    @Autowired
    private AdminSmLookupDictService adminSmLookupDictService;

    @AfterReturning(value = "@annotation(processResult)", returning = "result")
    public Object aftreReturning(JoinPoint joinPoint, ProcessResult processResult, Object result) throws IllegalAccessException 
        log.info(joinPoint.toString());
        log.info("<===================Aspect======================>");
        log.info(result.toString());

        if (result == null) 
            return null;
        
        if (result instanceof ResultDto) 
            Object body = ((ResultDto<?>) result).getData();
            if (body == null) 
                return null;
            
            if (body instanceof Collection) 
                for (Object obj : (Collection<?>) body) 
                    //处理
                    processObj(obj);
                
             else 
                //处理
                processObj(body);
            
         else if (result instanceof Collection) 
            //处理
            for (Object obj : (Collection<?>) result) 
                //处理
                processObj(obj);
            
         else 
            processObj(result);
        
        return result;
    

    private void processObj(Object obj) throws IllegalAccessException 
        //取出Obj所有 Field,以及父类 Field
        List<Field> fieldList = new ArrayList<>();
        Class<?> tempClass = obj.getClass();
        while (tempClass != null) 
            fieldList.addAll(Arrays.asList(tempClass.getDeclaredFields()));
            tempClass = tempClass.getSuperclass();
        
        Field[] fields = new Field[fieldList.size()];
        fieldList.toArray(fields);
        //遍历Field,查找添加 @SexValue 注解的字段 并 做翻译
        for (Field field : fields) 
            field.setAccessible(true);
            String name = field.getType().getName();
            Object fieldValue = field.get(obj);
            log.info("fieldValue:", fieldValue);
            if (null == fieldValue) 
                log.info("值为空不做转换");
                continue;
            
            //  金额转化
            if (field.isAnnotationPresent(AmoFormatt.class)) 
                AmoFormatt amoFormatt = field.getDeclaredAnnotation(AmoFormatt.class);
                int formattje = amoFormatt.formatt();
                if (formattje == 0) 
                    log.info("格式化金额 formattJe:,为零,转换成1", formattje);
                    formattje = 1;
                
                int scale = amoFormatt.scale();
                log.info("格式化金额 formattJe:,保留位数:", formattje, scale);
//                String name = field.getType().getName();
                if ("java.lang.Integer".equals(name) || "int".equals(name)) 
                    int value = (int) fieldValue;
                    value = value / formattje;
                    field.set(obj, value);
                 else if ("java.math.BigDecimal".equals(name)) 
                    BigDecimal value = (BigDecimal) fieldValue;
                    field.set(obj, divideBig(value, formattje, scale));
                 else if ("java.lang.Double".equals(name) || "double".equals(name)) 
                    Double value = (Double) fieldValue;
                    field.set(obj, divide(value, formattje, scale));
                
            
            // 码值转换
            if (field.isAnnotationPresent(CodeValueConversion.class) && name.equals("java.lang.String")) 
                CodeValueConversion codeValueConversion = field.getDeclaredAnnotation(CodeValueConversion.class);
                String dictionaryEnglish = codeValueConversion.value();
                if (StringUtils.isBlank(dictionaryEnglish)) 
                    log.info("字典配置为空,不进行转换");
                 else 
                    if (StringUtils.isNotBlank((String) fieldValue)) 
                    // 请更换成自己项目中的字典查询
                        Map<String, List<Map<String, Object>>> querylookupcode = adminSmLookupDictService.querylookupcode(dictionaryEnglish);
                        for (Map.Entry<String, List<Map<String, Object>>> entry : querylookupcode.entrySet()) 
                            String key = entry.getKey();
                            log.info("key:",key);
                            List<Map<String, Object>> value = entry.getValue();
                            for (Map<String, Object> it : value) 
                                if (fieldValue.equals(it.get("key"))) 
                                    field.set(obj, it.get("value"));
                                    break;
                                
                            
                        
                    

                
            

        
    


    public BigDecimal divideBig(BigDecimal v1, int v2, int scale) 
        return divide(v1, v2, scale, BigDecimal.ROUND_HALF_EVEN);
    

    public Double divide(double v1, int v2, int scale) 
        BigDecimal b1 = new BigDecimal(v1);
        return divide(b1, v2, scale, BigDecimal.ROUND_HALF_EVEN).doubleValue();
    

    public static BigDecimal divide(BigDecimal v1, int v2, int scale, int round_mode) 
        if (scale < 0) 
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        
        BigDecimal b1 = v1;
        BigDecimal b2 = new BigDecimal(v2);
        return b1.divide(b2, scale, round_mode);
    



4、使用方式

1、第一种
@ProcessResult
protected ResultDto<List<CimFGroupTradeByDayChart>> groupTradeByDayList(CimFGroupTradeVo vo) 
    ResultDto<List<CimFGroupTradeByDayChart>> result = new ResultDto<>();

@AmoFormatt
@ExcelField(title = "交易金额" )
private Double tradeBalan;


/** 行业类型*/
@CodeValueConversion("INDUST_TYPE")
@ExcelField(title = "行业类型")
private String industryType;
 
 2、调用类自己本身方法使用
@Override
public RadarChart queryHomePageGroupRadar(String groupId) throws IOException, InstantiationException, IllegalAccessException 
    if (StringUtils.isBlank(groupId)) 
        throw new RuntimeException("参数客群编号不得为空");
    
    List<CimFGroupRadarChart> cimFGroupRadarCharts =  custGroupViewAnalysisService.getCimFGroupRadarCharts(groupId);
/**
 * @param groupId
 * @return
 * @throws IllegalAccessException
 * @throws IOException
 * @throws InstantiationException
 */
@Override
@ProcessResult
public List<CimFGroupRadarChart> getCimFGroupRadarCharts(String groupId) throws IllegalAccessException, IOException, InstantiationException 
   return queryList(new TermQueryBuilder("group_id", groupId), CIM_F_GROUP_RADAR_CHART, CimFGroupRadarChart.class);

以上是关于项目中金额格式化,码值翻译的主要内容,如果未能解决你的问题,请参考以下文章

JAVA金额格式字符串转数值

格式金额,默认保留两位小数,并格式化为千分位

vue中如何格式化数据成图标?

字符串金额转化为指定格式的货币格式

中英文金额大写转换器

中英文金额大写转换器