From 5ce072de78ae263b52dc0e2be5c7391b23c093cf Mon Sep 17 00:00:00 2001
From: zj <1772600164@qq.com>
Date: Sun, 27 Apr 2025 18:29:22 +0800
Subject: [PATCH] 1

---
 src/main/java/com/nq/service/impl/UserPositionServiceImpl.java |  203 ++++++++++++++++++++++++++++++++------------------
 1 files changed, 129 insertions(+), 74 deletions(-)

diff --git a/src/main/java/com/nq/service/impl/UserPositionServiceImpl.java b/src/main/java/com/nq/service/impl/UserPositionServiceImpl.java
index 0022bfb..1fc5585 100644
--- a/src/main/java/com/nq/service/impl/UserPositionServiceImpl.java
+++ b/src/main/java/com/nq/service/impl/UserPositionServiceImpl.java
@@ -131,7 +131,8 @@
     @Autowired
     UserPositionCheckDzService userPositionCheckDzService;
 
-
+    @Autowired
+    UserPendingorderService userPendingorderService;
 
     @Transactional
     public ServerResponse buy(Integer stockId, Integer buyNum, Integer buyType, Integer lever, BigDecimal profitTarget, BigDecimal stopTarget, HttpServletRequest request) {
@@ -243,7 +244,7 @@
     }
 
     @Transactional
-    public ServerResponse goldCrudeOilbuy(String name, Integer buyNum, Integer lever, BigDecimal profitTarget, BigDecimal stopTarget, HttpServletRequest request) {
+    public ServerResponse goldCrudeOilbuy(String name, Integer buyNum, Integer buyType,Integer lever, BigDecimal profitTarget, BigDecimal stopTarget, HttpServletRequest request) {
 
         SiteProduct siteProduct = iSiteProductService.getProductSetting();
 
@@ -296,14 +297,14 @@
             userPosition.setUserId(user.getId());
             userPosition.setNickName(user.getRealName());
             userPosition.setAgentId(user.getAgentId());
-            userPosition.setStockCode(name);
+            userPosition.setStockCode("HJYY");
             userPosition.setStockName(name);
-            userPosition.setStockGid(name);
+            userPosition.setStockGid("HJYY");
             userPosition.setStockSpell(name);
             userPosition.setBuyOrderId(GeneratePosition.getPositionId());
             userPosition.setBuyOrderTime(new Date());
             userPosition.setBuyOrderPrice(price);
-            userPosition.setOrderDirection("买涨");
+            userPosition.setOrderDirection((buyType.intValue() == 0) ? "买涨" : "买跌");
             userPosition.setOrderNum(buyNum);
             userPosition.setIsLock(Integer.valueOf(0));
             userPosition.setOrderLever(lever);
@@ -392,8 +393,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("XAUUSD") || userPosition.getStockSpell().equals("USOIL")){
+            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,平仓失败,请稍后再试");
         }
@@ -404,15 +415,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("平仓成功!");
     }
@@ -439,13 +450,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("XAUUSD") || userPosition.getStockSpell().equals("USOIL")){
+            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()));
@@ -472,13 +500,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);
-        }
 
         //部分平仓
         if(number < userPosition.getOrderNum()){
@@ -502,12 +523,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());
@@ -516,18 +537,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);
@@ -704,58 +726,86 @@
 
     public ServerResponse findMyPositionByCodeAndSpell(String stockCode, String stockSpell,
                                                        Integer state, HttpServletRequest request,
-                                                       int pageNum, int pageSize, String stockType) {
+                                                       int pageNum, int pageSize, String stockType,Integer pendingStatus) {
         User user = this.iUserService.getCurrentUser(request);
         PageHelper.startPage(pageNum, pageSize);
-        List<UserPosition> userPositions;
+        List<UserPosition> userPositions = null;
+        if(state == 2){
+            List<UserPendingorder> list = userPendingorderService.list(new LambdaQueryWrapper<>(UserPendingorder.class)
+                    .eq(UserPendingorder::getUserId, user.getId())
+                    .eq(UserPendingorder::getStockGid,(null != stockCode && "HJYY".equals(stockCode)) ? stockCode :  stockType)
+                    .eq(UserPendingorder::getPositionType,pendingStatus)
+            );
+            PageInfo pageInfo = new PageInfo();
+            pageInfo.setList(list);
+            return ServerResponse.createBySuccess(pageInfo);
+        }else {
+            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) {
+                    BigDecimal nowPrice = BigDecimal.ZERO;
+                    if (position.getStockSpell().equals("XAUUSD") || position.getStockSpell().equals("USOIL")) {
+                        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())));
 
