Browse Source

获取广告计划信息

yangzian 3 years ago
parent
commit
c78639ee2d

+ 3 - 0
job-kuaishou/src/main/java/cn/com/ctop/job/kuaishou/data/constant/KuaishouConstant.java

@@ -20,6 +20,9 @@ public class KuaishouConstant {
     //获取广告组信息
     public static final String AD_UNIT_LIST = "/rest/openapi/v1/ad_unit/list";
 
+    //获取广告计划信息
+    public static final String CAMPAIGN_LIST = "/rest/openapi/v1/campaign/list";
+
 
 
 }

+ 5 - 1
job-kuaishou/src/main/java/cn/com/ctop/job/kuaishou/data/mapper/AdUnitReportMapper.java

@@ -2,6 +2,7 @@ package cn.com.ctop.job.kuaishou.data.mapper;
 
 import cn.com.ctop.job.kuaishou.data.entity.KuaishouAccountReportDaily;
 import cn.com.ctop.job.kuaishou.data.entity.KuaishouAdUnitList;
+import cn.com.ctop.job.kuaishou.data.entity.KuaishouCampaignList;
 import cn.com.ctop.job.kuaishou.data.entity.OauthToken;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import org.apache.ibatis.annotations.Param;
@@ -14,9 +15,12 @@ import java.util.List;
  * @author jeecg-boot 2021-09-07
  * @version V1.0
  */
