业务代码
/** * 天眼查,一次查询1000条数据 */ @Override @Transactional(rollbackFor = Exception.class) public AjaxResult tianyanchaAuto() { LambdaQueryWrapper<Customer> qw = new LambdaQueryWrapper<>(); qw.eq(Customer::getDelFlag, "0"); //判断表中有没有天眼查数据 qw.isNull(Customer::getTianyanchaUpdateTime); List<Customer> customers = baseMapper.selectList(qw); //分批执行 int batchSize = 1000; int totalSize = customers.size(); int batchCount = (totalSize + batchSize - 1) / batchSize; // ExecutorService executorService = Executors.newFixedThreadPool(10); // 设置线程池大小 for (int i = 0; i < batchCount; i++) { int fromIndex = i * batchSize; int toIndex = Math.min((i + 1) * batchSize, totalSize); List<Customer> batchCustomers = customers.subList(fromIndex, toIndex); batchCustomers.stream().forEach(customer -> { String cacheKey = "tianyancha:" + customer.getId(); //先从redis中查询天眼查信息是否在缓存中 String cachedResult = redisCache.getCacheObject(cacheKey); if (cachedResult != null) { BusinessInformationDto businessInformationDto = JSONObject.parseObject(cachedResult, BusinessInformationDto.class); updateCustomerWithBusinessInfo(customer, businessInformationDto); return; } else { String businessInformation = TianYanChaUtil.getBusinessInformation(customer.getAuthenticationName()); if (businessInformation.contains("ok")) { JSONObject jsonObject = JSONObject.parseObject(businessInformation); JSONArray items = jsonObject.getJSONObject("result").getJSONArray("items"); JSONObject firstItem = items.getJSONObject(0); BusinessInformationDto businessInformationDto = JSONObject.parseObject(firstItem.toJSONString(), BusinessInformationDto.class); updateCustomerWithBusinessInfo(customer, businessInformationDto); //持久化缓存天眼查查询信息 redisCache.setCacheObject(cacheKey, JSONObject.toJSONString(businessInformationDto)); } else { //请求失败,不能直接返回null,不然会回滚,钱也没了 customer.setRegStatus(null); customer.setLegalPersonName(null); customer.setEstiblishTime(null); customer.setCreditCode(null); customer.setRegCapital(null); customer.setTianyanchaUpdateTime(null); } } }); // executorService.shutdown(); // while (!executorService.isTerminated()) { // // 等待所有任务完成 // } // 批量修改 customerService.updateBatchById(batchCustomers); return AjaxResult.success("同步成功"); } return AjaxResult.success("同步失败"); }
工具类
package mmm.crm.common.utils; import mmm.crm.common.annotation.RateLimiter; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpConnectionParams; import org.apache.http.util.EntityUtils; /** * @Author: XGY * @Date: 2023/6/5 13:37 * 天眼查工具类 */ public class TianYanChaUtil { @RateLimiter(time = 60,count = 1000) public static String getBusinessInformation(String authenticationName) { String token="天眼查接口token"; String url = "http://open.api.tianyancha.com/services/open/search/2.0?word=" + authenticationName + "&pageSize=20&pageNum=1"; String result = ""; try { // 根据地址获取请求 HttpGet request = new HttpGet(url);//这⾥发送get请求 // 获取当前客户端对象 request.setHeader("Authorization", token); HttpClient httpClient = new DefaultHttpClient(); // 通过请求对象获取响应对象 HttpResponse response = httpClient.execute(request); // 判断⽹络连接状态码是否正常(0--200都数正常) if (response.getStatusLine().getStatusCode() == HttpStatus.SUCCESS) { result = EntityUtils.toString(response.getEntity(), "utf-8"); } } catch (Exception e) { e.printStackTrace(); } return result; } }