| | |
| | | package com.integrated.zyyt.util; |
| | | |
| | | import cn.hutool.core.convert.Convert; |
| | | import cn.hutool.core.io.IORuntimeException; |
| | | import cn.hutool.http.HttpRequest; |
| | | import cn.hutool.http.HttpResponse; |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.integrated.zyyt.ZyytConstant; |
| | |
| | | import com.integrated.zyyt.enetity.business.ZyytTShkdrb; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.http.HttpStatus; |
| | | import org.springframework.jdbc.core.JdbcTemplate; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.CollectionUtils; |
| | | import org.springframework.util.StringUtils; |
| | | |
| | | import java.time.LocalDate; |
| | | import java.util.HashMap; |
| | |
| | | @Component |
| | | @Slf4j |
| | | public class ZyytUtil { |
| | | /** |
| | | * 超时重试次数 |
| | | */ |
| | | private static int TRYNUM = 3; |
| | | |
| | | @Value("${pullData.tryNum}") |
| | | public void setTRYNUM(Integer tryNum) { |
| | | tryNum = tryNum <= 0 ? 3 : tryNum; |
| | | this.TRYNUM = tryNum; |
| | | } |
| | | |
| | | /** |
| | | * http超时时间设置 |
| | | */ |
| | | private static int TRYTIMEOUT = 3 * 1000; |
| | | |
| | | @Value("${pullData.tryTimeOut}") |
| | | public void setTRYTIMEOUT(Integer tryTimeOut) { |
| | | this.TRYTIMEOUT = tryTimeOut * 1000; |
| | | } |
| | | |
| | | private static JdbcTemplate jdbcTemplate; |
| | | |
| | | @Autowired |
| | |
| | | params.put("ciphertext", eciphrtext); |
| | | params.put("client", client); |
| | | params.put("secret", secret); |
| | | |
| | | String httpResult = HttpRequest.post(url) |
| | | HttpRequest httpRequest = HttpRequest.post(url) |
| | | .form(params) |
| | | .header("X-EOS-SourceSysKey", sourceSysKey) |
| | | .execute() |
| | | .body(); |
| | | .timeout(TRYTIMEOUT); |
| | | log.info("超时时间为{}毫秒", TRYTIMEOUT); |
| | | log.info("url =》》{}", url); |
| | | log.info("params =》》{}", params); |
| | | HttpResponse httpResponse = null; |
| | | int tryNumTemp = 1; |
| | | boolean isTimeOut = false; |
| | | try { |
| | | httpResponse = httpRequest.execute(); |
| | | } catch (IORuntimeException e) { |
| | | // e.printStackTrace(); |
| | | // 超时重试 |
| | | isTimeOut = true; |
| | | log.error(e.getMessage()); |
| | | } |
| | | while (tryNumTemp <= TRYNUM && isTimeOut) { |
| | | log.error("请求接口超时,进行第{}次重试!", tryNumTemp); |
| | | try { |
| | | httpResponse = httpRequest.execute(); |
| | | isTimeOut = false; |
| | | } catch (IORuntimeException e) { |
| | | // e.printStackTrace(); |
| | | // 超时重试 |
| | | log.error(e.getMessage()); |
| | | } |
| | | tryNumTemp++; |
| | | } |
| | | if (httpResponse == null) { |
| | | log.error("请求接口超时,请联系接口提供方!"); |
| | | return null; |
| | | } |
| | | int responseStatus = httpResponse.getStatus(); |
| | | if (HttpStatus.NOT_FOUND.value() == responseStatus) { |
| | | log.error("接口API异常,请联系接口提供方!HttpStatus:{}", responseStatus); |
| | | return null; |
| | | } else if (HttpStatus.OK.value() != responseStatus) { |
| | | log.error("请求接口状态有误!HttpStatus:{}", responseStatus); |
| | | return null; |
| | | } |
| | | String httpResult = httpResponse.body(); |
| | | log.info("result =》》{}", httpResult); |
| | | try { |
| | | return JSONObject.parseObject(httpResult, TokenResult.class); |
| | | } catch (Exception e) { |
| | | log.error(e.getMessage()); |
| | | log.error("Token解析失败!", e.getMessage()); |
| | | return null; |
| | | } |
| | | } |
| | |
| | | * @return |
| | | */ |
| | | public static <T> ZyytDataResult<T> getData(String url, String bearToken, String sourceSysKey, JSONObject params, int pageNum, int pageSize, Class<T> tClass) { |
| | | if (StringUtils.isEmpty(bearToken)) { |
| | | log.error("bearToken不能为null!"); |
| | | return null; |
| | | } |
| | | HashMap header = new HashMap(); |
| | | header.put("Authorization", "Bearer " + bearToken); |
| | | header.put("X-EOS-SourceSysKey", sourceSysKey); |
| | |
| | | log.info("url =》》{}", trueUrl); |
| | | log.info("header=>>bearToken》》{}\r\nSourceSysKey=>>{}", bearToken, sourceSysKey); |
| | | log.info("params =》》{}", params); |
| | | String httpResult = HttpRequest.post(trueUrl) |
| | | HttpRequest httpRequest = HttpRequest.post(trueUrl) |
| | | .addHeaders(header) |
| | | .contentType("application/json") |
| | | .body(params.toJSONString()) |
| | | .timeout(1000 * 50) |
| | | .execute() |
| | | .body(); |
| | | .timeout(TRYTIMEOUT); |
| | | log.info("超时时间为{}毫秒", TRYTIMEOUT); |
| | | HttpResponse httpResponse = null; |
| | | |
| | | int tryNumTemp = 1; |
| | | boolean isTimeOut = false; |
| | | try { |
| | | httpResponse = httpRequest.execute(); |
| | | } catch (IORuntimeException e) { |
| | | // 超时重试 |
| | | isTimeOut = true; |
| | | log.error(e.getMessage()); |
| | | } |
| | | while (tryNumTemp <= TRYNUM && isTimeOut) { |
| | | log.error("请求接口超时,进行第{}次重试!", tryNumTemp); |
| | | try { |
| | | httpResponse = httpRequest.execute(); |
| | | isTimeOut = false; |
| | | } catch (IORuntimeException e) { |
| | | // e.printStackTrace(); |
| | | // 超时重试 |
| | | log.error(e.getMessage()); |
| | | } |
| | | tryNumTemp++; |
| | | } |
| | | if (httpResponse == null) { |
| | | log.error("请求接口超时,请联系接口提供方!"); |
| | | return null; |
| | | } |
| | | int responseStatus = httpResponse.getStatus(); |
| | | if (HttpStatus.NOT_FOUND.value() != responseStatus) { |
| | | log.error("接口API异常,请联系接口提供方!HttpStatus:{}", responseStatus); |
| | | return null; |
| | | } else if (HttpStatus.OK.value() != responseStatus) { |
| | | log.error("请求接口状态有误!HttpStatus:{}", responseStatus); |
| | | return null; |
| | | } |
| | | String httpResult = httpResponse.body(); |
| | | try { |
| | | JSONObject respJson = JSONObject.parseObject(httpResult); |
| | | log.info("开始解析结果4"); |
| | | Long contentSize = respJson.getLong("contentSize"); |
| | | if (contentSize != null && contentSize.compareTo(0L) > 0) { |
| | | JSONArray dataArray = respJson.getJSONArray("content"); |
| | | log.info("开始解析结果5"); |
| | | List<T> content = dataArray.toJavaList(tClass); |
| | | respJson.remove("content"); |
| | | log.info("开始解析结果6"); |
| | | ZyytDataResult<T> zyytDataResult = respJson.toJavaObject(ZyytDataResult.class); |
| | | zyytDataResult.setContent(content); |
| | | return zyytDataResult; |
| | |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("getData 异常"); |
| | | e.printStackTrace(); |
| | | // e.printStackTrace(); |
| | | return null; |
| | | } |
| | | } |
| | |
| | | String sql = "SELECT*FROM (SELECT*FROM ZYYT_RECORD WHERE BATCH_NO='" + getBatchNo(date) + "' AND TABLE_NAME='" + tableName + "' ORDER BY START_TIME DESC) tmp WHERE ROWNUM<=1"; |
| | | Map<String, Object> map = null; |
| | | try { |
| | | map = jdbcTemplate.queryForMap(sql); |
| | | // map = jdbcTemplate.queryForMap(sql); |
| | | List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql); |
| | | if (CollectionUtils.isEmpty(maps)) { |
| | | return map; |
| | | } |
| | | map = maps.get(0); |
| | | } catch (Exception e) { |
| | | log.error("sql=={} 查询发生了异常,可能因为没有查询到数据", sql); |
| | | log.error(e.getMessage()); |
| | |
| | | public static Long getApiTotalElements(String tableName, LocalDate date) { |
| | | Zyyt businessEntity = getBusinessEntity(tableName, date); |
| | | ZyytDataResult zyytDataResult = businessEntity.getData(0, 1); |
| | | if (zyytDataResult == null) { |
| | | log.info("拉取数据失败,拉取返回值为null"); |
| | | return 0l; |
| | | } |
| | | log.info("总共拉取到数据{}条", zyytDataResult.getTotalElements()); |
| | | return zyytDataResult.getTotalElements(); |
| | | } |