From 53bc6ea0abbeb39146502a40292e6562c5a736d7 Mon Sep 17 00:00:00 2001 From: zainali5120 <512061637@qq.com> Date: Sat, 17 Oct 2020 16:27:32 +0800 Subject: [PATCH] 修复交易问题 --- src/main/java/com/xcong/excoin/modules/coin/service/impl/OrderCoinServiceImpl.java | 41 +++++++++++++++++++++++++++++++---------- 1 files changed, 31 insertions(+), 10 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 739c9d8..b59e970 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 @@ -382,7 +387,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(); @@ -413,6 +418,19 @@ closingPrice = price.multiply(amount).multiply(tradeSetting.getCoinFeeRatio()); totalPayPrice = price.multiply(amount).add(closingPrice); entrustAmount = price.multiply(amount); + // 限价买不能高于当前10% + if (OrderCoinsEntity.ORDERTYPE_BUY == type) { + BigDecimal multiply = nowPrice.multiply(new BigDecimal("1.2")); + if(price.compareTo(multiply)>0){ + return Result.fail("不能高于当前价的120%"); + } + }else{ + // 卖不能小于当前90 + BigDecimal multiply = nowPrice.multiply(new BigDecimal("0.8")); + if(price.compareTo(multiply)<0){ + return Result.fail("不能低于当前价的80%"); + } + } } else { // 市价 if (OrderCoinsEntity.ORDERTYPE_BUY == type) { @@ -485,10 +503,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); @@ -691,7 +709,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); @@ -715,7 +733,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); @@ -1005,12 +1023,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(); @@ -1049,12 +1067,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(); @@ -1077,6 +1095,9 @@ 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()); // 买单 实际成交金额小于委托的 这一部分从冻结扣除 @@ -1086,7 +1107,7 @@ 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()); + coinService.updateWalletBalance(memberWalletCoinEntity.getId(),subtract,null,subtract.negate()); } } } -- Gitblit v1.9.1