Przeglądaj źródła

素造素材添加同步供应商定时任务

zhaoxian 4 lat temu
rodzic
commit
a9794505d5

+ 50 - 0
module-job-kuaishou/src/main/java/cn/com/ctop/job/kuaishou/handler/KuaishouSupplierVideoGetJob.java

@@ -0,0 +1,50 @@
+package cn.com.ctop.job.kuaishou.handler;
+
+import cn.com.ctop.common.module.entity.CtopOauthToken;
+import cn.com.ctop.common.module.service.ICtopOauthTokenService;
+import cn.com.ctop.common.module.utils.Check;
+import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouVideoGetService;
+import com.alibaba.fastjson.JSONObject;
+import com.xxl.job.core.context.XxlJobHelper;
+import com.xxl.job.core.handler.annotation.XxlJob;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+/**
+ * 同步素造素材供应商
+ *
+ * @param
+ * @author ZHAOXA
+ * @return
+ * @throws
+ */
+@Component
+public class KuaishouSupplierVideoGetJob {
+    @Autowired
+    private IKuaiShouVideoGetService videoGetService;
+    @Autowired
+    private ICtopOauthTokenService tokenService;
+    static ExecutorService executorService = Executors.newFixedThreadPool(5);
+
+    @XxlJob("kuaishouSupplierVideoGetJob")
+    public void KuaishouSupplierVideoGetJob() {
+        List<JSONObject> datas = videoGetService.getAccountIds();
+        for (JSONObject data : datas) {
+            CtopOauthToken token = tokenService.getTokenByAccountId(data.getLong("accountId"));
+            if (Check.isNull(token)) {
+                continue;
+            }
+            executorService.submit(new Runnable() {
+                @Override
+                public void run() {
+                    videoGetService.getSuZaoList(token.getAccessToken(), data.getLong("accountId"), 1, data.getString("startDate"));
+                }
+            });
+        }
+        XxlJobHelper.log("同步素造素材供应商完成");
+    }
+}

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

