From 68b9175323a2f9b40ffcc48bb01af1d8279e5d01 Mon Sep 17 00:00:00 2001
From: zyy <zyy@email.com>
Date: Fri, 22 Aug 2025 10:21:51 +0800
Subject: [PATCH] 平仓返回保证金修改

---
 src/main/java/com/nq/service/impl/UserPositionServiceImpl.java |  233 +++++++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 156 insertions(+), 77 deletions(-)

diff --git a/src/main/java/com/nq/service/impl/UserPositionServiceImpl.java b/src/main/java/com/nq/service/impl/UserPositionServiceImpl.java
index 1be8727..6224d60 100644
--- a/src/main/java/com/nq/service/impl/UserPositionServiceImpl.java
+++ b/src/main/java/com/nq/service/impl/UserPositionServiceImpl.java
@@ -88,14 +88,10 @@
     @Autowired
     StockMapper stockMapper;
     @Autowired
-    AgentAgencyFeeMapper agentAgencyFeeMapper;
-    @Autowired
     IAgentAgencyFeeService iAgentAgencyFeeService;
     @Autowired
     ISiteProductService iSiteProductService;
 
-    @Autowired
-    FundsApplyMapper fundsApplyMapper;
     @Autowired
     UserStockSubscribeMapper userStockSubscribeMapper;
     @Resource
@@ -103,12 +99,6 @@
     @Resource
     UserIndexPositionMapper userIndexPositionMapper;
 
-    @Autowired
-    IStockFuturesService iStockFuturesService;
-    @Autowired
-    IStockCoinService iStockCoinService;
-    @Autowired
-    CurrencyUtils currencyUtils;
     @Resource
     StockDzMapper stockDzMapper;
 
@@ -187,12 +177,18 @@
                     return ServerResponse.createByErrorMsg("报价0,请稍后再试", request);
                 }
 
-                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 buyAmt = nowPrice.multiply(new BigDecimal(buyNum)).divide(new BigDecimal(lever));
+                //手续费
+                BigDecimal orderFree = siteSettingBuyFee.multiply(buyAmt);
+                BigDecimal needBuyAmt = buyAmt.add(orderFree);
+                //资金校验
+                if (!stock.getStockType().equals(EStockType.MX.getCode())) {
+                    needBuyAmt = userAssetsServices.exchangeAmountByRate(stock.getStockType(), buyAmt);
+                }
+                BigDecimal fundratio = new BigDecimal(user.getFundRatio()).divide(new BigDecimal(100));
                 BigDecimal availableBalance =  fundratio.multiply(userAssets.getAvailableBalance());
-                if (availableBalance.compareTo(buyAmt.add(orderFree)) < 0) {
+                if (availableBalance.compareTo(needBuyAmt) < 0) {
                     return ServerResponse.createByErrorMsg("订单失败,配资不足", request);
                 }
                 UserPosition userPosition = new UserPosition();
@@ -690,6 +686,71 @@
         return ServerResponse.createBySuccess(pageInfo);
     }
 
