package cn.gistack.nky.service.impl;
|
|
import cn.gistack.common.utils.CommonUtil;
|
import cn.gistack.common.utils.SpringContextUtil;
|
import cn.gistack.nky.resultpojo.HsybGetFuturePo;
|
import cn.gistack.nky.service.IHsybService;
|
import lombok.AllArgsConstructor;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springblade.core.tool.utils.DateUtil;
|
import org.springframework.http.HttpEntity;
|
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.MediaType;
|
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;
|
|
@Service
|
@AllArgsConstructor
|
@Slf4j
|
public class HsybServiceImpl implements IHsybService {
|
|
private static String ONLINE = "http://10.42.7.148:50001";
|
private static String LOCAL = "https://sk.hubeishuiyi.cn";
|
private static String GET_FUTURE = "/hsybApi/api/fh-admin/skkr/getFuture";
|
private static String GET_FUTURE_ONLINE = "/api/fh-admin/skkr/getFuture";
|
|
private static String AUTHORIZATION = "Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiJBRE1JTiIsIlVzZXJJZCI6IjEiLCJzY29wZSI6WyJhbGwiXSwiVXNlclJlYWxOYW1lIjoi6LaF57qn566h55CG5ZGYIiwiVXNlclh6cWhkbSI6IjQyMDUiLCJleHAiOjI1MTk3NzIwMTAsImp0aSI6ImExNTcxYzk1LTBkNjMtNDkyYi1iOWEyLTE2ZTIzNTQ5ZTY1ZiIsImNsaWVudF9pZCI6InVzZXItc2VydmljZSJ9.ebNVZrw9LbhKaj2w6RR8b2wccQiDkhvBeq79SxxCK-yWiOlIFqBkotTN4TNJg8umcpyYvLILwvqXWRJhffEtgi25sX2y6MqLIWM4kMZ9d8ptdnSmTpBPhltSiQOM0KFa1kl5nSDCBwYOLn-pESJglam76cjpgZNoC88x3iNHacdiXDItY0rtY85HrQ26uyJu9UovKtmYmZRHsIbGMpDta5Q1p4vfaCIr-YUayDrCweZJiQDEEcOSpWJ7O7RMk3pRkX_4UmPHFzrOI2lMp1jQIhxnSTE7EVAz_4z8h8r46muSkpF54Ic4XSawHKqdSLHx8T05LB0MpvOzWMPS6c1uHA";
|
|
|
@Override
|
public List<List<String>> getFuture(String resCd) {
|
HsybGetFuturePo hsybGetFuturePo = apiRequest(GET_FUTURE, resCd);
|
|
if (hsybGetFuturePo != null && hsybGetFuturePo.getRespCode().equals("200")) {
|
List<List<String>> data = hsybGetFuturePo.getData();
|
|
List<List<String>> collect = filterPredictData(data);
|
return collect;
|
} else {
|
return null;
|
}
|
}
|
|
public HsybGetFuturePo apiRequest(String url, String jsonparams) {
|
|
// 获取环境
|
String activeProfile = SpringContextUtil.getActiveProfile();
|
if (activeProfile.equals("dev")) {
|
url = LOCAL+ url ;
|
}
|
if (activeProfile.equals("prod")) {
|
url = ONLINE+ GET_FUTURE_ONLINE ;
|
}
|
if (activeProfile.equals("test")) {
|
url = ONLINE + GET_FUTURE_ONLINE;
|
}
|
|
|
log.info("预测水位请求参数:" + jsonparams);
|
log.info("预测水位请求地址:" + url);
|
|
// 声明一个header变量
|
HttpHeaders headers = new HttpHeaders();
|
// 设置为json格式
|
MediaType mediaType = MediaType.parseMediaType("application/json;charset=utf-8");
|
headers.setContentType(mediaType);
|
//设置请求头
|
headers.set("Authorization", AUTHORIZATION);
|
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
|
|
HttpEntity<String> httpEntity = new HttpEntity(jsonparams, headers);
|
RestTemplate template = new RestTemplate();
|
try {
|
HsybGetFuturePo hsybGetFuturePo = template.postForObject(url, httpEntity, HsybGetFuturePo.class);
|
log.info("预测水位返回结果:" + hsybGetFuturePo.toString());
|
return hsybGetFuturePo;
|
} catch (Exception e) {
|
System.out.println(e);
|
}
|
|
return null;
|
}
|
|
|
/**
|
* 对预测数据进行过滤
|
*
|
* @param data
|
* @return
|
*/
|
public List<List<String>> filterPredictData(List<List<String>> data) {
|
|
//获取当前时间
|
String formatter = "HH:00";
|
String nowTime = DateUtil.format(new Date(), formatter);
|
|
//获取等于当前时间的水位数据
|
List<List<String>> collect = data.stream().filter(infoList -> infoList.get(0).equals(nowTime)).collect(Collectors.toList());
|
|
collect.forEach(infoList->{
|
//给第一项重新赋值,因为后面取的是第一项,且预测参数时间只要日期
|
infoList.set(0,infoList.get(4).split(" ")[0]);
|
});
|
return collect;
|
|
|
|
|
|
|
|
|
|
//
|
// 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);
|
//
|
//
|
// String nowHour = DateUtil.format(new Date(),"HH");
|
//
|
// //过滤空值数据
|
// List<List<String>> collect = data.stream().filter(item -> item.get(0) != null).collect(Collectors.toList());
|
//
|
// //切割过去数据
|
// Integer nowIndex = findIndex(collect, nowHour);
|
// List<List<String>> subList = collect.subList(nowIndex, collect.size());
|
// int index = -1;
|
// //添加日期
|
// for (int i = 0; i < subList.size(); i++) {
|
// List<String> temp = subList.get(i);
|
// if (index == -1 && temp.get(0).indexOf("/")>-1){
|
// index = i;
|
// temp.set(0,tomorrow + " " +temp.get(0).substring(5));
|
// }
|
//
|
// if (index != -1){
|
// temp.set(0,tomorrow + " " + temp.get(0).substring(5));
|
// }else {
|
// //给后面的时间添加今天的日期
|
// temp.set(0,today + " " + temp.get(0));
|
// }
|
//
|
// }
|
//
|
//
|
// //截取掉时间部分
|
// subList.forEach(e->{
|
// e.set(0,e.get(0).split(" ")[0]);
|
// });
|
//
|
// //去除重复数据
|
// List<List<String>> distinctList = subList.stream().distinct().collect(Collectors.toList());
|
//
|
//
|
// //去重
|
// for ( int i = 0 ; i < distinctList.size() - 1 ; i ++ ) {
|
// for ( int j = distinctList.size() - 1 ; j > i; j -- ) {
|
// // 这里是对象的比较,如果去重条件不一样,在这里修改即可
|
// if (distinctList.get(j).get(0).equals(distinctList.get(i).get(0))) {
|
// distinctList.remove(i);
|
// }
|
// }
|
// }
|
|
// return distinctList;
|
}
|
|
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;
|
}
|
}
|