|
@@ -1,8 +1,13 @@
|
|
|
package com.zksy.data.utils;
|
|
package com.zksy.data.utils;
|
|
|
|
|
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
+import com.zksy.common.domain.response.BaseResponse;
|
|
|
|
|
+import com.zksy.common.utils.BeanUtils;
|
|
|
import com.zksy.data.config.XhConfigProperty;
|
|
import com.zksy.data.config.XhConfigProperty;
|
|
|
|
|
+import com.zksy.data.domain.po.BasePo;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import okhttp3.HttpUrl;
|
|
import okhttp3.HttpUrl;
|
|
|
import okhttp3.OkHttpClient;
|
|
import okhttp3.OkHttpClient;
|
|
@@ -11,8 +16,6 @@ import okhttp3.Response;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
-import java.io.IOException;
|
|
|
|
|
-
|
|
|
|
|
@Component
|
|
@Component
|
|
|
@Slf4j
|
|
@Slf4j
|
|
|
public class XhRequestUtil {
|
|
public class XhRequestUtil {
|
|
@@ -22,7 +25,14 @@ public class XhRequestUtil {
|
|
|
private OkHttpClient okHttpClient;
|
|
private OkHttpClient okHttpClient;
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private ObjectMapper objectMapper;
|
|
private ObjectMapper objectMapper;
|
|
|
- public String httpRequestByUniCode(String uniCode,String address){
|
|
|
|
|
|
|
+
|
|
|
|
|
+ public <T extends BaseResponse,K extends BasePo> String httpRequestByUniCode(String uniCode, String address,
|
|
|
|
|
+ Class<T> responseClass,
|
|
|
|
|
+ Class<K> entityClass,
|
|
|
|
|
+ BaseMapper entityMapper) {
|
|
|
|
|
+ if (StrUtil.isEmpty(uniCode)) {
|
|
|
|
|
+ log.warn("统一社会信用代码为空");
|
|
|
|
|
+ }
|
|
|
HttpUrl url = new HttpUrl.Builder()
|
|
HttpUrl url = new HttpUrl.Builder()
|
|
|
.scheme("http")
|
|
.scheme("http")
|
|
|
.host(xhConfigProperty.getHost())
|
|
.host(xhConfigProperty.getHost())
|
|
@@ -38,17 +48,70 @@ public class XhRequestUtil {
|
|
|
try {
|
|
try {
|
|
|
Response response = okHttpClient.newCall(request).execute();
|
|
Response response = okHttpClient.newCall(request).execute();
|
|
|
if (!response.isSuccessful()) {
|
|
if (!response.isSuccessful()) {
|
|
|
- log.error("当前接口请求失败:{}",address);
|
|
|
|
|
|
|
+ log.error("当前接口请求失败:{}", address);
|
|
|
}
|
|
}
|
|
|
- if (ObjectUtil.isEmpty(response.body())){
|
|
|
|
|
- log.error("当前接口请求数据出现问题:{}",address);
|
|
|
|
|
|
|
+ if (ObjectUtil.isEmpty(response.body())) {
|
|
|
|
|
+ log.error("当前接口请求数据出现问题:{}", address);
|
|
|
}
|
|
}
|
|
|
String s = response.body().string();
|
|
String s = response.body().string();
|
|
|
-// objectMapper.readValue(s,String.class);
|
|
|
|
|
- log.info("当前的请求结果:{}",s);
|
|
|
|
|
|
|
+ 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);
|
|
|
|
|
+ entityMapper.insert(e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
return s;
|
|
return s;
|
|
|
- } catch (IOException e) {
|
|
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("此次数据同步出现异常,接口是:{}", address);
|
|
|
throw new RuntimeException(e);
|
|
throw new RuntimeException(e);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+ private Boolean isEnd = false;
|
|
|
|
|
+ public String testHttpRequest(String uniCode,String address) {
|
|
|
|
|
+ if (isEnd) {
|
|
|
|
|
+ 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("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();
|
|
|
|
|
+ BaseResponse value = objectMapper.readValue(s, BaseResponse.class);
|
|
|
|
|
+ if(value.getData().size()>0){
|
|
|
|
|
+ isEnd = true;
|
|
|
|
|
+ return s;
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ throw new RuntimeException("当次请求失败");
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|