From dfd95b8354429de9b14087cb96cba0ba0cd7265a Mon Sep 17 00:00:00 2001
From: zyy <zyy@email.com>
Date: Tue, 15 Jul 2025 14:07:06 +0800
Subject: [PATCH] 大宗交易修改

---
 src/main/java/com/nq/service/impl/UserStockSubscribeServiceImpl.java       |    4 
 src/main/java/com/nq/common/ServerResponse.java                            |    9 ++-
 src/main/java/com/nq/service/impl/UserAssetsServices.java                  |   41 ++++++++++++-
 src/main/java/com/nq/common/interceptor/ApiAdminAuthorityInterceptor.java  |    4 
 src/main/resources/mapper/UserPositionMapper.xml                           |   15 -----
 src/main/java/com/nq/controller/backend/AdminStockSubscribeController.java |    1 
 src/main/java/com/nq/service/IUserAssetsServices.java                      |    6 +
 src/main/java/com/nq/dao/UserPositionMapper.java                           |    1 
 src/main/java/com/nq/service/impl/StockAiServiceImpl.java                  |   38 +++++++-----
 src/main/java/com/nq/service/impl/UserPositionServiceImpl.java             |    8 +-
 src/main/java/com/nq/common/interceptor/ApiUserAuthorityInterceptor.java   |    3 
 11 files changed, 79 insertions(+), 51 deletions(-)

diff --git a/src/main/java/com/nq/common/ServerResponse.java b/src/main/java/com/nq/common/ServerResponse.java
index 208c7ac..c31a467 100644
--- a/src/main/java/com/nq/common/ServerResponse.java
+++ b/src/main/java/com/nq/common/ServerResponse.java
@@ -74,7 +74,8 @@
 
 
     public static <T> ServerResponse<T> createBySuccessMsg(String msg,HttpServletRequest request) {
-        return new ServerResponse(ResponseCode.SUCCESS.getCode(), new GoogleTranslateUtil().translate(msg,request.getHeader(LANG)));
+        //return new ServerResponse(ResponseCode.SUCCESS.getCode(), new GoogleTranslateUtil().translate(msg,request.getHeader(LANG)));
+        return new ServerResponse(ResponseCode.SUCCESS.getCode(), msg);
     }
 
     public static <T> ServerResponse<T> createBySuccess(T data) {
@@ -83,7 +84,8 @@
 
 
     public static <T> ServerResponse<T> createBySuccess(String data,HttpServletRequest request) {
-        return new ServerResponse(ResponseCode.SUCCESS.getCode(), new GoogleTranslateUtil().translate(data,request.getHeader(LANG)));
+        //return new ServerResponse(ResponseCode.SUCCESS.getCode(), new GoogleTranslateUtil().translate(data,request.getHeader(LANG)));
+        return new ServerResponse(ResponseCode.SUCCESS.getCode(), data);
     }
 
     public static <T> ServerResponse<T> createBySuccess(String msg, T data) {
@@ -103,7 +105,8 @@
         return new ServerResponse(ResponseCode.ERROR.getCode(), errormsg);
     }
     public static <T> ServerResponse<T> createByErrorMsg(String errormsg, HttpServletRequest request) {
-        return new ServerResponse(ResponseCode.ERROR.getCode(), new GoogleTranslateUtil().translate(errormsg,request.getHeader(LANG)));
+        //return new ServerResponse(ResponseCode.ERROR.getCode(), new GoogleTranslateUtil().translate(errormsg,request.getHeader(LANG)));
+        return new ServerResponse(ResponseCode.ERROR.getCode(), errormsg);
     }
 
 
diff --git a/src/main/java/com/nq/common/interceptor/ApiAdminAuthorityInterceptor.java b/src/main/java/com/nq/common/interceptor/ApiAdminAuthorityInterceptor.java
index 7e1584b..c43ae91 100644
--- a/src/main/java/com/nq/common/interceptor/ApiAdminAuthorityInterceptor.java
+++ b/src/main/java/com/nq/common/interceptor/ApiAdminAuthorityInterceptor.java
@@ -30,7 +30,7 @@
             return true;
         }
 
-        /*SiteAdmin siteAdmin = null;
+        SiteAdmin siteAdmin = null;
         String loginToken = httpServletRequest.getHeader(PropertiesUtil.getProperty("admin.cookie.name"));
         if (StringUtils.isNotEmpty(loginToken)) {
             String adminJsonStr = RedisShardedPoolUtils.get(loginToken);
@@ -60,7 +60,7 @@
             writer.flush();
             writer.close();
             return false;
-        }*/
+        }
 
 //        194.26.73.150, 172.70.34.195
 //        String ip = IpUtils.getIp(httpServletRequest);
