Search in sources :

Example 1 with FundInfo

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

the class FundHistoryValueServiceImpl method getFundHistoryValuesByAssessmentDate.

@Override
public DailyFundHistoryValueResponse getFundHistoryValuesByAssessmentDate(String assessmentDate) {
    Long userId = UserIdContainer.getUserId();
    List<FundInfo> userFunds = fundInfoMapper.getFundInfoDistinctByUser(userId);
    DailyFundHistoryValueResponse response = ResponseUtil.success(new DailyFundHistoryValueResponse());
    response.setAssessmentDate(assessmentDate);
    if (CollectionUtils.isNotEmpty(userFunds)) {
        Map<String, String> fundInfoMap = new HashMap<>(userFunds.size());
        for (FundInfo fundInfo : userFunds) {
            fundInfoMap.put(fundInfo.getFundCode(), fundInfo.getFundName());
        }
        response.setFundInfoMap(fundInfoMap);
        List<FundHistoryValue> dailyFundHistoryValues = mapper.getFundHistoriesByFundCodes(userFunds.stream().map(FundInfo::getFundCode).collect(Collectors.toList()), assessmentDate);
        if (CollectionUtils.isNotEmpty(dailyFundHistoryValues)) {
            Map<String, List<FundHistoryValue>> fundHistoryValues = dailyFundHistoryValues.parallelStream().collect(Collectors.groupingBy(FundHistoryValue::getFundCode));
            Map<String, List<String>> resultMap = fundHistoryValues.entrySet().stream().map(entry -> {
                String fundCode = entry.getKey();
                List<String> increaseRateList = entry.getValue().stream().map(FundHistoryValue::getAssessmentIncreaseRate).collect(Collectors.toList());
                Map<String, List<String>> map = new HashMap<>(1);
                map.put(fundCode, increaseRateList);
                return map;
            }).reduce(new HashMap<>(userFunds.size()), (a, b) -> {
                a.putAll(b);
                return a;
            });
            response.setIncreaseRateMapping(resultMap);
            // 计算总增长率
            generateTotalIncreaseRateInfo(response, userId);
            response.reverseFundCodeAndName();
            response.reverseRateList();
        }
    }
    return response;
}
Also used : ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) DateUtil(com.tony.billing.util.DateUtil) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) FundInfoMapper(com.tony.billing.dao.mapper.FundInfoMapper) LoggerFactory(org.slf4j.LoggerFactory) FundInfoService(com.tony.billing.service.api.FundInfoService) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) UserIdContainer(com.tony.billing.util.UserIdContainer) ArrayList(java.util.ArrayList) ResponseUtil(com.tony.billing.util.ResponseUtil) Value(org.springframework.beans.factory.annotation.Value) DailyFundHistoryValueResponse(com.tony.billing.response.fund.DailyFundHistoryValueResponse) BigDecimal(java.math.BigDecimal) CollectionUtils(org.apache.commons.collections.CollectionUtils) Map(java.util.Map) AbstractServiceImpl(com.tony.billing.service.base.AbstractServiceImpl) Response(okhttp3.Response) FundHistoryValue(com.tony.billing.entity.FundHistoryValue) Service(org.apache.dubbo.config.annotation.Service) EnumYesOrNo(com.tony.billing.constants.enums.EnumYesOrNo) DailyFundChangedResponse(com.tony.billing.response.fund.DailyFundChangedResponse) FundHistoryValueService(com.tony.billing.service.api.FundHistoryValueService) FundHistoryNetValue(com.tony.billing.entity.FundHistoryNetValue) Request(okhttp3.Request) Logger(org.slf4j.Logger) FundHistoryNetValueMapper(com.tony.billing.dao.mapper.FundHistoryNetValueMapper) FundInfo(com.tony.billing.entity.FundInfo) FundHistoryValueMapper(com.tony.billing.dao.mapper.FundHistoryValueMapper) IOException(java.io.IOException) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) FundValueChangedModel(com.tony.billing.model.FundValueChangedModel) List(java.util.List) JSON(com.alibaba.fastjson.JSON) OkHttpClient(okhttp3.OkHttpClient) LocalDate(java.time.LocalDate) DateTimeFormatter(java.time.format.DateTimeFormatter) JSONObject(com.alibaba.fastjson.JSONObject) DailyFundHistoryValueResponse(com.tony.billing.response.fund.DailyFundHistoryValueResponse) HashMap(java.util.HashMap) FundHistoryValue(com.tony.billing.entity.FundHistoryValue) FundInfo(com.tony.billing.entity.FundInfo) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with FundInfo

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

