1
zj
2026-01-13 6edf5438c56814b26bf4308286ebe26ac93ccb5c
src/main/java/com/nq/service/impl/UserPendingorderServiceImpl.java
@@ -171,20 +171,20 @@
                // 创建挂单记录
                UserPendingorder userPendingorder = new UserPendingorder();
                userPendingorder.setUserId(user.getId());
                userPendingorder.setStockId(stockId);
                userPendingorder.setBuyNum(buyNum);
                userPendingorder.setBuyType(buyType);
                userPendingorder.setLever(lever);
                userPendingorder.setProfitTarget(profitTarget);
                userPendingorder.setStopTarget(stopTarget);
                userPendingorder.setNowPrice(new BigDecimal(0));
                userPendingorder.setTargetPrice(targetPrice);
                userPendingorder.setAddTime(new Date());
                userPendingorder.setStatus(0);
        userPendingorder.setUserId(user.getId());
        userPendingorder.setStockId(stockId);
        userPendingorder.setBuyNum(buyNum);
        userPendingorder.setBuyType(buyType);
        userPendingorder.setLever(lever);
        userPendingorder.setProfitTarget(profitTarget);
        userPendingorder.setStopTarget(stopTarget);
        userPendingorder.setNowPrice(new BigDecimal(0));
        userPendingorder.setTargetPrice(targetPrice);
        userPendingorder.setAddTime(new Date());
        userPendingorder.setStatus(0);
                int ret = userPendingorderMapper.insert(userPendingorder);
                if (ret > 0) {
        int ret = userPendingorderMapper.insert(userPendingorder);
        if (ret > 0) {
                    // 冻结资金
                    iUserAssetsServices.availablebalanceChange(stock.getStockType(), user.getId(),
                            com.nq.enums.EUserAssets.PENDING_ORDER_FREEZE, needFreezeAmt.negate(), "挂单冻结资金", "");
@@ -254,14 +254,14 @@
        List<UserPendingorder> userPendingorders = userPendingorderMapper.selectList(new QueryWrapper<UserPendingorder>().eq("status", 0));
        log.info("当前有挂单数量:{}", userPendingorders.size());
        
        for (UserPendingorder userPendingorder : userPendingorders) {
        orderLoop: for (UserPendingorder userPendingorder : userPendingorders) {
            try {
                // 参数校验
                if (userPendingorder.getUserId() == null || userPendingorder.getStockId() == null 
                        || userPendingorder.getBuyNum() == null || userPendingorder.getBuyType() == null 
                        || userPendingorder.getLever() == null || userPendingorder.getTargetPrice() == null) {
                    continue;
                }
                continue;
            }
                // 获取股票信息
                Stock stock = stockMapper.findStockByCode(userPendingorder.getStockId());
@@ -269,8 +269,8 @@
                    log.error("挂单转持仓失败,股票不存在:{}", userPendingorder.getStockId());
                    userPendingorder.setStatus(2);
                    this.userPendingorderMapper.updateById(userPendingorder);
                    continue;
                }
                continue;
            }
                // 获取当前价格
                BigDecimal nowPrice = priceServices.getNowPrice(stock.getStockCode());
@@ -284,31 +284,39 @@
                    continue;
                }
                // 价格达到目标价格,执行买入逻辑
                // 价格达到目标价格,先进行验证
                // 判断股票是否在可交易时间段
                Boolean b = tradingHourService.timeCheck(stock.getStockCode(), stock.getStockType());
                if (!b) {
                    continue;
                }
                // 检查股票是否被锁定
                if (stock.getIsLock() != 0) {
                    log.info("挂单转持仓失败,股票被锁定:{}", stock.getStockCode());
                                userPendingorder.setStatus(2);
                                this.userPendingorderMapper.updateById(userPendingorder);
                    continue;
                }
                // 检查是否有配额
                if (!priceServices.isLimitUpBuy(stock.getStockCode())) {
                    continue;
                }
                // 获取用户信息
                User user = this.userMapper.selectById(userPendingorder.getUserId());
                if (user == null) {
                    continue;
                }
                // 执行买入逻辑(在 synchronized 块内)
                synchronized (userPendingorder.getUserId()) {
                    // 判断股票是否在可交易时间段
                    Boolean b = tradingHourService.timeCheck(stock.getStockCode(), stock.getStockType());
                    if (!b) {
                        continue;
                    }
                    // 检查股票是否被锁定
                    if (stock.getIsLock() != 0) {
                        log.info("挂单转持仓失败,股票被锁定:{}", stock.getStockCode());
                        userPendingorder.setStatus(2);
                        this.userPendingorderMapper.updateById(userPendingorder);
                        continue;
                    }
                    // 检查是否有配额
                    if (!priceServices.isLimitUpBuy(stock.getStockCode())) {
                        continue;
                    }
                    // 获取用户信息
                    User user = this.userMapper.selectById(userPendingorder.getUserId());
                    if (user == null) {
                        continue;
                    // 再次检查挂单状态,防止并发处理
                    UserPendingorder checkOrder = userPendingorderMapper.selectById(userPendingorder.getId());
                    if (checkOrder == null || checkOrder.getStatus() != 0) {
                        // 挂单已被处理,跳过本次循环
                        continue orderLoop;
                    }
                    // 手续费率