Просмотр исходного кода

promotion list 超过1万拉取修改

hpk 1 год назад
Родитель
Сommit
e238074a23

+ 28 - 9
job-bytedance/src/main/java/cn/com/ctop/job/bytedance/data/service/impl/BytedanceCdpProjectServiceImpl.java

@@ -81,13 +81,19 @@ public class BytedanceCdpProjectServiceImpl implements IBytedanceCdpProjectServi
 
     @Override
     public void getPromotionList(Long advertiserId, JSONArray ids, String date, Long errorId, Integer isLog) {
-        this.getPromotionListVo(advertiserId, ids, date, 1, 1, errorId, isLog);
+        if (ids == null || ids.isEmpty()) {   //总数超过1万条必须使用 cursor count 分页,且不能和ids过滤一起使用
+            this.getPromotionListVo(advertiserId, ids, date, null, 0L, 0, errorId, isLog);
+        } else {                              //page page_size 分页
+            this.getPromotionListVo(advertiserId, ids, date, 1, null, 1, errorId, isLog);
+        }
+
     }
 
-    private void getPromotionListVo(Long advertiserId, JSONArray ids, String date, Integer page, int count, Long errorId, Integer isLog) {
+    private void getPromotionListVo(Long advertiserId, JSONArray ids, String date, Integer page, Long cursor, int count, Long errorId, Integer isLog) {
         try {
             log.info("执行id:{}", advertiserId);
             boolean nextPage = false;
+            Long nextCursor = null;
             String accessToken = tokenMapper.onlyGetToken(advertiserId);
             if (Check.isNull(accessToken)) {
                 log.error("账户({})查询token失败", advertiserId);
@@ -104,8 +110,13 @@ public class BytedanceCdpProjectServiceImpl implements IBytedanceCdpProjectServi
                 filtering.put("ids", ids);
             }
             params.put("filtering", filtering);
-            params.put("page", page);
-            params.put("page_size", 20);//最大20
+            if (cursor != null) {
+                params.put("cursor", cursor);
+                params.put("count", 20);//最大20
+            } else {
+                params.put("page", page);
+                params.put("page_size", 20);//最大20
+            }
             String url = "https://api.oceanengine.com/open_api/v3.0/promotion/list/";
             String results = HttpUtils.httpGet(url, params, HttpUtils.postMHeaders(accessToken));
             JSONObject obj = JSONObject.parseObject(results);
@@ -113,9 +124,17 @@ public class BytedanceCdpProjectServiceImpl implements IBytedanceCdpProjectServi
                 Integer code = obj.getInteger("code");
                 if (code == 0) {
                     JSONObject data = obj.getJSONObject("data");
-                    Integer totalPage = data.getJSONObject("page_info").getInteger("total_page");
-                    if (totalPage != 0 && totalPage > data.getJSONObject("page_info").getInteger("page")) {
-                        nextPage = true;
+                    if (cursor != null) {
+                        JSONObject cursorInfo = data.getJSONObject("cursor_info");
+                        nextPage = cursorInfo.getBoolean("has_more");
+                        if (nextPage) {
+                            nextCursor = cursorInfo.getLong("cursor");
+                        }
+                    } else {
+                        Integer totalPage = data.getJSONObject("page_info").getInteger("total_page");
+                        if (totalPage != 0 && totalPage > data.getJSONObject("page_info").getInteger("page")) {
+                            nextPage = true;
+                        }
                     }
                     JSONArray array = data.getJSONArray("list");
                     for (int i = 0; i < array.size(); i++) {
@@ -134,7 +153,7 @@ public class BytedanceCdpProjectServiceImpl implements IBytedanceCdpProjectServi
                     //40100请求超频 (40100 == code || 40110 == code || 40130 == code) &&
                     if (count <= 5) {
                         Thread.sleep(800);
-                        getPromotionListVo(advertiserId, ids, date, page, count + 1, errorId, isLog);
+                        getPromotionListVo(advertiserId, ids, date, page, cursor, count + 1, errorId, isLog);
                     } else {
                         insertErrorLog(isLog, advertiserId, "-", "-", "promotion_list", results);
                         log.error("获取广告列表失败(url:{}),入参:{}\n结果:{}", url, JSONObject.toJSONString(params), obj);
@@ -143,7 +162,7 @@ public class BytedanceCdpProjectServiceImpl implements IBytedanceCdpProjectServi
                 }
             }
             if (nextPage) {
-                getPromotionListVo(advertiserId, ids, date, page + 1, 1, errorId, isLog);
+                getPromotionListVo(advertiserId, ids, date, page + 1, nextCursor, 1, errorId, isLog);
             } else if (errorId != null) {
                 promoterReportMapper.deleteInfoByErrorId(errorId);
             }