Ver código fonte

试玩定时任务

hcst_sunzhen 4 anos atrás
pai
commit
5ced620c5f

+ 85 - 0
module-job-bytedance/src/main/java/cn/com/ctop/job/bytedance/handler/BytedanceDailyPlayableReportHistoryJob.java

@@ -0,0 +1,85 @@
+package cn.com.ctop.job.bytedance.handler;
+
+import cn.com.ctop.common.module.entity.CtopOauthToken;
+import cn.com.ctop.common.module.service.ICtopOauthTokenService;
+import cn.com.ctop.toutiao.modules.report.service.IBytedanceInterfaceService;
+import com.xxl.job.core.biz.model.ReturnT;
+import com.xxl.job.core.handler.annotation.XxlJob;
+import com.xxl.job.core.log.XxlJobLogger;
+import org.apache.commons.lang.StringUtils;
+import org.jeecg.common.util.DateUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.util.Date;
+import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+@Component
+public class BytedanceDailyPlayableReportHistoryJob {
+
+    static ExecutorService executorService = Executors.newFixedThreadPool(4);
+    @Autowired
+    private ICtopOauthTokenService tokenService;
+    @Autowired
+    private IBytedanceInterfaceService bytedanceInterfaceService;
+
+    @XxlJob("bytedanceDailyPlayableReportHistoryJob")
+    public ReturnT<String> execute(String param) throws Exception {
+        if(StringUtils.isBlank(param)){
+            XxlJobLogger.log("参数为空");
+            return ReturnT.FAIL;
+        }
+
+        String[] args = param.split(",");
+        String startDate = args[0];
+        String endDate = args[1];
+
+        if (StringUtils.isBlank(startDate) || StringUtils.isBlank(endDate)){
+            XxlJobLogger.log("参数不正确"+ param);
+            return ReturnT.FAIL;
+        }
+        try {
+            XxlJobLogger.log("头条获取试玩报表数据任务执行开始");
+            Long starttime = System.currentTimeMillis();
+            List<CtopOauthToken> tokens = tokenService.selectToutiaoToken();
+            if (null == tokens || tokens.size() <= 0) {
+                XxlJobLogger.log("头条获取试玩报表数据任务执行失败:未获取到可用的token");
+                return ReturnT.FAIL;
+            }
+
+            tokens.forEach(token -> {
+                        executorService.submit(new Runnable() {
+                            @Override
+                            public void run() {
+                                try {
+                                    //间隔天数
+                                    Long days = DateUtils.getDiscrepantDays(startDate, endDate);
+                                    String start = null;
+                                    String end = null;
+                                    for (int i = 0; i <= days; i++) {
+                                        start = DateUtils.addDay(startDate, i);
+                                        end = start;
+                                        //获取头条试玩报表数据
+                                        bytedanceInterfaceService.bytedancePlayableReport(token, start, end);
+                                    }
+                                } catch (Exception e) {
+                                    e.printStackTrace();
+                                }
+                            }
+                        });
+                    }
+            );
+            ////
+            Long endtime = System.currentTimeMillis();
+            XxlJobLogger.log("头条获取素材报表数据任务执行结束,执行耗时:{}秒", (endtime - starttime) / 1000);
+        } catch (Exception e) {
+            XxlJobLogger.log("头条获取素材报表数据任务执行结失败");
+            e.printStackTrace();
+            return ReturnT.FAIL;
+        }
+        return ReturnT.SUCCESS;
+    }
+
+}