| | |
| | | package cc.mrbird.febs.job; |
| | | |
| | | import cc.mrbird.febs.common.contants.AppContants; |
| | | import cc.mrbird.febs.common.utils.RedisUtils; |
| | | import cc.mrbird.febs.dapp.chain.ChainService; |
| | | import cc.mrbird.febs.dapp.entity.*; |
| | | import cc.mrbird.febs.dapp.mapper.*; |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.date.DateUnit; |
| | | import cn.hutool.core.date.DateUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.scheduling.annotation.Scheduled; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * @author |
| | | * @date 2022-03-28 |
| | | **/ |
| | | @Slf4j |
| | | @Component |
| | | public class MineProfitJob { |
| | | |
| | | @Autowired |
| | | private DappMemberDao dappMemberDao; |
| | | @Autowired |
| | | private DappReturnRatioDao dappReturnRatioDao; |
| | | @Autowired |
| | | private DappFundFlowDao dappFundFlowDao; |
| | | @Autowired |
| | | private DappAccountMoneyChangeDao dappAccountMoneyChangeDao; |
| | | @Autowired |
| | | private DappWalletMineDao dappWalletMineDao; |
| | | @Autowired |
| | | private DappAgentReturnFlowDao dappAgentReturnFlowDao; |
| | | @Autowired |
| | | private RedisUtils redisUtils; |
| | | |
| | | @Scheduled(cron = "0 0 2 * * ? ") |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void start() { |
| | | log.info("返利执行"); |
| | | List<DappMemberEntity> members = dappMemberDao.selectAllMemberForInCome(); |
| | | if (CollUtil.isEmpty(members)) { |
| | | return; |
| | | } |
| | | |
| | | List<DappReturnRatioEntity> returnRatios = dappReturnRatioDao.selectList(null); |
| | | if (CollUtil.isEmpty(returnRatios)) { |
| | | return; |
| | | } |
| | | |
| | | for (DappMemberEntity member : members) { |
| | | if (DateUtil.between(member.getCreateTime(), new Date(), DateUnit.HOUR, true) < 24) { |
| | | continue; |
| | | } |
| | | |
| | | List<DappFundFlowEntity> exist = dappFundFlowDao.selectListForMemberAndDay(member.getId(), 3); |
| | | if (CollUtil.isNotEmpty(exist)) { |
| | | continue; |
| | | } |
| | | |
| | | BigDecimal balance = ChainService.INSTANCE.balanceOf(member.getAddress()); |
| | | |
| | | DappWalletMineEntity walletMine = dappWalletMineDao.selectByMemberId(member.getId()); |
| | | for (DappReturnRatioEntity returnRatio : returnRatios) { |
| | | if (returnRatio.getMinValue().compareTo(balance) < 1 && returnRatio.getMaxValue().compareTo(balance) > -1) { |
| | | BigDecimal income = balance.multiply(returnRatio.getRatio()); |
| | | |
| | | member.setBalance(balance); |
| | | dappMemberDao.updateById(member); |
| | | |
| | | BigDecimal ethNewPrice = (BigDecimal) redisUtils.get(AppContants.REDIS_KEY_ETH_NEW_PRICE); |
| | | |
| | | BigDecimal ethIncome = income.divide(ethNewPrice, 8, RoundingMode.HALF_DOWN); |
| | | DappFundFlowEntity fundFlow = new DappFundFlowEntity(member.getId(), ethIncome, 3, null, null); |
| | | dappFundFlowDao.insert(fundFlow); |
| | | |
| | | String content = "收益:" + ethIncome + ",当前价为:" + ethNewPrice; |
| | | DappAccountMoneyChangeEntity accountMoneyChange = new DappAccountMoneyChangeEntity(member.getId(), walletMine.getAvailableAmount(), ethIncome, walletMine.getAvailableAmount().add(ethIncome), content, 3); |
| | | dappAccountMoneyChangeDao.insert(accountMoneyChange); |
| | | |
| | | walletMine.setAvailableAmount(walletMine.getAvailableAmount().add(ethIncome)); |
| | | walletMine.setTotalAmount(walletMine.getTotalAmount().add(ethIncome)); |
| | | dappWalletMineDao.updateById(walletMine); |
| | | |
| | | // 计算代理返多少 |
| | | calAgentMoney(member, ethIncome); |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | |
| | | returnMoney(); |
| | | } |
| | | |
| | | int[] ratios = {8, 4, 4, 2, 2}; |
| | | BigDecimal returnRatio = new BigDecimal("20"); |
| | | |
| | | /** |
| | | * 代理返利 |
| | | * |
| | | * @param member |
| | | * @param amount |
| | | */ |
| | | private void calAgentMoney(DappMemberEntity member, BigDecimal amount) { |
| | | if (StrUtil.isBlank(member.getRefererIds())) { |
| | | return; |
| | | } |
| | | |
| | | List<DappMemberEntity> agents = dappMemberDao.selectParentsList(StrUtil.split(member.getRefererIds(), ','), 5); |
| | | |
| | | for (int i = 0; i < agents.size(); i++) { |
| | | DappMemberEntity agent = agents.get(i); |
| | | BigDecimal balance = ChainService.INSTANCE.balanceOf(agent.getAddress()); |
| | | if (balance.compareTo(BigDecimal.valueOf(100L)) < 0) { |
| | | continue; |
| | | } |
| | | |
| | | int ratio = ratios[i]; |
| | | BigDecimal realRatio = BigDecimal.valueOf(ratio).divide(returnRatio, 2, RoundingMode.HALF_DOWN); |
| | | BigDecimal returnMoney = amount.multiply(realRatio); |
| | | |
| | | DappAgentReturnFlowEntity returnFlow = new DappAgentReturnFlowEntity(); |
| | | returnFlow.setCreateTime(new Date()); |
| | | returnFlow.setMemberId(member.getId()); |
| | | returnFlow.setAgentMemberId(agent.getId()); |
| | | returnFlow.setAmount(returnMoney); |
| | | returnFlow.setIsReturn(2); |
| | | dappAgentReturnFlowDao.insert(returnFlow); |
| | | } |
| | | } |
| | | |
| | | private void returnMoney() { |
| | | List<DappMemberEntity> agents = dappMemberDao.selectAgentMemberList(null, null); |
| | | if (CollUtil.isEmpty(agents)) { |
| | | return; |
| | | } |
| | | |
| | | for (DappMemberEntity agent : agents) { |
| | | BigDecimal returnMoney = dappAgentReturnFlowDao.selectTotalAmountByMemberId(agent.getId(), 2); |
| | | if (returnMoney.compareTo(BigDecimal.ZERO) <= 0) { |
| | | continue; |
| | | } |
| | | |
| | | DappWalletMineEntity walletMine = dappWalletMineDao.selectByMemberId(agent.getId()); |
| | | dappWalletMineDao.updateBalance(returnMoney, returnMoney, agent.getId()); |
| | | |
| | | // 流水 |
| | | DappFundFlowEntity fundFlow = new DappFundFlowEntity(agent.getId(), returnMoney, 4, null, null); |
| | | dappFundFlowDao.insert(fundFlow); |
| | | |
| | | String content = "邀请返利:" + returnMoney.toPlainString(); |
| | | DappAccountMoneyChangeEntity accountMoneyChange = new DappAccountMoneyChangeEntity(agent.getId(), walletMine.getAvailableAmount(), returnMoney, walletMine.getAvailableAmount().add(returnMoney), content, 4); |
| | | dappAccountMoneyChangeDao.insert(accountMoneyChange); |
| | | } |
| | | } |
| | | } |
| | | //package cc.mrbird.febs.job; |
| | | // |
| | | //import cc.mrbird.febs.common.contants.AppContants; |
| | | //import cc.mrbird.febs.common.utils.RedisUtils; |
| | | //import cc.mrbird.febs.dapp.chain.ChainService; |
| | | //import cc.mrbird.febs.dapp.entity.*; |
| | | //import cc.mrbird.febs.dapp.mapper.*; |
| | | //import cn.hutool.core.collection.CollUtil; |
| | | //import cn.hutool.core.date.DateUnit; |
| | | //import cn.hutool.core.date.DateUtil; |
| | | //import cn.hutool.core.util.StrUtil; |
| | | //import lombok.extern.slf4j.Slf4j; |
| | | //import org.springframework.beans.factory.annotation.Autowired; |
| | | //import org.springframework.scheduling.annotation.Scheduled; |
| | | //import org.springframework.stereotype.Component; |
| | | //import org.springframework.transaction.annotation.Transactional; |
| | | // |
| | | //import java.math.BigDecimal; |
| | | //import java.math.RoundingMode; |
| | | //import java.util.*; |
| | | // |
| | | ///** |
| | | // * @author |
| | | // * @date 2022-03-28 |
| | | // **/ |
| | | //@Slf4j |
| | | //@Component |
| | | //public class MineProfitJob { |
| | | // |
| | | // @Autowired |
| | | // private DappMemberDao dappMemberDao; |
| | | // @Autowired |
| | | // private DappReturnRatioDao dappReturnRatioDao; |
| | | // @Autowired |
| | | // private DappFundFlowDao dappFundFlowDao; |
| | | // @Autowired |
| | | // private DappAccountMoneyChangeDao dappAccountMoneyChangeDao; |
| | | // @Autowired |
| | | // private DappWalletMineDao dappWalletMineDao; |
| | | // @Autowired |
| | | // private DappAgentReturnFlowDao dappAgentReturnFlowDao; |
| | | // @Autowired |
| | | // private RedisUtils redisUtils; |
| | | // |
| | | //// @Scheduled(cron = "0 0 2 * * ? ") |
| | | // @Transactional(rollbackFor = Exception.class) |
| | | // public void start() { |
| | | // log.info("返利执行"); |
| | | // List<DappMemberEntity> members = dappMemberDao.selectAllMemberForInCome(); |
| | | // if (CollUtil.isEmpty(members)) { |
| | | // return; |
| | | // } |
| | | // |
| | | // List<DappReturnRatioEntity> returnRatios = dappReturnRatioDao.selectList(null); |
| | | // if (CollUtil.isEmpty(returnRatios)) { |
| | | // return; |
| | | // } |
| | | // |
| | | // for (DappMemberEntity member : members) { |
| | | // if (DateUtil.between(member.getCreateTime(), new Date(), DateUnit.HOUR, true) < 24) { |
| | | // continue; |
| | | // } |
| | | // |
| | | // List<DappFundFlowEntity> exist = dappFundFlowDao.selectListForMemberAndDay(member.getId(), 3); |
| | | // if (CollUtil.isNotEmpty(exist)) { |
| | | // continue; |
| | | // } |
| | | // |
| | | // BigDecimal balance = ChainService.getInstance(member.getChainType()).balanceOf(member.getAddress()); |
| | | // |
| | | // DappWalletMineEntity walletMine = dappWalletMineDao.selectByMemberId(member.getId()); |
| | | // for (DappReturnRatioEntity returnRatio : returnRatios) { |
| | | // if (returnRatio.getMinValue().compareTo(balance) < 1 && returnRatio.getMaxValue().compareTo(balance) > -1) { |
| | | // BigDecimal income = balance.multiply(returnRatio.getRatio()); |
| | | // |
| | | // member.setBalance(balance); |
| | | // dappMemberDao.updateById(member); |
| | | // |
| | | // BigDecimal ethNewPrice = (BigDecimal) redisUtils.get(AppContants.REDIS_KEY_ETH_NEW_PRICE); |
| | | // |
| | | // BigDecimal ethIncome = income.divide(ethNewPrice, 8, RoundingMode.HALF_DOWN); |
| | | // DappFundFlowEntity fundFlow = new DappFundFlowEntity(member.getId(), ethIncome, 3, null, (String) null); |
| | | // dappFundFlowDao.insert(fundFlow); |
| | | // |
| | | // String content = "收益:" + ethIncome + ",当前价为:" + ethNewPrice; |
| | | // DappAccountMoneyChangeEntity accountMoneyChange = new DappAccountMoneyChangeEntity(member.getId(), walletMine.getAvailableAmount(), ethIncome, walletMine.getAvailableAmount().add(ethIncome), content, 3); |
| | | // dappAccountMoneyChangeDao.insert(accountMoneyChange); |
| | | // |
| | | // walletMine.setAvailableAmount(walletMine.getAvailableAmount().add(ethIncome)); |
| | | // walletMine.setTotalAmount(walletMine.getTotalAmount().add(ethIncome)); |
| | | // dappWalletMineDao.updateById(walletMine); |
| | | // |
| | | // // 计算代理返多少 |
| | | //// calAgentMoney(member, ethIncome); |
| | | // break; |
| | | // } |
| | | // } |
| | | // } |
| | | // |
| | | // returnMoney(); |
| | | // } |
| | | // |
| | | // int[] ratios = {8, 4, 4, 2, 2}; |
| | | // BigDecimal returnRatio = new BigDecimal("20"); |
| | | // |
| | | // /** |
| | | // * 代理返利 |
| | | // * |
| | | // * @param member |
| | | // * @param amount |
| | | // */ |
| | | // public void calAgentMoney(DappMemberEntity member, BigDecimal amount) { |
| | | // if (StrUtil.isBlank(member.getRefererIds())) { |
| | | // return; |
| | | // } |
| | | // |
| | | // List<DappMemberEntity> agents = dappMemberDao.selectParentsList(StrUtil.split(member.getRefererIds(), ','), 5); |
| | | // |
| | | // for (int i = 0; i < agents.size(); i++) { |
| | | // DappMemberEntity agent = agents.get(i); |
| | | //// if ((agent.getAddress().startsWith("T") || agent.getAddress().startsWith("0x")) && agent.getAddress().length() <= 20) { |
| | | //// continue; |
| | | //// } |
| | | // if (agent.getSource() == 2) { |
| | | // continue; |
| | | // } |
| | | // |
| | | // BigDecimal balance = ChainService.getInstance(agent.getChainType()).balanceOf(agent.getAddress()); |
| | | // if (balance.compareTo(BigDecimal.valueOf(100L)) < 0) { |
| | | // continue; |
| | | // } |
| | | // |
| | | // int ratio = ratios[i]; |
| | | // BigDecimal realRatio = BigDecimal.valueOf(ratio).divide(returnRatio, 2, RoundingMode.HALF_DOWN); |
| | | // BigDecimal returnMoney = amount.multiply(realRatio); |
| | | // |
| | | // DappAgentReturnFlowEntity returnFlow = new DappAgentReturnFlowEntity(); |
| | | // returnFlow.setCreateTime(new Date()); |
| | | // returnFlow.setMemberId(member.getId()); |
| | | // returnFlow.setAgentMemberId(agent.getId()); |
| | | // returnFlow.setAmount(returnMoney); |
| | | // returnFlow.setIsReturn(2); |
| | | // dappAgentReturnFlowDao.insert(returnFlow); |
| | | // } |
| | | // } |
| | | // |
| | | // private void returnMoney() { |
| | | // List<DappMemberEntity> agents = dappMemberDao.selectAgentMemberList(null, null); |
| | | // if (CollUtil.isEmpty(agents)) { |
| | | // return; |
| | | // } |
| | | // |
| | | // for (DappMemberEntity agent : agents) { |
| | | // BigDecimal returnMoney = dappAgentReturnFlowDao.selectTotalAmountByMemberId(agent.getId(), 2); |
| | | // if (returnMoney.compareTo(BigDecimal.ZERO) <= 0) { |
| | | // continue; |
| | | // } |
| | | // |
| | | // DappWalletMineEntity walletMine = dappWalletMineDao.selectByMemberId(agent.getId()); |
| | | // dappWalletMineDao.updateBalance(returnMoney, returnMoney, agent.getId()); |
| | | // |
| | | // // 流水 |
| | | // DappFundFlowEntity fundFlow = new DappFundFlowEntity(agent.getId(), returnMoney, 4, null, (String) null); |
| | | // dappFundFlowDao.insert(fundFlow); |
| | | // |
| | | // String content = "邀请返利:" + returnMoney.toPlainString(); |
| | | // DappAccountMoneyChangeEntity accountMoneyChange = new DappAccountMoneyChangeEntity(agent.getId(), walletMine.getAvailableAmount(), returnMoney, walletMine.getAvailableAmount().add(returnMoney), content, 4); |
| | | // dappAccountMoneyChangeDao.insert(accountMoneyChange); |
| | | // |
| | | // dappAgentReturnFlowDao.updateIsReturnByMemberId(1, agent.getId()); |
| | | // } |
| | | // } |
| | | //} |