peter
2025-12-30 0c74d36f29e8279d9061d57466b3f488a4e1b89f
src/main/java/com/nq/service/impl/UserPositionServiceImpl.java
@@ -2,6 +2,7 @@
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.Gson;
import com.nq.config.StockType;
import com.nq.dao.*;
import com.nq.pojo.*;
import com.nq.service.*;
@@ -169,7 +170,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("下单失败,不在港股股交易时段内");
            }
@@ -225,40 +226,13 @@
        //股票类型 现价 数据源的处理
        stockListVO = SinaStockApi.assembleLideStockListVO(LiDeDataUtils.getStock(stock.getStockCode()));
        if (ObjectUtils.isEmpty(stockListVO)) {
            stockListVO = SinaStockApi.assembleStockListVO(SinaStockApi.getSinaStock(stock.getStockGid()));
            if(StockType.HK.getCode().equals(stock.getStockType())){
                stockListVO = SinaStockApi.assembleStockListVOHk(SinaStockApi.getSinaStock(StockType.HK.getCode()+stock.getStockGid()));
            }else {
                stockListVO = SinaStockApi.assembleStockListVO(SinaStockApi.getSinaStock(stock.getStockGid()));
            }
        }
        now_price = new BigDecimal(stockListVO.getNowPrice());
//        if (stock.getDataBase()!=0){
//             String date = getCurrentTimeMiaoZero();
//            String result = HttpClientRequest.doGet(PropertiesUtil.getProperty("changePrice.url")+"?cat_time="+date+"&stock_gid="+stock.getStockGid()+"&price="+stockListVO.getNowPrice());
//            JSONObject jsonObject = JSONObject.fromObject(result);
//            String nowPrice = jsonObject.getJSONObject("data").getString("new_price");
//            if (nowPrice!=null){
//                if ("us".equals(stock.getStockType())){
//                    ExchangeVO exchangeVO = this.iStockFuturesService.queryExchangeVO("USD").getData();
//                    now_price =new BigDecimal(nowPrice).multiply(new BigDecimal(exchangeVO.getNowPrice()));
//                } else if ("hk".equals(stock.getStockType())){
//                    ExchangeVO exchangeVO = this.iStockFuturesService.queryExchangeVO("HKD").getData();
//                    now_price =new BigDecimal(nowPrice).multiply(new BigDecimal(exchangeVO.getNowPrice()));
//                } else {
//                    now_price = new BigDecimal(nowPrice);
//                }
//
//            }else {
//                stockListVO.setNowPrice(stockListVO.getNowPrice());
//                if ("us".equals(stock.getStockType())){
//                    ExchangeVO exchangeVO = this.iStockFuturesService.queryExchangeVO("USD").getData();
//                    now_price =new BigDecimal(nowPrice).multiply(new BigDecimal(exchangeVO.getNowPrice()));
//                } else if ("hk".equals(stock.getStockType())){
//                    ExchangeVO exchangeVO = this.iStockFuturesService.queryExchangeVO("HKD").getData();
//                    now_price =new BigDecimal(nowPrice).multiply(new BigDecimal(exchangeVO.getNowPrice()));
//                } else {
//                    now_price = new BigDecimal(nowPrice);
//                }
//            }
//        }
//            String stockOther = RedisShardedPoolUtils.get(stock.getStockName(), 8);
        if (now_price.compareTo(new BigDecimal("0")) == 0) {
            return ServerResponse.createByErrorMsg("报价0,请稍后再试");
@@ -341,8 +315,14 @@
        }
        ServerResponse serverResponse = this.iStockService.selectRateByDaysAndStockCode(stock
                .getStockCode(), siteSetting.getStockDays().intValue());
        ServerResponse serverResponse = null;
        if(StockType.HK.getCode().equals(stock.getStockType())){
            serverResponse = this.iStockService.selectRateByDaysAndStockCode(stock
                    .getStockGid(), siteSetting.getStockDays().intValue());
        }else {
            serverResponse = this.iStockService.selectRateByDaysAndStockCode(stock
                    .getStockCode(), siteSetting.getStockDays().intValue());
        }
        if (!serverResponse.isSuccess()) {
            return serverResponse;
        }
@@ -416,7 +396,7 @@
        userPosition.setBuyOrderTime(new Date());
        userPosition.setBuyOrderPrice(now_price);
        userPosition.setOrderDirection((buyType.intValue() == 0) ? "买涨" : "买跌");
        userPosition.setStockType(stock.getStockType());
        userPosition.setOrderNum(buyNum);
@@ -646,39 +626,13 @@
        //股票賣出的 價格 數據源
        stockListVO = SinaStockApi.assembleLideStockListVO(LiDeDataUtils.getStock(userPosition.getStockCode()));
        if (ObjectUtils.isEmpty(stockListVO)) {
            stockListVO = SinaStockApi.assembleStockListVO(SinaStockApi.getSinaStock(userPosition.getStockGid()));
            if(StockType.HK.getCode().equals(userPosition.getStockType())){
                stockListVO = SinaStockApi.assembleStockListVOHk(SinaStockApi.getSinaStock(StockType.HK.getCode()+userPosition.getStockGid()));
            }else {
                stockListVO = SinaStockApi.assembleStockListVO(SinaStockApi.getSinaStock(userPosition.getStockGid()));
            }
//            stockListVO = SinaStockApi.assembleStockListVO(SinaStockApi.getSinaStock(userPosition.getStockGid()));
        }
        //stockListVO = SinaStockApi.assembleStockListVO(SinaStockApi.getSinaStock(userPosition.getStockGid()));