diff --git a/src/main/java/com/nq/common/interceptor/ApiUserAuthorityInterceptor.java b/src/main/java/com/nq/common/interceptor/ApiUserAuthorityInterceptor.java
index 8f1ffaf..bac006e 100644
--- a/src/main/java/com/nq/common/interceptor/ApiUserAuthorityInterceptor.java
+++ b/src/main/java/com/nq/common/interceptor/ApiUserAuthorityInterceptor.java
@@ -85,7 +85,8 @@
             httpServletResponse.setCharacterEncoding("UTF-8");
             httpServletResponse.setContentType("application/json;charset=UTF-8");
             PrintWriter writer = httpServletResponse.getWriter();
-            writer.print( new Gson().toJson(ServerResponse.createByErrorCodeMsg(401,googleTranslateUtil.translate("请登录",lang ))));
+            //writer.print( new Gson().toJson(ServerResponse.createByErrorCodeMsg(401,googleTranslateUtil.translate("请登录",lang ))));
+            writer.print( new Gson().toJson(ServerResponse.createByErrorCodeMsg(401,"请登录")));
             writer.flush();
             writer.close();
             return false;
diff --git a/src/main/java/com/nq/controller/backend/AdminStockSubscribeController.java b/src/main/java/com/nq/controller/backend/AdminStockSubscribeController.java
index 8b27f83..e5ce22f 100644
--- a/src/main/java/com/nq/controller/backend/AdminStockSubscribeController.java
+++ b/src/main/java/com/nq/controller/backend/AdminStockSubscribeController.java
@@ -126,6 +126,5 @@
         return this.iUserStockSubscribeService.del(id, request);
     }
 
-    //大宗交易
 
 }
diff --git a/src/main/java/com/nq/dao/UserPositionMapper.java b/src/main/java/com/nq/dao/UserPositionMapper.java
index e01262d..4b82671 100644
--- a/src/main/java/com/nq/dao/UserPositionMapper.java
+++ b/src/main/java/com/nq/dao/UserPositionMapper.java
@@ -61,5 +61,4 @@
 
   UserPosition findUserPositionByCode(@Param("userId") Integer paramInteger,@Param("stockCode") String stockCode);
 
-  List<UserPosition> getUserPositionDzList(@Param("userId") Integer userId,@Param("checkType") Integer checkType);
 }
diff --git a/src/main/java/com/nq/service/IUserAssetsServices.java b/src/main/java/com/nq/service/IUserAssetsServices.java
index b6ccd8a..3ac85dc 100644
--- a/src/main/java/com/nq/service/IUserAssetsServices.java
+++ b/src/main/java/com/nq/service/IUserAssetsServices.java
@@ -49,12 +49,14 @@
 
     /**
      * ai交易
-     * @param userAssets
+     * @param accetType
+     * @param userId
      * @param eUserAssets
      * @param amount
+     * @param buyAmount
      * @return
      */
-    Boolean aiAvailableBalanceChange(UserAssets userAssets, EUserAssets eUserAssets, BigDecimal amount, BigDecimal buyAmount);
+    Boolean aiAvailableBalanceChange(String accetType, Integer userId, EUserAssets eUserAssets, BigDecimal amount, BigDecimal buyAmount);
 
     /**
      * 根据汇率转换金额
diff --git a/src/main/java/com/nq/service/impl/StockAiServiceImpl.java b/src/main/java/com/nq/service/impl/StockAiServiceImpl.java
index 3a3f8e5..11eacec 100644
--- a/src/main/java/com/nq/service/impl/StockAiServiceImpl.java
+++ b/src/main/java/com/nq/service/impl/StockAiServiceImpl.java
@@ -75,11 +75,12 @@
      * @return
      */
     @Override
