From f28d8076108296c387ed7e8f1bf819c060a6c8a2 Mon Sep 17 00:00:00 2001
From: zainali5120 <512061637@qq.com>
Date: Sat, 10 Oct 2020 22:56:57 +0800
Subject: [PATCH] golden交易所分布式支持

---
 src/main/java/com/xcong/excoin/modules/coin/service/impl/OrderCoinServiceImpl.java |  109 ++++++++++++++++++------------------------------------
 1 files changed, 36 insertions(+), 73 deletions(-)

diff --git a/src/main/java/com/xcong/excoin/modules/coin/service/impl/OrderCoinServiceImpl.java b/src/main/java/com/xcong/excoin/modules/coin/service/impl/OrderCoinServiceImpl.java
index a8e174e..712312f 100644
--- a/src/main/java/com/xcong/excoin/modules/coin/service/impl/OrderCoinServiceImpl.java
+++ b/src/main/java/com/xcong/excoin/modules/coin/service/impl/OrderCoinServiceImpl.java
@@ -518,7 +518,9 @@
         if (ObjectUtil.isNotEmpty(orderCoinsEntity) && orderCoinsEntity.getMemberId().equals(memberId) ) {
             // 如果是撮合交易单
             if (SymbolsConstats.EXCHANGE_SYMBOLS.contains(orderCoinsEntity.getSymbol())) {
-                return this.cancelEntrustWalletCoinOrderForMatch(orderId);
+                orderSubmitProducer.sendCancelMsg(orderId);
+               // return this.cancelEntrustWalletCoinOrderForMatch(orderId);
+                return Result.ok("order_service_0013");
             }
             if (orderCoinsEntity.getOrderStatus() == OrderCoinsEntity.ORDERSTATUS_CANCEL || orderCoinsEntity.getOrderStatus()==OrderCoinsEntity.ORDERSTATUS_DONE) {
                 return Result.fail(MessageSourceUtils.getString("order_service_0012"));
@@ -596,12 +598,15 @@
     @Transactional
     public Result cancelEntrustWalletCoinOrderForMatch(String orderId) {
         //获取用户ID
-        Long memberId = LoginUserUtils.getAppLoginUser().getId();
         OrderCoinsEntity orderCoinsEntity = orderCoinsDao.selectById(orderId);
+        if(orderCoinsEntity==null){
+            return Result.ok("");
+        }
+        Long memberId = orderCoinsEntity.getMemberId();
         // 取消撮合订单的单
         CoinTrader trader = factory.getTrader(orderCoinsEntity.getSymbol());
         trader.cancelOrder(orderCoinsEntity);
-        if (ObjectUtil.isNotEmpty(orderCoinsEntity) && orderCoinsEntity.getMemberId().equals(memberId)) {
+        if (ObjectUtil.isNotEmpty(orderCoinsEntity) ) {
             if (orderCoinsEntity.getOrderStatus() == OrderCoinsEntity.ORDERSTATUS_CANCEL || orderCoinsEntity.getOrderStatus()==OrderCoinsEntity.ORDERSTATUS_DONE) {
                 return Result.fail(MessageSourceUtils.getString("order_service_0012"));
             }
@@ -633,21 +638,31 @@
                 if (ObjectUtil.isNotEmpty(walletCoin)) {
                     //手续费 = 开仓价*数量*手续费率
                     //返还金额=开仓价*未成交数量+手续费
-
+                    // 这里根据成交的单计算
+                    List<OrderCoinsDealEntity> orderCoinsDealEntities = orderCoinDealDao.selectCoinOrderDealByOrderId(Long.valueOf(orderId));
+                    BigDecimal dealAmount = BigDecimal.ZERO;
+                    if(CollectionUtils.isNotEmpty(orderCoinsDealEntities)){
+                        for (OrderCoinsDealEntity orderCoinsDealEntity : orderCoinsDealEntities) {
+                            dealAmount = dealAmount.add(orderCoinsDealEntity.getDealAmount());
+                        }
+                    }
                     // 市价的按成交额退款
-                    BigDecimal returnBalance = orderCoinsEntity.getEntrustAmount().subtract(orderCoinsEntity.getDealAmount());
+                    BigDecimal returnBalance = orderCoinsEntity.getEntrustAmount().subtract(dealAmount);
+
                     // 需要退回的手续费
                     BigDecimal returnFee = BigDecimal.ZERO;
                     if (returnBalance.compareTo(orderCoinsEntity.getEntrustAmount()) == 0) {
                         returnFee = orderCoinsEntity.getFeeAmount();
                     } else {
                         // 按比例退回
-                        BigDecimal needFee = orderCoinsEntity.getDealAmount().divide(orderCoinsEntity.getEntrustAmount(), 8, BigDecimal.ROUND_DOWN).multiply(orderCoinsEntity.getFeeAmount());
+                        BigDecimal needFee = orderCoinsEntity.getFeeAmount().multiply(dealAmount.divide(orderCoinsEntity.getEntrustAmount(), 8, BigDecimal.ROUND_DOWN));
                         returnFee = orderCoinsEntity.getFeeAmount().subtract(needFee);
                     }
+                    BigDecimal avi = walletCoin.getAvailableBalance().add(returnBalance).add(returnFee);
+                    memberWalletCoinDao.updateWalletBalance(walletCoin.getId(),avi,null,returnBalance.negate());
                     walletCoin.setAvailableBalance(walletCoin.getAvailableBalance().add(returnBalance).add(returnFee));
                     walletCoin.setFrozenBalance(walletCoin.getFrozenBalance().subtract(returnBalance));
-                    memberWalletCoinDao.updateById(walletCoin);
+                    //memberWalletCoinDao.updateById(walletCoin);
                     // 流水记录
                     MemberAccountFlowEntity record = new MemberAccountFlowEntity();
                     record.setSource(MemberAccountFlowEntity.SOURCE_CANCEL);
@@ -938,15 +953,24 @@
             }
             Long memberId = buyOrderCoinsEntity.getMemberId();
             if (buyOrderCoinsEntity != null) {
+                List<OrderCoinsDealEntity> orderCoinsDealEntities = orderCoinDealDao.selectCoinOrderDealByOrderId(buyOrderId);
                 // 比较剩余的量
-                BigDecimal dealAmount = buyOrderCoinsEntity.getDealAmount();
+                BigDecimal dealAmount = BigDecimal.ZERO;
+                BigDecimal dealCnt = BigDecimal.ZERO;
+                if(CollectionUtils.isNotEmpty(orderCoinsDealEntities)){
+                    for (OrderCoinsDealEntity orderCoinsDealEntity : orderCoinsDealEntities) {
+                        dealAmount=dealAmount.add(orderCoinsDealEntity.getDealAmount());
+                        dealCnt = dealCnt.add(orderCoinsDealEntity.getSymbolCnt());
+                    }
+                }
+
                 // 单的总金额
                 BigDecimal entrustAmount = buyOrderCoinsEntity.getEntrustAmount();
                 BigDecimal add = dealAmount.add(buyTurnover);
                 BigDecimal closingPrice = buyTurnover.multiply(new BigDecimal("0.002"));
 
-                //成交量
-                BigDecimal dealCnt = buyOrderCoinsEntity.getDealCnt().add(amount);
+                //成交总量
+                 dealCnt = dealCnt.add(amount);
                 // 创建一个完成的单
                 OrderCoinsDealEntity detail = new OrderCoinsDealEntity();
                 detail.setMemberId(buyOrderCoinsEntity.getMemberId());
@@ -979,12 +1003,8 @@
                         memberWalletCoinDao.updateWalletBalance(coinsEntity.getId(),subtract,subtract,subtract.negate());
                     }
                 } else {
-                    OrderCoinsEntity update = new OrderCoinsEntity();
-                    update.setId(buyOrderId);
-                    update.setDealAmount(add);
-                    update.setDealCnt(buyOrderCoinsEntity.getDealCnt().add(amount));
-                    update.setUpdateTime(new Date());
-                    orderCoinsDao.updateById(update);
+                    // 更新买单
+                    orderCoinsDao.updateDeal(buyOrderId,amount,buyTurnover);
                 }
             }
             // 卖单
@@ -1078,64 +1098,7 @@
     @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);
     }
 
 

--
Gitblit v1.9.1