Commit 705f33ad authored by wanghuazhou's avatar wanghuazhou Committed by wux

refactor: 货币汇率对外接口替换

parent 5e3bd425
package cn.iocoder.yudao.module.ecw.service.api; package cn.iocoder.yudao.module.ecw.service.api;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.module.ecw.api.currency.CurrencyApi; import cn.iocoder.yudao.module.ecw.api.currency.CurrencyApi;
import cn.iocoder.yudao.module.ecw.api.currency.dto.CurrencyRespDTO; import cn.iocoder.yudao.module.ecw.api.currency.dto.CurrencyRespDTO;
import cn.iocoder.yudao.module.ecw.api.currency.dto.ExchangeRateRespDTO; import cn.iocoder.yudao.module.ecw.api.currency.dto.ExchangeRateRespDTO;
import cn.iocoder.yudao.module.ecw.convert.currency.CurrencyConvert; import cn.iocoder.yudao.module.ecw.convert.currency.CurrencyConvert;
import cn.iocoder.yudao.module.ecw.convert.exchangeRate.ExchangeRateConvert;
import cn.iocoder.yudao.module.ecw.dal.dataobject.currency.CurrencyDO; import cn.iocoder.yudao.module.ecw.dal.dataobject.currency.CurrencyDO;
import cn.iocoder.yudao.module.ecw.dal.dataobject.exchangeRate.ExchangeRateDO;
import cn.iocoder.yudao.module.ecw.service.currency.CurrencyService; import cn.iocoder.yudao.module.ecw.service.currency.CurrencyService;
import cn.iocoder.yudao.module.ecw.service.exchangeRate.ExchangeRateService; import cn.iocoder.yudao.module.ecw.service.currencyRate.CurrencyRateService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.ecw.enums.ErrorCodeConstants.EXCHANGE_RATE_EXPIRE;
import static cn.iocoder.yudao.module.ecw.enums.ErrorCodeConstants.EXCHANGE_RATE_NOT_EXISTS_PARAM;
@Service @Service
public class CurrecyApiImpl implements CurrencyApi { public class CurrecyApiImpl implements CurrencyApi {
@Resource @Resource
CurrencyService currencyService; CurrencyService currencyService;
@Resource @Autowired
ExchangeRateService exchangeRateService; private CurrencyRateService currencyRateService;
@Override @Override
public Map<Integer, CurrencyRespDTO> getAllCurrency() { public Map<Integer, CurrencyRespDTO> getAllCurrency() {
...@@ -42,158 +35,55 @@ public class CurrecyApiImpl implements CurrencyApi { ...@@ -42,158 +35,55 @@ public class CurrecyApiImpl implements CurrencyApi {
@Override @Override
public ExchangeRateRespDTO getCurrencyRate(Long sourceId, Long targetId) { public ExchangeRateRespDTO getCurrencyRate(Long sourceId, Long targetId) {
if (sourceId == targetId) { ExchangeRateRespDTO dto = new ExchangeRateRespDTO();
ExchangeRateRespDTO dto = new ExchangeRateRespDTO();
dto.setCurrencyRate(new BigDecimal("1")); dto.setSourceCurrencyId(sourceId);
dto.setSourceCurrencyId(sourceId); dto.setTargetCurrencyId(targetId);
dto.setTargetCurrencyId(targetId);
return dto; if (sourceId == null || targetId == null) {
} dto.setCurrencyRate(BigDecimal.ZERO);
LambdaQueryWrapper<ExchangeRateDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(ExchangeRateDO::getSourceCurrencyId, sourceId);
lambdaQueryWrapper.eq(ExchangeRateDO::getTargetCurrencyId, targetId);
ExchangeRateDO rateDO = exchangeRateService.getOne(lambdaQueryWrapper);
CurrencyDO currencyDO1 = currencyService.getCurrency(sourceId.intValue());
CurrencyDO currencyDO2 = currencyService.getCurrency(targetId.intValue());
if (null != rateDO) {
//判断汇率有没有过期
if (null != rateDO.getExpireDate()) {
if (DateUtil.compare(rateDO.getExpireDate(), DateUtil.date()) == -1) {
if (null == currencyDO1 || null == currencyDO2) {
throw exception(EXCHANGE_RATE_EXPIRE, sourceId.toString(), targetId.toString());
} else {
throw exception(EXCHANGE_RATE_EXPIRE, currencyDO1.getTitleZh() + currencyDO1.getFuhao(), currencyDO2.getTitleZh() + currencyDO2.getFuhao());
}
}
} else {
if (null == currencyDO1 || null == currencyDO2) {
throw exception(EXCHANGE_RATE_EXPIRE, sourceId.toString(), targetId.toString());
} else {
throw exception(EXCHANGE_RATE_EXPIRE, currencyDO1.getTitleZh() + currencyDO1.getFuhao(), currencyDO2.getTitleZh() + currencyDO2.getFuhao());
}
}
return ExchangeRateConvert.INSTANCE.convert2(exchangeRateService.getOne(lambdaQueryWrapper));
} else { } else {
if (null == currencyDO1 || null == currencyDO2) { dto.setCurrencyRate(sourceId.equals(targetId) ? BigDecimal.ONE : currencyRateService.rate(sourceId, targetId));
ExchangeRateRespDTO dto = new ExchangeRateRespDTO();
dto.setCurrencyRate(BigDecimal.ZERO);
dto.setSourceCurrencyId(sourceId);
dto.setTargetCurrencyId(targetId);
return dto;
} else {
throw exception(EXCHANGE_RATE_NOT_EXISTS_PARAM, currencyDO1.getTitleZh() + currencyDO1.getFuhao(), currencyDO2.getTitleZh() + currencyDO2.getFuhao());
}
} }
return dto;
} }
@Override @Override
public List<ExchangeRateRespDTO> getCurrencyRateList(Collection<Long> sourceIds, Long targetId) { public List<ExchangeRateRespDTO> getCurrencyRateList(Collection<Long> sourceIds, Long targetId) {
List<ExchangeRateRespDTO> exchangeRateRespDTOList = new ArrayList<>(); return sourceIds.stream()
for (Long sourceId : sourceIds) { .map(sourceId -> getCurrencyRate(sourceId, targetId))
if (sourceId == targetId) { .collect(Collectors.toList());
ExchangeRateRespDTO dto = new ExchangeRateRespDTO();
dto.setCurrencyRate(new BigDecimal("1"));
dto.setSourceCurrencyId(sourceId);
dto.setTargetCurrencyId(targetId);
exchangeRateRespDTOList.add(dto);
continue;
}
LambdaQueryWrapper<ExchangeRateDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(ExchangeRateDO::getSourceCurrencyId, sourceId);
lambdaQueryWrapper.eq(ExchangeRateDO::getTargetCurrencyId, targetId);
ExchangeRateDO rateDO = exchangeRateService.getOne(lambdaQueryWrapper);
CurrencyDO currencyDO1 = currencyService.getCurrency(sourceId.intValue());
CurrencyDO currencyDO2 = currencyService.getCurrency(targetId.intValue());
if (null != rateDO) {
//判断汇率有没有过期
if (null != rateDO.getExpireDate()) {
if (DateUtil.compare(rateDO.getExpireDate(), DateUtil.date()) == -1) {
if (null == currencyDO1 || null == currencyDO2) {
throw exception(EXCHANGE_RATE_EXPIRE, sourceId.toString(), targetId.toString());
} else {
throw exception(EXCHANGE_RATE_EXPIRE, currencyDO1.getTitleZh() + currencyDO1.getFuhao(), currencyDO2.getTitleZh() + currencyDO2.getFuhao());
}
}
} else {
if (null == currencyDO1 || null == currencyDO2) {
throw exception(EXCHANGE_RATE_EXPIRE, sourceId.toString(), targetId.toString());
} else {
throw exception(EXCHANGE_RATE_EXPIRE, currencyDO1.getTitleZh() + currencyDO1.getFuhao(), currencyDO2.getTitleZh() + currencyDO2.getFuhao());
}
}
exchangeRateRespDTOList.add(ExchangeRateConvert.INSTANCE.convert2(rateDO));
} else {
if (null == currencyDO1 || null == currencyDO2) {
ExchangeRateRespDTO dto = new ExchangeRateRespDTO();
dto.setCurrencyRate(BigDecimal.ZERO);
dto.setSourceCurrencyId(sourceId);
dto.setTargetCurrencyId(targetId);
exchangeRateRespDTOList.add(dto);
} else {
throw exception(EXCHANGE_RATE_NOT_EXISTS_PARAM, currencyDO1.getTitleZh() + currencyDO1.getFuhao(), currencyDO2.getTitleZh() + currencyDO2.getFuhao());
}
}
}
return exchangeRateRespDTOList;
} }
@Override @Override
public ExchangeRateRespDTO getCurrencyRateByCode(String sourceCode, String targetCode) { public ExchangeRateRespDTO getCurrencyRateByCode(String sourceCode, String targetCode) {
ExchangeRateRespDTO dto = new ExchangeRateRespDTO();
dto.setSourceCurrencyCode(sourceCode);
dto.setTargetCurrencyCode(targetCode);
if (StrUtil.equals(sourceCode, targetCode)) { if (StrUtil.equals(sourceCode, targetCode)) {
ExchangeRateRespDTO dto = new ExchangeRateRespDTO(); dto.setCurrencyRate(BigDecimal.ONE);
dto.setCurrencyRate(new BigDecimal("1"));
dto.setSourceCurrencyCode(sourceCode);
dto.setTargetCurrencyCode(targetCode);
return dto; return dto;
} }
LambdaQueryWrapper<ExchangeRateDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(ExchangeRateDO::getSourceCurrencyCode, sourceCode); Integer sourceId = Optional.ofNullable(currencyService.getCurrencyByCode(sourceCode))
lambdaQueryWrapper.eq(ExchangeRateDO::getTargetCurrencyCode, targetCode); .map(CurrencyDO::getId)
ExchangeRateDO rateDO = exchangeRateService.getOne(lambdaQueryWrapper); .orElse(null);
Integer targetId = Optional.ofNullable(currencyService.getCurrencyByCode(targetCode))
CurrencyDO currencyDO1 = currencyService.getCurrencyByCode(sourceCode); .map(CurrencyDO::getId)
.orElse(null);
CurrencyDO currencyDO2 = currencyService.getCurrencyByCode(targetCode);
if (sourceId == null || targetId == null) {
dto.setCurrencyRate(BigDecimal.ZERO);
if (null != rateDO) {
//判断汇率有没有过期
if (null != rateDO.getExpireDate()) {
if (DateUtil.compare(rateDO.getExpireDate(), DateUtil.date()) == -1) {
if (null == currencyDO1 || null == currencyDO2) {
throw exception(EXCHANGE_RATE_EXPIRE, sourceCode, targetCode);
} else {
throw exception(EXCHANGE_RATE_EXPIRE, currencyDO1.getTitleZh() + currencyDO1.getFuhao(), currencyDO2.getTitleZh() + currencyDO2.getFuhao());
}
}
} else {
if (null == currencyDO1 || null == currencyDO2) {
throw exception(EXCHANGE_RATE_EXPIRE, sourceCode, targetCode);
} else {
throw exception(EXCHANGE_RATE_EXPIRE, currencyDO1.getTitleZh() + currencyDO1.getFuhao(), currencyDO2.getTitleZh() + currencyDO2.getFuhao());
}
}
return ExchangeRateConvert.INSTANCE.convert2(exchangeRateService.getOne(lambdaQueryWrapper));
} else { } else {
if (null == currencyDO1 || null == currencyDO2) { dto.setSourceCurrencyId(sourceId.longValue());
ExchangeRateRespDTO dto = new ExchangeRateRespDTO(); dto.setTargetCurrencyId(targetId.longValue());
dto.setCurrencyRate(BigDecimal.ZERO); dto.setCurrencyRate(sourceId.equals(targetId) ? BigDecimal.ONE : currencyRateService.rate(sourceId, targetId));
dto.setSourceCurrencyCode(sourceCode);
dto.setTargetCurrencyCode(targetCode);
return dto;
} else {
throw exception(EXCHANGE_RATE_NOT_EXISTS_PARAM, currencyDO1.getTitleZh() + currencyDO1.getFuhao(), currencyDO2.getTitleZh() + currencyDO2.getFuhao());
}
} }
return dto;
} }
@Override @Override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment