Selaa lähdekoodia

体验版-获取广告列表

yangzian 2 vuotta sitten
vanhempi
commit
6cf7d40766

+ 2 - 0
job-bytedance/src/main/java/cn/com/ctop/job/bytedance/data/constant/BytedanceConstant.java

@@ -29,4 +29,6 @@ public class BytedanceConstant {
     public static final String BYTEDANCE_V2_REPORT_INTEGRATED_GET = "/2/report/integrated/get/";
 
     public static final String BYTEDANCE_V3_REPORT_INTEGRATED_GET = "/v3.0/report/material/get/";
+
+    public static final String BYTEDANCE_V3_PROMOTION_LIST_GET = "/v3.0/promotion/list/";
 }

+ 41 - 0
job-bytedance/src/main/java/cn/com/ctop/job/bytedance/data/entity/BytedancePromotionList.java

@@ -0,0 +1,41 @@
+package cn.com.ctop.job.bytedance.data.entity;
+
+import lombok.Data;
+
+import java.math.BigDecimal;
+
+@Data
+public class BytedancePromotionList {
+
+    private Long advertiserId;
+
+    private Long projectId;
+
+    private Long promotionId;
+
+    private String promotionName;
+
+    private String promotionCreateTime;
+
+    private String promotionModifyTime;
+
+    private String status;
+
+    private String optStatus;
+
+    private String promotionMaterials;
+
+    private String source;
+
+    private String materialScoreInfo;
+
+    private BigDecimal deepCpabid;
+
+    private String roiGoal;
+
+    private BigDecimal cpaBid;
+
+    private BigDecimal budget;
+
+
+}

+ 14 - 0
job-bytedance/src/main/java/cn/com/ctop/job/bytedance/data/mapper/BytedancePromotionListMapper.java

@@ -0,0 +1,14 @@
+package cn.com.ctop.job.bytedance.data.mapper;
+
+
+import cn.com.ctop.job.bytedance.data.entity.BytedancePromotionList;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
+
+public interface BytedancePromotionListMapper {
+
+    int replacePromotionList(@Param("list")List<BytedancePromotionList> list);
+
+
+}

+ 47 - 0
job-bytedance/src/main/java/cn/com/ctop/job/bytedance/data/mapper/xml/BytedancePromotionListMapper.xml

@@ -0,0 +1,47 @@
+<?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.job.bytedance.data.mapper.BytedancePromotionListMapper">
+
+
+    <insert id="replacePromotionList">
+        REPLACE INTO bytedance_promotion_list (
+        advertiser_id,
+        project_id,
+        promotion_id,
+        promotion_name,
+        promotion_create_time,
+        promotion_modify_time,
+        STATUS,
+        opt_status,
+        promotion_materials,
+        source,
+        material_score_info,
+        deep_cpabid,
+        roi_goal,
+        cpa_bid,
+        budget
+        )
+        VALUES
+        <foreach collection = "list" item = "item" separator="," >
+        (#{item.advertiserId},
+        #{item.projectId},
+        #{item.promotionId},
+        #{item.promotionName},
+        #{item.promotionCreateTime},
+        #{item.promotionModifyTime},
+        #{item.status},
+        #{item.optStatus},
+        #{item.promotionMaterials},
+        #{item.source},
+        #{item.materialScoreInfo},
+        #{item.deepCpabid},
+        #{item.roiGoal},
+        #{item.cpaBid},
+        #{item.budget}
+        )
+        </foreach>
+    </insert>
+
+
+
+</mapper>

+ 15 - 0
job-bytedance/src/main/java/cn/com/ctop/job/bytedance/data/service/IBytedancePromotionListService.java

@@ -0,0 +1,15 @@
+package cn.com.ctop.job.bytedance.data.service;
+
+import cn.com.ctop.job.bytedance.data.entity.OauthToken;
+
+/**
+ * 获取体验版广告列表
+ *
+ * @author zianY
+ * 2022-10-19
+ */
+public interface IBytedancePromotionListService {
+
+    void getPromotionList(OauthToken oauthToken, String startDate,String endDate,int pageNumber, int PageSize);
+
+}

+ 87 - 0
job-bytedance/src/main/java/cn/com/ctop/job/bytedance/data/service/impl/BytedancePromotionListServiceImpl.java

