123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- package com.tiangua.star.service.impl;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONObject;
- import com.tiangua.star.model.UserStarFollowCallbackParam;
- import com.tiangua.star.model.XinLuUserStarCallbackParam;
- import com.tiangua.star.service.XinLuService;
- import com.tiangua.star.util.HttpClientThreeUtil;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.collections4.CollectionUtils;
- import org.apache.http.client.methods.CloseableHttpResponse;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.client.utils.URIBuilder;
- import org.apache.http.impl.client.CloseableHttpClient;
- import org.apache.http.impl.client.HttpClients;
- import org.apache.http.util.EntityUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.boot.SpringApplication;
- import org.springframework.context.ConfigurableApplicationContext;
- import org.springframework.core.env.Environment;
- import org.springframework.stereotype.Service;
- import javax.annotation.PostConstruct;
- import java.io.IOException;
- import java.net.URISyntaxException;
- import java.nio.charset.StandardCharsets;
- import java.time.LocalDate;
- import java.time.format.DateTimeFormatter;
- import java.util.*;
- import java.util.concurrent.Executor;
- import java.util.stream.Collectors;
- @Service
- @Slf4j
- public class XinLuDataProcessor implements XinLuService {
- // HTTP请求工具
- private static final CloseableHttpClient httpClient = HttpClients.createDefault();
- @Value("${xinlu.callback.url}")
- private String url;
- @Autowired
- @Qualifier("starExecutor")
- private Executor executor;
- @PostConstruct
- public void init() {
- log.info("xinlu init{}",url);
- }
- /**
- * 处理机构数据入口
- * @param jsonObjects 包含productBizId和busiId的配置列表
- */
- @Override
- public void processInstitutionData(List<JSONObject> jsonObjects) {
- jsonObjects.forEach(config -> executor.execute(() -> {
- Integer institutionId = config.getInteger("institutionId");
- String busiId = config.getString("busiId");
- // 1. 根据institutionId获取对应URL
- String targetUrl = config.getString("starLevelBackInter");
- if (targetUrl == null) {
- log.warn("未找到institutionId={}对应的URL", institutionId);
- return;
- }
- // 星级数据拉取周期从一天调整为一周
- for (int i = 6; i >= 0; i--) {
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
- String syncDate = LocalDate.now().minusDays(i).format(formatter);
- // 2. 发送GET请求
- String responseJson = sendGetRequest(targetUrl,1, syncDate, syncDate);
- if (responseJson == null) { continue; }
- // 3. 解析并映射数据
- List<XinLuUserStarCallbackParam> mappedData = parseAndMapData(responseJson, institutionId, busiId);
- log.info("鑫路回传诚易融数据{},{},同步时间:{}", institutionId, mappedData, syncDate);
- if (CollectionUtils.isEmpty(mappedData)) {
- log.info("鑫路没有回传数据{},同步时间:{}", institutionId, syncDate);
- continue;
- }
- // 4. 后续处理(存储/推送等)
- handleMappedData(mappedData);
- }
- }));
- }
- private String sendGetRequest(String originalUrl,int pageNum, String beginTime, String endTime){
- HttpGet httpGet = new HttpGet(buildUrl(originalUrl,pageNum,beginTime,endTime));
- try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
- int statusCode = response.getStatusLine().getStatusCode();
- if (statusCode == 200) {
- return EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
- } else {
- log.warn("请求失败,状态码:%d%n", statusCode);
- return null;
- }
- } catch (IOException e) {
- log.warn("HTTP请求异常,URL: %s%n", originalUrl);
- }
- return null;
- }
- private String buildUrl(String originalUrl, int pageNum, String beginTime, String endTime) {
- try {
- URIBuilder uriBuilder = new URIBuilder(originalUrl.trim());
- List<org.apache.http.NameValuePair> params = new ArrayList<>();
- for (org.apache.http.NameValuePair param : uriBuilder.getQueryParams()) {
- String name = param.getName();
- if (!"pageNum".equals(name) && !"pageSize".equals(name) && !"beginTime".equals(name) && !"endTime".equals(name)) {
- params.add(param);
- }
- }
- params.add(new org.apache.http.message.BasicNameValuePair("pageNum", String.valueOf(pageNum)));
- params.add(new org.apache.http.message.BasicNameValuePair("pageSize", "100"));
- params.add(new org.apache.http.message.BasicNameValuePair("beginTime", beginTime));
- params.add(new org.apache.http.message.BasicNameValuePair("endTime", endTime));
- uriBuilder.setParameters(params);
- return uriBuilder.build().toString();
- } catch (URISyntaxException e) {
- log.error("buildUrl error",e);
- }
- return null;
- }
- // public List<Map<String, Object>> fetchAllData(String originalUrl, String beginTime, String endTime) {
- // List<Map<String, Object>> allRows = new ArrayList<>();
- // int pageNum = 1;
- // int total = -1;
- // int count = 0;
- // while (count < 10) {
- // try {
- // String url = buildUrl(originalUrl, pageNum, beginTime, endTime);
- // ApiResponse response = sendGetRequest(url);
- // if (response == null || response.getCode() != 200) {
- // System.err.printf("请求失败,pageNum: %d%n", pageNum);
- // break;
- // }
- // if (total == -1) {
- // total = response.getTotal();
- // if (total == 0) break;
- // }
- // if (response.getRows() != null) {
- // allRows.addAll(response.getRows());
- // }
- // int currentSize = response.getRows() == null ? 0 : response.getRows().size();
- // if (currentSize < 100 || allRows.size() >= total) {
- // break;
- // }
- // pageNum++;
- // count++;
- // } catch (URISyntaxException | IOException e) {
- // System.err.println("请求异常: " + e.getMessage());
- // break;
- // }
- // }
- // return allRows;
- // }
- private List<XinLuUserStarCallbackParam> parseAndMapData(String json, Integer productBizId, String busiId) {
- JSONObject responseData = JSON.parseObject(json);
-
- // 验证响应结构
- if (responseData.getIntValue("code") != 200 || !responseData.containsKey("rows")) {
- log.warn("无效的响应数据");
- return Collections.emptyList();
- }
- // 解析rows数组
- return responseData.getJSONArray("rows").stream()
- .filter(obj -> {
- JSONObject item = (JSONObject) obj;
- return item.containsKey("importantDegree")
- && !Objects.isNull(item.getFloat("importantDegree"));
- })
- .map(obj -> {
- JSONObject item = (JSONObject) obj;
- XinLuUserStarCallbackParam param = new XinLuUserStarCallbackParam();
- // 基础字段映射
- param.setCustomerName(item.getString("name"));
- param.setMaskPhone(item.getString("phone"));
- param.setFollowTime(item.getString("customerCreateTime"));
- param.setStarLevel(item.getFloat("importantDegree"));
- param.setImportantDegreeStr(item.getString("importantDegreeStr"));
-
- // 从上游传入的字段
- param.setProductBizId(productBizId);
- param.setBusiId(busiId);
- // 处理跟进记录列表
- if (item.containsKey("cuFollowList") && CollectionUtils.isNotEmpty(item.getJSONArray("cuFollowList"))) {
- List<UserStarFollowCallbackParam> followParams = item.getJSONArray("cuFollowList")
- .stream()
- .map(followObj -> {
- JSONObject follow = (JSONObject) followObj;
- UserStarFollowCallbackParam followParam = new UserStarFollowCallbackParam();
- followParam.setFollowContent(follow.getString("followContent"));
- followParam.setFollowTime(follow.getString("followTime"));
- return followParam;
- })
- .collect(Collectors.toList());
- param.setCallbackParamList(followParams);
- }
-
- return param;
- })
- .collect(Collectors.toList());
- }
- private void handleMappedData(List<XinLuUserStarCallbackParam> data) {
- log.info("成功处理 {} 条映射数据 明细:{}", data.size(), data);
- String post = HttpClientThreeUtil.post(url, JSON.toJSONString(data));
- log.info("post结果:{}", post);
- }
- public static String getCurrentDate() {
- LocalDate date = LocalDate.now(); // 获取当前日期
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
- return date.format(formatter); // 格式化为字符串
- }
- public static void main(String[] args) {
- // CloseableHttpClient httpClient = HttpClients.createDefault();
- // ApiClient apiClient = new ApiClient(httpClient);
- // String url = "https://xl.cdsxyc.com/api-xl/customer/verification/customerPlatformList/your_token";
- // String beginTime = "2025-03-21";
- // String endTime = "2025-03-21";
- // List<Map<String, Object>> result = apiClient.fetchAllData(url, beginTime, endTime);
- // System.out.println("获取数据条数: " + result.size());
- // SpringApplication app = new SpringApplication(LoanCallBackStarApplication.class);
- // app.setAdditionalProfiles("pre"); // 显式指定激活 pre 环境
- // ConfigurableApplicationContext ctx = app.run(args);
- // Environment env = ctx.getEnvironment();
- // System.out.println("=== 当前配置 ===");
- // System.out.println("server.port: " + env.getProperty("server.port"));
- // System.out.println("xinlu.callback.url: " + env.getProperty("xinlu.callback.url"));
- }
- }
|