xiaoyong931011
2022-12-05 d42cd9ab4ced3182a2a57132e1fac4ed85a2cad4
src/main/java/cc/mrbird/febs/dapp/service/impl/DappSystemServiceImpl.java
@@ -4,17 +4,26 @@
import cc.mrbird.febs.common.utils.LoginUserUtil;
import cc.mrbird.febs.common.utils.RedisUtils;
import cc.mrbird.febs.common.utils.SpringContextUtil;
import cc.mrbird.febs.dapp.chain.ChainEnum;
import cc.mrbird.febs.dapp.chain.ChainService;
import cc.mrbird.febs.dapp.dto.SystemDto;
import cc.mrbird.febs.dapp.dto.TransferDto;
import cc.mrbird.febs.dapp.entity.*;
import cc.mrbird.febs.dapp.enumerate.DataDictionaryEnum;
import cc.mrbird.febs.dapp.enumerate.LevelProfitEnum;
import cc.mrbird.febs.dapp.mapper.*;
import cc.mrbird.febs.dapp.service.DappSystemService;
import cc.mrbird.febs.dapp.service.DappWalletService;
import cc.mrbird.febs.rabbit.producer.ChainProducer;
import cc.mrbird.febs.tree.MatrixTree;
import cc.mrbird.febs.tree.MemberNode;
import cc.mrbird.febs.tree.TreeConstants;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@@ -41,6 +50,9 @@
    private final DappAchieveTreeDao dappAchieveTreeDao;
    private final DappAchieveMemberTreeDao dappAchieveMemberTreeDao;
    private final DappWalletService dappWalletService;
    private final DataDictionaryCustomMapper dataDictionaryCustomMapper;
    private final DappSystemProfitDao dappSystemProfitDao;
    private final ChainProducer chainProducer;
    @Override
@@ -163,8 +175,8 @@
        DappMemberEntity parent = dappMemberDao.selectMemberInfoByInviteId(member.getRefererId());
        if (parent != null) {
            BigDecimal directProfit = new BigDecimal("100").multiply(new BigDecimal("0.15"));
            dappWalletService.updateWalletCoinWithLock(directProfit, parent.getId());
            BigDecimal directProfit = TreeConstants.PUT_IN_AMOUNT.multiply(new BigDecimal("0.1"));
            dappWalletService.updateWalletCoinWithLock(directProfit, parent.getId(), 1);
            DappFundFlowEntity fundFlow = new DappFundFlowEntity(parent.getId(), directProfit, 3, 2, null, null);
            dappFundFlowDao.insert(fundFlow);
@@ -172,29 +184,42 @@
        // 如果是复投的,则需奖励
        if (isReIn == 1) {
            member.setOutCnt(member.getOutCnt() + 1);
            dappMemberDao.updateById(member);
            List<DappMemberEntity> directCnt = dappMemberDao.selectChildMemberDirectOrNot(member.getInviteId(), 1, 1);
            dappWalletService.updateWalletCoinWithLock(new BigDecimal(190), member.getId());
            /**
             * 复投后,推一个拿一轮,推两个拿两轮,推三个后终身有效。
             * 200u单。
             * 无直推则收益200U
             * 一人加一轮400u,二人二轮
             * 三人永久。
             */
            if (directCnt.size() < 3 && member.getOutCnt() != 0 && member.getOutCnt() - 1 == directCnt.size()) {
                return;
            } else {
                BigDecimal amount = null;
                if (directCnt.size() < 1) {
                    amount = TreeConstants.PUT_IN_AMOUNT;
                } else {
                    amount = TreeConstants.FULL_PROFIT_AMOUNT;
                }
            DappFundFlowEntity matrixProfitFlow = new DappFundFlowEntity(memberId, new BigDecimal("190"), 2, 2, null, null);
            dappFundFlowDao.insert(matrixProfitFlow);
                member.setOutCnt(member.getOutCnt() + 1);
                dappMemberDao.updateById(member);
            DappFundFlowEntity rePutInFlow = new DappFundFlowEntity(memberId, new BigDecimal("100").negate(), 1, 2, null, null);
                dappWalletService.updateWalletCoinWithLock(amount, member.getId(), 1);
                DappFundFlowEntity matrixProfitFlow = new DappFundFlowEntity(memberId, amount, 2, 2, null, null);
                dappFundFlowDao.insert(matrixProfitFlow);
            }
            DappFundFlowEntity rePutInFlow = new DappFundFlowEntity(memberId, TreeConstants.PUT_IN_AMOUNT.negate(), 1, 2, null, null);
            dappFundFlowDao.insert(rePutInFlow);
            DappFundFlowEntity safePoolFlow = new DappFundFlowEntity(memberId, new BigDecimal("10").negate(), 4, 2, null, null);
            dappFundFlowDao.insert(safePoolFlow);
            DappMineDataEntity mineData = dappSystemDao.selectMineDataForOne();
            mineData.setSafePool(mineData.getSafePool().add(BigDecimal.TEN));
            dappSystemDao.updateMineData(mineData);
        }
    }
    @Override
    public void tfcNewPrice(String data) {
        redisUtils.set(AppContants.REDIS_KEY_TFC_NEW_PRICE, new BigDecimal(data));
//        redisUtils.set(AppContants.REDIS_KEY_TFC_NEW_PRICE, new BigDecimal(data));
    }
    @Override
