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());
}
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();
}
Aggregations