新版仿ok交易所-后端
1
dd
2026-05-30 030e1d50c1f643137220f1ecf1d90ce39174204a
trading-order-service/src/main/java/com/yami/trading/service/contract/ContractApplyOrderService.java
@@ -23,6 +23,7 @@
import com.yami.trading.common.util.StringUtils;
import com.yami.trading.dao.contract.ContractApplyOrderMapper;
import com.yami.trading.service.MoneyLogService;
import com.yami.trading.service.StrongLevelCalculationService;
import com.yami.trading.service.data.DataService;
import com.yami.trading.service.user.UserService;
import com.yami.trading.service.WalletService;
@@ -73,6 +74,8 @@
    private WalletService walletService;
    @Autowired
    private ContractOrderService contractOrderService;
    @Autowired
    private StrongLevelCalculationService strongLevelCalculationService;
    public Page<Map<String, Object>> findList(Page<ContractApplyOrder> page, String partyId, String symbol, String type, String startTime, String endTime, String symbolType) {
        QueryWrapper<ContractApplyOrder> queryWrapper = new QueryWrapper<>();
@@ -163,12 +166,15 @@
    public void saveCreate(ContractApplyOrder order) {
        if (order.getVolumeOpen().doubleValue() % 1 != 0) {
            throw new YamiShopBindException("Parameter Error1");
        }
        if (order.getVolumeOpen().compareTo(BigDecimal.ZERO) <= 0) {
            throw new YamiShopBindException("Parameter Error2");
//        if (order.getVolumeOpen().doubleValue() % 1 != 0) {
//            throw new YamiShopBindException("Parameter Error1");
//        }
//
//        if (order.getVolumeOpen().compareTo(BigDecimal.ZERO) <= 0) {
//            throw new YamiShopBindException("Parameter Error2");
//        }
        if(order.getMoney().compareTo(BigDecimal.ZERO) <= 0){
            throw new YamiShopBindException("Please enter the order amount");
        }
        boolean orderOpen = this.sysparaService.find("order_open").getBoolean();
@@ -195,65 +201,47 @@
     */
    public void open(ContractApplyOrder order) {
        Item item = this.itemService.findBySymbol(order.getSymbol());
        //虚拟币新币
        if (!itemService.isContractTrading(item)) {
            throw new YamiShopBindException("未开放合约交易");
        }
        /*if (itemService.isSuspended(item.getSymbol())) {
            throw new YamiShopBindException("停牌禁止交易");
        }*/
        List<ItemLeverageDTO> levers = itemLeverageService.findByItemId(item.getUuid());
        log.info("{}  --- order --- {}  --- {}", order.getSymbol(), item.getUuid(), levers.size());
        checkLever(order, levers);
        List<ContractOrder> contractOrderSubmitted = contractOrderService.findSubmitted(order.getPartyId(),
                order.getSymbol(), order.getDirection());
        for (int i = 0; i < contractOrderSubmitted.size(); i++) {
            BigDecimal sourceLeverRate = order.getLeverRate();
            sourceLeverRate = sourceLeverRate == null ? BigDecimal.ZERO : sourceLeverRate;
            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().toString(),
                order.getSymbol(), "open", order.getDirection());
        for (int i = 0; i < applyOrderSubmittedList.size(); i++) {
            BigDecimal sourceLeverRate = order.getLeverRate();
            sourceLeverRate = sourceLeverRate == null ? BigDecimal.ZERO : sourceLeverRate;
            BigDecimal targetLeverRate = applyOrderSubmittedList.get(i).getLeverRate();
            targetLeverRate = targetLeverRate == null ? BigDecimal.ZERO : targetLeverRate;
            if (sourceLeverRate.compareTo(targetLeverRate) != 0) {
                throw new YamiShopBindException("存在不同杠杆的持仓单");
            }
        }
        //checkLever(order, levers);
        order.setOrderNo(DateUtil.getToday("yyMMddHHmmss") + RandomUtil.getRandomNum(8));
        double number = strongLevelCalculationService.countSheets(order.getMoney().doubleValue(), order.getLeverRate().intValue(), 0.01, order.getPrice().doubleValue());
        order.setVolumeOpen(new BigDecimal(number));
        order.setVolume(new BigDecimal(number));
        BigDecimal unitAmount = order.getPrice().multiply(BigDecimal.valueOf(item.getFaceValue()));
        unitAmount = unitAmount.setScale(4, RoundingMode.DOWN);
        BigDecimal deposit = unitAmount.multiply(order.getVolumeOpen()).divide(order.getLeverRate(), 4, RoundingMode.DOWN);
        order.setUnitAmount(unitAmount);
        order.setDeposit(deposit);
        order.setDeposit(order.getMoney());
        if (order.getLeverRate() != null) {
            /**
             * 加上杠杆
             */
            // 设置订单数量
            order.setVolume(order.getVolumeOpen());
            BigDecimal fee = order.getDeposit().multiply(order.getLeverRate()).multiply(item.getUnitFee());
            fee = fee.setScale(4, RoundingMode.DOWN);  // 保留两位小数
            order.setFee(fee);
        }
        order.setVolumeOpen(order.getVolumeOpen());
        Wallet wallet = this.walletService.findByUserId(order.getPartyId());
        BigDecimal amountBefore = wallet.getMoney();
        BigDecimal totalAmountCost = order.getDeposit().add(order.getFee());
        BigDecimal totalAmountCost = order.getDeposit().add(order.getFee() != null ? order.getFee() : BigDecimal.ZERO);
        BigDecimal available = getAvailableOpenMargin(order.getPartyId(), null);
        if (amountBefore.compareTo(totalAmountCost) < 0) {
            throw new YamiShopBindException("余额不足");
        if (available.compareTo(totalAmountCost) < 0) {
            throw new YamiShopBindException("not sufficient funds");
        }
        //如果是限价单先扣钱
@@ -267,6 +255,48 @@
            );
        }
        save(order);
        // 市价单同步成交,避免异步撮合失败时长期挂单
        if (ContractApplyOrder.ORDER_PRICE_TYPE_OPPONENT.equals(order.getOrderPriceType())) {
            executeOpenImmediately(order);
        }
    }
    /**
     * 市价开仓立即撮合;失败则向上抛出,由事务回滚委托单。
     */
    private void executeOpenImmediately(ContractApplyOrder order) {
        List<Realtime> realtimes = dataService.realtime(order.getSymbol());
        if (CollectionUtil.isEmpty(realtimes)) {
            return;
        }
        contractOrderService.saveOpen(order, realtimes.get(0));
    }
    /**
     * 可用于新开仓的余额(双向持仓下多空各自占用保证金,需扣除未成交市价委托预留额)。
     *
     * @param excludeOrderNo 正在成交的委托单号,不计入预留
     */
    public BigDecimal getAvailableOpenMargin(String partyId, String excludeOrderNo) {
        Wallet wallet = walletService.findByUserId(partyId);
        if (wallet == null || wallet.getMoney() == null) {
            return BigDecimal.ZERO;
        }
        BigDecimal available = wallet.getMoney();
        List<ContractApplyOrder> pendingOpens = findSubmitted(partyId, null, ContractApplyOrder.OFFSET_OPEN, null);
        for (ContractApplyOrder pending : pendingOpens) {
            if (excludeOrderNo != null && excludeOrderNo.equals(pending.getOrderNo())) {
                continue;
            }
            if (!ContractApplyOrder.ORDER_PRICE_TYPE_OPPONENT.equals(pending.getOrderPriceType())) {
                continue;
            }
            BigDecimal deposit = pending.getDeposit() != null ? pending.getDeposit() : BigDecimal.ZERO;
            BigDecimal fee = pending.getFee() != null ? pending.getFee() : BigDecimal.ZERO;
            available = available.subtract(deposit).subtract(fee);
        }
        return available.max(BigDecimal.ZERO);
    }
    /**
@@ -291,7 +321,7 @@
            volume = volume.subtract(applyOrderSubmittedList.get(i).getVolume());
        }
        if (order.getVolume().compareTo(volume) > 0) {
            throw new YamiShopBindException("可平仓合约张数不足");
            throw new YamiShopBindException("可平仓合约数量不足");
        }
        save(order);