ybf

ybf


1.8处理抓取汇率数据(有的网站数据需要做汇率时效性处理)

/**

  • 处理抓取汇率数据
  • @author **/ @Slf4j @Service public class DisposeRateDataService {

    @Autowired private ExchangeRateValidSettingsServiceImpl exchangeRateValidSettingsService; @Autowired private BaseExchangeRateServiceImpl baseExchangeRateService; @Autowired private StringRedisTemplate stringRedisTemplate;

    /**

    • 处理抓取汇率数据 */ public CommonExchageResp disposeRateData(PatformKeyEnum patformKeyEnum, TradeDirectionEnum tradeDirectionEnum, List<CommonExchageResp> rateDataList){ //查询汇率有效性配置数据 ExchangeRateValidSettingsPO exchangeRateValidSettingsPO = getExchangeRateValidSettings(tradeDirectionEnum.getCode()); log.info("处理抓取汇率数据 exchangeRateValidSettingsPO="+ JacksonUtils.toJson(exchangeRateValidSettingsPO)); if(Objects.isNull(exchangeRateValidSettingsPO)){ log.info("处理抓取汇率数据 查询汇率有效性配置为空"); return null; } if(rateDataList==null||rateDataList.size()==0){ log.info("处理抓取汇率数据 要处理的数据为空"); return null; } return disposeData(rateDataList, exchangeRateValidSettingsPO, patformKeyEnum); }

    private ExchangeRateValidSettingsPO getExchangeRateValidSettings(Integer type) { ExchangeRateValidSettingsPO exchangeRateValidSettingsPO = null; String key = RedisConstantKey.EXCHANGE_RATE_VALID_SETTINGS +type; if(stringRedisTemplate.hasKey(key)){ log.info("处理抓取汇率数据 getExchangeRateValidSettings从redis取"); exchangeRateValidSettingsPO = JSONObject.parseObject(stringRedisTemplate.opsForValue().get(key), ExchangeRateValidSettingsPO.class); }else{ log.info("处理抓取汇率数据 getExchangeRateValidSettings从数据库取"); LambdaQueryWrapper<ExchangeRateValidSettingsPO> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(ExchangeRateValidSettingsPO::getType, type); exchangeRateValidSettingsPO = exchangeRateValidSettingsService.getOne(queryWrapper); stringRedisTemplate.opsForValue().set(key, JSONObject.toJSONString(exchangeRateValidSettingsPO), 1, TimeUnit.HOURS); } return exchangeRateValidSettingsPO; }

    private CommonExchageResp disposeData(List<CommonExchageResp> rateDataList,ExchangeRateValidSettingsPO exchangeRateValidSettingsPO,PatformKeyEnum patformKeyEnum) { CommonExchageResp commonExchageResp = null; List<CommonExchageResp> dataList = new ArrayList<>(); for (CommonExchageResp data:rateDataList) { //交易限额最低值(人民币)> 5万 if(data.getDownLimit().compareTo(new BigDecimal(Integer.parseInt(exchangeRateValidSettingsPO.getMin().toString()))) > 0){ log.info("处理抓取汇率数据 最低限额 > 汇率有效性配置值 downLimit={},min={}" ,data.getDownLimit().toString(), exchangeRateValidSettingsPO.getMin().toString()); continue; } //交易限额最高值于最低值(人民币)的差 < 5000 if(data.getUpLimit().subtract(data.getDownLimit()).compareTo( new BigDecimal(Integer.parseInt(exchangeRateValidSettingsPO.getDiff().toString()))) < 0){ log.info("处理抓取汇率数据 交易限额最高值于最低值(人民币)的差 < 汇率有效性配置值 upLimit-downLimit={}, diff={}" ,data.getUpLimit().subtract(data.getDownLimit()).toString() ,exchangeRateValidSettingsPO.getDiff().toString()); continue; } //挂单人成交量 < 20 if(data.getTradingVolume()<exchangeRateValidSettingsPO.getTrading()){ log.info("处理抓取汇率数据 挂单人成交量 < 汇率有效性配置值 tradingVolume={}, trading={}" ,data.getTradingVolume().toString(),exchangeRateValidSettingsPO.getTrading().toString()); continue; } //挂单人成交率 < 90% if (data.getCloseRate().multiply(Constant.HUNDRED).compareTo(exchangeRateValidSettingsPO.getTradingRate()) < 0) { log.info("处理抓取汇率数据 挂单人成交率 < 汇率有效性配置值 closeRate={}, tradingRate={}" ,data.getCloseRate().multiply(Constant.HUNDRED).toString(), exchangeRateValidSettingsPO.getTradingRate().toString()); continue; } dataList.add(data); } log.info("处理抓取汇率数据 筛选数据count={}", dataList.size()); if(dataList.size()< Constant.RATE_VALID_NUM){ log.info("处理抓取汇率数据 筛选数据小于"+Constant.RATE_VALID_NUM+"条"); //芒果告警 baseExchangeRateService.sendRateWarnMangoMsg(patformKeyEnum.getPatformName(), patformKeyEnum.getKey()); return null; } //可用数据列表的价格平均值 BigDecimal priceSum = dataList.stream().map(CommonExchageResp::getPrice).reduce(BigDecimal.ZERO, BigDecimal::add); BigDecimal priceAvg = priceSum.divide(new BigDecimal(dataList.size()), 8, RoundingMode.CEILING); log.info("处理抓取汇率数据 筛选数据priceAvg={}", priceAvg.toString()); //从满足的数据List里取一条最可靠的数据 for (CommonExchageResp c:dataList) { //当前一条汇率金额/所有可用数据平均值 计算结果是否大于1% 不可用 if(c.getPrice().divide(priceAvg, 4, RoundingMode.CEILING).subtract(BigDecimal.ONE).abs() .compareTo(exchangeRateValidSettingsPO.getDiffRate()) <= 0){ log.info("处理抓取汇率数据 当前一条汇率金额/所有可用数据平均值-1={},diffRate={}",c.getPrice() .divide(priceAvg, 4, RoundingMode.CEILING).subtract(BigDecimal.ONE).abs().toString(), exchangeRateValidSettingsPO.getDiffRate().toString()); log.info("处理抓取汇率数据 可用数据c="+JacksonUtils.toJson(c)); commonExchageResp = c; break; } } return commonExchageResp; } }

页面列表

ITEM_HTML