-        userPositions = userPositionMapper.
-                findMyPositionByCodeAndSpell(user.getId(),
-                        stockCode, stockSpell,
-                        state, stockType);
-
-
-        List<UserPositionVO> userPositionVOS = Lists.newArrayList();
-        if (userPositions.size() > 0) {
-            for (UserPosition position : userPositions) {
-                BigDecimal nowPrice = BigDecimal.ZERO;
-                if(state == 0){
-                    nowPrice = priceServices.getNowPrice(position.getStockCode());
-                }else{
-                    nowPrice = position.getSellOrderPrice();
-                }
-////                if(state == 1){
-////                    nowPrice = position.getSellOrderPrice();
-////                }else{
-////                    nowPrice = priceServices.getNowPrice(position.getStockCode());
-////                }
-                UserPositionVO userPositionVO = UserPointUtil.assembleUserPositionVO(position,nowPrice);
-                userPositionVO.setOrderTotalPrice(userPositionVO.getOrderTotalPrice().multiply(new BigDecimal(userPositionVO.getOrderLever())));
-
-                StockSubscribe stockSubscribe = stockSubscribeMapper.selectOne(new LambdaQueryWrapper<StockSubscribe>()
-                        .eq(StockSubscribe::getCode, userPositionVO.getStockCode()));
-                if(position.getSellOrderId() == null){
-                    if (null != stockSubscribe && DateUtil.date().before(stockSubscribe.getListDate())) {
-                        userPositionVO.setProfitAndLose(BigDecimal.ZERO);
-                        userPositionVO.setProfitAndLoseParent("0%");
-                        userPositionVO.setIsListed(false);
-                    }else{
-                        userPositionVO.setIsListed(true);
+                    StockSubscribe stockSubscribe = stockSubscribeMapper.selectOne(new LambdaQueryWrapper<StockSubscribe>()
+                            .eq(StockSubscribe::getCode, userPositionVO.getStockCode()));
+                    if (position.getSellOrderId() == null) {
+                        if (null != stockSubscribe && DateUtil.date().before(stockSubscribe.getListDate())) {
+                            userPositionVO.setProfitAndLose(BigDecimal.ZERO);
+                            userPositionVO.setProfitAndLoseParent("0%");
+                            userPositionVO.setIsListed(false);
+                        } else {
+                            userPositionVO.setIsListed(true);
+                            userPositionVO.setProfitAndLose(userPositionVO.getProfitAndLose().multiply(new BigDecimal(userPositionVO.getOrderLever())));
+                        }
+                    } else {
                         userPositionVO.setProfitAndLose(userPositionVO.getProfitAndLose().multiply(new BigDecimal(userPositionVO.getOrderLever())));
                     }
-                }else{
-                    userPositionVO.setProfitAndLose(userPositionVO.getProfitAndLose().multiply(new BigDecimal(userPositionVO.getOrderLever())));
+                    userPositionVOS.add(userPositionVO);
                 }
-                userPositionVOS.add(userPositionVO);
+            }
+
+            PageInfo pageInfo = new PageInfo(userPositions);
+            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);
             }
         }
-
-        PageInfo pageInfo = new PageInfo(userPositions);
-        pageInfo.setList(userPositionVOS);
-
-        return ServerResponse.createBySuccess(pageInfo);
+        wrapper.orderByDesc(UserPosition::getId);
+        return wrapper;
     }
 
     public PositionVO findUserPositionAllProfitAndLose(Integer userId) {
@@ -1282,8 +1332,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("XAUUSD") || position.getStockSpell().equals("USOIL")){
+            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());

--
Gitblit v1.9.3