客户搜索 API 超过 1 万条客户数据的 JAVA 请求示例


import com.alibaba.fastjson.JSONObject;
import com.youzan.cloud.open.sdk.common.exception.SDKException;
import com.youzan.cloud.open.sdk.core.client.auth.Token;
import com.youzan.cloud.open.sdk.core.client.core.DefaultYZClient;
import com.youzan.cloud.open.sdk.gen.v3_1_0.api.YouzanScrmCustomerSearch;
import com.youzan.cloud.open.sdk.gen.v3_1_0.model.YouzanScrmCustomerSearchParams;
import com.youzan.cloud.open.sdk.gen.v3_1_0.model.YouzanScrmCustomerSearchResult;
import org.junit.Test;

public class TestScrmCustomer {
    private int MAXCOUNT = 10000, MAXPAGESIZE = 50, MAXPAGENO = 200, PAGENO = 1;

    @Test
    public void main() {
        DefaultYZClient yzClient = new DefaultYZClient();
        Token token = new Token("8237717f21334ec*********1");
        Long createAtEnd = 0L;
        doCheck(yzClient, token, MAXPAGESIZE, PAGENO, createAtEnd);
    }

 /**
     * 判断是否需要进行时间分割再次请求
     * @param yzClient
     * @param token
     * @param pageSize
     * @param pageNo
     * @param createAtEnd
     */
   public void doCheck(DefaultYZClient yzClient, Token token, int pageSize, int pageNo, Long createAtEnd) {
        YouzanScrmCustomerSearchParams youzanScrmCustomerSearchParams = new YouzanScrmCustomerSearchParams();
        YouzanScrmCustomerSearch youzanScrmCustomerSearch = new YouzanScrmCustomerSearch();
        youzanScrmCustomerSearchParams.setPage(pageNo);
        youzanScrmCustomerSearchParams.setPageSize(pageSize);
        if (createAtEnd > 0)
            youzanScrmCustomerSearchParams.setCreatedAtEnd(createAtEnd);
        youzanScrmCustomerSearch.setAPIParams(youzanScrmCustomerSearchParams);
        try {
            YouzanScrmCustomerSearchResult result = yzClient.invoke(youzanScrmCustomerSearch, token, YouzanScrmCustomerSearchResult.class);
            Long total = result.getData().getTotal();
 //搜索的总记录数
            float totalPageNo = (float) total / pageSize; //需要调用的次数
            if (total > MAXCOUNT) {
                youzanScrmCustomerSearchParams.setPage(MAXPAGENO);
                youzanScrmCustomerSearchParams.setPageSize(pageSize);
                youzanScrmCustomerSearch.setAPIParams(youzanScrmCustomerSearchParams);
                YouzanScrmCustomerSearchResult result_j = yzClient.invoke(youzanScrmCustomerSearch, token, YouzanScrmCustomerSearchResult.class);
                //将拉取的到1万要数据存入数据库
                doSave(yzClient, token, Long.parseLong(String.valueOf(MAXPAGENO)), pageSize, createAtEnd);
                //获取最后一条数据的时间,用于分割数据进行下一次查询
                createAtEnd = result_j.getData().getRecordList().get(pageSize - 1).getCreatedAt();
                //进行下一次查询
                doCheck(yzClient, token, pageSize, pageNo, createAtEnd);

            } else {
                doSave(yzClient, token, totalPageNo, pageSize, createAtEnd);
            }
        } catch (SDKException e) {
            e.printStackTrace();
        }

    }

/**
     * 保存每次请求的结果到数据库
     * @param yzClient
     * @param token
     * @param totalPageNo
     * @param pageSize
     * @param createAtEnd
     */
  public void doSave(DefaultYZClient yzClient, Token token, float totalPageNo, int pageSize, Long createAtEnd) {
        for (int j = 0; j < totalPageNo; j++) {
            YouzanScrmCustomerSearchParams youzanScrmCustomerSearchParams = new YouzanScrmCustomerSearchParams();
            YouzanScrmCustomerSearch youzanScrmCustomerSearch = new YouzanScrmCustomerSearch();
            youzanScrmCustomerSearchParams.setPage(j + 1);
            youzanScrmCustomerSearchParams.setPageSize(pageSize);
            if (createAtEnd > 0)
                youzanScrmCustomerSearchParams.setCreatedAtEnd(createAtEnd);
            youzanScrmCustomerSearch.setAPIParams(youzanScrmCustomerSearchParams);
            try {
                YouzanScrmCustomerSearchResult result = yzClient.invoke(youzanScrmCustomerSearch, token, YouzanScrmCustomerSearchResult.class);
                System.out.println(JSONObject.toJSONString(result)); //将结果存入数据库,注意:手机号重复的需要做更新处理
                //将第一条数据的时间存入数据库,用于下次查询时的开始时间条件
                if (0 == createAtEnd && j == 0) {
                    Long nextCreateAt = result.getData().getRecordList().get(0).getCreatedAt();
                    System.out.println("下次查询的开始时间:" + JSONObject.toJSONString(nextCreateAt));
  //存入数据库
                }
            } catch (SDKException e) {
                e.printStackTrace();
            }
        }
    }
}