//            if (stock.getDataBase() != 0) {
//                  String date = getCurrentTimeMiaoZero();
//                String result = HttpClientRequest.doGet(PropertiesUtil.getProperty("changePrice.url")+"?cat_time="+date+"&stock_gid="+stock.getStockGid()+"&price="+stockListVO.getNowPrice());
//                JSONObject jsonObject = JSONObject.fromObject(result);
//                String nowPrice1 = jsonObject.getJSONObject("data").getString("new_price");
//                if (nowPrice1!=null){
//                    if ("us".equals(stock.getStockType())){
//                        ExchangeVO exchangeVO = this.iStockFuturesService.queryExchangeVO("USD").getData();
//                        now_price = new BigDecimal(nowPrice1).multiply(new BigDecimal(exchangeVO.getNowPrice()));
//                    } else if ("hk".equals(stock.getStockType())){
//                        ExchangeVO exchangeVO = this.iStockFuturesService.queryExchangeVO("HKD").getData();
//                        now_price = new BigDecimal(nowPrice1).multiply(new BigDecimal(exchangeVO.getNowPrice()));
//                    } else {
//                        now_price = new BigDecimal(nowPrice1);
//                    }
//
//                }else {
//                    stockListVO.setNowPrice(stockListVO.getNowPrice());
//                    if ("us".equals(stock.getStockType())){
//                        ExchangeVO exchangeVO = this.iStockFuturesService.queryExchangeVO("USD").getData();
//                        now_price = new BigDecimal(stockListVO.getNowPrice()).multiply(new BigDecimal(exchangeVO.getNowPrice()));
//                    } else if ("hk".equals(stock.getStockType())){
//                        ExchangeVO exchangeVO = this.iStockFuturesService.queryExchangeVO("HKD").getData();
//                        now_price = new BigDecimal(stockListVO.getNowPrice()).multiply(new BigDecimal(exchangeVO.getNowPrice()));
//                    } else {
//                        now_price = new BigDecimal(stockListVO.getNowPrice());
//                    }
//                }
//
//        }
        if (stockListVO.getNowPrice() == null) {
            return ServerResponse.createByErrorMsg("平仓失败,获取股票信息失败");
@@ -1221,11 +1175,14 @@
            StockListVO stockListVO = SinaStockApi.assembleLideStockListVO(LiDeDataUtils.getStock(position.getStockCode()));
            if (ObjectUtils.isEmpty(stockListVO)) {
                stockListVO = SinaStockApi.assembleStockListVO(SinaStockApi.getSinaStock(position.getStockGid()));
                if(StockType.HK.getCode().equals(position.getStockType())){
                    stockListVO = SinaStockApi.assembleStockListVOHk(SinaStockApi.getSinaStock(position.getStockType()+position.getStockGid()));
                }else {
                    stockListVO = SinaStockApi.assembleStockListVO(SinaStockApi.getSinaStock(position.getStockGid()));
                }
            }
            if (stockListVO.getNowPrice() == null || new BigDecimal(stockListVO.getNowPrice()).compareTo(BigDecimal.ZERO) <= 0) {
//                stockListVO.setNowPrice("0");
                stockListVO.setNowPrice(String.valueOf(position.getBuyOrderIdIndex()));
            }
@@ -1239,7 +1196,6 @@
                BigDecimal subPrice = nowPrice.subtract(buyPrice);
                BigDecimal profit_and_lose = subPrice.multiply(new BigDecimal(position.getOrderNum().intValue()));
                //BigDecimal profit_and_lose = subPrice.multiply(new BigDecimal(position.getOrderNum().intValue())).divide(new BigDecimal(position.getOrderLever())).setScale(2,4);
                if ("买跌".equals(position.getOrderDirection())) {
                    profit_and_lose = profit_and_lose.negate();
                }
@@ -1255,7 +1211,6 @@
                BigDecimal position_freez = position.getOrderTotalPrice().divide(new BigDecimal(position.getOrderLever().intValue()), 2, 4);
                //BigDecimal position_freez = position.getOrderTotalPrice();
                allFreezAmt = allFreezAmt.add(position_freez).add(position.getMarginAdd());
                continue;
            }
@@ -1873,12 +1828,14 @@
            allProfitAndLose = profitAndLose.subtract(position.getOrderFee()).subtract(position.getOrderSpread()).subtract(position.getOrderStayFee()).subtract(position.getSpreadRatePrice());
        } else {
            StockListVO stockListVO = new StockListVO();
            StockCoin stockCoin = new StockCoin();
            stockListVO = SinaStockApi.assembleLideStockListVO(LiDeDataUtils.getStock(position.getStockCode()));
            if (ObjectUtils.isEmpty(stockListVO)) {
                stockListVO = SinaStockApi.assembleStockListVO(SinaStockApi.getSinaStock(position.getStockGid()));
                if(StockType.HK.getCode().equals(position.getStockType())){
                    stockListVO = SinaStockApi.assembleStockListVOHk(SinaStockApi.getSinaStock(StockType.HK.getCode()+position.getStockGid()));
                }else {
                    stockListVO = SinaStockApi.assembleStockListVO(SinaStockApi.getSinaStock(position.getStockGid()));
                }
            }
            //    stockListVO = SinaStockApi.assembleStockListVO(SinaStockApi.getSinaStock(position.getStockGid()));
            nowPrice = stockListVO.getNowPrice();
            if (nowPrice == null || new BigDecimal(stockListVO.getNowPrice()).compareTo(BigDecimal.ZERO) <= 0) {
                if(null != position.getBuyOrderIdIndex() && position.getBuyOrderIdIndex().compareTo(BigDecimal.ZERO) > 0){