Browse Source

头条视频素材报表编写

hcst_sunzhen 5 years ago
parent
commit
1d735a96cb

+ 1 - 0
module-toutiao/src/main/java/cn/com/ctop/toutiao/modules/report/entity/BytedanceReportMaterialRetry.java

@@ -58,4 +58,5 @@ public class BytedanceReportMaterialRetry {
     @ApiModelProperty(value = "updateTime")
 	private Date updateTime;
     private Integer statusCode;
+    private Integer type;
 }

+ 43 - 27
module-toutiao/src/main/java/cn/com/ctop/toutiao/modules/report/entity/BytedanceReportVideoMaterialDaily.java

@@ -146,31 +146,47 @@ public class BytedanceReportVideoMaterialDaily {
 	public BytedanceReportVideoMaterialDaily() {
 	}
 
-	//public BytedanceReportVideoMaterialDaily(JSONObject detailJson, Long accountId) {
-	//	JSONObject dimensions = detailJson.getJSONObject("dimensions");
-	//	this.accountId = accountId;
-	//	this.setMaterialId(dimensions.getLong("material_id"));
-	//	this.setStatDatetime(dimensions.getDate("stat_datetime") == null ? null : DateUtils.formatDate(dimensions.getDate("stat_datetime"), "yyyy-MM-dd"));
-	//
-	//	JSONObject metrics = detailJson.getJSONObject("metrics");
-	//	this.setValidPlayCost(metrics.getBigDecimal("valid_play_cost"));
-	//	this.setConvertMaterial(metrics.getInteger("convert"));
-	//	this.setPlayOverRate(metrics.getBigDecimal("play_over_rate"));
-	//	this.setCtr(metrics.getBigDecimal("ctr"));
-	//	this.setLikeMaterial(metrics.getInteger("like"));
-	//	this.setClick(metrics.getInteger("click"));
-	//	this.setTotalPlay(metrics.getInteger("total_play"));
-	//	this.setShowMaterial(metrics.getInteger("show"));
-	//	this.setConvertRate(metrics.getBigDecimal("convert_rate"));
-	//	this.setShareMaterial(metrics.getInteger("share"));
-	//	this.setValidPlay(metrics.getInteger("valid_play"));
-	//	this.setCost(metrics.getBigDecimal("cost"));
-	//	this.setPlayOver(metrics.getInteger("play_over"));
-	//	this.setFollow(metrics.getInteger("follow"));
-	//	this.setValidPlayRate(metrics.getBigDecimal("valid_play_rate"));
-	//	this.setAveragePlayTimePerPlay(metrics.getBigDecimal("average_play_time_per_play"));
-	//	this.setConvertShowRate(metrics.getBigDecimal("convert_show_rate"));
-	//	this.setCommentMaterial(metrics.getInteger("comment"));
-	//	this.setConvertCost(metrics.getBigDecimal("convert_cost"));
-	//}
+	public BytedanceReportVideoMaterialDaily(JSONObject detailJson, Long accountId) {
+		JSONObject dimensions = detailJson.getJSONObject("dimensions");
+		this.accountId = accountId;
+		this.setMaterialId(dimensions.getLong("material_id"));
+		this.setStatDatetime(dimensions.getDate("stat_datetime") == null ? null : DateUtils.formatDate(dimensions.getDate("stat_datetime"), "yyyy-MM-dd"));
+
+		JSONObject metrics = detailJson.getJSONObject("metrics");
+		this.setValidPlayCost(metrics.getBigDecimal("valid_play_cost"));
+		this.setClick(metrics.getLong("click"));
+		this.setCost(metrics.getBigDecimal("cost"));
+		this.setAvgClickCost(metrics.getBigDecimal("avg_click_cost"));
+		this.setDeepConvertCost(metrics.getBigDecimal("deep_convert_cost"));
+		this.setDeepConvertRate(metrics.getBigDecimal("deep_convert_rate"));
+		this.setPlayDuration2sRate(metrics.getBigDecimal("playDuration2sRate"));
+		this.setPlayDuration3sRate(metrics.getBigDecimal("playDuration3sRate"));
+		this.setPlayDuration5sRate(metrics.getBigDecimal("playDuration5sRate"));
+		this.setPlayDuration10sRate(metrics.getBigDecimal("playDuration10sRate"));
+		this.setPlay25FeedBreakRate(metrics.getBigDecimal("play25FeedBreakRate"));
+		this.setPlay50FeedBreakRate(metrics.getBigDecimal("play50FeedBreakRate"));
+		this.setPlay75FeedBreakRate(metrics.getBigDecimal("play75FeedBreakRate"));
+		this.setPlay100FeedBreakRate(metrics.getBigDecimal("play100FeedBreakRate"));
+		this.setPlayOverRate(metrics.getBigDecimal("play_over_rate"));
+		this.setAvgShowCost(metrics.getBigDecimal("avg_show_cost"));
+		this.setTotalPlay(metrics.getLong("total_play"));
+		this.setValidPlay(metrics.getLong("valid_play"));
+		this.setShareMaterial(metrics.getLong("share_material"));
+		this.setCommentMaterial(metrics.getLong("comment_material"));
+		this.setFollow(metrics.getLong("follow"));
+		this.setValidPlayCost(metrics.getBigDecimal("valid_play_cost"));
+		this.setConvertMaterial(metrics.getLong("convert_material"));
+		this.setPlayOverRate(metrics.getBigDecimal("play_over_rate"));
+		this.setCtr(metrics.getBigDecimal("ctr"));
+		this.setLikeMaterial(metrics.getLong("like_material"));
+		this.setShowMaterial(metrics.getLong("show_material"));
+		this.setConvertRate(metrics.getBigDecimal("convert_rate"));
+		this.setPlayOver(metrics.getLong("play_over"));
+		this.setMessageAction(metrics.getLong("message_action"));
+		this.setValidPlayRate(metrics.getBigDecimal("valid_play_rate"));
+		this.setAveragePlayTimePerPlay(metrics.getBigDecimal("average_play_time_per_play"));
+		this.setConvertCost(metrics.getBigDecimal("convert_cost"));
+		this.setConvertShowRate(metrics.getBigDecimal("convert_show_rate"));
+	}
+
 }

