Search in sources :

Example 6 with BaseBusinessException

use of com.tony.billing.exceptions.BaseBusinessException 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 7 with BaseBusinessException

use of com.tony.billing.exceptions.BaseBusinessException in project BillingDubbo by TonyJiangWJ.

the class FundInfoServiceImpl method enhanceFund.

@Override
@Transactional(rollbackFor = { BaseBusinessException.class, Exception.class })
public boolean enhanceFund(FundEnhanceRequest request) {
    FundInfo condition = new FundInfo();
    condition.setInStore(EnumYesOrNo.YES.val());
    condition.setFundCode(request.getFundCode());
    condition.setUserId(UserIdContainer.getUserId());
    List<FundInfo> matchedFunds = mapper.listFundsBefore(condition, request.getDateBefore());
    AtomicBoolean operateSuccess = new AtomicBoolean(false);
    AtomicBoolean sqlError = new AtomicBoolean(false);
    if (CollectionUtils.isNotEmpty(matchedFunds)) {
        Optional<BigDecimal> totalAmountOpt = matchedFunds.stream().map(FundInfo::getPurchaseAmount).reduce(BigDecimal::add);
        totalAmountOpt.ifPresent(totalAmount -> {
            BigDecimal currentAmount = new BigDecimal(request.getCurrentAmount());
            if (currentAmount.compareTo(totalAmount) > 0) {
                BigDecimal enhanceRate = currentAmount.divide(totalAmount, 4, BigDecimal.ROUND_HALF_UP);
                logger.info("增强基金:{} 增强前总量:{} 增强后总量:{}  倍率:{}", request.getFundCode(), totalAmount, currentAmount, enhanceRate);
                matchedFunds.parallelStream().forEach(fundInfo -> {
                    FundInfoHistory fundInfoHistory = new FundInfoHistory();
                    BeanUtils.copyProperties(fundInfo, fundInfoHistory);
                    fundInfoHistory.setId(null);
                    fundInfoHistory.setOriginId(fundInfo.getId());
                    fundInfoHistory.setChangeType(EnumFundChangeType.ENHANCE.getType());
                    fundInfoHistory.setCreateTime(new Date());
                    fundInfoHistory.setModifyTime(new Date());
                    try {
                        fundInfoHistoryMapper.insert(fundInfoHistory);
                        fundInfo.setPurchaseAmount(fundInfo.getPurchaseAmount().multiply(enhanceRate).setScale(2, BigDecimal.ROUND_HALF_UP));
                        fundInfo.setPurchaseValue(fundInfo.getPurchaseValue().divide(enhanceRate, BigDecimal.ROUND_HALF_UP));
                        mapper.update(fundInfo);
                    } catch (Exception e) {
                        logger.error("保存增强后基金数据异常", e);
                        sqlError.set(true);
                    }
                });
                operateSuccess.set(true);
            } else {
                logger.error("增强后持有总量必须大于增强前的持有总量");
            }
        });
        if (sqlError.get()) {
            throw new BaseBusinessException("保存增强后基金数据,SQL执行失败");
        }
    }
    return operateSuccess.get();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FundInfoHistory(com.tony.billing.entity.FundInfoHistory) FundInfo(com.tony.billing.entity.FundInfo) BaseBusinessException(com.tony.billing.exceptions.BaseBusinessException) BigDecimal(java.math.BigDecimal) Date(java.util.Date) LocalDate(java.time.LocalDate) BaseBusinessException(com.tony.billing.exceptions.BaseBusinessException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

BaseBusinessException (com.tony.billing.exceptions.BaseBusinessException)7 FundInfo (com.tony.billing.entity.FundInfo)3 BigDecimal (java.math.BigDecimal)3 LocalDate (java.time.LocalDate)3 Transactional (org.springframework.transaction.annotation.Transactional)3 Admin (com.tony.billing.entity.Admin)2 FundInfoHistory (com.tony.billing.entity.FundInfoHistory)2 ModifyAdmin (com.tony.billing.entity.ModifyAdmin)2 Date (java.util.Date)2 FundHistoryValue (com.tony.billing.entity.FundHistoryValue)1 FundPreSaleInfo (com.tony.billing.entity.FundPreSaleInfo)1 FundPreSaleRef (com.tony.billing.entity.FundPreSaleRef)1 BaseResponse (com.tony.billing.response.BaseResponse)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 MessagingException (javax.mail.MessagingException)1 MimeMessage (javax.mail.internet.MimeMessage)1