订单搜索 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.v4_0_0.api.YouzanTradesSoldGet;
import com.youzan.cloud.open.sdk.gen.v4_0_0.model.YouzanTradesSoldGetParams;
import com.youzan.cloud.open.sdk.gen.v4_0_0.model.YouzanTradesSoldGetResult;
import org.junit.Test;

import java.util.Date;

public class GetTrades {
    private int MAXCOUNT = 10000, MAXPAGESIZE = 100, MAXPAGENO = 100, PAGENO = 1;
    private String STATUS="WAIT_SELLER_SEND_GOODS";

    public void main() {
        DefaultYZClient yzClient = new DefaultYZClient();
        Token token = new Token("8eee606d101f623ca7c16*******7");
        Date createTime = null;
        doCheck(yzClient, token, MAXPAGESIZE, PAGENO, createTime);
    }
    /**
     * 判断是否需要进行时间分割再次请求
     * @param yzClient
     * @param token
     * @param pageSize
     * @param pageNo
     * @param createTime
     */
    public void doCheck(DefaultYZClient yzClient, Token token, int pageSize, int pageNo, Date createTime) {
        YouzanTradesSoldGetParams youzanTradesSoldGetParams = new YouzanTradesSoldGetParams();
        YouzanTradesSoldGet youzanTradesSoldGet = new YouzanTradesSoldGet();
        youzanTradesSoldGetParams.setPageNo(pageNo);
        youzanTradesSoldGetParams.setPageSize(pageSize);
        youzanTradesSoldGetParams.setStatus(STATUS);
        if (createTime !=null)
            youzanTradesSoldGetParams.setEndCreated(createTime);
        youzanTradesSoldGet.setAPIParams(youzanTradesSoldGetParams);
        try {
            YouzanTradesSoldGetResult result = yzClient.invoke(youzanTradesSoldGet, token, YouzanTradesSoldGetResult.class);
            Long total = result.getData().getTotalResults();
            System.out.println("搜索的总记录数:"+total);
            //搜索的总记录数
            float totalPageNo = (float) total / pageSize; //需要调用的次数
            if (total > MAXCOUNT) {
                youzanTradesSoldGetParams.setPageNo(MAXPAGENO);
                youzanTradesSoldGetParams.setPageSize(pageSize);
                youzanTradesSoldGetParams.setStatus(STATUS);
                youzanTradesSoldGet.setAPIParams(youzanTradesSoldGetParams);
                YouzanTradesSoldGetResult result_j = yzClient.invoke(youzanTradesSoldGet, token, YouzanTradesSoldGetResult.class);
                //获取最后一条数据的时间,用于分割数据进行下一次查询
                createTime = result_j.getData().getFullOrderInfoList().get(pageSize - 1).getFullOrderInfo().getOrderInfo().getCreated();
                System.out.println("1万条数据最后一条数据的时间:"+createTime);
                //将拉取的到1万条数据存入数据库
                doSave(yzClient, token, Long.parseLong(String.valueOf(MAXPAGENO)), pageSize, createTime);
                //进行下一次查询
                doCheck(yzClient, token, pageSize, pageNo, createTime);

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

    }

    /**
     * 保存每次请求的结果到数据库
     * @param yzClient
     * @param token
     * @param totalPageNo
     * @param pageSize
     * @param createTime
     */
    public void doSave(DefaultYZClient yzClient, Token token, float totalPageNo, int pageSize, Date createTime) {
        for (int j = 0; j < totalPageNo; j++) {
            System.out.println("页数:"+j);
            YouzanTradesSoldGetParams youzanTradesSoldGetParams = new YouzanTradesSoldGetParams();
            YouzanTradesSoldGet youzanTradesSoldGet = new YouzanTradesSoldGet();
            youzanTradesSoldGetParams.setPageNo(j + 1);
            youzanTradesSoldGetParams.setPageSize(pageSize);
            youzanTradesSoldGetParams.setStatus(STATUS);
            if (createTime!=null)
                youzanTradesSoldGetParams.setEndCreated(createTime);
            youzanTradesSoldGet.setAPIParams(youzanTradesSoldGetParams);
            try {
                YouzanTradesSoldGetResult result = yzClient.invoke(youzanTradesSoldGet, token, YouzanTradesSoldGetResult.class);
                System.out.println("存入数据的数据数:"+result.getData().getFullOrderInfoList().size());
                System.out.println(JSONObject.toJSONString(result)); //将结果存入数据库
                //将第一条数据的时间存入数据库,用于下次查询时的开始时间条件
                if (createTime == null && j == 0) {
                    Date nextCreateAt = result.getData().getFullOrderInfoList().get(0).getFullOrderInfo().getOrderInfo().getCreated();
                    System.out.println("下次查询的开始时间:" + JSONObject.toJSONString(nextCreateAt));
                    //存入数据库
                }
            } catch (SDKException e) {
                e.printStackTrace();
            }
        }
    }
}