Search in sources :

Example 6 with FundInfo

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

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

the class FundInfoController method updateFundInfo.

@RequestMapping(value = "/fund/info/update", method = RequestMethod.POST)
public BaseResponse updateFundInfo(@ModelAttribute("request") @Validated FundUpdateRequest request) {
    FundInfo fundInfo = new FundInfo();
    BeanUtils.copyProperties(request, fundInfo);
    fundInfo.setConfirmDate(request.getPurchaseConfirmDate());
    if (fundInfoService.update(fundInfo)) {
        return ResponseUtil.success();
    } else {
        return ResponseUtil.error();
    }
}
Also used : FundInfo(com.tony.billing.entity.FundInfo) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 8 with FundInfo

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

the class FundInfoController method addFundInfo.

@RequestMapping(value = "/fund/info/put", method = RequestMethod.POST)
public BaseResponse addFundInfo(@ModelAttribute("request") @Validated FundAddRequest request) {
    FundInfo fundInfo = new FundInfo();
    fundInfo.setUserId(request.getUserId());
    fundInfo.setFundName(request.getFundName());
    fundInfo.setFundCode(request.getFundCode());
    fundInfo.setPurchaseAmount(new BigDecimal(request.getPurchaseAmount()));
    fundInfo.setPurchaseValue(new BigDecimal(request.getPurchaseValue()));
    fundInfo.setPurchaseCost(new BigDecimal(request.getPurchaseCost()));
    fundInfo.setPurchaseFee(new BigDecimal(request.getPurchaseFee()));
    fundInfo.setInStore(EnumYesOrNo.YES.val());
    fundInfo.setConfirmDate(request.getPurchaseConfirmDate());
    fundInfo.setPurchaseDate(request.getPurchaseDate());
    if (fundInfoService.insert(fundInfo) > 0) {
        return ResponseUtil.success();
    } else {
        return ResponseUtil.error();
    }
}
Also used : FundInfo(com.tony.billing.entity.FundInfo) BigDecimal(java.math.BigDecimal) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 9 with FundInfo

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

the class FundHistoryValueServiceImpl method getFundChangedInfosByAssessmentDate.

@Override
public DailyFundChangedResponse getFundChangedInfosByAssessmentDate(String assessmentDate) {
    DailyFundChangedResponse response = ResponseUtil.success(new DailyFundChangedResponse());
    Long userId = UserIdContainer.getUserId();
    List<FundInfo> userFunds = fundInfoService.listGroupedFundsByUserId(userId);
    FundInfo fundInfo = new FundInfo();
    fundInfo.setUserId(userId);
    fundInfo.setInStore(EnumYesOrNo.YES.val());
    List<FundInfo> userFundDetailList = fundInfoService.list(fundInfo);
    if (CollectionUtils.isNotEmpty(userFunds)) {
        List<FundHistoryValue> latestHistoryValues = mapper.getLatestFundHistoryValueByFundCodes(userFunds.stream().map(FundInfo::getFundCode).collect(Collectors.toList()), assessmentDate);
        Map<String, FundHistoryValue> latestHistoryValueMap = new HashMap<>(latestHistoryValues.size());
        if (CollectionUtils.isNotEmpty(latestHistoryValues)) {
            latestHistoryValues.forEach(fundHistoryValue -> latestHistoryValueMap.put(fundHistoryValue.getFundCode(), fundHistoryValue));
        }
        response.setSummaryFundInfos(getFundChangedInfos(userFunds, latestHistoryValueMap, assessmentDate));
        response.setFundDetailInfos(getFundChangedInfos(userFundDetailList, latestHistoryValueMap, assessmentDate));
        response.setAssessmentDate(assessmentDate);
        response.calculateIncreaseInfo();
    }
    return response;
}
Also used : FundHistoryValue(com.tony.billing.entity.FundHistoryValue) HashMap(java.util.HashMap) FundInfo(com.tony.billing.entity.FundInfo) DailyFundChangedResponse(com.tony.billing.response.fund.DailyFundChangedResponse)

Example 10 with FundInfo

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

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