From 6edf5438c56814b26bf4308286ebe26ac93ccb5c Mon Sep 17 00:00:00 2001
From: zj <1772600164@qq.com>
Date: Tue, 13 Jan 2026 19:24:30 +0800
Subject: [PATCH] 1

---
 src/main/java/com/nq/service/impl/UserPendingorderServiceImpl.java |  439 ++++++++++++++++++++++++++++++++++++------------------
 1 files changed, 290 insertions(+), 149 deletions(-)

diff --git a/src/main/java/com/nq/service/impl/UserPendingorderServiceImpl.java b/src/main/java/com/nq/service/impl/UserPendingorderServiceImpl.java
index 58a4c44..d113d32 100644
--- a/src/main/java/com/nq/service/impl/UserPendingorderServiceImpl.java
+++ b/src/main/java/com/nq/service/impl/UserPendingorderServiceImpl.java
@@ -1,6 +1,7 @@
 package com.nq.service.impl;
 
 import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
@@ -62,28 +63,114 @@
     @Autowired
     private ISiteSettingService iSiteSettingService;
 
+    @Autowired
+    private IUserAssetsServices iUserAssetsServices;
+
+    @Autowired
+    private ISiteProductService iSiteProductService;
+
+    @Autowired
+    private IStockConfigServices iStockConfigServices;
+
+    @Autowired
+    private StockBuySettingMapper stockBuySettingMapper;
+
+    @Autowired
+    private ITradingHourService tradingHourService;
+
+    @Autowired
+    private IPriceServices priceServices;
+
+    @Autowired
+    private UserPositionMapper userPositionMapper;
+
     @Override
