zainali5120
2020-10-10 f8a0008705fd8067959151ce83c5dce19e72fb85
src/main/java/com/xcong/excoin/modules/coin/service/impl/OrderCoinServiceImpl.java
@@ -8,12 +8,16 @@
import javax.annotation.Resource;
import com.alibaba.fastjson.JSONObject;
import com.xcong.excoin.common.enumerates.CoinTypeEnum;
import com.xcong.excoin.modules.blackchain.service.RocService;
import com.xcong.excoin.modules.coin.mapper.OrderCoinsDealMapper;
import com.xcong.excoin.modules.member.dao.MemberDao;
import com.xcong.excoin.modules.member.entity.MemberEntity;
import com.xcong.excoin.modules.platform.entity.PlatformCnyUsdtExchangeEntity;
import com.xcong.excoin.modules.platform.entity.PlatformSymbolsCoinEntity;
import com.xcong.excoin.modules.symbols.constants.SymbolsConstats;
import com.xcong.excoin.rabbit.producer.OrderSubmitProducer;
import com.xcong.excoin.trade.CoinTrader;
import com.xcong.excoin.trade.CoinTraderFactory;
import com.xcong.excoin.trade.ExchangeTrade;
@@ -85,6 +89,12 @@
    @Resource
    private CoinTraderFactory factory;
    @Resource
    private MemberDao memberDao;
    @Resource
    private OrderSubmitProducer orderSubmitProducer;
    @Override
@@ -332,6 +342,11 @@
    public Result submitSalesWalletCoinOrderWithMatch(String symbol, Integer type, Integer tradeType, BigDecimal price, BigDecimal amount, BigDecimal entrustAmount) {
        //获取用户ID
        Long memberId = LoginUserUtils.getAppLoginUser().getId();
        // 需要实名
        MemberEntity memberEntity = memberDao.selectById(memberId);
        if(!MemberEntity.CERTIFY_STATUS_Y.equals(memberEntity.getCertifyStatus())){
            return Result.fail(MessageSourceUtils.getString("member_controller_0001"));
        }
        BigDecimal nowPriceinBigDecimal = price;
        //查询当前价
        //BigDecimal nowPrice = new BigDecimal(redisUtils.getString(CoinTypeConvert.convertToKey(symbol + "/USDT")));
@@ -454,9 +469,11 @@
//            memberWalletCoinDao.updateById(walletCoin);
            memberWalletCoinDao.updateWalletBalance(walletCoin.getId(),amount.negate(),amount.negate(),amount);
        }
        // 加入到撮合
        CoinTrader trader = factory.getTrader(symbol);
        trader.trade(order);
        // 加入到撮合 TODO  通过消息队列发送到交易撮合
        //CoinTrader trader = factory.getTrader(symbol);
        //trader.trade(order);
        order.setSymbol(symbol);
        orderSubmitProducer.sendMsg(JSONObject.toJSONString(order));
        return Result.ok(MessageSourceUtils.getString("order_service_0011"));
    }
@@ -498,7 +515,7 @@
        //获取用户ID
        Long memberId = LoginUserUtils.getAppLoginUser().getId();
        OrderCoinsEntity orderCoinsEntity = orderCoinsDao.selectById(orderId);
        if (ObjectUtil.isNotEmpty(orderCoinsEntity) && orderCoinsEntity.getMemberId() == memberId) {
        if (ObjectUtil.isNotEmpty(orderCoinsEntity) && orderCoinsEntity.getMemberId().equals(memberId) ) {
            // 如果是撮合交易单
            if (SymbolsConstats.EXCHANGE_SYMBOLS.contains(orderCoinsEntity.getSymbol())) {
                return this.cancelEntrustWalletCoinOrderForMatch(orderId);
@@ -584,7 +601,7 @@
        // 取消撮合订单的单
        CoinTrader trader = factory.getTrader(orderCoinsEntity.getSymbol());
        trader.cancelOrder(orderCoinsEntity);
        if (ObjectUtil.isNotEmpty(orderCoinsEntity) && orderCoinsEntity.getMemberId() == memberId) {
        if (ObjectUtil.isNotEmpty(orderCoinsEntity) && orderCoinsEntity.getMemberId().equals(memberId)) {
            if (orderCoinsEntity.getOrderStatus() == OrderCoinsEntity.ORDERSTATUS_CANCEL || orderCoinsEntity.getOrderStatus()==OrderCoinsEntity.ORDERSTATUS_DONE) {
                return Result.fail(MessageSourceUtils.getString("order_service_0012"));
            }
@@ -616,6 +633,8 @@
                if (ObjectUtil.isNotEmpty(walletCoin)) {
                    //手续费 = 开仓价*数量*手续费率
                    //返还金额=开仓价*未成交数量+手续费
                    // 市价的按成交额退款
                    BigDecimal returnBalance = orderCoinsEntity.getEntrustAmount().subtract(orderCoinsEntity.getDealAmount());
                    // 需要退回的手续费
                    BigDecimal returnFee = BigDecimal.ZERO;
@@ -698,6 +717,12 @@
        return Result.ok(pageEntityToPageVo);
    }
    @Override
    public Result findAllWalletCoinOrder() {
        List<OrderCoinsDealEntity> orderCoinsDealEntities = orderCoinDealDao.selectAllCoinDealsOrderBySymbol(CoinTypeEnum.ROC.toString());
        return Result.ok(orderCoinsDealEntities);
    }
    @Override
@@ -946,6 +971,13 @@
                    update.setOrderStatus(OrderCoinsEntity.ORDERSTATUS_DONE);
                    update.setUpdateTime(new Date());
                    orderCoinsDao.updateById(update);
                    // 限价买入时,如果成交价比设置的价格低,需要退还多余的冻结
                    OrderCoinsEntity coinsEntity = orderCoinsDao.selectById(buyOrderId);
                    BigDecimal subtract = coinsEntity.getEntrustAmount().subtract(coinsEntity.getDealAmount());
                    if(subtract.compareTo(BigDecimal.ZERO)>=0){
                        // 下单扣的比较多
                        memberWalletCoinDao.updateWalletBalance(coinsEntity.getId(),subtract,subtract,subtract.negate());
                    }
                } else {
                    OrderCoinsEntity update = new OrderCoinsEntity();
                    update.setId(buyOrderId);
@@ -1106,4 +1138,5 @@
        trader.trade(order);
    }
}