From c6f0d2d2b3fd0af17673690cabdb95018fc4d761 Mon Sep 17 00:00:00 2001
From: zainali5120 <512061637@qq.com>
Date: Mon, 19 Oct 2020 15:30:41 +0800
Subject: [PATCH] 优化卖出限制,使用redis配置进行控制
---
src/main/java/com/xcong/excoin/modules/coin/service/impl/OrderCoinServiceImpl.java | 44 +++++++++++++++++++++++++++++++-------------
1 files changed, 31 insertions(+), 13 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 4583faf..86b919e 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
@@ -7,11 +7,13 @@
import javax.annotation.Resource;
+import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xcong.excoin.common.contants.AppContants;
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.coin.service.CoinService;
import com.xcong.excoin.modules.member.dao.MemberDao;
import com.xcong.excoin.modules.member.entity.MemberEntity;
import com.xcong.excoin.modules.platform.entity.PlatformCnyUsdtExchangeEntity;
@@ -96,6 +98,9 @@
@Resource
private OrderSubmitProducer orderSubmitProducer;
+
+ @Resource
+ private CoinService coinService;
@Override
@@ -350,7 +355,7 @@
}
BigDecimal nowPriceinBigDecimal = price;
//查询当前价
- //BigDecimal nowPrice = new BigDecimal(redisUtils.getString(CoinTypeConvert.convertToKey(symbol + "/USDT")));
+ BigDecimal nowPrice = new BigDecimal(redisUtils.getString(CoinTypeConvert.convertToKey(symbol + "/USDT")));
// 获取交易管理的杠杠倍率,手续费率等信息,由平台进行设置
symbol = symbol.toUpperCase();
@@ -381,6 +386,16 @@
closingPrice = price.multiply(amount).multiply(tradeSetting.getCoinFeeRatio());
totalPayPrice = price.multiply(amount).add(closingPrice);
entrustAmount = price.multiply(amount);
+ // 限价买不能高于当前10%
+ BigDecimal multiply = nowPrice.multiply(new BigDecimal("1.2"));
+ if (price.compareTo(multiply) > 0) {
+ return Result.fail("不能高于当前价的120%");
+ }
+ multiply= nowPrice.multiply(new BigDecimal("0.8"));
+ if (price.compareTo(multiply) < 0) {
+ return Result.fail("不能低于当前价的80%");
+ }
+
} else {
// 市价
if (OrderCoinsEntity.ORDERTYPE_BUY == type) {
@@ -453,10 +468,10 @@
//冻结相应的资产
if (OrderCoinsEntity.ORDERTYPE_BUY.equals(type)) {
//如果是买入,所对应的币种增加,USDT账户减少金额
- memberWalletCoinDao.updateWalletBalance(walletCoinUsdt.getId(), totalPayPrice.negate(), totalPayPrice.negate(), entrustAmount);
+ coinService.updateWalletBalance(walletCoinUsdt.getId(), totalPayPrice.negate(), totalPayPrice.negate(), entrustAmount);
} else {
//如果是卖出,币种减少,USDT增加
- memberWalletCoinDao.updateWalletBalance(walletCoin.getId(), amount.negate(), amount.negate(), amount);
+ coinService.updateWalletBalance(walletCoin.getId(), amount.negate(), amount.negate(), amount);
}
// 加入到撮合
order.setSymbol(symbol);
@@ -659,7 +674,7 @@
returnFee = orderCoinsEntity.getFeeAmount().subtract(needFee);
}
BigDecimal avi = returnBalance.add(returnFee);
- memberWalletCoinDao.updateWalletBalance(walletCoin.getId(), avi, null, returnBalance.negate());
+ coinService.updateWalletBalance(walletCoin.getId(), avi, null, returnBalance.negate());
walletCoin.setAvailableBalance(walletCoin.getAvailableBalance().add(returnBalance).add(returnFee));
walletCoin.setFrozenBalance(walletCoin.getFrozenBalance().subtract(returnBalance));
//memberWalletCoinDao.updateById(walletCoin);
@@ -683,7 +698,7 @@
walletCoin.setAvailableBalance(walletCoin.getAvailableBalance().add(returnBalance));
walletCoin.setFrozenBalance(walletCoin.getFrozenBalance().subtract(returnBalance));
//memberWalletCoinDao.updateById(walletCoin);
- memberWalletCoinDao.updateWalletBalance(walletCoin.getId(), returnBalance, null, returnBalance.negate());
+ coinService.updateWalletBalance(walletCoin.getId(), returnBalance, null, returnBalance.negate());
// 流水记录
MemberAccountFlowEntity record = new MemberAccountFlowEntity();
record.setSource(MemberAccountFlowEntity.SOURCE_CANCEL);
@@ -973,12 +988,12 @@
MemberWalletCoinEntity usdtWallet = memberWalletCoinDao.selectWalletCoinBymIdAndCode(buyOrderCoinsEntity.getMemberId(), MemberWalletCoinEnum.WALLETCOINCODE.getValue());
if (usdtWallet != null) {
// 减少usdt冻结
- memberWalletCoinDao.updateWalletBalance(usdtWallet.getId(), null, null, buyTurnover.negate());
+ coinService.updateWalletBalance(usdtWallet.getId(), null, null, buyTurnover.negate());
}
// 增加买的币
MemberWalletCoinEntity buySymbolWallet = memberWalletCoinDao.selectWalletCoinBymIdAndCode(buyOrderCoinsEntity.getMemberId(), buyOrderCoinsEntity.getSymbol());
if (buySymbolWallet != null) {
- memberWalletCoinDao.updateWalletBalance(buySymbolWallet.getId(), amount, amount, null);
+ coinService.updateWalletBalance(buySymbolWallet.getId(), amount, amount, null);
}
// 流水记录
MemberAccountFlowEntity record = new MemberAccountFlowEntity();
@@ -1017,12 +1032,12 @@
MemberWalletCoinEntity memberWalletCoinEntity = memberWalletCoinDao.selectWalletCoinBymIdAndCode(sellOrderCoinsEntity.getMemberId(), sellOrderCoinsEntity.getSymbol());
if (memberWalletCoinEntity != null) {
// 更新卖币减少的币种
- memberWalletCoinDao.updateWalletBalance(memberWalletCoinEntity.getId(), null, null, amount.negate());
+ coinService.updateWalletBalance(memberWalletCoinEntity.getId(), null, null, amount.negate());
}
// 更新卖币得到的usdt
MemberWalletCoinEntity sellWalletCoinEntity = memberWalletCoinDao.selectWalletCoinBymIdAndCode(sellOrderCoinsEntity.getMemberId(), MemberWalletCoinEnum.WALLETCOINCODE.getValue());
if (sellOrderCoinsEntity != null) {
- memberWalletCoinDao.updateWalletBalance(sellWalletCoinEntity.getId(), buyTurnover, buyTurnover, null);
+ coinService.updateWalletBalance(sellWalletCoinEntity.getId(), buyTurnover, buyTurnover, null);
}
// 流水记录
MemberAccountFlowEntity recordSell = new MemberAccountFlowEntity();
@@ -1045,16 +1060,19 @@
if (CollectionUtils.isNotEmpty(trades)) {
for (OrderCoinsEntity trade : trades) {
if (trade != null) {
+ if (trade.getOrderType() == 2 && trade.getEntrustCnt().compareTo(trade.getDealCnt()) != 0) {
+ System.out.println("问题卖单:" + JSON.toJSONString(trade));
+ }
//orderCoinsDao.updateStatus(trade.getId(),OrderCoinsEntity.ORDERSTATUS_DONE);
ids.add(trade.getId());
// 买单 实际成交金额小于委托的 这一部分从冻结扣除
- if(OrderCoinsEntity.ORDERTYPE_BUY==trade.getOrderType()){
- if(trade.getEntrustAmount().compareTo(trade.getDealAmount())>0){
+ if (OrderCoinsEntity.ORDERTYPE_BUY == trade.getOrderType()) {
+ if (trade.getEntrustAmount().compareTo(trade.getDealAmount()) > 0) {
// 此时退回这部分的差额
BigDecimal subtract = trade.getEntrustAmount().subtract(trade.getDealAmount());
MemberWalletCoinEntity memberWalletCoinEntity = memberWalletCoinDao.selectWalletCoinBymIdAndCode(trade.getMemberId(), CoinTypeEnum.USDT.name());
- if(memberWalletCoinEntity!=null){
- memberWalletCoinDao.updateWalletBalance(memberWalletCoinEntity.getId(),subtract,null,subtract.negate());
+ if (memberWalletCoinEntity != null) {
+ coinService.updateWalletBalance(memberWalletCoinEntity.getId(), subtract, null, subtract.negate());
}
}
}
--
Gitblit v1.9.1