Search in sources :

Example 11 with FundInfo

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

the class FundInfoServiceImpl method checkFundsExistence.

@Override
public List<FundExistenceCheck> checkFundsExistence(List<FundExistenceCheck> fundCheckList) {
    Preconditions.checkState(CollectionUtils.isNotEmpty(fundCheckList), "待检测列表不能为空");
    List<FundInfo> userFundsList = mapper.listUserExistsFunds(UserIdContainer.getUserId(), fundCheckList.stream().map(FundExistenceCheck::getFundCode).distinct().collect(Collectors.toList()));
    if (CollectionUtils.isEmpty(userFundsList)) {
        return Collections.emptyList();
    }
    Map<String, List<FundInfo>> resultMap = userFundsList.stream().collect(Collectors.groupingBy(FundInfo::getFundCode));
    List<FundExistenceCheck> resultList = new CopyOnWriteArrayList<>();
    fundCheckList.parallelStream().forEach(fundCheck -> {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        List<FundInfo> fundInfos = resultMap.get(fundCheck.getFundCode());
        if (CollectionUtils.isNotEmpty(fundInfos)) {
            for (FundInfo fundInfo : fundInfos) {
                String confirmDate = fundInfo.getConfirmDate().toInstant().atZone(TimeConstants.CHINA_ZONE).format(formatter);
                if (confirmDate.equals(fundCheck.getConfirmDate()) && fundInfo.getPurchaseAmount().compareTo(fundCheck.getPurchaseAmount()) == 0) {
                    resultList.add(fundCheck);
                }
            }
        }
    });
    return resultList;
}
Also used : FundInfo(com.tony.billing.entity.FundInfo) ArrayList(java.util.ArrayList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) FundExistenceCheck(com.tony.billing.model.FundExistenceCheck) DateTimeFormatter(java.time.format.DateTimeFormatter) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 12 with FundInfo

use of com.tony.billing.entity.FundInfo 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)12 BigDecimal (java.math.BigDecimal)9 LocalDate (java.time.LocalDate)7 FundHistoryValue (com.tony.billing.entity.FundHistoryValue)5 Transactional (org.springframework.transaction.annotation.Transactional)5 BaseBusinessException (com.tony.billing.exceptions.BaseBusinessException)4 ArrayList (java.util.ArrayList)4 Date (java.util.Date)4 FundInfoHistory (com.tony.billing.entity.FundInfoHistory)3 FundPreSaleInfo (com.tony.billing.entity.FundPreSaleInfo)3 DateTimeFormatter (java.time.format.DateTimeFormatter)3 List (java.util.List)3 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)3 JSON (com.alibaba.fastjson.JSON)2 EnumYesOrNo (com.tony.billing.constants.enums.EnumYesOrNo)2 FundInfoMapper (com.tony.billing.dao.mapper.FundInfoMapper)2 FundPreSaleRef (com.tony.billing.entity.FundPreSaleRef)2 FundExistenceCheck (com.tony.billing.model.FundExistenceCheck)2 FundHistoryValueService (com.tony.billing.service.api.FundHistoryValueService)2 FundInfoService (com.tony.billing.service.api.FundInfoService)2