| | |
| | | import cn.hutool.core.date.DateUnit; |
| | | import cn.hutool.core.date.DateUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.xcong.excoin.common.contants.AppContants; |
| | | import com.xcong.excoin.modules.coin.dao.MemberAccountMoneyChangeDao; |
| | | import com.xcong.excoin.modules.member.dao.MemberDao; |
| | | import com.xcong.excoin.modules.member.dao.MemberWalletCoinDao; |
| | | import com.xcong.excoin.modules.member.entity.MemberEntity; |
| | | import com.xcong.excoin.modules.member.entity.MemberWalletCoinEntity; |
| | | import com.xcong.excoin.modules.yunding.dao.YdBasicLevelSettingDao; |
| | | import com.xcong.excoin.modules.yunding.dao.YdBasicSettingDao; |
| | | import com.xcong.excoin.modules.yunding.dao.YdOrderDao; |
| | | import com.xcong.excoin.modules.yunding.dao.YdProductDao; |
| | | import com.xcong.excoin.modules.yunding.entity.YdBasicLevelSettingEntity; |
| | | import com.xcong.excoin.modules.yunding.entity.YdBasicSettingEntity; |
| | | import com.xcong.excoin.modules.yunding.entity.YdOrderEntity; |
| | | import com.xcong.excoin.modules.yunding.entity.YdProductEntity; |
| | | import com.xcong.excoin.modules.yunding.service.XchProfitService; |
| | | import com.xcong.excoin.netty.common.Contans; |
| | | import com.xcong.excoin.rabbit.producer.YunDingProducter; |
| | | import com.xcong.excoin.utils.LogRecordUtils; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.*; |
| | | |
| | | @Slf4j |
| | | @Service |
| | | public class XchProfitServiceImpl implements XchProfitService { |
| | | |
| | |
| | | @Autowired |
| | | private YdProductDao ydProductDao; |
| | | |
| | | @Autowired |
| | | private YdBasicLevelSettingDao ydBasicLevelSettingDao; |
| | | |
| | | @Autowired |
| | | private YunDingProducter yunDingProducter; |
| | | |
| | | @Autowired |
| | | private YdBasicSettingDao ydBasicSettingDao; |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void autoBeAgent(Long id) { |
| | | MemberEntity member = memberDao.selectById(id); |
| | | |
| | | List<MemberEntity> members = new ArrayList<>(); |
| | | List<String> inviteIds = StrUtil.split(member.getRefererIds(), ','); |
| | | if (CollUtil.isNotEmpty(inviteIds)) { |
| | | members = memberDao.selectMemberByInviteIds(inviteIds); |
| | | } |
| | | members.add(member); |
| | | List<YdBasicLevelSettingEntity> settings = ydBasicLevelSettingDao.selectAgentLevelSetting(); |
| | | |
| | | if (CollUtil.isNotEmpty(members)) { |
| | | for (MemberEntity memberEntity : members) { |
| | | MemberEntity update = new MemberEntity(); |
| | | update.setId(memberEntity.getId()); |
| | | |
| | | if (memberEntity.getAgentLevel() == null) { |
| | | // 判断是否达到市代标准 |
| | | if(becomeSd(memberEntity, settings.get(1))) { |
| | | update.setAgentLevel(2); |
| | | memberDao.updateById(update); |
| | | } |
| | | } else if (memberEntity.getAgentLevel() != null && memberEntity.getAgentLevel() == 2){ |
| | | // 判断是否达到总代标准 |
| | | if(becomeZd(memberEntity, settings.get(0))) { |
| | | update.setAgentLevel(1); |
| | | memberDao.updateById(update); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 市代 团队(包含自己)算力达到指定数量 |
| | | * @param member |
| | | * @param basicLevelSetting |
| | | * @return |
| | | */ |
| | | private Boolean becomeSd(MemberEntity member, YdBasicLevelSettingEntity basicLevelSetting) { |
| | | List<YdOrderEntity> orders = ydOrderDao.selectTeamAllPower(member.getInviteId()); |
| | | BigDecimal totalPower = BigDecimal.ZERO; |
| | | if (CollUtil.isNotEmpty(orders)) { |
| | | for (YdOrderEntity order : orders) { |
| | | if ("P".equals(order.getYdProductEntity().getProUnit())) { |
| | | totalPower = totalPower.add(BigDecimal.valueOf(order.getQuantity() * order.getYdProductEntity().getProNum() * 1024)); |
| | | } else { |
| | | totalPower = totalPower.add(BigDecimal.valueOf(order.getQuantity() * order.getYdProductEntity().getProNum())); |
| | | } |
| | | } |
| | | } |
| | | |
| | | BigDecimal needPower = BigDecimal.ZERO; |
| | | if ("P".equals(basicLevelSetting.getUnit())) { |
| | | needPower = basicLevelSetting.getCalculationPower().multiply(BigDecimal.valueOf(1024)); |
| | | } else { |
| | | needPower = basicLevelSetting.getCalculationPower(); |
| | | } |
| | | return totalPower.compareTo(needPower) >= 0; |
| | | } |
| | | |
| | | /** |
| | | * 总代 团队(包含自己)算力达到指定数量,并下级存在3个市代且三个市代不在同一条线 |
| | | * |
| | | * @param member |
| | | * @param basicLevelSetting |
| | | * @return |
| | | */ |
| | | private Boolean becomeZd(MemberEntity member, YdBasicLevelSettingEntity basicLevelSetting) { |
| | | // 判断算力是否达到标准 |
| | | if(!becomeSd(member, basicLevelSetting)) { |
| | | return false; |
| | | } |
| | | |
| | | // 判断下级存在3个市代且不在同一条线上 |
| | | List<MemberEntity> childs = memberDao.selectMemberByRefererId(member.getInviteId()); |
| | | int i = 0; |
| | | if (CollUtil.isNotEmpty(childs)) { |
| | | for (MemberEntity child : childs) { |
| | | List<MemberEntity> agents = memberDao.selectTeamAgentList(child.getInviteId()); |
| | | if (agents.size() > 0) { |
| | | i++; |
| | | } |
| | | } |
| | | } |
| | | return i >= 3; |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void agentUsdtProfitDistributor() { |
| | | List<YdOrderEntity> orders = ydOrderDao.selectNeedReturnOrders(); |
| | | |
| | | if (CollUtil.isNotEmpty(orders)) { |
| | | for (YdOrderEntity order : orders) { |
| | | MemberEntity memberEntity = memberDao.selectById(order.getMemberId()); |
| | | List<String> inviteIds = StrUtil.split(memberEntity.getRefererIds(), ','); |
| | | List<MemberEntity> agents = memberDao.selectYdParentAgent(inviteIds); |
| | | usdtProfitDistributor(order); |
| | | } |
| | | } |
| | | } |
| | | |
| | | if (CollUtil.isNotEmpty(agents)) { |
| | | Map<Long, BigDecimal> returnRatio = buildReturnRatioObj(agents, 1); |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void usdtProfitDistributorByOrderId(Long id) { |
| | | YdOrderEntity ydOrderEntity = ydOrderDao.selectById(id); |
| | | usdtProfitDistributor(ydOrderEntity); |
| | | |
| | | for (Map.Entry<Long, BigDecimal> entry : returnRatio.entrySet()) { |
| | | String conent = "USDT返利"; |
| | | BigDecimal amount = order.getAmount().multiply(entry.getValue()); |
| | | LogRecordUtils.insertMemberAccountMoneyChangeWithId(entry.getKey(), conent, amount, "USDT", 1, 6, order.getId()); |
| | | MemberWalletCoinEntity walletCoin = memberWalletCoinDao.selectWalletCoinBymIdAndCode(entry.getKey(), "USDT"); |
| | | yunDingProducter.sendYunDingAutoAgent(ydOrderEntity.getMemberId()); |
| | | } |
| | | |
| | | memberWalletCoinDao.updateBlockBalance(walletCoin.getId(), amount, BigDecimal.ZERO, 0); |
| | | } |
| | | private void usdtProfitDistributor(YdOrderEntity order) { |
| | | MemberEntity memberEntity = memberDao.selectById(order.getMemberId()); |
| | | List<String> inviteIds = StrUtil.split(memberEntity.getRefererIds(), ','); |
| | | List<MemberEntity> agents = memberDao.selectYdParentAgent(inviteIds); |
| | | |
| | | YdOrderEntity updateOrder = new YdOrderEntity(); |
| | | updateOrder.setReturnState(2); |
| | | updateOrder.setId(order.getId()); |
| | | ydOrderDao.updateById(updateOrder); |
| | | } |
| | | // if (CollUtil.isNotEmpty(agents)) { |
| | | // Map<Long, BigDecimal> returnRatio = buildReturnRatioObj(agents, 1); |
| | | // |
| | | // for (Map.Entry<Long, BigDecimal> entry : returnRatio.entrySet()) { |
| | | // String conent = "USDT返利"; |
| | | // BigDecimal amount = order.getAmount().multiply(entry.getValue()); |
| | | // LogRecordUtils.insertMemberAccountMoneyChangeWithId(entry.getKey(), conent, amount, "USDT", 1, 6, order.getId()); |
| | | // MemberWalletCoinEntity walletCoin = memberWalletCoinDao.selectWalletCoinBymIdAndCode(entry.getKey(), "USDT"); |
| | | // |
| | | // memberWalletCoinDao.updateBlockBalance(walletCoin.getId(), amount, BigDecimal.ZERO, 0); |
| | | // } |
| | | // |
| | | // YdOrderEntity updateOrder = new YdOrderEntity(); |
| | | // updateOrder.setReturnState(2); |
| | | // updateOrder.setId(order.getId()); |
| | | // ydOrderDao.updateById(updateOrder); |
| | | // } |
| | | |
| | | // 合伙人分红 |
| | | List<MemberEntity> partners = memberDao.selectPartnerMemberList(); |
| | | YdBasicSettingEntity setting = ydBasicSettingDao.selectById(1L); |
| | | if (CollUtil.isNotEmpty(partners)) { |
| | | BigDecimal returnAmount = order.getAmount().multiply(setting.getPartnerRatio()).divide(BigDecimal.valueOf(partners.size()), 8, BigDecimal.ROUND_DOWN); |
| | | log.info("合伙人分红:{}", returnAmount); |
| | | for (MemberEntity partner : partners) { |
| | | String conent = "合伙人USDT分红"; |
| | | LogRecordUtils.insertMemberAccountMoneyChangeWithId(partner.getId(), conent, returnAmount, "USDT", 1, 9, order.getId()); |
| | | // MemberWalletCoinEntity walletCoin = memberWalletCoinDao.selectWalletCoinBymIdAndCode(partner.getId(), "USDT"); |
| | | // memberWalletCoinDao.updateBlockBalance(walletCoin.getId(), returnAmount, BigDecimal.ZERO, 0); |
| | | } |
| | | } |
| | | |
| | | if(!AppContants.SYSTEM_REFERER.equals(memberEntity.getRefererId())) { |
| | | // 直推返利 |
| | | MemberEntity parentNode = memberDao.selectMemberInfoByInviteId(memberEntity.getRefererId()); |
| | | if (parentNode != null) { |
| | | BigDecimal parentAmount = order.getAmount().multiply(setting.getParentRatio()); |
| | | |
| | | String conent = "直推USDT返利"; |
| | | LogRecordUtils.insertMemberAccountMoneyChangeWithId(parentNode.getId(), conent, parentAmount, "USDT", 1, 10, order.getId()); |
| | | MemberWalletCoinEntity walletCoin = memberWalletCoinDao.selectWalletCoinBymIdAndCode(parentNode.getId(), "USDT"); |
| | | |
| | | memberWalletCoinDao.updateBlockBalance(walletCoin.getId(), parentAmount, BigDecimal.ZERO, 0); |
| | | } |
| | | } |
| | | } |
| | |
| | | private Map<Long, BigDecimal> buildReturnRatioObj(List<MemberEntity> agents, int type) { |
| | | Map<Long, BigDecimal> returnRatio = new HashMap<Long, BigDecimal>(); |
| | | Long lastId = null; |
| | | int i = 0; |
| | | for (MemberEntity agent : agents) { |
| | | YdBasicLevelSettingEntity settingEntity = agent.getYdBasicLevelSettingEntity(); |
| | | |
| | |
| | | } |
| | | if(settingEntity.getLevel() == 1) { |
| | | if (CollUtil.isNotEmpty(returnRatio)) { |
| | | BigDecimal lastRatio = returnRatio.get(lastId); |
| | | returnRatio.put(agent.getId(), ratio.subtract(lastRatio)); |
| | | if (i != 0) { |
| | | returnRatio.put(agent.getId(), BigDecimal.valueOf(0.01)); |
| | | } else { |
| | | BigDecimal lastRatio = returnRatio.get(lastId); |
| | | returnRatio.put(agent.getId(), ratio.subtract(lastRatio)); |
| | | } |
| | | } else { |
| | | returnRatio.put(agent.getId(), ratio); |
| | | } |
| | | break; |
| | | |
| | | if (i >= 1) { |
| | | break; |
| | | } |
| | | i++; |
| | | } |
| | | |
| | | if (CollUtil.isEmpty(returnRatio)) { |
| | |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void xchProfitDistributor(BigDecimal totalProfit) { |
| | | Date currentDate = new Date(); |
| | | List<YdProductEntity> products = ydProductDao.selectList(null); |
| | | List<YdProductEntity> products = ydProductDao.selectXchProductList(); |
| | | if (CollUtil.isNotEmpty(products)) { |
| | | BigDecimal totalCount = BigDecimal.ZERO; |
| | | for (YdProductEntity product : products) { |
| | | BigDecimal count = product.getTotalT(); |
| | | BigDecimal count = product.getTotalT().multiply(BigDecimal.valueOf(product.getProNum())); |
| | | if ("P".equals(product.getProUnit())) { |
| | | count = count.multiply(BigDecimal.valueOf(1024)); |
| | | } |
| | |
| | | |
| | | // 单位XCH收益 |
| | | BigDecimal unitProfit = totalProfit.divide(totalCount, 8, BigDecimal.ROUND_DOWN); |
| | | log.info("单位XCH收益:{}", unitProfit); |
| | | |
| | | List<MemberEntity> partners = memberDao.selectPartnerMemberList(); |
| | | YdBasicSettingEntity setting = ydBasicSettingDao.selectById(1L); |
| | | |
| | | List<YdOrderEntity> orders = ydOrderDao.selectAllValidOrders(); |
| | | if (CollUtil.isNotEmpty(orders)) { |
| | |
| | | List<String> inviteIds = StrUtil.split(memberEntity.getRefererIds(), ','); |
| | | List<MemberEntity> agents = memberDao.selectYdParentAgent(inviteIds); |
| | | |
| | | BigDecimal count = BigDecimal.valueOf(order.getQuantity()); |
| | | BigDecimal count = BigDecimal.valueOf(order.getQuantity() * order.getYdProductEntity().getProNum()); |
| | | if ("P".equals(order.getYdProductEntity().getProUnit())) { |
| | | count = count.multiply(BigDecimal.valueOf(1024)); |
| | | } |
| | | |
| | | // 订单总收益 |
| | | BigDecimal orderProfit = count.multiply(unitProfit); |
| | | BigDecimal remainProfit = orderProfit; |
| | | if (CollUtil.isNotEmpty(agents)) { |
| | |
| | | } |
| | | } |
| | | |
| | | BigDecimal partnerAmount = orderProfit.multiply(setting.getPartnerXchRatio()); |
| | | remainProfit = remainProfit.subtract(partnerAmount); |
| | | // 合伙人收益 |
| | | if (CollUtil.isNotEmpty(partners)) { |
| | | BigDecimal unitPartnerAmount = partnerAmount.divide(BigDecimal.valueOf(partners.size()), 8, BigDecimal.ROUND_DOWN); |
| | | for (MemberEntity partner : partners) { |
| | | String conent = "合伙人XCH分红"; |
| | | LogRecordUtils.insertMemberAccountMoneyChangeWithId(partner.getId(), conent, unitPartnerAmount, "XCH", 1, 10, order.getId()); |
| | | } |
| | | } |
| | | |
| | | remainProfit = remainProfit.subtract(orderProfit.multiply(order.getYdProductEntity().getManageExpense())); |
| | | String content = "XCH收益"; |
| | | LogRecordUtils.insertMemberAccountMoneyChangeWithId(order.getMemberId(), content, remainProfit, "XCH", 1, 4, order.getId()); |
| | | MemberWalletCoinEntity walletCoin = memberWalletCoinDao.selectWalletCoinBymIdAndCode(order.getMemberId(), "XCH"); |
| | | |
| | | ydOrderDao.updateOrderProfit(remainProfit, order.getId()); |
| | | memberWalletCoinDao.updateBlockBalance(walletCoin.getId(), remainProfit, BigDecimal.ZERO, 0); |
| | | } |
| | | } |