|
@@ -2,15 +2,19 @@ package com.zksy.data.utils;
|
|
|
|
|
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
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.BaseResponse;
|
|
|
import com.zksy.common.domain.response.BaseResponse2;
|
|
import com.zksy.common.domain.response.BaseResponse2;
|
|
|
|
|
+import com.zksy.common.domain.response.BaseResponse3;
|
|
|
import com.zksy.common.exception.CommonException;
|
|
import com.zksy.common.exception.CommonException;
|
|
|
import com.zksy.common.utils.BeanUtils;
|
|
import com.zksy.common.utils.BeanUtils;
|
|
|
import com.zksy.data.config.XhConfigProperty;
|
|
import com.zksy.data.config.XhConfigProperty;
|
|
|
import com.zksy.data.config.XinyonghuaihuaProperty;
|
|
import com.zksy.data.config.XinyonghuaihuaProperty;
|
|
|
import com.zksy.data.constant.RedisKeyConstant;
|
|
import com.zksy.data.constant.RedisKeyConstant;
|
|
|
import com.zksy.data.domain.po.BasePo;
|
|
import com.zksy.data.domain.po.BasePo;
|
|
|
|
|
+import com.zksy.data.schedule.RefreshTokenSchedule;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import okhttp3.*;
|
|
import okhttp3.*;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -33,12 +37,12 @@ public class XhRequestUtil {
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private ObjectMapper objectMapper;
|
|
private ObjectMapper objectMapper;
|
|
|
@Autowired
|
|
@Autowired
|
|
|
- private RedisTemplate<String,String> redisTemplate;
|
|
|
|
|
|
|
+ private RedisTemplate<String, String> redisTemplate;
|
|
|
|
|
|
|
|
- public <T extends BaseResponse,K extends BasePo> List<K> httpRequestByUniCode(String uniCode, String address,
|
|
|
|
|
- Class<T> responseClass,
|
|
|
|
|
- Class<K> entityClass) {
|
|
|
|
|
- log.info("正在同步:{}下的:{}",address,uniCode);
|
|
|
|
|
|
|
+ public <T extends BaseResponse, K extends BasePo> List<K> httpRequestByUniCode(String uniCode, String address,
|
|
|
|
|
+ Class<T> responseClass,
|
|
|
|
|
+ Class<K> entityClass) {
|
|
|
|
|
+ log.info("正在同步:{}下的:{}", address, uniCode);
|
|
|
ArrayList<K> resList = new ArrayList<>();
|
|
ArrayList<K> resList = new ArrayList<>();
|
|
|
if (StrUtil.isEmpty(uniCode)) {
|
|
if (StrUtil.isEmpty(uniCode)) {
|
|
|
log.warn("统一社会信用代码为空");
|
|
log.warn("统一社会信用代码为空");
|
|
@@ -83,8 +87,59 @@ public class XhRequestUtil {
|
|
|
throw new RuntimeException(e);
|
|
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) {
|
|
|
|
|
+ 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());
|
|
|
|
|
+ } 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;
|
|
private Boolean isEnd = false;
|
|
|
- public String testHttpRequest(String uniCode,String address,String paramsValue) {
|
|
|
|
|
|
|
+
|
|
|
|
|
+ public String testHttpRequest(String uniCode, String address, String paramsValue) {
|
|
|
if (isEnd) {
|
|
if (isEnd) {
|
|
|
isEnd = false;
|
|
isEnd = false;
|
|
|
throw new RuntimeException("已经找到请求结构,不用继续了");
|
|
throw new RuntimeException("已经找到请求结构,不用继续了");
|
|
@@ -92,7 +147,7 @@ public class XhRequestUtil {
|
|
|
if (StrUtil.isEmpty(uniCode)) {
|
|
if (StrUtil.isEmpty(uniCode)) {
|
|
|
log.warn("统一社会信用代码为空");
|
|
log.warn("统一社会信用代码为空");
|
|
|
}
|
|
}
|
|
|
- log.info("正在找:{}",uniCode);
|
|
|
|
|
|
|
+ log.info("正在找:{}", uniCode);
|
|
|
HttpUrl url = new HttpUrl.Builder()
|
|
HttpUrl url = new HttpUrl.Builder()
|
|
|
.scheme("http")
|
|
.scheme("http")
|
|
|
.host(xhConfigProperty.getHost())
|
|
.host(xhConfigProperty.getHost())
|
|
@@ -116,7 +171,7 @@ public class XhRequestUtil {
|
|
|
}
|
|
}
|
|
|
String s = response.body().string();
|
|
String s = response.body().string();
|
|
|
BaseResponse value = objectMapper.readValue(s, BaseResponse.class);
|
|
BaseResponse value = objectMapper.readValue(s, BaseResponse.class);
|
|
|
- if(!value.getData().isEmpty()){
|
|
|
|
|
|
|
+ if (!value.getData().isEmpty()) {
|
|
|
isEnd = true;
|
|
isEnd = true;
|
|
|
return s;
|
|
return s;
|
|
|
}
|
|
}
|
|
@@ -126,72 +181,164 @@ public class XhRequestUtil {
|
|
|
return null;
|
|
return null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public <T extends BaseResponse2> void requestXinyonghuanhuai(Map requestBodyMap, String address,
|
|
|
|
|
- Class<T> entityResponseClass){
|
|
|
|
|
- getToken();
|
|
|
|
|
|
|
+ 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()
|
|
HttpUrl url = new HttpUrl.Builder()
|
|
|
.scheme("http")
|
|
.scheme("http")
|
|
|
.host(xinyonghuaihuaProperty.getHost())
|
|
.host(xinyonghuaihuaProperty.getHost())
|
|
|
.addPathSegment(address)
|
|
.addPathSegment(address)
|
|
|
.build();
|
|
.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 {
|
|
try {
|
|
|
var json = objectMapper.writeValueAsString(requestBodyMap);
|
|
var json = objectMapper.writeValueAsString(requestBodyMap);
|
|
|
RequestBody requestBody = RequestBody.create(json, MediaType.parse("application/json; charset=utf-8"));
|
|
RequestBody requestBody = RequestBody.create(json, MediaType.parse("application/json; charset=utf-8"));
|
|
|
|
|
+ while (redisTemplate.opsForValue().get(RedisKeyConstant.HUAIHUA_LOCK_KEY) != null) {
|
|
|
|
|
+ System.out.println("当前锁住了");
|
|
|
|
|
+ Thread.sleep(3000);
|
|
|
|
|
+ }
|
|
|
|
|
+ String token = redisTemplate.opsForValue().get(RedisKeyConstant.HUAIHUA_TOKEN_KEY);
|
|
|
Request request = new Request.Builder()
|
|
Request request = new Request.Builder()
|
|
|
.url(url)
|
|
.url(url)
|
|
|
.header("Content-Type", "application/json")
|
|
.header("Content-Type", "application/json")
|
|
|
- .header("Token",redisTemplate.opsForValue().get(RedisKeyConstant.HUAIHUA_TOKEN_KEY))
|
|
|
|
|
|
|
+ .header("Token", token)
|
|
|
.post(requestBody)
|
|
.post(requestBody)
|
|
|
.build();
|
|
.build();
|
|
|
Response response = okHttpClient.newCall(request).execute();
|
|
Response response = okHttpClient.newCall(request).execute();
|
|
|
if (!response.isSuccessful() || ObjectUtil.isEmpty(response.body())) {
|
|
if (!response.isSuccessful() || ObjectUtil.isEmpty(response.body())) {
|
|
|
- throw new CommonException("当前接口请求失败",5000);
|
|
|
|
|
|
|
+ throw new CommonException("当前接口请求失败", 5000);
|
|
|
}
|
|
}
|
|
|
String s = response.body().string();
|
|
String s = response.body().string();
|
|
|
T value = objectMapper.readValue(s, entityResponseClass);
|
|
T value = objectMapper.readValue(s, entityResponseClass);
|
|
|
-
|
|
|
|
|
- if(value.getStatus() != 0){
|
|
|
|
|
- throw new CommonException("当前请求结果异常",5000);
|
|
|
|
|
|
|
+ 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);
|
|
|
}
|
|
}
|
|
|
- System.out.println(value);
|
|
|
|
|
log.info(s);
|
|
log.info(s);
|
|
|
|
|
+ return resList;
|
|
|
|
|
+ }catch (InvalidFormatException e){
|
|
|
|
|
+ refreshTokenSchedule.getToken();
|
|
|
|
|
+ this.requestXinyonghuanhuai(requestBodyMap, address, entityResponseClass, entityClass);
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
throw new RuntimeException(e);
|
|
throw new RuntimeException(e);
|
|
|
}
|
|
}
|
|
|
|
|
+ return resList;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- private void getToken() {
|
|
|
|
|
|
|
+ @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()
|
|
HttpUrl url = new HttpUrl.Builder()
|
|
|
.scheme("http")
|
|
.scheme("http")
|
|
|
.host(xinyonghuaihuaProperty.getHost())
|
|
.host(xinyonghuaihuaProperty.getHost())
|
|
|
- .addPathSegment("interface/v1/auth/getToken")
|
|
|
|
|
- .addQueryParameter("refreshToken", xinyonghuaihuaProperty.getRefreshToken())
|
|
|
|
|
- .build();
|
|
|
|
|
- Request request = new Request.Builder()
|
|
|
|
|
- .url(url)
|
|
|
|
|
- .get()
|
|
|
|
|
|
|
+ .addPathSegment(address)
|
|
|
.build();
|
|
.build();
|
|
|
|
|
+ var resList = new ArrayList<K>();
|
|
|
try {
|
|
try {
|
|
|
- Response response = okHttpClient.newCall(request).execute();
|
|
|
|
|
- if (!response.isSuccessful()) {
|
|
|
|
|
- throw new CommonException("刷新token的接口请求失败",5000);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ 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) {
|
|
|
|
|
+ System.out.println("当前锁住了");
|
|
|
|
|
+ Thread.sleep(3000);
|
|
|
}
|
|
}
|
|
|
- if (ObjectUtil.isEmpty(response.body())) {
|
|
|
|
|
- throw new CommonException("刷新token的接口出现问题",5000);
|
|
|
|
|
|
|
+ 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();
|
|
String s = response.body().string();
|
|
|
- Map map = objectMapper.readValue(s, Map.class);
|
|
|
|
|
- if((Integer) map.get("status") != 0){
|
|
|
|
|
- throw new CommonException("刷新token的接口返回异常",5000);
|
|
|
|
|
|
|
+ T value = objectMapper.readValue(s, entityResponseClass);
|
|
|
|
|
+ if (value.getStatus() != 0) {
|
|
|
|
|
+ throw new CommonException("当前请求结果异常", 5000);
|
|
|
}
|
|
}
|
|
|
- String token = (String) map.get("data");
|
|
|
|
|
- if(StrUtil.isEmpty(token)){
|
|
|
|
|
- throw new CommonException("刷新token的接口返回异常",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);
|
|
|
|
|
+ });
|
|
|
}
|
|
}
|
|
|
-// return token;
|
|
|
|
|
- redisTemplate.opsForValue().set(RedisKeyConstant.HUAIHUA_TOKEN_KEY,token);
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
|
|
+ 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){
|
|
|
|
|
+ refreshTokenSchedule.getToken();
|
|
|
|
|
+ this.requestXinyonghuanhuai2(requestBodyMap, address, entityResponseClass, entityClass);
|
|
|
|
|
+ }catch (Exception e) {
|
|
|
throw new RuntimeException(e);
|
|
throw new RuntimeException(e);
|
|
|
}
|
|
}
|
|
|
|
|
+ return resList;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|