Search in sources :

Example 1 with FundInfoHistory

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

the class FundInfoServiceImpl method preSalePortion.

@Override
@Transactional(rollbackFor = { Exception.class, BaseBusinessException.class })
public boolean preSalePortion(FundPreSalePortionRequest request) {
    FundInfo fundInfo = mapper.getById(request.getId(), UserIdContainer.getUserId());
    if (fundInfo != null) {
        BigDecimal saleAmount = request.getSaleAmount();
        if (saleAmount.compareTo(fundInfo.getPurchaseAmount()) <= 0) {
            BigDecimal restAmount = fundInfo.getPurchaseAmount().subtract(saleAmount);
            Long saleFundId = null;
            if (restAmount.compareTo(BigDecimal.ZERO) > 0) {
                BigDecimal oneHundred = new BigDecimal("100");
                BigDecimal salePercent = saleAmount.multiply(oneHundred).divide(fundInfo.getPurchaseAmount(), BigDecimal.ROUND_HALF_UP);
                BigDecimal salePurchaseCost = salePercent.multiply(fundInfo.getPurchaseCost()).divide(oneHundred, 2, BigDecimal.ROUND_HALF_UP);
                BigDecimal salePurchaseFee = salePercent.multiply(fundInfo.getPurchaseFee()).divide(oneHundred, 2, BigDecimal.ROUND_HALF_UP);
                FundInfo saleFund = new FundInfo();
                BeanUtils.copyProperties(fundInfo, saleFund);
                saleFund.setPurchaseAmount(saleAmount);
                saleFund.setPurchaseCost(salePurchaseCost);
                saleFund.setPurchaseFee(salePurchaseFee);
                saleFund.setId(null);
                // 售出的记录下来
                saleFundId = super.insert(saleFund);
                // 记录部分售出基金的历史信息
                FundInfoHistory fundInfoHistory = new FundInfoHistory();
                BeanUtils.copyProperties(fundInfo, fundInfoHistory);
                fundInfoHistory.setId(null);
                fundInfoHistory.setOriginId(fundInfo.getId());
                fundInfoHistory.setChangeType(EnumFundChangeType.SALE_PORTION.getType());
                fundInfoHistory.setCreateTime(new Date());
                fundInfoHistory.setModifyTime(new Date());
                fundInfoHistoryMapper.insert(fundInfoHistory);
                // 更新剩余份额
                fundInfo.setPurchaseAmount(restAmount);
                fundInfo.setPurchaseCost(fundInfo.getPurchaseCost().subtract(salePurchaseCost));
                fundInfo.setPurchaseFee(fundInfo.getPurchaseFee().subtract(salePurchaseFee));
                super.update(fundInfo);
            } else {
                // 全部卖出
                saleFundId = fundInfo.getId();
            }
            // 标记为卖出
            if (preMarkFundAsSold(saleFundId, UserIdContainer.getUserId(), request.getSaleFeeRate(), request.getAssessmentDate())) {
                return true;
            } else {
                throw new BaseBusinessException("预售出基金信息保存失败");
            }
        }
    }
    return false;
}
Also used : 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) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with FundInfoHistory

use of com.tony.billing.entity.FundInfoHistory 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

FundInfo (com.tony.billing.entity.FundInfo)2 FundInfoHistory (com.tony.billing.entity.FundInfoHistory)2 BaseBusinessException (com.tony.billing.exceptions.BaseBusinessException)2 BigDecimal (java.math.BigDecimal)2 LocalDate (java.time.LocalDate)2 Date (java.util.Date)2 Transactional (org.springframework.transaction.annotation.Transactional)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1