package com.changhong.epc.rely.api.epc.admin; import com.changhong.epc.bean.admin.CorresElField; import com.changhong.epc.bean.admin.ExchangeRateInfo; import com.changhong.epc.bean.count.MeInfo; import com.changhong.epc.bean.tenant.master.AutoMasterVal; import com.changhong.epc.constter.admin.CorresElFieldConst; import com.changhong.epc.rely.api.epc.tenant.MasterApi; import com.changhong.epc.rely.api.tool.CurrencyTool; import com.iemsoft.framework.cloud.core.tools.DateTool; import com.iemsoft.framework.cloud.core.tools.JSONTool; import com.iemsoft.framework.cloud.core.tools.ObjectUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.Arrays; import java.util.Date; import java.util.Optional; @Service @Slf4j public class CurrencyApi implements CorresElFieldConst { @Resource private MasterApi masterApi; @Resource private CorresElFieldApi corresElFieldApi; public String exchangeRate(String currencyCode, String exchangeCurrencyCode, Object date){ ExchangeRateInfo exchangeRateInfo = Optional.ofNullable(corresElFieldApi.getVal(EXCHANGE_RATE_MASTER_JSON, CorresElField::getMdCode)) .map(json->JSONTool.toObj(json, ExchangeRateInfo.class)).orElse(null); if(exchangeRateInfo == null){ log.error("没有配置汇率主数据json"); return "0"; } AutoMasterVal autoMasterVal = new AutoMasterVal(); autoMasterVal.setMdCode(exchangeRateInfo.getMasterCode()); autoMasterVal.setMeCode(exchangeRateInfo.getExchange()); autoMasterVal.setParams( Arrays.asList( MeInfo.of(exchangeRateInfo.getCurrencyMini(), currencyCode) , MeInfo.of(exchangeRateInfo.getExchangeCurrencyMini(), exchangeCurrencyCode) , MeInfo.of(exchangeRateInfo.getExchangeDate(), toYM(date)) ) ); String val = masterApi.getMasterVal(autoMasterVal); if(ObjectUtil.empty(val)){ log.debug("缺失汇率主数据:{}-{}-{}", currencyCode, exchangeCurrencyCode, toYM(date)); return "0"; } // 主数据中默认100个单位 return CurrencyTool.divide(val, 100); } public String toYM(Object date){ if(date instanceof Date){ return DateTool.dateToString((Date) date, "yyyyMM"); }else if(date instanceof String){ return DateTool.dateToString(DateTool.stringToDate((String) date), "yyyyMM"); }else{ return DateTool.dateToString(new Date(), "yyyyMM"); } } }