|
@@ -42,6 +42,8 @@ import java.io.InputStreamReader;
|
|
import java.math.BigDecimal;
|
|
import java.math.BigDecimal;
|
|
import java.math.RoundingMode;
|
|
import java.math.RoundingMode;
|
|
import java.net.URI;
|
|
import java.net.URI;
|
|
|
|
+import java.sql.Timestamp;
|
|
|
|
+import java.text.ParseException;
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
|
|
|
@Service
|
|
@Service
|
|
@@ -674,18 +676,42 @@ public class BytedanceReportServiceImpl implements IBytedanceReportService {
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////
|
|
//创建异步任务
|
|
//创建异步任务
|
|
- public void bytedanceAsyncTaskCreate () {
|
|
|
|
- String startDate = DateUtils.formatDate(DateUtils.addDay(new Date(), -1));
|
|
|
|
- String endDate = DateUtils.formatDate(DateUtils.addDay(new Date(), -31));
|
|
|
|
- String type = "BYTEDANCE_LOAD_JOB_TYPE_ASYNC";
|
|
|
|
- Long accountId = 1662649623517198L;
|
|
|
|
|
|
+ public void bytedanceAsyncTaskCreate (String startDate, String endDate, CtopOauthToken token) 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);
|
|
|
|
+ }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);
|
|
|
|
+ }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);
|
|
|
|
+ }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);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
- CtopOauthToken token = ctopOauthTokenService.getTokenByAccountId(accountId);
|
|
|
|
|
|
+ /***
|
|
|
|
+ * 由于异步拉取数据不能跨月,故只能按月循环拉取
|
|
|
|
+ * @param startDate
|
|
|
|
+ * @param endDate
|
|
|
|
+ * @param token
|
|
|
|
+ */
|
|
|
|
+ private void bytedanceAsyncTaskCreateByMonth(String startDate, String endDate, CtopOauthToken token){
|
|
String access_token = token.getAccessToken();
|
|
String access_token = token.getAccessToken();
|
|
- Long advertiser_id = accountId;
|
|
|
|
- //final Long[] ad_ids = new Long[]{1L};
|
|
|
|
- //List<String> fields_list = FIELDS;
|
|
|
|
- //ist<String> fields_list = new ArrayList<>();
|
|
|
|
|
|
+ Long accountId = token.getAccountId();
|
|
|
|
|
|
// 请求地址
|
|
// 请求地址
|
|
String open_api_domain = "https://ad.toutiao.com";
|
|
String open_api_domain = "https://ad.toutiao.com";
|
|
@@ -695,16 +721,14 @@ public class BytedanceReportServiceImpl implements IBytedanceReportService {
|
|
// 请求参数
|
|
// 请求参数
|
|
final Map filtering = new HashMap() {
|
|
final Map filtering = new HashMap() {
|
|
{
|
|
{
|
|
- //put("start_date", startDate);
|
|
|
|
- //put("end_date", endDate);
|
|
|
|
- put("start_date", "2020-04-01");
|
|
|
|
- put("end_date", "2020-04-15");
|
|
|
|
|
|
+ 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"}); //按照创意分组、天分组、创意类型分组查询
|
|
put("group_by", new String[]{"STAT_GROUP_BY_CREATIVE_ID","STAT_GROUP_BY_TIME_DAY","STAT_GROUP_BY_CREATIVE_MATERIAL_MODE"}); //按照创意分组、天分组、创意类型分组查询
|
|
}
|
|
}
|
|
};
|
|
};
|
|
- Map data = new HashMap() {
|
|
|
|
|
|
+ final Map data = new HashMap() {
|
|
{
|
|
{
|
|
- put("advertiser_id", advertiser_id);
|
|
|
|
|
|
+ put("advertiser_id", accountId);
|
|
put("task_name", taskName);
|
|
put("task_name", taskName);
|
|
put("task_type", "REPORT");
|
|
put("task_type", "REPORT");
|
|
put("task_params", filtering);
|
|
put("task_params", filtering);
|
|
@@ -760,6 +784,7 @@ public class BytedanceReportServiceImpl implements IBytedanceReportService {
|
|
task.setJson(jsonString);
|
|
task.setJson(jsonString);
|
|
task.setTaskStatus(dataJson.getString("task_status"));
|
|
task.setTaskStatus(dataJson.getString("task_status"));
|
|
task.setTaskType(dataJson.getString("task_type"));
|
|
task.setTaskType(dataJson.getString("task_type"));
|
|
|
|
+ task.setIsDownload(0);
|
|
bytedanceDailyReportTaskMapper.insert(task);
|
|
bytedanceDailyReportTaskMapper.insert(task);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -786,7 +811,9 @@ public class BytedanceReportServiceImpl implements IBytedanceReportService {
|
|
public void bytedanceAsyncTaskGet(){
|
|
public void bytedanceAsyncTaskGet(){
|
|
QueryWrapper<BytedanceDailyReportTask> taskQueryWrapper = new QueryWrapper<>();
|
|
QueryWrapper<BytedanceDailyReportTask> taskQueryWrapper = new QueryWrapper<>();
|
|
//查询三种状态的信息--任务已创建、任务执行中、任务处理中
|
|
//查询三种状态的信息--任务已创建、任务执行中、任务处理中
|
|
- taskQueryWrapper.in("task_status", "ASYNC_TASK_STATUS_CREATED","ASYNC_TASK_STATUS_EXECUTING","ASYNC_TASK_STATUS_EXECUTING_STAGE2");
|
|
|
|
|
|
+ taskQueryWrapper.eq("is_download",0);
|
|
|
|
+ //taskQueryWrapper.in("task_status", "ASYNC_TASK_STATUS_CREATED","ASYNC_TASK_STATUS_EXECUTING","ASYNC_TASK_STATUS_EXECUTING_STAGE2");
|
|
|
|
+ taskQueryWrapper.notIn("task_status", "ASYNC_TASK_STATUS_FAILED");
|
|
taskQueryWrapper.orderByDesc("create_time");
|
|
taskQueryWrapper.orderByDesc("create_time");
|
|
List<BytedanceDailyReportTask> taskList = bytedanceDailyReportTaskService.list(taskQueryWrapper);
|
|
List<BytedanceDailyReportTask> taskList = bytedanceDailyReportTaskService.list(taskQueryWrapper);
|
|
|
|
|
|
@@ -802,7 +829,6 @@ public class BytedanceReportServiceImpl implements IBytedanceReportService {
|
|
String access_token = token.getAccessToken();
|
|
String access_token = token.getAccessToken();
|
|
Long accountId = task.getAccountId();
|
|
Long accountId = task.getAccountId();
|
|
final Long advertiser_id = accountId;
|
|
final Long advertiser_id = accountId;
|
|
- //Long taskId = 682327L;
|
|
|
|
|
|
|
|
// 请求参数
|
|
// 请求参数
|
|
final Map filtering = new HashMap() {
|
|
final Map filtering = new HashMap() {
|
|
@@ -871,6 +897,7 @@ public class BytedanceReportServiceImpl implements IBytedanceReportService {
|
|
|
|
|
|
if (taskStatus .equals( "ASYNC_TASK_STATUS_COMPLETED")) {
|
|
if (taskStatus .equals( "ASYNC_TASK_STATUS_COMPLETED")) {
|
|
bytedanceAsyncTaskDownload(task,token);
|
|
bytedanceAsyncTaskDownload(task,token);
|
|
|
|
+ task.setIsDownload(1);
|
|
}
|
|
}
|
|
task.setTaskStatus(taskStatus);
|
|
task.setTaskStatus(taskStatus);
|
|
bytedanceDailyReportTaskMapper.updateById(task);
|
|
bytedanceDailyReportTaskMapper.updateById(task);
|
|
@@ -908,73 +935,18 @@ public class BytedanceReportServiceImpl implements IBytedanceReportService {
|
|
String open_api_domain = "https://ad.toutiao.com";
|
|
String open_api_domain = "https://ad.toutiao.com";
|
|
String path = "/open_api/2/async_task/download/";
|
|
String path = "/open_api/2/async_task/download/";
|
|
|
|
|
|
- Long accountId = task.getAccountId();
|
|
|
|
- final Long advertiser_id = accountId;
|
|
|
|
- String access_token = token.getAccessToken();
|
|
|
|
- Long taskId = task.getTaskId();
|
|
|
|
-
|
|
|
|
- //Map data = new HashMap(){
|
|
|
|
- // {
|
|
|
|
- // put("advertiser_id", advertiser_id);
|
|
|
|
- // put("task_id", taskId);
|
|
|
|
- // }
|
|
|
|
- //};
|
|
|
|
- //
|
|
|
|
- //// 构造请求
|
|
|
|
- //HttpEntityEnclosingRequestBase httpEntity = new HttpEntityEnclosingRequestBase() {
|
|
|
|
- // @Override
|
|
|
|
- // public String getMethod() {
|
|
|
|
- // return "GET";
|
|
|
|
- // }
|
|
|
|
- //};
|
|
|
|
- //
|
|
|
|
- //httpEntity.setHeader("Access-Token",access_token);
|
|
|
|
- //
|
|
|
|
- //CloseableHttpResponse response = null;
|
|
|
|
- //CloseableHttpClient client = null;
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- try {
|
|
|
|
- //client = HttpClientBuilder.create().build();
|
|
|
|
- //httpEntity.setURI(URI.create(open_api_domain + path));
|
|
|
|
- //httpEntity.setEntity(new StringEntity(JSONObject.toJSONString(data), ContentType.APPLICATION_JSON));
|
|
|
|
- //
|
|
|
|
- //response = client.execute(httpEntity);
|
|
|
|
- //log.info("response为:" + response);
|
|
|
|
- //if (response != null && response.getStatusLine().getStatusCode() == 200) {
|
|
|
|
- // BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
|
|
|
|
- // StringBuffer result = new StringBuffer();
|
|
|
|
- // String line = "";
|
|
|
|
- // while((line = bufferedReader.readLine()) != null) {
|
|
|
|
- // result.append(line);
|
|
|
|
- // }
|
|
|
|
- // bufferedReader.close();
|
|
|
|
-
|
|
|
|
- String url = open_api_domain + path;
|
|
|
|
- String fileName = task.getAccountId() + "_" + ".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);
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- //task.setTaskStatus(5);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return;
|
|
|
|
|
|
+ String url = open_api_domain + path;
|
|
|
|
+ String fileName = task.getAccountId() + "_" + 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);
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
/////////////////////////////////////////////////////////\
|
|
/////////////////////////////////////////////////////////\
|
|
//素材报表
|
|
//素材报表
|
|
public void bytedanceMaterialReportRetry(CtopOauthToken token, String startDate, String endDate){
|
|
public void bytedanceMaterialReportRetry(CtopOauthToken token, String startDate, String endDate){
|
|
- //CtopOauthToken token = ctopOauthTokenService.getTokenByAccountId(accountId);
|
|
|
|
Long accountId = token.getAccountId();
|
|
Long accountId = token.getAccountId();
|
|
log.info("头条素材报表当前accountId为:" + accountId);
|
|
log.info("头条素材报表当前accountId为:" + accountId);
|
|
Integer page = 1;
|
|
Integer page = 1;
|
|
@@ -996,7 +968,6 @@ public class BytedanceReportServiceImpl implements IBytedanceReportService {
|
|
}
|
|
}
|
|
|
|
|
|
public void bytedanceMaterialReport(CtopOauthToken token, String startDate, String endDate){
|
|
public void bytedanceMaterialReport(CtopOauthToken token, String startDate, String endDate){
|
|
- //CtopOauthToken token = ctopOauthTokenService.getTokenByAccountId(accountId);
|
|
|
|
Long accountId = token.getAccountId();
|
|
Long accountId = token.getAccountId();
|
|
log.info("头条素材报表当前accountId为:" + accountId);
|
|
log.info("头条素材报表当前accountId为:" + accountId);
|
|
Integer page = 1;
|
|
Integer page = 1;
|
|
@@ -1232,15 +1203,7 @@ public class BytedanceReportServiceImpl implements IBytedanceReportService {
|
|
return -1;
|
|
return -1;
|
|
}
|
|
}
|
|
}else{
|
|
}else{
|
|
- //BytedanceReportMaterialRetry retry = new BytedanceReportMaterialRetry();
|
|
|
|
- //retry.setAccountId(accountId);
|
|
|
|
- //retry.setStatus(0);
|
|
|
|
- //retry.setStartDate(startDate);
|
|
|
|
- //retry.setEndDate(endDate);
|
|
|
|
- //bytedanceReportMaterialDailyMapper.replaceMaterialRetry(retry);
|
|
|
|
-
|
|
|
|
log.error("头条素材报表请求有误:accountId:" + accountId + ",开始时间:" + startDate + "结束时间:" + endDate + "错误返回:" +response);
|
|
log.error("头条素材报表请求有误:accountId:" + accountId + ",开始时间:" + startDate + "结束时间:" + endDate + "错误返回:" +response);
|
|
-
|
|
|
|
returnCode = 0; //504等状态0;-2连接服务器报错 ;-1对方服务器返回值为空;200正常;1没有数据(也属于正常)
|
|
returnCode = 0; //504等状态0;-2连接服务器报错 ;-1对方服务器返回值为空;200正常;1没有数据(也属于正常)
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|