the class FundHistoryValueServiceImpl method calculateResult.

private String calculateResult(int index, Map<String, List<String>> increaseRateMapping, List<FundInfo> userFundsWithValue) {
    BigDecimal totalIncrease = BigDecimal.ZERO;
    BigDecimal totalValue = BigDecimal.ZERO;
    for (FundInfo fundInfo : userFundsWithValue) {
        List<String> rateList = increaseRateMapping.get(fundInfo.getFundCode());
        if (CollectionUtils.isNotEmpty(rateList) && index < rateList.size()) {
            String rate = rateList.get(index);
            totalIncrease = totalIncrease.add(new BigDecimal(rate).multiply(fundInfo.getPurchaseAmount().multiply(fundInfo.getPurchaseValue())));
        } else {
            logger.debug("fund[{}]'s rate is not exist, index: {}", fundInfo.getFundCode(), index);
        }
        totalValue = totalValue.add(fundInfo.getPurchaseValue().multiply(fundInfo.getPurchaseAmount()));
    }
    return totalIncrease.divide(totalValue, BigDecimal.ROUND_HALF_UP).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
}
Also used : FundInfo(com.tony.billing.entity.FundInfo) BigDecimal(java.math.BigDecimal)

Example 3 with FundInfo

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

the class FundInfoServiceImpl method listGroupedFundsByUserId.

@Override
public List<FundInfo> listGroupedFundsByUserId(Long userId) {
    FundInfo fundInfo = new FundInfo();
    fundInfo.setUserId(userId);
    fundInfo.setIsDeleted(EnumDeleted.NOT_DELETED.val());
    fundInfo.setInStore(EnumYesOrNo.YES.val());
    List<FundInfo> fundInfos = mapper.list(fundInfo);
    if (CollectionUtils.isNotEmpty(fundInfos)) {
        fundInfos = fundInfos.stream().collect(Collectors.groupingBy(FundInfo::getFundCode)).entrySet().parallelStream().map(entry -> {
            // 分组后按各组计算总量
            String fundCode = entry.getKey();
            FundInfo resultFund = new FundInfo();
            resultFund.setFundCode(fundCode);
            resultFund.setUserId(userId);
            resultFund.setPurchaseAmount(BigDecimal.ZERO);
            resultFund.setPurchaseCost(BigDecimal.ZERO);
            resultFund.setPurchaseFee(BigDecimal.ZERO);
            resultFund = entry.getValue().stream().reduce(resultFund, (f1, f2) -> {
                f1.setPurchaseAmount(f1.getPurchaseAmount().add(f2.getPurchaseAmount()));
                f1.setPurchaseCost(f1.getPurchaseCost().add(f2.getPurchaseCost()));
                f1.setPurchaseFee(f1.getPurchaseFee().add(f2.getPurchaseFee()));
                f1.setFundName(f2.getFundName());
                return f1;
            });
            if (resultFund.getPurchaseAmount().compareTo(BigDecimal.ZERO) > 0) {
                // 平均成本净值
                resultFund.setPurchaseValue(resultFund.getPurchaseCost().divide(resultFund.getPurchaseAmount(), 4, BigDecimal.ROUND_HALF_UP));
            } else {
                logger.warn("fund amount is zero:{}", JSON.toJSONString(resultFund));
            }
            return resultFund;
        }).collect(Collectors.toList());
    }
    return fundInfos;
}
Also used : FundPreSaleInfoService(com.tony.billing.service.api.FundPreSaleInfoService) FundInfoMapper(com.tony.billing.dao.mapper.FundInfoMapper) Date(java.util.Date) FundInfoService(com.tony.billing.service.api.FundInfoService) Autowired(org.springframework.beans.factory.annotation.Autowired) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UserIdContainer(com.tony.billing.util.UserIdContainer) StringUtils(org.apache.commons.lang3.StringUtils) BaseBusinessException(com.tony.billing.exceptions.BaseBusinessException) FundAddModel(com.tony.billing.model.FundAddModel) FundEnhanceRequest(com.tony.billing.request.fund.FundEnhanceRequest) ArrayList(java.util.ArrayList) EnumDeleted(com.tony.billing.constants.enums.EnumDeleted) BigDecimal(java.math.BigDecimal) EnumFundChangeType(com.tony.billing.constants.enums.EnumFundChangeType) CollectionUtils(org.apache.commons.collections.CollectionUtils) Map(java.util.Map) AbstractServiceImpl(com.tony.billing.service.base.AbstractServiceImpl) FundHistoryValue(com.tony.billing.entity.FundHistoryValue) Service(org.apache.dubbo.config.annotation.Service) EnumYesOrNo(com.tony.billing.constants.enums.EnumYesOrNo) FundHistoryValueService(com.tony.billing.service.api.FundHistoryValueService) FundPreSaleRefService(com.tony.billing.service.api.FundPreSaleRefService) FundPreSalePortionRequest(com.tony.billing.request.fund.FundPreSalePortionRequest) FundHistoryNetValueService(com.tony.billing.service.api.FundHistoryNetValueService) FundInfo(com.tony.billing.entity.FundInfo) FundInfoHistory(com.tony.billing.entity.FundInfoHistory) FundInfoHistoryMapper(com.tony.billing.dao.mapper.FundInfoHistoryMapper) Collectors(java.util.stream.Collectors) TimeConstants(com.tony.billing.constants.timing.TimeConstants) FundPreSaleInfo(com.tony.billing.entity.FundPreSaleInfo) List(java.util.List) JSON(com.alibaba.fastjson.JSON) FundPreSaleRef(com.tony.billing.entity.FundPreSaleRef) LocalDate(java.time.LocalDate) DateTimeFormatter(java.time.format.DateTimeFormatter) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) Collections(java.util.Collections) FundExistenceCheck(com.tony.billing.model.FundExistenceCheck) BeanUtils(org.springframework.beans.BeanUtils) Transactional(org.springframework.transaction.annotation.Transactional) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) FundInfo(com.tony.billing.entity.FundInfo)

