From c0355bc45e03512c67a2d411223647ca5b5b4bd8 Mon Sep 17 00:00:00 2001 From: "fengyu.wang" Date: Wed, 10 Jun 2020 23:57:42 +0800 Subject: [PATCH] indicator support --- .../server/btc/inner/BtcWalletInner.java | 5 +- .../server/btc/mapper/BtcWalletMapper.java | 4 +- .../server/btc/service/BtcWalletService.java | 8 ++ .../service/impl/BtcWalletServiceImpl.java | 5 + .../main/resources/mapper/BtcWalletMapper.xml | 8 +- .../eos/inner/EosWalletInnerController.java | 7 +- .../server/eos/mapper/WalletMapper.java | 8 ++ .../server/eos/service/EosWalletService.java | 8 ++ .../service/impl/EosWalletServiceImpl.java | 6 ++ .../main/resources/mapper/WalletMapper.xml | 8 +- .../server/eth/inner/EthWalletInner.java | 5 +- .../server/eth/mapper/EthWalletMapper.java | 10 +- .../server/eth/service/IEthWalletService.java | 8 ++ .../service/impl/EthWalletServiceImpl.java | 7 ++ .../main/resources/mapper/EthWalletMapper.xml | 8 +- .../blockchain-server-yyyf/pom.xml | 5 + .../server/yyyf/feign/BtcFeign.java | 3 +- .../server/yyyf/feign/CurrencyFeign.java | 6 +- .../server/yyyf/feign/EosFeign.java | 4 +- .../server/yyyf/feign/EthFeign.java | 3 +- .../indicators/DefaultIndicatorsManager.java | 94 +++++++++++++++++-- .../yyyf/indicators/dto/AppTypeEnum.java | 12 +++ .../yyyf/indicators/dto/CurrencyEnum.java | 24 +++++ .../indicators/dto/IndicatorsTypeEnum.java | 4 +- ...setLiabilityRiskRateIndicatorsHandler.java | 17 ---- 25 files changed, 227 insertions(+), 50 deletions(-) create mode 100644 blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/dto/AppTypeEnum.java create mode 100644 blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/dto/CurrencyEnum.java delete mode 100644 blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/AssetLiabilityRiskRateIndicatorsHandler.java diff --git a/blockchain-server/blockchain-server-btc/src/main/java/com/blockchain/server/btc/inner/BtcWalletInner.java b/blockchain-server/blockchain-server-btc/src/main/java/com/blockchain/server/btc/inner/BtcWalletInner.java index f8bd558..e51e8cf 100644 --- a/blockchain-server/blockchain-server-btc/src/main/java/com/blockchain/server/btc/inner/BtcWalletInner.java +++ b/blockchain-server/blockchain-server-btc/src/main/java/com/blockchain/server/btc/inner/BtcWalletInner.java @@ -34,9 +34,8 @@ public class BtcWalletInner { } @GetMapping("/getWallets") - public ResultDTO> getWallets(@RequestParam("userId") String userId, - @RequestParam("walletType") String walletType) { - return ResultDTO.requstSuccess(btcWalletService.selectAllByUserOpenId(userId, walletType)); + public ResultDTO> getWallets(@RequestParam("userId") String userId) { + return ResultDTO.requstSuccess(btcWalletService.selectAllByUserOpenId(userId)); } } diff --git a/blockchain-server/blockchain-server-btc/src/main/java/com/blockchain/server/btc/mapper/BtcWalletMapper.java b/blockchain-server/blockchain-server-btc/src/main/java/com/blockchain/server/btc/mapper/BtcWalletMapper.java index ba8fb75..6e8c816 100644 --- a/blockchain-server/blockchain-server-btc/src/main/java/com/blockchain/server/btc/mapper/BtcWalletMapper.java +++ b/blockchain-server/blockchain-server-btc/src/main/java/com/blockchain/server/btc/mapper/BtcWalletMapper.java @@ -69,6 +69,8 @@ public interface BtcWalletMapper extends Mapper { */ List selectAllByUserOpenId(@Param("userOpenId") String userOpenId, @Param("walletType") String walletType); + List selectAll(@Param("userOpenId") String userOpenId); + /** * 用户充值修改钱包金额 * @@ -78,4 +80,4 @@ public interface BtcWalletMapper extends Mapper { @Param("tokenSymbol") String tokenSymbol, @Param("walletType") String walletType, @Param("modifyTime") Date modifyTime); -} \ No newline at end of file +} diff --git a/blockchain-server/blockchain-server-btc/src/main/java/com/blockchain/server/btc/service/BtcWalletService.java b/blockchain-server/blockchain-server-btc/src/main/java/com/blockchain/server/btc/service/BtcWalletService.java index db4a62a..bfe1657 100644 --- a/blockchain-server/blockchain-server-btc/src/main/java/com/blockchain/server/btc/service/BtcWalletService.java +++ b/blockchain-server/blockchain-server-btc/src/main/java/com/blockchain/server/btc/service/BtcWalletService.java @@ -27,6 +27,14 @@ public interface BtcWalletService { */ List selectAllByUserOpenId(String userOpenId, String walletType); + /** + * 获取用户某应用钱包的所有区块 + * + * @param userOpenId 用户id + * @return + */ + List selectAllByUserOpenId(String userOpenId); + /** * 获取用户钱包 * diff --git a/blockchain-server/blockchain-server-btc/src/main/java/com/blockchain/server/btc/service/impl/BtcWalletServiceImpl.java b/blockchain-server/blockchain-server-btc/src/main/java/com/blockchain/server/btc/service/impl/BtcWalletServiceImpl.java index de108ff..9b5ab60 100644 --- a/blockchain-server/blockchain-server-btc/src/main/java/com/blockchain/server/btc/service/impl/BtcWalletServiceImpl.java +++ b/blockchain-server/blockchain-server-btc/src/main/java/com/blockchain/server/btc/service/impl/BtcWalletServiceImpl.java @@ -91,6 +91,11 @@ public class BtcWalletServiceImpl implements BtcWalletService { return btcWalletMapper.selectAllByUserOpenId(userOpenId, walletType); } + @Override + public List selectAllByUserOpenId(String userOpenId) { + return btcWalletMapper.selectAll(userOpenId); + } + @Override public BtcWalletDTO selectByUserOpenId(String userOpenId, Integer tokenId, String walletType) { return btcWalletMapper.selectByUserOpenId(userOpenId, tokenId, walletType); diff --git a/blockchain-server/blockchain-server-btc/src/main/resources/mapper/BtcWalletMapper.xml b/blockchain-server/blockchain-server-btc/src/main/resources/mapper/BtcWalletMapper.xml index 3f408c5..4ac1428 100644 --- a/blockchain-server/blockchain-server-btc/src/main/resources/mapper/BtcWalletMapper.xml +++ b/blockchain-server/blockchain-server-btc/src/main/resources/mapper/BtcWalletMapper.xml @@ -30,6 +30,12 @@ WHERE user_open_id = #{userOpenId} AND wallet_type = #{walletType} + + + + + + UPDATE @@ -135,4 +141,4 @@ AND wallet.token_symbol = #{tokenSymbol} AND wallet.wallet_type = #{walletType} - \ No newline at end of file + diff --git a/blockchain-server/blockchain-server-yyyf/pom.xml b/blockchain-server/blockchain-server-yyyf/pom.xml index 220da63..4970867 100644 --- a/blockchain-server/blockchain-server-yyyf/pom.xml +++ b/blockchain-server/blockchain-server-yyyf/pom.xml @@ -73,6 +73,11 @@ hutool-core 5.3.6 + + com.github.ben-manes.caffeine + caffeine + 2.8.4 + diff --git a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/feign/BtcFeign.java b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/feign/BtcFeign.java index 6a9e3b2..c095b2f 100644 --- a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/feign/BtcFeign.java +++ b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/feign/BtcFeign.java @@ -13,6 +13,5 @@ public interface BtcFeign { String CONTENT_PATH = "/inner/wallet"; @PostMapping(CONTENT_PATH + "/getWallets") - ResultDTO> getWallets(@RequestParam("userId") String userId, - @RequestParam("walletType") String walletType); + ResultDTO> getWallets(@RequestParam("userId") String userId); } diff --git a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/feign/CurrencyFeign.java b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/feign/CurrencyFeign.java index c631a0c..01a2bd8 100644 --- a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/feign/CurrencyFeign.java +++ b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/feign/CurrencyFeign.java @@ -1,13 +1,13 @@ package com.blockchain.server.yyyf.feign; -import com.blockchain.common.base.dto.ResultDTO; import com.blockchain.server.yyyf.feign.dto.CurrencyMarketDTO; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestParam; + +import java.util.List; @FeignClient(name = "dapp-currency-server") public interface CurrencyFeign { @GetMapping("/inner/market/getList") - ResultDTO getList(@RequestParam("currencyPair") String currencyPair); + List getList(); } diff --git a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/feign/EosFeign.java b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/feign/EosFeign.java index e29f7c3..0b9725a 100644 --- a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/feign/EosFeign.java +++ b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/feign/EosFeign.java @@ -9,6 +9,6 @@ import java.util.List; @FeignClient("dapp-eos-server") public interface EosFeign { - @GetMapping("/inner/walletTx/selectWalletByWalletType") - ResultDTO> selectWalletByWalletType(String userOpenId, String walletType); + @GetMapping("/inner/walletTx/selectWalletByUserOpenId") + ResultDTO> selectWalletByWalletType(String userOpenId); } diff --git a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/feign/EthFeign.java b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/feign/EthFeign.java index 3fff3ae..360094b 100644 --- a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/feign/EthFeign.java +++ b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/feign/EthFeign.java @@ -12,8 +12,7 @@ import java.util.List; @FeignClient("dapp-eth-server") public interface EthFeign { @GetMapping("/inner/wallet/getWallets") - ResultDTO> getWallets(@RequestParam("userId") String userId, - @RequestParam(name = "walletType") String walletType); + ResultDTO> getWallets(@RequestParam("userId") String userId); @GetMapping("/inner/walletParam/getGasConfig") ResultDTO getGasConfig(String tokenSymbol); diff --git a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/DefaultIndicatorsManager.java b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/DefaultIndicatorsManager.java index a20f726..bdee034 100644 --- a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/DefaultIndicatorsManager.java +++ b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/DefaultIndicatorsManager.java @@ -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> CURRENCY_MARKET_CACHE = Caffeine.newBuilder() + .expireAfterWrite(1, TimeUnit.HOURS) + .maximumSize(5) + .build(); @Override public Map 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 = CURRENCY_MARKET_CACHE.get(CURRENCY_CACHE_KEY, t -> currencyFeign.getList()); + assert currencyMarketDto != null; + Map 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 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 currencyPairMap) { + ResultDTO> btcWallets = btcFeign.getWallets(userId); + ResultDTO> ethWallets = ethFeign.getWallets(userId); + ResultDTO> 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 currencyPairMap, + List btcWallets, List ethWallets, + List eosWallets) { + //BTC + Map 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 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 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); } + } diff --git a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/dto/AppTypeEnum.java b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/dto/AppTypeEnum.java new file mode 100644 index 0000000..21e86ba --- /dev/null +++ b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/dto/AppTypeEnum.java @@ -0,0 +1,12 @@ +package com.blockchain.server.yyyf.indicators.dto; + +/** + * @author fengyu + */ + +public enum AppTypeEnum { + /*法币交易*/ + C2C, + /*币币交易*/ + CCT +} diff --git a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/dto/CurrencyEnum.java b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/dto/CurrencyEnum.java new file mode 100644 index 0000000..c5c1ee7 --- /dev/null +++ b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/dto/CurrencyEnum.java @@ -0,0 +1,24 @@ +package com.blockchain.server.yyyf.indicators.dto; + +import lombok.Getter; + +/** + * @author fengyu + */ +public enum CurrencyEnum { + /*btc*/ + BTC("BTC"), + ETH("ETH"), + EOS("EOS"), + USDT("USDT"), + BTC_USDT("BTC-USDT"), + ETH_USDT("ETH-USDT"), + EOS_USDT("EOS-USDT"); + + @Getter + private final String code; + + CurrencyEnum(String code) { + this.code = code; + } +} diff --git a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/dto/IndicatorsTypeEnum.java b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/dto/IndicatorsTypeEnum.java index 48b0e2c..562890a 100644 --- a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/dto/IndicatorsTypeEnum.java +++ b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/dto/IndicatorsTypeEnum.java @@ -23,9 +23,7 @@ public enum IndicatorsTypeEnum { MAX_DRAWDOWN_RATE("MaxDrawdownRate"), /*最大亏损率*/ MAX_LOSS_RATE("MaxLossRate"), - /*风险率(资产负债比)*/ - ASSET_LIABILITY_RISK_RATE("AssetLiabilityRiskRate"), - /*TVPI*/ + /*Total Value to Paid-in Capital*/ TVPI("TVPI"); @Getter diff --git a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/AssetLiabilityRiskRateIndicatorsHandler.java b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/AssetLiabilityRiskRateIndicatorsHandler.java deleted file mode 100644 index 20baedb..0000000 --- a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/AssetLiabilityRiskRateIndicatorsHandler.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.blockchain.server.yyyf.indicators.handle; - -import com.blockchain.server.yyyf.indicators.IndicatorsHandler; -import org.springframework.stereotype.Component; - -/** - * @author fengyu - */ -@Component(AssetLiabilityRiskRateIndicatorsHandler.HANDLER_NAME + IndicatorsHandler.HANDLER_NAME) -public class AssetLiabilityRiskRateIndicatorsHandler implements IndicatorsHandler { - public static final String HANDLER_NAME = "AssetLiabilityRiskRate"; - - @Override - public Float handleIndicator(String assessUserId) { - return null; - } -}