+    @org.springframework.transaction.annotation.Transactional(rollbackFor = Exception.class)
     public ServerResponse addOrder(String stockId, Integer buyNum, Integer buyType, Integer lever, BigDecimal profitTarget, BigDecimal stopTarget, BigDecimal targetPrice, HttpServletRequest request) {
         User user = this.iUserService.getCurrentRefreshUser(request);
 
         if (user == null) {
-            return ServerResponse.createByErrorMsg("Please log in first");
-        }
-        SiteSetting siteSetting = this.iSiteSettingService.getSiteSetting();
-        if (buyNum.intValue() < siteSetting.getBuyMinNum().intValue()) {
-            return ServerResponse.createByErrorMsg("The pending order failed, and the purchased quantity was less than" + siteSetting
-                    .getBuyMinNum() + "stocks");
-        }
-        if (buyNum.intValue() > siteSetting.getBuyMaxNum().intValue()) {
-            return ServerResponse.createByErrorMsg("The pending order failed because the purchased quantity was greater than" + siteSetting
-                    .getBuyMaxNum() + "stocks");
-        }
-        UserPendingorder userPendingorder = userPendingorderMapper.selectOne(new QueryWrapper<UserPendingorder>().eq("user_id", user.getId()).eq("stock_id", stockId).eq("status", 0));
-        if (userPendingorder != null) {
-            return ServerResponse.createByErrorMsg("Please do not repeat the order");
+            return ServerResponse.createByErrorMsg("请先登录", request);
         }
 
-        userPendingorder = new UserPendingorder();
+        try {
+            synchronized (user.getId()) {
+                // 获取系统设置
+                SiteProduct siteProduct = iSiteProductService.getProductSetting();
+                if (siteProduct.getRealNameDisplay() && user.getIsActive() != 2) {
+                    return ServerResponse.createByErrorMsg("挂单失败,请先实名认证", request);
+                }
+
+                if (siteProduct.getRealNameDisplay() && user.getIsLock().intValue() == 1) {
+                    return ServerResponse.createByErrorMsg("挂单失败,帐户已被锁定", request);
+                }
+
+                // 验证目标价格
+                if (targetPrice == null || targetPrice.compareTo(BigDecimal.ZERO) <= 0) {
+                    return ServerResponse.createByErrorMsg("挂单失败,目标价格必须大于0", request);
+                }
+
+                // 获取股票信息
+                Stock stock = stockMapper.findStockByCode(stockId);
+                if (stock == null) {
+                    return ServerResponse.createByErrorMsg("挂单失败,股票代码不存在", request);
+                }
+
+                // 判断股票是否被锁定
+                if (stock.getIsLock() != 0) {
+                    return ServerResponse.createByErrorMsg("挂单失败,股票被锁定", request);
+                }
+
+                // 手续费率
+                BigDecimal siteSettingBuyFee = new BigDecimal(iStockConfigServices.queryByKey(com.nq.enums.EConfigKey.BUY_HANDLING_CHARGE.getCode()).getCValue());
+
+                // 处理购买数量(手数转换)
+                StockBuySetting stockBuySetting = stockBuySettingMapper.selectOne(new QueryWrapper<com.nq.pojo.StockBuySetting>().eq("accets_type", stock.getStockType()));
+                if (stockBuySetting != null && stockBuySetting.getHandsNum() != null && stockBuySetting.getStockNum() != null) {
+                    if (buyNum < stockBuySetting.getHandsNum()) {
+                        return ServerResponse.createByErrorMsg("最低购买手数" + stockBuySetting.getHandsNum(), request);
+                    }
+                    buyNum = buyNum * stockBuySetting.getStockNum();
+                }
+
+                // 获取用户资产
+                UserAssets userAssets = iUserAssetsServices.assetsByTypeAndUserId(stock.getStockType(), user.getId());
+
+                // 验证最高购买数量
+                com.nq.pojo.StockConfig maxBuyConfig = iStockConfigServices.queryByKey(com.nq.enums.EConfigKey.MAX_BUY.getCode());
+                if (buyNum > Integer.parseInt(maxBuyConfig.getCValue())) {
+                    return ServerResponse.createByErrorMsg("最高购买数量" + maxBuyConfig.getCValue(), request);
+                }
+
+                // 检查待补资金
+                if (userAssets.getAmountToBeCovered().compareTo(BigDecimal.ZERO) > 0) {
+                    return ServerResponse.createByErrorMsg("请先缴清待补资金", request);
+                }
+
+                // 使用目标价格计算需要冻结的金额
+                BigDecimal buyAmt = targetPrice.multiply(new BigDecimal(buyNum)).divide(new BigDecimal(lever), 4, RoundingMode.HALF_UP);
+                // 手续费
+                BigDecimal orderFree = siteSettingBuyFee.multiply(buyAmt);
+                BigDecimal needFreezeAmt = buyAmt.add(orderFree);
+
+                // 资金校验(考虑配资比例)
+                BigDecimal fundratio = new BigDecimal(user.getFundRatio()).divide(new BigDecimal(100));
+                BigDecimal availableBalance = fundratio.multiply(userAssets.getAvailableBalance());
+                if (availableBalance.compareTo(needFreezeAmt) < 0) {
+                    return ServerResponse.createByErrorMsg("挂单失败,配资不足", request);
+                }
+
+                // 检查是否已有相同股票的挂单
+                UserPendingorder existingOrder = userPendingorderMapper.selectOne(new QueryWrapper<UserPendingorder>()
+                        .eq("user_id", user.getId())
+                        .eq("stock_id", stockId)
+                        .eq("status", 0));
+                if (existingOrder != null) {
+                    return ServerResponse.createByErrorMsg("该股票已有挂单,请勿重复挂单", request);
+                }
+
+                // 创建挂单记录
+                UserPendingorder userPendingorder = new UserPendingorder();
         userPendingorder.setUserId(user.getId());
         userPendingorder.setStockId(stockId);
         userPendingorder.setBuyNum(buyNum);
@@ -95,12 +182,20 @@
         userPendingorder.setTargetPrice(targetPrice);
         userPendingorder.setAddTime(new Date());
         userPendingorder.setStatus(0);
+
         int ret = userPendingorderMapper.insert(userPendingorder);
         if (ret > 0) {
-            return ServerResponse.createBySuccessMsg("If the pending order is successfully added, the order will be automatically placed if the order conditions are met");
+                    // 冻结资金
+                    iUserAssetsServices.availablebalanceChange(stock.getStockType(), user.getId(),
+                            com.nq.enums.EUserAssets.PENDING_ORDER_FREEZE, needFreezeAmt.negate(), "挂单冻结资金", "");
+                    return ServerResponse.createBySuccessMsg("挂单成功,资金已冻结", request);
+                }
+                return ServerResponse.createByErrorMsg("挂单失败", request);
+            }
+        } catch (Exception e) {
+            log.error("挂单异常:{}", e.getMessage(), e);
+            return ServerResponse.createByErrorMsg("挂单异常:" + e.getMessage(), request);
         }
-        return ServerResponse.createByErrorMsg("Add failure");
-
     }
 
     @Override