+    public ServerResponse findMyPositionByParam(String stockCode, String stockSpell,
+                                                       Integer state, HttpServletRequest request,
+                                                       int pageNum, int pageSize, String stockType, Integer positionType) {
+        try {
+            User user = iUserService.getCurrentUser(request);
+            if (user == null ){
+                return ServerResponse.createByErrorCodeMsg(ResponseCode.NEED_LOGIN.getCode(),"请先登录", request);
+            }
+
+            PageHelper.startPage(pageNum, pageSize);
+            List<UserPositionVO> userPositions = userPositionMapper.findMyPositionByParam(user.getId(),
+                            stockCode, stockSpell, state, stockType, positionType);
+            PageInfo<UserPositionVO> pageInfo = new PageInfo<>(userPositions);
+
+            List<UserPositionVO> resultUserPositions = new ArrayList<>();
+            if (!pageInfo.getList().isEmpty()) {
+                for (UserPositionVO position : userPositions) {
+                    UserPositionVO userPositionVO = position;
+                    if (position.getPositionType() != 4) {
+                        BigDecimal nowPrice;
+                        if(state == 0){
+                            nowPrice = priceServices.getNowPrice(position.getStockCode());
+                        }else{
+                            nowPrice = position.getSellOrderPrice();
+                        }
+
+                        userPositionVO = UserPointUtil.assembleUserPositionVO2(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);
+                                userPositionVO.setProfitAndLose(userPositionVO.getProfitAndLose().multiply(new BigDecimal(userPositionVO.getOrderLever())));
+                            }
+                        }else{
+                            userPositionVO.setProfitAndLose(userPositionVO.getProfitAndLose().multiply(new BigDecimal(userPositionVO.getOrderLever())));
+                        }
+                    } else {
+                        //ai交易 计算收益率 收益/本金*100
+                        BigDecimal profitAndLose = userPositionVO.getProfitAndLose()
+                                .divide(userPositionVO.getOrderTotalPrice(), RoundingMode.HALF_UP)
+                                .multiply(new BigDecimal(100))
+                                .setScale(2, RoundingMode.DOWN);
+                        userPositionVO.setNow_price(String.valueOf(userPositionVO.getSellOrderPrice().setScale(2, RoundingMode.HALF_UP)));
+                        userPositionVO.setProfitAndLoseParent(profitAndLose + "%");
+                        userPositionVO.setSellOrderTotalPrice(userPositionVO.getSellOrderPrice().multiply(BigDecimal.valueOf(userPositionVO.getOrderNum())));
+                    }
+                    resultUserPositions.add(userPositionVO);
+                }
+            }
+            pageInfo.setList(resultUserPositions);
+            return ServerResponse.createBySuccess(pageInfo);
+        } catch (Exception e) {
+            e.printStackTrace();
+            log.error("IUserPositionService findMyPositionByParam  {}", e.getMessage());
+        }
+        return ServerResponse.createByError();
+    }
+
     public PositionVO findUserPositionAllProfitAndLose(Integer userId) {
         List<UserPosition> userPositions = this.userPositionMapper.findPositionByUserIdAndSellIdIsNull(userId);
 
@@ -958,7 +1019,7 @@
         userPosition.setAgentId(user.getAgentId());
         userPosition.setStockCode(stock.getStockCode());
         userPosition.setStockName(stock.getStockName());
-        userPosition.setStockGid(stock.getStockGid());
+        userPosition.setStockGid(stock.getStockType());
         userPosition.setStockSpell(stock.getStockSpell());
         userPosition.setBuyOrderId(GeneratePosition.getPositionId());
         userPosition.setBuyOrderTime(DateTimeUtil.strToDate(buyTime));
@@ -1341,7 +1402,7 @@
                 stockType = stock.getStockType();
             }
 
-            userPosition.setPositionType(1);
+            userPosition.setPositionType(2);
             userPosition.setPositionSn(KeyUtils.getUniqueKey());
             userPosition.setUserId(userStockSubscribe.getUserId());
             userPosition.setNickName(userStockSubscribe.getRealName());
@@ -1599,7 +1660,7 @@
         userPosition.setAgentId(user.getAgentId());
         userPosition.setStockCode(stock.getStockCode());
         userPosition.setStockName(stock.getStockName());
-        userPosition.setStockGid(stock.getStockGid());
+        userPosition.setStockGid(stock.getStockType());
         userPosition.setStockSpell(stock.getStockSpell());
         userPosition.setBuyOrderId(GeneratePosition.getPositionId());
         userPosition.setBuyOrderTime(new Date());
@@ -1696,71 +1757,86 @@
      * @return
      */
     @Transactional(rollbackFor = Exception.class)
