Просмотр исходного кода

快手操作记录获取接口

sunzhen 4 лет назад
Родитель
Сommit
46e353ba6d

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

@@ -14,7 +14,10 @@ import cn.com.ctop.toutiao.modules.report.service.IBytedancePlanHourlyReportServ
 import cn.com.ctop.toutiao.modules.report.service.IBytedanceReportService;
 import cn.com.ctop.toutiao.modules.report.service.IReportService;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.extension.api.R;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.ibatis.annotations.Param;
 import org.jeecg.common.api.vo.Result;
 import org.jeecg.common.util.DateUtils;
 import org.quartz.JobExecutionException;
@@ -68,6 +71,9 @@ public class TestController {
     private IMaterialImageInfoService iMaterialImageInfoService;
     @Autowired
     private IBytedanceReportService bytedanceReportService;
+    @Autowired
+    private IKuaishouInterfaceService2 kuaishouInterfaceService2;
+
     static ExecutorService executorService = Executors.newFixedThreadPool(15);
     static ExecutorService videoService = Executors.newFixedThreadPool(5);
     static ExecutorService suzhaoService = Executors.newFixedThreadPool(5);
@@ -856,35 +862,65 @@ public class TestController {
 
 
     @GetMapping(value = "/getOperationRecord")
-    public void getOperationRecord(){
+    public Result getOperationRecord(@Param("accountId")Long accountId, @Param("operationTarget")Integer operationTarget, @Param("startDate")String startDate, @Param("endDate")String endDate){
+        Result result = new Result();
         try {
-            //1:查询当日数据
-            QueryWrapper<CtopOauthToken> queryWrapper = new QueryWrapper<>();
-            queryWrapper.eq("media_id", 2);
-            queryWrapper.orderByDesc("create_time");
-            List<CtopOauthToken> tokens = tokenService.list(queryWrapper);
-            if (null == tokens || tokens.size() <= 0) {
-                log.info("定时获取快手数据异常:未获取到可用的token");
-                return;
+            if(StringUtils.isBlank(startDate) || StringUtils.isBlank(endDate)){
+                result.setSuccess(false);
+                result.setMessage("时间参数不能为空");
+                return result;
             }
 
-            tokens.forEach(token -> {
+            if(operationTarget == null){
+                result.setSuccess(false);
+                result.setMessage("类型不能为空");
+                return result;
+            }
 
-               // kuaishouInterfaceService.getOperationRecordByPage(token.getAccountId(),token.getAccessToken());
-                //suzhaoService.submit(new Runnable() {
-                //    @Override
-                //    public void run() {
-                //        //1: 获取广告主信息数据
-                //        log.info("获取素造素材:accountId:{}", token.getAccountId());
-                //        kuaishouInterfaceService.getSuZaoList(token.getAccessToken(), token.getAccountId(), 1, null, null);
-                //    }
+            if(accountId == null){
+                //1:查询当日数据
+                QueryWrapper<CtopOauthToken> queryWrapper = new QueryWrapper<>();
+                queryWrapper.eq("media_id", 2);
+                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(3);
+                tokens.forEach(token -> {
+                    //    executorService.submit(new Runnable() {
+                    //        @Override
+                    //        public void run() {
+                    log.info("获取操作记录开始:accountId:{},startDate:{},endDate:{}", token.getAccountId(), startDate, endDate);
+                    kuaishouInterfaceService2.getOperationRecord(token.getAccountId(), token.getAccessToken(), operationTarget, startDate, endDate);
+                    log.info("获取操作记录开始:accountId:{}", token.getAccountId());
+                    // }
+                });
                 //});
-            });
+            }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;
+                }
+                kuaishouInterfaceService2.getOperationRecord(token.getAccountId(), token.getAccessToken(), operationTarget, startDate, endDate);
+
+            }
+
+
         } catch (Exception e) {
             e.printStackTrace();
         }
 
-
+        return result;
     }
 
 

+ 2 - 0
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/batch/entity/KuaishouOperationRecord.java

@@ -36,4 +36,6 @@ public class KuaishouOperationRecord {
 	private String originalData;
 
 	private String updateData;
+
+	private String statDate;
 }

+ 77 - 21
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/batch/mapper/xml/KuaishouOperationRecordMapper.xml

@@ -7,7 +7,10 @@
         from
         ctop_kuaishou_operation_record_account
         where
-        account_id = #{record.account_id}
+        account_id = #{record.accountId}
+        and
+        stat_date = #{record.statDate}
+        <!--
         and
         object_id = #{record.objectId}
         and
@@ -18,6 +21,7 @@
         role_type = #{record.roleType}
         and
         operation_time = #{record.operationTime}
+        -->
     </delete>
 
     <insert id="insertOperationRecordInfoAccount">
@@ -25,6 +29,7 @@
         ctop_kuaishou_operation_record_account
         (
             account_id,
+            stat_date,
             operation_time,
             operation_type,
             role_type,
@@ -32,12 +37,14 @@
             operation_target,
             object_id,
             field_name,
-            original_data
+            original_data,
+            update_data
         )
         values
         <foreach item="record" index="index" collection="records"  separator="," >
             (
                 #{record.accountId},
+                #{record.statDate},
                 #{record.operationTime},
                 #{record.operationType},
                 #{record.roleType},
@@ -45,7 +52,8 @@
                 #{record.operationTarget},
                 #{record.objectId},
                 #{record.fieldName},
-                #{record.originalData}
+                #{record.originalData},
+                #{record.updateData}
             )
         </foreach>
 
@@ -60,7 +68,10 @@
         from
         ctop_kuaishou_operation_record_campaign
         where
-        account_id = #{record.account_id}
+        account_id = #{record.accountId}
+        and
+        stat_date = #{record.statDate}
+     <!--
         and
         object_id = #{record.objectId}
         and
@@ -71,6 +82,7 @@
         role_type = #{record.roleType}
         and
         operation_time = #{record.operationTime}
+        -->
     </delete>
 
     <insert id="insertOperationRecordInfoCampaign">
@@ -78,6 +90,7 @@
         ctop_kuaishou_operation_record_campaign
         (
         account_id,
+        stat_date,
         operation_time,
         operation_type,
         role_type,
@@ -85,12 +98,14 @@
         operation_target,
         object_id,
         field_name,
-        original_data
+        original_data,
+        update_data
         )
         values
         <foreach item="record" index="index" collection="records"  separator="," >
             (
             #{record.accountId},
+            #{record.statDate},
             #{record.operationTime},
             #{record.operationType},
             #{record.roleType},
@@ -98,7 +113,8 @@
             #{record.operationTarget},
             #{record.objectId},
             #{record.fieldName},
-            #{record.originalData}
+            #{record.originalData},
+            #{record.updateData}
             )
         </foreach>
 
@@ -110,7 +126,10 @@
         from
         ctop_kuaishou_operation_record_unit
         where
-        account_id = #{record.account_id}
+        account_id = #{record.accountId}
+        and
+        stat_date = #{record.statDate}
+       <!--
         and
         object_id = #{record.objectId}
         and
@@ -121,6 +140,7 @@
         role_type = #{record.roleType}
         and
         operation_time = #{record.operationTime}
+        -->
     </delete>
 
     <insert id="insertOperationRecordInfoUnit">
@@ -128,6 +148,7 @@
         ctop_kuaishou_operation_record_unit
         (
         account_id,
+        stat_date,
         operation_time,
         operation_type,
         role_type,
@@ -135,12 +156,14 @@
         operation_target,
         object_id,
         field_name,
-        original_data
+        original_data,
+        update_data
         )
         values
         <foreach item="record" index="index" collection="records"  separator="," >
             (
             #{record.accountId},
+            #{record.statDate},
             #{record.operationTime},
             #{record.operationType},
             #{record.roleType},
@@ -148,7 +171,8 @@
             #{record.operationTarget},
             #{record.objectId},
             #{record.fieldName},
-            #{record.originalData}
+            #{record.originalData},
+            #{record.updateData}
             )
         </foreach>
 
@@ -162,7 +186,10 @@
         from
         ctop_kuaishou_operation_record_creative
         where
-        account_id = #{record.account_id}
+        account_id = #{record.accountId}
+        and
+        stat_date = #{record.statDate}
+        <!--
         and
         object_id = #{record.objectId}
         and
@@ -173,6 +200,7 @@
         role_type = #{record.roleType}
         and
         operation_time = #{record.operationTime}
+        -->
     </delete>
 
     <insert id="insertOperationRecordInfoCreative">
@@ -180,6 +208,7 @@
         ctop_kuaishou_operation_record_creative
         (
         account_id,
+        stat_date,
         operation_time,
         operation_type,
         role_type,
@@ -187,12 +216,14 @@
         operation_target,
         object_id,
         field_name,
-        original_data
+        original_data,
+        update_data
         )
         values
         <foreach item="record" index="index" collection="records"  separator="," >
             (
             #{record.accountId},
+            #{record.statDate},
             #{record.operationTime},
             #{record.operationType},
             #{record.roleType},
@@ -200,7 +231,8 @@
             #{record.operationTarget},
             #{record.objectId},
             #{record.fieldName},
-            #{record.originalData}
+            #{record.originalData},
+            #{record.updateData}
             )
         </foreach>
 
@@ -213,7 +245,10 @@
         from
         ctop_kuaishou_operation_record_video
         where
-        account_id = #{record.account_id}
+        account_id = #{record.accountId}
+        and
+        stat_date = #{record.statDate}
+        <!--
         and
         object_id = #{record.objectId}
         and
@@ -224,6 +259,7 @@
         role_type = #{record.roleType}
         and
         operation_time = #{record.operationTime}
+        -->
     </delete>
 
     <insert id="insertOperationRecordInfoVideo">
@@ -231,6 +267,7 @@
         ctop_kuaishou_operation_record_video
         (
         account_id,
+        stat_date,
         operation_time,
         operation_type,
         role_type,
@@ -238,12 +275,14 @@
         operation_target,
         object_id,
         field_name,
-        original_data
+        original_data,
+        update_data
         )
         values
         <foreach item="record" index="index" collection="records"  separator="," >
             (
             #{record.accountId},
+            #{record.statDate},
             #{record.operationTime},
             #{record.operationType},
             #{record.roleType},
@@ -251,7 +290,8 @@
             #{record.operationTarget},
             #{record.objectId},
             #{record.fieldName},
-            #{record.originalData}
+            #{record.originalData},
+            #{record.updateData}
             )
         </foreach>
 
@@ -265,7 +305,10 @@
         from
         ctop_kuaishou_operation_record_app
         where
-        account_id = #{record.account_id}
+        account_id = #{record.accountId}
+        and
+        stat_date = #{record.statDate}
+        <!--
         and
         object_id = #{record.objectId}
         and
@@ -276,6 +319,7 @@
         role_type = #{record.roleType}
         and
         operation_time = #{record.operationTime}
+        -->
     </delete>
 
     <insert id="insertOperationRecordInfoApp">
@@ -283,6 +327,7 @@
         ctop_kuaishou_operation_record_app
         (
         account_id,
+        stat_date,
         operation_time,
         operation_type,
         role_type,
@@ -290,12 +335,14 @@
         operation_target,
         object_id,
         field_name,
-        original_data
+        original_data,
+        update_data
         )
         values
         <foreach item="record" index="index" collection="records"  separator="," >
             (
             #{record.accountId},
+            #{record.statDate},
             #{record.operationTime},
             #{record.operationType},
             #{record.roleType},
@@ -303,7 +350,8 @@
             #{record.operationTarget},
             #{record.objectId},
             #{record.fieldName},
-            #{record.originalData}
+            #{record.originalData},
+            #{record.updateData}
             )
         </foreach>
 
@@ -316,7 +364,10 @@
         from
         ctop_kuaishou_operation_record_package
         where
-        account_id = #{record.account_id}
+        account_id = #{record.accountId}
+        and
+        stat_date = #{record.statDate}
+        <!--
         and
         object_id = #{record.objectId}
         and
@@ -327,6 +378,7 @@
         role_type = #{record.roleType}
         and
         operation_time = #{record.operationTime}
+        -->
     </delete>
 
     <insert id="insertOperationRecordInfoPackage">
@@ -334,6 +386,7 @@
         ctop_kuaishou_operation_record_package
         (
         account_id,
+        stat_date,
         operation_time,
         operation_type,
         role_type,
@@ -341,12 +394,14 @@
         operation_target,
         object_id,
         field_name,
-        original_data
+        original_data,
+        update_data
         )
         values
         <foreach item="record" index="index" collection="records"  separator="," >
             (
             #{record.accountId},
+            #{record.statDate},
             #{record.operationTime},
             #{record.operationType},
             #{record.roleType},
@@ -354,7 +409,8 @@
             #{record.operationTarget},
             #{record.objectId},
             #{record.fieldName},
-            #{record.originalData}
+            #{record.originalData},
+            #{record.updateData}
             )
         </foreach>
 

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

@@ -3,7 +3,7 @@ package cn.com.ctop.kuaishou.modules.batch.service;
 
 public interface IKuaishouInterfaceService2 {
 
-    //void getOperationRecordByPage(Long advertiserId, String accessToken);
+    void getOperationRecord(Long advertiserId, String accessToken, Integer operationTarget, String startDate, String endDate);
 }
 
 

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

@@ -11,6 +11,7 @@ import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.collections.ArrayStack;
+import org.jeecg.common.util.DateUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -23,28 +24,32 @@ public class KuaishouInterfaceServiceImpl2 implements IKuaishouInterfaceService2
     @Autowired
     private KuaishouOperationRecordMapper kuaishouOperationRecordMapper;
 
+    @Override
+    public void getOperationRecord(Long advertiserId, String accessToken, Integer operationTarget, String startDate, String endDate){
 
-    public void getOperationRecord(){
-
-
+        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);
+        }
     }
 
-    private void getOperationRecordByPage(Long advertiserId, String accessToken, Integer page, Integer pageSize, Integer operationTarget) {
+    private void getOperationRecordByPage(Long advertiserId, String accessToken, Integer page, Integer pageSize, Integer operationTarget, String startDate, String endDate) {
         String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.OPERATION_RECORD;
         Map<String, String> headers = new HashMap<>();
         headers.put("Access-Token", accessToken);
         headers.put("Content-Type", " application/json");
         JSONObject json = new JSONObject();
         json.put("advertiser_id", advertiserId);
-        //if(operationType != null){
-        //    json.put("operation_type", operationType);
-        //}
-        if(operationTarget != null){
-            json.put("operation_target", operationTarget);
-        }
-
+        json.put("operation_target", operationTarget);
         json.put("page", page);
         json.put("page_size", pageSize);
+        json.put("start_date", startDate);
+        json.put("end_date", endDate);
+
         String result = HttpUtils.kuaiShouhttpPostRequest(url, json.toJSONString(), headers);
         JSONObject resultJson = JSONObject.parseObject(result);
 
@@ -68,15 +73,16 @@ public class KuaishouInterfaceServiceImpl2 implements IKuaishouInterfaceService2
                             String objectId = dataJson.getString("object_id");
                             String objectName = dataJson.getString("object_name");
 
-                            if (page == 1) { //如果页数为1,删除原来的数据
+                            if (page == 1 && i==0) { //如果页数为1,删除原来的数据
                                 KuaishouOperationRecord recordDelete = new KuaishouOperationRecord();
+                                recordDelete.setStatDate(startDate);//按天删除记录
                                 recordDelete.setAccountId(advertiserId);
-                                recordDelete.setOperationTime(operationTime);
-                                recordDelete.setOperationType(operationType);
-                                recordDelete.setRoleType(roleType);
-                                recordDelete.setObjectName(objectName);
-                                recordDelete.setOperationTarget(operationTarget);
-                                recordDelete.setObjectId(objectId);
+                                //recordDelete.setOperationTime(operationTime);
+                                //recordDelete.setOperationType(operationType);
+                                //recordDelete.setRoleType(roleType);
+                                //recordDelete.setObjectName(objectName);
+                                //recordDelete.setOperationTarget(operationTarget);
+                                //recordDelete.setObjectId(objectId);
                                 operationRecordDelete(recordDelete,operationTarget);
                             }
 
@@ -86,6 +92,7 @@ public class KuaishouInterfaceServiceImpl2 implements IKuaishouInterfaceService2
                                 JSONObject log = contentLogArray.getJSONObject(j);
                                 KuaishouOperationRecord record = new KuaishouOperationRecord();
                                 record.setAccountId(advertiserId);
+                                record.setStatDate(startDate);
                                 record.setOperationTime(operationTime);
                                 record.setOperationType(operationType);
                                 record.setRoleType(roleType);
@@ -100,16 +107,22 @@ public class KuaishouInterfaceServiceImpl2 implements IKuaishouInterfaceService2
                             }
                         }
                     }
-                    operationRecordInsert(recordList,operationTarget);
+                    if(recordList !=null && recordList.size()!=0){
+                        operationRecordInsert(recordList,operationTarget);
+                    }
 
                     Integer totalPage = (totalCount - 1) / pageSize + 1;
                     if (page < totalPage) {
-                        getOperationRecordByPage(advertiserId, accessToken, page + 1, pageSize, operationTarget);
+                        getOperationRecordByPage(advertiserId, accessToken, page + 1, pageSize, operationTarget, startDate, endDate);
                     }
+                }else{
+                    log.error("快手操作记录接口data结果不正确,{} ~ {}, resultJson:{}", startDate, endDate, result);
                 }
+            }else{
+                log.error("快手操作记录接口code返回结果不正确,{} ~ {}, resultJson:{}", startDate, endDate, result);
             }
         } else {
-            log.error("获取地域列表返回结果为空");
+            log.error("快手操作记录接口获取地域列表返回结果为空,{} ~ {}, resultJson:{}", startDate, endDate, result);
         }
 
     }
@@ -148,9 +161,6 @@ public class KuaishouInterfaceServiceImpl2 implements IKuaishouInterfaceService2
         }else if(operationTarget == 7){
             kuaishouOperationRecordMapper.insertOperationRecordInfoPackage(recordList);
         }
-
-
-
     }