Commit cbedcb77 authored by 332784038@qq.com's avatar 332784038@qq.com Committed by wux

汇率模块调用api逻辑修改

parent ec0f2a79
...@@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.ecw.api.currency.dto; ...@@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.ecw.api.currency.dto;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
import java.math.BigDecimal;
import java.util.Date; import java.util.Date;
@Data @Data
...@@ -20,7 +21,14 @@ public class ExchangeRateRespDTO { ...@@ -20,7 +21,14 @@ public class ExchangeRateRespDTO {
private String targetCurrencyCode; private String targetCurrencyCode;
@ApiModelProperty(value = "汇率") @ApiModelProperty(value = "汇率")
private java.math.BigDecimal currencyRate; private BigDecimal currencyRate;
@ApiModelProperty(value = "原币种基础兑换金额")
private BigDecimal sourceCurrencyAmount;
@ApiModelProperty(value = "目标币种基础兑换金额")
private BigDecimal targetCurrencyAmount;
@ApiModelProperty(value = "汇率有效期") @ApiModelProperty(value = "汇率有效期")
private Date expireDate; private Date expireDate;
} }
...@@ -6,6 +6,7 @@ import cn.iocoder.yudao.module.ecw.api.currency.dto.CurrencyRespDTO; ...@@ -6,6 +6,7 @@ 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.dal.dataobject.currency.CurrencyDO; import cn.iocoder.yudao.module.ecw.dal.dataobject.currency.CurrencyDO;
import cn.iocoder.yudao.module.ecw.dal.dataobject.currencyRate.CurrencyRateDO;
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.currencyRate.CurrencyRateService; import cn.iocoder.yudao.module.ecw.service.currencyRate.CurrencyRateService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -36,14 +37,24 @@ public class CurrecyApiImpl implements CurrencyApi { ...@@ -36,14 +37,24 @@ public class CurrecyApiImpl implements CurrencyApi {
@Override @Override
public ExchangeRateRespDTO getCurrencyRate(Long sourceId, Long targetId) { public ExchangeRateRespDTO getCurrencyRate(Long sourceId, Long targetId) {
ExchangeRateRespDTO dto = new ExchangeRateRespDTO(); ExchangeRateRespDTO dto = new ExchangeRateRespDTO();
dto.setSourceCurrencyId(sourceId); dto.setSourceCurrencyId(sourceId);
dto.setTargetCurrencyId(targetId); dto.setTargetCurrencyId(targetId);
if (sourceId == null || targetId == null) { if (sourceId == null || targetId == null) {
dto.setCurrencyRate(BigDecimal.ZERO); dto.setCurrencyRate(BigDecimal.ZERO);
dto.setSourceCurrencyAmount(BigDecimal.ZERO);
dto.setTargetCurrencyAmount(BigDecimal.ZERO);
} else { } else {
dto.setCurrencyRate(sourceId.equals(targetId) ? BigDecimal.ONE : currencyRateService.rate(sourceId, targetId)); if (sourceId.equals(targetId)){
dto.setSourceCurrencyAmount(BigDecimal.ONE);
dto.setTargetCurrencyAmount(BigDecimal.ONE);
dto.setCurrencyRate(BigDecimal.ONE);
}else {
CurrencyRateDO currencyRateDO = currencyRateService.exchangeBaseValue(sourceId, targetId);
dto.setSourceCurrencyAmount(currencyRateDO.getSourceAmount());
dto.setTargetCurrencyAmount(currencyRateDO.getTargetAmount());
dto.setCurrencyRate(currencyRateService.rate(currencyRateDO.getSourceAmount(), currencyRateDO.getTargetAmount()));
}
} }
return dto; return dto;
...@@ -77,10 +88,20 @@ public class CurrecyApiImpl implements CurrencyApi { ...@@ -77,10 +88,20 @@ public class CurrecyApiImpl implements CurrencyApi {
if (sourceId == null || targetId == null) { if (sourceId == null || targetId == null) {
dto.setCurrencyRate(BigDecimal.ZERO); dto.setCurrencyRate(BigDecimal.ZERO);
dto.setSourceCurrencyAmount(BigDecimal.ZERO);
dto.setTargetCurrencyAmount(BigDecimal.ZERO);
} else { } else {
dto.setSourceCurrencyId(sourceId.longValue()); if (sourceId.equals(targetId)){
dto.setTargetCurrencyId(targetId.longValue()); dto.setSourceCurrencyAmount(BigDecimal.ONE);
dto.setCurrencyRate(sourceId.equals(targetId) ? BigDecimal.ONE : currencyRateService.rate(sourceId, targetId)); dto.setTargetCurrencyAmount(BigDecimal.ONE);
dto.setCurrencyRate(BigDecimal.ONE);
}else {
CurrencyRateDO currencyRateDO = currencyRateService.exchangeBaseValue(sourceId, targetId);
dto.setSourceCurrencyAmount(currencyRateDO.getSourceAmount());
dto.setTargetCurrencyAmount(currencyRateDO.getTargetAmount());
dto.setCurrencyRate(currencyRateService.rate(currencyRateDO.getSourceAmount(), currencyRateDO.getTargetAmount()));
}
} }
return dto; return dto;
......
...@@ -52,6 +52,27 @@ public interface CurrencyRateService { ...@@ -52,6 +52,27 @@ public interface CurrencyRateService {
.orElseThrow(() -> exception(CURRENCY_RATE_TOO_SMALL)); .orElseThrow(() -> exception(CURRENCY_RATE_TOO_SMALL));
} }
/**
* 获得两个币种的兑换基础值。
*
* @param sourceId 原币种编号
* @param targetId 支付币种编号
* @return 获得两个币种的兑换基础值
* @throws ServiceException 汇率不存在或过期
*/
default CurrencyRateDO exchangeBaseValue(long sourceId, long targetId) throws ServiceException {
CurrencyRateDO entity = Optional.ofNullable(find(sourceId, targetId))
.orElseThrow(() -> exception(CURRENCY_RATE_NOT_EXISTS));
LocalDate now = LocalDate.now();
if (now.isAfter(entity.getExpiration())) {
throw exception(CURRENCY_RATE_EXPIRED, String.valueOf(sourceId), String.valueOf(targetId));
}
return entity;
}
/** /**
* 计算两个币种的汇率。 * 计算两个币种的汇率。
* *
......
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.iocoder.yudao.module.ecw.dal.mysql.ladingTemplate.LadingTemplateMapper">
<!--
一般情况下,尽可能使用 Mapper 进行 CRUD 增删改查即可。
无法满足的场景,例如说多表关联查询,才使用 XML 编写 SQL。
代码生成器暂时只生成 Mapper XML 文件本身,更多推荐 MybatisX 快速开发插件来生成查询。
文档可见:https://www.iocoder.cn/MyBatis/x-plugins/
-->
</mapper>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.iocoder.yudao.module.ecw.dal.mysql.warehouse.WarehouseMapper">
<!--
一般情况下,尽可能使用 Mapper 进行 CRUD 增删改查即可。
无法满足的场景,例如说多表关联查询,才使用 XML 编写 SQL。
代码生成器暂时只生成 Mapper XML 文件本身,更多推荐 MybatisX 快速开发插件来生成查询。
文档可见:https://www.iocoder.cn/MyBatis/x-plugins/
-->
</mapper>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.iocoder.yudao.module.ecw.dal.mysql.zhongPao.ZhongPaoMapper">
<!--
一般情况下,尽可能使用 Mapper 进行 CRUD 增删改查即可。
无法满足的场景,例如说多表关联查询,才使用 XML 编写 SQL。
代码生成器暂时只生成 Mapper XML 文件本身,更多推荐 MybatisX 快速开发插件来生成查询。
文档可见:https://www.iocoder.cn/MyBatis/x-plugins/
-->
</mapper>
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