Преглед изворни кода

异步报表、素材报表

hcst_sunzhen пре 5 година
родитељ
комит
cf92112b28

+ 5 - 4
module-report/src/main/java/cn/com/ctop/bytedance/controller/BytedanceReportController.java

@@ -101,7 +101,8 @@ public class BytedanceReportController {
 
     @PostMapping("/bytedance/async")
     public Result<List<JSONObject>> asyncTaskCreate(@RequestParam(name = "startDate")String startDate,
-                                                    @RequestParam(name = "endDate")String endDate) {
+                                                    @RequestParam(name = "endDate")String endDate,
+                                                    @RequestParam(name = "type")Integer type) {
 
         Result<List<JSONObject>> result = new Result<>();
         try {
@@ -114,9 +115,9 @@ public class BytedanceReportController {
             }
 
             for (CtopOauthToken token : tokens) {
-                if(1629785592929294L == token.getAccountId()){
-                    bytedanceReportService.bytedanceAsyncTaskCreate(startDate, endDate, token);
-                }
+                //if(1629785592929294L == token.getAccountId()){
+                    bytedanceReportService.bytedanceAsyncTaskCreate(startDate, endDate, token, type);
+                //}
              }
             Long endtime = System.currentTimeMillis();
             log.info("头条异步报表数据任务执行结束,执行耗时:{}秒", (endtime - starttime) / 1000);

+ 4 - 0
module-report/src/main/java/cn/com/ctop/bytedance/entity/BytedanceDailyReportTask.java

@@ -72,4 +72,8 @@ public class BytedanceDailyReportTask {
     private String taskStatus;
 
     private Integer isDownload;
+
+    private Integer type;
+
+    private Integer status;
 }

+ 5 - 0
module-report/src/main/java/cn/com/ctop/bytedance/mapper/BytedanceDailyReportTaskMapper.java

@@ -16,4 +16,9 @@ public interface BytedanceDailyReportTaskMapper extends BaseMapper<BytedanceDail
 
     void loadBytedanceDailyCreativeFileAsync(@Param("localPath")String localPath);
 
+    void loadBytedanceDailyAccountFileAsync(@Param("localPath")String localPath);
+
+    void loadBytedanceDailyCampaignFileAsync(@Param("localPath")String localPath);
+
+    void loadBytedanceDailyPlanFileAsync(@Param("localPath")String localPath);
 }

+ 77 - 0
module-report/src/main/java/cn/com/ctop/bytedance/mapper/xml/BytedanceDailyReportTaskMapper.xml

@@ -31,5 +31,82 @@
         )
     </insert>
 
+    <insert id="loadBytedanceDailyAccountFileAsync">
+        LOAD DATA local INFILE  #{localPath}
+        REPLACE INTO TABLE ctop_bytedance_report_account_daily_async CHARACTER SET utf8mb4
+        FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'
+        IGNORE 1 LINES
+        (
+            deep_convert_rate,
+            pricing_category,
+            ctr,
+            show_material,
+            avg_show_cost,
+            convert_rate,
+            convert_cost,
+            convert_material,
+            deep_convert_cost,
+            account_id,
+            cost,
+            deep_convert,
+            avg_click_cost,
+            click,
+            stat_datetime
+        )
+    </insert>
+
+    <insert id="loadBytedanceDailyCampaignFileAsync">
+        LOAD DATA local INFILE  #{localPath}
+        REPLACE INTO TABLE ctop_bytedance_report_campaign_daily_async CHARACTER SET utf8mb4
+        FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'
+        IGNORE 1 LINES
+        (
+            campaign_type,
+            deep_convert_rate,
+            ctr,
+            show_material,
+            avg_show_cost,
+            convert_rate,
+            convert_cost,
+            campaign_id,
+            deep_convert_cost,
+            account_id,
+            cost,
+            deep_convert,
+            avg_click_cost,
+            campaign_name,
+            convert_material,
+            click,
+            stat_datetime
+        )
+    </insert>
+
+    <insert id="loadBytedanceDailyPlanFileAsync">
+        LOAD DATA local INFILE  #{localPath}
+        REPLACE INTO TABLE ctop_bytedance_report_plan_daily_async CHARACTER SET utf8mb4
+        FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'
+        IGNORE 1 LINES
+        (
+            landing_type,
+            deep_convert_rate,
+            ad_id,
+            ctr,
+            show_material,
+            avg_show_cost,
+            convert_rate,
+            convert_cost,
+            convert_material,
+            deep_convert_cost,
+            account_id,
+            cost,
+            deep_convert,
+            avg_click_cost,
+            ad_name,
+            campaign_name,
+            click,
+            stat_datetime,
+            campaign_id
+        )
+    </insert>
 
 </mapper>

+ 1 - 1
module-report/src/main/java/cn/com/ctop/bytedance/service/IBytedanceReportService.java

@@ -36,7 +36,7 @@ public interface IBytedanceReportService {
      */
     List<JSONObject> getReportList(JSONObject requestJson);
 
-    void bytedanceAsyncTaskCreate(String startDate, String endDate, CtopOauthToken token) throws ParseException;
+    void bytedanceAsyncTaskCreate(String startDate, String endDate, CtopOauthToken token, Integer type) throws ParseException;
 
     void bytedanceAsyncTaskGet();
 

+ 37 - 12
module-report/src/main/java/cn/com/ctop/bytedance/service/impl/BytedanceReportServiceImpl.java

@@ -31,6 +31,7 @@ import org.apache.http.entity.ContentType;
 import org.apache.http.entity.StringEntity;
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClientBuilder;
+import org.bytedeco.javacpp.freenect2;
 import org.jeecg.common.util.DateUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
@@ -676,28 +677,28 @@ public class BytedanceReportServiceImpl implements IBytedanceReportService {
 
     /////////////////////////////////////////////////////////////////////////////////////////////////
     //创建异步任务
-    public void bytedanceAsyncTaskCreate (String startDate, String endDate, CtopOauthToken token) throws ParseException {
+    public void bytedanceAsyncTaskCreate (String startDate, String endDate, CtopOauthToken token, Integer type) throws ParseException {
         int diffMonths = DateUtils.calDiffMonth(DateUtils.parseDate(startDate,"yyyy-MM-dd"),DateUtils.parseDate(endDate,"yyyy-MM-dd"));
         if (diffMonths == 0) {
-            bytedanceAsyncTaskCreateByMonth(startDate, endDate, token);
+            bytedanceAsyncTaskCreateByMonth(startDate, endDate, token, type);
         }else{
             for(int i=0; i<=diffMonths ; i++){
                 if (i == 0){
                     Map<String,String> dateMap = DateUtils.getMaxMinDaysOfAddMonth(startDate,"yyyy-MM-dd", i);
                     String end = dateMap.get("endDate");
                     //System.out.println(startDate + " ----- " + end);
-                    bytedanceAsyncTaskCreateByMonth(startDate, end, token);
+                    bytedanceAsyncTaskCreateByMonth(startDate, end, token, type);
                 }else if(i == diffMonths){
                     Map<String,String> dateMap = DateUtils.getMaxMinDaysOfAddMonth(startDate,"yyyy-MM-dd", i);
                     String start = dateMap.get("startDate");
                     //System.out.println(start + " ----- " + endDate);
-                    bytedanceAsyncTaskCreateByMonth(start, endDate, token);
+                    bytedanceAsyncTaskCreateByMonth(start, endDate, token, type);
                 }else{
                     Map<String,String> dateMap = DateUtils.getMaxMinDaysOfAddMonth(startDate,"yyyy-MM-dd", i);
                     String start = dateMap.get("startDate");
                     String end = dateMap.get("endDate");
                     //System.out.println(start + " ----- " + end);
-                    bytedanceAsyncTaskCreateByMonth(start, end, token);
+                    bytedanceAsyncTaskCreateByMonth(start, end, token, type);
                 }
             }
         }
@@ -709,7 +710,7 @@ public class BytedanceReportServiceImpl implements IBytedanceReportService {
      * @param endDate
      * @param token
      */
-    private void bytedanceAsyncTaskCreateByMonth(String startDate, String endDate, CtopOauthToken token){
+    private void bytedanceAsyncTaskCreateByMonth(String startDate, String endDate, CtopOauthToken token, Integer type){
         String access_token = token.getAccessToken();
         Long accountId = token.getAccountId();
 
@@ -723,7 +724,15 @@ public class BytedanceReportServiceImpl implements IBytedanceReportService {
             {
                 put("start_date", startDate);
                 put("end_date", endDate);
-                put("group_by", new String[]{"STAT_GROUP_BY_CREATIVE_ID","STAT_GROUP_BY_TIME_DAY","STAT_GROUP_BY_CREATIVE_MATERIAL_MODE"});   //按照创意分组、天分组、创意类型分组查询
+                if(type == 1){//素材
+                    put("group_by", new String[]{"STAT_GROUP_BY_CREATIVE_ID","STAT_GROUP_BY_TIME_DAY","STAT_GROUP_BY_CREATIVE_MATERIAL_MODE"});   //按照创意分组、天分组、创意类型分组查询
+                }else if(type == 2){//广告主
+                    put("group_by", new String[]{"STAT_GROUP_BY_ADVERTISER_ID","STAT_GROUP_BY_TIME_DAY","STAT_GROUP_BY_PRICING_CATEGORY"});   //按照广告主分组、天分组、广告类型类型分组查询
+                }else if(type == 3){//广告组
+                    put("group_by", new String[]{"STAT_GROUP_BY_CAMPAIGN_ID","STAT_GROUP_BY_TIME_DAY","STAT_GROUP_BY_CAMPAIGN_TYPE"});  //按照广告组、天、广告组类型分组
+                }else {//4广告计划
+                    put("group_by", new String[]{"STAT_GROUP_BY_AD_ID","STAT_GROUP_BY_TIME_DAY","STAT_GROUP_BY_LANDING_TYPE"});  //按照广告计划、天、推广类型分组
+                }
             }
         };
         final Map data = new HashMap() {
@@ -784,7 +793,9 @@ public class BytedanceReportServiceImpl implements IBytedanceReportService {
                     task.setJson(jsonString);
                     task.setTaskStatus(dataJson.getString("task_status"));
                     task.setTaskType(dataJson.getString("task_type"));
+                    task.setType(type);
                     task.setIsDownload(0);
+                    task.setStatus(1); //状态1正常 0废弃
                     bytedanceDailyReportTaskMapper.insert(task);
                 }
 
@@ -896,7 +907,7 @@ public class BytedanceReportServiceImpl implements IBytedanceReportService {
                                         String taskStatus = detailJson.getString("task_status");
 
                                         if (taskStatus .equals( "ASYNC_TASK_STATUS_COMPLETED")) {
-                                            bytedanceAsyncTaskDownload(task,token);
+                                            bytedanceAsyncTaskDownload(task,token,task.getType());
                                             task.setIsDownload(1);
                                         }
                                             task.setTaskStatus(taskStatus);
@@ -929,19 +940,33 @@ public class BytedanceReportServiceImpl implements IBytedanceReportService {
         }
     }
 
-    //下载任务结果
-    private void bytedanceAsyncTaskDownload(BytedanceDailyReportTask task, CtopOauthToken token){
+    //下载任务结果   type 1.creative 2.account 3.campaign 4.plan
+    private void bytedanceAsyncTaskDownload(BytedanceDailyReportTask task, CtopOauthToken token, Integer type){
         // 请求地址
         String open_api_domain = "https://ad.toutiao.com";
         String path = "/open_api/2/async_task/download/";
 
         String url = open_api_domain + path;
-        String fileName = task.getAccountId() + "_" + System.currentTimeMillis() + ".csv";
+        String fileName = task.getAccountId() + "_" + "type" + type + "_" +System.currentTimeMillis() + ".csv";
         Map<String, Object> requestMap = new HashMap<>();
         requestMap.put("advertiser_id", task.getAccountId());
         requestMap.put("task_id", task.getTaskId());
         String localPath = DownloadUtils.downloadByUrl(requestMap, downloadPath, url, token.getAccessToken(), fileName);
-        bytedanceDailyReportTaskMapper.loadBytedanceDailyCreativeFileAsync(localPath);
+        if(type == 1){
+            //素材异步报表
+            bytedanceDailyReportTaskMapper.loadBytedanceDailyCreativeFileAsync(localPath);
+        }else if(type == 2){
+            //账户异步报表
+            bytedanceDailyReportTaskMapper.loadBytedanceDailyAccountFileAsync(localPath);
+        }else if(type == 3){
+            //campaign异步报表
+            bytedanceDailyReportTaskMapper.loadBytedanceDailyCampaignFileAsync(localPath);
+        }else if(type == 4){
+            //plan异步报表
+            bytedanceDailyReportTaskMapper.loadBytedanceDailyPlanFileAsync(localPath);
+        }else{
+            log.error("异步报表类型传入错误,task_id:" + task.getTaskId() + ";类型:" + type);
+        }
     }
 
     /////////////////////////////////////////////////////////\