| | |
| | | import cc.mrbird.febs.common.exception.FebsException; |
| | | import cc.mrbird.febs.common.utils.RedisUtils; |
| | | import cc.mrbird.febs.dapp.chain.ChainEnum; |
| | | import cc.mrbird.febs.dapp.chain.ChainService; |
| | | import cc.mrbird.febs.dapp.entity.*; |
| | | import cc.mrbird.febs.dapp.mapper.*; |
| | | import cc.mrbird.febs.dapp.service.AsyncCjService; |
| | |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.math.BigInteger; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | |
| | | */ |
| | | |
| | | //当天实际当天滑点 |
| | | |
| | | BigDecimal gfaPrice = new BigDecimal( |
| | | redisUtils.getString(DataDicEnum.GFA_PRICE.getValue()) |
| | | ).setScale(2,BigDecimal.ROUND_DOWN); |
| | | BigDecimal usdtAmount = new BigDecimal( |
| | | redisUtils.getString(DataDicEnum.GFA_HUA_DIAN_WALLET_USDT_AMOUNT.getValue()) |
| | | ).setScale(2,BigDecimal.ROUND_DOWN); |
| | |
| | | if(BigDecimal.ZERO.compareTo(usdtAmount) > 0){ |
| | | return; |
| | | } |
| | | if(BigDecimal.ZERO.compareTo(coinAmount) > 0){ |
| | | return; |
| | | } |
| | | BigDecimal usdtAmountReal = usdtAmount; |
| | | BigDecimal coinAmountReal = coinAmount; |
| | | //生成一条记录 |
| | | DappHdRecord dappHdRecordAdd = new DappHdRecord(); |
| | | dappHdRecordAdd.setPrice(gfaPrice); |
| | | dappHdRecordAdd.setUsdtEvery(usdtAmountReal); |
| | | dappHdRecordAdd.setUsdtAmount(usdtAmountReal); |
| | | dappHdRecordAdd.setCoinEvery(coinAmountReal); |
| | | dappHdRecordAdd.setCoinAmount(coinAmountReal); |
| | | dappHdRecordMapper.insert(dappHdRecordAdd); |
| | | |
| | | /** |
| | | * 5%滑奖分配: |
| | | * 1%用自身质押 |
| | |
| | | //平分份数 |
| | | BigDecimal totalPercent = new BigDecimal(selfPercent + teamPercent + workPercent); |
| | | //每一份有多少 |
| | | BigDecimal divideEveryUsdt = usdtAmountReal.divide(totalPercent, 4, BigDecimal.ROUND_DOWN); |
| | | BigDecimal divideEveryUsdt = usdtAmountReal.divide(totalPercent, 8, BigDecimal.ROUND_DOWN); |
| | | //每一份有多少 |
| | | BigDecimal divideEveryCoin = coinAmountReal.divide(totalPercent, 4, BigDecimal.ROUND_DOWN); |
| | | BigDecimal divideEveryCoin = coinAmountReal.divide(totalPercent, 8, BigDecimal.ROUND_DOWN); |
| | | //1%用自身质押 |
| | | selfHdPerk(selfPercent,divideEveryUsdt,divideEveryCoin); |
| | | //3%用于团队业绩计算 |
| | | teamHdPerk(teamPercent,divideEveryUsdt,divideEveryCoin); |
| | | } |
| | | |
| | | @Override |
| | | public void confirmOrder(DappFundFlowEntity dappFundFlow) { |
| | | Long id = dappFundFlow.getId(); |
| | | DappFundFlowEntity dappFundFlowEntity = dappFundFlowDao.selectById(id); |
| | | Integer status = dappFundFlowEntity.getStatus(); |
| | | if(1 != status){ |
| | | return; |
| | | } |
| | | Integer type = dappFundFlowEntity.getType(); |
| | | BigDecimal amount = dappFundFlowEntity.getAmount().negate(); |
| | | if(BigDecimal.ZERO.compareTo(amount) >=0){ |
| | | amount = amount.negate(); |
| | | } |
| | | Long memberId = dappFundFlowEntity.getMemberId(); |
| | | DappMemberEntity dappMemberEntity = dappMemberDao.selectById(memberId); |
| | | if(FlowTypeEnum.USDT_OUT.getValue() == type){ |
| | | //USDT转账 |
| | | String transfer = ChainService.getInstance(ChainEnum.BSC_USDT.name()).transfer(dappMemberEntity.getAddress(), amount); |
| | | int retryTime = 0;//重试次数 |
| | | while (StrUtil.isEmpty(transfer)) { |
| | | // 休眠2s |
| | | try { |
| | | Thread.sleep(2000); |
| | | } catch (InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | if(retryTime < 3){ |
| | | transfer = ChainService.getInstance(ChainEnum.BSC_USDT.name()).transfer(dappMemberEntity.getAddress(), amount); |
| | | } |
| | | retryTime ++ ; |
| | | } |
| | | if(StrUtil.isNotEmpty(transfer)){ |
| | | dappFundFlowEntity.setFromHash(transfer); |
| | | dappFundFlowEntity.setStatus(2); |
| | | dappFundFlowDao.updateById(dappFundFlowEntity); |
| | | }else{ |
| | | dappFundFlowEntity.setStatus(1); |
| | | dappFundFlowDao.updateById(dappFundFlowEntity); |
| | | } |
| | | }else if(FlowTypeEnum.DAI_BI_OUT.getValue() == type){ |
| | | //代币转账 |
| | | String transfer = ChainService.getInstance(ChainEnum.BSC_GFA.name()).transfer(dappMemberEntity.getAddress(), amount); |
| | | if(StrUtil.isNotEmpty(transfer)){ |
| | | dappFundFlowEntity.setFromHash(transfer); |
| | | dappFundFlowEntity.setStatus(2); |
| | | dappFundFlowDao.updateById(dappFundFlowEntity); |
| | | }else{ |
| | | dappFundFlowEntity.setStatus(1); |
| | | dappFundFlowDao.updateById(dappFundFlowEntity); |
| | | } |
| | | }else{ |
| | | return; |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void confirmCancel(DappFundFlowEntity dappFundFlow) { |
| | | Long id = dappFundFlow.getId(); |
| | | DappFundFlowEntity dappFundFlowEntity = dappFundFlowDao.selectById(id); |
| | | Integer status = dappFundFlowEntity.getStatus(); |
| | | if(1 != status){ |
| | | return; |
| | | } |
| | | Integer type = dappFundFlowEntity.getType(); |
| | | BigDecimal amount = dappFundFlowEntity.getAmount().negate(); |
| | | if(BigDecimal.ZERO.compareTo(amount) >=0){ |
| | | amount = amount.negate(); |
| | | } |
| | | Long memberId = dappFundFlowEntity.getMemberId(); |
| | | DappMemberEntity dappMemberEntity = dappMemberDao.selectById(memberId); |
| | | |
| | | dappFundFlowEntity.setStatus(DappFundFlowEntity.WITHDRAW_STATUS_DISAGREE); |
| | | dappFundFlowDao.updateById(dappFundFlowEntity); |
| | | if(FlowTypeEnum.USDT_OUT.getValue() == type){ |
| | | dappMemberEntity.setUsdtBalance(dappMemberEntity.getUsdtBalance().add(amount)); |
| | | dappMemberDao.updateUsdtBalanceWithVersion(dappMemberEntity); |
| | | }else if(FlowTypeEnum.DAI_BI_OUT.getValue() == type){ |
| | | dappMemberEntity.setBalance(dappMemberEntity.getBalance().add(amount)); |
| | | dappMemberDao.updateBalanceWithVersion(dappMemberEntity); |
| | | }else{ |
| | | return; |
| | | } |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | String transfer = ChainService.getInstance(ChainEnum.BSC_USDT.name()).transfer("0xCEBfd36e03BD80c7015Cbad17eFfBc33d2923FF3", new BigDecimal("0.1")); |
| | | // BigInteger bigInteger = org.web3j.utils.Convert.toWei("35", org.web3j.utils.Convert.Unit.GWEI).toBigInteger(); |
| | | // System.out.println(bigInteger); |
| | | } |
| | | |
| | | public void teamHdPerk(int teamPercent,BigDecimal divideEveryUsdt,BigDecimal divideEveryCoin){ |
| | |
| | | Map<Long, BigDecimal> memberPerkMap = new HashMap<>(); |
| | | //总团队业绩 |
| | | BigDecimal memberPerkTotal = BigDecimal.ZERO; |
| | | // BigDecimal memberPerkTotal = dappAchieves.stream().map(DappAchieve::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add); |
| | | // if(BigDecimal.ZERO.compareTo(memberPerkTotal) >= 0){ |
| | | // return; |
| | | // } |
| | | //获取全部会员 |
| | | Set<Long> allMembers = dappMemberEntities.stream().map(DappMemberEntity::getId).collect(Collectors.toSet()); |
| | | //获取正在质押的会员ID |
| | | Set<Long> achieveMembers = dappAchieves.stream().map(DappAchieve::getMemberId).collect(Collectors.toSet()); |
| | | Collection<Long> intersection = CollUtil.intersection(allMembers, achieveMembers); |
| | | for(Long memberId : intersection){ |
| | | // Set<Long> achieveMembers = dappAchieves.stream().map(DappAchieve::getMemberId).collect(Collectors.toSet()); |
| | | // Collection<Long> intersection = CollUtil.intersection(allMembers, achieveMembers); |
| | | for(Long memberId : allMembers){ |
| | | /** |
| | | * 获取每一个成员的四级直推 |
| | | */ |
| | |
| | | memberPerkTotal = memberPerkTotal.add(teamAchieveMemberSum); |
| | | memberPerkMap.put(memberId,teamAchieveMemberSum); |
| | | } |
| | | if(BigDecimal.ZERO.compareTo(memberPerkTotal) >= 0){ |
| | | return; |
| | | } |
| | | |
| | | BigDecimal everyUsdt = multiplyDivideEveryUsdt.divide(memberPerkTotal,4,BigDecimal.ROUND_DOWN); |
| | | BigDecimal everyCoin = multiplyDivideEveryCoin.divide(memberPerkTotal,4,BigDecimal.ROUND_DOWN); |
| | | BigDecimal everyUsdt = multiplyDivideEveryUsdt.divide(memberPerkTotal,8,BigDecimal.ROUND_DOWN); |
| | | BigDecimal everyCoin = multiplyDivideEveryCoin.divide(memberPerkTotal,8,BigDecimal.ROUND_DOWN); |
| | | Set<Long> set = memberPerkMap.keySet(); // 得到所有key的集合 |
| | | for (Long memberId : set) { |
| | | //当前生效中的团队业绩 |
| | | BigDecimal amountMember = memberPerkMap.get(memberId); |
| | | //这个人实际可以分到的数量USDT |
| | | BigDecimal multiplyUsdt = amountMember.multiply(everyUsdt).setScale(4, BigDecimal.ROUND_DOWN); |
| | | BigDecimal multiplyUsdt = amountMember.multiply(everyUsdt).setScale(8, BigDecimal.ROUND_DOWN); |
| | | if(BigDecimal.ZERO.compareTo(multiplyUsdt) >= 0){ |
| | | continue; |
| | | } |
| | | //这个人实际可以分到的数量COIN |
| | | BigDecimal multiplyCoin = divideEveryCoin.multiply(everyCoin).setScale(4, BigDecimal.ROUND_DOWN); |
| | | |
| | | // createFlow( |
| | | // multiplyUsdt, |
| | | // memberId, |
| | | // FlowTypeEnum.HUA_DIAN_TUANDUI_USDT.getValue(), |
| | | // FlowTypeEnum.HUA_DIAN_TUANDUI_USDT.getDescrition(), |
| | | // 4, |
| | | // 1, |
| | | // ChainEnum.BSC_TFC_REWARD.name(), |
| | | // DataDicEnum.USDT_COIN.getValue(), |
| | | // 2); |
| | | BigDecimal multiplyCoin = amountMember.multiply(everyCoin).setScale(8, BigDecimal.ROUND_DOWN); |
| | | DappMemberEntity dappMemberEntityExit = dappMemberDao.selectById(memberId); |
| | | if(ObjectUtil.isEmpty(dappMemberEntityExit)){ |
| | | continue; |
| | | } |
| | | //如果没有进行中的质押,直接返回 |
| | | QueryWrapper<DappAchieve> objectQueryWrappereq = new QueryWrapper<>(); |
| | | objectQueryWrappereq.eq("state",DappAchieve.STATUS_ING); |
| | | objectQueryWrappereq.eq("member_id",memberId); |
| | | List<DappAchieve> dappAchieveseq = dappAchieveMapper.selectList(objectQueryWrappereq); |
| | | if(CollUtil.isEmpty(dappAchieveseq)){ |
| | | DappFundFlowEntity fundFlowTuiJian = new DappFundFlowEntity( |
| | | memberId, |
| | | multiplyUsdt, |
| | | FlowTypeEnum.TUAN_DUI_FAIL.getValue(), |
| | | DappFundFlowEntity.WITHDRAW_STATUS_AGREE, |
| | | StrUtil.format(FlowTypeEnum.TUAN_DUI_FAIL_DES.getDescrition(), amountMember)); |
| | | dappFundFlowDao.insert(fundFlowTuiJian); |
| | | continue; |
| | | } |
| | | createFlow( |
| | | multiplyCoin, |
| | | multiplyUsdt, |
| | | memberId, |
| | | FlowTypeEnum.HUA_DIAN_TUANDUI_DAIBI.getValue(), |
| | | FlowTypeEnum.HUA_DIAN_TUANDUI_DAIBI.getDescrition(), |
| | | FlowTypeEnum.HUA_DIAN_TUANDUI_USDT.getValue(), |
| | | StrUtil.format(FlowTypeEnum.HUA_DIAN_TUANDUI_USDT.getDescrition(), amountMember), |
| | | 4, |
| | | 1, |
| | | ChainEnum.BSC_TFC_REWARD.name(), |
| | | DataDicEnum.USDT_COIN.getValue(), |
| | | 1); |
| | | 2); |
| | | // createFlow( |
| | | // multiplyCoin, |
| | | // memberId, |
| | | // FlowTypeEnum.HUA_DIAN_TUANDUI_DAIBI.getValue(), |
| | | // StrUtil.format(FlowTypeEnum.HUA_DIAN_TUANDUI_DAIBI.getDescrition(), amountMember), |
| | | // 4, |
| | | // 1, |
| | | // ChainEnum.BSC_TFC_REWARD.name(), |
| | | // DataDicEnum.USDT_COIN.getValue(), |
| | | // 1); |
| | | |
| | | } |
| | | } |
| | |
| | | if(BigDecimal.ZERO.compareTo(amountMemberTotal) >= 0){ |
| | | return; |
| | | } |
| | | BigDecimal everyUsdt = multiplyDivideEveryUsdt.divide(amountMemberTotal,4,BigDecimal.ROUND_DOWN); |
| | | BigDecimal everyCoin = multiplyDivideEveryCoin.divide(amountMemberTotal,4,BigDecimal.ROUND_DOWN); |
| | | BigDecimal everyUsdt = multiplyDivideEveryUsdt.divide(amountMemberTotal,8,BigDecimal.ROUND_DOWN); |
| | | BigDecimal everyCoin = multiplyDivideEveryCoin.divide(amountMemberTotal,8,BigDecimal.ROUND_DOWN); |
| | | |
| | | Map<Long, List<DappAchieve>> collect = dappAchieves.stream().collect(Collectors.groupingBy(DappAchieve::getMemberId)); |
| | | Set<Long> set = collect.keySet(); // 得到所有key的集合 |
| | |
| | | //当前每个人生效中的质押总数 |
| | | BigDecimal amountMember = collect.get(memberId).stream().map(DappAchieve::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add); |
| | | //这个人实际可以分到的数量USDT |
| | | BigDecimal multiplyUsdt = amountMember.multiply(everyUsdt).setScale(4, BigDecimal.ROUND_DOWN); |
| | | BigDecimal multiplyUsdt = amountMember.multiply(everyUsdt).setScale(8, BigDecimal.ROUND_DOWN); |
| | | if(BigDecimal.ZERO.compareTo(multiplyUsdt) >= 0){ |
| | | continue; |
| | | } |
| | | //这个人实际可以分到的数量COIN |
| | | BigDecimal multiplyCoin = divideEveryCoin.multiply(everyCoin).setScale(4, BigDecimal.ROUND_DOWN); |
| | | BigDecimal multiplyCoin = amountMember.multiply(everyCoin).setScale(8, BigDecimal.ROUND_DOWN); |
| | | |
| | | // createFlow( |
| | | // multiplyUsdt, |
| | | // memberId, |
| | | // FlowTypeEnum.HUA_DIAN_GEREN.getValue(), |
| | | // FlowTypeEnum.HUA_DIAN_GEREN.getDescrition(), |
| | | // 4, |
| | | // 1, |
| | | // ChainEnum.BSC_TFC_REWARD.name(), |
| | | // DataDicEnum.USDT_COIN.getValue(), |
| | | // 2); |
| | | DappMemberEntity dappMemberEntityExit = dappMemberDao.selectById(memberId); |
| | | if(ObjectUtil.isEmpty(dappMemberEntityExit)){ |
| | | continue; |
| | | } |
| | | createFlow( |
| | | multiplyCoin, |
| | | multiplyUsdt, |
| | | memberId, |
| | | FlowTypeEnum.HUA_DIAN_GEREN.getValue(), |
| | | FlowTypeEnum.HUA_DIAN_GEREN.getDescrition(), |
| | | StrUtil.format(FlowTypeEnum.HUA_DIAN_GEREN.getDescrition(), amountMember), |
| | | 4, |
| | | 1, |
| | | ChainEnum.BSC_TFC_REWARD.name(), |
| | | DataDicEnum.GFA_COIN.getValue(), |
| | | 1); |
| | | DataDicEnum.USDT_COIN.getValue(), |
| | | 2); |
| | | // createFlow( |
| | | // multiplyCoin, |
| | | // memberId, |
| | | // FlowTypeEnum.HUA_DIAN_GEREN.getValue(), |
| | | // StrUtil.format(FlowTypeEnum.HUA_DIAN_GEREN.getDescrition(), amountMember), |
| | | // 4, |
| | | // 1, |
| | | // ChainEnum.BSC_TFC_REWARD.name(), |
| | | // DataDicEnum.GFA_COIN.getValue(), |
| | | // 1); |
| | | } |
| | | } |
| | | |