From a8bc17f577f2bd997bcd6081e700cb3bc63e4283 Mon Sep 17 00:00:00 2001
From: zj <1772600164@qq.com>
Date: Thu, 17 Apr 2025 15:03:50 +0800
Subject: [PATCH] 1

---
 src/main/java/com/nq/service/impl/UserPositionServiceImpl.java |  190 ++++++++++++++++++++++++----------------------
 1 files changed, 99 insertions(+), 91 deletions(-)

diff --git a/src/main/java/com/nq/service/impl/UserPositionServiceImpl.java b/src/main/java/com/nq/service/impl/UserPositionServiceImpl.java
index 55d5fdd..2ff418f 100644
--- a/src/main/java/com/nq/service/impl/UserPositionServiceImpl.java
+++ b/src/main/java/com/nq/service/impl/UserPositionServiceImpl.java
@@ -137,105 +137,107 @@
         SiteProduct siteProduct = iSiteProductService.getProductSetting();
 
         User user = this.iUserService.getCurrentRefreshUser(request);
-        if (siteProduct.getRealNameDisplay() && user.getIsActive() != 2) {
-            return ServerResponse.createByErrorMsg("订单失败,请先实名认证", request);
-        }
-        // 手续费率
-        BigDecimal siteSettingBuyFee = new BigDecimal(iStockConfigServices.queryByKey(EConfigKey.BUY_HANDLING_CHARGE.getCode()).getCValue()) ;
+        synchronized (user.getId()){
+            if (siteProduct.getRealNameDisplay() && user.getIsActive() != 2) {
+                return ServerResponse.createByErrorMsg("订单失败,请先实名认证", request);
+            }
+            // 手续费率
+            BigDecimal siteSettingBuyFee = new BigDecimal(iStockConfigServices.queryByKey(EConfigKey.BUY_HANDLING_CHARGE.getCode()).getCValue()) ;
 
-        if (siteProduct.getRealNameDisplay() && user.getIsLock().intValue() == 1) {
-            return ServerResponse.createByErrorMsg("订单失败,帐户已被锁定", request);
-        }
+            if (siteProduct.getRealNameDisplay() && user.getIsLock().intValue() == 1) {
+                return ServerResponse.createByErrorMsg("订单失败,帐户已被锁定", request);
+            }
 
-        Stock stock = stockMapper.selectByPrimaryKey(stockId);
-        if (stock == null) {
-            return ServerResponse.createByErrorMsg("订单失败,股票代码不存在", request);
-        }
-        //判断股票是否在可交易时间段
-        Boolean b = tradingHourService.timeCheck(stock.getStockCode());
-        if (!b) {
-            return ServerResponse.createByErrorMsg("订单失败,不在交易时间之内", request);
-        }
+            Stock stock = stockMapper.selectByPrimaryKey(stockId);
+            if (stock == null) {
+                return ServerResponse.createByErrorMsg("订单失败,股票代码不存在", request);
+            }
+            //判断股票是否在可交易时间段
+            Boolean b = tradingHourService.timeCheck(stock.getStockCode());
+            if (!b) {
+                return ServerResponse.createByErrorMsg("订单失败,不在交易时间之内", request);
+            }
 
 
-       StockConfig mainBuyConfig =  iStockConfigServices.queryByKey(EConfigKey.MIN_BUY.getCode());
+            StockConfig mainBuyConfig =  iStockConfigServices.queryByKey(EConfigKey.MIN_BUY.getCode());
 
-        if(buyNum<Integer.parseInt(mainBuyConfig.getCValue())){
-            return ServerResponse.createByErrorMsg("最低购买数量"+mainBuyConfig.getCValue(), request);
-        }
+            if(buyNum<Integer.parseInt(mainBuyConfig.getCValue())){
+                return ServerResponse.createByErrorMsg("最低购买数量"+mainBuyConfig.getCValue(), request);
+            }
 
-        UserAssets userAssets = iUserAssetsServices.assetsByTypeAndUserId(stock.getStockType(), user.getId());
-        StockConfig maxBuyConfig =  iStockConfigServices.queryByKey(EConfigKey.MAX_BUY.getCode());
-        if(buyNum<Integer.parseInt(mainBuyConfig.getCValue())){
-            return ServerResponse.createByErrorMsg("最高购买数量"+maxBuyConfig.getCValue(), request);
-        }
-        if(userAssets.getAmountToBeCovered().compareTo(BigDecimal.ZERO) > 0){
-            return ServerResponse.createByErrorMsg("请先缴清待补资金", request);
-        }
-        if (stock.getIsLock() != 0) {
-            return ServerResponse.createByErrorMsg("订单失败,股票被锁定", request);
-        }
+            UserAssets userAssets = iUserAssetsServices.assetsByTypeAndUserId(stock.getStockType(), user.getId());
+            StockConfig maxBuyConfig =  iStockConfigServices.queryByKey(EConfigKey.MAX_BUY.getCode());
+            if(buyNum<Integer.parseInt(mainBuyConfig.getCValue())){
+                return ServerResponse.createByErrorMsg("最高购买数量"+maxBuyConfig.getCValue(), request);
+            }
+            if(userAssets.getAmountToBeCovered().compareTo(BigDecimal.ZERO) > 0){
+                return ServerResponse.createByErrorMsg("请先缴清待补资金", request);
+            }
+            if (stock.getIsLock() != 0) {
+                return ServerResponse.createByErrorMsg("订单失败,股票被锁定", request);
+            }
 
-        if (!priceServices.isLimitUpBuy(stock.getStockCode())) {
-            return ServerResponse.createByErrorMsg("暂无配额", request);
-        }
+            if (!priceServices.isLimitUpBuy(stock.getStockCode())) {
+                return ServerResponse.createByErrorMsg("暂无配额", request);
+            }
 
-        //股票类型 现价 数据源的处理
-        BigDecimal nowPrice = priceServices.getNowPrice(stock.getStockCode());
+            //股票类型 现价 数据源的处理
+            BigDecimal nowPrice = priceServices.getNowPrice(stock.getStockCode());
 
-        if (nowPrice.compareTo(new BigDecimal("0")) == 0) {
-            return ServerResponse.createByErrorMsg("报价0,请稍后再试", request);
-        }
+            if (nowPrice.compareTo(new BigDecimal("0")) == 0) {
+                return ServerResponse.createByErrorMsg("报价0,请稍后再试", request);
+            }
 
-        BigDecimal buyAmt = nowPrice.multiply(new BigDecimal(buyNum)).divide(new BigDecimal(lever));
-        BigDecimal orderFree = siteSettingBuyFee.multiply(buyAmt);
+            BigDecimal buyAmt = nowPrice.multiply(new BigDecimal(buyNum)).divide(new BigDecimal(lever));
+            BigDecimal orderFree = siteSettingBuyFee.multiply(buyAmt);
 
-        BigDecimal   fundratio = new BigDecimal(user.getFundRatio()).divide(new BigDecimal(100));
-        BigDecimal availableBalance =  fundratio.multiply(userAssets.getAvailableBalance());
-        if (availableBalance.compareTo(buyAmt.add(orderFree)) < 0) {
-            return ServerResponse.createByErrorMsg("订单失败,配资不足", request);
-        }
-        UserPosition userPosition = new UserPosition();
-        if (profitTarget != null && profitTarget.compareTo(new BigDecimal("0")) > 0) {
-            userPosition.setProfitTargetPrice(profitTarget);
-        }
-        if (stopTarget != null && stopTarget.compareTo(new BigDecimal("0")) > 0) {
-            userPosition.setStopTargetPrice(stopTarget);
-        }
-        userPosition.setPositionType(user.getAccountType());
-        userPosition.setPositionSn(KeyUtils.getUniqueKey());
-        userPosition.setUserId(user.getId());
-        userPosition.setNickName(user.getRealName());
-        userPosition.setAgentId(user.getAgentId());
-        userPosition.setStockCode(stock.getStockCode());
-        userPosition.setStockName(stock.getStockName());
-        userPosition.setStockGid(stock.getStockType());
-        userPosition.setStockSpell(stock.getStockSpell());
-        userPosition.setBuyOrderId(GeneratePosition.getPositionId());
-        userPosition.setBuyOrderTime(new Date());
-        userPosition.setBuyOrderPrice(nowPrice);
-        userPosition.setOrderDirection((buyType.intValue() == 0) ? "买涨" : "买跌");
-        userPosition.setOrderNum(buyNum);
-        if (stock.getStockPlate() != null) {
-            userPosition.setStockPlate(stock.getStockPlate());
-        }
-        userPosition.setIsLock(Integer.valueOf(0));
-        userPosition.setOrderLever(lever);
-        userPosition.setOrderTotalPrice(buyAmt);
-        // 手续费
+            BigDecimal   fundratio = new BigDecimal(user.getFundRatio()).divide(new BigDecimal(100));
+            BigDecimal availableBalance =  fundratio.multiply(userAssets.getAvailableBalance());
+            if (availableBalance.compareTo(buyAmt.add(orderFree)) < 0) {
+                return ServerResponse.createByErrorMsg("订单失败,配资不足", request);
+            }
+            UserPosition userPosition = new UserPosition();
+            if (profitTarget != null && profitTarget.compareTo(new BigDecimal("0")) > 0) {
+                userPosition.setProfitTargetPrice(profitTarget);
+            }
+            if (stopTarget != null && stopTarget.compareTo(new BigDecimal("0")) > 0) {
+                userPosition.setStopTargetPrice(stopTarget);
+            }
+            userPosition.setPositionType(user.getAccountType());
+            userPosition.setPositionSn(KeyUtils.getUniqueKey());
+            userPosition.setUserId(user.getId());
+            userPosition.setNickName(user.getRealName());
+            userPosition.setAgentId(user.getAgentId());
+            userPosition.setStockCode(stock.getStockCode());
+            userPosition.setStockName(stock.getStockName());
+            userPosition.setStockGid(stock.getStockType());
+            userPosition.setStockSpell(stock.getStockSpell());
+            userPosition.setBuyOrderId(GeneratePosition.getPositionId());
+            userPosition.setBuyOrderTime(new Date());
+            userPosition.setBuyOrderPrice(nowPrice);
+            userPosition.setOrderDirection((buyType.intValue() == 0) ? "买涨" : "买跌");
+            userPosition.setOrderNum(buyNum);
+            if (stock.getStockPlate() != null) {
+                userPosition.setStockPlate(stock.getStockPlate());
+            }
+            userPosition.setIsLock(Integer.valueOf(0));
+            userPosition.setOrderLever(lever);
+            userPosition.setOrderTotalPrice(buyAmt);
+            // 手续费
 
-        userPosition.setOrderFee(orderFree);
-        userPosition.setOrderSpread(BigDecimal.ZERO);
-        userPosition.setSpreadRatePrice(BigDecimal.ZERO);
-        BigDecimal profit_and_lose = new BigDecimal("0");
-        userPosition.setProfitAndLose(profit_and_lose);
-        userPosition.setAllProfitAndLose(profit_and_lose.add(orderFree));
-        userPosition.setOrderStayDays(Integer.valueOf(0));
-        userPosition.setOrderStayFee(BigDecimal.ZERO);
-        userPositionMapper.insert(userPosition);
-        iUserAssetsServices.availablebalanceChange(stock.getStockType(), user.getId(), EUserAssets.BUY, buyAmt.negate(), "", "");
-        iUserAssetsServices.availablebalanceChange(stock.getStockType(), user.getId(), EUserAssets.HANDLING_CHARGE, orderFree, "", "");
-        return ServerResponse.createBySuccessMsg("下单成功", request);
+            userPosition.setOrderFee(orderFree);
+            userPosition.setOrderSpread(BigDecimal.ZERO);
+            userPosition.setSpreadRatePrice(BigDecimal.ZERO);
+            BigDecimal profit_and_lose = new BigDecimal("0");
+            userPosition.setProfitAndLose(profit_and_lose);
+            userPosition.setAllProfitAndLose(profit_and_lose.add(orderFree));
+            userPosition.setOrderStayDays(Integer.valueOf(0));
+            userPosition.setOrderStayFee(BigDecimal.ZERO);
+            userPositionMapper.insert(userPosition);
+            iUserAssetsServices.availablebalanceChange(stock.getStockType(), user.getId(), EUserAssets.BUY, buyAmt.negate(), "", "");
+            iUserAssetsServices.availablebalanceChange(stock.getStockType(), user.getId(), EUserAssets.HANDLING_CHARGE, orderFree, "", "");
+            return ServerResponse.createBySuccessMsg("下单成功", request);
+        }
     }
 
 
