peter
2026-01-01 a66b2a41f265cc9526781e39c1d6c6d5f5d7c013
src/main/java/com/nq/service/impl/UserPositionServiceImpl.java
@@ -129,6 +129,7 @@
            return ServerResponse.createByErrorMsg("下单失败,请先实名认证");
        }
        BigDecimal user_enable_amt = user.getEnableAmt();
        BigDecimal user_enable_withdraw_amt = user.getEnaleWithdrawAmt();
        log.info("用户 {} 下单,股票id = {} ,数量 = {} , 方向 = {} , 杠杆 = {}", new Object[]{user
                .getId(), stockId, buyNum, buyType, lever});
@@ -185,7 +186,7 @@
            boolean am_flag = BuyAndSellUtils.isTransTime(am_begin, am_end);
            boolean pm_flag = BuyAndSellUtils.isTransTime(pm_begin, pm_end);
            log.info("是否在上午交易时间 = {} 是否在下午交易时间 = {}", Boolean.valueOf(am_flag), Boolean.valueOf(pm_flag));
            //TODO
            if (!am_flag && !pm_flag) {
                return ServerResponse.createByErrorMsg("下单失败,不在交易时段内");
            }
@@ -222,7 +223,6 @@
        }
        BigDecimal now_price;
        StockListVO stockListVO = new StockListVO();
        StockCoin stockCoin = new StockCoin();
        //股票类型 现价 数据源的处理
        stockListVO = SinaStockApi.assembleLideStockListVO(LiDeDataUtils.getStock(stock.getStockCode()));
        if (ObjectUtils.isEmpty(stockListVO)) {
@@ -232,6 +232,7 @@
                stockListVO = SinaStockApi.assembleStockListVO(SinaStockApi.getSinaStock(stock.getStockGid()));
            }
        }
        now_price = new BigDecimal(stockListVO.getNowPrice());
        if (now_price.compareTo(new BigDecimal("0")) == 0) {
@@ -351,7 +352,9 @@
                    .getBuyMinAmt() + "元");
        }
        if(StockType.HK.getCode().equals(stock.getStockType())){
            user_enable_amt = user.getHkAmt();
        }
        BigDecimal max_buy_amt = user_enable_amt.multiply(siteSetting.getBuyMaxAmtPercent());
        int compareCwInt = buy_amt_autual.compareTo(max_buy_amt);
        if (compareCwInt == 1) {
@@ -459,7 +462,7 @@
            //修改用户可用余额= 当前余额-下单金额-买入手续费-印花税-点差费
            //BigDecimal reckon_enable = user_enable_amt.subtract(buy_amt_autual).subtract(buy_fee_amt).subtract(buy_yhs_amt).subtract(spread_rate_amt);
            //修改用户可用余额= 当前余额-下单总金额
            BigDecimal reckon_enable = user_enable_amt.subtract(buy_amt_autual);
            BigDecimal reckon_enable = user_enable_amt.subtract(buy_amt_autual);;
            //修改用户可取余额=当前可取余额-下单总金额
            int compareUserWithdrawAmtInt = user_enable_withdraw_amt.compareTo(buy_amt_autual);
            if (compareUserWithdrawAmtInt == -1) {
@@ -469,7 +472,12 @@
                user_enable_withdraw_amt = user_enable_withdraw_amt.subtract(buy_amt_autual);
                user.setEnaleWithdrawAmt(user_enable_withdraw_amt);
            }
            user.setEnableAmt(reckon_enable);
            if(StockType.HK.getCode().equals(stock.getStockType())){
                user.setHkAmt(reckon_enable);
            }else {
                user.setEnableAmt(reckon_enable);
            }
            int updateUserCount = this.userMapper.updateByPrimaryKeySelective(user);
            if (updateUserCount > 0) {
                log.info("【用户交易下单】修改用户金额成功");
@@ -575,6 +583,7 @@
                boolean am_flag = BuyAndSellUtils.isTransTime(am_begin, am_end);
                boolean pm_flag = BuyAndSellUtils.isTransTime(pm_begin, pm_end);
                log.info("是否在上午交易時間 = {} 是否在下午交易時間 = {}", Boolean.valueOf(am_flag), Boolean.valueOf(pm_flag));
                //TODO
                if (!am_flag && !pm_flag) {
                    return ServerResponse.createByErrorMsg("平仓失败,不在交易时段内");
                }
@@ -682,7 +691,12 @@
        }
        log.info("買入總金額 = {} , 賣出總金額 = {} , 盈虧 = {}", new Object[]{all_buy_amt, all_sell_amt, profitLoss});
        BigDecimal user_all_amt = user.getUserAmt();
        BigDecimal user_all_amt = BigDecimal.ZERO;
        if(StockType.HK.getCode().equals(userPosition.getStockType())){
            user_all_amt = user.getHkAmt();
        }else {
            user_all_amt = user.getUserAmt();
        }
        BigDecimal user_enable_amt = user.getEnableAmt();
        log.info("用戶原本總資金 = {} , 可用 = {}", user_all_amt, user_enable_amt);
@@ -727,14 +741,17 @@
        BigDecimal freez_amt = all_buy_amt.divide(new BigDecimal(userPosition.getOrderLever().intValue()), 2, 4);
        //BigDecimal freez_amt = all_buy_amt;
        BigDecimal reckon_all = user_all_amt.add(all_profit);
        //修改用戶可用余額=當前可用余額+總盈虧+買入總金額+追加保證金
        BigDecimal reckon_enable = user_enable_amt.add(all_profit).add(freez_amt).add(userPosition.getMarginAdd());
        log.info("用戶平倉後的總資金  = {} , 可用資金 = {}", reckon_all, reckon_enable);
        user.setUserAmt(reckon_all);
        user.setEnableAmt(reckon_enable);
        if(StockType.HK.getCode().equals(userPosition.getStockType())){
            user.setHkAmt(reckon_enable);
        }else {
            user.setUserAmt(reckon_all);
            user.setEnableAmt(reckon_enable);
        }
        int updateUserCount = this.userMapper.updateByPrimaryKeySelective(user);
        if (updateUserCount > 0) {
            log.info("【用戶平倉】修改用戶金額成功");
@@ -1171,62 +1188,67 @@
        BigDecimal allProfitAndLose = new BigDecimal("0");
        BigDecimal allFreezAmt = new BigDecimal("0");
        BigDecimal allProfitAndLoseHk = new BigDecimal("0");
        BigDecimal allFreezAmtHk = new BigDecimal("0");
        for (UserPosition position : userPositions) {
            StockListVO stockListVO = SinaStockApi.assembleLideStockListVO(LiDeDataUtils.getStock(position.getStockCode()));
            if (ObjectUtils.isEmpty(stockListVO)) {
                if(StockType.HK.getCode().equals(position.getStockType())){
                    stockListVO = SinaStockApi.assembleStockListVOHk(SinaStockApi.getSinaStock(position.getStockType()+position.getStockGid()));
                    if (stockListVO.getNowPrice() == null || new BigDecimal(stockListVO.getNowPrice()).compareTo(BigDecimal.ZERO) <= 0) {
                        stockListVO.setNowPrice(String.valueOf(position.getBuyOrderIdIndex()));
                    }
                    BigDecimal nowPrice = new BigDecimal(stockListVO.getNowPrice());
                    if (nowPrice.compareTo(new BigDecimal("0")) != 0) {
                        BigDecimal buyPrice = position.getBuyOrderPrice();
                        BigDecimal subPrice = nowPrice.subtract(buyPrice);
                        BigDecimal profit_and_lose = subPrice.multiply(new BigDecimal(position.getOrderNum().intValue()));
                        if ("买跌".equals(position.getOrderDirection())) {
                            profit_and_lose = profit_and_lose.negate();
                        }
                        BigDecimal total_fee = position.getOrderFee().add(position.getOrderSpread()).add(position.getOrderStayFee());
                        BigDecimal position_profit = profit_and_lose.subtract(total_fee);
                        allProfitAndLoseHk = allProfitAndLoseHk.add(position_profit);
                        BigDecimal position_freez = position.getOrderTotalPrice().divide(new BigDecimal(position.getOrderLever().intValue()), 2, 4);
                        allFreezAmtHk = allFreezAmtHk.add(position_freez).add(position.getMarginAdd());
                        continue;
                    }
                    log.info("查询所有持仓单的总盈亏,现价返回0,当前为集合竞价");
                }else {
                    stockListVO = SinaStockApi.assembleStockListVO(SinaStockApi.getSinaStock(position.getStockGid()));
                    if (stockListVO.getNowPrice() == null || new BigDecimal(stockListVO.getNowPrice()).compareTo(BigDecimal.ZERO) <= 0) {
                        stockListVO.setNowPrice(String.valueOf(position.getBuyOrderIdIndex()));
                    }
                    BigDecimal nowPrice = new BigDecimal(stockListVO.getNowPrice());
                    if (nowPrice.compareTo(new BigDecimal("0")) != 0) {
                        BigDecimal buyPrice = position.getBuyOrderPrice();
                        BigDecimal subPrice = nowPrice.subtract(buyPrice);
                        BigDecimal profit_and_lose = subPrice.multiply(new BigDecimal(position.getOrderNum().intValue()));
                        if ("买跌".equals(position.getOrderDirection())) {
                            profit_and_lose = profit_and_lose.negate();
                        }
                        BigDecimal total_fee = position.getOrderFee().add(position.getOrderSpread()).add(position.getOrderStayFee());
                        BigDecimal position_profit = profit_and_lose.subtract(total_fee);
                        allProfitAndLose = allProfitAndLose.add(position_profit);
                        BigDecimal position_freez = position.getOrderTotalPrice().divide(new BigDecimal(position.getOrderLever().intValue()), 2, 4);
                        allFreezAmt = allFreezAmt.add(position_freez).add(position.getMarginAdd());
                        continue;
                    }
                    log.info("查询所有持仓单的总盈亏,现价返回0,当前为集合竞价");
                }
            }
            if (stockListVO.getNowPrice() == null || new BigDecimal(stockListVO.getNowPrice()).compareTo(BigDecimal.ZERO) <= 0) {
                stockListVO.setNowPrice(String.valueOf(position.getBuyOrderIdIndex()));
            }
            BigDecimal nowPrice = new BigDecimal(stockListVO.getNowPrice());
            if (nowPrice.compareTo(new BigDecimal("0")) != 0) {
                BigDecimal buyPrice = position.getBuyOrderPrice();
                BigDecimal subPrice = nowPrice.subtract(buyPrice);
                BigDecimal profit_and_lose = subPrice.multiply(new BigDecimal(position.getOrderNum().intValue()));
                if ("买跌".equals(position.getOrderDirection())) {
                    profit_and_lose = profit_and_lose.negate();
                }
                BigDecimal total_fee = position.getOrderFee().add(position.getOrderSpread()).add(position.getOrderStayFee());
                BigDecimal position_profit = profit_and_lose.subtract(total_fee);
                allProfitAndLose = allProfitAndLose.add(position_profit);
                BigDecimal position_freez = position.getOrderTotalPrice().divide(new BigDecimal(position.getOrderLever().intValue()), 2, 4);
                allFreezAmt = allFreezAmt.add(position_freez).add(position.getMarginAdd());
                continue;
            }
            log.info("查询所有持仓单的总盈亏,现价返回0,当前为集合竞价");
        }
        //加上分仓交易保证金
        List<FundsApply> fundsApplyList = fundsApplyMapper.getUserMarginList(userId);
        for (FundsApply fundsApply : fundsApplyList) {
            allFreezAmt = allFreezAmt.add(fundsApply.getMargin());
        }
//                    List<FundsApply> fundsApplyList = fundsApplyMapper.getUserMarginList(userId);
//                    for (FundsApply fundsApply : fundsApplyList) {
//                        allFreezAmt = allFreezAmt.add(fundsApply.getMargin());
//                    }
        PositionVO positionVO = new PositionVO();
        positionVO.setAllProfitAndLose(allProfitAndLose);
        positionVO.setAllFreezAmt(allFreezAmt);
        positionVO.setAllProfitAndLoseHk(allProfitAndLoseHk);
        positionVO.setAllFreezAmtHk(allFreezAmtHk);
        return positionVO;
    }
@@ -1850,14 +1872,17 @@
            BigDecimal subPrice = (new BigDecimal(nowPrice)).subtract(position.getBuyOrderPrice());
//            profitAndLose = subPrice.multiply(new BigDecimal(position.getOrderNum().intValue())).multiply(new BigDecimal(position.getOrderLever())).setScale(2,4);
            profitAndLose = subPrice.multiply(new BigDecimal(position.getOrderNum().intValue())).setScale(2,4);
            if ("买跌".equals(position.getOrderDirection())) {
                profitAndLose = profitAndLose.negate();
            }
            //总盈亏= 浮动盈亏 – 手续费 – 印花税 – 留仓费 – 点差费
            allProfitAndLose = profitAndLose.subtract(position.getOrderFee()).subtract(position.getOrderSpread()).subtract(position.getOrderStayFee()).subtract(position.getSpreadRatePrice());
            //改成盈亏百分比
            BigDecimal allProfitAndLoseStrTemp = new BigDecimal(nowPrice).subtract(position.getBuyOrderPrice()).divide(position.getBuyOrderPrice(), 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)).setScale(2,4);
            allProfitAndLoseStr = allProfitAndLoseStrTemp.toString() + "%";
            if ("买跌".equals(position.getOrderDirection())) {
                profitAndLose = profitAndLose.negate();
                allProfitAndLoseStr = allProfitAndLoseStrTemp.negate() + "%";
            }
            //总盈亏= 浮动盈亏 – 手续费 – 印花税 – 留仓费 – 点差费
            allProfitAndLose = profitAndLose.subtract(position.getOrderFee()).subtract(position.getOrderSpread()).subtract(position.getOrderStayFee()).subtract(position.getSpreadRatePrice());
        }
        PositionProfitVO positionProfitVO = new PositionProfitVO();
        positionProfitVO.setProfitAndLose(profitAndLose);