+    @Transactional
     public ServerResponse buyStockAi(Long id, BigDecimal buyNum, HttpServletRequest request) {
         try {
             User user = iUserService.getCurrentUser(request);
             if (user == null ){
-                return ServerResponse.createBySuccessMsg("请先登录");
+                return ServerResponse.createByErrorCodeMsg(401,"请先登录");
             }
             Object lock = locks.computeIfAbsent(Long.valueOf(user.getId()), k -> new Object());
             synchronized (lock){
@@ -104,7 +105,7 @@
                 }
                 //获取用户账户
                 UserAssets userAssets = iUserAssetsServices.assetsByTypeAndUserId(EStockType.MX.getCode(), user.getId());
-                BigDecimal finalBuyNum = buyNum;    //购买金额
+                /*BigDecimal finalBuyNum = buyNum;    //购买金额
                 //如果不是墨西哥币需要转换金额
                 if (!stockAI.getStockType().equals(EStockType.MX.getCode())) {
                     EStockType stockType = EStockType.getEStockTypeByCode(stockAI.getStockType());
@@ -116,7 +117,7 @@
                     }
                     //转换为墨西哥币
                     buyNum = iUserAssetsServices.exchangeAmountByRate(buyNum, exchangeRate.getRata());
-                }
+                }*/
                 if(buyNum.compareTo(userAssets.getAvailableBalance()) > 0){
                     return ServerResponse.createByErrorMsg("可用余额不足" + userAssets.getAvailableBalance());
                 }
@@ -125,16 +126,19 @@
                     return ServerResponse.createByErrorMsg("请先缴清待补资金");
                 }
 
+                Boolean flag = iUserAssetsServices.aiAvailableBalanceChange(stockAI.getStockType(), user.getId(), EUserAssets.BUY_AI, buyNum, null);
+                if (!flag)  {
+                    return ServerResponse.createByErrorMsg("扣款失败,无法转换货币");
+                }
                 StockAIOrder stockAIOrder = new StockAIOrder();
                 stockAIOrder.setUserId(user.getId());
                 stockAIOrder.setStockAiId(id);
                 stockAIOrder.setBuyDate(new Date());
-                stockAIOrder.setBuyAmount(finalBuyNum);
-                stockAIOrder.setRemainAmount(finalBuyNum);
+                stockAIOrder.setBuyAmount(buyNum);
+                stockAIOrder.setRemainAmount(buyNum);
                 stockAIOrder.setRealEarning(BigDecimal.valueOf(0));
                 stockAIOrder.setStatus(EStockAIOrderStatus.wait.getStatus());   //等待审核
                 stockAiOrderMapper.insert(stockAIOrder);
-                iUserAssetsServices.aiAvailableBalanceChange(userAssets, EUserAssets.BUY_AI, buyNum, null);
                 return ServerResponse.createBySuccessMsg("下单成功");
             }
         } catch (Exception e) {
@@ -156,7 +160,7 @@
         try {
             User user = iUserService.getCurrentUser(request);
             if (user == null ){
-                return ServerResponse.createBySuccessMsg("请先登录");
+                return ServerResponse.createByErrorCodeMsg(401,"请先登录");
             }
             PageHelper.startPage(pageNum, pageSize);
             List<StockAiOrderTypeVO> stockAIOrders = stockAiOrderMapper.getStockAiOrderList(user.getId(), status);
@@ -330,7 +334,7 @@
                     StockAI stockAI = stockAiMapper.selectById(stockAIOrder.getStockAiId());
                     //买入金额
                     BigDecimal buyNum =  stockAIOrder.getBuyAmount();
-                    //如果不是墨西哥币需要转换金额
+                    /*//如果不是墨西哥币需要转换金额
                     if (!stockAI.getStockType().equals(EStockType.MX.getCode())) {
                         EStockType stockType = EStockType.getEStockTypeByCode(stockAI.getStockType());
                         ExchangeRate exchangeRate = exchangeRateRepository.findExchangeRateByCurrencyAndConversionCurrency(stockType.getSymbol(), EStockType.MX.getSymbol())
@@ -340,10 +344,11 @@
                         }
                         //转换为墨西哥币
                         buyNum = iUserAssetsServices.exchangeAmountByRate(buyNum, exchangeRate.getRata());
+                    }*/
+                    Boolean flag = iUserAssetsServices.aiAvailableBalanceChange(stockAI.getStockType(), stockAIOrder.getUserId(), EUserAssets.BUY_AI_REJECT, buyNum, null);
+                    if (!flag)  {
+                        return ServerResponse.createByErrorMsg("扣款失败,无法转换货币");
                     }
-                    //获取用户账户
-                    UserAssets userAssets = iUserAssetsServices.assetsByTypeAndUserId(EStockType.MX.getCode(), stockAIOrder.getUserId());
-                    iUserAssetsServices.aiAvailableBalanceChange(userAssets, EUserAssets.BUY_AI_REJECT, buyNum, null);
                 } else if (status.equals(EStockAIOrderStatus.finished.getStatus())) {
                     if (!stockAIOrder.getStatus().equals(EStockAIOrderStatus.passed.getStatus())) {
                         return ServerResponse.createByErrorMsg("只能结算申请通过订单");
@@ -364,11 +369,11 @@
                         amount = amount.add(earningsSUM);
                     }*/
                     StockAI stockAI = stockAiMapper.selectById(stockAIOrder.getStockAiId());
-                    //收益
+                    //收益+本金
                     BigDecimal amount = stockAIOrder.getBuyAmount().add(stockAIOrder.getRealEarning());
                     //需要解冻金额
                     BigDecimal buyAmount = stockAIOrder.getBuyAmount();
-                    //如果不是墨西哥币需要转换金额
+                    /*//如果不是墨西哥币需要转换金额
                     if (!stockAI.getStockType().equals(EStockType.MX.getCode())) {
                         EStockType stockType = EStockType.getEStockTypeByCode(stockAI.getStockType());
                         ExchangeRate exchangeRate = exchangeRateRepository.findExchangeRateByCurrencyAndConversionCurrency(stockType.getSymbol(), EStockType.MX.getSymbol())
@@ -379,10 +384,11 @@
                         //转换为墨西哥币
                         amount = iUserAssetsServices.exchangeAmountByRate(amount, exchangeRate.getRata());
                         buyAmount = iUserAssetsServices.exchangeAmountByRate(buyAmount, exchangeRate.getRata());
+                    }*/
+                    Boolean flag = iUserAssetsServices.aiAvailableBalanceChange(stockAI.getStockType(), stockAIOrder.getUserId(), EUserAssets.AI_SETTLEMENT, amount, buyAmount);
+                    if (!flag)  {
+                        return ServerResponse.createByErrorMsg("扣款失败,无法转换货币");
                     }
-                    //获取用户账户
-                    UserAssets userAssets = iUserAssetsServices.assetsByTypeAndUserId(EStockType.MX.getCode(), stockAIOrder.getUserId());
-                    iUserAssetsServices.aiAvailableBalanceChange(userAssets, EUserAssets.AI_SETTLEMENT, amount, buyAmount);
                 }
                 stockAIOrder.setStatus(status);
                 stockAIOrder.setAuditDate(new Date());
diff --git a/src/main/java/com/nq/service/impl/UserAssetsServices.java b/src/main/java/com/nq/service/impl/UserAssetsServices.java
index 3e4ad25..9f3b275 100644
--- a/src/main/java/com/nq/service/impl/UserAssetsServices.java
+++ b/src/main/java/com/nq/service/impl/UserAssetsServices.java
@@ -3,6 +3,7 @@
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
+import com.nq.Repository.ExchangeRateRepository;
 import com.nq.common.ServerResponse;
 import com.nq.dao.*;
 import com.nq.enums.EStockType;
@@ -49,6 +50,10 @@
 
     @Autowired
     UserPositionMapper userPositionMapper;
+
+    @Autowired
+    ExchangeRateRepository exchangeRateRepository;
+
 
     @Override
     public UserAssets assetsByTypeAndUserId(String accetType, Integer userId) {
@@ -172,8 +177,19 @@
 
     @Override
     public Boolean availablebalanceChange(String accetType, Integer userId, EUserAssets eUserAssets, BigDecimal amount, String desc, String descType) {
-
-        UserAssets userAssets = assetsByTypeAndUserId(accetType,userId);
+        //UserAssets userAssets = assetsByTypeAndUserId(accetType,userId);
+        //查询墨西哥账户
+        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());
+            }
+        }
         UserPosition userPosition = userPositionMapper.selectOne(new LambdaQueryWrapper<UserPosition>().gt(UserPosition::getAmountToBeCovered, BigDecimal.ZERO).eq(UserPosition::getUserId,userAssets.getUserId()));
         String type = eUserAssets.getDesc();
         String before = userAssets.getAvailableBalance().toString();
