zhaoxian преди 1 година
родител
ревизия
536a839a87

+ 2 - 4
job-kuaishou/src/main/java/cn/com/ctop/job/kuaishou/data/constant/KuaishouConstant.java

@@ -15,8 +15,6 @@ public class KuaishouConstant {
     public static final String VIDEO_LIST = "/rest/openapi/v1/file/ad/video/list";
 
 
-
-
     //获取广告组信息
     public static final String AD_UNIT_LIST = "/rest/openapi/v1/ad_unit/list";
 
@@ -38,8 +36,8 @@ public class KuaishouConstant {
     //快手账户操作记录
     public static final String OPERATION_RECORD_LIST = "/rest/openapi/v1/tool/operation_record/list";
 
-
-
+    //视频关联创意数查询
+    public static final String VIDEO_RELATE_CREATIVES = "/rest/openapi/v1/file/ad/video/relate/creatives";
 
 
 }

+ 9 - 0
job-kuaishou/src/main/java/cn/com/ctop/job/kuaishou/data/mapper/KuaishouVideoListMapper.java

@@ -1,6 +1,7 @@
 package cn.com.ctop.job.kuaishou.data.mapper;
 
 import cn.com.ctop.job.kuaishou.data.entity.KuaishouVideoList;
+import com.alibaba.fastjson.JSONArray;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import org.apache.ibatis.annotations.Param;
 
@@ -16,4 +17,12 @@ import java.util.List;
 public interface KuaishouVideoListMapper extends BaseMapper<KuaishouVideoList> {
 
     void replaceBatch(@Param("addList") List<KuaishouVideoList> videoGetList);
+
+    KuaishouVideoList getByPhotoId(@Param("photoId") String photoId, @Param("accountId") Long accountId);
+
+    void replaceBatchVideoRelateCreatives(@Param("list") JSONArray list);
+
+    List<Long> getAccountIds(@Param("projectId") Long projectId);
+
+    List<String> getPhototIdsByAccountId(@Param("accountId") Long accountId, @Param("startTime") String startTime, @Param("endTime") String endTime);
 }

+ 52 - 1
job-kuaishou/src/main/java/cn/com/ctop/job/kuaishou/data/mapper/xml/KuaishouVideoListMapper.xml

@@ -38,4 +38,55 @@
         </foreach>
 
     </insert>
-</mapper>
+
+
+    <select id="getByPhotoId" resultType="cn.com.ctop.job.kuaishou.data.entity.KuaishouVideoList">
+        SELECT signature,
+               photo_name
+        from kuaishou_video_list
+        WHERE photo_id = #{photoId}
+          AND account_id = #{accountId}
+    </select>
+
+
+    <insert id="replaceBatchVideoRelateCreatives">
+        replace into kuaishou_video_create_count(
+        `account_id`,
+        `photo_id`,
+        `photo_name`,
+        `signature`,
+        `creative_count`,
+        `advanced_creative_count`
+        )
+        values
+        <foreach collection="list" item="add" separator=",">
+            (
+            #{add.account_id},
+            #{add.photo_id},
+            #{add.photo_name},
+            #{add.signature},
+            #{add.creative_count},
+            #{add.advanced_creative_count}
+            )
+        </foreach>
+    </insert>
+
+
+    <select id="getAccountIds" resultType="Long">
+        SELECT account_id
+        FROM `jeecg-boot`.ctop_user_allocation
+        WHERE project_id = #{projectId}
+    </select>
+
+    <select id="getPhototIdsByAccountId" resultType="String">
+        SELECT photo_id FROM kuaishou_video_list
+        WHERE account_id = #{accountId}
+        AND photo_name like '%真人%'
+        <if test="startTime!=null">
+            AND stat_date >=#{startTime}
+            AND stat_date &lt;=#{endTime}
+        </if>
+
+    </select>
+
+</mapper>

+ 8 - 1
job-kuaishou/src/main/java/cn/com/ctop/job/kuaishou/data/service/IKuaishouVideoListService.java

@@ -16,5 +16,12 @@ import java.util.Map;
 public interface IKuaishouVideoListService extends IService<KuaishouVideoList> {
     void getVideoList(Long advertiserId, String accessToken, String startDate, String endDate, int page);
 
-    Map<String,String> getKuaishouVideoByAccountId(Long advertiserId, String accessToken, List<String> materialIds, int page);
+    Map<String, String> getKuaishouVideoByAccountId(Long advertiserId, String accessToken, List<String> materialIds, int page);
+
+    List<Long> getAccountIds(Long projectId);
+
+    void getVideosByAccountId(Long accountId, String startTime, String endTime);
+
+    void getVideoRelateCreatives(String accessToken, List<String> photoIds, Long accountId);
 }
+

+ 89 - 28
job-kuaishou/src/main/java/cn/com/ctop/job/kuaishou/data/service/impl/KuaishouVideoListServiceImpl.java

@@ -5,19 +5,25 @@ import cn.com.ctop.job.kuaishou.data.entity.KuaishouVideoList;
 import cn.com.ctop.job.kuaishou.data.enums.MaterialEnum;
 import cn.com.ctop.job.kuaishou.data.mapper.KuaishouVideoListMapper;
 import cn.com.ctop.job.kuaishou.data.service.IKuaishouVideoListService;
+import cn.com.ctop.job.kuaishou.data.service.IOauthTokenService;
 import cn.com.ctop.job.kuaishou.data.utils.Check;
 import cn.com.ctop.job.kuaishou.data.utils.HttpUtils;
 import cn.com.ctop.job.kuaishou.utils.RedisUtil;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.google.common.collect.Lists;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 import java.util.concurrent.TimeUnit;
 
 /**
@@ -33,6 +39,8 @@ public class KuaishouVideoListServiceImpl extends ServiceImpl<KuaishouVideoListM
     @Value("${api.kuaishou.postUrl}")
     private String postUrl;
 
+    @Autowired
+    private IOauthTokenService tokenService;
     @Resource
     private KuaishouVideoListMapper videoListMapper;
 
@@ -46,7 +54,7 @@ public class KuaishouVideoListServiceImpl extends ServiceImpl<KuaishouVideoListM
 
         log.info("开始获取所有视频信息 accountID:{},page:{}", advertiserId, page);
 
-        List<Object> adAccountList = redisUtil.getList("kuaiShou:allVideo:list",null);
+        List<Object> adAccountList = redisUtil.getList("kuaiShou:allVideo:list", null);
 
         String url = postUrl + KuaishouConstant.VIDEO_LIST;
         Map<String, String> headers = new HashMap<>();
@@ -71,19 +79,19 @@ public class KuaishouVideoListServiceImpl extends ServiceImpl<KuaishouVideoListM
             log.error("获取快手视频列表数据异常:{},accountId:{}", message, advertiserId);
 
             //请求过于频繁
-            if (400001 == code){
-                log.info("定时任务-->>allVideoList<<<<----获取所有快手视频请求频繁,code:{},准备重试,accountId:{},错误消息:{}",code,advertiserId,message);
+            if (400001 == code) {
+                log.info("定时任务-->>allVideoList<<<<----获取所有快手视频请求频繁,code:{},准备重试,accountId:{},错误消息:{}", code, advertiserId, message);
                 //添加重试
-                if (Check.isNull(adAccountList)){
+                if (Check.isNull(adAccountList)) {
                     redisUtil.add("kuaiShou:allVideo:list", Arrays.asList(advertiserId), 1, TimeUnit.HOURS);
-                }else {
+                } else {
                     adAccountList.add(advertiserId);
-                    redisUtil.add("kuaiShou:allVideo:list",adAccountList, 1, TimeUnit.HOURS);
+                    redisUtil.add("kuaiShou:allVideo:list", adAccountList, 1, TimeUnit.HOURS);
                 }
-            }else {
-                if (adAccountList.contains(advertiserId)){
+            } else {
+                if (adAccountList.contains(advertiserId)) {
                     adAccountList.remove(advertiserId);
-                    redisUtil.add("kuaiShou:allVideo:list",advertiserId, 1, TimeUnit.HOURS);
+                    redisUtil.add("kuaiShou:allVideo:list", advertiserId, 1, TimeUnit.HOURS);
                 }
             }
             return;
@@ -146,22 +154,19 @@ public class KuaishouVideoListServiceImpl extends ServiceImpl<KuaishouVideoListM
     }
 
 
-
     /**
-     *
-     * @description: 根据账户获取视频素材-账户下全部视频素材
-     *
      * @param advertiserId
      * @param accessToken
      * @param page
-     * @return: java.util.Map<java.lang.String,java.lang.String>
+     * @description: 根据账户获取视频素材-账户下全部视频素材
+     * @return: java.util.Map<java.lang.String, java.lang.String>
      * @author: zianY
      * @time: 2022/2/9
      */
     @Override
-    public Map<String,String> getKuaishouVideoByAccountId(Long advertiserId, String accessToken,List<String> materialIds, int page) {
-        log.info("调用接口------getKuaishouVideoByAccountId----根据账户获取视频素材---账户--{}===第{}页===》》》",advertiserId,page);
-        Map<String,String> resultMap = new HashMap<>();
+    public Map<String, String> getKuaishouVideoByAccountId(Long advertiserId, String accessToken, List<String> materialIds, int page) {
+        log.info("调用接口------getKuaishouVideoByAccountId----根据账户获取视频素材---账户--{}===第{}页===》》》", advertiserId, page);
+        Map<String, String> resultMap = new HashMap<>();
         String url = postUrl + KuaishouConstant.VIDEO_LIST;
         Map<String, String> headers = new HashMap<>();
         headers.put("Access-Token", accessToken);
@@ -175,29 +180,29 @@ public class KuaishouVideoListServiceImpl extends ServiceImpl<KuaishouVideoListM
             param.put("end_date", endDate);
         }*/
 
-        if (!Check.isNull(materialIds)){
+        if (!Check.isNull(materialIds)) {
             param.put("photo_ids", materialIds);
         }
         String result = HttpUtils.httpPostRequest(url, param, headers);
         JSONObject resultJson = JSONObject.parseObject(result);
         if (Check.isNull(resultJson)) {
-            resultMap.put("code","0");
-            resultMap.put("message","获取快手视频素材数据为空");
+            resultMap.put("code", "0");
+            resultMap.put("message", "获取快手视频素材数据为空");
             return resultMap;
         }
         Integer code = resultJson.getInteger("code");
         String message = resultJson.getString("message");
         if (null == code || code != 0) {
             log.error("获取快手视频列表数据异常:{},accountId:{}", message, advertiserId);
-            resultMap.put("code","500");
-            resultMap.put("message","账户id:"+advertiserId+"---获取快手视频列表数据异常:"+message);
+            resultMap.put("code", "500");
+            resultMap.put("message", "账户id:" + advertiserId + "---获取快手视频列表数据异常:" + message);
             return resultMap;
         }
         JSONArray details = resultJson.getJSONObject("data").getJSONArray("details");
         if (null == details || details.size() <= 0) {
             log.info("快手视频列表信息为空=》accountId:{}", advertiserId);
-            resultMap.put("code","0");
-            resultMap.put("message","获取快手视频素材列表信息为空");
+            resultMap.put("code", "0");
+            resultMap.put("message", "获取快手视频素材列表信息为空");
             return resultMap;
         }
         Boolean toGet = true;
@@ -246,13 +251,69 @@ public class KuaishouVideoListServiceImpl extends ServiceImpl<KuaishouVideoListM
             videoListMapper.replaceBatch(videoGetList);
         }
         if (toGet) {
-            getKuaishouVideoByAccountId(advertiserId, accessToken, materialIds,page + 1);
+            getKuaishouVideoByAccountId(advertiserId, accessToken, materialIds, page + 1);
         } else {
             log.info("视频列表数据获取完成:accountId:{}", advertiserId);
         }
-        resultMap.put("code","0");
-        resultMap.put("message","success");
+        resultMap.put("code", "0");
+        resultMap.put("message", "success");
         return resultMap;
     }
 
+
+    @Override
+    public List<Long> getAccountIds(Long projectId) {
+        return videoListMapper.getAccountIds(projectId);
+    }
+
+    @Override
+    public void getVideosByAccountId(Long accountId, String startTime, String endTime) {
+        String token = tokenService.getByAccountId(accountId);
+        List<String> allPhotos = videoListMapper.getPhototIdsByAccountId(accountId, startTime, endTime);
+        if (!Check.isNull(allPhotos)) {
+            List<List<String>> photoIdList = Lists.partition(allPhotos, 20);
+            System.out.println(photoIdList);
+            for (List<String> photoIds : photoIdList) {
+                getVideoRelateCreatives(token, photoIds, accountId);
+            }
+        }
+    }
+
+    @Override
+    public void getVideoRelateCreatives(String accessToken, List<String> photoIds, Long accountId) {
+        log.info("调用接口------getVideoRelateCreatives----视频关联创意数查询---账户:{} ", accountId);
+        String url = postUrl + KuaishouConstant.VIDEO_RELATE_CREATIVES;
+        Map<String, String> headers = new HashMap<>();
+        headers.put("Access-Token", accessToken);
+        Map<String, Object> param = new HashMap<>();
+        param.put("advertiser_id", accountId);
+        param.put("photo_ids", photoIds);
+        String result = HttpUtils.httpPostRequest(url, param, headers);
+        JSONObject resultJson = JSONObject.parseObject(result);
+        Integer code = resultJson.getInteger("code");
+        String message = resultJson.getString("message");
+        if (null == code || code != 0) {
+            log.error("调用接口---getVideoRelateCreatives----视频关联创意数查询 返回结果为空");
+            return;
+        }
+        JSONArray list = resultJson.getJSONObject("data").getJSONArray("related_creatives");
+        if (null == list || list.size() <= 0) {
+            log.info("视频关联创意数查询为空=》accountId:{}", accountId);
+            return;
+        }
+        for (int i = 0; i < list.size(); i++) {
+            JSONObject obj = list.getJSONObject(i);
+            obj.put("account_id", accountId);
+            KuaishouVideoList vo = videoListMapper.getByPhotoId(obj.getString("photo_id"), accountId);
+            if (!Check.isNull(vo)) {
+                obj.put("signature", vo.getSignature());
+                obj.put("photo_name", vo.getPhotoName());
+            }
+        }
+        if (!Check.isNull(list)) {
+            videoListMapper.replaceBatchVideoRelateCreatives(list);
+        }
+    }
+
+
 }

