guoshilong
2024-03-12 b7c3fc85eba070a5c6625bc680ae9993d5553fae
skjcmanager/skjcmanager-service/skjcmanager-nky/src/main/java/cn/gistack/nky/service/impl/HsybServiceImpl.java
@@ -9,15 +9,25 @@
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import java.security.KeyStore;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
@@ -74,16 +84,19 @@
         HsybGetFuturePo hsybGetFuturePo = sendRequestToHsyb(GET_NEW_FUTURE, urlParams);
         //程序转为hashmap,手动转换类型
         List<NewSwFuturePo> data = JSON.parseArray(JSON.toJSONString(hsybGetFuturePo.getData()), NewSwFuturePo.class);
         List<NewSwFuturePo> filterData = filterPredict(data);
         if (hsybGetFuturePo.getData() !=null){
            //程序转为hashmap,手动转换类型
            List<NewSwFuturePo> data = JSON.parseArray(JSON.toJSONString(hsybGetFuturePo.getData()), NewSwFuturePo.class);
            List<NewSwFuturePo> filterData = filterPredict(data);
         log.info(StringUtil.format("过滤后的预测水位数据:{}", JSON.toJSONString(filterData)));
         return filterData;
//      }else{
//         //拿不到预测水位
//         return null;
//      }
            log.info(StringUtil.format("过滤后的预测水位数据:{}", JSON.toJSONString(filterData)));
            return filterData;
         }else {
            //因为会有data为null的数据,所以返回空数组
            List<NewSwFuturePo> list = new ArrayList<>();
            return list;
         }
   }
   public Boolean isResHasModel(String resId){
@@ -189,6 +202,7 @@
      return null;
   }
   @SneakyThrows
   public HsybGetFuturePo sendRequestToHsyb(String url, String urlParam) {
      // 获取环境
      String activeProfile = SpringContextUtil.getActiveProfile();
@@ -203,14 +217,40 @@
      }
      url = url + urlParam;
      log.info("洪水预报请求地址:{}", url);
      // 创建一个信任所有证书的 TrustManager
      TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
      trustManagerFactory.init((KeyStore) null);
      TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
      // 创建一个不验证主机名的 SSLContext
      SSLContext sslContext = SSLContext.getInstance("TLS");
      sslContext.init(null, trustManagers, new java.security.SecureRandom());
      // 创建一个不验证主机名的 SSLConnectionSocketFactory
      SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
      // 创建 HttpClient
      CloseableHttpClient httpClient = HttpClients.custom()
         .setSSLSocketFactory(sslsf)
         .build();
      // 创建 HttpComponentsClientHttpRequestFactory 并设置 HttpClient
      HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
      requestFactory.setHttpClient(httpClient);
      // 创建 RestTemplate 并设置请求工厂
      RestTemplate restTemplate = new RestTemplate(requestFactory);
      //设置请求头
      HttpHeaders headers = new HttpHeaders();
      headers.add("Authorization", AUTHORIZATION);
      //封装请求头
      HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<MultiValueMap<String, Object>>(headers);
      HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<>(headers);
      try {
         ResponseEntity<HsybGetFuturePo> exchange = restTemplate.exchange(url, HttpMethod.GET, formEntity, HsybGetFuturePo.class);
         log.info("请求结果:"+ exchange.getBody().toString());
         return exchange.getBody();
      } catch (Exception e) {
         e.printStackTrace();
@@ -313,7 +353,7 @@
       *             "resId": "42092250024"
       *         },
       */
      if (list.size() == 0) {
      if (null == list || list.size() == 0) {
         return null;
      }