-public interface AdUnitReportMapper extends BaseMapper<KuaishouAdUnitList> {
+public interface AdUnitReportMapper{
 
   //批量插入 快手-广告组信息
   void replaceBatchKsAdUnit(@Param("addList") List<KuaishouAdUnitList> addList);
 
+  //批量插入 快手-广告计划信息
+  void replaceBatchKsCampaign(@Param("addList") List<KuaishouCampaignList> addList);
+
 }

+ 40 - 2
job-kuaishou/src/main/java/cn/com/ctop/job/kuaishou/data/mapper/xml/AdUnitReportMapper.xml

@@ -2,7 +2,8 @@
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="cn.com.ctop.job.kuaishou.data.mapper.AdUnitReportMapper">
 
-   <insert id="replaceBatchKsAdUnit">
+    <!-- 插入 快手广告组信息 -->
+    <insert id="replaceBatchKsAdUnit">
        replace into kuaishou_ad_unit_list(
        `campaign_id`,
        `advertiser_id`,
@@ -162,7 +163,44 @@
            #{add.entionTarget}
            )
        </foreach>
+   </insert>
+
+
+
+<!-- 插入 快手广告计划信息 -->
+    <insert id="replaceBatchKsCampaign">
+        replace into kuaishou_campaign_list(
+        `advertiser_id`,
+        `status`,
+        `campaign_id`,
+        `campaign_name`,
+        `put_status`,
+        `create_channel`,
+        `day_budget`,
+        `day_budget_schedule`,
+        `campaign_type`,
+        `campaign_sub_type`,
+        `create_time`,
+        `update_time`
+        )
+        values
+        <foreach collection="addList" item="add" separator=",">
+            (
+            #{add.advertiserId},
+            #{add.status},
+            #{add.campaignId},
+            #{add.campaignName},
+            #{add.putStatus},
+            #{add.createChannel},
+            #{add.dayBudget},
+            #{add.dayBudgetSchedule},
+            #{add.campaignType},
+            #{add.campaignSubType},
+            #{add.createTime},
+            #{add.updateTime}
+            )
+        </foreach>
+    </insert>
 
 
-   </insert>
 </mapper>

+ 3 - 1
job-kuaishou/src/main/java/cn/com/ctop/job/kuaishou/data/service/IAdUnitReportService.java

@@ -12,8 +12,10 @@ import java.util.PrimitiveIterator;
  * @author jeecg-boot 2021-09-07
  * @version V1.0
  */
-public interface IAdUnitReportService extends IService<KuaishouAdUnitList> {
+public interface IAdUnitReportService{
 
     public void getAdUnitReport(Long advertiserId, String accessToken, String startDate, String endDate, int page);
 
+    public void getAdPlanReport(Long advertiserId, String accessToken, String startDate, String endDate, int page);
+
 }

+ 68 - 5
job-kuaishou/src/main/java/cn/com/ctop/job/kuaishou/data/service/impl/AdUnitReportServiceImpl.java

@@ -1,10 +1,7 @@
 package cn.com.ctop.job.kuaishou.data.service.impl;
 
 import cn.com.ctop.job.kuaishou.data.constant.KuaishouConstant;
-import cn.com.ctop.job.kuaishou.data.entity.KuaishouAccountReportDaily;
-import cn.com.ctop.job.kuaishou.data.entity.KuaishouAdUnitList;
-import cn.com.ctop.job.kuaishou.data.entity.KuaishouVideoList;
-import cn.com.ctop.job.kuaishou.data.entity.OauthToken;
+import cn.com.ctop.job.kuaishou.data.entity.*;
 import cn.com.ctop.job.kuaishou.data.mapper.AdUnitReportMapper;
 import cn.com.ctop.job.kuaishou.data.mapper.OauthTokenMapper;
 import cn.com.ctop.job.kuaishou.data.service.IAdUnitReportService;
@@ -14,6 +11,7 @@ import cn.com.ctop.job.kuaishou.data.utils.HttpUtils;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.xxl.job.core.context.XxlJobHelper;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
@@ -33,7 +31,7 @@ import java.util.Map;
  */
 @Slf4j
 @Service
-public class AdUnitReportServiceImpl extends ServiceImpl<AdUnitReportMapper, KuaishouAdUnitList> implements IAdUnitReportService {
+public class AdUnitReportServiceImpl implements IAdUnitReportService {
 
     @Value("${api.kuaishou.postUrl}")
     private String postUrl;
@@ -114,5 +112,70 @@ public class AdUnitReportServiceImpl extends ServiceImpl<AdUnitReportMapper, Kua
       }
     }
 
+  /**
+   * 快手广告计划信息
+   * @param advertiserId
+   * @param accessToken
+   * @param startDate
+   * @param endDate
+   * @param page
+   */
+  @Override
+  public void getAdPlanReport(Long advertiserId, String accessToken, String startDate, String endDate, int page) {
+
+    log.info("accountId:=={}=====获取广告计划数据信息====page--{}===》》》》",advertiserId,page);
+    Integer page_size = 500;
+
+    JSONObject param = new JSONObject();
+    param.put("advertiser_id", advertiserId);
+    param.put("page_size", 500);
+    param.put("page", page);
+
+    Map<String, String> headers = new HashMap<>();
+    headers.put("Access-Token", accessToken);
+    headers.put("Content-Type", "application/json");
+
+    String result = HttpUtils.httpPostRequest(postUrl + KuaishouConstant.CAMPAIGN_LIST, param, headers);
+    JSONObject resultJson = JSONObject.parseObject(result);
+    if (Check.isNull(resultJson)) {
+      XxlJobHelper.log("获取广告计划接口异常,advertiserId:{}", advertiserId);
+      return;
+    }
+    Integer code = resultJson.getInteger("code");
+    if (null == code || code != 0) {
+      XxlJobHelper.log("获取广告计划返回结果异常,advertiserId:{},异常信息:{}", advertiserId, resultJson);
+      return;
+    }
+    JSONArray details = resultJson.getJSONObject("data").getJSONArray("details");
+    if (Check.isNull(details)) {
+      log.info("快手广告计划数据为空=》accountId:{}", advertiserId);
+      return;
+    }
+
+    List<KuaishouCampaignList> kscampaignList = new ArrayList<>();
+    for (Object detail : details) {
+      JSONObject unitDetail = (JSONObject) detail;
+      unitDetail.put("advertiser_id",advertiserId);
+      KuaishouCampaignList ksAdunit = KuaishouCampaignList.getKuaishouCampaignListInfo(unitDetail);
+      kscampaignList.add(ksAdunit);
+    }
+    if (!Check.isNull(kscampaignList)) {
+      adUnitReportMapper.replaceBatchKsCampaign(kscampaignList);
+    }
+
+    //是否有下一页
+    Boolean toGet = true;
+    if (details.size() < page_size) {
+      toGet = false;
+    }
+    if (toGet) {
+      getAdPlanReport(advertiserId, accessToken, startDate, endDate, page + 1);
+    } else {
+      log.info("快手广告计划数据获取完成:accountId:{}", advertiserId);
+    }
+
+
+  }
+
 
 }

+ 41 - 2
job-kuaishou/src/main/java/cn/com/ctop/job/kuaishou/handler/AdUnitReportJob.java

@@ -37,8 +37,8 @@ public class AdUnitReportJob {
    *
    * @throws Exception
    */
-  @XxlJob("AdUnitListReport")
-  public void AdUnitListReport(){
+  @XxlJob("AdUnitListReportJob")
+  public void AdUnitListReportJob(){
     String param = XxlJobHelper.getJobParam(); // 执行参数
     if (Check.isNull(param)) {
       log.error("入参为空");
@@ -65,4 +65,43 @@ public class AdUnitReportJob {
         });
   }
 
+
+
+
+  /**
+   * 快手广告计划信息
+   * 前一天
+   *
+   * @throws Exception
+   */
+  @XxlJob("KuaishouCampaignListJob")
+  public void KuaishouCampaignListJob(){
+    String param = XxlJobHelper.getJobParam(); // 执行参数
+    if (Check.isNull(param)) {
+      log.error("入参为空");
+      return;
+    }
+    String token = tokenService.getByAccountId(Long.valueOf(param));
+    if (Check.isNull(token)) {
+      log.error("此账户未获取到相关token,accountId:{}", param);
+      return;
+    }
+
+    // 前一天 时间
+    String date = DateUtils.getAnotherDay("yyyy-MM-dd", DateUtils.getNowDate("yyyy-MM-dd"), -1);
+    dailyExecutorService.submit(
+            new Runnable() {
+              @Override
+              public void run() {
+                try {
+                  adUnitReportService.getAdPlanReport(Long.valueOf(param),token, date, date, 1);
+                }catch (Exception e){
+                  log.info(String.valueOf(e));;
+                }
+              }
+            });
+  }
+
+
+
 }