XhRequestUtil.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. package com.zksy.data.utils;
  2. import cn.hutool.core.util.ObjectUtil;
  3. import cn.hutool.core.util.StrUtil;
  4. import com.fasterxml.jackson.core.JsonProcessingException;
  5. import com.fasterxml.jackson.databind.ObjectMapper;
  6. import com.fasterxml.jackson.databind.exc.InvalidFormatException;
  7. import com.zksy.common.domain.response.BaseResponse;
  8. import com.zksy.common.domain.response.BaseResponse2;
  9. import com.zksy.common.domain.response.BaseResponse3;
  10. import com.zksy.common.exception.CommonException;
  11. import com.zksy.common.utils.BeanUtils;
  12. import com.zksy.data.config.XhConfigProperty;
  13. import com.zksy.data.config.XinyonghuaihuaProperty;
  14. import com.zksy.data.constant.RedisKeyConstant;
  15. import com.zksy.data.domain.po.BasePo;
  16. import com.zksy.data.schedule.RefreshTokenSchedule;
  17. import lombok.extern.slf4j.Slf4j;
  18. import okhttp3.*;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.data.redis.core.RedisTemplate;
  21. import org.springframework.stereotype.Component;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.concurrent.atomic.AtomicInteger;
  26. @Component
  27. @Slf4j
  28. public class XhRequestUtil {
  29. @Autowired
  30. private XhConfigProperty xhConfigProperty;
  31. @Autowired
  32. private XinyonghuaihuaProperty xinyonghuaihuaProperty;
  33. @Autowired
  34. private OkHttpClient okHttpClient;
  35. @Autowired
  36. private ObjectMapper objectMapper;
  37. @Autowired
  38. private RedisTemplate<String, String> redisTemplate;
  39. private static final int MAX_RETRIES = 3;
  40. private static AtomicInteger retryCount = new AtomicInteger(0);
  41. public <T extends BaseResponse, K extends BasePo> List<K> httpRequestByUniCode(String uniCode, String address,
  42. Class<T> responseClass,
  43. Class<K> entityClass) {
  44. if(redisTemplate.opsForValue().get(address) !=null){
  45. throw new CommonException("停止并行流",5001);
  46. }
  47. // log.info("正在同步:{}下的:{}", address, uniCode);
  48. ArrayList<K> resList = new ArrayList<>();
  49. if (StrUtil.isEmpty(uniCode)) {
  50. log.warn("统一社会信用代码为空");
  51. }
  52. HttpUrl url = new HttpUrl.Builder()
  53. .scheme("http")
  54. .host(xhConfigProperty.getHost())
  55. .port(Integer.valueOf(xhConfigProperty.getPort()))
  56. .addPathSegment(address)
  57. .addQueryParameter("UNISCID", uniCode)
  58. .build();
  59. Request request = new Request.Builder()
  60. .url(url)
  61. .get()
  62. .addHeader("appKey", xhConfigProperty.getAppKey())
  63. .build();
  64. try {
  65. Response response = okHttpClient.newCall(request).execute();
  66. if (!response.isSuccessful()) {
  67. log.error("当前接口请求失败:{}", address);
  68. }
  69. if (ObjectUtil.isEmpty(response.body())) {
  70. log.error("当前接口请求数据出现问题:{}", address);
  71. }
  72. String s = response.body().string();
  73. T value = objectMapper.readValue(s, responseClass);
  74. if (value.getCode() != 200) {
  75. log.error("当前请求结果异常:{}", value.getMessage());
  76. } else {
  77. if (value.getData().isEmpty()) {
  78. return null;
  79. }
  80. for (int i = 0; i < value.getData().size(); i++) {
  81. K e = BeanUtils.copyBean(value.getData().get(i), entityClass);
  82. e.setUniCode(uniCode);
  83. resList.add(e);
  84. }
  85. }
  86. return resList;
  87. } catch (Exception e) {
  88. log.error("此次数据同步出现异常,接口是:{}", address);
  89. throw new RuntimeException(e);
  90. }
  91. }
  92. public <T extends BaseResponse, K extends BasePo> List<K> httpRequestByUniCode(String uniCode, String address,
  93. Class<T> responseClass,
  94. Class<K> entityClass,
  95. String key) {
  96. if(redisTemplate.opsForValue().get(address) !=null){
  97. throw new CommonException("停止并行流",5001);
  98. }
  99. // log.info("正在同步:{}下的:{}", address, uniCode);
  100. ArrayList<K> resList = new ArrayList<>();
  101. if (StrUtil.isEmpty(uniCode)) {
  102. log.warn("统一社会信用代码为空");
  103. }
  104. HttpUrl url = new HttpUrl.Builder()
  105. .scheme("http")
  106. .host(xhConfigProperty.getHost())
  107. .port(Integer.valueOf(xhConfigProperty.getPort()))
  108. .addPathSegment(address)
  109. .addQueryParameter(key, uniCode)
  110. .build();
  111. Request request = new Request.Builder()
  112. .url(url)
  113. .get()
  114. .addHeader("appKey", xhConfigProperty.getAppKey())
  115. .build();
  116. try {
  117. Response response = okHttpClient.newCall(request).execute();
  118. if (!response.isSuccessful()) {
  119. log.error("当前接口请求失败:{}", address);
  120. }
  121. if (ObjectUtil.isEmpty(response.body())) {
  122. log.error("当前接口请求数据出现问题:{}", address);
  123. }
  124. String s = response.body().string();
  125. T value = objectMapper.readValue(s, responseClass);
  126. if (value.getCode() != 200) {
  127. log.error("当前请求结果异常:{}", value.getMessage());
  128. throw new CommonException("请求异常",5000);
  129. } else {
  130. if (value.getData().isEmpty()) {
  131. return null;
  132. }
  133. for (int i = 0; i < value.getData().size(); i++) {
  134. K e = BeanUtils.copyBean(value.getData().get(i), entityClass);
  135. e.setUniCode(uniCode);
  136. resList.add(e);
  137. }
  138. }
  139. return resList;
  140. } catch (Exception e) {
  141. log.error("此次数据同步出现异常,接口是:{}", address);
  142. throw new RuntimeException(e);
  143. }
  144. }
  145. private Boolean isEnd = false;
  146. public String testHttpRequest(String uniCode, String address, String paramsValue) {
  147. if (isEnd) {
  148. isEnd = false;
  149. throw new RuntimeException("已经找到请求结构,不用继续了");
  150. }
  151. if (StrUtil.isEmpty(uniCode)) {
  152. log.warn("统一社会信用代码为空");
  153. }
  154. log.info("正在找:{}", uniCode);
  155. HttpUrl url = new HttpUrl.Builder()
  156. .scheme("http")
  157. .host(xhConfigProperty.getHost())
  158. .port(Integer.valueOf(xhConfigProperty.getPort()))
  159. .addPathSegment(address)
  160. .addQueryParameter(paramsValue, uniCode)
  161. .build();
  162. Request request = new Request.Builder()
  163. .url(url)
  164. .get()
  165. .addHeader("appKey", xhConfigProperty.getAppKey())
  166. .build();
  167. try {
  168. Response response = okHttpClient.newCall(request).execute();
  169. if (!response.isSuccessful()) {
  170. log.error("当前接口请求失败:{}", address);
  171. }
  172. if (ObjectUtil.isEmpty(response.body())) {
  173. log.error("当前接口请求数据出现问题:{}", address);
  174. }
  175. String s = response.body().string();
  176. BaseResponse value = objectMapper.readValue(s, BaseResponse.class);
  177. if (!value.getData().isEmpty()) {
  178. isEnd = true;
  179. return s;
  180. }
  181. } catch (Exception e) {
  182. throw new RuntimeException("当次请求失败");
  183. }
  184. return null;
  185. }
  186. public String testHttpRequest(Map map, String address) {
  187. if (isEnd) {
  188. isEnd = false;
  189. throw new RuntimeException("已经找到请求结构,不用继续了");
  190. }
  191. if (map.get("entityCode") == null) {
  192. log.warn("统一社会信用代码为空");
  193. }
  194. log.info("正在找:{}", map.get("entityCode"));
  195. HttpUrl url = new HttpUrl.Builder()
  196. .scheme("http")
  197. .host(xinyonghuaihuaProperty.getHost())
  198. .addPathSegment(address)
  199. .build();
  200. String token = redisTemplate.opsForValue().get(RedisKeyConstant.HUAIHUA_TOKEN_KEY);
  201. String json;
  202. try {
  203. json = objectMapper.writeValueAsString(map);
  204. } catch (JsonProcessingException e) {
  205. throw new RuntimeException(e);
  206. }
  207. RequestBody requestBody = RequestBody.create(json, MediaType.parse("application/json; charset=utf-8"));
  208. Request request = new Request.Builder()
  209. .url(url)
  210. .header("Content-Type", "application/json")
  211. .header("Token", token)
  212. .post(requestBody)
  213. .build();
  214. try {
  215. Response response = okHttpClient.newCall(request).execute();
  216. if (!response.isSuccessful()) {
  217. log.error("当前接口请求失败:{}", address);
  218. }
  219. if (ObjectUtil.isEmpty(response.body())) {
  220. log.error("当前接口请求数据出现问题:{}", address);
  221. }
  222. String s = response.body().string();
  223. BaseResponse2 value = objectMapper.readValue(s, BaseResponse2.class);
  224. if (value.getData().getTotalElements() > 0) {
  225. isEnd = true;
  226. return s;
  227. }
  228. } catch (Exception e) {
  229. throw new RuntimeException("当次请求失败");
  230. }
  231. return null;
  232. }
  233. public <T extends BaseResponse2, K extends BasePo> List<K> requestXinyonghuanhuai(Map requestBodyMap, String address,
  234. Class<T> entityResponseClass,
  235. Class<K> entityClass) {
  236. HttpUrl url = new HttpUrl.Builder()
  237. .scheme("http")
  238. .host(xinyonghuaihuaProperty.getHost())
  239. .addPathSegment(address)
  240. .build();
  241. var resList = new ArrayList<K>();
  242. try {
  243. var json = objectMapper.writeValueAsString(requestBodyMap);
  244. RequestBody requestBody = RequestBody.create(json, MediaType.parse("application/json; charset=utf-8"));
  245. while (redisTemplate.opsForValue().get(RedisKeyConstant.HUAIHUA_LOCK_KEY) != null) {
  246. if (retryCount.get() < MAX_RETRIES) {
  247. retryCount.incrementAndGet();
  248. System.out.println("当前锁住了");
  249. Thread.sleep(3000);
  250. } else {
  251. throw new RuntimeException("达到最大重试次数");
  252. }
  253. }
  254. String token = redisTemplate.opsForValue().get(RedisKeyConstant.HUAIHUA_TOKEN_KEY);
  255. Request request = new Request.Builder()
  256. .url(url)
  257. .header("Content-Type", "application/json")
  258. .header("Token", token)
  259. .post(requestBody)
  260. .build();
  261. Response response = okHttpClient.newCall(request).execute();
  262. if (!response.isSuccessful() || ObjectUtil.isEmpty(response.body())) {
  263. throw new CommonException("当前接口请求失败", 5000);
  264. }
  265. String s = response.body().string();
  266. T value = objectMapper.readValue(s, entityResponseClass);
  267. if (value.getStatus() != 0) {
  268. throw new CommonException("当前请求结果异常", 5000);
  269. }
  270. value.getData().getList().stream().forEach(q -> {
  271. K converted = objectMapper.convertValue(q, entityClass);
  272. converted.setUniCode(requestBodyMap.get("entityCode").toString());
  273. resList.add(converted);
  274. });
  275. if (value.getData().getTotalElements() > (Integer.valueOf(requestBodyMap.get("size").toString()) * (Integer.valueOf(requestBodyMap.get("page").toString())))) {
  276. //需要再次获取页数为+1的数据
  277. requestBodyMap.put("page", Integer.valueOf(requestBodyMap.get("page").toString()+1));
  278. List<K> list = requestXinyonghuanhuai(requestBodyMap, address, entityResponseClass, entityClass);
  279. resList.addAll(list);
  280. }
  281. log.info(s);
  282. return resList;
  283. }catch (InvalidFormatException e){
  284. if (retryCount.get() < MAX_RETRIES) {
  285. retryCount.incrementAndGet();
  286. refreshTokenSchedule.getToken();
  287. return requestXinyonghuanhuai(requestBodyMap, address, entityResponseClass, entityClass);
  288. } else {
  289. throw new RuntimeException("达到最大重试次数", e);
  290. }
  291. } catch (Exception e) {
  292. throw new RuntimeException(e);
  293. }finally {
  294. if (retryCount.get() > 0) {
  295. retryCount.set(0);
  296. }
  297. }
  298. }
  299. @Autowired
  300. private RefreshTokenSchedule refreshTokenSchedule;
  301. public <T extends BaseResponse3, K extends BasePo> List<K> requestXinyonghuanhuai2(Map requestBodyMap, String address,
  302. Class<T> entityResponseClass,
  303. Class<K> entityClass) {
  304. HttpUrl url = new HttpUrl.Builder()
  305. .scheme("http")
  306. .host(xinyonghuaihuaProperty.getHost())
  307. .addPathSegment(address)
  308. .build();
  309. var resList = new ArrayList<K>();
  310. try {
  311. var json = objectMapper.writeValueAsString(requestBodyMap);
  312. RequestBody requestBody = RequestBody.create(json, MediaType.parse("application/json; charset=utf-8"));
  313. while (redisTemplate.opsForValue().get(RedisKeyConstant.HUAIHUA_LOCK_KEY) != null) {
  314. if (retryCount.get() < MAX_RETRIES) {
  315. retryCount.incrementAndGet();
  316. System.out.println("当前锁住了");
  317. Thread.sleep(3000);
  318. } else {
  319. throw new RuntimeException("达到最大重试次数");
  320. }
  321. }
  322. String token = redisTemplate.opsForValue().get(RedisKeyConstant.HUAIHUA_TOKEN_KEY);
  323. Request request = new Request.Builder()
  324. .url(url)
  325. .header("Content-Type", "application/json")
  326. .header("Token", token)
  327. .post(requestBody)
  328. .build();
  329. Response response = okHttpClient.newCall(request).execute();
  330. if (!response.isSuccessful() || ObjectUtil.isEmpty(response.body())) {
  331. throw new CommonException("当前接口请求失败", 5000);
  332. }
  333. String s = response.body().string();
  334. T value = objectMapper.readValue(s, entityResponseClass);
  335. if (value.getStatus() != 0) {
  336. throw new CommonException("当前请求结果异常", 5000);
  337. }
  338. if (!value.getData().getItems().isEmpty()) {
  339. value.getData().getItems().stream().forEach(q -> {
  340. K converted = objectMapper.convertValue(q, entityClass);
  341. converted.setUniCode(requestBodyMap.get("entityCode").toString());
  342. resList.add(converted);
  343. });
  344. }
  345. if (value.getData().getTotalElements() > (Integer.valueOf(requestBodyMap.get("size").toString()) * (Integer.valueOf(requestBodyMap.get("page").toString())))) {
  346. //需要再次获取页数为+1的数据
  347. requestBodyMap.put("page", Integer.valueOf(requestBodyMap.get("page").toString()+1));
  348. List<K> list = requestXinyonghuanhuai2(requestBodyMap, address, entityResponseClass, entityClass);
  349. resList.addAll(list);
  350. }
  351. log.info(requestBodyMap.get("entityCode")+s);
  352. return resList;
  353. }catch (InvalidFormatException e){
  354. if (retryCount.get() < MAX_RETRIES) {
  355. retryCount.incrementAndGet();
  356. refreshTokenSchedule.getToken();
  357. return requestXinyonghuanhuai2(requestBodyMap, address, entityResponseClass, entityClass);
  358. } else {
  359. throw new RuntimeException("达到最大重试次数", e);
  360. }
  361. }catch (Exception e) {
  362. throw new RuntimeException(e);
  363. }finally {
  364. if (retryCount.get() > 0) {
  365. retryCount.set(0);
  366. }
  367. }
  368. }
  369. }