Bladeren bron

头条操作记录

hcst_sunzhen 4 jaren geleden
bovenliggende
commit
34b0748d9d

+ 51 - 23
jeecg-boot-module-system/src/main/java/org/jeecg/modules/ctop/controller/TestController.java

@@ -884,7 +884,7 @@ public class TestController {
                     return result;
                 }
 
-                executorService = Executors.newFixedThreadPool(3);
+                executorService = Executors.newFixedThreadPool(8);
                 tokens.forEach(token -> {
                     executorService.submit(new Runnable() {
                         @Override
@@ -905,8 +905,9 @@ public class TestController {
                     result.setMessage("定时获取快手操作记录数据异常:未获取到可用的toke");
                     return result;
                 }
+                log.info("获取快手操作记录开始:accountId:{},startDate:{},endDate:{}", token.getAccountId(), startDate, endDate);
                 kuaishouInterfaceService2.getOperationRecord(token.getAccountId(), token.getAccessToken(), operationTarget, startDate, endDate);
-
+                log.info("获取快手操作记录结束:accountId:{},startDate:{},endDate:{}", token.getAccountId(), startDate, endDate);
             }
 
 
@@ -917,27 +918,54 @@ public class TestController {
         return result;
     }
 
-    //@Autowired
-    //public IBytedanceInterfaceService bytedanceInterfaceService;
-    //
-    //@GetMapping(value = "/searchLog")
-    //public Result searchLog(@Param("accountId")Long accountId, @Param("startDate")String startDate, @Param("endDate")String endDate, @Param("operationTarget")Integer operationTarget) {
-    //    Result result = new Result();
-    //
-    //    QueryWrapper<CtopOauthToken> queryWrapper = new QueryWrapper<>();
-    //    queryWrapper.eq("account_id", accountId);
-    //    CtopOauthToken token = tokenService.getOauthTokenByAccountId(String.valueOf(accountId));
-    //    if (null == token) {
-    //        log.error("定时获取快手操作记录数据异常:未获取到可用的token");
-    //        result.setSuccess(false);
-    //        result.setMessage("定时获取快手操作记录数据异常:未获取到可用的toke");
-    //        return result;
-    //    }
-    //
-    //    bytedanceInterfaceService.searchLog(accountId, token, operationTarget,startDate, endDate);
-    //
-    //    return result;
-    //}
+    @Autowired
+    public IBytedanceInterfaceService bytedanceInterfaceService;
+
+    @GetMapping(value = "/searchLog")
+    public Result searchLog(@Param("accountId")Long accountId, @Param("startDate")String startDate, @Param("endDate")String endDate, @Param("operationTarget")Integer operationTarget) {
+        Result result = new Result();
+
+        if(accountId == null){
+            //1:查询当日数据
+            QueryWrapper<CtopOauthToken> queryWrapper = new QueryWrapper<>();
+            queryWrapper.eq("media_id", 1);
+            queryWrapper.orderByDesc("create_time");
+            List<CtopOauthToken> tokens = tokenService.list(queryWrapper);
+            if (null == tokens || tokens.size() <= 0) {
+                log.error("定时获取快手操作记录数据异常:未获取到可用的token");
+                result.setSuccess(false);
+                result.setMessage("定时获取快手操作记录数据异常:未获取到可用的toke");
+                return result;
+            }
+
+            executorService = Executors.newFixedThreadPool(8);
+            tokens.forEach(token -> {
+                executorService.submit(new Runnable() {
+                    @Override
+                    public void run() {
+                        log.info("获取操作记录开始:accountId:{},startDate:{},endDate:{}", token.getAccountId(), startDate, endDate);
+                        bytedanceInterfaceService.searchLog(token.getAccountId(), token, operationTarget,startDate, endDate);
+                        log.info("获取快手操作记录结束:accountId:{},startDate:{},endDate:{}", token.getAccountId(), startDate, endDate);
+                    }
+                });
+            });
+        }else{
+            QueryWrapper<CtopOauthToken> queryWrapper = new QueryWrapper<>();
+            queryWrapper.eq("account_id", accountId);
+            CtopOauthToken token = tokenService.getOauthTokenByAccountId(String.valueOf(accountId));
+            if (null == token) {
+                log.error("定时获取头条操作记录数据异常:未获取到可用的token");
+                result.setSuccess(false);
+                result.setMessage("定时获取头条操作记录数据异常:未获取到可用的toke");
+                return result;
+            }
+            log.info("获取快手操作记录开始:accountId:{},startDate:{},endDate:{}", token.getAccountId(), startDate, endDate);
+            bytedanceInterfaceService.searchLog(accountId, token, operationTarget,startDate, endDate);
+            log.info("获取快手操作记录结束:accountId:{},startDate:{},endDate:{}", token.getAccountId(), startDate, endDate);
+        }
+
+        return result;
+    }
 
 
 }

+ 7 - 6
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/batch/service/impl/KuaishouInterfaceServiceImpl2.java

@@ -30,11 +30,11 @@ public class KuaishouInterfaceServiceImpl2 implements IKuaishouInterfaceService2
         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;
-            getOperationRecordByPage(advertiserId, accessToken,1, 500, operationTarget, start, end);
-        }
+        //for (int i = 0; i <= days; i++) {
+        //    start = DateUtils.addDay(startDate, i);
+        //    end = start;
+            getOperationRecordByPage(advertiserId, accessToken,1, 500, operationTarget, startDate, endDate);
+        //}
     }
 
     private void getOperationRecordByPage(Long advertiserId, String accessToken, Integer page, Integer pageSize, Integer operationTarget, String startDate, String endDate) {
@@ -67,6 +67,7 @@ public class KuaishouInterfaceServiceImpl2 implements IKuaishouInterfaceService2
                         if (!Check.isNull(dataJson)) {
 
                             Date operationTime = dataJson.getDate("operation_time");
+                            //Date operationDate = DateUtils.parseDate()
                             Integer operationType = dataJson.getInteger("operation_type");
                             operationTarget = dataJson.getInteger("operation_target");
                             Integer roleType = dataJson.getInteger("role_type");
@@ -94,7 +95,7 @@ public class KuaishouInterfaceServiceImpl2 implements IKuaishouInterfaceService2
 
                                 KuaishouOperationRecord record = new KuaishouOperationRecord();
                                 record.setAccountId(advertiserId);
-                                record.setStatDate(startDate);
+                                record.setStatDate(DateUtils.formatDate(operationTime));
                                 record.setOperationTime(operationTime);
                                 record.setOperationType(operationType);
                                 record.setRoleType(roleType);

+ 22 - 0
module-toutiao/src/main/java/cn/com/ctop/toutiao/modules/material/entity/ByteDanceOperationRecord.java

@@ -0,0 +1,22 @@
+package cn.com.ctop.toutiao.modules.material.entity;
+
+import lombok.Data;
+
+import java.util.Date;
+
+@Data
+public class ByteDanceOperationRecord {
+
+    private Long accountId;
+    private Long objectId;
+    private String contentTitle;
+    private String objectType;
+    private String objectName;
+    private Date operationCreateTime;
+    private String contentLog;
+    private String operator;
+    private String optIp;
+    private Integer operationTarget;
+    private String statDate;
+
+}

+ 19 - 0
module-toutiao/src/main/java/cn/com/ctop/toutiao/modules/report/mapper/BytedanceOperationRecordMapper.java

@@ -0,0 +1,19 @@
+package cn.com.ctop.toutiao.modules.report.mapper;
+
+import cn.com.ctop.toutiao.modules.material.entity.ByteDanceOperationRecord;
+import cn.com.ctop.toutiao.modules.report.DTO.ByteDanceReportAccountDailyDTO;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
+
+public interface BytedanceOperationRecordMapper {
+
+    void insertOperationRecordCampaign(@Param("records") List<ByteDanceOperationRecord> records);
+
+    void insertOperationRecordPlan(@Param("records") List<ByteDanceOperationRecord> records);
+
+    void insertOperationRecordCreative(@Param("records") List<ByteDanceOperationRecord> records);
+
+}

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

@@ -0,0 +1,110 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="cn.com.ctop.toutiao.modules.report.mapper.BytedanceOperationRecordMapper">
+
+    <select id="insertOperationRecordCampaign">
+        insert into
+        ctop_bytedance_operation_record_campaign
+        (
+            account_id,
+            object_id,
+            object_type,
+            content_title,
+            object_name,
+            operation_create_time,
+            content_log,
+            operator,
+            opt_ip,
+            operation_target
+        )
+        values
+        <foreach collection="reports" item="report" separator=",">
+            (
+            #{record.accountId},
+            #{record.objectId},
+            #{record.objectType},
+            #{record.contentTitle},
+            #{record.objectName},
+            #{record.operationCreateTime},
+            #{record.contentLog},
+            #{record.operator},
+            #{record.type},
+            #{record.operationTarget}
+            )
+        </foreach>
+    </select>
+
+    <select id="insertOperationRecordPlan">
+        insert into
+        ctop_bytedance_operation_record_plan
+        (
+            account_id,
+            object_id,
+            object_type,
+            content_title,
+            object_name,
+            operation_create_time,
+            content_log,
+            operator,
+            opt_ip,
+            operation_target
+        )
+        values
+        <foreach collection="reports" item="report" separator=",">
+            (
+            #{record.accountId},
+            #{record.objectId},
+            #{record.objectType},
+            #{record.contentTitle},
+            #{record.objectName},
+            #{record.operationCreateTime},
+            #{record.contentLog},
+            #{record.operator},
+            #{record.type},
+            #{record.operationTarget}
+            )
+        </foreach>
+    </select>
+
+    <select id="insertOperationRecordCreative">
+        insert into
+        ctop_bytedance_operation_record_creative
+        (
+        account_id,
+        object_id,
+        object_type,
+        content_title,
+        object_name,
+        operation_create_time,
+        content_log,
+        operator,
+        opt_ip,
+        operation_target
+        )
+        values
+        <foreach collection="reports" item="report" separator=",">
+            (
+            #{record.accountId},
+            #{record.objectId},
+            #{record.objectType},
+            #{record.contentTitle},
+            #{record.objectName},
+            #{record.operationCreateTime},
+            #{record.contentLog},
+            #{record.operator},
+            #{record.type},
+            #{record.operationTarget}
+            )
+        </foreach>
+    </select>
+
+
+
+
+
+
+
+
+
+
+</mapper>

+ 9 - 0
module-toutiao/src/main/java/cn/com/ctop/toutiao/modules/report/service/IBytedanceInterfaceService.java

@@ -0,0 +1,9 @@
+package cn.com.ctop.toutiao.modules.report.service;
+
+import cn.com.ctop.common.module.entity.CtopOauthToken;
+
+public interface IBytedanceInterfaceService {
+
+    void searchLog(Long accountId, CtopOauthToken token, Integer operationType, String startDate, String endDate);
+
+}

+ 172 - 0
module-toutiao/src/main/java/cn/com/ctop/toutiao/modules/report/service/impl/BytedanceInterfaceServiceImpl.java

@@ -0,0 +1,172 @@
+package cn.com.ctop.toutiao.modules.report.service.impl;
+
+import cn.com.ctop.common.module.entity.CtopOauthToken;
+import cn.com.ctop.common.module.entity.MaterialParameter;
+import cn.com.ctop.common.module.utils.HttpUtils;
+import cn.com.ctop.toutiao.modules.material.entity.ByteDanceAdvertisePlan;
+import cn.com.ctop.toutiao.modules.material.entity.ByteDanceCampaign;
+import cn.com.ctop.toutiao.modules.material.entity.ByteDanceCreative;
+import cn.com.ctop.toutiao.modules.material.entity.ByteDanceOperationRecord;
+import cn.com.ctop.toutiao.modules.material.service.IByteDanceAdvertisePlanService;
+import cn.com.ctop.toutiao.modules.material.service.IByteDanceCampaignService;
+import cn.com.ctop.toutiao.modules.material.service.IByteDanceCreativeService;
+import cn.com.ctop.toutiao.modules.report.entity.BytedancePlanDailyReport;
+import cn.com.ctop.toutiao.modules.report.mapper.BytedanceOperationRecordMapper;
+import cn.com.ctop.toutiao.modules.report.service.IBytedanceInterfaceService;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import lombok.extern.slf4j.Slf4j;
+import org.jeecg.common.util.DateUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Primary;
+import org.springframework.stereotype.Service;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+@Service
+@Slf4j
+@Primary
+public class BytedanceInterfaceServiceImpl implements IBytedanceInterfaceService {
+    @Autowired
+    private IByteDanceCampaignService byteDanceCampaignService;
+    @Autowired
+    private IByteDanceAdvertisePlanService byteDanceAdvertisePlanService;
+    @Autowired
+    private BytedanceOperationRecordMapper bytedanceOperationRecordMapper;
+    @Autowired
+    private IByteDanceCreativeService byteDanceCreativeService;
+
+    @Override
+    public void searchLog(Long accountId, CtopOauthToken token, Integer operationTarget, String startDate, String endDate){
+
+        //int campaignCount = byteDanceCampaignService.count(parameterQueryWrapper);
+        //int campaignPageSize = 20;
+        //int campaignPage = 1;
+        //
+        //Integer campaignTotalPage = (campaignCount - 1) / campaignPageSize + 1;
+        //if (campaignPage < campaignTotalPage) {
+        //    QueryWrapper<ByteDanceCampaign> parameterQueryWrapper2 = new QueryWrapper<>();
+        //    parameterQueryWrapper2.eq("account_id", accountId);
+        //    parameterQueryWrapper2.last("limit" +  (campaignPage-1) +  "," + campaignPageSize);
+        //    parameterQueryWrapper2.select("id");
+        //
+        //}
+        int pageNum = 1;
+        int pageSize = 20;
+        String startTime = startDate + " 00:00:00";
+        String endTime = endDate + " 23:59:59";
+
+        try {
+            if(operationTarget == 2){
+                QueryWrapper<ByteDanceCampaign> parameterQueryWrapper = new QueryWrapper<>();
+                parameterQueryWrapper.eq("account_id", accountId);
+                parameterQueryWrapper.select("id");
+                List<ByteDanceCampaign> list = byteDanceCampaignService.list(parameterQueryWrapper);
+                for (ByteDanceCampaign campaign:list){
+                    logSearchByPage(pageNum, pageSize, token, accountId, campaign.getId(), 2, startTime, endTime);  //组
+                }
+            }else if(operationTarget == 3){
+                QueryWrapper<ByteDanceAdvertisePlan> parameterQueryWrapper = new QueryWrapper<>();
+                parameterQueryWrapper.eq("account_id", accountId);
+                parameterQueryWrapper.select("id");
+                List<ByteDanceAdvertisePlan> list = byteDanceAdvertisePlanService.list(parameterQueryWrapper);
+                for (ByteDanceAdvertisePlan plan:list){
+                    logSearchByPage(pageNum, pageSize, token, accountId, plan.getId(), 3, startTime, endTime);  //计划
+                }
+            }else if(operationTarget == 4){
+                QueryWrapper<ByteDanceCreative> parameterQueryWrapper = new QueryWrapper<>();
+                parameterQueryWrapper.eq("account_id", accountId);
+                parameterQueryWrapper.select("id");
+                List<ByteDanceCreative> list = byteDanceCreativeService.list(parameterQueryWrapper);
+                for(ByteDanceCreative creative:list){
+                    logSearchByPage(pageNum, pageSize, token, accountId, creative.getId(), 4, startTime, endTime);  //创意
+                }
+            }
+        }catch (Exception e){
+            log.error("获取头条操作记录失败:accountId:{},startDate:{},endDate:{}", token.getAccountId(), startDate, endDate);
+            log.error(e.getMessage());
+        }
+    }
+
+    private void logSearchByPage(Integer pageNum, Integer pageSize, CtopOauthToken token, Long accountId, Long objectId, Integer operationType, String startTime, String endTime) {
+        log.info("当前页数:" + pageNum);
+        String access_token = token.getAccessToken();
+
+        // 请求地址
+        String open_api_domain = "https://ad.oceanengine.com";
+        String path = "/open_api/2/tools/log_search/";
+
+        // 请求参数
+        Map data = new HashMap();
+        data.put("advertiser_id", accountId);
+        data.put("start_time", startTime);
+        data.put("end_time", endTime);
+        data.put("page", pageNum);
+        data.put("page_size", pageSize);
+
+        List<Long> list = new ArrayList<>();
+        list.add(objectId);
+        data.put("object_id", list);
+
+        JSONObject json = HttpUtils.bytedanceGetRequest(access_token, open_api_domain + path, JSONObject.parseObject(JSONObject.toJSONString(data)));
+        if (json == null) {
+            log.error("头条日志查询返回为空,请求有误:" + JSONObject.toJSONString(data));
+            return ;
+        }
+
+        int code =  json.getInteger("code");
+        if(code == 0){
+            List<ByteDanceOperationRecord>  records = new ArrayList<>();
+
+            JSONObject dataResultJson = json.getJSONObject("data");
+            JSONObject pageInfoJson = dataResultJson.getJSONObject("page_info");
+            int totalPage = pageInfoJson.getInteger("total_number");
+            int page = pageInfoJson.getInteger("page");
+
+            JSONArray logsArray = dataResultJson.getJSONArray("logs");
+            int logsSize = logsArray.size();
+            for (int i=0; i<logsSize ;i++){
+                JSONObject logJson =  logsArray.getJSONObject(i);
+                ByteDanceOperationRecord record = new ByteDanceOperationRecord();
+                record.setAccountId(accountId);
+                record.setContentTitle(logJson.getString("content_title"));
+                record.setContentLog(logJson.getString("content_log"));
+                record.setObjectId(logJson.getLong("object_id"));
+                record.setObjectName(logJson.getString("object_name"));
+                record.setObjectType(logJson.getString("object_type"));
+                record.setOperationCreateTime(logJson.getDate("create_time"));
+                record.setStatDate(DateUtils.formatDate(logJson.getDate("create_time")));
+                record.setOperator(logJson.getString("operator"));
+                record.setOptIp(logJson.getString("opt_id"));
+                record.setOperationTarget(operationType);
+
+                records.add(record);
+            }
+
+            //按类型入库
+            insertLog(operationType, records);
+
+            if(page < totalPage){
+                logSearchByPage(page+1, pageSize, token, accountId, objectId, operationType, startTime, endTime);
+            }
+        }else{
+            log.error("头条日志查询返回为空,请求有误:" + JSONObject.toJSONString(data),";;;返回值为:"+ json);
+            return ;
+        }
+    }
+
+    private void insertLog(Integer operationTarget, List<ByteDanceOperationRecord> records){
+        if(operationTarget == 2){
+            bytedanceOperationRecordMapper.insertOperationRecordCampaign(records);
+        }else if(operationTarget == 3){
+            bytedanceOperationRecordMapper.insertOperationRecordPlan(records);
+        }else if(operationTarget == 4){
+            bytedanceOperationRecordMapper.insertOperationRecordCreative(records);
+        }
+    }
+
+}