+ 74 - 0
module-toutiao/src/main/java/cn/com/ctop/toutiao/modules/report/mapper/xml/BytedanceReportMaterialDailyMapper.xml

@@ -246,4 +246,78 @@
         and start_date = #{retry.startDate}
         and end_date = #{retry.endDate}
     </update>
+
+
+
+
+
+
+    <insert id="replaceIntoBatch">
+        REPLACE INTO ctop_bytedance_report_material_daily
+        (
+        account_id,
+        material_id,
+        stat_datetime,
+        click,
+        cost,
+        avg_click_cost,
+        deep_convert,
+        deep_convert_cost,
+        deep_convert_rate,
+        dislike,
+        replay_duration_2s_rate,
+        play_duration_3s_rate,
+        play_duration_5s_rate,
+        play_duration_10s_rate,
+        avg_show_cost,
+        total_play,
+        valid_play,
+        share_material,
+        comment_material,
+        follow,
+        valid_play_cost,
+        convert_material,
+        play_over_rate,
+        ctr,
+        like_material,
+        show_material,
+        convert_rate,
+        play_over,
+        message_action,
+        valid_play_rate,
+        average_play_time_per_play,
+        convert_cost,
+        convert_show_rate
+        )
+        VALUES
+        <foreach collection="infos" item="info" separator=",">
+            (
+            #{info.accountId},
+            #{info.materialId},
+            #{info.statDatetime},
+            #{info.click},
+            #{info.avg_click_cost},
+            #{info.deep_convert},
+            #{info.cost},
+            #{info.cost},
+            #{info.cost},
+            #{info.cost},
+            #{info.cost},
+            #{info.cost},
+            #{info.cost},
+            #{info.cost},
+
+            )
+        </foreach>
+    </insert>
+
+
+
+
+
+
+
+
+
+
 </mapper>

+ 121 - 3
module-toutiao/src/main/java/cn/com/ctop/toutiao/modules/report/service/impl/BytedanceReportServiceImpl.java

