Search in sources :

Example 1 with FundPreSaleInfo

use of com.tony.billing.entity.FundPreSaleInfo 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;
}
Also used : FundHistoryValue(com.tony.billing.entity.FundHistoryValue) FundPreSaleInfo(com.tony.billing.entity.FundPreSaleInfo) FundInfo(com.tony.billing.entity.FundInfo) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal)

Example 2 with FundPreSaleInfo

use of com.tony.billing.entity.FundPreSaleInfo in project BillingDubbo by TonyJiangWJ.

the class FundInfoServiceImpl method preMarkFundsAsSold.

@Override
@Transactional(rollbackFor = Exception.class)
public boolean preMarkFundsAsSold(List<Long> fundIds, BigDecimal soldFeeRate, String assessmentDate) {
    Preconditions.checkState(CollectionUtils.isNotEmpty(fundIds), "基金id列表不能为空");
    Preconditions.checkState(soldFeeRate != null, "基金售出费率不能为空");
    Preconditions.checkState(StringUtils.isNotEmpty(assessmentDate), "基金估算日期不能为空");
    List<FundInfo> inStoreFunds = mapper.listInStoreFunds(fundIds, UserIdContainer.getUserId());
    if (CollectionUtils.isNotEmpty(inStoreFunds)) {
        inStoreFunds.forEach(fundInfo -> {
            fundInfo.setInStore(EnumYesOrNo.NO.val());
            super.update(fundInfo);
        });
        FundPreSaleInfo preSaleInfo = new FundPreSaleInfo();
        preSaleInfo.setConverted(EnumYesOrNo.NO.val());
        preSaleInfo.setFundCode(inStoreFunds.get(0).getFundCode());
        preSaleInfo.setFundName(inStoreFunds.get(0).getFundName());
        preSaleInfo.setUserId(UserIdContainer.getUserId());
        BigDecimal purchaseCost = BigDecimal.ZERO;
        BigDecimal purchaseFee = BigDecimal.ZERO;
        BigDecimal soldAmount = BigDecimal.ZERO;
        BigDecimal assessmentSoldIncome = BigDecimal.ZERO;
        BigDecimal assessmentSoldFee = BigDecimal.ZERO;
        FundHistoryValue latestFundValue = fundHistoryValueService.getFundLatestValue(preSaleInfo.getFundCode(), assessmentDate);
        boolean hasAssessmentValue = latestFundValue != null;
        for (FundInfo fundInfo : inStoreFunds) {
            purchaseCost = purchaseCost.add(fundInfo.getPurchaseCost());
            purchaseFee = purchaseFee.add(fundInfo.getPurchaseFee());
            soldAmount = soldAmount.add(fundInfo.getPurchaseAmount());
            if (hasAssessmentValue) {
                assessmentSoldIncome = assessmentSoldIncome.add(latestFundValue.getAssessmentValue().multiply(fundInfo.getPurchaseAmount()));
            }
        }
        if (hasAssessmentValue) {
            assessmentSoldFee = assessmentSoldIncome.multiply(soldFeeRate).divide(BigDecimal.valueOf(100), BigDecimal.ROUND_HALF_UP).setScale(2, BigDecimal.ROUND_HALF_UP);
            assessmentSoldIncome = assessmentSoldIncome.setScale(2, BigDecimal.ROUND_HALF_UP);
            preSaleInfo.setAssessmentValue(latestFundValue.getAssessmentValue());
        }
        preSaleInfo.setAssessmentSoldFee(assessmentSoldFee);
        preSaleInfo.setAssessmentSoldIncome(assessmentSoldIncome);
        preSaleInfo.setSoldAmount(soldAmount);
        preSaleInfo.setPurchaseCost(purchaseCost);
        preSaleInfo.setPurchaseFee(purchaseFee);
        LocalDate localDate = LocalDate.parse(assessmentDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        // 将估算日期作为卖出日期
        preSaleInfo.setSoldDate(Date.from(localDate.atStartOfDay(TimeConstants.CHINA_ZONE).toInstant()));
        // 成本净值为买入总支出除以买入(卖出)份额
        preSaleInfo.setCostValue(purchaseCost.divide(soldAmount, 4, BigDecimal.ROUND_HALF_UP));
        Long preSaleId = preSaleInfoService.insert(preSaleInfo);
        if (preSaleId > 0) {
            inStoreFunds.forEach(fundInfo -> {
                FundPreSaleRef soldRef = new FundPreSaleRef();
                soldRef.setFundId(fundInfo.getId());
                soldRef.setFundPreSaleId(preSaleId);
                fundPreSaleRefService.insert(soldRef);
            });
            return true;
        } else {
            throw new BaseBusinessException("保存预售信息失败");
        }
    }
    return false;
}
Also used : FundHistoryValue(com.tony.billing.entity.FundHistoryValue) FundPreSaleInfo(com.tony.billing.entity.FundPreSaleInfo) FundInfo(com.tony.billing.entity.FundInfo) BaseBusinessException(com.tony.billing.exceptions.BaseBusinessException) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) FundPreSaleRef(com.tony.billing.entity.FundPreSaleRef) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with FundPreSaleInfo

use of com.tony.billing.entity.FundPreSaleInfo in project BillingDubbo by TonyJiangWJ.

the class PreSaleServiceTest method testListSoldFundInfos.

@Test
public void testListSoldFundInfos() {
    FundPreSaleInfo saleInfo = new FundPreSaleInfo();
    saleInfo.setUserId(userId);
    List<FundPreSaleInfo> preSoldList = fundPreSaleInfoMapper.list(saleInfo);
    logger.info("saleInfo: {}", JSON.toJSONString(preSoldList));
    BigDecimal totalIncome = preSoldList.stream().map(FundPreSaleInfo::getAssessmentSoldIncome).reduce(BigDecimal::add).get();
    BigDecimal totalCost = preSoldList.stream().map(FundPreSaleInfo::getPurchaseCost).reduce(BigDecimal::add).get();
    BigDecimal totalSoldFee = preSoldList.stream().map(FundPreSaleInfo::getAssessmentSoldFee).reduce(BigDecimal::add).get();
    logger.info("总收入:{}", totalIncome);
    logger.info("总支出:{}", totalCost);
    logger.info("总卖出手续费:{}", totalSoldFee);
    logger.info("总收益:{}", totalIncome.subtract(totalSoldFee).subtract(totalCost));
}
Also used : FundPreSaleInfo(com.tony.billing.entity.FundPreSaleInfo) BigDecimal(java.math.BigDecimal) BaseServiceTest(com.tony.billing.service.BaseServiceTest) Test(org.junit.Test)

Aggregations

FundPreSaleInfo (com.tony.billing.entity.FundPreSaleInfo)3 BigDecimal (java.math.BigDecimal)3 FundHistoryValue (com.tony.billing.entity.FundHistoryValue)2 FundInfo (com.tony.billing.entity.FundInfo)2 LocalDate (java.time.LocalDate)2 FundPreSaleRef (com.tony.billing.entity.FundPreSaleRef)1 BaseBusinessException (com.tony.billing.exceptions.BaseBusinessException)1 BaseServiceTest (com.tony.billing.service.BaseServiceTest)1 Test (org.junit.Test)1 Transactional (org.springframework.transaction.annotation.Transactional)1