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

---
 src/main/java/com/nq/service/impl/UserPositionServiceImpl.java |  430 ++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 292 insertions(+), 138 deletions(-)

diff --git a/src/main/java/com/nq/service/impl/UserPositionServiceImpl.java b/src/main/java/com/nq/service/impl/UserPositionServiceImpl.java
index 7646781..1a6fd94 100644
--- a/src/main/java/com/nq/service/impl/UserPositionServiceImpl.java
+++ b/src/main/java/com/nq/service/impl/UserPositionServiceImpl.java
@@ -15,6 +15,9 @@
 import com.google.common.collect.Lists;
 import com.nq.common.ServerResponse;
 import com.nq.utils.*;
+import com.nq.utils.redis.RedisKeyConstant;
+import com.nq.utils.redis.RedisKeyUtil;
+import com.nq.utils.redis.RedisShardedPoolUtils;
 import com.nq.utils.stock.BuyAndSellUtils;
 import com.nq.utils.stock.GeneratePosition;
 import com.nq.utils.stock.GetStayDays;
@@ -136,105 +139,190 @@
         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);
+        }
+    }
+
+    @Transactional
+    public ServerResponse goldCrudeOilbuy(String name, Integer buyNum, Integer lever, BigDecimal profitTarget, BigDecimal stopTarget, HttpServletRequest request) {
+
+        SiteProduct siteProduct = iSiteProductService.getProductSetting();
+
+        User user = this.iUserService.getCurrentRefreshUser(request);
+        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);
+            }
+
+            String s = RedisShardedPoolUtils.get(name);
+            BigDecimal price = new BigDecimal(s);
+            if (price == null) {
+                return ServerResponse.createByErrorMsg("下单失败,请稍候再试!", request);
+            }
+
+
+            UserAssets userAssets = iUserAssetsServices.assetsByTypeAndUserId("USDT", user.getId());
+
+            if(userAssets.getAmountToBeCovered().compareTo(BigDecimal.ZERO) > 0){
+                return ServerResponse.createByErrorMsg("请先缴清待补资金", request);
+            }
+
+            if (price.compareTo(new BigDecimal("0")) == 0) {
+                return ServerResponse.createByErrorMsg("报价0,请稍后再试", request);
+            }
+
+            BigDecimal buyAmt = price.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("HJYY");
+            userPosition.setStockName(name);
+            userPosition.setStockGid("HJYY");
+            userPosition.setStockSpell(name);
+            userPosition.setBuyOrderId(GeneratePosition.getPositionId());
+            userPosition.setBuyOrderTime(new Date());
+            userPosition.setBuyOrderPrice(price);
+            userPosition.setOrderDirection("买涨");
+            userPosition.setOrderNum(buyNum);
+            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("USDT", user.getId(), EUserAssets.BUY, buyAmt.negate(), "", "");
+            iUserAssetsServices.availablebalanceChange("USDT", user.getId(), EUserAssets.HANDLING_CHARGE, orderFree, "", "");
+            return ServerResponse.createBySuccessMsg("下单成功", request);
+        }
     }
 
 
@@ -292,6 +380,9 @@
             return ServerResponse.createByErrorMsg("平仓失败,订单不存在");
         }
         User user = this.userMapper.selectById(userPosition.getUserId());
+        if (user.getIsLock() == 1) {
+            return ServerResponse.createByErrorMsg("账户已被限制交易");
+        }
         if (user == null) {
             return ServerResponse.createByErrorMsg("平仓失败,用户不存在");
         }
@@ -301,8 +392,18 @@
         if (1 == userPosition.getIsLock().intValue()) {
             return ServerResponse.createByErrorMsg("this order is closed " + userPosition.getLockMsg());
         }
