From c16a12ca1209e104847c4e7a6f8b6b102267fd44 Mon Sep 17 00:00:00 2001 From: "huan.xu" <275942173@qq.com> Date: Thu, 4 Jun 2020 17:57:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8C=87=E6=A0=87=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AnnualGrowthRateIndicatorsHandler.java | 5 +- .../FluctuationRateIndicatorsHandler.java | 54 ++++++++++++++++++- .../handle/GrowthRateIndicatorsHandler.java | 2 +- .../ProfitLossRateRateIndicatorsHandler.java | 49 ++++++++++++++++- .../handle/ShapeRateIndicatorsHandler.java | 26 ++++++++- .../TotalGrowthRateIndicatorsHandler.java | 29 +++++++++- 6 files changed, 157 insertions(+), 8 deletions(-) diff --git a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/AnnualGrowthRateIndicatorsHandler.java b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/AnnualGrowthRateIndicatorsHandler.java index e6e822f..aa51fae 100644 --- a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/AnnualGrowthRateIndicatorsHandler.java +++ b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/AnnualGrowthRateIndicatorsHandler.java @@ -8,6 +8,7 @@ import org.springframework.stereotype.Component; import javax.persistence.EntityExistsException; import java.math.BigDecimal; +import java.util.ArrayList; import java.util.Comparator; import java.util.List; @@ -34,9 +35,9 @@ public class AnnualGrowthRateIndicatorsHandler implements IndicatorsHandler { .min(Comparator.comparingLong(AssetsReporter::getDays)) .orElseThrow(() -> new EntityExistsException("assessUser:" + assessUserId)); BigDecimal result = lastAssets.getAssets() - .divide(firstAssets.getAssets()) + .divide(firstAssets.getAssets(), 5, BigDecimal.ROUND_HALF_UP) .pow((int) (360.0 / lastAssets.getDays())) .subtract(BigDecimal.valueOf(1)); - return result.divide(BigDecimal.valueOf(100)).floatValue(); + return result.divide(BigDecimal.valueOf(100), 2, BigDecimal.ROUND_HALF_UP).floatValue(); } } diff --git a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/FluctuationRateIndicatorsHandler.java b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/FluctuationRateIndicatorsHandler.java index 0921810..dc1744f 100644 --- a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/FluctuationRateIndicatorsHandler.java +++ b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/FluctuationRateIndicatorsHandler.java @@ -1,17 +1,69 @@ package com.blockchain.server.yyyf.indicators.handle; import com.blockchain.server.yyyf.indicators.IndicatorsHandler; +import com.blockchain.server.yyyf.indicators.IndicatorsManager; +import com.blockchain.server.yyyf.indicators.dto.AssetsReporter; +import lombok.AllArgsConstructor; import org.springframework.stereotype.Component; +import javax.persistence.EntityExistsException; +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; + /** * @author fengyu */ +@AllArgsConstructor @Component(FluctuationRateIndicatorsHandler.HANDLER_NAME + IndicatorsHandler.HANDLER_NAME) public class FluctuationRateIndicatorsHandler implements IndicatorsHandler { public static final String HANDLER_NAME = "FluctuationRate"; + private final IndicatorsManager indicatorsManager; + @Override public Float handleIndicator(String assessUserId) { - return null; + List reporterList = indicatorsManager.getAssetsReporterListByAssessUserId(assessUserId); + if (reporterList != null && reporterList.size() > 0) { + //排序 + reporterList.sort(Comparator.comparingLong(AssetsReporter::getDays)); + //总天数 + int dayCount = reporterList.size() - 1; + //每日收益率List + List dayYieldList = new ArrayList(dayCount); + //收益率总和 + BigDecimal totalYield = BigDecimal.ZERO; + //上一天的资产总额 + BigDecimal upAssets = reporterList.get(0).getAssets(); + + //计算总收益率以及每日收益率 + for (AssetsReporter assetsReporter : reporterList) { + if (assetsReporter.getDays() > 0) { + BigDecimal thisAssets = assetsReporter.getAssets(); + BigDecimal subtract = thisAssets.subtract(upAssets); + //当天收益率 + BigDecimal thisYeid = subtract.multiply(BigDecimal.valueOf(100)).divide(upAssets, 5, BigDecimal.ROUND_HALF_UP); + //加入每日收益率集合 + dayYieldList.add(thisYeid); + //总收益率 + totalYield = totalYield.add(thisYeid); + //更换上一天的总额 + upAssets = thisAssets; + } + } + //平均收益率 + BigDecimal avgYeid = totalYield.divide(BigDecimal.valueOf(dayCount), 5, BigDecimal.ROUND_HALF_UP); + //(每日收益率-平均收益率)的平方和 + BigDecimal yeid2Count = BigDecimal.ZERO; + for (BigDecimal yeid : dayYieldList) { + yeid2Count = yeid2Count.add(yeid.subtract(avgYeid).pow(2)); + } + return Float.valueOf(String.format("%.2f", Math.sqrt(BigDecimal.valueOf(360).divide(BigDecimal.valueOf(dayCount - 1), 5, BigDecimal.ROUND_HALF_UP).multiply(yeid2Count).doubleValue()))); + + } else { + throw new EntityExistsException("assessUser:" + assessUserId); + } } + } diff --git a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/GrowthRateIndicatorsHandler.java b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/GrowthRateIndicatorsHandler.java index 28d5f27..80cb987 100644 --- a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/GrowthRateIndicatorsHandler.java +++ b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/GrowthRateIndicatorsHandler.java @@ -32,6 +32,6 @@ public class GrowthRateIndicatorsHandler implements IndicatorsHandler { .stream() .min(Comparator.comparingLong(AssetsReporter::getDays)) .orElseThrow(() -> new EntityExistsException("assessUser:" + assessUserId)); - return lastAssets.getAssets().subtract(firstAssets.getAssets()).divide(firstAssets.getAssets()).divide(BigDecimal.valueOf(100)).floatValue(); + return lastAssets.getAssets().subtract(firstAssets.getAssets()).divide(firstAssets.getAssets(),4,BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(100)).floatValue(); } } diff --git a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/ProfitLossRateRateIndicatorsHandler.java b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/ProfitLossRateRateIndicatorsHandler.java index d58c537..980bac0 100644 --- a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/ProfitLossRateRateIndicatorsHandler.java +++ b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/ProfitLossRateRateIndicatorsHandler.java @@ -1,17 +1,64 @@ package com.blockchain.server.yyyf.indicators.handle; import com.blockchain.server.yyyf.indicators.IndicatorsHandler; +import com.blockchain.server.yyyf.indicators.IndicatorsManager; +import com.blockchain.server.yyyf.indicators.dto.AssetsReporter; +import lombok.AllArgsConstructor; import org.springframework.stereotype.Component; +import javax.persistence.EntityExistsException; +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; + /** * @author fengyu */ +@AllArgsConstructor @Component(ProfitLossRateRateIndicatorsHandler.HANDLER_NAME + IndicatorsHandler.HANDLER_NAME) public class ProfitLossRateRateIndicatorsHandler implements IndicatorsHandler { public static final String HANDLER_NAME = "ProfitLossRate"; + private final IndicatorsManager indicatorsManager; + @Override public Float handleIndicator(String assessUserId) { - return null; + + List reporterList = indicatorsManager.getAssetsReporterListByAssessUserId(assessUserId); + if (reporterList != null && reporterList.size() > 0) { + //排序 + reporterList.sort(Comparator.comparingLong(AssetsReporter::getDays)); + //第一天(上一天)的资产总额 + BigDecimal upAssets = reporterList.get(0).getAssets(); + + //总亏损额 + BigDecimal lossMoneyCount = BigDecimal.ZERO; + //盈利计数 + int profitCount = 0; + //总盈利额 + BigDecimal profitMoneyCount = BigDecimal.ZERO; + for (AssetsReporter assetsReporter : reporterList) { + + BigDecimal thisYeid = assetsReporter.getAssets(); + //盈利 + if (thisYeid.compareTo(upAssets) > 0) { //盈利 + profitCount++; + profitMoneyCount = profitMoneyCount.add(thisYeid).subtract(upAssets); + }else{ + lossMoneyCount = lossMoneyCount.add(thisYeid).subtract(upAssets); + } + upAssets=thisYeid; + } + if (profitCount == 0) { //每天亏损、或者每天既不盈利也不亏损 + return Float.valueOf(0); + }else if(profitCount==(reporterList.size()-1)){ //每天都在盈利 + return Float.POSITIVE_INFINITY; + }else { //计算盈亏比 + return profitMoneyCount.divide(lossMoneyCount,4,BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(100)).floatValue(); + } + } else { + throw new EntityExistsException("assessUser:" + assessUserId); + } } } diff --git a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/ShapeRateIndicatorsHandler.java b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/ShapeRateIndicatorsHandler.java index 01e3ee5..a7116f0 100644 --- a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/ShapeRateIndicatorsHandler.java +++ b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/ShapeRateIndicatorsHandler.java @@ -1,17 +1,39 @@ package com.blockchain.server.yyyf.indicators.handle; import com.blockchain.server.yyyf.indicators.IndicatorsHandler; +import lombok.AllArgsConstructor; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; +import java.math.BigDecimal; + /** * @author fengyu */ +@AllArgsConstructor @Component(ShapeRateIndicatorsHandler.HANDLER_NAME + IndicatorsHandler.HANDLER_NAME) public class ShapeRateIndicatorsHandler implements IndicatorsHandler { - public static final String HANDLER_NAME = "AnnualGrowthRate"; + public static final String HANDLER_NAME = "ShapeRate"; + + //无风险利率,配置文件注入,防止后期更改 + @Value("${rate.riskFree}") + private BigDecimal riskFree; + + private final AnnualGrowthRateIndicatorsHandler annualGrowthRateIndicatorsHandler; + + private final FluctuationRateIndicatorsHandler fluctuationRateIndicatorsHandler; @Override public Float handleIndicator(String assessUserId) { - return null; + + //年化收益率 + Float annualGrowthRate=annualGrowthRateIndicatorsHandler.handleIndicator(assessUserId); + //波动率 + Float fluctuationRate=fluctuationRateIndicatorsHandler.handleIndicator(assessUserId); + + return BigDecimal.valueOf(annualGrowthRate).subtract(riskFree).divide(BigDecimal.valueOf(fluctuationRate),4,BigDecimal.ROUND_HALF_UP) + .multiply(BigDecimal.valueOf(100)).floatValue(); + } + } diff --git a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/TotalGrowthRateIndicatorsHandler.java b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/TotalGrowthRateIndicatorsHandler.java index 7b78da6..2bcd177 100644 --- a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/TotalGrowthRateIndicatorsHandler.java +++ b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/TotalGrowthRateIndicatorsHandler.java @@ -1,17 +1,44 @@ package com.blockchain.server.yyyf.indicators.handle; import com.blockchain.server.yyyf.indicators.IndicatorsHandler; +import com.blockchain.server.yyyf.indicators.IndicatorsManager; +import com.blockchain.server.yyyf.indicators.dto.AssetsReporter; +import lombok.AllArgsConstructor; import org.springframework.stereotype.Component; +import javax.persistence.EntityExistsException; +import java.math.BigDecimal; +import java.util.Comparator; +import java.util.List; + /** * @author fengyu */ +@AllArgsConstructor @Component(TotalGrowthRateIndicatorsHandler.HANDLER_NAME + IndicatorsHandler.HANDLER_NAME) public class TotalGrowthRateIndicatorsHandler implements IndicatorsHandler { public static final String HANDLER_NAME = "TotalGrowthRate"; + private final IndicatorsManager indicatorsManager; + @Override public Float handleIndicator(String assessUserId) { - return null; + List reporterList = indicatorsManager.getAssetsReporterListByAssessUserId(assessUserId); + AssetsReporter lastAssets = reporterList + .stream() + .max(Comparator.comparingLong(AssetsReporter::getDays)) + .orElseThrow(() -> new EntityExistsException("assessUser:" + assessUserId)); + + //最初的金额 + AssetsReporter firstAssets = reporterList + .stream() + .min(Comparator.comparingLong(AssetsReporter::getDays)) + .orElseThrow(() -> new EntityExistsException("assessUser:" + assessUserId)); + //充值的总金额 + //todo 不知道如何获取 + BigDecimal rechargeAmount=null; + + return lastAssets.getAssets().subtract(firstAssets.getAssets()).divide(rechargeAmount,4,BigDecimal.ROUND_HALF_UP) + .multiply(BigDecimal.valueOf(100)).floatValue(); } }