@@ -0,0 +1,87 @@
+package cn.com.ctop.job.bytedance.data.service.impl;
+
+import cn.com.ctop.job.bytedance.data.constant.BytedanceConstant;
+import cn.com.ctop.job.bytedance.data.entity.BytedancePromotionList;
+import cn.com.ctop.job.bytedance.data.entity.OauthToken;
+import cn.com.ctop.job.bytedance.data.mapper.BytedancePromotionListMapper;
+import cn.com.ctop.job.bytedance.data.service.IBytedancePromotionListService;
+import cn.com.ctop.job.bytedance.data.utils.Check;
+import cn.com.ctop.job.bytedance.data.utils.HttpUtils;
+import cn.com.ctop.job.bytedance.utils.RedisUtil;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+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.ArrayList;
+import java.util.List;
+
+/**
+ * 获取体验版广告列表
+ *
+ * @author zianY
+ * 2022-10-19
+ */
+@Service
+@Slf4j
+public class BytedancePromotionListServiceImpl implements IBytedancePromotionListService {
+    @Value("${api.bytedance.api-prefix}")
+    private String bytedanceApiUrl;
+
+    @Resource
+    BytedancePromotionListMapper bytedancePromotionListMapper;
+
+    @Autowired
+    private RedisUtil redisUtil;
+
+
+    @Override
+    public void getPromotionList(OauthToken oauthToken, String startDate, String endDate, int pageNumber, int pageSize) {
+        log.info("开始获取体验版-广告列表数据 accountIDL{},日期{},page:{}", oauthToken.getAccountId(), startDate,pageNumber);
+        // 请求地址
+        String url = bytedanceApiUrl + BytedanceConstant.BYTEDANCE_V3_PROMOTION_LIST_GET;
+        JSONObject param = new JSONObject();
+        param.put("advertiser_id", oauthToken.getAccountId());
+        param.put("page", pageNumber);
+        param.put("page_size", pageSize);
+//        JSONObject filtering = new JSONObject();
+//        filtering.put("promotion_modify_time", startDate);
+//        param.put("filtering", filtering);
+        JSONObject resultObject = HttpUtils.bytedanceGetRequest(oauthToken.getAccessToken(), url, param);
+        if(Check.isNull(resultObject)){
+            log.error("体验版-广告列表异常==》accountId:{},调用结果为null", oauthToken.getAccountId());
+            return;
+        }
+        Integer code = resultObject.getInteger("code");
+        if (null == code || !code.equals(0)) {
+            log.error("获取体验版-广告列表接口异常==》accountId:{},message:{},code{}", oauthToken.getAccountId(), resultObject.getString("message"),code);
+            return;
+        }
+        JSONArray data = resultObject.getJSONObject("data").getJSONArray("list");
+        if (null == data || data.isEmpty()) {
+            log.error("获取体验版-广告列表数据为空==》accountId:{},message:{},code{}", oauthToken.getAccountId(), resultObject.getString("message"),code);
+            return;
+        }
+        List<BytedancePromotionList> list = new ArrayList<>();
+        for (int i = 0; i < data.size(); i++) {
+            JSONObject dataObject = data.getJSONObject(i);
+            //log.info("info-->{}",dataObject);
+            BytedancePromotionList report = JSONObject.parseObject(dataObject.toJSONString(), BytedancePromotionList.class);
+            list.add(report);
+        }
+        bytedancePromotionListMapper.replacePromotionList(list);
+        pageNumber++;
+        int totalPage = resultObject.getJSONObject("data").getJSONObject("page_info").getInteger("total_page");
+        if (pageNumber <= totalPage) {
+            getPromotionList(oauthToken, startDate, endDate, pageNumber, 10);
+        }
+    }
+
+
+
+
+
+}

+ 77 - 0
job-bytedance/src/main/java/cn/com/ctop/job/bytedance/handler/BytedancePromotionJob.java

@@ -0,0 +1,77 @@
+package cn.com.ctop.job.bytedance.handler;
+
+import cn.com.ctop.job.bytedance.data.entity.OauthToken;
+import cn.com.ctop.job.bytedance.data.service.IBytedancePromotionListService;
+import cn.com.ctop.job.bytedance.data.service.IOauthTokenService;
+import cn.com.ctop.job.bytedance.data.utils.Check;
+import cn.com.ctop.job.bytedance.data.utils.DateUtils;
+import com.xxl.job.core.context.XxlJobHelper;
+import com.xxl.job.core.handler.annotation.XxlJob;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+/**
+ * 获取体验版广告列表
+ *
+ * @author zianY
+ * 2022-10-19
+ */
+@Slf4j
+@Component
+public class BytedancePromotionJob {
+    @Autowired
+    private IOauthTokenService tokenService;
+
+    private static ExecutorService dailyExecutorService = Executors.newFixedThreadPool(3);
+
+
+    @Autowired
+    private IBytedancePromotionListService promotionListService;
+
+
+    /**
+     * 体验版-获取广告列表
+     *
+     * @throws Exception
+     */
+    @XxlJob("getPromotionList")
+    public void getPromotionList() throws Exception {
+        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;
+        }
+        OauthToken oauthToken = new OauthToken();
+        oauthToken.setAccessToken(token);
+        oauthToken.setAccountId(Long.parseLong(param));
+
+        String nowDate = DateUtils.getNowDate("yyyy-MM-dd");
+        String yesterda = DateUtils.getAnotherDay("yyyy-MM-dd", nowDate, -1);
+        String yesterda2 = DateUtils.getAnotherDay("yyyy-MM-dd", nowDate, -2);
+        String yesterda3 = DateUtils.getAnotherDay("yyyy-MM-dd", nowDate, -3);
+        dailyExecutorService.submit(
+                new Runnable() {
+                    @Override
+                    public void run() {
+                        promotionListService.getPromotionList(oauthToken, yesterda, yesterda, 1,10);
+                        promotionListService.getPromotionList(oauthToken, yesterda2, yesterda2, 1,10);
+                        promotionListService.getPromotionList(oauthToken, yesterda3, yesterda3, 1,10);
+                    }
+                });
+
+
+    }
+
+}