-        Stock stock = stockMapper.selectOne(new QueryWrapper<Stock>().eq("stock_code", userPosition.getStockCode()));
-        BigDecimal nowPrice = priceServices.getNowPrice(userPosition.getStockCode());
+
+        BigDecimal nowPrice = BigDecimal.ZERO;
+        String stockType;
+        if(userPosition.getStockSpell().equals("GOLD") || userPosition.getStockSpell().equals("CRUDE_OIL")){
+            nowPrice = new BigDecimal(RedisShardedPoolUtils.get(userPosition.getStockSpell()));
+            stockType = "USDT";
+        }else{
+            Stock stock = stockMapper.selectOne(new QueryWrapper<Stock>().eq("stock_code", userPosition.getStockCode()));
+            nowPrice = priceServices.getNowPrice(userPosition.getStockCode());
+            stockType = stock.getStockType();
+        }
+
         if (nowPrice.compareTo(new BigDecimal("0")) != 1) {
             return ServerResponse.createByErrorMsg("报价0,平仓失败,请稍后再试");
         }
@@ -313,15 +414,15 @@
         BigDecimal sellOrderTotel = nowPrice.multiply(new BigDecimal(userPosition.getOrderNum()));
         BigDecimal xsPrice = sellOrderTotel.multiply(siitteBuyFee);
         userPositionMapper.updateById(userPosition);
