use of com.tony.billing.entity.FundInfo in project BillingDubbo by TonyJiangWJ.
the class FundHistoryValueServiceImpl method getFundHistoryValuesByAssessmentDate.
@Override
public DailyFundHistoryValueResponse getFundHistoryValuesByAssessmentDate(String assessmentDate) {
Long userId = UserIdContainer.getUserId();
List<FundInfo> userFunds = fundInfoMapper.getFundInfoDistinctByUser(userId);
DailyFundHistoryValueResponse response = ResponseUtil.success(new DailyFundHistoryValueResponse());
response.setAssessmentDate(assessmentDate);
if (CollectionUtils.isNotEmpty(userFunds)) {
Map<String, String> fundInfoMap = new HashMap<>(userFunds.size());
for (FundInfo fundInfo : userFunds) {
fundInfoMap.put(fundInfo.getFundCode(), fundInfo.getFundName());
}
response.setFundInfoMap(fundInfoMap);
List<FundHistoryValue> dailyFundHistoryValues = mapper.getFundHistoriesByFundCodes(userFunds.stream().map(FundInfo::getFundCode).collect(Collectors.toList()), assessmentDate);
if (CollectionUtils.isNotEmpty(dailyFundHistoryValues)) {
Map<String, List<FundHistoryValue>> fundHistoryValues = dailyFundHistoryValues.parallelStream().collect(Collectors.groupingBy(FundHistoryValue::getFundCode));
Map<String, List<String>> resultMap = fundHistoryValues.entrySet().stream().map(entry -> {
String fundCode = entry.getKey();
List<String> increaseRateList = entry.getValue().stream().map(FundHistoryValue::getAssessmentIncreaseRate).collect(Collectors.toList());
Map<String, List<String>> map = new HashMap<>(1);
map.put(fundCode, increaseRateList);
return map;
}).reduce(new HashMap<>(userFunds.size()), (a, b) -> {
a.putAll(b);
return a;
});
response.setIncreaseRateMapping(resultMap);
// 计算总增长率
generateTotalIncreaseRateInfo(response, userId);
response.reverseFundCodeAndName();
response.reverseRateList();
}
}
return response;
}
use of com.tony.billing.entity.FundInfo in project BillingDubbo by TonyJiangWJ.
the class FundHistoryValueServiceImpl method calculateResult.
private String calculateResult(int index, Map<String, List<String>> increaseRateMapping, List<FundInfo> userFundsWithValue) {
BigDecimal totalIncrease = BigDecimal.ZERO;
BigDecimal totalValue = BigDecimal.ZERO;
for (FundInfo fundInfo : userFundsWithValue) {
List<String> rateList = increaseRateMapping.get(fundInfo.getFundCode());
if (CollectionUtils.isNotEmpty(rateList) && index < rateList.size()) {
String rate = rateList.get(index);
totalIncrease = totalIncrease.add(new BigDecimal(rate).multiply(fundInfo.getPurchaseAmount().multiply(fundInfo.getPurchaseValue())));
} else {
logger.debug("fund[{}]'s rate is not exist, index: {}", fundInfo.getFundCode(), index);
}
totalValue = totalValue.add(fundInfo.getPurchaseValue().multiply(fundInfo.getPurchaseAmount()));
}
return totalIncrease.divide(totalValue, BigDecimal.ROUND_HALF_UP).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
}
use of com.tony.billing.entity.FundInfo in project BillingDubbo by TonyJiangWJ.
the class FundInfoServiceImpl method listGroupedFundsByUserId.
@Override
public List<FundInfo> listGroupedFundsByUserId(Long userId) {
FundInfo fundInfo = new FundInfo();
fundInfo.setUserId(userId);
fundInfo.setIsDeleted(EnumDeleted.NOT_DELETED.val());
fundInfo.setInStore(EnumYesOrNo.YES.val());
List<FundInfo> fundInfos = mapper.list(fundInfo);
if (CollectionUtils.isNotEmpty(fundInfos)) {
fundInfos = fundInfos.stream().collect(Collectors.groupingBy(FundInfo::getFundCode)).entrySet().parallelStream().map(entry -> {
// 分组后按各组计算总量
String fundCode = entry.getKey();
FundInfo resultFund = new FundInfo();
resultFund.setFundCode(fundCode);
resultFund.setUserId(userId);
resultFund.setPurchaseAmount(BigDecimal.ZERO);
resultFund.setPurchaseCost(BigDecimal.ZERO);
resultFund.setPurchaseFee(BigDecimal.ZERO);
resultFund = entry.getValue().stream().reduce(resultFund, (f1, f2) -> {
f1.setPurchaseAmount(f1.getPurchaseAmount().add(f2.getPurchaseAmount()));
f1.setPurchaseCost(f1.getPurchaseCost().add(f2.getPurchaseCost()));
f1.setPurchaseFee(f1.getPurchaseFee().add(f2.getPurchaseFee()));
f1.setFundName(f2.getFundName());
return f1;
});
if (resultFund.getPurchaseAmount().compareTo(BigDecimal.ZERO) > 0) {
// 平均成本净值
resultFund.setPurchaseValue(resultFund.getPurchaseCost().divide(resultFund.getPurchaseAmount(), 4, BigDecimal.ROUND_HALF_UP));
} else {
logger.warn("fund amount is zero:{}", JSON.toJSONString(resultFund));
}
return resultFund;
}).collect(Collectors.toList());
}
return fundInfos;
}
use of com.tony.billing.entity.FundInfo in project BillingDubbo by TonyJiangWJ.
the class FundInfoServiceImpl method preMarkFundAsSold.
private boolean preMarkFundAsSold(Long saleFundId, Long userId, BigDecimal soldFeeRate, String assessmentDate) {
FundInfo saleFund = mapper.getById(saleFundId, userId);
if (saleFund == null) {
throw new IllegalStateException("sale fund should never by null");
}
FundPreSaleInfo preSaleInfo = new FundPreSaleInfo();
preSaleInfo.setFundCode(saleFund.getFundCode());
preSaleInfo.setFundName(saleFund.getFundName());
preSaleInfo.setConverted(EnumYesOrNo.NO.val());
preSaleInfo.setUserId(userId);
preSaleInfo.setPurchaseCost(saleFund.getPurchaseCost());
preSaleInfo.setPurchaseFee(saleFund.getPurchaseFee());
preSaleInfo.setSoldAmount(saleFund.getPurchaseAmount());
// 将估算日期作为卖出日期
LocalDate localDate = LocalDate.parse(assessmentDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
preSaleInfo.setSoldDate(Date.from(localDate.atStartOfDay(TimeConstants.CHINA_ZONE).toInstant()));
// 成本净值为买入总支出除以买入(卖出)份额
preSaleInfo.setCostValue(saleFund.getPurchaseCost().divide(saleFund.getPurchaseAmount(), 4, BigDecimal.ROUND_HALF_UP));
FundHistoryValue latestFundValue = fundHistoryValueService.getFundLatestValue(preSaleInfo.getFundCode(), assessmentDate);
if (latestFundValue != null) {
BigDecimal assessmentSoldIncome = latestFundValue.getAssessmentValue().multiply(saleFund.getPurchaseAmount());
BigDecimal assessmentSoldFee = assessmentSoldIncome.multiply(soldFeeRate).divide(BigDecimal.valueOf(100), BigDecimal.ROUND_HALF_UP).setScale(2, BigDecimal.ROUND_HALF_UP);
preSaleInfo.setAssessmentValue(latestFundValue.getAssessmentValue());
preSaleInfo.setAssessmentSoldFee(assessmentSoldFee.setScale(2, BigDecimal.ROUND_HALF_UP));
preSaleInfo.setAssessmentSoldIncome(assessmentSoldIncome.setScale(2, BigDecimal.ROUND_HALF_UP));
preSaleInfo.setAssessmentValue(latestFundValue.getAssessmentValue());
}
saleFund.setInStore(EnumYesOrNo.NO.val());
return mapper.update(saleFund) > 0 && preSaleInfoService.insert(preSaleInfo) > 0;
}
use of com.tony.billing.entity.FundInfo in project BillingDubbo by TonyJiangWJ.
the class FundInfoServiceImpl method batchAddFunds.
@Override
@Transactional(rollbackFor = Exception.class)
public boolean batchAddFunds(List<FundAddModel> fundInfoList) {
Preconditions.checkState(CollectionUtils.isNotEmpty(fundInfoList), "待增加基金列表不能为空");
final Long userId = UserIdContainer.getUserId();
List<FundInfo> forAddFunds = new ArrayList<>();
fundInfoList.forEach(addFund -> {
FundInfo fundInfo = new FundInfo();
fundInfo.setInStore(EnumYesOrNo.YES.val());
fundInfo.setUserId(userId);
fundInfo.setFundName(addFund.getFundName());
fundInfo.setFundCode(addFund.getFundCode());
fundInfo.setPurchaseAmount(new BigDecimal(addFund.getPurchaseAmount()));
fundInfo.setPurchaseValue(new BigDecimal(addFund.getPurchaseValue()));
fundInfo.setPurchaseCost(new BigDecimal(addFund.getPurchaseCost()));
fundInfo.setPurchaseFee(new BigDecimal(addFund.getPurchaseFee()));
fundInfo.setConfirmDate(addFund.getPurchaseConfirmDate());
fundInfo.setPurchaseDate(addFund.getPurchaseDate());
fundInfo.setModifyTime(new Date());
fundInfo.setCreateTime(new Date());
fundInfo.setIsDeleted(EnumYesOrNo.NO.val());
fundInfo.setVersion(0);
forAddFunds.add(fundInfo);
});
// 更新历史数据
fundHistoryValueService.updateFundHistoryValues();
// 更新历史净值数据
fundHistoryNetValueService.updateHistoryNetValues();
return mapper.batchInsert(forAddFunds) > 0;
}
Aggregations