KKSU
2024-05-09 7db03fff5f1d089a9c14da450b88d979945df564
src/main/java/cc/mrbird/febs/dapp/service/impl/DappWalletServiceImpl.java
@@ -17,6 +17,7 @@
import cc.mrbird.febs.rabbit.producer.ChainProducer;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
@@ -47,6 +48,7 @@
    private final DappAchieveItemMapper dappAchieveItemMapper;
    private final ChainProducer chainProducer;
    private final AsyncCjService asyncCjService;
    private final DappMemberFundMapper dappMemberFundMapper;
    @Override
    public WalletInfoVo walletInfo() {
@@ -79,11 +81,47 @@
        BigDecimal zyToday = dappFundFlowDao.selectAmountTotalByTypeAndMemberIdAndDate(member.getId(), FlowTypeEnum.ZHI_YA.getValue(), new Date()).negate();
        walletInfo.setZyToday(zyToday);
        BigDecimal ccTotal = dappFundFlowDao.selectAmountTotalByTypeAndMemberIdAndDate(member.getId(), FlowTypeEnum.ZHIYA_CHA_CHU.getValue(),null).negate();
        walletInfo.setCcTotal(ccTotal);
        BigDecimal ccToday = dappFundFlowDao.selectAmountTotalByTypeAndMemberIdAndDate(member.getId(), FlowTypeEnum.ZHIYA_CHA_CHU.getValue(), new Date()).negate();
        QueryWrapper<DappMemberFund> fundQueryWrapper = new QueryWrapper<>();
        fundQueryWrapper.eq("member_id",memberId);
        List<DappMemberFund> ccTodays = dappMemberFundMapper.selectList(fundQueryWrapper);
        BigDecimal ccToday = BigDecimal.ZERO;
        if(CollUtil.isNotEmpty(ccTodays)){
            for(DappMemberFund dappMemberFund : ccTodays){
                BigDecimal usdtCoin = dappMemberFund.getUsdtCoin();
                BigDecimal coinTarget = dappMemberFund.getCoinTarget();
                BigDecimal add = coinTarget.add(usdtCoin);
                ccToday = ccToday.add(add);
            }
        }
        walletInfo.setCcToday(ccToday);
        List<DappMemberFund> dappMemberFunds = dappMemberFundMapper.selectList(null);
        walletInfo.setCcTotal(CollUtil.isEmpty(dappMemberFunds) ? BigDecimal.ZERO :
                dappMemberFunds.stream().map(DappMemberFund::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(8,BigDecimal.ROUND_DOWN));
        List<DappAchieve> dappAchieves = dappAchieveMapper.selectListByMemberId(memberId);
        BigDecimal goldAmountDone = dappAchieveMapper.selectGoldAmountByMemberId(memberId);
        BigDecimal coinAmountDone = dappAchieveMapper.selectCoinAmountByMemberId(memberId);
        if(CollUtil.isNotEmpty(dappAchieves)){
            BigDecimal goldAmount = dappAchieves.stream().map(DappAchieve::getGoldAmount).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(8,BigDecimal.ROUND_DOWN);
            BigDecimal coinAmount = dappAchieves.stream().map(DappAchieve::getCoinAmount).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(8,BigDecimal.ROUND_DOWN);
            BigDecimal goldAmountAva = goldAmount.subtract(goldAmountDone);//金本位剩余
            BigDecimal coinAmountAva = coinAmount.subtract(coinAmountDone);//币本位剩余
            if(goldAmountAva.compareTo(BigDecimal.ZERO) > 0){
                walletInfo.setGoldAmount(goldAmountAva);
            }else{
                walletInfo.setGoldAmount(BigDecimal.ZERO);
            }
            if(coinAmountAva.compareTo(BigDecimal.ZERO) > 0){
                walletInfo.setCoinAmount(coinAmountAva);
            }else{
                walletInfo.setCoinAmount(BigDecimal.ZERO);
            }
        }else{
            walletInfo.setGoldAmount(BigDecimal.ZERO);
            walletInfo.setCoinAmount(BigDecimal.ZERO);
        }
        BigDecimal gfaDays = new BigDecimal(redisUtils.getString(DataDicEnum.GFA_DAYS.getValue())).setScale(2,BigDecimal.ROUND_DOWN);
        walletInfo.setGfaDays(gfaDays);
@@ -166,6 +204,9 @@
        if (recordInPageDto.getType() != null && recordInPageDto.getType() != 0) {
            dappFundFlowEntity.setStatus(recordInPageDto.getType());
        }
        if (recordInPageDto.getState() != null && recordInPageDto.getState() != 0) {
            dappFundFlowEntity.setType(recordInPageDto.getState());
        }
        dappFundFlowEntity.setMemberId(member.getId());
        IPage<DappFundFlowEntity> records = dappFundFlowDao.selectInPages(page, dappFundFlowEntity);
        return records.getRecords();
@@ -235,6 +276,30 @@
    @Transactional(rollbackFor = Exception.class)
    public Long transfer(TransferDto transferDto) {
        DappMemberEntity member = LoginUserUtil.getAppUser();
//        QueryWrapper<DappFundFlowEntity> objectQueryWrapper = new QueryWrapper<>();
//        objectQueryWrapper.eq("type" ,FlowTypeEnum.ZHI_YA.getValue());
//        objectQueryWrapper.eq("status" ,DappFundFlowEntity.WITHDRAW_STATUS_AGREE);
//        objectQueryWrapper.eq("member_id" ,member.getId());
//        List<DappFundFlowEntity> dappFundFlowEntities = dappFundFlowDao.selectList(objectQueryWrapper);
        List<DappFundFlowEntity> dappFundFlowEntities = dappFundFlowDao.selectAmountTotalByTypeAndMemberIdAndDateAndState(
                member.getId(),
                FlowTypeEnum.ZHI_YA.getValue(),
                DappFundFlowEntity.WITHDRAW_STATUS_AGREE,
                new Date());
        BigDecimal zhiyaAmount = new BigDecimal(redisUtils.getString(DataDicEnum.MEMBER_ZHIYA_AMOUNT.getValue()));
        Integer zhiyaTime = Integer.parseInt(redisUtils.getString(DataDicEnum.MEMBER_ZHIYA_TIME.getValue()));
        if(CollUtil.isNotEmpty(dappFundFlowEntities)){
            if(dappFundFlowEntities.size() >= zhiyaTime){
                throw new FebsException("今日暂停质押");
            }
            BigDecimal teamAchieveMemberSum = dappFundFlowEntities.stream().map(DappFundFlowEntity::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add).negate();
            if(zhiyaAmount.compareTo(teamAchieveMemberSum.add(transferDto.getAmount())) < 0){
                throw new FebsException("今日最多质押"+zhiyaAmount.subtract(teamAchieveMemberSum).setScale(4,BigDecimal.ROUND_DOWN));
            }
        }
        String hasStart = redisUtils.getString(DataDicEnum.SYSTEM_START_FLAG.getValue());
        BigDecimal minAmount = new BigDecimal(redisUtils.getString(DataDicEnum.GFA_BUY_MIN_AMOUNT.getValue()));
@@ -253,11 +318,11 @@
                    if (transferDto.getAmount().compareTo(minAmount) < 0) {
                        throw new FebsException("超过购买限制");
                    }
                    if (BigDecimal.ZERO.compareTo(gfaDays) >= 0) {
                        throw new FebsException("今日暂停质押");
                    }
                    if (transferDto.getAmount().compareTo(gfaDays) > 0) {
                        throw new FebsException("今日最多质押"+gfaDays);
//                    if (BigDecimal.ZERO.compareTo(gfaDays) >= 0) {
//                        throw new FebsException("今日暂停质押");
//                    }
                    if (transferDto.getAmount().compareTo(zhiyaAmount) > 0) {
                        throw new FebsException("今日最多质押"+zhiyaAmount);
                    }
                } else {
                    throw new FebsException("暂停质押");
@@ -298,6 +363,7 @@
            //新增一条用户质押记录
            DappAchieve dappAchieve = new DappAchieve();
            dappAchieve.setMemberId(memberId);
            dappAchieve.setPrice(transferDto.getPrice());
            dappAchieve.setAmount(amount);
            BigDecimal achieveReleasePercent = new BigDecimal("0.01").multiply(
                    new BigDecimal(redisUtils.getString(DataDicEnum.GFA_ACHIEVE_RELEASE.getValue())).setScale(2,BigDecimal.ROUND_DOWN)
@@ -307,9 +373,21 @@
            dappAchieve.setAmountMax(achieveOut.multiply(amount).setScale(2,BigDecimal.ROUND_DOWN));
            dappAchieve.setState(DappAchieve.STATUS_ING);
            dappAchieve.setAmountDone(BigDecimal.ZERO);
            /**
             * 计算金本位和币本位
             *  金本位:数量*价格*70*产矿百分比
             *  币本位:数量*30*产矿百分比
             */
            BigDecimal price = dappAchieve.getPrice();
            BigDecimal goldAmount = amount.multiply(price).multiply(new BigDecimal("0.7")).multiply(achieveOut).setScale(8, BigDecimal.ROUND_DOWN);
            BigDecimal coinAmount = amount.multiply(new BigDecimal("0.3")).multiply(achieveOut).setScale(8, BigDecimal.ROUND_DOWN);
            dappAchieve.setGoldAmount(goldAmount);
            dappAchieve.setCoinAmount(coinAmount);
            dappAchieveMapper.insert(dappAchieve);
            asyncCjService.insertTeamPerk(flow.getId(), dappAchieve.getId());
//            asyncCjService.calculateAchieve(dappAchieve);
            //发送一个延时队列消息。24小时之后产生第一次的质押产出
//            chainProducer.sendZhiYaDelayMsg(dappAchieve.getId(), 24 * 60 * 60 * 1000L);
@@ -328,7 +406,14 @@
    @Override
    public IPage<DappAchieve> dappAchieveInPage(DappAchieve dappAchieve, QueryRequest request) {
        Page<DappAchieve> page = new Page<>(request.getPageNum(), request.getPageSize());
        return dappAchieveMapper.selectInPage(page, dappAchieve);
        IPage<DappAchieve> dappAchieveIPage = dappAchieveMapper.selectInPage(page, dappAchieve);
        List<DappAchieve> records = dappAchieveIPage.getRecords();
        if(CollUtil.isNotEmpty(records)){
            for(DappAchieve dappAchieve1 : records){
                dappAchieve1.setUsdtAmount(dappAchieve1.getAmount().multiply(dappAchieve1.getPrice()).setScale(2,BigDecimal.ROUND_DOWN));
            }
        }
        return dappAchieveIPage;
    }
    @Override
@@ -356,8 +441,11 @@
            }
            flowType = FlowTypeEnum.DAI_BI_OUT.getValue();
            flowDes = FlowTypeEnum.DAI_BI_OUT.getDescrition();
            dappMemberEntity.setBalance(balance.subtract(amount).setScale(2,BigDecimal.ROUND_DOWN));
            dappMemberDao.updateBalanceWithVersion(dappMemberEntity);
        }else{
            if(new BigDecimal("100").compareTo(amount) > 0){
            if(new BigDecimal("1").compareTo(amount) > 0){
                throw new FebsException("最小数量为100");
            }
            if(usdtBalance.compareTo(amount) < 0){
@@ -365,6 +453,9 @@
            }
            flowType = FlowTypeEnum.USDT_OUT.getValue();
            flowDes = FlowTypeEnum.USDT_OUT.getDescrition();
            dappMemberEntity.setUsdtBalance(usdtBalance.subtract(amount).setScale(2,BigDecimal.ROUND_DOWN));
            dappMemberDao.updateUsdtBalanceWithVersion(dappMemberEntity);
        }
        DappFundFlowEntity fundFlowTuiJian = new DappFundFlowEntity(
@@ -377,10 +468,20 @@
                dappMemberEntity.getAddress());
        dappFundFlowDao.insert(fundFlowTuiJian);
        dappMemberEntity.setBalance(balance.subtract(amount).setScale(2,BigDecimal.ROUND_DOWN));
        dappMemberEntity.setUsdtBalance(usdtBalance.subtract(amount).setScale(2,BigDecimal.ROUND_DOWN));
        dappMemberDao.updateBalanceAndUsdtBalanceWithVersion(dappMemberEntity);
//        dappMemberEntity.setBalance(balance.subtract(amount).setScale(2,BigDecimal.ROUND_DOWN));
//        dappMemberEntity.setUsdtBalance(usdtBalance.subtract(amount).setScale(2,BigDecimal.ROUND_DOWN));
//        dappMemberDao.updateBalanceAndUsdtBalanceWithVersion(dappMemberEntity);
        return new FebsResponse().success();
    }
    @Override
    public List<DappMemberFund> memberFundList(RecordInPageDto recordInPageDto) {
        DappMemberEntity member = LoginUserUtil.getAppUser();
        Page<DappMemberFund> page = new Page<>(recordInPageDto.getPageNum(), recordInPageDto.getPageSize());
        DappMemberFund dappMemberFund = new DappMemberFund();
        dappMemberFund.setMemberId(member.getId());
        IPage<DappMemberFund> records = dappFundFlowDao.memberFundList(page, dappMemberFund);
        return records.getRecords();
    }
}