-    public ServerResponse buyDz(Integer dzId, String password, Integer num, HttpServletRequest request) throws Exception {
-        /*实名认证开关开启*/
-        SiteProduct siteProduct = iSiteProductService.getProductSetting();
-        User user = this.iUserService.getCurrentRefreshUser(request);
+    public ServerResponse buyDz(Integer dzId, String password, Integer num, HttpServletRequest request){
+        try {
+            /*实名认证开关开启*/
+            SiteProduct siteProduct = iSiteProductService.getProductSetting();
+            User user = this.iUserService.getCurrentRefreshUser(request);
 
-        if (siteProduct.getRealNameDisplay() && user.getIsActive() != 2) {
-            return ServerResponse.createByErrorMsg("Order failed, please first real name authentication");
-        }
-        if (siteProduct.getRealNameDisplay() && user.getIsLock().intValue() == 1) {
-            return ServerResponse.createByErrorMsg("Order failed, account has been locked");
-        }
-        StockDz stockDz = this.stockDzMapper.selectOne(new QueryWrapper<StockDz>().eq("id", dzId));
-        if (StringUtils.isNotEmpty(stockDz.getPassword()) && !Objects.equals(stockDz.getPassword(), password)) {
-            return ServerResponse.createByErrorMsg("密码错误", request);
-        }
-        if (stockDz.getIsLock() != 0) {
-            return ServerResponse.createByErrorMsg("股票被锁定,不能购买", request);
-        }
-        //价格处理
-        Stock stock = stockMapper.selectOne(new QueryWrapper<Stock>().eq("stock_code", stockDz.getStockCode()));
+            if (siteProduct.getRealNameDisplay() && user.getIsActive() != 2) {
+                return ServerResponse.createByErrorMsg("Order failed, please first real name authentication");
+            }
+            if (siteProduct.getRealNameDisplay() && user.getIsLock().intValue() == 1) {
+                return ServerResponse.createByErrorMsg("Order failed, account has been locked");
+            }
+            StockDz stockDz = this.stockDzMapper.selectOne(new QueryWrapper<StockDz>().eq("id", dzId));
+            if (StringUtils.isNotEmpty(stockDz.getPassword()) && !Objects.equals(stockDz.getPassword(), password)) {
+                return ServerResponse.createByErrorMsg("密码错误", request);
+            }
+            if (stockDz.getIsLock() != 0) {
+                return ServerResponse.createByErrorMsg("股票被锁定,不能购买", request);
+            }
+            //价格处理
+            Stock stock = stockMapper.selectOne(new QueryWrapper<Stock>().eq("stock_code", stockDz.getStockCode()));
 
-        UserAssets userAssets = userAssetsServices.assetsByTypeAndUserId(stock.getStockType(), user.getId());
-        if(userAssets.getAmountToBeCovered().compareTo(BigDecimal.ZERO) > 0){
-            return ServerResponse.createByErrorMsg("请先缴清待补资金", request);
-        }
+            UserAssets userAssets = userAssetsServices.assetsByTypeAndUserId(stock.getStockType(), user.getId());
+            if(userAssets.getAmountToBeCovered().compareTo(BigDecimal.ZERO) > 0){
+                return ServerResponse.createByErrorMsg("请先缴清待补资金", request);
+            }
 
-        if(stockDz.getStartTime().getTime() > new Date().getTime() || stockDz.getEndTime().getTime() < new Date().getTime()){
-            return ServerResponse.createByErrorMsg("不在内幕交易时间之内", request);
-        }
+            if(stockDz.getStartTime().getTime() > new Date().getTime() || stockDz.getEndTime().getTime() < new Date().getTime()){
+                return ServerResponse.createByErrorMsg("不在内幕交易时间之内", request);
+            }
 //        BigDecimal nowPrice = priceServices.getNowPrice(stockDz.getStockCode()).multiply(stockDz.getDiscount());
-        BigDecimal nowPrice = stockDz.getNowPrice();
+            BigDecimal nowPrice = stockDz.getNowPrice();
 
-        if (nowPrice.compareTo(new BigDecimal("0")) == 0) {
-            return ServerResponse.createByErrorMsg("股票价格0,请重试", request);
-        }
-        if (stockDz.getStockNum() > num) {
-            return ServerResponse.createByErrorMsg("最小购买数据" + stockDz.getStockNum(), request);
-        }
+            if (nowPrice.compareTo(new BigDecimal("0")) == 0) {
+                return ServerResponse.createByErrorMsg("股票价格0,请重试", request);
+            }
+            if (stockDz.getStockNum() > num) {
+                return ServerResponse.createByErrorMsg("最小购买数据" + stockDz.getStockNum(), request);
+            }
 
-        BigDecimal siteSettingBuyFee = new BigDecimal(iStockConfigServices.queryByKey(EConfigKey.BUY_HANDLING_CHARGE.getCode()).getCValue()) ;
+            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 (availableBalance.compareTo(buyAmt.add(orderFree)) < 0) {
-            return ServerResponse.createByErrorMsg("订单失败,配资不足", request);
-        }
+            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 (availableBalance.compareTo(buyAmt.add(orderFree)) < 0) {
+                return ServerResponse.createByErrorMsg("订单失败,配资不足", request);
+            }
 
-        //判断审核开关
-        if(stockDz.getSwitchType() == 1) {
+            BigDecimal newBuyAmt = buyAmt;
+            //如果不是墨西哥币需要转换金额
+            if (!stock.getStockType().equals(EStockType.MX.getCode())) {
+                newBuyAmt = userAssetsServices.exchangeAmountByRate(stock.getStockType(), buyAmt);
+            }
+            if(newBuyAmt.compareTo(userAssets.getAvailableBalance()) > 0){
+                return ServerResponse.createByErrorMsg("可用余额不足" + userAssets.getAvailableBalance(), request);
+            }
+
+            //判断审核开关
+            if(stockDz.getSwitchType() == 1) {
+                UserPosition userPosition = getUserPosition(dzId,num, user, stockDz, nowPrice, stock, buyAmt);
+                UserPositionCheckDz userPositionCheckDz = Convert.convert(UserPositionCheckDz.class, userPosition);
+                userPositionCheckDz.setDzId(dzId);
+                userPositionCheckDzService.save(userPositionCheckDz);
+                return ServerResponse.createBySuccess("购买成功,等待审核", request);
+            }
+
+            // 创建UserPosition对象
             UserPosition userPosition = getUserPosition(dzId,num, user, stockDz, nowPrice, stock, buyAmt);
-            UserPositionCheckDz userPositionCheckDz = Convert.convert(UserPositionCheckDz.class, userPosition);
-            userPositionCheckDz.setDzId(dzId);
-            userPositionCheckDzService.save(userPositionCheckDz);
-            return ServerResponse.createBySuccess("购买成功,等待审核", request);
+            userPositionMapper.insert(userPosition);
+            BigDecimal buy_fee_amt = siteSettingBuyFee.multiply(newBuyAmt);
+            //已经转化 直接穿MEX类型
+            userAssetsServices.availablebalanceChange(EStockType.MX.getCode(), user.getId(), EUserAssets.BUY, newBuyAmt.negate(),"","");
+            iUserAssetsServices.availablebalanceChange(EStockType.MX.getCode(), userAssets.getUserId(), EUserAssets.HANDLING_CHARGE, buy_fee_amt, "", "");
+            return ServerResponse.createBySuccess("购买成功", request);
+        } catch (Exception e) {
+            log.error("大宗下单异常{}", e.getMessage());
         }
-
-        // 创建UserPosition对象
-        UserPosition userPosition = getUserPosition(dzId,num, user, stockDz, nowPrice, stock, buyAmt);
-        userPositionMapper.insert(userPosition);
-        BigDecimal buy_fee_amt = siteSettingBuyFee.multiply(buyAmt);
-        userAssetsServices.availablebalanceChange(stock.getStockType(), user.getId(), EUserAssets.BUY, buyAmt.negate(),"","");
-        iUserAssetsServices.availablebalanceChange(stock.getStockType(), userAssets.getUserId(), EUserAssets.HANDLING_CHARGE, buy_fee_amt, "", "");
-        return ServerResponse.createBySuccess("购买成功", request);
+        return ServerResponse.createByError();
     }
 
     private UserPosition getUserPosition(Integer dzId,Integer num, User user, StockDz stockDz, BigDecimal nowPrice, Stock stock, BigDecimal buyAmt) {
@@ -2042,14 +2118,14 @@
             BigDecimal usPositionEarningsParent = BigDecimal.ZERO; //美股持仓收益百分比
             BigDecimal mxPositionEarningsParent = BigDecimal.ZERO; //墨西哥持仓收益百分比
 
-            if (userPositions.size() > 0) {
+            if (!userPositions.isEmpty()) {
                 for (UserPosition position : userPositions) {
-                    BigDecimal nowPrice;
-                    if(state == 0){
+                    BigDecimal nowPrice = priceServices.getNowPrice(position.getStockCode());
+                    /*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())));
 
@@ -2112,6 +2188,9 @@
                 userPositionCheckDzService.updateById(userPositionCheckDz);
                 return ServerResponse.createBySuccess("审核成功", request);
             }
+            if (orderNum > userPositionCheckDz.getOrderNum()) {
+                return ServerResponse.createByErrorMsg("输入数量大于用户买入数量", request);
+            }
             User user = userMapper.selectById(userPositionCheckDz.getUserId());
 
             StockDz stockDz = this.stockDzMapper.selectOne(new QueryWrapper<StockDz>().eq("id", userPositionCheckDz.getDzId()));
@@ -2141,7 +2220,7 @@
             userPosition.setId(null);
             userPosition.setDzId(stockDz.getId());
             userPositionMapper.insert(userPosition);
-            userAssetsServices.availablebalanceChange(userAssets.getAccectType(), user.getId(), EUserAssets.BUY, buyAmt.negate(), "", "");
+            userAssetsServices.availablebalanceChange(stockDz.getStockType(), user.getId(), EUserAssets.BUY, buyAmt.negate(), "", "");
             return ServerResponse.createBySuccessMsg("审核成功,订单已转客户持仓", request);
         } catch (Exception e) {
             return ServerResponse.createByErrorMsg(e.getMessage());

--
Gitblit v1.9.3