@@ -114,7 +209,7 @@
             User user = (User) JsonUtil.string2Obj(userJson, User.class);
 //            log.info("user:{}",user);
             if (user != null) {
-                List<UserPendingorder> userPendingorders = userPendingorderMapper.selectList(new QueryWrapper<UserPendingorder>().eq("user_id", user.getId()));
+                List<UserPendingorder> userPendingorders = userPendingorderMapper.selectList(new QueryWrapper<UserPendingorder>().eq("user_id", user.getId()).eq("status",0));
 
                 List UserPendingorderList = new ArrayList();
 
@@ -133,6 +228,7 @@
                     }
                     userPendingorderVO.setNowPrice(new BigDecimal(nowPrice));
                     userPendingorderVO.setStockName(stock.getStockName());
+                    userPendingorderVO.setStockGid(stock.getStockType());
                     userPendingorderVO.setStockId(stock.getStockCode());
                     userPendingorderVO.setBuyNum(userPendingorder.getBuyNum());
                     userPendingorderVO.setBuyType(userPendingorder.getBuyType());
@@ -154,157 +250,202 @@
 
     @Override
     public void orderTask() {
-
+        // 查询所有待处理的挂单
         List<UserPendingorder> userPendingorders = userPendingorderMapper.selectList(new QueryWrapper<UserPendingorder>().eq("status", 0));
-        log.info("当前有挂单的用户数量 为 {}", Integer.valueOf(userPendingorders.size()));
-        for (int i = 0; i < userPendingorders.size(); i++) {
-            Integer userId = (Integer) userPendingorders.get(i).getUserId();
-            User user = this.userMapper.selectById(userId);
-            if (user == null) {
+        log.info("当前有挂单数量:{}", userPendingorders.size());
+        
+        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;
             }
-            List<UserPendingorder> userPendingorderList = userPendingorderMapper.selectList(new QueryWrapper<UserPendingorder>().eq("user_id", userId).eq("status", 0));
-            if (userPendingorderList == null) {
+
+                // 获取股票信息
+                Stock stock = stockMapper.findStockByCode(userPendingorder.getStockId());
+                if (stock == null) {
+                    log.error("挂单转持仓失败,股票不存在:{}", userPendingorder.getStockId());
+                    userPendingorder.setStatus(2);
+                    this.userPendingorderMapper.updateById(userPendingorder);
                 continue;
             }
-            log.info("用户id = {} 姓名 = {} 已挂单数: {}", new Object[]{userId, user.getRealName(), Integer.valueOf(userPendingorders.size())});
-            BigDecimal all_freez_amt = new BigDecimal("0");
-            String nowPrice = "";
-            String code = "";
-            Integer indexId = null;
-            StockListVO stockListVO = new StockListVO();
-            StockCoin stockCoin = new StockCoin();
-            for (UserPendingorder userPendingorder : userPendingorderList) {
-                //指数
-                if (userPendingorder.getStockId().contains("sh") || userPendingorder.getStockId().contains("sz") || userPendingorder.getStockId().contains("hk") || userPendingorder.getStockId().contains("us")) {
-                    StockIndex model = stockIndexMapper.selectIndexByCode(userPendingorder.getStockId().replace("sh", "").replace("sz", "").replace("hk", "").replace("us", ""));
-                    all_freez_amt = (new BigDecimal(model.getDepositAmt().intValue())).multiply(new BigDecimal(userPendingorder.getBuyNum())).divide(new BigDecimal(userPendingorder.getLever())).setScale(4, 2);
 
-//                    if (){
-//
-//                    }else {
-                    MarketVO marketVO = this.iStockIndexService.querySingleIndex(model.getIndexGid());
-                    nowPrice = marketVO.getNowPrice();
-//                    }
-
-                    indexId = model.getId();
-
-                } else {
-                    //股票
-                    Stock stock = stockMapper.findStockByCode(userPendingorder.getStockId());
-                    if ("hk".equals(stock.getStockType())) {
-                        String hk = RedisShardedPoolUtils.get(stock.getStockGid(), 1);
-                        stockListVO = StockApi.otherStockListVO(hk);
-                        //                        stockCoin = iStockCoinService.selectCoinByCode("HKD");
-                        ExchangeVO exchangeVO = this.iStockFuturesService.queryExchangeVO("HKD").getData();
-                        nowPrice = String.valueOf(new BigDecimal(stockListVO.getNowPrice()).multiply(new BigDecimal(exchangeVO.getNowPrice())));
-                    } else if ("us".equals(stock.getStockType())) {
-                        String us = RedisShardedPoolUtils.get(stock.getStockGid(), 2);
-                        stockListVO = StockApi.otherStockListVO(us);
-                        //                        stockCoin = iStockCoinService.selectCoinByCode("USD");
-                        ExchangeVO exchangeVO = this.iStockFuturesService.queryExchangeVO("USD").getData();
-                        nowPrice = String.valueOf(new BigDecimal(stockListVO.getNowPrice()).multiply(new BigDecimal(exchangeVO.getNowPrice())));
-                    } else {
-                        stockListVO = StockApi.getStockRealTime(stock);
-                        nowPrice = stockListVO.getNowPrice();
-                    }
-
-
-                    all_freez_amt = new BigDecimal(nowPrice).multiply(new BigDecimal(userPendingorder.getBuyNum())).divide(new BigDecimal(userPendingorder.getLever()), 2, 4);
-                    code = stock.getStockCode();
+                // 获取当前价格
+                BigDecimal nowPrice = priceServices.getNowPrice(stock.getStockCode());
+                if (nowPrice.compareTo(BigDecimal.ZERO) == 0) {
+                    continue;
                 }
-                if (nowPrice == null) {
-                    nowPrice = String.valueOf(0);
+
+                // 判断价格是否达到目标价格:当前价格 <= 挂单价就买入
+                if (nowPrice.compareTo(userPendingorder.getTargetPrice()) > 0) {
+                    // 当前价格大于挂单价,继续等待
+                    continue;
                 }
-                if (userPendingorder.getUserId() != null && userPendingorder.getStockId() != null && userPendingorder.getBuyNum() != null && userPendingorder.getBuyType() != null && userPendingorder.getLever() != null && userPendingorder.getTargetPrice() != null) {
-                    int ret = userPendingorder.getBuyType().intValue() == 0 ? userPendingorder.getTargetPrice().compareTo(new BigDecimal(nowPrice)) : new BigDecimal(nowPrice).compareTo(userPendingorder.getTargetPrice());
-                    //当前时间String
-                    String buyTime = DateTimeUtil.dateToStr(new Date());
-                    if (ret <= 0) {
-                        if (code != null && !"".equals(code)) {
-                            try {
-                                this.iUserPositionService.create(userPendingorder.getUserId(), code, nowPrice, buyTime, userPendingorder.getBuyNum(), userPendingorder.getBuyType(), userPendingorder.getLever(), userPendingorder.getProfitTarget(), userPendingorder.getStopTarget());
-                                userPendingorder.setStatus(1);
-                                this.userPendingorderMapper.updateById(userPendingorder);
-                                SiteTaskLog siteTaskLog = new SiteTaskLog();
-                                siteTaskLog.setTaskType("股票挂单转持仓");
-                                String accountType = (user.getAccountType() == 0) ? "正式用户" : "模拟用户";
-                                String tasktarget = "此次挂单买入id:" + userPendingorder.getId();
-                                siteTaskLog.setTaskTarget(tasktarget);
-                                siteTaskLog.setAddTime(new Date());
-                                siteTaskLog.setIsSuccess(0);
-                                siteTaskLog.setErrorMsg("");
-                                int insertTaskCount = this.siteTaskLogMapper.insert(siteTaskLog);
-                                if (insertTaskCount > 0) {
-                                    log.info("挂单task任务成功");
-                                } else {
-                                    log.info("挂单task任务失败");
-                                }
-                            } catch (Exception e) {
-                                log.error("股票挂单任务失败...");
+
+                // 价格达到目标价格,先进行验证
+                // 判断股票是否在可交易时间段
+                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);
-                            }
-                        } else if (indexId != null) {
-                            try {
-                                this.iUserIndexPositionService.buyIndexOrder(indexId, userPendingorder.getBuyNum(), userPendingorder.getBuyType(), userPendingorder.getLever(), userPendingorder.getProfitTarget(), userPendingorder.getStopTarget(), userPendingorder.getUserId());
-                                userPendingorder.setStatus(1);
-                                this.userPendingorderMapper.updateById(userPendingorder);
-                                SiteTaskLog siteTaskLog = new SiteTaskLog();
-                                siteTaskLog.setTaskType("指数挂单转持仓");
-                                String accountType = (user.getAccountType() == 0) ? "正式用户" : "模拟用户";
-                                String tasktarget = "此次挂单买入id:" + userPendingorder.getId();
-                                siteTaskLog.setTaskTarget(tasktarget);
-                                siteTaskLog.setAddTime(new Date());
-                                siteTaskLog.setIsSuccess(0);
-                                siteTaskLog.setErrorMsg("");
-                                int insertTaskCount = this.siteTaskLogMapper.insert(siteTaskLog);
-                                if (insertTaskCount > 0) {
-                                    log.info("挂单task任务成功");
-                                    userPendingorder.setStatus(1);
-                                } else {
-                                    log.info("挂单task任务失败");
-                                }
-                            } catch (Exception e) {
-                                log.error("指数挂单任务失败...");
-                                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()) {
+                    // 再次检查挂单状态,防止并发处理
+                    UserPendingorder checkOrder = userPendingorderMapper.selectById(userPendingorder.getId());
+                    if (checkOrder == null || checkOrder.getStatus() != 0) {
+                        // 挂单已被处理,跳过本次循环
+                        continue orderLoop;
+                    }
+
+                    // 手续费率
+                    BigDecimal siteSettingBuyFee = new BigDecimal(iStockConfigServices.queryByKey(com.nq.enums.EConfigKey.BUY_HANDLING_CHARGE.getCode()).getCValue());
+
+                    // 计算买入金额和手续费(使用当前价格)
+                    BigDecimal buyAmt = nowPrice.multiply(new BigDecimal(userPendingorder.getBuyNum())).divide(new BigDecimal(userPendingorder.getLever()), 4, RoundingMode.HALF_UP);
+                    BigDecimal orderFree = siteSettingBuyFee.multiply(buyAmt);
+                    
+                    // 计算挂单时冻结的金额(用于解冻)
+                    BigDecimal targetBuyAmt = userPendingorder.getTargetPrice().multiply(new BigDecimal(userPendingorder.getBuyNum())).divide(new BigDecimal(userPendingorder.getLever()), 4, RoundingMode.HALF_UP);
+                    BigDecimal targetOrderFree = siteSettingBuyFee.multiply(targetBuyAmt);
+                    BigDecimal freezeAmt = targetBuyAmt.add(targetOrderFree);
+
+                    // 先解冻挂单冻结的资金
+                    iUserAssetsServices.availablebalanceChange(stock.getStockType(), userPendingorder.getUserId(),
+                            com.nq.enums.EUserAssets.PENDING_ORDER_UNFREEZE, freezeAmt, "挂单触发解冻资金", "");
+
+                    // 创建 UserPosition,与 buy.do 逻辑完全一致
+                    UserPosition userPosition = new UserPosition();
+                    if (userPendingorder.getProfitTarget() != null && userPendingorder.getProfitTarget().compareTo(BigDecimal.ZERO) > 0) {
+                        userPosition.setProfitTargetPrice(userPendingorder.getProfitTarget());
+                    }
+                    if (userPendingorder.getStopTarget() != null && userPendingorder.getStopTarget().compareTo(BigDecimal.ZERO) > 0) {
+                        userPosition.setStopTargetPrice(userPendingorder.getStopTarget());
+                    }
+                    userPosition.setPositionType(user.getAccountType());
+                    userPosition.setPositionSn(com.nq.utils.KeyUtils.getUniqueKey());
+                    userPosition.setUserId(userPendingorder.getUserId());
+                    userPosition.setNickName(user.getRealName());
+                    userPosition.setAgentId(user.getAgentId());
+                    userPosition.setStockCode(stock.getStockCode());
+                    userPosition.setStockName(stock.getStockName());
+                    userPosition.setStockGid(stock.getStockType());
+                    userPosition.setStockSpell(stock.getStockSpell());
+                    userPosition.setBuyOrderId(com.nq.utils.stock.GeneratePosition.getPositionId());
+                    userPosition.setBuyOrderTime(new Date());
+                    userPosition.setBuyOrderPrice(nowPrice);
+                    userPosition.setOrderDirection((userPendingorder.getBuyType().intValue() == 0) ? "买涨" : "买跌");
+                    userPosition.setOrderNum(userPendingorder.getBuyNum());
+                    if (stock.getStockPlate() != null) {
+                        userPosition.setStockPlate(stock.getStockPlate());
+                    }
+                    userPosition.setIsLock(Integer.valueOf(0));
+                    userPosition.setOrderLever(userPendingorder.getLever());
+                    userPosition.setOrderTotalPrice(buyAmt);
+                    userPosition.setOrderFee(orderFree);
+                    userPosition.setOrderSpread(BigDecimal.ZERO);
+                    userPosition.setSpreadRatePrice(BigDecimal.ZERO);
+                    BigDecimal profit_and_lose = new BigDecimal("0");
+                    userPosition.setProfitAndLose(profit_and_lose);
+                    userPosition.setAllProfitAndLose(profit_and_lose.add(orderFree));
+                    userPosition.setOrderStayDays(Integer.valueOf(0));
+                    userPosition.setOrderStayFee(BigDecimal.ZERO);
+                    
+                    // 插入持仓记录
+                    userPositionMapper.insert(userPosition);
+                    
+                    // 扣款和手续费,与 buy.do 完全一致
+                    iUserAssetsServices.availablebalanceChange(stock.getStockType(), userPendingorder.getUserId(), 
+                            com.nq.enums.EUserAssets.BUY, buyAmt.negate(), "", "");
+                    iUserAssetsServices.availablebalanceChange(stock.getStockType(), userPendingorder.getUserId(), 
+                            com.nq.enums.EUserAssets.HANDLING_CHARGE, orderFree, "", "");
+
+                    // 更新挂单状态
+                    userPendingorder.setStatus(1);
+                    this.userPendingorderMapper.updateById(userPendingorder);
+                    
+                    log.info("挂单转持仓成功,挂单id:{},股票:{}", userPendingorder.getId(), stock.getStockCode());
+                }
+            } catch (Exception e) {
+                log.error("挂单转持仓失败,挂单id:{}", userPendingorder.getId(), e);
+                userPendingorder.setStatus(2);
+                this.userPendingorderMapper.updateById(userPendingorder);
             }
-
         }
-        log.info("===========挂单结束==========");
+        log.info("===========挂单任务结束==========");
     }
 
     //删除
     @Override
+    @org.springframework.transaction.annotation.Transactional(rollbackFor = Exception.class)
     public ServerResponse delOrder(Integer id, HttpServletRequest request) {
-        String property = PropertiesUtil.getProperty("user.cookie.name");
-        String header = request.getHeader(property);
-        if (header != null) {
-            String userJson = RedisShardedPoolUtils.get(header);
-            User user = (User) JsonUtil.string2Obj(userJson, User.class);
-            UserPendingorder userPendingorder = this.userPendingorderMapper.selectById(id);
-            if (userPendingorder == null) {
-                return ServerResponse.createByErrorMsg("The pending order does not exist");
-            }
-            if (user.getId().intValue() != userPendingorder.getUserId().intValue()) {
-                return ServerResponse.createByErrorMsg("The pending order does not belong to you");
-            }
-            int delCount = this.userPendingorderMapper.deleteById(id);
-            if (delCount > 0) {
-                return ServerResponse.createByErrorMsg("Successfully deleted");
-            }
-            return ServerResponse.createByErrorMsg("Deletion failure");
+        User user = this.iUserService.getCurrentRefreshUser(request);
+        if (user == null) {
+            return ServerResponse.createByErrorMsg("请先登录", request);
         }
 
-        return ServerResponse.createByErrorMsg("Please log in");
+        try {
+            UserPendingorder userPendingorder = this.userPendingorderMapper.selectById(id);
+            if (userPendingorder == null) {
+                return ServerResponse.createByErrorMsg("挂单不存在", request);
+            }
+
+            if (user.getId().intValue() != userPendingorder.getUserId().intValue()) {
+                return ServerResponse.createByErrorMsg("该挂单不属于您", request);
+            }
+
+            // 检查挂单状态,只有待处理的挂单才能取消
+            if (userPendingorder.getStatus() != 0) {
+                return ServerResponse.createByErrorMsg("该挂单已处理,无法取消", request);
+            }
+
+            // 获取股票信息以确定资产类型
+            Stock stock = stockMapper.findStockByCode(userPendingorder.getStockId());
+            if (stock == null) {
+                return ServerResponse.createByErrorMsg("股票不存在", request);
+            }
+
+            // 计算需要解冻的金额(根据挂单参数重新计算)
+            BigDecimal siteSettingBuyFee = new BigDecimal(iStockConfigServices.queryByKey(com.nq.enums.EConfigKey.BUY_HANDLING_CHARGE.getCode()).getCValue());
+            BigDecimal buyAmt = userPendingorder.getTargetPrice().multiply(new BigDecimal(userPendingorder.getBuyNum())).divide(new BigDecimal(userPendingorder.getLever()), 4, RoundingMode.HALF_UP);
+            BigDecimal orderFree = siteSettingBuyFee.multiply(buyAmt);
+            BigDecimal needUnfreezeAmt = buyAmt.add(orderFree);
+
+            // 解冻资金
+            iUserAssetsServices.availablebalanceChange(stock.getStockType(), user.getId(),
+                    com.nq.enums.EUserAssets.PENDING_ORDER_UNFREEZE, needUnfreezeAmt, "取消挂单解冻资金", "");
+
+            // 删除挂单记录
+            int delCount = this.userPendingorderMapper.deleteById(id);
+            if (delCount > 0) {
+                return ServerResponse.createBySuccessMsg("取消挂单成功,资金已解冻", request);
+            }
+            return ServerResponse.createByErrorMsg("取消挂单失败", request);
+        } catch (Exception e) {
+            log.error("取消挂单异常:{}", e.getMessage(), e);
+            return ServerResponse.createByErrorMsg("取消挂单异常:" + e.getMessage(), request);
+        }
     }
 
 

--
Gitblit v1.9.3