Example 4 with FundInfo

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

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

the class FundInfoServiceImpl method batchAddFunds.

@Override
@Transactional(rollbackFor = Exception.class)
public boolean batchAddFunds(List<FundAddModel> fundInfoList) {
    Preconditions.checkState(CollectionUtils.isNotEmpty(fundInfoList), "待增加基金列表不能为空");
    final Long userId = UserIdContainer.getUserId();
    List<FundInfo> forAddFunds = new ArrayList<>();
    fundInfoList.forEach(addFund -> {
        FundInfo fundInfo = new FundInfo();
        fundInfo.setInStore(EnumYesOrNo.YES.val());
        fundInfo.setUserId(userId);
        fundInfo.setFundName(addFund.getFundName());
        fundInfo.setFundCode(addFund.getFundCode());
        fundInfo.setPurchaseAmount(new BigDecimal(addFund.getPurchaseAmount()));
        fundInfo.setPurchaseValue(new BigDecimal(addFund.getPurchaseValue()));
        fundInfo.setPurchaseCost(new BigDecimal(addFund.getPurchaseCost()));
        fundInfo.setPurchaseFee(new BigDecimal(addFund.getPurchaseFee()));
        fundInfo.setConfirmDate(addFund.getPurchaseConfirmDate());
        fundInfo.setPurchaseDate(addFund.getPurchaseDate());
        fundInfo.setModifyTime(new Date());
        fundInfo.setCreateTime(new Date());
        fundInfo.setIsDeleted(EnumYesOrNo.NO.val());
        fundInfo.setVersion(0);
        forAddFunds.add(fundInfo);
    });
    // 更新历史数据
    fundHistoryValueService.updateFundHistoryValues();
    // 更新历史净值数据
    fundHistoryNetValueService.updateHistoryNetValues();
    return mapper.batchInsert(forAddFunds) > 0;
}
Also used : FundInfo(com.tony.billing.entity.FundInfo) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) BigDecimal(java.math.BigDecimal) Date(java.util.Date) LocalDate(java.time.LocalDate) 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