From dc11990acb945329cdcb28dae7a0aa353a3c18c7 Mon Sep 17 00:00:00 2001
From: zyy <zyy@email.com>
Date: Fri, 18 Jul 2025 10:38:35 +0800
Subject: [PATCH] 货币转换优化

---
 src/main/java/com/nq/service/impl/UserStockSubscribeServiceImpl.java |   17 ++++++++
 src/main/java/com/nq/service/impl/UserAssetsServices.java            |   38 +++++++-----------
 src/main/java/com/nq/service/IUserAssetsServices.java                |    8 ++--
 3 files changed, 35 insertions(+), 28 deletions(-)

diff --git a/src/main/java/com/nq/service/IUserAssetsServices.java b/src/main/java/com/nq/service/IUserAssetsServices.java
index 9399f91..02153d5 100644
--- a/src/main/java/com/nq/service/IUserAssetsServices.java
+++ b/src/main/java/com/nq/service/IUserAssetsServices.java
@@ -59,10 +59,10 @@
     Boolean aiAvailableBalanceChange(String accetType, Integer userId, EUserAssets eUserAssets, BigDecimal amount) throws Exception;
 
     /**
-     * 根据汇率转换金额
-     * @param amount    转换金额
-     * @param rate      转换汇率
+     * 根据汇率转换MEX金额
+     * @param accetType  账户类型
+     * @param amount     转换金额
      * @return
      */
-    BigDecimal exchangeAmountByRate(BigDecimal amount, BigDecimal rate);
+    BigDecimal exchangeAmountByRate(String accetType, BigDecimal amount) throws Exception;
 }
diff --git a/src/main/java/com/nq/service/impl/UserAssetsServices.java b/src/main/java/com/nq/service/impl/UserAssetsServices.java
index 45143b5..46dd379 100644
--- a/src/main/java/com/nq/service/impl/UserAssetsServices.java
+++ b/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("货币转换汇率未设置");
+        }
+
+
     }
 }
diff --git a/src/main/java/com/nq/service/impl/UserStockSubscribeServiceImpl.java b/src/main/java/com/nq/service/impl/UserStockSubscribeServiceImpl.java
index 3faf5d6..ee62972 100644
--- a/src/main/java/com/nq/service/impl/UserStockSubscribeServiceImpl.java
+++ b/src/main/java/com/nq/service/impl/UserStockSubscribeServiceImpl.java
@@ -5,6 +5,7 @@
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.github.pagehelper.PageHelper;
 import com.github.pagehelper.PageInfo;
+import com.nq.Repository.ExchangeRateRepository;
 import com.nq.common.ResponseCode;
 import com.nq.common.ServerResponse;
 import com.nq.dao.StockSubscribeMapper;
@@ -69,6 +70,9 @@
 
     @Autowired
     UserAssetsMapper userAssetsMapper;
+
+    @Autowired
+    ExchangeRateRepository exchangeRateRepository;
 
     /**
      * 用户新股申购
@@ -148,7 +152,6 @@
                 }else{
                     bound =  new BigDecimal(model.getApplyNums()).multiply(stockSubscribe.getPrice());
                 }
-
                 if(stockSubscribe.getType() == 1){
                     model.setUserId(user.getId());
                     model.setNewName(stockSubscribe.getName());
@@ -165,6 +168,10 @@
                     userStockSubscribe.setNewStockId(stockSubscribe.getNewlistId());
                     ret = userStockSubscribeMapper.insert(userStockSubscribe);
 
+                    //如果不是墨西哥币需要转换金额
+                    if (!stockSubscribe.getStockType().equals(EStockType.MX.getCode())) {
+                        bound = iUserAssetsServices.exchangeAmountByRate(stockSubscribe.getStockType(), bound);
+                    }
                     BigDecimal subtract = userAssets.getAvailableBalance().subtract(bound);
                     if(subtract.compareTo(BigDecimal.ZERO) >= 0){
                         userAssets.setFreezeMoney(userAssets.getFreezeMoney().add(bound));
@@ -268,12 +275,20 @@
                         if(applyNumber > 0){
                             //需要退回的资金
                             BigDecimal refundPrice = userStockSubscribe.getBuyPrice().multiply(new BigDecimal(applyNumber));
+                            //如果不是墨西哥币需要转换金额
+                            if (!stockSubscribe.getStockType().equals(EStockType.MX.getCode())) {
+                                refundPrice = iUserAssetsServices.exchangeAmountByRate(stockSubscribe.getStockType(), refundPrice);
+                            }
                             userAssets.setAvailableBalance(userAssets.getAvailableBalance().add(refundPrice));
                             userAssets.setFreezeMoney(userAssets.getFreezeMoney().subtract(refundPrice));
                         }
                     }else{
                         if(applyNumber > 0){
                             BigDecimal refundPrice = userStockSubscribe.getBuyPrice().multiply(new BigDecimal(applyNumber));
+                            //如果不是墨西哥币需要转换金额
+                            if (!stockSubscribe.getStockType().equals(EStockType.MX.getCode())) {
+                                refundPrice = iUserAssetsServices.exchangeAmountByRate(stockSubscribe.getStockType(), refundPrice);
+                            }
                             BigDecimal subtract = refundPrice.subtract(userAssets.getAmountToBeCovered());
                             if(subtract.compareTo(BigDecimal.ZERO) <= 0){//如果退回的资金不足补足待补则直接减
                                 userAssets.setAmountToBeCovered(userAssets.getAmountToBeCovered().subtract(refundPrice));

--
Gitblit v1.9.3