@@ -44,6 +44,13 @@ public class KuaiShouVideoGet {
     private Integer materialType;
     private Integer channelType;
     private Integer status;
+    /**素造素材标签*/
+    private String materialTag;
+    /**素造供应商id*/
+    private Long supplierAccountId;
+    /**供应商名称*/
+    private String supplierAccountName;
+
     /**
      * 创建时间
      */

+ 4 - 0
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/batch/mapper/KuaiShouVideoGetMapper.java

@@ -47,4 +47,8 @@ public interface KuaiShouVideoGetMapper extends BaseMapper<KuaiShouVideoGet> {
     List<KuaiShouVideoGet> getHistoryTopVideoByParams(@Param("accountId")Long accountId, @Param("startTime")String startTime, @Param("endTime")String endTime, @Param("videoCnt")Long videoCnt,@Param("appVersion")String appVersion,@Param("channelType")Integer channelType);
 
     List<KuaishouProjectVideoGet> selectProjectVideo(@Param("projectId") Long projectId,@Param("updateTime") String updateTime);
+
+    void updateBatch(@Param(value = "videoGetList")  List<KuaiShouVideoGet> videoGetList);
+
+    List<JSONObject> getAccountIds();
 }

+ 32 - 0
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/batch/mapper/xml/KuaiShouVideoGetMapper.xml

@@ -324,4 +324,36 @@
         </if>
 
     </select>
+
+    <select id="getAccountIds" resultType="com.alibaba.fastjson.JSONObject">
+        SELECT
+            account_id 'accountId',DATE_FORMAT(MAX(create_time),'%Y-%m-%d %H:%i:%s') 'startDate'
+        FROM ctop_kuaishou_video_get
+        where channel_type = 1
+          and  status = 0
+        GROUP BY account_id
+    </select>
+
+    <update id="updateBatch">
+        UPDATE ctop_kuaishou_video_get SET
+        material_tag = CASE id
+        <foreach item="videoGet" index="index" collection="videoGetList">
+            WHEN #{videoGet.id} THEN #{videoGet.materialTag}
+        </foreach>
+        END,
+        supplier_account_id = CASE id
+        <foreach item="videoGet" index="index" collection="videoGetList">
+            WHEN #{videoGet.id} THEN #{videoGet.supplierAccountId}
+        </foreach>
+        END,
+        supplier_account_name = CASE id
+        <foreach item="videoGet" index="index" collection="videoGetList">
+            WHEN #{videoGet.id} THEN #{videoGet.supplierAccountName}
+        </foreach>
+        END
+        WHERE id IN
+        <foreach item="videoGet" index="index" collection="videoGetList" open="(" separator="," close=")">
+             #{videoGet.id}
+        </foreach>
+    </update>
 </mapper>

+ 4 - 0
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/batch/service/IKuaiShouVideoGetService.java

@@ -52,4 +52,8 @@ public interface IKuaiShouVideoGetService extends IService<KuaiShouVideoGet> {
     List<KuaiShouVideoGet> getZeroVideoByParams(Long accountId, String startTime, String endTime, Long videoCnt,String appVersion,Integer channelType,Integer relativeCnt);
 
     List<KuaiShouVideoGet> getHistoryTopVideoByParams(Long accountId, String startTime, String endTime, Long videoCnt, String appVersion, Integer channelType);
+
+    void getSuZaoList(String token, Long accountId, int page,String startDate);
+
+    List<JSONObject> getAccountIds();
 }

+ 73 - 10
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/batch/service/impl/KuaiShouVideoGetServiceImpl.java

@@ -15,6 +15,7 @@ import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import lombok.extern.slf4j.Slf4j;
+import org.jeecg.common.util.DateUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
@@ -22,9 +23,12 @@ import org.springframework.stereotype.Service;
 import javax.annotation.Resource;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
 
 /**
  * @Description: 快手-获取视频接口
@@ -151,7 +155,7 @@ public class KuaiShouVideoGetServiceImpl extends ServiceImpl<KuaiShouVideoGetMap
 
 
     @Override
-    public List<KuaiShouVideoGet> getNewVideoBetweenTime(Long accountId, String startTime, String endTime,Integer channelType,String appVersion,Integer videoCnt) {
+    public List<KuaiShouVideoGet> getNewVideoBetweenTime(Long accountId, String startTime, String endTime, Integer channelType, String appVersion, Integer videoCnt) {
         QueryWrapper<KuaiShouVideoGet> queryWrapper = new QueryWrapper<>();
         queryWrapper.isNotNull("signature");
         if (null != accountId && accountId != 0) {
@@ -161,15 +165,15 @@ public class KuaiShouVideoGetServiceImpl extends ServiceImpl<KuaiShouVideoGetMap
             queryWrapper.le("stat_date", endTime);
             queryWrapper.gt("stat_date", startTime);
         }
-        if(null!=channelType&&channelType!=2){
-            queryWrapper.eq("channel_type",channelType);
+        if (null != channelType && channelType != 2) {
+            queryWrapper.eq("channel_type", channelType);
         }
 
-        if(null!=appVersion&&!appVersion.trim().equals("")){
-            queryWrapper.likeRight("photo_name",appVersion);
+        if (null != appVersion && !appVersion.trim().equals("")) {
+            queryWrapper.likeRight("photo_name", appVersion);
         }
-        if(null!=videoCnt&&videoCnt!=0){
-            queryWrapper.last("limit "+videoCnt);
+        if (null != videoCnt && videoCnt != 0) {
+            queryWrapper.last("limit " + videoCnt);
         }
         return this.list(queryWrapper);
     }
@@ -277,12 +281,71 @@ public class KuaiShouVideoGetServiceImpl extends ServiceImpl<KuaiShouVideoGetMap
     }
 
     @Override
-    public List<KuaiShouVideoGet> getZeroVideoByParams(Long accountId, String startTime, String endTime, Long videoCnt,String appVersion,Integer channelType,Integer relativeCnt) {
-        return videoGetMapper.getZeroVideoByParams(accountId,startTime,endTime,videoCnt,appVersion,channelType,relativeCnt);
+    public List<KuaiShouVideoGet> getZeroVideoByParams(Long accountId, String startTime, String endTime, Long videoCnt, String appVersion, Integer channelType, Integer relativeCnt) {
+        return videoGetMapper.getZeroVideoByParams(accountId, startTime, endTime, videoCnt, appVersion, channelType, relativeCnt);
     }
 
     @Override
     public List<KuaiShouVideoGet> getHistoryTopVideoByParams(Long accountId, String startTime, String endTime, Long videoCnt, String appVersion, Integer channelType) {
-        return videoGetMapper.getHistoryTopVideoByParams(accountId,startTime,endTime,videoCnt,appVersion,channelType);
+        return videoGetMapper.getHistoryTopVideoByParams(accountId, startTime, endTime, videoCnt, appVersion, channelType);
+    }
+
+    /**
+     * 素造接口,获取同步供应商素材标签数据
+     */
+    @Override
+    public void getSuZaoList(String token, Long accountId, int page,String startDate) {
+        String url = "https://ad.e.kuaishou.com/rest/openapi/v1/ad_creator/video/list";
+        Map<String, String> headers = new HashMap<String, String>();
+        headers.put("Content-Type", "application/json");
+        headers.put("Access-Token", token);
+        Map<String, Object> param = new HashMap<String, Object>();
+        param.put("advertiser_id", accountId);
+        param.put("page", page);
+        param.put("page_size", 200);
+        param.put("start_date", startDate);
+        param.put("end_date", DateUtils.formatDate(new Date(),DateUtils.NEW_FORMAT));
+        String result = HttpUtils.httpPostRequest(url, param, headers);
+        JSONObject resultJson = JSONObject.parseObject(result);
+        System.out.println(result);
+        if (Check.isNull(resultJson)) {
+            return;
+        }
+        Integer code = resultJson.getInteger("code");
+        String message = resultJson.getString("message");
+        if (null == code || code != 0) {
+            log.error("获取快手素造列表数据异常:{},accountId:{}", message, accountId);
+            return;
+        }
+        JSONObject dataJson = resultJson.getJSONObject("data");
+        if (Check.isNull(dataJson)) {
+            return;
+        }
+        JSONArray details = dataJson.getJSONArray("details");
+        if (Check.isNull(details)) {
+            return;
+        }
+        List<KuaiShouVideoGet> videoGetList = new ArrayList<>();
+        for (int i = 0; i < details.size(); i++) {
+            JSONObject detailJson = details.getJSONObject(i);
+            if (Check.isNull(detailJson)) {
+                continue;
+            }
+            KuaiShouVideoGet videoGet = new KuaiShouVideoGet();
+            videoGet.setMaterialTag(detailJson.getString("material_tag"));
+            videoGet.setSupplierAccountId(detailJson.getLong("supplier_account_id"));
+            videoGet.setSupplierAccountName(detailJson.getString("supplier_account_name"));
+            videoGet.setId(accountId + detailJson.getString("photo_id"));
+            videoGetList.add(videoGet);
+        }
+        if (!Check.isNull(videoGetList)) {
+            videoGetMapper.updateBatch(videoGetList);
+        }
+        getSuZaoList(token, accountId, page + 1,startDate);
+    }
+
+    @Override
+    public List<JSONObject> getAccountIds() {
+        return videoGetMapper.getAccountIds();
     }
 }