+ 48 - 15
job-kuaishou/src/main/java/cn/com/ctop/job/kuaishou/handler/VideoListJob.java

@@ -1,6 +1,5 @@
 package cn.com.ctop.job.kuaishou.handler;
 
-import cn.com.ctop.job.kuaishou.data.entity.OauthToken;
 import cn.com.ctop.job.kuaishou.data.service.IKuaishouVideoListService;
 import cn.com.ctop.job.kuaishou.data.service.IOauthTokenService;
 import cn.com.ctop.job.kuaishou.data.utils.Check;
@@ -13,7 +12,6 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
 import java.util.ArrayList;
-import java.util.Date;
 import java.util.List;
 import java.util.Set;
 import java.util.concurrent.ExecutorService;
@@ -27,6 +25,7 @@ public class VideoListJob {
     private static ExecutorService nowExecutorService = Executors.newFixedThreadPool(3);
     private static ExecutorService yesterdayExecutorService = Executors.newFixedThreadPool(3);
     private static ExecutorService allExecutorService = Executors.newFixedThreadPool(3);
+    private static ExecutorService videoRelateCreateCountExecutorService = Executors.newFixedThreadPool(8);
     @Autowired
     private IKuaishouVideoListService videoListService;
 
@@ -113,7 +112,6 @@ public class VideoListJob {
     }
 
 
-
     @Autowired
     private RedisUtil redisUtil;
 
@@ -126,18 +124,18 @@ public class VideoListJob {
     public void allVideoListSupplement() throws Exception {
         //获取视频物料信息异常的key
         Set<String> set = redisUtil.kesy("kuaiShou:allVideo:list*");
-        if (Check.isNull(set)){
-            log.info("快手视频数据暂时无遗漏---》》》日期:{}",DateUtils.getNowDate("yyyy-MM-dd"));
+        if (Check.isNull(set)) {
+            log.info("快手视频数据暂时无遗漏---》》》日期:{}", DateUtils.getNowDate("yyyy-MM-dd"));
             return;
         }
         List<String> list = new ArrayList<>(set);
         for (String key : list) {
             //添加重试
             List<Object> accountList;
-            accountList = redisUtil.getList(key,null);
+            accountList = redisUtil.getList(key, null);
             //遍历key下的所有信息
             for (Object accountId : accountList) {
-                log.info("快手视频遗漏数据补充,accountId:{}",accountId);
+                log.info("快手视频遗漏数据补充,accountId:{}", accountId);
                 String token = tokenService.getByAccountId(Long.valueOf(String.valueOf(accountId)));
                 if (Check.isNull(token)) {
                     log.error("此账户未获取到相关token,accountId:{}", accountId);
@@ -145,14 +143,14 @@ public class VideoListJob {
                 }
 
                 videoListService.getVideoList(Long.valueOf(accountId.toString()), token, null, null, 1);
-                accountList = redisUtil.getList(key,null);
+                accountList = redisUtil.getList(key, null);
 
-                if (accountList.size() == 0){
+                if (accountList.size() == 0) {
                     redisUtil.delete(key);
                 }
 
                 try {
-                    Thread.sleep(1000*2);
+                    Thread.sleep(1000 * 2);
                 } catch (InterruptedException e) {
                     e.printStackTrace();
                 }
@@ -163,10 +161,45 @@ public class VideoListJob {
     }
 
 
-
-
-
-
-
+    /**
+     * 支付宝-拉活-快手
+     * 支付宝-商家码
+     * 支付宝-激活
+     * 快手-瓜子二手车
+     * 瓜子-专项
+     * 百度极速版
+     * 易车
+     * 易车汽车报价
+     * 快手-京东超市-拉新
+     */
+    @XxlJob("getVideoRelateCreateCount")
+    public void getVideoRelateCreateCount() throws Exception {
+        List<Long> projectIds = new ArrayList<>();
+        projectIds.add(50L);
+        projectIds.add(67L);
+        projectIds.add(123L);
+        projectIds.add(2400915L);
+        projectIds.add(7920890L);
+        projectIds.add(12330885L);
+        projectIds.add(14310886L);
+        projectIds.add(14490902L);
+        projectIds.add(15120886L);
+        projectIds.add(15750885L);
+
+//        String endTime = DateUtils.getNowDate("yyyy-MM-dd");
+//        String startTime = DateUtils.getAnotherDay("yyyy-MM-dd", endTime, -31);
+
+        String endTime = null;
+        String startTime = null;
+
+        for (Long projectId : projectIds) {
+            List<Long> accountIds = videoListService.getAccountIds(projectId);
+            if (!Check.isNull(accountIds)) {
+                accountIds.forEach(accountId -> videoRelateCreateCountExecutorService.submit(() -> {
+                    videoListService.getVideosByAccountId(accountId, startTime, endTime);
+                }));
+            }
+        }
+    }
 
 }