@@ -215,7 +240,7 @@
            BigDecimal preAmount = mineDataEntity.getSafePool().divide(new BigDecimal(list.size()), 2, RoundingMode.HALF_UP);
            list.forEach(item -> {
                dappWalletService.updateWalletCoinWithLock(preAmount, item.getMidNode());
                dappWalletService.updateWalletCoinWithLock(preAmount, item.getMidNode(), 1);
                DappFundFlowEntity flow = new DappFundFlowEntity(item.getMidNode(), preAmount, 8, 2, null, null);
                dappFundFlowDao.insert(flow);
            });
@@ -228,4 +253,265 @@
        dappAchieveMemberTreeDao.emptyTable();
        dappMemberDao.updateMemberActive();
    }
    @Override
    public void feeDistribute(String data) {
        if (StrUtil.isBlank(data)) {
            return;
        }
        Long flowId = Long.parseLong(data);
        DappFundFlowEntity fundFlow = dappFundFlowDao.selectById(flowId);
        if (fundFlow == null) {
            return;
        }
        DataDictionaryCustom memberFeeDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.MEMBER_FEE.getType(), DataDictionaryEnum.MEMBER_FEE.getCode());
        if (memberFeeDic == null) {
            log.info("未设置手续费返利比例");
            return;
        }
        List<DappMemberEntity> memberList = dappMemberDao.selectMemberListNeedProfit();
        if (CollUtil.isEmpty(memberList)) {
            return;
        }
        BigDecimal feeReturnRatio = new BigDecimal(memberFeeDic.getValue());
        int size = memberList.size();
        BigDecimal totalProfit = fundFlow.getFee().multiply(feeReturnRatio.divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_DOWN));
        BigDecimal perProfit = totalProfit.divide(BigDecimal.valueOf(size), 8, RoundingMode.HALF_DOWN);
        memberList.forEach(item -> {
            dappWalletService.updateWalletMineWithLock(perProfit, item.getId(), 1);
            DappFundFlowEntity profitFlow = new DappFundFlowEntity(item.getId(), perProfit, 12, 2, null, null);
            dappFundFlowDao.insert(profitFlow);
        });
    }
    @Override
    public void levelProfit(Long id) {
        DappSystemProfit dappSystemProfit = dappSystemProfitDao.selectById(id);
        if(ObjectUtil.isEmpty(dappSystemProfit)){
            return;
        }
        Integer levelProfitState = dappSystemProfit.getLevelProfit();
        if(DappSystemProfit.ENUM_YES == levelProfitState){
            return;
        }
        Long memberId = dappSystemProfit.getMemberId();
        //获取用户的上级用户信息
        DappMemberEntity dappMemberEntity = dappMemberDao.selectById(memberId);
        if(ObjectUtil.isEmpty(dappMemberEntity)){
            return;
        }
        String refererIds = dappMemberEntity.getRefererIds();
        if(StrUtil.isEmpty(refererIds)){
            return;
        }
        DataDictionaryCustom levelProfitSet = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.LEVEL_PROFIT.getType(), DataDictionaryEnum.LEVEL_PROFIT.getCode());
        BigDecimal levelProfit = new BigDecimal(StrUtil.isEmpty(levelProfitSet.getValue()) ? "0.3" : levelProfitSet.getValue());
        //投入金额减去技术方收益
        DataDictionaryCustom systemProfitSet = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.SYSTEM_PROFIT.getType(), DataDictionaryEnum.SYSTEM_PROFIT.getCode());
        BigDecimal systemProfit = new BigDecimal(StrUtil.isEmpty(systemProfitSet.getValue()) ? "0.05" : systemProfitSet.getValue());
        BigDecimal amount = dappSystemProfit.getAmount().subtract(systemProfit);
        //层级奖励总奖金
        BigDecimal levelProfitTotal = amount.multiply(levelProfit);
        //实发层级奖励 -- 如果还有剩余给技术方
        BigDecimal systemProfitTotal = BigDecimal.ZERO;
        //返回十层
        List<String> refererIdList = StrUtil.split(refererIds, ',');
        //i:计数层数,同时i也为对应层数应推广的人数,当达到对应的直推人数时,才能获取对应层级奖励
        if(CollUtil.isEmpty(refererIdList)){
            return;
        }
        //层级奖励最大循环次数
        int maxLevel = 10;
        if(maxLevel > refererIdList.size()){
            maxLevel = refererIdList.size();
        }
        for(int i = 0;i < maxLevel; i++){
            if(systemProfitTotal.compareTo(levelProfitTotal) < 0){
                String inviteId = refererIdList.get(i);
                //获取每层用户的直推人数,判断能否获得这个层级的层级奖励
                DappMemberEntity refererMember = dappMemberDao.selectMemberInfoByInviteId(inviteId);
                //获取直推用户数量
                QueryWrapper<DappMemberEntity> objectQueryWrapper = new QueryWrapper<>();
                objectQueryWrapper.eq("referer_id",refererMember.getInviteId());
                Integer selectCount = dappMemberDao.selectCount(objectQueryWrapper);
                if(i >= selectCount){
                    continue;
                }
                //获取对应层级奖励
                BigDecimal profit = LevelProfitEnum.YI.getProfit(i+1);
                String accountType = refererMember.getAccountType();
                //根据会员的等级类型,获取对应的百分比收益
                DataDictionaryCustom memberLevelSet = dataDictionaryCustomMapper.selectDicDataByTypeAndCode("MEMBER_LEVEL", accountType);
                BigDecimal memberLevel = new BigDecimal(memberLevelSet.getValue());
                BigDecimal memberLevelProfit = levelProfitTotal.multiply(profit).multiply(memberLevel);
                DappFundFlowEntity fundFlow = new DappFundFlowEntity(refererMember.getId(), memberLevelProfit, 4, 1, BigDecimal.ZERO,null,dappSystemProfit.getId());
                dappFundFlowDao.insert(fundFlow);
                //发送转币消息
                chainProducer.sendBnbTransferMsg(fundFlow.getId());
                systemProfitTotal = systemProfitTotal.add(memberLevelProfit);
            }
        }
        //如果还有剩余给技术方
        if(levelProfitTotal.compareTo(systemProfitTotal) > 0){
            BigDecimal avaProfit = levelProfitTotal.subtract(systemProfitTotal);
            DappFundFlowEntity fundFlow = new DappFundFlowEntity(1L, avaProfit, 5, 1, BigDecimal.ZERO,null,dappSystemProfit.getId());
            dappFundFlowDao.insert(fundFlow);
            //发送转币消息
            chainProducer.sendBnbTransferMsg(fundFlow.getId());
        }
        dappSystemProfitDao.updateLevelProfitById(DappSystemProfit.ENUM_YES,dappSystemProfit.getId());
    }
    @Override
    public void memberOut(Long id) {
        //验证是否已经加入动能队列
        DappSystemProfit systemProfit = dappSystemProfitDao.selectById(id);
        if(ObjectUtil.isEmpty(systemProfit)){
            return;
        }
        //获取当前是第几轮队列
        String redisKey = "QUEUE_COUNT";
        String memberOutCount = redisUtils.getString(redisKey);
        DataDictionaryCustom queueCountSet = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.QUEUE_COUNT.getType(), DataDictionaryEnum.QUEUE_COUNT.getCode());
        String queueCount = queueCountSet.getValue();
        if(StrUtil.isBlank(memberOutCount) || !queueCount.equals(memberOutCount)){
            redisUtils.set(redisKey,queueCount,0L);
            memberOutCount = queueCount;
        }
        //出局条件的人数
        /**
         * 初始大小 5+4*0
         * 1  1,2,3,4,5  1出局 5+4*0
         * 2  2,3,4,5,1(复投),7,8,9,10  2出局 5+4*1
         * 3  3,4,5,1(复投),7,8,9,10,2(复投),12,13,14,15 3出局 5+4*2
         * 4  4,5,1(复投),7,8,9,10,2(复投),12,13,14,15,3(复投),17,18,19,20  4出局 5+4*3
         */
        Integer memberCount = Integer.parseInt(memberOutCount) * 4 + 5;
        //判断当前是否符合出局条件
        QueryWrapper<DappSystemProfit> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("state",DappSystemProfit.STATE_IN);
        //实际投资人数
        Integer selectCount = dappSystemProfitDao.selectCount(objectQueryWrapper);
        //实际投资人数小于出局条件人数
        if(selectCount < memberCount){
            return;
        }
        //符合则出局 实际投资人数等于出局条件人数
        DappSystemProfit dappSystemProfit = dappSystemProfitDao.selectSystemProfitByState(DappSystemProfit.STATE_IN);
        if(ObjectUtil.isEmpty(dappSystemProfit)){
            return;
        }
        //符合则出局,轮数+1
        Integer realCount = (Integer.parseInt(queueCount) + 1);
        queueCountSet.setValue(realCount.toString());
        dataDictionaryCustomMapper.updateById(queueCountSet);
        redisUtils.set(redisKey,realCount,0L);
        DappSystemProfit dappSystemProfitNow = dappSystemProfitDao.selectByIdForUpdate(dappSystemProfit.getId(),DappSystemProfit.STATE_IN);
        dappSystemProfitDao.updateStateById(DappSystemProfit.STATE_OUT,dappSystemProfitNow.getId());
        //todo 直接拿走0.95ge
        DataDictionaryCustom investAmountProfitSet = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.INVEST_AMOUNT_PROFIT.getType(), DataDictionaryEnum.INVEST_AMOUNT_PROFIT.getCode());
        BigDecimal investAmountProfit = new BigDecimal(investAmountProfitSet.getValue());
        DappFundFlowEntity fundFlowOut = new DappFundFlowEntity(dappSystemProfitNow.getMemberId(), investAmountProfit, 7, 1, BigDecimal.ZERO, null,dappSystemProfitNow.getId());
        dappFundFlowDao.insert(fundFlowOut);
        //发送转币消息
        chainProducer.sendBnbTransferMsg(fundFlowOut.getId());
        //复投 成功{type: 1, txHash: result.transactionHash, id: res.data, flag: 'success', buyType: 2}
        DataDictionaryCustom investAmountSet = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.INVEST_AMOUNT.getType(), DataDictionaryEnum.INVEST_AMOUNT.getCode());
        BigDecimal investAmount = new BigDecimal(investAmountSet.getValue());
        //todo 直接运行转账
        String txHash = "复投";
        DappFundFlowEntity fundFlow = new DappFundFlowEntity(dappSystemProfitNow.getMemberId(), investAmount, 6, 1, BigDecimal.ZERO, txHash);
        dappFundFlowDao.insert(fundFlow);
        TransferDto transferDto = new TransferDto();
        transferDto.setType(1);
        transferDto.setTxHash(txHash);
        transferDto.setBuyType(2);
        transferDto.setId(fundFlow.getId());
        transferDto.setFlag("success");
        transferDto.setAmount(investAmount);
        transferDto.setMemberId(dappSystemProfitNow.getMemberId());
        dappWalletService.transferAgain(transferDto);
    }
    @Override
    public void bnbTransfer(Long id) {
        log.info("{}",id);
        //获取对应的流水记录
        DappFundFlowEntity dappFundFlowEntity = dappFundFlowDao.selectInfoById(id);
        if(ObjectUtil.isEmpty(dappFundFlowEntity)){
            return;
        }
        if(DappFundFlowEntity.WITHDRAW_STATUS_AGREE == dappFundFlowEntity.getStatus()){
            return;
        }
        Long memberId = dappFundFlowEntity.getMemberId();
        DappMemberEntity dappMemberEntity = dappMemberDao.selectById(memberId);
        if(ObjectUtil.isEmpty(dappMemberEntity)){
            return;
        }
        Integer count = dappFundFlowDao.updateStatusById(DappFundFlowEntity.WITHDRAW_STATUS_AGREE,dappFundFlowEntity.getId());
        if(count > 0){
            //金额
            BigDecimal amount = dappFundFlowEntity.getAmount();
            //目标地址
            String address = dappMemberEntity.getAddress();
    //        String hash = ChainService.getInstance(ChainEnum.BSC_USDT.name()).transfer(address, amount);
            String hash = ChainService.getInstance(ChainEnum.BNB.name()).transferBaseToken(address, amount);
            if(StrUtil.isEmpty(hash)){
                return;
            }
            dappFundFlowEntity.setFromHash(hash);
            dappFundFlowDao.updateById(dappFundFlowEntity);
        }
    }
    @Override
    public void agentUp(Long id) {
        //根据邀请码获取用户信息
        DappMemberEntity dappMemberEntity = dappMemberDao.selectById(id);
        if(ObjectUtil.isEmpty(dappMemberEntity)){
            return;
        }
        String accountType = dappMemberEntity.getAccountType();
        if(DataDictionaryEnum.BIG_BOSS.getCode().equals(accountType)){
            return;
        }
        //当前为BOSS,且直推中有两个BOSS以上,则升级BIG_BOSS
        if(DataDictionaryEnum.BOSS.getCode().equals(accountType)){
            Integer bossCount = dappMemberDao.selectCountByAccountTypeAndRefererId(DataDictionaryEnum.BOSS.getCode(),dappMemberEntity.getInviteId());
            if(2 <= bossCount){
                dappMemberDao.updateMemberAccountType(DataDictionaryEnum.BIG_BOSS.getCode(),dappMemberEntity.getId());
            }
            return;
        }
        //当前为AGENT,且直推中有两个以上,则升级BOSS
        if(DataDictionaryEnum.AGENT.getCode().equals(accountType)){
            Integer bossCount = dappMemberDao.selectCountByAccountTypeAndRefererId(null,dappMemberEntity.getInviteId());
            if(2 <= bossCount){
                dappMemberDao.updateMemberAccountType(DataDictionaryEnum.BOSS.getCode(),dappMemberEntity.getId());
            }
            return;
        }
    }
    public static void main(String[] args) {
        String refererIds = "1,2,3,4,5," +
                            "6,7,8,9,10," +
                            "11,12,13,14,15";
        List<String> refererIdList = StrUtil.split(refererIds, ',');
        System.out.println(refererIdList);
    }
}