|
|
|
@ -1,11 +1,24 @@ |
|
|
|
|
package com.blockchain.server.yyyf.indicators; |
|
|
|
|
|
|
|
|
|
import cn.hutool.core.map.MapUtil; |
|
|
|
|
import com.blockchain.common.base.dto.ResultDTO; |
|
|
|
|
import com.blockchain.server.yyyf.dto.UserTimeMoneyDto; |
|
|
|
|
import com.blockchain.server.yyyf.entity.AssessUserTarget; |
|
|
|
|
import com.blockchain.server.yyyf.feign.BtcFeign; |
|
|
|
|
import com.blockchain.server.yyyf.feign.CurrencyFeign; |
|
|
|
|
import com.blockchain.server.yyyf.feign.EosFeign; |
|
|
|
|
import com.blockchain.server.yyyf.feign.EthFeign; |
|
|
|
|
import com.blockchain.server.yyyf.feign.dto.BtcWalletDTO; |
|
|
|
|
import com.blockchain.server.yyyf.feign.dto.CurrencyMarketDTO; |
|
|
|
|
import com.blockchain.server.yyyf.feign.dto.EosWalletDTO; |
|
|
|
|
import com.blockchain.server.yyyf.feign.dto.EthWalletDTO; |
|
|
|
|
import com.blockchain.server.yyyf.indicators.dto.AppTypeEnum; |
|
|
|
|
import com.blockchain.server.yyyf.indicators.dto.CurrencyEnum; |
|
|
|
|
import com.blockchain.server.yyyf.indicators.dto.IndicatorsTypeEnum; |
|
|
|
|
import com.blockchain.server.yyyf.mapper.AssessUserMapper; |
|
|
|
|
import com.blockchain.server.yyyf.mapper.AssessUserTargetMapper; |
|
|
|
|
import com.github.benmanes.caffeine.cache.Cache; |
|
|
|
|
import com.github.benmanes.caffeine.cache.Caffeine; |
|
|
|
|
import lombok.AllArgsConstructor; |
|
|
|
|
import org.springframework.context.ApplicationContext; |
|
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
@ -13,6 +26,11 @@ import org.springframework.stereotype.Service; |
|
|
|
|
import java.math.BigDecimal; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Map; |
|
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
|
import static cn.hutool.core.util.NumberUtil.add; |
|
|
|
|
import static cn.hutool.core.util.NumberUtil.mul; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @author fengyu |
|
|
|
@ -23,6 +41,16 @@ public class DefaultIndicatorsManager implements IndicatorsManager { |
|
|
|
|
private final ApplicationContext applicationContext; |
|
|
|
|
private final AssessUserTargetMapper assessUserTargetMapper; |
|
|
|
|
private final AssessUserMapper assessUserMapper; |
|
|
|
|
private final BtcFeign btcFeign; |
|
|
|
|
private final EosFeign eosFeign; |
|
|
|
|
private final EthFeign ethFeign; |
|
|
|
|
private final CurrencyFeign currencyFeign; |
|
|
|
|
|
|
|
|
|
private static final String CURRENCY_CACHE_KEY = "currency"; |
|
|
|
|
private static final Cache<String, List<CurrencyMarketDTO>> CURRENCY_MARKET_CACHE = Caffeine.newBuilder() |
|
|
|
|
.expireAfterWrite(1, TimeUnit.HOURS) |
|
|
|
|
.maximumSize(5) |
|
|
|
|
.build(); |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Map<String, Float> calculatePageIndicators(String assessUserId) { |
|
|
|
@ -37,17 +65,70 @@ public class DefaultIndicatorsManager implements IndicatorsManager { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public BigDecimal calculateCurrentTotalAssets(String userId, String assessUserId) { |
|
|
|
|
//feign interface call
|
|
|
|
|
//币币总资产 + 法币总资产 + 冻结资金
|
|
|
|
|
List<CurrencyMarketDTO> currencyMarketDto = CURRENCY_MARKET_CACHE.get(CURRENCY_CACHE_KEY, t -> currencyFeign.getList()); |
|
|
|
|
assert currencyMarketDto != null; |
|
|
|
|
Map<String, BigDecimal> currencyPairMap = currencyMarketDto.stream() |
|
|
|
|
.collect(Collectors.toMap(CurrencyMarketDTO::getCurrencyPair, CurrencyMarketDTO::getAmount)); |
|
|
|
|
BigDecimal currencyAssets = getAssetsByUserId(userId, currencyPairMap); |
|
|
|
|
//数字钱包
|
|
|
|
|
UserTimeMoneyDto timeMoneyDto = assessUserMapper.selectCurrentUserTimeMoneyDto(assessUserId, userId); |
|
|
|
|
BigDecimal digitalAssets = getDigitalAssets(timeMoneyDto, currencyPairMap); |
|
|
|
|
//初始资产剩余资产 单位RMB
|
|
|
|
|
BigDecimal remainingAssets = getRemainingAssets(timeMoneyDto); |
|
|
|
|
//计算总资产 转换成RMB
|
|
|
|
|
Double rmbRate = currencyMarketDto.stream().filter(dto -> dto.getCurrencyPair().equals(CurrencyEnum.BTC_USDT.getCode())).map(CurrencyMarketDTO::getCnyAmount).findFirst().orElse(7.0); |
|
|
|
|
return add(mul(add(currencyAssets, digitalAssets), rmbRate), remainingAssets); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private BigDecimal getRemainingAssets(UserTimeMoneyDto timeMoneyDto) { |
|
|
|
|
return add(timeMoneyDto.getAliPay(), timeMoneyDto.getWxPay(), timeMoneyDto.getBankCard()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private BigDecimal getDigitalAssets(UserTimeMoneyDto timeMoneyDto, Map<String, BigDecimal> currencyPairMap) { |
|
|
|
|
BigDecimal btcMoney = mul(timeMoneyDto.getBtc(), currencyPairMap.get(CurrencyEnum.BTC_USDT.getCode())); |
|
|
|
|
BigDecimal usdtMoney = timeMoneyDto.getUsdt(); |
|
|
|
|
BigDecimal eosMoney = mul(timeMoneyDto.getEos(), currencyPairMap.get(CurrencyEnum.EOS_USDT.getCode())); |
|
|
|
|
BigDecimal ethMoney = mul(timeMoneyDto.getEth(), currencyPairMap.get(CurrencyEnum.ETH_USDT.getCode())); |
|
|
|
|
return add(btcMoney, usdtMoney, eosMoney, ethMoney); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//1.获取 币币总资产 + 法币总资产 + 数字钱包 + 初始资产 + 挂单资金
|
|
|
|
|
//cache rate
|
|
|
|
|
//2.计算总资产 转换成RMB
|
|
|
|
|
//cache RMB rate
|
|
|
|
|
return null; |
|
|
|
|
private BigDecimal getAssetsByUserId(String userId, Map<String, BigDecimal> currencyPairMap) { |
|
|
|
|
ResultDTO<List<BtcWalletDTO>> btcWallets = btcFeign.getWallets(userId); |
|
|
|
|
ResultDTO<List<EthWalletDTO>> ethWallets = ethFeign.getWallets(userId); |
|
|
|
|
ResultDTO<List<EosWalletDTO>> eosWallets = eosFeign.selectWalletByWalletType(userId); |
|
|
|
|
BigDecimal c2cAssets = getAssetsByType(AppTypeEnum.C2C, currencyPairMap, btcWallets.getData(), ethWallets.getData(), eosWallets.getData()); |
|
|
|
|
BigDecimal cctAssets = getAssetsByType(AppTypeEnum.CCT, currencyPairMap, btcWallets.getData(), ethWallets.getData(), eosWallets.getData()); |
|
|
|
|
return add(c2cAssets, cctAssets); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private BigDecimal getAssetsByType(AppTypeEnum appType, Map<String, BigDecimal> currencyPairMap, |
|
|
|
|
List<BtcWalletDTO> btcWallets, List<EthWalletDTO> ethWallets, |
|
|
|
|
List<EosWalletDTO> eosWallets) { |
|
|
|
|
//BTC
|
|
|
|
|
Map<String, BigDecimal> btcBalanceMap = btcWallets.stream() |
|
|
|
|
.filter(dto -> dto.getWalletType().equals(appType.name())) |
|
|
|
|
.collect(Collectors.toMap(BtcWalletDTO::getTokenSymbol, dto -> BigDecimal.valueOf(dto.getBalance() + dto.getFreezeBalance()))); |
|
|
|
|
BigDecimal btcAmount = currencyPairMap.get(CurrencyEnum.BTC_USDT.getCode()); |
|
|
|
|
BigDecimal btcPrice = mul(btcAmount, btcBalanceMap.get(CurrencyEnum.BTC.getCode())); |
|
|
|
|
//USDT
|
|
|
|
|
BigDecimal usdtPrice = btcBalanceMap.get(CurrencyEnum.USDT.getCode()); |
|
|
|
|
|
|
|
|
|
//ETH
|
|
|
|
|
Map<String, BigDecimal> ethBalanceMap = ethWallets.stream() |
|
|
|
|
.filter(dto -> dto.getWalletType().equals(appType.name())) |
|
|
|
|
.collect(Collectors.toMap(EthWalletDTO::getTokenSymbol, dto -> BigDecimal.valueOf(Double.parseDouble(dto.getBalance()) + Double.parseDouble(dto.getFreezeBalance())))); |
|
|
|
|
BigDecimal ethAmount = currencyPairMap.get(CurrencyEnum.ETH_USDT.getCode()); |
|
|
|
|
BigDecimal ethPrice = mul(ethAmount, ethBalanceMap.get(CurrencyEnum.ETH.getCode())); |
|
|
|
|
|
|
|
|
|
//EOS
|
|
|
|
|
Map<String, BigDecimal> eosBalanceMap = eosWallets.stream() |
|
|
|
|
.filter(dto -> dto.getWalletType().equals(appType.name())) |
|
|
|
|
.collect(Collectors.toMap(EosWalletDTO::getTokenSymbol, dto -> add(dto.getBalance(), dto.getFreezeBalance()))); |
|
|
|
|
BigDecimal eosAmount = currencyPairMap.get(CurrencyEnum.EOS_USDT.getCode()); |
|
|
|
|
BigDecimal eosPrice = mul(eosAmount, eosBalanceMap.get(CurrencyEnum.EOS.getCode())); |
|
|
|
|
return add(btcPrice, usdtPrice, ethPrice, eosPrice); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* calculate single indicator |
|
|
|
@ -62,4 +143,5 @@ public class DefaultIndicatorsManager implements IndicatorsManager { |
|
|
|
|
IndicatorsHandler indicatorsHandler = applicationContext.getBean(type.getCode(), IndicatorsHandler.class); |
|
|
|
|
return indicatorsHandler.handleIndicator(assessUserId); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|