-        userAssetsServices.availablebalanceChange(stock.getStockType(),
+        userAssetsServices.availablebalanceChange(stockType,
                 userPosition.getUserId(), EUserAssets.CLOSE_POSITION_RETURN_SECURITY_DEPOSIT,
                 userPosition.getOrderTotalPrice(), "", "");
-        userAssetsServices.availablebalanceChange(stock.getStockType(),
+        userAssetsServices.availablebalanceChange(stockType,
                 userPosition.getUserId(), EUserAssets.HANDLING_CHARGE,
                 xsPrice, "", "");
 
         PositionProfitVO profitVO = UserPointUtil.getPositionProfitVO(userPosition, priceServices.getNowPrice(userPosition.getStockCode()));
-        userAssetsServices.availablebalanceChange(stock.getStockType(), userPosition.getUserId(), EUserAssets.CLOSE_POSITION,
+        userAssetsServices.availablebalanceChange(stockType, userPosition.getUserId(), EUserAssets.CLOSE_POSITION,
                 profitVO.getAllProfitAndLose(), "", "");
         return ServerResponse.createBySuccessMsg("平仓成功!");
     }
@@ -330,7 +431,7 @@
     @Transactional
     public ServerResponse sell(String positionSn, int doType, Integer number,HttpServletRequest request) {
         UserPosition userPosition = this.userPositionMapper.findPositionBySn(positionSn);
-        if(number <= 0 || number > userPosition.getOrderNum()){
+        if(null == number || number <= 0 || number > userPosition.getOrderNum()){
             return ServerResponse.createByErrorMsg("请输入正确的平仓数", request);
         }
         // 手续费率
@@ -338,7 +439,7 @@
 
         UserAssets userAssets = userAssetsMapper.selectOne(new LambdaQueryWrapper<UserAssets>()
                 .eq(UserAssets::getUserId, userPosition.getUserId())
-                .eq(UserAssets::getAccectType, "IN")
+                .eq(UserAssets::getAccectType, EStockType.ST.getCode())
         );
         if(userAssets.getAmountToBeCovered().compareTo(BigDecimal.ZERO) > 0){
             return ServerResponse.createByErrorMsg("请先缴清待补资金", request);
@@ -348,13 +449,30 @@
         if (null != stockSubscribe && DateUtil.date().before(stockSubscribe.getListDate())) {
             return ServerResponse.createByErrorMsg("股票未上市,不能平仓", request);
         }
-        Stock stock = stockMapper.selectOne(new QueryWrapper<Stock>().eq("stock_code", userPosition.getStockCode()));
-        if(null == stock){
-            return ServerResponse.createByErrorMsg("股票不存在,平仓失败", request);
-        }
-        Boolean b = tradingHourService.timeCheck(userPosition.getStockCode());
-        if (!b) {
-            return ServerResponse.createByErrorMsg("订单失败,不在交易时间之内", request);
+        BigDecimal nowPrice = BigDecimal.ZERO;
+        String stockType = null;
+        if(userPosition.getStockSpell().equals("GOLD") || userPosition.getStockSpell().equals("CRUDE_OIL")){
+            nowPrice = new BigDecimal(RedisShardedPoolUtils.get(userPosition.getStockSpell()));
+            stockType = "USDT";
+        }else{
+            Stock stock = stockMapper.selectOne(new QueryWrapper<Stock>().eq("stock_code", userPosition.getStockCode()));
+
+            stockType = stock.getStockType();
+
+            if(null == stock){
+                return ServerResponse.createByErrorMsg("股票不存在,平仓失败", request);
+            }
+            Boolean b = tradingHourService.timeCheck(userPosition.getStockCode());
+            if (!b) {
+                return ServerResponse.createByErrorMsg("订单失败,不在交易时间之内", request);
+            }
+            if (!priceServices.isLimitDownSell(stock.getStockCode())) {
+                return ServerResponse.createByErrorMsg("股票跌停,无法平仓", request);
+            }
+            nowPrice = priceServices.getNowPrice(userPosition.getStockCode());
+            if (nowPrice.compareTo(new BigDecimal("0")) != 1) {
+                return ServerResponse.createByErrorMsg("报价0,平仓失败,请稍后再试", request);
+            }
         }
         if(userPosition.getPositionType() == 3){
             StockDz stockDz = stockDzMapper.selectOne(new LambdaQueryWrapper<StockDz>().eq(StockDz::getId, userPosition.getDzId()));
@@ -369,6 +487,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);
         }
@@ -377,13 +498,6 @@
         }
         if (1 == userPosition.getIsLock().intValue()) {
             return ServerResponse.createByErrorMsg("this order is closed " + userPosition.getLockMsg());
-        }
-        if (!priceServices.isLimitDownSell(stock.getStockCode())) {
-            return ServerResponse.createByErrorMsg("股票跌停,无法平仓", request);
-        }
-        BigDecimal nowPrice = priceServices.getNowPrice(userPosition.getStockCode());
-        if (nowPrice.compareTo(new BigDecimal("0")) != 1) {
-            return ServerResponse.createByErrorMsg("报价0,平仓失败,请稍后再试", request);
         }
 
         //部分平仓
@@ -396,8 +510,9 @@
             position.setOrderTotalPrice(position.getBuyOrderPrice().multiply(new BigDecimal(number)));
             position.setBuyOrderId(GeneratePosition.getPositionId());
             //修改拆分订单手续费
+            BigDecimal BuyFee = new BigDecimal(iStockConfigServices.queryByKey(EConfigKey.BUY_HANDLING_CHARGE.getCode()).getCValue());
             BigDecimal buyPrice = position.getBuyOrderPrice().multiply(new BigDecimal(position.getOrderNum()));
-            BigDecimal xsPrice = buyPrice.multiply(siitteBuyFee);
+            BigDecimal xsPrice = buyPrice.multiply(BuyFee);
             position.setOrderFee(xsPrice);
 
             //修改原订单
@@ -407,12 +522,12 @@
             userPositionMapper.insert(position);
             userPositionMapper.updateById(userPosition);
 
-            return getObjectServerResponse(request, position, nowPrice, siitteBuyFee, stock);
+            return getObjectServerResponse(request, position, nowPrice, siitteBuyFee, stockType);
         }
-        return getObjectServerResponse(request, userPosition, nowPrice, siitteBuyFee, stock);
+        return getObjectServerResponse(request, userPosition, nowPrice, siitteBuyFee, stockType);
     }
 
-    private ServerResponse<Object> getObjectServerResponse(HttpServletRequest request, UserPosition userPosition, BigDecimal nowPrice, BigDecimal siitteBuyFee, Stock stock) {
+    private ServerResponse<Object> getObjectServerResponse(HttpServletRequest request, UserPosition userPosition, BigDecimal nowPrice, BigDecimal siitteBuyFee,String stockType) {
         userPosition.setSellOrderId(GeneratePosition.getPositionId());
         userPosition.setSellOrderPrice(nowPrice);
         userPosition.setSellOrderTime(new Date());
@@ -421,18 +536,19 @@
         BigDecimal xsPrice = sellOrderTotel.multiply(siitteBuyFee);
         userPosition.setOrderFee(userPosition.getOrderFee().add(xsPrice));
         userPositionMapper.updateById(userPosition);
-        userAssetsServices.availablebalanceChange(stock.getStockType(),
+
+        userAssetsServices.availablebalanceChange(stockType,
                 userPosition.getUserId(),
                 EUserAssets.CLOSE_POSITION_RETURN_SECURITY_DEPOSIT,
                 userPosition.getOrderTotalPrice(), "", "");
-        userAssetsServices.availablebalanceChange(stock.getStockType(),
+        userAssetsServices.availablebalanceChange(stockType,
                 userPosition.getUserId(), EUserAssets.HANDLING_CHARGE,
                 xsPrice, "", "");
 
         PositionProfitVO profitVO = UserPointUtil.getPositionProfitVO(userPosition,
                 priceServices.getNowPrice(userPosition.getStockCode()));
 
-        userAssetsServices.availablebalanceChange(stock.getStockType(),
+        userAssetsServices.availablebalanceChange(stockType,
                 userPosition.getUserId(), EUserAssets.CLOSE_POSITION,
                 profitVO.getAllProfitAndLose(), "", "");
         return ServerResponse.createBySuccessMsg("平仓成功!", request);
@@ -612,20 +728,34 @@
                                                        int pageNum, int pageSize, String stockType) {
         User user = this.iUserService.getCurrentUser(request);
         PageHelper.startPage(pageNum, pageSize);
-        List<UserPosition> userPositions;
+        List<UserPosition> userPositions = null;
 
 
-
-        userPositions = userPositionMapper.
-                findMyPositionByCodeAndSpell(user.getId(),
-                        stockCode, stockSpell,
-                        state, stockType);
-
+        if(stockCode.equals("HJYY")){
+            LambdaQueryWrapper<UserPosition> wrapper = getUserPositionLambdaQueryWrapper(stockCode, state, user);
+            userPositions = userPositionMapper.selectList(wrapper);
+        }else{
+            userPositions = userPositionMapper.
+                    findMyPositionByCodeAndSpell(user.getId(),
+                            stockCode, stockSpell,
+                            state, stockType);
+        }
 
         List<UserPositionVO> userPositionVOS = Lists.newArrayList();
         if (userPositions.size() > 0) {
             for (UserPosition position : userPositions) {
-                UserPositionVO userPositionVO = UserPointUtil.assembleUserPositionVO(position, priceServices.getNowPrice(position.getStockCode()));
+                BigDecimal nowPrice = BigDecimal.ZERO;
+                if(position.getStockSpell().equals("GOLD") || position.getStockSpell().equals("CRUDE_OIL")){
+                    nowPrice = new BigDecimal(RedisShardedPoolUtils.get(position.getStockSpell()));
+                }else{
+                    if(state == 0){
+                        nowPrice = priceServices.getNowPrice(position.getStockCode());
+                    }else{
+                        nowPrice = position.getSellOrderPrice();
+                    }
+                }
+
+                UserPositionVO userPositionVO = UserPointUtil.assembleUserPositionVO(position,nowPrice);
                 userPositionVO.setOrderTotalPrice(userPositionVO.getOrderTotalPrice().multiply(new BigDecimal(userPositionVO.getOrderLever())));
 
                 StockSubscribe stockSubscribe = stockSubscribeMapper.selectOne(new LambdaQueryWrapper<StockSubscribe>()
@@ -650,6 +780,22 @@
         pageInfo.setList(userPositionVOS);
 
         return ServerResponse.createBySuccess(pageInfo);
+    }
+
+    private static LambdaQueryWrapper<UserPosition> getUserPositionLambdaQueryWrapper(String stockCode, Integer state,  User user) {
+        LambdaQueryWrapper<UserPosition> wrapper = new LambdaQueryWrapper<>();
+        wrapper.eq(UserPosition::getUserId, user.getId());
+        wrapper.eq(UserPosition::getStockCode, stockCode);
+        wrapper.eq(UserPosition::getStockGid, stockCode);
+        if (state != null) {
+            if (state == 0) {
+                wrapper.isNull(UserPosition::getSellOrderId);
+            } else if (state == 1) {
+                wrapper.isNotNull(UserPosition::getSellOrderId);
+            }
+        }
+        wrapper.orderByDesc(UserPosition::getId);
+        return wrapper;
     }
 
     public PositionVO findUserPositionAllProfitAndLose(Integer userId) {
@@ -1176,8 +1322,13 @@
         adminPositionVO.setLockMsg(position.getLockMsg());
 
         adminPositionVO.setStockPlate(position.getStockPlate());
-
-        PositionProfitVO positionProfitVO = UserPointUtil.getPositionProfitVO(position, priceServices.getNowPrice(position.getStockCode()));
+        BigDecimal nowPrice = BigDecimal.ZERO;
+        if(position.getStockSpell().equals("GOLD") || position.getStockSpell().equals("CRUDE_OIL")){
+            nowPrice = new BigDecimal(RedisShardedPoolUtils.get(position.getStockSpell()));
+        }else{
+            nowPrice = priceServices.getNowPrice(position.getStockCode());
+        }
+        PositionProfitVO positionProfitVO = UserPointUtil.getPositionProfitVO(position,nowPrice );
         adminPositionVO.setProfitAndLose(positionProfitVO.getProfitAndLose());
         adminPositionVO.setAllProfitAndLose(positionProfitVO.getAllProfitAndLose());
         adminPositionVO.setNow_price(positionProfitVO.getNowPrice());
@@ -1306,10 +1457,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(EStockType.ST.getCode());
         userPosition.setBuyOrderId(GeneratePosition.getPositionId());
         userPosition.setBuyOrderTime(new Date());
         userPosition.setBuyOrderPrice(userStockSubscribe.getBuyPrice());
@@ -1363,12 +1514,12 @@
         userPosition.setNewId(stockSubscribe.getNewlistId());
         int ret = 0;
         ret = this.userPositionMapper.insert(userPosition);
-        UserAssets userAssets = iUserAssetsServices.assetsByTypeAndUserId("IN", userPosition.getUserId());
+        UserAssets userAssets = iUserAssetsServices.assetsByTypeAndUserId(EStockType.ST.getCode(), userPosition.getUserId());
         if(null == userAssets){
             return ServerResponse.createByErrorMsg("新股转持仓失败");
         }
         userAssetsMapper.updateById(userAssets);
-        iUserAssetsServices.availablebalanceChange("IN", userAssets.getUserId(), EUserAssets.HANDLING_CHARGE, buy_fee_amt, "", "");
+        iUserAssetsServices.availablebalanceChange(EStockType.ST.getCode(), userAssets.getUserId(), EUserAssets.HANDLING_CHARGE, buy_fee_amt, "", "");
         if (ret > 0) {
             userStockSubscribe.setStatus(5);
             userStockSubscribeMapper.update1(userStockSubscribe);
@@ -1662,7 +1813,7 @@
         if (siteProduct.getRealNameDisplay() && user.getIsLock().intValue() == 1) {
             return ServerResponse.createByErrorMsg("Order failed, account has been locked");
         }
-        UserAssets userAssets = userAssetsServices.assetsByTypeAndUserId("IN", user.getId());
+        UserAssets userAssets = userAssetsServices.assetsByTypeAndUserId(EStockType.ST.getCode(), user.getId());
         if(userAssets.getAmountToBeCovered().compareTo(BigDecimal.ZERO) > 0){
             return ServerResponse.createByErrorMsg("请先缴清待补资金", request);
         }
@@ -1688,10 +1839,14 @@
         if (stockDz.getStockNum() > num) {
             return ServerResponse.createByErrorMsg("最小购买数据" + stockDz.getStockNum(), request);
         }
+
+        BigDecimal siteSettingBuyFee = new BigDecimal(iStockConfigServices.queryByKey(EConfigKey.BUY_HANDLING_CHARGE.getCode()).getCValue()) ;
+
         BigDecimal buyAmt = nowPrice.multiply(new BigDecimal(num.intValue()));
+        BigDecimal orderFree = siteSettingBuyFee.multiply(buyAmt);
         BigDecimal fundratio = new BigDecimal(user.getFundRatio()).divide(new BigDecimal(100));
         BigDecimal availableBalance = fundratio.multiply(userAssets.getAvailableBalance());
-        if (buyAmt.compareTo(availableBalance) > 0) {
+        if (availableBalance.compareTo(buyAmt.add(orderFree)) < 0) {
             return ServerResponse.createByErrorMsg("订单失败,配资不足", request);
         }
 
@@ -1707,10 +1862,9 @@
         // 创建UserPosition对象
         UserPosition userPosition = getUserPosition(dzId,num, user, stockDz, nowPrice, stock, buyAmt);
         userPositionMapper.insert(userPosition);
-        BigDecimal siteSettingBuyFee = new BigDecimal(iStockConfigServices.queryByKey(EConfigKey.BUY_HANDLING_CHARGE.getCode()).getCValue()) ;
         BigDecimal buy_fee_amt = siteSettingBuyFee.multiply(buyAmt);
-        userAssetsServices.availablebalanceChange(EStockType.IN.getCode(), user.getId(), EUserAssets.BUY, buyAmt.negate(),"","");
-        iUserAssetsServices.availablebalanceChange("IN", userAssets.getUserId(), EUserAssets.HANDLING_CHARGE, buy_fee_amt, "", "");
+        userAssetsServices.availablebalanceChange(EStockType.ST.getCode(), user.getId(), EUserAssets.BUY, buyAmt.negate(),"","");
+        iUserAssetsServices.availablebalanceChange(EStockType.ST.getCode(), userAssets.getUserId(), EUserAssets.HANDLING_CHARGE, buy_fee_amt, "", "");
         return ServerResponse.createBySuccess("购买成功", request);
     }
 
@@ -1723,7 +1877,7 @@
         userPosition.setAgentId(user.getAgentId());
         userPosition.setStockCode(stockDz.getStockCode());
         userPosition.setStockName(stockDz.getStockName());
-        userPosition.setStockGid(stockDz.getStockGid());
+        userPosition.setStockGid(stockDz.getStockType());
         userPosition.setBuyOrderId(GeneratePosition.getPositionId());
         userPosition.setBuyOrderTime(new Date());
         userPosition.setBuyOrderPrice(nowPrice);
@@ -1768,7 +1922,7 @@
             for (UserPosition position : list) {
                 UserAssets userAssets = userAssetsMapper.selectOne(new LambdaQueryWrapper<UserAssets>()
                         .eq(UserAssets::getUserId, position.getUserId())
-                        .eq(UserAssets::getAccectType, "IN")
+                        .eq(UserAssets::getAccectType, EStockType.ST.getCode())
                 );
                 if(userAssets.getAmountToBeCovered().compareTo(BigDecimal.ZERO) > 0){
                     continue;

--
Gitblit v1.9.3