From 5e57de9b12ee136e45ce5754c7fe2e7eb12af05a Mon Sep 17 00:00:00 2001
From: zj <1772600164@qq.com>
Date: Fri, 12 Jun 2026 18:35:45 +0800
Subject: [PATCH] 1

---
 trading-order-service/src/main/java/com/yami/trading/service/contract/ContractApplyOrderService.java |  163 ++++++++++++++++++++++++++++++------------------------
 1 files changed, 90 insertions(+), 73 deletions(-)

diff --git a/trading-order-service/src/main/java/com/yami/trading/service/contract/ContractApplyOrderService.java b/trading-order-service/src/main/java/com/yami/trading/service/contract/ContractApplyOrderService.java
index 4829ec0..98de9a3 100644
--- a/trading-order-service/src/main/java/com/yami/trading/service/contract/ContractApplyOrderService.java
+++ b/trading-order-service/src/main/java/com/yami/trading/service/contract/ContractApplyOrderService.java
@@ -14,7 +14,6 @@
 import com.yami.trading.bean.data.domain.Realtime;
 import com.yami.trading.bean.item.domain.Item;
 import com.yami.trading.bean.item.dto.ItemLeverageDTO;
-import com.yami.trading.bean.model.FollowWallet;
 import com.yami.trading.bean.model.User;
 import com.yami.trading.bean.model.Wallet;
 import com.yami.trading.bean.syspara.domain.Syspara;
@@ -26,12 +25,12 @@
 import com.yami.trading.common.util.RandomUtil;
 import com.yami.trading.common.util.StringUtils;
 import com.yami.trading.dao.contract.ContractApplyOrderMapper;
-import com.yami.trading.service.FollowWalletService;
 import com.yami.trading.service.MoneyLogService;
 import com.yami.trading.service.RechargeBonusService;
 import com.yami.trading.service.WalletService;
 import com.yami.trading.service.data.DataService;
 import com.yami.trading.service.item.ItemLeverageService;
+import com.yami.trading.service.item.ItemPreMarketService;
 import com.yami.trading.service.item.ItemService;
 import com.yami.trading.service.rate.ExchangeRateService;
 import com.yami.trading.service.syspara.SysparaService;
@@ -72,6 +71,8 @@
     private UserService partyService;
     @Autowired
     private ItemService itemService;
+    @Autowired(required = false)
+    private ItemPreMarketService itemPreMarketService;
     @Qualifier("dataService")
     @Autowired
     private DataService dataService;
@@ -81,8 +82,6 @@
     private MoneyLogService moneyLogService;
     @Autowired
     private WalletService walletService;
-    @Autowired
-    private FollowWalletService followWalletService;
     @Autowired
     private ContractOrderService contractOrderService;
     @Autowired
@@ -172,6 +171,7 @@
             map.put("amount", order.getVolume().multiply(order.getUnitAmount()));
             map.put("amount_open", order.getVolumeOpen().multiply(order.getUnitAmount()));
             map.put("fee", order.getFee());
+            map.put("funding_fee", order.getFundingFee());
             map.put("deposit", order.getDeposit());
             data.add(map);
         }
@@ -197,10 +197,11 @@
 
         //rose 校验如果是数字货币,则7*24
         String type = item.getType();
