| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- package com.zksy.data.utils;
- import cn.hutool.core.util.ObjectUtil;
- import cn.hutool.core.util.StrUtil;
- import com.fasterxml.jackson.core.JsonProcessingException;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.fasterxml.jackson.databind.exc.InvalidFormatException;
- import com.zksy.common.domain.response.BaseResponse;
- import com.zksy.common.domain.response.BaseResponse2;
- import com.zksy.common.domain.response.BaseResponse3;
- import com.zksy.common.exception.CommonException;
- import com.zksy.common.utils.BeanUtils;
- import com.zksy.data.config.XhConfigProperty;
- import com.zksy.data.config.XinyonghuaihuaProperty;
- import com.zksy.data.constant.RedisKeyConstant;
- import com.zksy.data.domain.po.BasePo;
- import com.zksy.data.schedule.RefreshTokenSchedule;
- import lombok.extern.slf4j.Slf4j;
- import okhttp3.*;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.stereotype.Component;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- import java.util.concurrent.atomic.AtomicInteger;
- @Component
- @Slf4j
- public class XhRequestUtil {
- @Autowired
- private XhConfigProperty xhConfigProperty;
- @Autowired
- private XinyonghuaihuaProperty xinyonghuaihuaProperty;
- @Autowired
- private OkHttpClient okHttpClient;
- @Autowired
- private ObjectMapper objectMapper;
- @Autowired
- private RedisTemplate<String, String> redisTemplate;
- private static final int MAX_RETRIES = 3;
- private static AtomicInteger retryCount = new AtomicInteger(0);
- public <T extends BaseResponse, K extends BasePo> List<K> httpRequestByUniCode(String uniCode, String address,
- Class<T> responseClass,
- Class<K> entityClass) {
- if(redisTemplate.opsForValue().get(address) !=null){
- throw new CommonException("停止并行流",5001);
- }
- // log.info("正在同步:{}下的:{}", address, uniCode);
- ArrayList<K> resList = new ArrayList<>();
- if (StrUtil.isEmpty(uniCode)) {
- log.warn("统一社会信用代码为空");
- }
- HttpUrl url = new HttpUrl.Builder()
- .scheme("http")
- .host(xhConfigProperty.getHost())
- .port(Integer.valueOf(xhConfigProperty.getPort()))
- .addPathSegment(address)
- .addQueryParameter("UNISCID", uniCode)
- .build();
- Request request = new Request.Builder()
- .url(url)
- .get()
- .addHeader("appKey", xhConfigProperty.getAppKey())
- .build();
- try {
- Response response = okHttpClient.newCall(request).execute();
- if (!response.isSuccessful()) {
- log.error("当前接口请求失败:{}", address);
- }
- if (ObjectUtil.isEmpty(response.body())) {
- log.error("当前接口请求数据出现问题:{}", address);
- }
- String s = response.body().string();
- T value = objectMapper.readValue(s, responseClass);
- if (value.getCode() != 200) {
- log.error("当前请求结果异常:{}", value.getMessage());
- } else {
- if (value.getData().isEmpty()) {
- return null;
- }
- for (int i = 0; i < value.getData().size(); i++) {
- K e = BeanUtils.copyBean(value.getData().get(i), entityClass);
- e.setUniCode(uniCode);
- resList.add(e);
- }
- }
- return resList;
- } catch (Exception e) {
- log.error("此次数据同步出现异常,接口是:{}", address);
- throw new RuntimeException(e);
- }
- }
- public <T extends BaseResponse, K extends BasePo> List<K> httpRequestByUniCode(String uniCode, String address,
- Class<T> responseClass,
- Class<K> entityClass,
- String key) {
- if(redisTemplate.opsForValue().get(address) !=null){
- throw new CommonException("停止并行流",5001);
- }
- // log.info("正在同步:{}下的:{}", address, uniCode);
- ArrayList<K> resList = new ArrayList<>();
- if (StrUtil.isEmpty(uniCode)) {
- log.warn("统一社会信用代码为空");
- }
- HttpUrl url = new HttpUrl.Builder()
- .scheme("http")
- .host(xhConfigProperty.getHost())
- .port(Integer.valueOf(xhConfigProperty.getPort()))
- .addPathSegment(address)
- .addQueryParameter(key, uniCode)
- .build();
- Request request = new Request.Builder()
- .url(url)
- .get()
- .addHeader("appKey", xhConfigProperty.getAppKey())
- .build();
- try {
- Response response = okHttpClient.newCall(request).execute();
- if (!response.isSuccessful()) {
- log.error("当前接口请求失败:{}", address);
- }
- if (ObjectUtil.isEmpty(response.body())) {
- log.error("当前接口请求数据出现问题:{}", address);
- }
- String s = response.body().string();
- T value = objectMapper.readValue(s, responseClass);
- if (value.getCode() != 200) {
- log.error("当前请求结果异常:{}", value.getMessage());
- throw new CommonException("请求异常",5000);
- } else {
- if (value.getData().isEmpty()) {
- return null;
- }
- for (int i = 0; i < value.getData().size(); i++) {
- K e = BeanUtils.copyBean(value.getData().get(i), entityClass);
- e.setUniCode(uniCode);
- resList.add(e);
- }
- }
- return resList;
- } catch (Exception e) {
- log.error("此次数据同步出现异常,接口是:{}", address);
- throw new RuntimeException(e);
- }
- }
- private Boolean isEnd = false;
- public String testHttpRequest(String uniCode, String address, String paramsValue) {
- if (isEnd) {
- isEnd = false;
- throw new RuntimeException("已经找到请求结构,不用继续了");
- }
- if (StrUtil.isEmpty(uniCode)) {
- log.warn("统一社会信用代码为空");
- }
- log.info("正在找:{}", uniCode);
- HttpUrl url = new HttpUrl.Builder()
- .scheme("http")
- .host(xhConfigProperty.getHost())
- .port(Integer.valueOf(xhConfigProperty.getPort()))
- .addPathSegment(address)
- .addQueryParameter(paramsValue, uniCode)
- .build();
- Request request = new Request.Builder()
- .url(url)
- .get()
- .addHeader("appKey", xhConfigProperty.getAppKey())
- .build();
- try {
- Response response = okHttpClient.newCall(request).execute();
- if (!response.isSuccessful()) {
- log.error("当前接口请求失败:{}", address);
- }
- if (ObjectUtil.isEmpty(response.body())) {
- log.error("当前接口请求数据出现问题:{}", address);
- }
- String s = response.body().string();
- BaseResponse value = objectMapper.readValue(s, BaseResponse.class);
- if (!value.getData().isEmpty()) {
- isEnd = true;
- return s;
- }
- } catch (Exception e) {
- throw new RuntimeException("当次请求失败");
- }
- return null;
- }
- public String testHttpRequest(Map map, String address) {
- if (isEnd) {
- isEnd = false;
- throw new RuntimeException("已经找到请求结构,不用继续了");
- }
- if (map.get("entityCode") == null) {
- log.warn("统一社会信用代码为空");
- }
- log.info("正在找:{}", map.get("entityCode"));
- HttpUrl url = new HttpUrl.Builder()
- .scheme("http")
- .host(xinyonghuaihuaProperty.getHost())
- .addPathSegment(address)
- .build();
- String token = redisTemplate.opsForValue().get(RedisKeyConstant.HUAIHUA_TOKEN_KEY);
- String json;
- try {
- json = objectMapper.writeValueAsString(map);
- } catch (JsonProcessingException e) {
- throw new RuntimeException(e);
- }
- RequestBody requestBody = RequestBody.create(json, MediaType.parse("application/json; charset=utf-8"));
- Request request = new Request.Builder()
- .url(url)
- .header("Content-Type", "application/json")
- .header("Token", token)
- .post(requestBody)
- .build();
- try {
- Response response = okHttpClient.newCall(request).execute();
- if (!response.isSuccessful()) {
- log.error("当前接口请求失败:{}", address);
- }
- if (ObjectUtil.isEmpty(response.body())) {
- log.error("当前接口请求数据出现问题:{}", address);
- }
- String s = response.body().string();
- BaseResponse2 value = objectMapper.readValue(s, BaseResponse2.class);
- if (value.getData().getTotalElements() > 0) {
- isEnd = true;
- return s;
- }
- } catch (Exception e) {
- throw new RuntimeException("当次请求失败");
- }
- return null;
- }
- public <T extends BaseResponse2, K extends BasePo> List<K> requestXinyonghuanhuai(Map requestBodyMap, String address,
- Class<T> entityResponseClass,
- Class<K> entityClass) {
- HttpUrl url = new HttpUrl.Builder()
- .scheme("http")
- .host(xinyonghuaihuaProperty.getHost())
- .addPathSegment(address)
- .build();
- var resList = new ArrayList<K>();
- try {
- var json = objectMapper.writeValueAsString(requestBodyMap);
- RequestBody requestBody = RequestBody.create(json, MediaType.parse("application/json; charset=utf-8"));
- while (redisTemplate.opsForValue().get(RedisKeyConstant.HUAIHUA_LOCK_KEY) != null) {
- if (retryCount.get() < MAX_RETRIES) {
- retryCount.incrementAndGet();
- System.out.println("当前锁住了");
- Thread.sleep(3000);
- } else {
- throw new RuntimeException("达到最大重试次数");
- }
- }
- String token = redisTemplate.opsForValue().get(RedisKeyConstant.HUAIHUA_TOKEN_KEY);
- Request request = new Request.Builder()
- .url(url)
- .header("Content-Type", "application/json")
- .header("Token", token)
- .post(requestBody)
- .build();
- Response response = okHttpClient.newCall(request).execute();
- if (!response.isSuccessful() || ObjectUtil.isEmpty(response.body())) {
- throw new CommonException("当前接口请求失败", 5000);
- }
- String s = response.body().string();
- T value = objectMapper.readValue(s, entityResponseClass);
- if (value.getStatus() != 0) {
- throw new CommonException("当前请求结果异常", 5000);
- }
- value.getData().getList().stream().forEach(q -> {
- K converted = objectMapper.convertValue(q, entityClass);
- converted.setUniCode(requestBodyMap.get("entityCode").toString());
- resList.add(converted);
- });
- if (value.getData().getTotalElements() > (Integer.valueOf(requestBodyMap.get("size").toString()) * (Integer.valueOf(requestBodyMap.get("page").toString())))) {
- //需要再次获取页数为+1的数据
- requestBodyMap.put("page", Integer.valueOf(requestBodyMap.get("page").toString()+1));
- List<K> list = requestXinyonghuanhuai(requestBodyMap, address, entityResponseClass, entityClass);
- resList.addAll(list);
- }
- log.info(s);
- return resList;
- }catch (InvalidFormatException e){
- if (retryCount.get() < MAX_RETRIES) {
- retryCount.incrementAndGet();
- refreshTokenSchedule.getToken();
- return requestXinyonghuanhuai(requestBodyMap, address, entityResponseClass, entityClass);
- } else {
- throw new RuntimeException("达到最大重试次数", e);
- }
- } catch (Exception e) {
- throw new RuntimeException(e);
- }finally {
- if (retryCount.get() > 0) {
- retryCount.set(0);
- }
- }
- }
- @Autowired
- private RefreshTokenSchedule refreshTokenSchedule;
- public <T extends BaseResponse3, K extends BasePo> List<K> requestXinyonghuanhuai2(Map requestBodyMap, String address,
- Class<T> entityResponseClass,
- Class<K> entityClass) {
- HttpUrl url = new HttpUrl.Builder()
- .scheme("http")
- .host(xinyonghuaihuaProperty.getHost())
- .addPathSegment(address)
- .build();
- var resList = new ArrayList<K>();
- try {
- var json = objectMapper.writeValueAsString(requestBodyMap);
- RequestBody requestBody = RequestBody.create(json, MediaType.parse("application/json; charset=utf-8"));
- while (redisTemplate.opsForValue().get(RedisKeyConstant.HUAIHUA_LOCK_KEY) != null) {
- if (retryCount.get() < MAX_RETRIES) {
- retryCount.incrementAndGet();
- System.out.println("当前锁住了");
- Thread.sleep(3000);
- } else {
- throw new RuntimeException("达到最大重试次数");
- }
- }
- String token = redisTemplate.opsForValue().get(RedisKeyConstant.HUAIHUA_TOKEN_KEY);
- Request request = new Request.Builder()
- .url(url)
- .header("Content-Type", "application/json")
- .header("Token", token)
- .post(requestBody)
- .build();
- Response response = okHttpClient.newCall(request).execute();
- if (!response.isSuccessful() || ObjectUtil.isEmpty(response.body())) {
- throw new CommonException("当前接口请求失败", 5000);
- }
- String s = response.body().string();
- T value = objectMapper.readValue(s, entityResponseClass);
- if (value.getStatus() != 0) {
- throw new CommonException("当前请求结果异常", 5000);
- }
- if (!value.getData().getItems().isEmpty()) {
- value.getData().getItems().stream().forEach(q -> {
- K converted = objectMapper.convertValue(q, entityClass);
- converted.setUniCode(requestBodyMap.get("entityCode").toString());
- resList.add(converted);
- });
- }
- if (value.getData().getTotalElements() > (Integer.valueOf(requestBodyMap.get("size").toString()) * (Integer.valueOf(requestBodyMap.get("page").toString())))) {
- //需要再次获取页数为+1的数据
- requestBodyMap.put("page", Integer.valueOf(requestBodyMap.get("page").toString()+1));
- List<K> list = requestXinyonghuanhuai2(requestBodyMap, address, entityResponseClass, entityClass);
- resList.addAll(list);
- }
- log.info(requestBodyMap.get("entityCode")+s);
- return resList;
- }catch (InvalidFormatException e){
- if (retryCount.get() < MAX_RETRIES) {
- retryCount.incrementAndGet();
- refreshTokenSchedule.getToken();
- return requestXinyonghuanhuai2(requestBodyMap, address, entityResponseClass, entityClass);
- } else {
- throw new RuntimeException("达到最大重试次数", e);
- }
- }catch (Exception e) {
- throw new RuntimeException(e);
- }finally {
- if (retryCount.get() > 0) {
- retryCount.set(0);
- }
- }
- }
- }
|