@@ -294,7 +310,22 @@
     }
 
 
-    public Boolean aiAvailableBalanceChange(UserAssets userAssets, EUserAssets eUserAssets, BigDecimal amount, BigDecimal buyAmount) {
+    public Boolean aiAvailableBalanceChange(String accetType, Integer userId, EUserAssets eUserAssets, BigDecimal amount, BigDecimal buyAmount) {
+        //查询墨西哥账户
+        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());
+                buyAmount = exchangeAmountByRate(buyAmount, exchangeRate.getRata());
+            } else {
+                return false;
+            }
+        }
         String before = userAssets.getAvailableBalance().toString();
         if (eUserAssets.getCode().equals(EUserAssets.BUY_AI.getCode())) {
             //冻结金额
@@ -307,9 +338,9 @@
             //归还可用金额
             userAssets.setAvailableBalance(userAssets.getAvailableBalance().add(amount));
         } else if (eUserAssets.getCode().equals(EUserAssets.AI_SETTLEMENT.getCode())) {
-            //解除冻结金额
+            //解除购买冻结金额
             userAssets.setFreezeMoney(userAssets.getFreezeMoney().add(buyAmount.negate()));
-            //归还可用金额(买入金额+收益)
+            //归还可用金额(购买金额+收益)
             userAssets.setAvailableBalance(userAssets.getAvailableBalance().add(amount));
         }
         String after = userAssets.getAvailableBalance().toString();
