| | |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.client.RestTemplate; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | |
| | |
| | | |
| | | @Override |
| | | public List<List<String>> getFuture(String resCd) { |
| | | HsybGetFuturePo hsybGetFuturePo = apiRequest(GET_FUTURE, resCd); |
| | | HsybGetFuturePo hsybGetFuturePo = apiRequest(GET_FUTURE, resCd); |
| | | |
| | | if (hsybGetFuturePo != null && hsybGetFuturePo.getRespCode().equals("200")){ |
| | | if (hsybGetFuturePo != null && hsybGetFuturePo.getRespCode().equals("200")) { |
| | | List<List<String>> data = hsybGetFuturePo.getData(); |
| | | |
| | | //过滤空值数据,过滤过去时间 |
| | | List<List<String>> collect = |
| | | data.stream().filter(item -> item.get(0) != null).filter(item -> CommonUtil.strToDate(item.get(0)).after(DateUtil.now())).collect(Collectors.toList()); |
| | | List<List<String>> collect = filterPredictData(data); |
| | | return collect; |
| | | }else { |
| | | } else { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | public HsybGetFuturePo apiRequest(String url, String jsonparams){ |
| | | public HsybGetFuturePo apiRequest(String url, String jsonparams) { |
| | | |
| | | // Long param = Long.parseLong(jsonparams); |
| | | System.out.println("请求参数:"+ jsonparams); |
| | | System.out.println("请求参数:" + jsonparams); |
| | | // 声明一个header变量 |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | // 设置为json格式 |
| | | MediaType mediaType = MediaType.parseMediaType("application/json;charset=utf-8"); |
| | | headers.setContentType(mediaType); |
| | | //设置请求头 |
| | | headers.set("Authorization",AUTHORIZATION); |
| | | headers.set("Authorization", AUTHORIZATION); |
| | | headers.add("Accept", MediaType.APPLICATION_JSON.toString()); |
| | | |
| | | HttpEntity<String> httpEntity = new HttpEntity(jsonparams, headers); |
| | |
| | | HsybGetFuturePo hsybGetFuturePo = template.postForObject(url, httpEntity, HsybGetFuturePo.class); |
| | | System.out.println(hsybGetFuturePo.toString()); |
| | | return hsybGetFuturePo; |
| | | }catch (Exception e){ |
| | | } catch (Exception e) { |
| | | System.out.println(e); |
| | | } |
| | | |
| | | return null; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 对预测数据进行过滤 |
| | | * |
| | | * @param data |
| | | * @return |
| | | */ |
| | | public List<List<String>> filterPredictData(List<List<String>> data) { |
| | | String formatter = "yyyy-MM-dd"; |
| | | String today = DateUtil.format(new Date(), formatter); |
| | | |
| | | Long time = new Date().getTime() + 60 * 60 * 24 * 1000; |
| | | Date tomorrowDate = new Date(time); |
| | | String tomorrow = DateUtil.format(tomorrowDate, formatter); |
| | | |
| | | //过滤空值数据,过滤过去时间 |
| | | List<List<String>> collect = data.stream().filter(item -> item.get(0) != null).collect(Collectors.toList()); |
| | | Integer index = findIndex(collect, "/"); |
| | | |
| | | if (index>-1){ |
| | | collect.get(index).set(0,tomorrow + collect.get(index).get(0).substring(5)); |
| | | } |
| | | |
| | | for (int i = 0; i < collect.size(); i++) { |
| | | if (i<index && index>-1){ |
| | | collect.get(i).set(0,today + " " + collect.get(i).get(0)); |
| | | }else if (i>index && index>-1){ |
| | | collect.get(i).set(0,tomorrow + " " + collect.get(i).get(0)); |
| | | }else if ( i!= index && index == -1){ |
| | | collect.get(i).set(0,today + " " + collect.get(i).get(0)); |
| | | } |
| | | } |
| | | List<List<String>> filterData = data.stream().filter(item -> item.get(0) != null).filter(item -> CommonUtil.strToDate(item.get(0)).after(DateUtil.now())).collect(Collectors.toList()); |
| | | return filterData; |
| | | } |
| | | |
| | | |
| | | public Integer findIndex(List<List<String>> list,String reg){ |
| | | for (int i = 0; i < list.size(); i++) { |
| | | if (list.get(i).get(0).indexOf(reg)>-1){ |
| | | return i; |
| | | } |
| | | } |
| | | return -1; |
| | | } |
| | | } |