syh %!s(int64=5) %!d(string=hai) anos
pai
achega
4b1fe17643

+ 30 - 3
jeecg-boot-module-system/src/main/java/org/jeecg/modules/ctop/job/KuaishouDailyReportTaskJob.java

@@ -13,6 +13,9 @@ import org.springframework.beans.factory.annotation.Autowired;
 
 import java.util.Date;
 import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
 
 @Slf4j
 public class KuaishouDailyReportTaskJob implements Job {
@@ -21,8 +24,12 @@ public class KuaishouDailyReportTaskJob implements Job {
     @Autowired
     private IKuaiShouHistoryReportTaskService reportTaskService;
 
+    private static ExecutorService executorService = Executors.newFixedThreadPool(3);
+    private static CountDownLatch countDownLatch = null;
     @Override
     public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
+        log.info("快手获取日报数据任务执行开始");
+        Long start = System.currentTimeMillis();
         Date getDate = DateUtils.addDay(new Date(), -1);
         List<CtopOauthToken> tokens = tokenService.getTokenListByType(CtopAdConstant.PLATFORM_TYPE_KUAISHOU);
         if (null == tokens || tokens.size() <= 0) {
@@ -30,8 +37,28 @@ public class KuaishouDailyReportTaskJob implements Job {
             return;
         }
         String getDateStr = DateUtils.formatDate(getDate);
-        tokens.forEach(token -> {
-            reportTaskService.createTask(token.getAccountId(), token.getAccessToken(), getDateStr);
-        });
+        countDownLatch = new CountDownLatch(tokens.size());
+        for (CtopOauthToken token : tokens) {
+            executorService.submit(new Runnable() {
+                @Override
+                public void run() {
+                    //创建任务
+                    try {
+                        reportTaskService.createTask(token.getAccountId(), token.getAccessToken(), getDateStr, getDateStr, CtopAdConstant.KUAISHOU_LOAD_JOB_TYPE_DAILY);
+                    } catch (Exception e) {
+                        e.printStackTrace();
+                    } finally {
+                        countDownLatch.countDown();
+                    }
+                }
+            });
+        }
+        try {
+            countDownLatch.await();
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+        Long end = System.currentTimeMillis();
+        log.info("快手获取日报数据任务执行结束,执行耗时:{}秒", (end - start) / 1000);
     }
 }

+ 52 - 9
jeecg-boot-module-system/src/test/java/org/jeecg/SampleTest.java

@@ -1,13 +1,23 @@
 package org.jeecg;
 
+import cn.com.ctop.common.module.entity.CtopOauthToken;
+import cn.com.ctop.common.module.service.ICtopOauthTokenService;
+import cn.com.ctop.common.module.utils.CtopAdConstant;
 import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouHistoryReportTaskService;
 import lombok.extern.slf4j.Slf4j;
+import org.jeecg.common.util.DateUtils;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.test.context.junit4.SpringRunner;
 
+import java.util.Date;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
 @RunWith(SpringRunner.class)
 @SpringBootTest
 @Slf4j
@@ -19,16 +29,9 @@ public class SampleTest {
 
     @Test
     public void kuaisShouReport() {
-
-
         try {
-
-
-            reportTaskService.createTask(3727345L, "871fc2c248782a94ad2962c941555abc");
+            reportTaskService.createTask(3727345L, "871fc2c248782a94ad2962c941555abc", "");
             reportTaskService.getTaskList();
-
-
-
          /*   String url = "https://ad.e.kuaishou.com/rest/openapi/v1/async_task/list";
             Map<String, String> headers = new HashMap<>();
             headers.put("Content-Type", " application/json");
@@ -83,7 +86,6 @@ public class SampleTest {
                 inputStream.close();
             }*/
          /*   String localPath = "D:\\file\\123.csv";
-
             reportDailyAccountMapper.loadAccountDailyReport(142402L,localPath);*/
 
 
@@ -93,6 +95,47 @@ public class SampleTest {
         }
     }
 
+    @Autowired
+    private ICtopOauthTokenService tokenService;
+    private static ExecutorService executorService = Executors.newFixedThreadPool(3);
+    private static CountDownLatch countDownLatch = null;
+
+    @Test
+    public void testLoadJob() {
+        log.info("快手获取日报数据任务执行开始");
+        Long start = System.currentTimeMillis();
+        Date getDate = DateUtils.addDay(new Date(), -1);
+        List<CtopOauthToken> tokens = tokenService.getTokenListByType(CtopAdConstant.PLATFORM_TYPE_KUAISHOU);
+        if (null == tokens || tokens.size() <= 0) {
+            log.info("快手获取日报数据任务执行失败:未获取到可用的token");
+            return;
+        }
+        String getDateStr = DateUtils.formatDate(getDate);
+        countDownLatch = new CountDownLatch(tokens.size());
+        for (CtopOauthToken token : tokens) {
+            executorService.submit(new Runnable() {
+                @Override
+                public void run() {
+                    //创建任务
+                    try {
+                        reportTaskService.createTask(token.getAccountId(), token.getAccessToken(), getDateStr);
+                    } catch (Exception e) {
+                        e.printStackTrace();
+                    } finally {
+                        countDownLatch.countDown();
+                    }
+                }
+            });
+        }
+        try {
+            countDownLatch.await();
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+        Long end = System.currentTimeMillis();
+        log.info("快手获取日报数据任务执行结束,执行耗时:{}秒", (end - start) / 1000);
+    }
+
 
 
 

+ 3 - 0
module-common/src/main/java/cn/com/ctop/common/module/utils/CtopAdConstant.java

@@ -18,4 +18,7 @@ public class CtopAdConstant {
 
     public static final String CTOP_ORGCODE_NORTH_CHINA_PREFIX = "A01";
     public static final String CTOP_ORGCODE_EAST_CHINA_PREFIX = "A02";
+
+    public static final String KUAISHOU_LOAD_JOB_TYPE_HISTORY = "history";
+    public static final String KUAISHOU_LOAD_JOB_TYPE_DAILY = "daily";
 }

+ 1 - 1
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/batch/service/IKuaiShouHistoryReportTaskService.java

@@ -11,7 +11,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
  * @date 2020-03-09
  */
 public interface IKuaiShouHistoryReportTaskService extends IService<KuaiShouHistoryReportTask> {
-    void createTask(Long accountId, String token, String date);
+    void createTask(Long accountId, String token, String startDate, String endDate, String taskType);
 
     void getTaskList();
 

+ 82 - 41
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/batch/service/impl/KuaiShouHistoryReportTaskServiceImpl.java

@@ -2,12 +2,11 @@ package cn.com.ctop.kuaishou.modules.batch.service.impl;
 
 import cn.com.ctop.common.module.entity.CtopOauthToken;
 import cn.com.ctop.common.module.service.ICtopOauthTokenService;
-import cn.com.ctop.common.module.utils.Check;
-import cn.com.ctop.common.module.utils.HttpUtils;
-import cn.com.ctop.common.module.utils.KuaishouInterfaceConstant;
-import cn.com.ctop.common.module.utils.PropertiesUtils;
+import cn.com.ctop.common.module.utils.*;
+import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouDailyReportTask;
 import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouHistoryReportTask;
 import cn.com.ctop.kuaishou.modules.batch.mapper.KuaiShouHistoryReportTaskMapper;
+import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouDailyReportTaskService;
 import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouHistoryReportTaskService;
 import cn.com.ctop.kuaishou.modules.report.mapper.KuaishouReportDailyAccountMapper;
 import cn.com.ctop.kuaishou.modules.report.mapper.KuaishouReportDailyCampaignMapper;
@@ -42,7 +41,8 @@ import java.util.concurrent.Executors;
 public class KuaiShouHistoryReportTaskServiceImpl extends ServiceImpl<KuaiShouHistoryReportTaskMapper, KuaiShouHistoryReportTask> implements IKuaiShouHistoryReportTaskService {
     @Value("${jeecg.path.reportDownload}")
     private static String downloadPath;
-    static List<Integer> typeList;
+    static List<Integer> historyTypeList;
+    static List<Integer> dailyTypeList;
     static ExecutorService executorService = Executors.newFixedThreadPool(3);
 
     @Autowired
@@ -59,9 +59,11 @@ public class KuaiShouHistoryReportTaskServiceImpl extends ServiceImpl<KuaiShouHi
 
     @Autowired
     private KuaishouReportDailyCreativeMapper reportDailyCreativeMapper;
+    @Autowired
+    private IKuaiShouDailyReportTaskService dailyReportTaskService;
 
     @Override
-    public void createTask(Long accountId, String token, String date) {
+    public void createTask(Long accountId, String token, String startDate, String endDate, String taskType) {
         if (Check.isNull(accountId) || Check.isNull(token)) {
             log.error("token或accountId为空");
             return;
@@ -74,35 +76,67 @@ public class KuaiShouHistoryReportTaskServiceImpl extends ServiceImpl<KuaiShouHi
             JSONObject param = new JSONObject();
             param.put("advertiser_id", accountId);
             JSONObject taskParams = new JSONObject();
-            taskParams.put("start_date", date);
-            taskParams.put("end_date", date);
-
-            if (Check.isNull(typeList)) {
-                getTypeList();
-            }
-            for (int i = 0; i < typeList.size(); i++) {
-                Integer type = typeList.get(i);
-                String taskName = accountId + "_" + type;
-                param.put("task_name", taskName);
-                taskParams.put("view_type", type);
-                param.put("task_params", taskParams);
-                String result = HttpUtils.kuaiShouhttpPostRequest(url, param.toJSONString(), headers);
-                JSONObject resultJson = JSONObject.parseObject(result);
-                if (!Check.isNull(resultJson)) {
-                    Integer code = resultJson.getInteger("code");
-                    if (code == 0) {
-                        JSONObject dataJson = resultJson.getJSONObject("data");
-                        KuaiShouHistoryReportTask reportTask = new KuaiShouHistoryReportTask();
-                        reportTask.setAccountId(accountId);
-                        reportTask.setStartDate(date);
-                        reportTask.setEndDate(date);
-                        reportTask.setTaskId(dataJson.getString("task_id"));
-                        reportTask.setViewType(type);
-                        reportTask.setTaskStatus(0);
-                        reportTask.setTaskName(taskName);
-                        this.save(reportTask);
-                    } else {
-                        log.error("快手历史报表请求失败,accountId:{},返回数据:{}", accountId, resultJson);
+            taskParams.put("start_date", startDate);
+            taskParams.put("end_date", endDate);
+            if (null != taskType && taskType.equals(CtopAdConstant.KUAISHOU_LOAD_JOB_TYPE_DAILY)) {
+                if (Check.isNull(dailyTypeList)) {
+                    getDailyTypeList();
+                }
+                for (int i = 0; i < dailyTypeList.size(); i++) {
+                    Integer type = dailyTypeList.get(i);
+                    String taskName = accountId + "_" + type + "_" + System.currentTimeMillis();
+                    log.info("任务名称:{}", taskName);
+                    param.put("task_name", taskName);
+                    taskParams.put("view_type", type);
+                    param.put("task_params", taskParams);
+                    String result = HttpUtils.kuaiShouhttpPostRequest(url, param.toJSONString(), headers);
+                    JSONObject resultJson = JSONObject.parseObject(result);
+                    if (!Check.isNull(resultJson)) {
+                        Integer code = resultJson.getInteger("code");
+                        if (code == 0) {
+                            JSONObject dataJson = resultJson.getJSONObject("data");
+                            KuaiShouDailyReportTask dailyReportTask = new KuaiShouDailyReportTask();
+                            dailyReportTask.setAccountId(accountId);
+                            dailyReportTask.setStatDate(startDate);
+                            dailyReportTask.setTaskId(dataJson.getString("task_id"));
+                            dailyReportTask.setViewType(type);
+                            dailyReportTask.setTaskStatus(0);
+                            dailyReportTask.setTaskName(taskName);
+                            dailyReportTaskService.save(dailyReportTask);
+                        } else {
+                            log.error("快手历史报表请求失败,accountId:{},返回数据:{}", accountId, resultJson);
+                        }
+                    }
+                }
+            } else {
+                if (Check.isNull(historyTypeList)) {
+                    getHistoryTypeList();
+                }
+                for (int i = 0; i < historyTypeList.size(); i++) {
+                    Integer type = historyTypeList.get(i);
+                    String taskName = accountId + "_" + type + "_" + System.currentTimeMillis();
+                    log.info("任务名称:{}", taskName);
+                    param.put("task_name", taskName);
+                    taskParams.put("view_type", type);
+                    param.put("task_params", taskParams);
+                    String result = HttpUtils.kuaiShouhttpPostRequest(url, param.toJSONString(), headers);
+                    JSONObject resultJson = JSONObject.parseObject(result);
+                    if (!Check.isNull(resultJson)) {
+                        Integer code = resultJson.getInteger("code");
+                        if (code == 0) {
+                            JSONObject dataJson = resultJson.getJSONObject("data");
+                            KuaiShouHistoryReportTask reportTask = new KuaiShouHistoryReportTask();
+                            reportTask.setAccountId(accountId);
+                            reportTask.setStartDate(startDate);
+                            reportTask.setEndDate(endDate);
+                            reportTask.setTaskId(dataJson.getString("task_id"));
+                            reportTask.setViewType(type);
+                            reportTask.setTaskStatus(0);
+                            reportTask.setTaskName(taskName);
+                            this.save(reportTask);
+                        } else {
+                            log.error("快手历史报表请求失败,accountId:{},返回数据:{}", accountId, resultJson);
+                        }
                     }
                 }
             }
@@ -203,13 +237,20 @@ public class KuaiShouHistoryReportTaskServiceImpl extends ServiceImpl<KuaiShouHi
     }
 
 
-    private void getTypeList() {
-        typeList = new ArrayList<>();
-//        typeList.add(1);
-        typeList.add(2);
-        typeList.add(3);
-        typeList.add(4);
+    private void getHistoryTypeList() {
+        historyTypeList = new ArrayList<>();
+        historyTypeList.add(1);
+        historyTypeList.add(2);
+        historyTypeList.add(3);
+        historyTypeList.add(4);
+    }
 
+    private void getDailyTypeList() {
+        dailyTypeList = new ArrayList<>();
+        dailyTypeList.add(1);
+        dailyTypeList.add(2);
+        dailyTypeList.add(3);
+        dailyTypeList.add(4);
     }
 
 }