diff --git a/src/main/java/com/nq/service/impl/UserPositionServiceImpl.java b/src/main/java/com/nq/service/impl/UserPositionServiceImpl.java
index c991031..32376f1 100644
--- a/src/main/java/com/nq/service/impl/UserPositionServiceImpl.java
+++ b/src/main/java/com/nq/service/impl/UserPositionServiceImpl.java
@@ -1843,12 +1843,14 @@
         try {
             User user = iUserService.getCurrentUser(request);
             if (user == null ){
-                return ServerResponse.createBySuccessMsg("请先登录");
+                return ServerResponse.createByErrorCodeMsg(401,"请先登录");
             }
             PageHelper.startPage(pageNum, pageSize);
-            List<UserPosition> userPositions = userPositionMapper.getUserPositionDzList(user.getId(), status);
+            List<UserPositionCheckDz> userPositionCheckDzs = userPositionCheckDzService.list(
+                            new QueryWrapper<UserPositionCheckDz>()
+                            .eq("user_id", user.getId()).eq(status != null,"check_type", status));
             // 获取分页信息
-            PageInfo<UserPosition> pageInfo = new PageInfo<>(userPositions);
+            PageInfo<UserPositionCheckDz> pageInfo = new PageInfo<>(userPositionCheckDzs);
             return ServerResponse.createBySuccess(pageInfo);
         } catch (Exception e) {
             log.error("StockAiService getStockAiOrderList error", e);
diff --git a/src/main/java/com/nq/service/impl/UserStockSubscribeServiceImpl.java b/src/main/java/com/nq/service/impl/UserStockSubscribeServiceImpl.java
index f4b57d9..535c81f 100644
--- a/src/main/java/com/nq/service/impl/UserStockSubscribeServiceImpl.java
+++ b/src/main/java/com/nq/service/impl/UserStockSubscribeServiceImpl.java
@@ -475,7 +475,7 @@
             }
             return ServerResponse.createBySuccess(list);
         }
-        return ServerResponse.createByErrorMsg("请先登录",request);
+        return ServerResponse.createByErrorCodeMsg(401,"请先登录");
     }
     /**
      * 新股申购-用户提交金额
@@ -519,7 +519,7 @@
                 return ServerResponse.createByErrorMsg("新股认购订单不存在!",request);
             }
         }
-        return ServerResponse.createByErrorMsg("请先登录",request);
+        return ServerResponse.createByErrorCodeMsg(401,"请先登录");
     }
     /**
      * 新股申购-删除
diff --git a/src/main/resources/mapper/UserPositionMapper.xml b/src/main/resources/mapper/UserPositionMapper.xml
index 63c945d..d4e785c 100644
--- a/src/main/resources/mapper/UserPositionMapper.xml
+++ b/src/main/resources/mapper/UserPositionMapper.xml
@@ -542,21 +542,6 @@
     limit 1
   </select>
 
-  <select id="getUserPositionDzList" resultType="com.nq.pojo.UserPosition">
-        SELECT u.*
-            FROM user_position u
-        LEFT JOIN user_position_check_dz dz ON dz.id = u.dz_id
-
-    <where>
-      <if test="userId != null ">
-        and u.user_id = #{userId}
-      </if>
-      <if test="checkType != null ">
-        and dz.check_type = #{checkType}
-      </if>
-    </where>
-    ORDER BY u.buy_order_time DESC
-  </select>
 
 </mapper>
 

--
Gitblit v1.9.3