package com.amarsoft.aims.util; import com.amarsoft.are.ARE; import com.amarsoft.are.jbo.BizObject; import com.amarsoft.are.jbo.BizObjectManager; import com.amarsoft.are.jbo.JBOException; import com.amarsoft.are.jbo.JBOFactory; import com.amarsoft.are.lang.StringX; /** * 汇率辅助处理工具类 * @author syang * @date 2013-12-27 */ public class ERateTool { /** 人民币币种常量 */ public static final String CURRENCY_RMB = "01"; /** * 取任一两种币种间的汇率 * @param fromCurrency 原币种 * @param toCurrency 转换的目标币种 * @return */ public static double getERate(String fromCurrency,String toCurrency){ if(StringX.isEmpty(fromCurrency)){ ARE.getLog().error("汇率转换源币种为空"); return 1.0; } if(StringX.isEmpty(toCurrency)){ ARE.getLog().error("汇率转换目标币种为空"); return 1.0; } if(fromCurrency.equals(toCurrency)){ //两个币种相同,直接返回1 return 1.0; } double currencyFrom = 1.0; double currencyTo = 1.0; try { BizObjectManager bm = JBOFactory.getBizObjectManager("jbo.sys.ERATE_INFO"); BizObject boFrom = bm.createQuery("Currency=:Currency").setParameter("Currency", fromCurrency).getSingleResult(false); BizObject boTo = bm.createQuery("Currency=:Currency").setParameter("Currency", toCurrency).getSingleResult(false); if(boFrom==null||boTo==null)return 1.0; currencyFrom = boFrom.getAttribute("ExchangeValue").getDouble(); currencyTo = boTo.getAttribute("ExchangeValue").getDouble(); } catch (JBOException e) { ARE.getLog().error("汇率获取错误:JBO读取错误"); } return NumberAssistant.divided(currencyFrom, currencyTo); } /** * 获取转换成人民币的汇率 * @param sCurrency * @return */ public static double getERateRMB(String currency){ return getERate(currency,CURRENCY_RMB); } /** * 获取金额通过两个币种转换后的值 * @param sum 金额 * @param fromCurrency 原币种 * @param toCurrency 目标币种 * @return */ public static double getExchange(double sum,String fromCurrency,String toCurrency){ return NumberAssistant.multiply(sum,getERate(fromCurrency,toCurrency)); } /** * 指定币种的金额转换为人民币后的金额 * @param sum 金额 * @param fromCurrency 原币种 * @return */ public static double getExchangeRMB(double sum,String fromCurrency){ return getExchange(sum,fromCurrency,CURRENCY_RMB); } /** * 人民币转换为本位币后的金额 * @param rmbSum 人民币金额 * @param baseCurrency 本位币币种 * @return */ public static double getRMBExchangeBase(double rmbSum,String baseCurrency){ return getExchange(rmbSum,CURRENCY_RMB,baseCurrency); } }