@@ -9,6 +9,7 @@ import cn.com.ctop.common.module.utils.LoadFileUtil;
 import cn.com.ctop.toutiao.modules.report.entity.BytedanceDailyReportTask;
 import cn.com.ctop.toutiao.modules.report.entity.BytedanceReportMaterialDaily;
 import cn.com.ctop.toutiao.modules.report.entity.BytedanceReportMaterialRetry;
+import cn.com.ctop.toutiao.modules.report.entity.BytedanceReportVideoMaterialDaily;
 import cn.com.ctop.toutiao.modules.report.mapper.BytedanceAdvertiserDailyReportMapper;
 import cn.com.ctop.toutiao.modules.report.mapper.BytedanceDailyReportTaskMapper;
 import cn.com.ctop.toutiao.modules.report.mapper.BytedanceReportMapper;
@@ -722,6 +723,7 @@ public class BytedanceReportServiceImpl implements IBytedanceReportService {
         retry.setStartDate(startDate);
         retry.setEndDate(endDate);
         retry.setStatusCode(code);
+        retry.setType(1);
         if (code != 200 && code != 1) {
             retry.setStatus(0);
             bytedanceReportMaterialDailyMapper.replaceMaterialRetry(retry);
@@ -746,6 +748,7 @@ public class BytedanceReportServiceImpl implements IBytedanceReportService {
             retry.setStartDate(startDate);
             retry.setEndDate(endDate);
             retry.setStatusCode(code);
+            retry.setType(1);
             bytedanceReportMaterialDailyMapper.replaceMaterialRetry(retry);
         }
         log.info("头条素材报表当前accountId为:{}  {}~{}" , accountId, startDate,endDate);
@@ -758,8 +761,7 @@ public class BytedanceReportServiceImpl implements IBytedanceReportService {
 
         // 请求地址
         String open_api_domain = "https://ad.oceanengine.com";
-        //String path = "/open_api/2/report/integrated/get/";
-        String path = "/open_api/2/report/video/get/";
+        String path = "/open_api/2/report/integrated/get/";
 
         // 请求参数
         Map data = new HashMap() {
@@ -769,7 +771,7 @@ public class BytedanceReportServiceImpl implements IBytedanceReportService {
                 put("end_date", endDate);
                 put("page", page);
                 put("page_size", pageSize);
-                put("group_by", new String[]{"STAT_GROUP_BY_MATERIAL_ID", "STAT_GROUP_BY_TIME_DAY"});
+                put("group_by", new String[]{"STAT_GROUP_BY_MATERIAL_ID", "STAT_GROUP_BY_INVENTORY", "STAT_GROUP_BY_IMAGE_MODE", "STAT_GROUP_BY_TIME_DAY"});
             }
         };
 
@@ -942,4 +944,120 @@ public class BytedanceReportServiceImpl implements IBytedanceReportService {
         return bytedanceReportMaterialDailyMapper.getRetryList();
     }
 
+
+    /**
+     * 头条视频素材报表
+     * @param token
+     * @param startDate
+     * @param endDate
+     * @return
+     */
+    //@Override
+    public int bytedanceVideoMaterialReport(CtopOauthToken token, String startDate, String endDate) {
+        Long accountId = token.getAccountId();
+        log.info("头条素材报表当前accountId为:" + accountId);
+        Integer page = 1;
+        Integer pageSize = 100;
+        int code = bytedanceVideoMaterialReportByPage(page, pageSize, token, accountId, startDate, endDate);
+        if (code != 200 && code != 1) {
+            BytedanceReportMaterialRetry retry = new BytedanceReportMaterialRetry();
+            retry.setAccountId(accountId);
+            retry.setStatus(0);
+            retry.setStartDate(startDate);
+            retry.setEndDate(endDate);
+            retry.setStatusCode(code);
+            retry.setType(2);  //1素材报表重试,2视频素材报表重试
+            bytedanceReportMaterialDailyMapper.replaceMaterialRetry(retry);
+        }
+        log.info("头条素材报表当前accountId为:{}  {}~{}" , accountId, startDate,endDate);
+        return code;
+    }
+
+    private int bytedanceVideoMaterialReportByPage(Integer page, Integer pageSize, CtopOauthToken token, Long accountId, String startDate, String endDate) {
+        //log.info("当前页数:"+ page);
+        String access_token = token.getAccessToken();
+
+        // 请求地址
+        String open_api_domain = "https";
+        String path = "/open_api/2/report/video/get/";
+
+        // 请求参数
+        Map data = new HashMap();
+        data.put("advertiser_id", accountId);
+        data.put("start_date", startDate);
+        data.put("end_date", endDate);
+        data.put("page", page);
+        data.put("page_size", pageSize);
+        data.put("group_by", new String[]{"STAT_GROUP_BY_MATERIAL_ID", "STAT_GROUP_BY_TIME_DAY"});
+
+        JSONObject json = HttpUtils.bytedanceGetRequest(access_token, open_api_domain + path, JSONObject.parseObject(JSONObject.toJSONString(data)));
+        if (json == null) {
+            log.error("请求有误:" + JSONObject.toJSONString(data));
+            return -2;
+        }
+
+        int returnCode = 200;
+        try {
+            if (!Check.isNull(json)) {
+                Integer code = json.getInteger("code");
+                if (code != 0) {
+                    log.error("获取任务列表返回信息错误,错误码为:" + code + ",头条accountId:" + accountId + ";返回json为:" + JSONObject.toJSONString(data));
+                    return -1;
+                }
+
+                JSONObject jsonData = json.getJSONObject("data");
+                if (Check.isNull(jsonData)) {
+                    log.error("获取任务列表返回信息data内容为空,头条accountId:" + accountId + ";返回json为:" + JSONObject.toJSONString(data));
+                    return -1;
+                }
+                JSONObject pageInfo = jsonData.getJSONObject("page_info");
+                Integer totalPage = pageInfo.getInteger("total_page");
+                Integer currentPage = pageInfo.getInteger("page");
+
+                JSONArray jsonArrayay = jsonData.getJSONArray("list");
+                if (jsonArrayay.size() == 0) {
+                    log.info("accountId:" + accountId + ";没有数据。总页数为:" + totalPage + "当前页数为:" + currentPage);
+                    //returnCode = 1;
+                    return 1;
+                }
+
+                List<BytedanceReportVideoMaterialDaily> bytedanceReportVideoMaterialDailyList = new ArrayList<>();
+                for (int i = 0; i < jsonArrayay.size(); i++) {
+                    //BytedanceReportMaterialDaily daily = new BytedanceReportMaterialDaily();
+                    //daily.setAccountId(accountId);
+                    JSONObject detailJson = jsonArrayay.getJSONObject(i);
+                    if (!Check.isNull(detailJson)) {
+                        BytedanceReportVideoMaterialDaily daily = new BytedanceReportVideoMaterialDaily(detailJson, accountId);
+
+                        bytedanceReportVideoMaterialDailyList.add(daily);
+                    }
+                }
+                //Long insertStartTime = System.currentTimeMillis();
+
+
+                bytedanceReportMaterialDailyMapper.replaceIntoBatch(bytedanceReportVideoMaterialDailyList);
+
+
+                //Long insertEndTime = System.currentTimeMillis();
+                //log.info("头条获取素材报表插入数据结束,执行耗时:{}秒", (insertEndTime - insertStartTime) / 1000);
+                if (currentPage >= totalPage) {
+                    //log.info("accountId:" + accountId + "数据同步完成,开始时间:" + startDate + ",结束时间:"+ endDate);
+                    return 1;
+                } else {
+                    int pageCode = bytedanceVideoMaterialReportByPage(page + 1, pageSize, token, accountId, startDate, endDate);
+                    return pageCode;
+                }
+            } else {
+                //returnCode = -1;
+                log.error("服务器返回为空,json:" + JSONObject.toJSONString(data));
+                return -1;
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+            log.error("头条报表其他错误,accountId:" + accountId + ",json:" + JSONObject.toJSONString(data));
+            returnCode = -3;
+        }
+        return returnCode;
+    }
+
 }