Search in sources :

Example 1 with FundHistoryNetValue

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

the class FundHistoryNetValueServiceImpl method getHistoryNetValuesByFundCode.

@Override
public FundHistoryNetValueResponse getHistoryNetValuesByFundCode(String fundCode, String dateAfter, String dateBefore) {
    Preconditions.checkState(StringUtils.isNotEmpty(fundCode), "基金编码不能为空");
    if (StringUtils.isEmpty(dateAfter)) {
        LocalDate localDate = LocalDate.now();
        // 大约30个工作日
        localDate = localDate.plusDays(-43);
        dateAfter = DateUtil.formatDateTime(localDate, "yyyy-MM-dd");
    }
    if (StringUtils.isEmpty(dateBefore)) {
        dateBefore = DateUtil.formatDateTime(LocalDate.now(), "yyyy-MM-dd");
    }
    List<FundHistoryNetValue> historyNetValues = mapper.getHistoryNetValueInRange(dateAfter, dateBefore, fundCode);
    if (CollectionUtils.isNotEmpty(historyNetValues)) {
        FundHistoryNetValueResponse response = new FundHistoryNetValueResponse();
        response.setFundCode(fundCode);
        response.setHistoryNetValues(historyNetValues.stream().map(fundHistory -> new HashMap<String, String>(4) {

            {
                put(fundHistory.getConfirmDate(), fundHistory.getFundNetValue().toString());
            }
        }).collect(Collectors.toList()));
        response.setPurchaseDates(fundInfoMapper.listPurchaseDatesInRange(dateAfter, dateBefore, fundCode));
        response.setSoldDates(fundPreSaleInfoMapper.listPurchaseDatesInRange(dateAfter, dateBefore, fundCode));
        return response;
    }
    return ResponseUtil.error(new FundHistoryNetValueResponse());
}
Also used : FundHistoryNetValueResponse(com.tony.billing.response.fund.FundHistoryNetValueResponse) FundHistoryNetValue(com.tony.billing.entity.FundHistoryNetValue) LocalDate(java.time.LocalDate)

Example 2 with FundHistoryNetValue

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

the class FundHistoryNetValueServiceImpl method updateFundInfo.

private List<FundHistoryNetValue> updateFundInfo(String fundCode) {
    String queryUrl = String.format(fundHistoryNetValueQueryUrl, fundCode, System.currentTimeMillis());
    OkHttpClient client = new OkHttpClient.Builder().callTimeout(10, TimeUnit.SECONDS).build();
    Request request = new Request.Builder().url(queryUrl).build();
    int tryTime = 3;
    // 尝试三次
    while (tryTime-- > 0) {
        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful() && response.body() != null) {
                String responseBodyStr = response.body().string();
                responseBodyStr = responseBodyStr.replaceAll("//\\*[^*]\\*//", "");
                logger.debug("responseBody: {}", responseBodyStr);
                List<FundHistoryNetValue> historyNetValueList = new ArrayList<>();
                Matcher netWorthMatcher = DATA_NET_WORTH.matcher(responseBodyStr);
                Matcher acWorthMatcher = DATA_AC_WORTH.matcher(responseBodyStr);
                if (netWorthMatcher.find() && acWorthMatcher.find()) {
                    JSONArray historyNetWorthArray = JSON.parseArray(netWorthMatcher.group(1));
                    JSONArray historyAcWorthArray = JSON.parseArray(acWorthMatcher.group(1));
                    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                    for (int i = 0; i < historyNetWorthArray.size(); i++) {
                        JSONObject netWorthObj = historyNetWorthArray.getJSONObject(i);
                        JSONArray acWorthArray = historyAcWorthArray.getJSONArray(i);
                        if (netWorthObj != null) {
                            LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(netWorthObj.getLong("x")), TimeConstants.CHINA_ZONE);
                            if (dateTime.getYear() < LocalDateTime.now().getYear() - 1) {
                                // 丢弃前年的数据
                                continue;
                            }
                            FundHistoryNetValue netValue = new FundHistoryNetValue();
                            netValue.setConfirmDate(dateTimeFormatter.format(dateTime));
                            netValue.setFundNetValue(new BigDecimal(netWorthObj.getString("y")));
                            try {
                                netValue.setFundAcNetValue(acWorthArray.getBigDecimal(1));
                            } catch (Exception e) {
                                netValue.setFundAcNetValue(new BigDecimal(netWorthObj.getString("y")));
                            }
                            netValue.setIncreaseRate(new BigDecimal(netWorthObj.getString("equityReturn")));
                            netValue.setCreateTime(new Date());
                            netValue.setModifyTime(new Date());
                            netValue.setIsDeleted(EnumDeleted.NOT_DELETED.val());
                            netValue.setFundCode(fundCode);
                            historyNetValueList.add(netValue);
                        }
                    }
                    logger.debug("get fund info length: {} values: {}", historyNetValueList.size(), JSON.toJSONString(historyNetValueList));
                    return historyNetValueList;
                }
            }
        } catch (Exception e) {
            logger.error("获取基金:" + fundCode + " 估值信息失败", e);
        }
    }
    return Collections.emptyList();
}
Also used : LocalDateTime(java.time.LocalDateTime) OkHttpClient(okhttp3.OkHttpClient) FundHistoryNetValue(com.tony.billing.entity.FundHistoryNetValue) Matcher(java.util.regex.Matcher) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Request(okhttp3.Request) ArrayList(java.util.ArrayList) JSONArray(com.alibaba.fastjson.JSONArray) BigDecimal(java.math.BigDecimal) Date(java.util.Date) LocalDate(java.time.LocalDate) FundHistoryNetValueResponse(com.tony.billing.response.fund.FundHistoryNetValueResponse) Response(okhttp3.Response) JSONObject(com.alibaba.fastjson.JSONObject) DateTimeFormatter(java.time.format.DateTimeFormatter)

Aggregations

FundHistoryNetValue (com.tony.billing.entity.FundHistoryNetValue)2 FundHistoryNetValueResponse (com.tony.billing.response.fund.FundHistoryNetValueResponse)2 LocalDate (java.time.LocalDate)2 JSONArray (com.alibaba.fastjson.JSONArray)1 JSONObject (com.alibaba.fastjson.JSONObject)1 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 BigDecimal (java.math.BigDecimal)1 LocalDateTime (java.time.LocalDateTime)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Matcher (java.util.regex.Matcher)1 OkHttpClient (okhttp3.OkHttpClient)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1