From 43385a77303aa2295d902b5a2be9e28723d08cef Mon Sep 17 00:00:00 2001
From: zainali5120 <512061637@qq.com>
Date: Mon, 12 Oct 2020 16:00:21 +0800
Subject: [PATCH] Merge branches 'cpv' and 'feature/撮合交易' of https://gitee.com/chonggaoxiao/new_excoin into cpv
---
src/main/java/com/xcong/excoin/modules/coin/service/impl/OrderCoinServiceImpl.java | 67 +++++++++++++++++++++++----------
1 files changed, 46 insertions(+), 21 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 9a2c740..35ee332 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
@@ -17,6 +17,7 @@
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;
@@ -91,6 +92,9 @@
@Resource
private MemberDao memberDao;
+
+ @Resource
+ private OrderSubmitProducer orderSubmitProducer;
@Override
@@ -465,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"));
}
@@ -512,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(MessageSourceUtils.getString("order_service_0013"));
}
if (orderCoinsEntity.getOrderStatus() == OrderCoinsEntity.ORDERSTATUS_CANCEL || orderCoinsEntity.getOrderStatus()==OrderCoinsEntity.ORDERSTATUS_DONE) {
return Result.fail(MessageSourceUtils.getString("order_service_0012"));
@@ -590,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"));
}
@@ -617,8 +628,6 @@
detail.setDealPrice(orderCoinsEntity.getDealPrice());
detail.setDealAmount(orderCoinsEntity.getDealAmount());
detail.setFeeAmount(orderCoinsEntity.getFeeAmount());
- orderCoinDealDao.insert(detail);
-
if (OrderCoinsEntity.ORDERTYPE_BUY.equals(orderCoinsEntity.getOrderType())) {
//如果是限价买入,撤单将USDT账户冻结金额返回
String walletCode = MemberWalletCoinEnum.WALLETCOINCODE.getValue();
@@ -627,21 +636,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 = 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);
@@ -674,6 +693,7 @@
return Result.ok(MessageSourceUtils.getString("order_service_0013"));
}
}
+ orderCoinDealDao.insert(detail);
}
return Result.fail(MessageSourceUtils.getString("order_service_0043"));
}
@@ -932,15 +952,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());
@@ -973,12 +1002,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);
}
}
// 卖单
--
Gitblit v1.9.1