heng.zhang1
2020-09-16 2abd0e479bd68cc377d77ac8178cc6b21783fcf1
src/main/java/com/xcong/excoin/modules/coin/service/impl/OrderCoinServiceImpl.java
@@ -161,13 +161,18 @@
    @Override
    @Transactional
    public Result submitSalesWalletCoinOrder(String symbol, Integer type, Integer tradeType, BigDecimal price, BigDecimal amount) {
    public Result submitSalesWalletCoinOrder(String symbol, Integer type, Integer tradeType, BigDecimal price, BigDecimal amount,BigDecimal entrustAmount) {
        //获取用户ID
        Long memberId = LoginUserUtils.getAppLoginUser().getId();
        BigDecimal nowPriceinBigDecimal = price;
        //查询当前价
        BigDecimal nowPrice = new BigDecimal(redisUtils.getString(CoinTypeConvert.convertToKey(symbol + "/USDT")));
        if(OrderCoinsEntity.ORDERTYPE_BUY.equals(type) && OrderCoinsEntity.TRADETYPE_MARKETPRICE.equals(tradeType)){
            amount = entrustAmount.divide(nowPrice,BigDecimal.ROUND_DOWN);
        }
        // 处理市价
        // 获取交易管理的杠杠倍率,手续费率等信息,由平台进行设置
        symbol = symbol.toUpperCase();
        MemberWalletCoinEntity walletCoin = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, symbol);
@@ -319,7 +324,7 @@
    @Override
    public Result submitSalesWalletCoinOrderWithMatch(String symbol, Integer type, Integer tradeType, BigDecimal price, BigDecimal amount, BigDecimal entrustAmount) {
        //获取用户ID
        Long memberId = 13L;
        Long memberId = LoginUserUtils.getAppLoginUser().getId();
        BigDecimal nowPriceinBigDecimal = price;
        //查询当前价
        //BigDecimal nowPrice = new BigDecimal(redisUtils.getString(CoinTypeConvert.convertToKey(symbol + "/USDT")));
@@ -818,11 +823,16 @@
            if(exchangeTrade==null){
                continue;
            }
            // 量
            BigDecimal amount = exchangeTrade.getAmount();
            // 买单ID
            Long buyOrderId = exchangeTrade.getBuyOrderId();
            // 成交金额(usdt)
            BigDecimal buyTurnover = exchangeTrade.getBuyTurnover();
            int direction = exchangeTrade.getDirection();
            // 成交价
            BigDecimal price = exchangeTrade.getPrice();
            // 卖单
            Long sellOrderId = exchangeTrade.getSellOrderId();
            // 买卖单都需要处理
            // 买单
@@ -918,4 +928,67 @@
            }
        }
    }
    @Override
    public void initOrders(String symbol, Integer type, Integer tradeType, BigDecimal price,
                           BigDecimal amount,BigDecimal entrustAmount) {
        //获取用户ID
        Long memberId = 10L;
        BigDecimal nowPriceinBigDecimal = price;
        //查询当前价
        //BigDecimal nowPrice = new BigDecimal(redisUtils.getString(CoinTypeConvert.convertToKey(symbol + "/USDT")));
        // 获取交易管理的杠杠倍率,手续费率等信息,由平台进行设置
        symbol = symbol.toUpperCase();
        // 手续费用(手续费=建仓价X数量X手续费率)
        BigDecimal closingPrice = BigDecimal.ZERO ;
        // BigDecimal totalPayPricCoin = nowPrice.multiply(amount).add(closingPrice);
        // 首先将单插入到数据库主表(委托表)
        // 创建订单
        OrderCoinsEntity order = new OrderCoinsEntity();
        //根据委托类型生成不同数据
        // 如果是限价交易直接插入主表数据
        order.setMemberId(memberId);
        order.setOrderNo(generateSimpleSerialno(memberId.toString()));
        order.setOrderType(type);
        order.setSymbol(symbol);
        //order.setMarkPrice(nowPrice);
        // 成交量 先设置为0
        order.setDealCnt(BigDecimal.ZERO);
        // 成交价
        //order.setDealPrice(price);
        // 成交金额
        order.setDealAmount(BigDecimal.ZERO);
        order.setOrderStatus(OrderCoinsEntity.ORDERSTATUS_DODING);
        order.setTradeType(tradeType);
        // 手续费
        order.setFeeAmount(closingPrice);
        if(OrderCoinsEntity.TRADETYPE_FIXEDPRICE.equals(tradeType)){
            // 限价 是需要价格和数量 可以得到成交金额
            // 下单量
            order.setEntrustCnt(amount);
            // 下单价格
            order.setEntrustPrice(price);
            order.setEntrustAmount(amount.multiply(price));
        }else{
            if(OrderCoinsEntity.ORDERTYPE_BUY.equals(type)){
                // 市价 只有金额
                order.setEntrustAmount(entrustAmount);
            }else{
                // 下单量
                order.setEntrustCnt(amount);
                // 下单价格
                order.setEntrustPrice(price);
                order.setEntrustAmount(amount.multiply(price));
            }
        }
        orderCoinsDao.insert(order);
        // 加入到撮合
        CoinTrader trader = factory.getTrader(symbol);
        trader.trade(order);
    }
}