From 83e2fd8d61d116ce9bf2034e8d05346910f96260 Mon Sep 17 00:00:00 2001 From: zainali5120 <512061637@qq.com> Date: Sun, 29 Nov 2020 13:23:15 +0800 Subject: [PATCH] 卖出额度限制 --- src/main/java/com/xcong/excoin/modules/coin/service/impl/OrderCoinServiceImpl.java | 70 ++++++++++++++++++++++------------- 1 files changed, 44 insertions(+), 26 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 0e6d092..545f350 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 @@ -1,6 +1,7 @@ package com.xcong.excoin.modules.coin.service.impl; import java.math.BigDecimal; +import java.math.RoundingMode; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; @@ -11,6 +12,7 @@ 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.DateUtil; import com.xcong.excoin.modules.blackchain.service.RocService; import com.xcong.excoin.modules.coin.mapper.OrderCoinsDealMapper; import com.xcong.excoin.modules.coin.service.CoinService; @@ -101,6 +103,13 @@ @Resource private CoinService coinService; + + private static BigDecimal PERSENT = new BigDecimal("100"); + + /** + * 每日卖出限制 日期 20201128 会员ID + */ + String LIMIT_SELL_PERSENT_KEY ="SELL:PERSENT:%s:%s"; @Override @@ -360,46 +369,25 @@ // 禁止挂卖 String string = redisUtils.getString("BUY_LIMIT_KEY_PHONE"); if (StringUtils.isNotBlank(string) && StringUtils.isNotBlank(phone) && string.contains(phone)) { - return Result.fail("买入受限"); + return Result.fail(""); } if (StringUtils.isNotBlank(string) && StringUtils.isNotBlank(memberEntity.getEmail()) && string.contains(memberEntity.getEmail())) { - return Result.fail("买入受限"); + return Result.fail(""); } // 不能超过800个 - -// if (amount != null && amount.compareTo(new BigDecimal("800")) > 0) { -// return Result.fail("买入额度受限"); -// } -// BigDecimal bigDecimal = orderCoinDealDao.sumTodayBuyAmount(memberId, symbol); -// if (bigDecimal == null) { -// bigDecimal = BigDecimal.ZERO; -// } -// amount = amount == null ? BigDecimal.ZERO : amount; -// bigDecimal = bigDecimal.add(amount); -// if (bigDecimal != null && bigDecimal.compareTo(new BigDecimal("800")) > 0) { -// return Result.fail("买入额度受限"); -// } -// // 挂单不能超过800 -// BigDecimal bigDecimal1 = orderCoinDealDao.sumTodayEntrustCntBuyAmount(memberId, symbol); -// if (bigDecimal1 == null) { -// bigDecimal1 = BigDecimal.ZERO; -// } -// bigDecimal1 = bigDecimal1.add(amount); -// if (bigDecimal1 != null && bigDecimal1.compareTo(new BigDecimal("800")) > 0) { -// return Result.fail("买入额度受限"); -// } } else { // 判断redis开关 //String string = redisUtils.getString("SELL_LIMIT_KEY"); String string = redisUtils.getString("SELL_LIMIT_KEY_PHONE"); if (StringUtils.isNotBlank(string) && StringUtils.isNotBlank(phone) && string.contains(phone)) { - return Result.fail("卖出受限"); + return Result.fail(""); } if (StringUtils.isNotBlank(string) && StringUtils.isNotBlank(memberEntity.getEmail()) && string.contains(memberEntity.getEmail())) { - return Result.fail("卖出受限"); + return Result.fail(""); } + } } @@ -466,6 +454,26 @@ BigDecimal availableBalance = walletCoin.getAvailableBalance(); if (amount.compareTo(availableBalance) > 0) { return Result.fail(MessageSourceUtils.getString("order_service_0010")); + } + // 控制这个用户每日的挂卖额度 TODO + // 每天3% 第一次的为基数 + BigDecimal spread = memberEntity.getSpread(); + if(spread!=null && spread.compareTo(BigDecimal.ZERO)>0 && spread.compareTo(PERSENT)<0){ + // 获取当日的额度 + String yyyyMMdd = DateUtil.dateToString(new Date(), "yyyyMMdd"); + String persentKey = String.format(LIMIT_SELL_PERSENT_KEY,yyyyMMdd,memberId.toString()); + BigDecimal aviSell =BigDecimal.ZERO; + if(StringUtils.isBlank(persentKey)){ + // 当日可以卖的数量 + aviSell = spread.divide(PERSENT,4, RoundingMode.HALF_UP).multiply(availableBalance); + } + if(aviSell.compareTo(amount)<0){ + return Result.fail("卖出超过上限"+spread+"%"); + } + aviSell = aviSell.subtract(amount); + // 设置 + redisUtils.set(persentKey,aviSell.toPlainString()); + //redisUtils.getString() } } @@ -746,11 +754,21 @@ return Result.ok(MessageSourceUtils.getString("order_service_0013")); } } else { + //如果是限价卖出,撤单将对应的钱包冻结金额返回 MemberWalletCoinEntity walletCoin = memberWalletCoinDao.selectWalletCoinBymIdAndCode(memberId, symbol); if (ObjectUtil.isNotEmpty(walletCoin)) { // 卖出按卖出的数量计算手续费 BigDecimal returnBalance = orderCoinsEntity.getEntrustCnt().subtract(coinsEntityCancel.getDealCnt()); + // 取消订单 则退回额度 + String yyyyMMdd = DateUtil.dateToString(new Date(), "yyyyMMdd"); + String persentKey = String.format(LIMIT_SELL_PERSENT_KEY,yyyyMMdd,memberId.toString()); + String aviSell = redisUtils.getString(persentKey); + // TODO + if(StringUtils.isNotBlank(aviSell)){ + BigDecimal needSub = new BigDecimal(aviSell).add(returnBalance); + redisUtils.set(persentKey,needSub.toPlainString()); + } walletCoin.setAvailableBalance(walletCoin.getAvailableBalance().add(returnBalance)); walletCoin.setFrozenBalance(walletCoin.getFrozenBalance().subtract(returnBalance)); //memberWalletCoinDao.updateById(walletCoin); -- Gitblit v1.9.1