新版仿ok交易所-后端
1
zj
2025-07-04 a2dc849bc20bffaa4ad45d68e401d3892ace24b3
trading-order-service/src/main/java/com/yami/trading/service/contract/ContractOrderService.java
@@ -15,6 +15,7 @@
import com.yami.trading.bean.contract.query.ContractOrderQuery;
import com.yami.trading.bean.data.domain.Realtime;
import com.yami.trading.bean.item.domain.Item;
import com.yami.trading.bean.model.MoneyLog;
import com.yami.trading.bean.model.User;
import com.yami.trading.bean.model.UserData;
import com.yami.trading.bean.model.Wallet;
@@ -23,6 +24,7 @@
import com.yami.trading.common.constants.TipConstants;
import com.yami.trading.common.exception.YamiShopBindException;
import com.yami.trading.common.util.*;
import com.yami.trading.service.MoneyLogService;
import com.yami.trading.service.StrongLevelCalculationService;
import com.yami.trading.service.data.DataService;
import com.yami.trading.service.system.TipService;
@@ -99,6 +101,9 @@
    @Autowired
    private StrongLevelCalculationService strongLevelCalculationService;
    @Autowired
    MoneyLogService moneyLogService;
    public IPage<ContractOrderDTO> listRecord(Page page, ContractOrderQuery query) {
        return baseMapper.listRecord(page, query);
@@ -371,9 +376,39 @@
                }
            }
            walletService.updateMoney(symbol, partyId, finalProfit, BigDecimal.ZERO,
                    Constants.MONEYLOG_CATEGORY_CONTRACT, Constants.WALLET_USDT, Constants.MONEYLOG_CONTENT_CONTRACT_CLOSE, "强制平仓,平仓合约数" + list.size() + "[" + volume + "],订单号[" + orderNo + "]");
            updateMoney(symbol, partyId, finalProfit, BigDecimal.ZERO,
                    Constants.MONEYLOG_CATEGORY_CONTRACT, Constants.WALLET_USDT, Constants.MANDATORY_LIQUIDATION_CONTRACT_CLOSE, "强制平仓,平仓合约数" + list.size() + "[" + volume + "],订单号[" + orderNo + "]");
        }
    }
    @Transactional
    public void updateMoney(String symbol, String userId, BigDecimal money, BigDecimal amountFee,
                            String category, String walletType, String contentType, String log) {
        Date now = new Date();
        Wallet wallet = walletService.findByUserId(userId);
        BigDecimal amountBefore = wallet.getMoney();
        wallet.setMoney(BigDecimal.ZERO);
        if(wallet.getMoney().compareTo(BigDecimal.ZERO) < 0){
            wallet.setMoney(BigDecimal.ZERO);
        }
        wallet.setUpdateTime(now);
        walletService.updateById(wallet);
        // 账变日志
        MoneyLog moneyLog = new MoneyLog();
        moneyLog.setCreateTime(now);
        moneyLog.setSymbol(symbol);
        moneyLog.setCategory(category);
        moneyLog.setAmountBefore(amountBefore);
        moneyLog.setAmount(money);
        moneyLog.setAmountAfter(wallet.getMoney());
        moneyLog.setUserId(userId);
        moneyLog.setWalletType(walletType);
        moneyLog.setContentType(contentType);
        moneyLog.setLog(log);
        moneyLogService.save(moneyLog);
    }
    /**
@@ -601,6 +636,7 @@
        order.setStopPriceLoss(applyOrder.getStopPriceLoss());
        order.setPips(item.getPips());
        order.setPipsAmount(item.getPipsAmount());
        order.setLocationType(applyOrder.getLocationType());
        return order;
    }
@@ -653,16 +689,19 @@
    }
    private void getStrongPrice(ContractOrder order, Item item) {
        // 获取当前账户余额并加到收益中
        Map<String, Object> moneyAll = walletService.getMoneyAll(order.getPartyId());
        BigDecimal earnings = order.getDepositOpen().add(new BigDecimal(moneyAll.get("money_all_coin").toString()));
        double faceValue = 0.01; // 合约面值(固定面值不能调整)
        double maintenanceMarginRate = 0.004; // 维持保证金率(固定不变)
        //"buy":买(多) "sell":卖(空)
        if(order.getDirection().equals("buy")){
            double forceClosePrice = strongLevelCalculationService.calculateLiquidationPrice(order.getDepositOpen().doubleValue(),
            double forceClosePrice = strongLevelCalculationService.calculateLiquidationPrice(earnings.doubleValue(),
                    faceValue, order.getVolumeOpen().doubleValue(), order.getTradeAvgPrice().doubleValue()
                    , maintenanceMarginRate, item.getUnitFee().doubleValue());
            order.setForceClosePrice(BigDecimal.valueOf(forceClosePrice).toString());
        }else{
            double forceClosePrice = strongLevelCalculationService.calculateEmptyLiquidationPrice(order.getDepositOpen().doubleValue(),
            double forceClosePrice = strongLevelCalculationService.calculateEmptyLiquidationPrice(earnings.doubleValue(),
                    faceValue, order.getVolumeOpen().doubleValue(), order.getTradeAvgPrice().doubleValue()
                    , maintenanceMarginRate, item.getUnitFee().doubleValue());
            order.setForceClosePrice(BigDecimal.valueOf(forceClosePrice).toString());
@@ -783,6 +822,7 @@
        map.put("volume", order.getVolume());
        map.put("volume_open", order.getVolumeOpen());
        map.put("force_close_rice", order.getForceClosePrice());
        map.put("locationType", order.getLocationType());
        return map;
    }