zyy
2025-07-18 dc11990acb945329cdcb28dae7a0aa353a3c18c7
src/main/java/com/nq/service/impl/UserAssetsServices.java
@@ -182,16 +182,7 @@
        UserAssets userAssets = assetsByTypeAndUserId(EStockType.MX.getCode(), userId);
        //如果不是墨西哥币需要转换金额
        if (!accetType.equals(EStockType.MX.getCode())) {
            EStockType stockType = EStockType.getEStockTypeByCode(accetType);
            ExchangeRate exchangeRate = exchangeRateRepository.findExchangeRateByCurrencyAndConversionCurrency(
                    stockType.getSymbol(), EStockType.MX.getSymbol()).orElse(null);
            if (exchangeRate != null) {
                //转换为墨西哥币
                amount = exchangeAmountByRate(amount, exchangeRate.getRata());
            } else {
                log.error("availablebalanceChange ExchangeRate is null:{}>>{}", stockType.getSymbol(), EStockType.MX.getSymbol());
                throw new Exception("货币转换汇率未设置");
            }
            amount = exchangeAmountByRate(accetType, amount);
        }
        UserPosition userPosition = userPositionMapper.selectOne(new LambdaQueryWrapper<UserPosition>().gt(UserPosition::getAmountToBeCovered, BigDecimal.ZERO).eq(UserPosition::getUserId,userAssets.getUserId()));
        String type = eUserAssets.getDesc();
@@ -318,16 +309,7 @@
        UserAssets userAssets = assetsByTypeAndUserId(EStockType.MX.getCode(), userId);
        //如果不是墨西哥币需要转换金额
        if (!accetType.equals(EStockType.MX.getCode())) {
            EStockType stockType = EStockType.getEStockTypeByCode(accetType);
            ExchangeRate exchangeRate = exchangeRateRepository.findExchangeRateByCurrencyAndConversionCurrency(
                    stockType.getSymbol(), EStockType.MX.getSymbol()).orElse(null);
            if (exchangeRate != null) {
                //转换为墨西哥币
                amount = exchangeAmountByRate(amount, exchangeRate.getRata());
            } else {
                log.error("ExchangeRate is null:{}>>{}", stockType.getSymbol(), EStockType.MX.getSymbol());
                throw new Exception("货币转换汇率未设置");
            }
            amount = exchangeAmountByRate(accetType, amount);
        }
        String before = userAssets.getAvailableBalance().toString();
        if (eUserAssets.getCode().equals(EUserAssets.BUY_AI.getCode())) {
@@ -375,8 +357,18 @@
    }
    @Override
    public BigDecimal exchangeAmountByRate(BigDecimal amount, BigDecimal rate) {
        //保留5位小数
        return amount.multiply(rate).setScale(5, RoundingMode.HALF_UP);
    public BigDecimal exchangeAmountByRate(String accetType, BigDecimal amount) throws Exception {
        EStockType stockType = EStockType.getEStockTypeByCode(accetType);
        ExchangeRate exchangeRate = exchangeRateRepository.findExchangeRateByCurrencyAndConversionCurrency(
                stockType.getSymbol(), EStockType.MX.getSymbol()).orElse(null);
        if (exchangeRate != null) {
            //转换为墨西哥币 保留5位小数
            return amount.multiply(exchangeRate.getRata()).setScale(5, RoundingMode.HALF_UP);
        } else {
            log.error("exchangeAmountByRate is null:{}>>{}", stockType.getSymbol(), EStockType.MX.getSymbol());
            throw new Exception("货币转换汇率未设置");
        }
    }
}