@@ -293,6 +295,9 @@
             return ServerResponse.createByErrorMsg("平仓失败,订单不存在");
         }
         User user = this.userMapper.selectById(userPosition.getUserId());
+        if (user.getIsLock() == 1) {
+            return ServerResponse.createByErrorMsg("账户已被限制交易");
+        }
         if (user == null) {
             return ServerResponse.createByErrorMsg("平仓失败,用户不存在");
         }
@@ -370,6 +375,9 @@
             return ServerResponse.createByErrorMsg("平仓失败,订单不存在", request);
         }
         User user = this.userMapper.selectById(userPosition.getUserId());
+        if (user.getIsLock() == 1) {
+            return ServerResponse.createByErrorMsg("账户已被限制交易", request);
+        }
         if (user == null) {
             return ServerResponse.createByErrorMsg("平仓失败,用户不存在", request);
         }
@@ -1319,10 +1327,10 @@
         userPosition.setAgentId(userStockSubscribe.getAgentId());
 
         userPosition.setStockName(userStockSubscribe.getNewName());
-        StringBuffer gid = new StringBuffer();
-        gid.append(stockSubscribe.getStockType()!=null?stockSubscribe.getStockType():"");
-        gid.append(userStockSubscribe.getNewCode()!=null?userStockSubscribe.getNewCode():"stock code invaild");
-        userPosition.setStockGid(gid.toString());
+//        StringBuffer gid = new StringBuffer();
+//        gid.append(stockSubscribe.getStockType()!=null?stockSubscribe.getStockType():"");
+//        gid.append(userStockSubscribe.getNewCode()!=null?userStockSubscribe.getNewCode():"stock code invaild");
+        userPosition.setStockGid("IN");
         userPosition.setBuyOrderId(GeneratePosition.getPositionId());
         userPosition.setBuyOrderTime(new Date());
         userPosition.setBuyOrderPrice(userStockSubscribe.getBuyPrice());

--
Gitblit v1.9.3