Search in sources :

Example 1 with FundHistoryValue

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

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

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

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

the class FundHistoryValueServiceImpl method updateFundInfo.

private void updateFundInfo(FundInfo fundInfo) {
    String queryUrl = String.format(fundValueQueryUrl, fundInfo.getFundCode());
    OkHttpClient client = new OkHttpClient.Builder().callTimeout(10, TimeUnit.SECONDS).build();
    Request request = new Request.Builder().url(queryUrl).build();
    int tryTime = 3;
    boolean succeed = false;
    // 尝试三次
    while (!succeed && tryTime-- > 0) {
        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful() && response.body() != null) {
                String responseBodyStr = response.body().string();
                responseBodyStr = responseBodyStr.replaceAll("jsonpgz\\(", "").replaceAll("\\);", "");
                logger.debug("responseBody: {}", responseBodyStr);
                JSONObject jsonObject = JSONObject.parseObject(responseBodyStr);
                FundHistoryValue fundHistoryValue = new FundHistoryValue();
                fundHistoryValue.setFundCode(fundInfo.getFundCode());
                fundHistoryValue.setFundName(fundInfo.getFundName());
                fundHistoryValue.setAssessmentIncreaseRate(jsonObject.getString("gszzl"));
                fundHistoryValue.setAssessmentTime(jsonObject.getDate("gztime"));
                fundHistoryValue.setAssessmentDate(jsonObject.getDate("gztime"));
                fundHistoryValue.setAssessmentValue(new BigDecimal(jsonObject.getString("gsz")));
                fundHistoryValue.setFundConfirmDate(jsonObject.getDate("jzrq"));
                fundHistoryValue.setFundValue(new BigDecimal(jsonObject.getString("dwjz")));
                logger.debug("get fund info:{}", JSON.toJSONString(fundHistoryValue));
                insertIfNotExist(fundHistoryValue);
                succeed = true;
            }
        } catch (Exception e) {
            logger.error("获取基金:" + fundInfo.getFundCode() + " 估值信息失败", e);
        }
    }
}
Also used : DailyFundHistoryValueResponse(com.tony.billing.response.fund.DailyFundHistoryValueResponse) Response(okhttp3.Response) DailyFundChangedResponse(com.tony.billing.response.fund.DailyFundChangedResponse) OkHttpClient(okhttp3.OkHttpClient) JSONObject(com.alibaba.fastjson.JSONObject) FundHistoryValue(com.tony.billing.entity.FundHistoryValue) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Request(okhttp3.Request) BigDecimal(java.math.BigDecimal) IOException(java.io.IOException)

Example 5 with FundHistoryValue

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

FundHistoryValue (com.tony.billing.entity.FundHistoryValue)5 FundInfo (com.tony.billing.entity.FundInfo)4 BigDecimal (java.math.BigDecimal)4 DailyFundChangedResponse (com.tony.billing.response.fund.DailyFundChangedResponse)3 LocalDate (java.time.LocalDate)3 JSONObject (com.alibaba.fastjson.JSONObject)2 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)2 FundPreSaleInfo (com.tony.billing.entity.FundPreSaleInfo)2 DailyFundHistoryValueResponse (com.tony.billing.response.fund.DailyFundHistoryValueResponse)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 OkHttpClient (okhttp3.OkHttpClient)2 Request (okhttp3.Request)2 Response (okhttp3.Response)2 JSON (com.alibaba.fastjson.JSON)1 EnumYesOrNo (com.tony.billing.constants.enums.EnumYesOrNo)1 FundHistoryNetValueMapper (com.tony.billing.dao.mapper.FundHistoryNetValueMapper)1 FundHistoryValueMapper (com.tony.billing.dao.mapper.FundHistoryValueMapper)1 FundInfoMapper (com.tony.billing.dao.mapper.FundInfoMapper)1 FundHistoryNetValue (com.tony.billing.entity.FundHistoryNetValue)1