XinLuDataProcessor.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package com.tiangua.star.service.impl;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.tiangua.star.model.UserStarFollowCallbackParam;
  5. import com.tiangua.star.model.XinLuUserStarCallbackParam;
  6. import com.tiangua.star.service.XinLuService;
  7. import com.tiangua.star.util.HttpClientThreeUtil;
  8. import lombok.extern.slf4j.Slf4j;
  9. import org.apache.commons.collections4.CollectionUtils;
  10. import org.apache.http.client.methods.CloseableHttpResponse;
  11. import org.apache.http.client.methods.HttpGet;
  12. import org.apache.http.client.utils.URIBuilder;
  13. import org.apache.http.impl.client.CloseableHttpClient;
  14. import org.apache.http.impl.client.HttpClients;
  15. import org.apache.http.util.EntityUtils;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.beans.factory.annotation.Qualifier;
  18. import org.springframework.beans.factory.annotation.Value;
  19. import org.springframework.boot.SpringApplication;
  20. import org.springframework.context.ConfigurableApplicationContext;
  21. import org.springframework.core.env.Environment;
  22. import org.springframework.stereotype.Service;
  23. import javax.annotation.PostConstruct;
  24. import java.io.IOException;
  25. import java.net.URISyntaxException;
  26. import java.nio.charset.StandardCharsets;
  27. import java.time.LocalDate;
  28. import java.time.format.DateTimeFormatter;
  29. import java.util.*;
  30. import java.util.concurrent.Executor;
  31. import java.util.stream.Collectors;
  32. @Service
  33. @Slf4j
  34. public class XinLuDataProcessor implements XinLuService {
  35. // HTTP请求工具
  36. private static final CloseableHttpClient httpClient = HttpClients.createDefault();
  37. @Value("${xinlu.callback.url}")
  38. private String url;
  39. @Autowired
  40. @Qualifier("starExecutor")
  41. private Executor executor;
  42. @PostConstruct
  43. public void init() {
  44. log.info("xinlu init{}",url);
  45. }
  46. /**
  47. * 处理机构数据入口
  48. * @param jsonObjects 包含productBizId和busiId的配置列表
  49. */
  50. @Override
  51. public void processInstitutionData(List<JSONObject> jsonObjects) {
  52. jsonObjects.forEach(config -> executor.execute(() -> {
  53. Integer institutionId = config.getInteger("institutionId");
  54. String busiId = config.getString("busiId");
  55. // 1. 根据institutionId获取对应URL
  56. String targetUrl = config.getString("starLevelBackInter");
  57. if (targetUrl == null) {
  58. log.warn("未找到institutionId={}对应的URL", institutionId);
  59. return;
  60. }
  61. // 星级数据拉取周期从一天调整为一周
  62. for (int i = 6; i >= 0; i--) {
  63. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  64. String syncDate = LocalDate.now().minusDays(i).format(formatter);
  65. // 2. 发送GET请求
  66. String responseJson = sendGetRequest(targetUrl,1, syncDate, syncDate);
  67. if (responseJson == null) { continue; }
  68. // 3. 解析并映射数据
  69. List<XinLuUserStarCallbackParam> mappedData = parseAndMapData(responseJson, institutionId, busiId);
  70. log.info("鑫路回传诚易融数据{},{},同步时间:{}", institutionId, mappedData, syncDate);
  71. if (CollectionUtils.isEmpty(mappedData)) {
  72. log.info("鑫路没有回传数据{},同步时间:{}", institutionId, syncDate);
  73. continue;
  74. }
  75. // 4. 后续处理(存储/推送等)
  76. handleMappedData(mappedData);
  77. }
  78. }));
  79. }
  80. private String sendGetRequest(String originalUrl,int pageNum, String beginTime, String endTime){
  81. HttpGet httpGet = new HttpGet(buildUrl(originalUrl,pageNum,beginTime,endTime));
  82. try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
  83. int statusCode = response.getStatusLine().getStatusCode();
  84. if (statusCode == 200) {
  85. return EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
  86. } else {
  87. log.warn("请求失败,状态码:%d%n", statusCode);
  88. return null;
  89. }
  90. } catch (IOException e) {
  91. log.warn("HTTP请求异常,URL: %s%n", originalUrl);
  92. }
  93. return null;
  94. }
  95. private String buildUrl(String originalUrl, int pageNum, String beginTime, String endTime) {
  96. try {
  97. URIBuilder uriBuilder = new URIBuilder(originalUrl.trim());
  98. List<org.apache.http.NameValuePair> params = new ArrayList<>();
  99. for (org.apache.http.NameValuePair param : uriBuilder.getQueryParams()) {
  100. String name = param.getName();
  101. if (!"pageNum".equals(name) && !"pageSize".equals(name) && !"beginTime".equals(name) && !"endTime".equals(name)) {
  102. params.add(param);
  103. }
  104. }
  105. params.add(new org.apache.http.message.BasicNameValuePair("pageNum", String.valueOf(pageNum)));
  106. params.add(new org.apache.http.message.BasicNameValuePair("pageSize", "100"));
  107. params.add(new org.apache.http.message.BasicNameValuePair("beginTime", beginTime));
  108. params.add(new org.apache.http.message.BasicNameValuePair("endTime", endTime));
  109. uriBuilder.setParameters(params);
  110. return uriBuilder.build().toString();
  111. } catch (URISyntaxException e) {
  112. log.error("buildUrl error",e);
  113. }
  114. return null;
  115. }
  116. // public List<Map<String, Object>> fetchAllData(String originalUrl, String beginTime, String endTime) {
  117. // List<Map<String, Object>> allRows = new ArrayList<>();
  118. // int pageNum = 1;
  119. // int total = -1;
  120. // int count = 0;
  121. // while (count < 10) {
  122. // try {
  123. // String url = buildUrl(originalUrl, pageNum, beginTime, endTime);
  124. // ApiResponse response = sendGetRequest(url);
  125. // if (response == null || response.getCode() != 200) {
  126. // System.err.printf("请求失败,pageNum: %d%n", pageNum);
  127. // break;
  128. // }
  129. // if (total == -1) {
  130. // total = response.getTotal();
  131. // if (total == 0) break;
  132. // }
  133. // if (response.getRows() != null) {
  134. // allRows.addAll(response.getRows());
  135. // }
  136. // int currentSize = response.getRows() == null ? 0 : response.getRows().size();
  137. // if (currentSize < 100 || allRows.size() >= total) {
  138. // break;
  139. // }
  140. // pageNum++;
  141. // count++;
  142. // } catch (URISyntaxException | IOException e) {
  143. // System.err.println("请求异常: " + e.getMessage());
  144. // break;
  145. // }
  146. // }
  147. // return allRows;
  148. // }
  149. private List<XinLuUserStarCallbackParam> parseAndMapData(String json, Integer productBizId, String busiId) {
  150. JSONObject responseData = JSON.parseObject(json);
  151. // 验证响应结构
  152. if (responseData.getIntValue("code") != 200 || !responseData.containsKey("rows")) {
  153. log.warn("无效的响应数据");
  154. return Collections.emptyList();
  155. }
  156. // 解析rows数组
  157. return responseData.getJSONArray("rows").stream()
  158. .filter(obj -> {
  159. JSONObject item = (JSONObject) obj;
  160. return item.containsKey("importantDegree")
  161. && !Objects.isNull(item.getFloat("importantDegree"));
  162. })
  163. .map(obj -> {
  164. JSONObject item = (JSONObject) obj;
  165. XinLuUserStarCallbackParam param = new XinLuUserStarCallbackParam();
  166. // 基础字段映射
  167. param.setCustomerName(item.getString("name"));
  168. param.setMaskPhone(item.getString("phone"));
  169. param.setFollowTime(item.getString("customerCreateTime"));
  170. param.setStarLevel(item.getFloat("importantDegree"));
  171. param.setImportantDegreeStr(item.getString("importantDegreeStr"));
  172. // 从上游传入的字段
  173. param.setProductBizId(productBizId);
  174. param.setBusiId(busiId);
  175. // 处理跟进记录列表
  176. if (item.containsKey("cuFollowList") && CollectionUtils.isNotEmpty(item.getJSONArray("cuFollowList"))) {
  177. List<UserStarFollowCallbackParam> followParams = item.getJSONArray("cuFollowList")
  178. .stream()
  179. .map(followObj -> {
  180. JSONObject follow = (JSONObject) followObj;
  181. UserStarFollowCallbackParam followParam = new UserStarFollowCallbackParam();
  182. followParam.setFollowContent(follow.getString("followContent"));
  183. followParam.setFollowTime(follow.getString("followTime"));
  184. return followParam;
  185. })
  186. .collect(Collectors.toList());
  187. param.setCallbackParamList(followParams);
  188. }
  189. return param;
  190. })
  191. .collect(Collectors.toList());
  192. }
  193. private void handleMappedData(List<XinLuUserStarCallbackParam> data) {
  194. log.info("成功处理 {} 条映射数据 明细:{}", data.size(), data);
  195. String post = HttpClientThreeUtil.post(url, JSON.toJSONString(data));
  196. log.info("post结果:{}", post);
  197. }
  198. public static String getCurrentDate() {
  199. LocalDate date = LocalDate.now(); // 获取当前日期
  200. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  201. return date.format(formatter); // 格式化为字符串
  202. }
  203. public static void main(String[] args) {
  204. // CloseableHttpClient httpClient = HttpClients.createDefault();
  205. // ApiClient apiClient = new ApiClient(httpClient);
  206. // String url = "https://xl.cdsxyc.com/api-xl/customer/verification/customerPlatformList/your_token";
  207. // String beginTime = "2025-03-21";
  208. // String endTime = "2025-03-21";
  209. // List<Map<String, Object>> result = apiClient.fetchAllData(url, beginTime, endTime);
  210. // System.out.println("获取数据条数: " + result.size());
  211. // SpringApplication app = new SpringApplication(LoanCallBackStarApplication.class);
  212. // app.setAdditionalProfiles("pre"); // 显式指定激活 pre 环境
  213. // ConfigurableApplicationContext ctx = app.run(args);
  214. // Environment env = ctx.getEnvironment();
  215. // System.out.println("=== 当前配置 ===");
  216. // System.out.println("server.port: " + env.getProperty("server.port"));
  217. // System.out.println("xinlu.callback.url: " + env.getProperty("xinlu.callback.url"));
  218. }
  219. }