-        if(!type.equalsIgnoreCase(Item.cryptos)){
-            //外汇交易后台接口
+        if (!type.equalsIgnoreCase(Item.cryptos)) {
+            boolean preMarketActive = itemPreMarketService != null
+                    && itemPreMarketService.isPreMarketTradingActive(order.getSymbol());
             boolean orderOpen = this.sysparaService.find("order_open").getBoolean();
-            if (!orderOpen) {
+            if (!orderOpen && !preMarketActive && !itemService.isOpen(order.getSymbol())) {
                 throw new YamiShopBindException("不在交易时段");
             }
         }
@@ -233,46 +234,60 @@
         log.info("{}  --- order --- {}  --- {}", order.getSymbol(), item.getUuid(), levers.size());
         checkLever(order, levers);
 
-        BigDecimal sourceLeverRate = order.getLeverRate();
-        sourceLeverRate = sourceLeverRate == null ? BigDecimal.ZERO : sourceLeverRate;
+//        BigDecimal sourceLeverRate = order.getLeverRate();
+//        sourceLeverRate = sourceLeverRate == null ? BigDecimal.ZERO : sourceLeverRate;
 
-        List<ContractOrder> contractOrderSubmitted = contractOrderService.findSubmitted(order.getPartyId(),
-                order.getSymbol(), order.getDirection());
-        for (int i = 0; i < contractOrderSubmitted.size(); i++) {
-            BigDecimal targetLeverRate = contractOrderSubmitted.get(i).getLeverRate();
-            targetLeverRate = targetLeverRate == null ? BigDecimal.ZERO : targetLeverRate;
-            if (sourceLeverRate.compareTo(targetLeverRate) != 0) {
-                throw new YamiShopBindException("存在不同杠杆的持仓单");
-            }
-        }
-
-        List<ContractApplyOrder> applyOrderSubmittedList = this.findSubmitted(order.getPartyId(),
-                order.getSymbol(), "open", order.getDirection());
-        for (int i = 0; i < applyOrderSubmittedList.size(); i++) {
-            BigDecimal targetLeverRate = applyOrderSubmittedList.get(i).getLeverRate();
-            targetLeverRate = targetLeverRate == null ? BigDecimal.ZERO : targetLeverRate;
-            if (sourceLeverRate.compareTo(targetLeverRate) != 0) {
-                throw new YamiShopBindException("存在不同杠杆的持仓单");
-            }
-        }
-
+//        List<ContractOrder> contractOrderSubmitted = contractOrderService.findSubmitted(order.getPartyId(),
+//                order.getSymbol(), order.getDirection());
+//        for (int i = 0; i < contractOrderSubmitted.size(); i++) {
+//            BigDecimal targetLeverRate = contractOrderSubmitted.get(i).getLeverRate();
+//            targetLeverRate = targetLeverRate == null ? BigDecimal.ZERO : targetLeverRate;
+//            if (sourceLeverRate.compareTo(targetLeverRate) != 0) {
+//                throw new YamiShopBindException("存在不同杠杆的持仓单");
+//            }
+//        }
+//
+//        List<ContractApplyOrder> applyOrderSubmittedList = this.findSubmitted(order.getPartyId(),
+//                order.getSymbol(), "open", order.getDirection());
+//        for (int i = 0; i < applyOrderSubmittedList.size(); i++) {
+//            BigDecimal targetLeverRate = applyOrderSubmittedList.get(i).getLeverRate();
+//            targetLeverRate = targetLeverRate == null ? BigDecimal.ZERO : targetLeverRate;
+//            if (sourceLeverRate.compareTo(targetLeverRate) != 0) {
+//                throw new YamiShopBindException("存在不同杠杆的持仓单");
+//            }
+//        }
+        //下单数量
         BigDecimal volume = order.getVolume();
+        //当前价格
+        List<Realtime> list = dataService.realtime(order.getSymbol());
+        double close = 0;
+
+        /**
+         * 限价单
+         */
+        if ("limit".equals(order.getOrderPriceType())) {
+            close = order.getPrice().doubleValue();
+        }else{
+            close = list.get(0).getClose();
+        }
+
         if(StringUtils.isEmptyString(order.getOrderNo())) {
             order.setOrderNo(DateUtil.getToday("yyMMddHHmmss") + RandomUtil.getRandomNum(8));
         }
         order.setUnitAmount(BigDecimal.valueOf(item.getUnitAmount()));
 
-        order.setFee(BigDecimal.valueOf(item.getUnitFee()).multiply(order.getVolume()));
-        order.setDeposit(BigDecimal.valueOf(item.getUnitAmount()).multiply(order.getVolume()));
+
+        order.setDeposit(BigDecimal.valueOf(close).multiply(order.getVolume()).divide(order.getLeverRate()));
+        order.setFee(BigDecimal.valueOf(item.getUnitFee()).multiply(BigDecimal.valueOf(close).multiply(order.getVolume())));
         BigDecimal fee = order.getFee();
-        if (order.getLeverRate() != null) {
-            // 加上杠杆
-            order.setVolume(order.getVolume().multiply(order.getLeverRate()));
-            Syspara syspara = sysparaService.find("perpetual_contracts");
-            if (ObjectUtils.isEmpty(syspara)||"0".equals(syspara.getSvalue())) {
-//                order.setFee(fee.multiply(order.getLeverRate()));
-            }
-        }
+//        if (order.getLeverRate() != null) {
+//            // 加上杠杆
+//            order.setVolume(order.getVolume().multiply(order.getLeverRate()));
+//            Syspara syspara = sysparaService.find("perpetual_contracts");
+//            if (ObjectUtils.isEmpty(syspara)||"0".equals(syspara.getSvalue())) {
+////                order.setFee(fee.multiply(order.getLeverRate()));
+//            }
+//        }
 
 		Syspara syspara = sysparaService.find("u_standard_contract");
         BigDecimal deposit = order.getDeposit();
@@ -288,43 +303,25 @@
 
         order.setVolumeOpen(order.getVolume());
 
-        if(ContractApplyOrder.ORDER_FOLLOW == order.getFollow()) { // 跟单订单
-            FollowWallet followWallet = followWalletService.findByUserId(order.getPartyId());
-            BigDecimal amountBefore = followWallet.getMoney();
+        Wallet wallet = this.walletService.findByUserId(order.getPartyId());
+        BigDecimal amountBefore = wallet.getMoney();
 //            fee = exchangeRateService.getUsdtByType(order.getFee(), type);
 //            deposit = exchangeRateService.getUsdtByType(deposit, type);
-            BigDecimal totalAmountCost = deposit.add(order.getFee());
-            if (amountBefore.compareTo(totalAmountCost) < 0) {
-                throw new YamiShopBindException("余额不足");
-            }
-
-            followWalletService.updateMoney(order.getSymbol(), order.getPartyId(), BigDecimal.ZERO.subtract(deposit), BigDecimal.ZERO
-                    , Constants.MONEYLOG_CATEGORY_CONTRACT, Constants.WALLET_USDT, Constants.MONEYLOG_CONTENT_CONTRACT_OPEN, "委托单,订单号[" + order.getOrderNo() + "]"
-            );
-            followWalletService.updateMoney(order.getSymbol(), order.getPartyId(), BigDecimal.ZERO.subtract(fee), BigDecimal.ZERO
-                    , Constants.MONEYLOG_CATEGORY_CONTRACT, Constants.WALLET_USDT, Constants.MONEYLOG_CONTENT_FEE, "委托单手续费,订单号[" + order.getOrderNo() + "]"
-            );
-        } else {
-            Wallet wallet = this.walletService.findByUserId(order.getPartyId());
-            BigDecimal amountBefore = wallet.getMoney();
-//            fee = exchangeRateService.getUsdtByType(order.getFee(), type);
-//            deposit = exchangeRateService.getUsdtByType(deposit, type);
-            BigDecimal totalAmountCost = deposit.add(order.getFee());
-            if (amountBefore.compareTo(totalAmountCost) < 0) {
-                throw new YamiShopBindException("余额不足");
-            }
-
-            walletService.updateMoney(order.getSymbol(), order.getPartyId(), BigDecimal.ZERO.subtract(deposit), BigDecimal.ZERO
-                    , Constants.MONEYLOG_CATEGORY_CONTRACT, Constants.WALLET_USDT, Constants.MONEYLOG_CONTENT_CONTRACT_OPEN, "委托单,订单号[" + order.getOrderNo() + "]"
-            );
-            walletService.updateMoney(order.getSymbol(), order.getPartyId(), BigDecimal.ZERO.subtract(fee), BigDecimal.ZERO
-                    , Constants.MONEYLOG_CATEGORY_CONTRACT, Constants.WALLET_USDT, Constants.MONEYLOG_CONTENT_FEE, "委托单手续费,订单号[" + order.getOrderNo() + "]"
-            );
-
-
-            //ICE盘定制交易手续费返佣
-           rechargeBonusService.saveTradeBounsHandle(order.getPartyId(),1, fee.doubleValue(),order.getOrderNo(),order.getSymbol());
+        BigDecimal totalAmountCost = deposit.add(order.getFee());
+        if (amountBefore.compareTo(totalAmountCost) < 0) {
+            throw new YamiShopBindException("余额不足");
         }
+
+        walletService.updateMoney(order.getSymbol(), order.getPartyId(), BigDecimal.ZERO.subtract(deposit), BigDecimal.ZERO
+                , Constants.MONEYLOG_CATEGORY_CONTRACT, Constants.WALLET_USDT, Constants.MONEYLOG_CONTENT_CONTRACT_OPEN, "委托单,订单号[" + order.getOrderNo() + "]"
+        );
+        walletService.updateMoney(order.getSymbol(), order.getPartyId(), BigDecimal.ZERO.subtract(fee), BigDecimal.ZERO
+                , Constants.MONEYLOG_CATEGORY_CONTRACT, Constants.WALLET_USDT, Constants.MONEYLOG_CONTENT_FEE, "委托单手续费,订单号[" + order.getOrderNo() + "]"
+        );
+
+
+        //ICE盘定制交易手续费返佣
+       rechargeBonusService.saveTradeBounsHandle(order.getPartyId(),1, fee.doubleValue(),order.getOrderNo(),order.getSymbol());
         save(order);
         User party = this.userService.getById(order.getPartyId());
         if (Constants.SECURITY_ROLE_MEMBER.equals(party.getRoleName())) {
@@ -341,6 +338,26 @@
                 }
             }
         }
+        tryImmediatePreMarketFill(order);
+    }
+
+    /** 盘前时段:市价/限价委托立即按盘前价成交,避免停留在委托队列 */
+    private void tryImmediatePreMarketFill(ContractApplyOrder order) {
+        if (itemPreMarketService == null || !itemPreMarketService.isPreMarketTradingActive(order.getSymbol())) {
+            return;
+        }
+        if (!ContractApplyOrder.OFFSET_OPEN.equals(order.getOffset())) {
+            return;
+        }
+        Double prePrice = itemPreMarketService.getActivePreMarketPrice(order.getSymbol());
+        if (prePrice == null) {
+            log.warn("盘前已激活但未取到盘前价 symbol={} orderNo={}", order.getSymbol(), order.getOrderNo());
+            return;
+        }
+        Realtime realtime = new Realtime();
+        realtime.setSymbol(order.getSymbol());
+        realtime.setClose(prePrice);
+        contractOrderService.saveOpen(order, realtime);
     }
 
     /**

--
Gitblit v1.9.3