package com.xcong.excoin.modules.yunding.service.Impl; 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 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.YdOrderDao; import com.xcong.excoin.modules.yunding.dao.YdProductDao; import com.xcong.excoin.modules.yunding.entity.YdBasicLevelSettingEntity; 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.utils.LogRecordUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.math.BigDecimal; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @Service public class XchProfitServiceImpl implements XchProfitService { @Autowired private MemberDao memberDao; @Autowired private YdOrderDao ydOrderDao; @Autowired private MemberWalletCoinDao memberWalletCoinDao; @Autowired private YdProductDao ydProductDao; @Override public void autoBeAgent(Long id) { } @Override public void agentUsdtProfitDistributor() { List orders = ydOrderDao.selectNeedReturnOrders(); if (CollUtil.isNotEmpty(orders)) { for (YdOrderEntity order : orders) { usdtProfitDistributor(order); } } } @Override public void usdtProfitDistributorByOrderId(Long id) { YdOrderEntity ydOrderEntity = ydOrderDao.selectById(id); usdtProfitDistributor(ydOrderEntity); } private void usdtProfitDistributor(YdOrderEntity order) { MemberEntity memberEntity = memberDao.selectById(order.getMemberId()); List inviteIds = StrUtil.split(memberEntity.getRefererIds(), ','); List agents = memberDao.selectYdParentAgent(inviteIds); if (CollUtil.isNotEmpty(agents)) { Map returnRatio = buildReturnRatioObj(agents, 1); for (Map.Entry 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); } } private Map buildReturnRatioObj(List agents, int type) { Map returnRatio = new HashMap(); Long lastId = null; for (MemberEntity agent : agents) { YdBasicLevelSettingEntity settingEntity = agent.getYdBasicLevelSettingEntity(); BigDecimal ratio; if (type == 1) { ratio = settingEntity.getUsdtRatio(); } else { ratio = settingEntity.getXchRatio(); } if(settingEntity.getLevel() == 1) { if (CollUtil.isNotEmpty(returnRatio)) { BigDecimal lastRatio = returnRatio.get(lastId); returnRatio.put(agent.getId(), ratio.subtract(lastRatio)); } else { returnRatio.put(agent.getId(), ratio); } break; } if (CollUtil.isEmpty(returnRatio)) { lastId = agent.getId(); returnRatio.put(agent.getId(), ratio); } } return returnRatio; } @Override public void xchProfitDistributor(BigDecimal totalProfit) { Date currentDate = new Date(); List products = ydProductDao.selectList(null); if (CollUtil.isNotEmpty(products)) { BigDecimal totalCount = BigDecimal.ZERO; for (YdProductEntity product : products) { BigDecimal count = product.getTotalT(); if ("P".equals(product.getProUnit())) { count = count.multiply(BigDecimal.valueOf(1024)); } totalCount = totalCount.add(count); } // 单位XCH收益 BigDecimal unitProfit = totalProfit.divide(totalCount, 8, BigDecimal.ROUND_DOWN); List orders = ydOrderDao.selectAllValidOrders(); if (CollUtil.isNotEmpty(orders)) { for (YdOrderEntity order : orders) { long between = DateUtil.between(order.getWorkTime(), currentDate, DateUnit.DAY); if (between < 1) { continue; } MemberEntity memberEntity = memberDao.selectById(order.getMemberId()); List inviteIds = StrUtil.split(memberEntity.getRefererIds(), ','); List agents = memberDao.selectYdParentAgent(inviteIds); BigDecimal count = BigDecimal.valueOf(order.getQuantity()); if ("P".equals(order.getYdProductEntity().getProUnit())) { count = count.multiply(BigDecimal.valueOf(1024)); } BigDecimal orderProfit = count.multiply(unitProfit); BigDecimal remainProfit = orderProfit; if (CollUtil.isNotEmpty(agents)) { Map returnRatio = buildReturnRatioObj(agents, 2); for (Map.Entry entry : returnRatio.entrySet()) { String content = "XCH收益返利"; BigDecimal amount = entry.getValue().multiply(orderProfit); LogRecordUtils.insertMemberAccountMoneyChangeWithId(entry.getKey(), content, amount, "XCH", 1, 5, order.getId()); MemberWalletCoinEntity walletCoin = memberWalletCoinDao.selectWalletCoinBymIdAndCode(entry.getKey(), "XCH"); memberWalletCoinDao.updateBlockBalance(walletCoin.getId(), amount, BigDecimal.ZERO, 0); remainProfit = remainProfit.subtract(amount); } } String content = "XCH收益"; LogRecordUtils.insertMemberAccountMoneyChangeWithId(order.getMemberId(), content, remainProfit, "XCH", 1, 4, order.getId()); MemberWalletCoinEntity walletCoin = memberWalletCoinDao.selectWalletCoinBymIdAndCode(order.getMemberId(), "XCH"); memberWalletCoinDao.updateBlockBalance(walletCoin.getId(), remainProfit, BigDecimal.ZERO, 0); } } } } }