天眼查查询企业工商资料

业务代码

  /**
     * 天眼查,一次查询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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我无力抗拒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值