Browse Source

快手-账户余额

yangzian 3 years ago
parent
commit
7f86cad544

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

@@ -32,6 +32,9 @@ public class KuaishouConstant {
     //账户流水
     public static final String FUND_DAILY_FLOWS = "/rest/openapi/v1/advertiser/fund/daily_flows";
 
+    //账户余额
+    public static final String FUND_GET = "/rest/openapi/v1/advertiser/fund/get";
+
 
 
 

+ 49 - 0
job-kuaishou/src/main/java/cn/com/ctop/job/kuaishou/data/entity/KuaishouAdvertiserFundGet.java

@@ -0,0 +1,49 @@
+package cn.com.ctop.job.kuaishou.data.entity;
+
+import cn.com.ctop.job.kuaishou.data.utils.DateUtils;
+import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.annotation.TableName;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import lombok.experimental.Accessors;
+import org.jeecgframework.poi.excel.annotation.Excel;
+
+import java.math.BigDecimal;
+
+/**
+ * 快手账户余额
+ * zian Y
+ * 2022/1/7
+ **/
+@Data
+@TableName("kuaishou_advertiser_fund_get")
+@EqualsAndHashCode(callSuper = false)
+@Accessors(chain = true)
+@ApiModel(value = "kuaishou_advertiser_fund_get快手账户余额", description = "快手账户余额")
+public class KuaishouAdvertiserFundGet {
+
+    /**
+     * 账户ID
+     */
+    @Excel(name = "账户ID", width = 15)
+    @ApiModelProperty(value = "账户ID")
+    private Long advertiserId;
+    /**
+     * 账户ID
+     */
+    @Excel(name = "账户总余额", width = 15)
+    @ApiModelProperty(value = "账户总余额")
+    private BigDecimal balance;
+
+
+    public static KuaishouAdvertiserFundGet getKuaishouFundGetInfo(JSONObject detailJson){
+        KuaishouAdvertiserFundGet dailyFlows = new KuaishouAdvertiserFundGet();
+        dailyFlows.setAdvertiserId(detailJson.getLong("advertiser_id"));
+        dailyFlows.setBalance(detailJson.getBigDecimal("balance"));
+        return dailyFlows;
+    }
+
+
+}

+ 4 - 0
job-kuaishou/src/main/java/cn/com/ctop/job/kuaishou/data/mapper/KuaishouAdvertiserFundDailyFlowsMapper.java

@@ -1,6 +1,7 @@
 package cn.com.ctop.job.kuaishou.data.mapper;
 
 import cn.com.ctop.job.kuaishou.data.entity.KuaishouAdvertiserFundDailyFlows;
+import cn.com.ctop.job.kuaishou.data.entity.KuaishouAdvertiserFundGet;
 import org.apache.ibatis.annotations.Param;
 
 import java.util.List;
@@ -15,6 +16,9 @@ public interface KuaishouAdvertiserFundDailyFlowsMapper {
   //批量插入 快手-账户流水
   void replaceBatchKsFundDailyFlows(@Param("addList") List<KuaishouAdvertiserFundDailyFlows> addList);
 
+  //批量插入 快手-账户余额
+  void replaceBatchKsFundGetBalance(KuaishouAdvertiserFundGet kuaishouAdvertiserFundGet);
+
 
 
 }

+ 14 - 0
job-kuaishou/src/main/java/cn/com/ctop/job/kuaishou/data/mapper/xml/KuaishouAdvertiserFundDailyFlowsMapper.xml

@@ -38,4 +38,18 @@
         </insert>
 
 
+    <!-- 快手-账户余额 (这个【账户余额表】就2个字段 就不创建单独的mapper了 让【余额】和【账户流水】挤一下 都放在【账户余额】的mapper里吧)-->
+    <insert id="replaceBatchKsFundGetBalance">
+        replace into kuaishou_advertiser_fund_get(
+            `advertiser_id`,
+            `balance`
+            )
+            values
+                (
+                #{advertiserId},
+                #{balance}
+                )
+        </insert>
+
+
 </mapper>

+ 2 - 0
job-kuaishou/src/main/java/cn/com/ctop/job/kuaishou/data/service/IAdUnitReportService.java

@@ -27,4 +27,6 @@ public interface IAdUnitReportService{
     void crowdAnalysisReport(Long advertiserId, String accessToken, String date,  List<Long> campaignIds,List<Long> unitIds, List<String> reportDims,Integer page);
 
     void getKuaishouDailyFlows(Long advertiserId, String accessToken,int page);
+
+    void getKuaishouDailyFundGet(Long advertiserId,String accessToken);
 }

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

@@ -23,9 +23,9 @@ import java.util.List;
 import java.util.Map;
 
 /**
- * token信息
+ * 快手 定时任务
  *
- * @author jeecg-boot 2021-09-07
+ * @author zian Y 2022-01-05
  * @version V1.0
  */
 @Slf4j
@@ -121,6 +121,7 @@ public class AdUnitReportServiceImpl implements IAdUnitReportService {
       } else {
         log.info("快手广告组数据获取完成:accountId:{}", advertiserId);
       }
+
     }
 
   /**
@@ -184,6 +185,7 @@ public class AdUnitReportServiceImpl implements IAdUnitReportService {
     } else {
       log.info("快手广告计划数据获取完成:accountId:{}", advertiserId);
     }
+
   }
 
   /**
@@ -200,6 +202,7 @@ public class AdUnitReportServiceImpl implements IAdUnitReportService {
         getAgentReport(agentToken.getAccessToken(), agentToken.getAgentId(), date, date, 1);
       }
     }
+
   }
 
 
@@ -214,8 +217,8 @@ public class AdUnitReportServiceImpl implements IAdUnitReportService {
    */
   private void getAgentReport(String token, Long agentId, String startDate, String endDate, int page) {
     try {
+      Integer page_size = 1000;
       log.info("=====获取代理商数据信息====page--{}===《《《《《", page);
-
       Map<String, String> headers = new HashMap<String, String>();
       headers.put("Content-Type", " application/json");
       headers.put("Access-Token", token);
@@ -223,7 +226,7 @@ public class AdUnitReportServiceImpl implements IAdUnitReportService {
       param.put("start_date", startDate);
       param.put("end_date", endDate);
       param.put("agent_id", agentId);
-      param.put("page_size", 1000);
+      param.put("page_size", page_size);
       param.put("page", page);
 
       String result = HttpUtils.httpPostRequest(postUrl + KuaishouConstant.ANGENT_REPORT, param, headers);
@@ -259,10 +262,20 @@ public class AdUnitReportServiceImpl implements IAdUnitReportService {
       if (!Check.isNull(agentList)) {
         adUnitReportMapper.replaceBatchAgentReport(agentList);
       }
-      getAgentReport(token, agentId, startDate, endDate, page + 1);
+      //是否有下一页
+      Boolean toGet = true;
+      if (details.size() < page_size) {
+        toGet = false;
+      }
+      if (toGet) {
+        getAgentReport(token, agentId, startDate, endDate, page + 1);
+      } else {
+        log.info("快手代理商数据获取完成========");
+      }
     } catch (Exception e) {
       e.printStackTrace();
     }
+
   }
 
 
@@ -284,6 +297,7 @@ public class AdUnitReportServiceImpl implements IAdUnitReportService {
   @Override
     public void getKuaishouAccountAdSceneDaily(Long advertiserId, String accessToken, String startDate, String endDate, int page,String temporalGranularity,List<String> reportDims) {
       log.info("获取账户日报====广告场景====,账户id:{},开始时间:{},结束时间:{},page--{}", advertiserId, startDate, endDate,page);
+      Integer page_size = 2000;
       String url = postUrl + KuaishouConstant.ACCOUNT_REPORT;
       Map<String, String> headers = new HashMap<>();
       headers.put("Access-Token", accessToken);
@@ -293,7 +307,7 @@ public class AdUnitReportServiceImpl implements IAdUnitReportService {
       param.put("advertiser_id", advertiserId);
       param.put("temporal_granularity", temporalGranularity);
       param.put("report_dims", reportDims);
-      param.put("page_size", 2000);
+      param.put("page_size", page_size);
       param.put("page", page);
       String result = HttpUtils.httpPostRequest(url, param, headers);
       JSONObject resultJson = JSONObject.parseObject(result);
@@ -318,8 +332,17 @@ public class AdUnitReportServiceImpl implements IAdUnitReportService {
       if (!Check.isNull(addList)) {
         accountAdSceneReportDailyMapper.replaceBatchKsAccountAdScene(addList);
       }
-      getKuaishouAccountAdSceneDaily(advertiserId, accessToken, startDate, endDate, page + 1,temporalGranularity,reportDims);
-      log.info("账户日报数据====广告场景=====获取完成:accountId:{}", advertiserId);
+      //是否有下一页
+      Boolean toGet = true;
+      if (details.size() < page_size) {
+        toGet = false;
+      }
+      if (toGet) {
+        getKuaishouAccountAdSceneDaily(advertiserId, accessToken, startDate, endDate, page + 1,temporalGranularity,reportDims);
+      } else {
+        log.info("账户日报数据====广告场景=====获取完成:accountId:{}", advertiserId);
+      }
+
     }
 
 
@@ -337,6 +360,7 @@ public class AdUnitReportServiceImpl implements IAdUnitReportService {
   public void crowdAnalysisReport(Long advertiserId, String accessToken, String date, List<Long> campaignIds,List<Long> unitIds, List<String> reportDims,Integer page) {
     log.info("获取人群报表===={}====,账户id:{},开始时间:{},结束时间:{},page==={}", reportDims,advertiserId, date, date,page);
     try {
+        Integer page_size = 2000;
         Map<String, String> headers = new HashMap<>();
         headers.put("Access-Token", accessToken);
         headers.put("Content-Type", "application/json");
@@ -348,11 +372,9 @@ public class AdUnitReportServiceImpl implements IAdUnitReportService {
         requestJson.put("unit_ids", unitIds);
         requestJson.put("report_dims", reportDims);
         requestJson.put("page", page);
-        requestJson.put("page_size", 2000);
-
+        requestJson.put("page_size", page_size);
         String result = HttpUtils.httpPostRequest(postUrl + KuaishouConstant.AUDIENCE_REPORT, requestJson, headers);
         JSONObject resultJson = JSONObject.parseObject(result);
-
         Integer code = resultJson.getInteger("code");
         if (null == code || code != 0) {
           log.error("获取快手======{}===人群报表=====异常:{},accountId:{}",reportDims, resultJson.getString("message"), advertiserId);
@@ -363,12 +385,10 @@ public class AdUnitReportServiceImpl implements IAdUnitReportService {
           log.error("快手人群报表==={}=====返回详情为空,accountId:{}", reportDims,advertiserId);
           return;
         }
-
       List<KuaishouAudienceReportDaily> provinceList = new ArrayList<>();
       List<KuaishouAudienceReportDaily> cityList = new ArrayList<>();
       List<KuaishouAudienceReportDaily> ageList = new ArrayList<>();
       List<KuaishouAudienceReportDaily> genderList = new ArrayList<>();
-
         for (int i = 0; i < details.size(); i++) {
           JSONObject returnJson = details.getJSONObject(i);
           returnJson.put("advertiser_id",advertiserId);
@@ -403,9 +423,16 @@ public class AdUnitReportServiceImpl implements IAdUnitReportService {
         if (reportDims.contains("gender")){
           audienceReportDailyMapper.replaceBatchKsAudienceGender(genderList);
       }
-
-      crowdAnalysisReport(advertiserId, accessToken, date,campaignIds,unitIds, reportDims,page+1);
+      //是否有下一页
+      Boolean toGet = true;
+      if (details.size() < page_size) {
+        toGet = false;
+      }
+      if (toGet) {
+        crowdAnalysisReport(advertiserId, accessToken, date,campaignIds,unitIds, reportDims,page+1);
+      } else {
         log.info("快手人群报表==={}==========获取完成:accountId:{}", reportDims,advertiserId);
+      }
     }catch (Exception e){
       log.info(String.valueOf(e));
     }
@@ -421,12 +448,13 @@ public class AdUnitReportServiceImpl implements IAdUnitReportService {
   @Override
   public void getKuaishouDailyFlows(Long advertiserId, String accessToken,int page) {
     log.info("获取账户流水========,账户id:{},page--{}", advertiserId,page);
+    Integer page_size = 500;
     String url = postUrl + KuaishouConstant.FUND_DAILY_FLOWS;
     Map<String, String> headers = new HashMap<>();
     headers.put("Access-Token", accessToken);
     Map<String, Object> param = new HashMap<>();
     param.put("advertiser_id", advertiserId);
-    param.put("page_size", 500);
+    param.put("page_size", page_size);
     param.put("page", page);
     String result = HttpUtils.httpPostRequest(url, param, headers);
     JSONObject resultJson = JSONObject.parseObject(result);
@@ -450,10 +478,51 @@ public class AdUnitReportServiceImpl implements IAdUnitReportService {
     if (!Check.isNull(addList)) {
       fundDailyFlowsMapper.replaceBatchKsFundDailyFlows(addList);
     }
-    getKuaishouDailyFlows(advertiserId, accessToken, page + 1);
-    log.info("账户流水========获取完成:accountId:{}", advertiserId);
+    //是否有下一页
+    Boolean toGet = true;
+    if (details.size() < page_size) {
+      toGet = false;
+    }
+    if (toGet) {
+      getKuaishouDailyFlows(advertiserId, accessToken, page + 1);
+    } else {
+      log.info("账户流水========获取完成:accountId:{}", advertiserId);
+    }
+
+  }
 
 
+  /**
+   *  快手-账户余额
+   * @param advertiserId
+   */
+  @Override
+  public void getKuaishouDailyFundGet(Long advertiserId,String accessToken) {
+    log.info("获取账户余额========,账户id:{}", advertiserId);
+    String url = postUrl + KuaishouConstant.FUND_GET;
+    Map<String, String> headers = new HashMap<>();
+    headers.put("Access-Token", accessToken);
+    Map<String, Object> param = new HashMap<>();
+    param.put("advertiser_id", advertiserId);
+    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("获取快手账户余额======异常:{},accountId:{}", message, advertiserId);
+      return;
+    }
+    JSONObject details = resultJson.getJSONObject("data");
+    if (null == details || details.size() <= 0) {
+      log.error("快手账户余额=====返回详情为空,accountId:{}", advertiserId);
+      return;
+    }
+    details.put("advertiser_id",advertiserId);
+    KuaishouAdvertiserFundGet reportDaily = KuaishouAdvertiserFundGet.getKuaishouFundGetInfo(details);
+    if (!Check.isNull(reportDaily)) {
+      fundDailyFlowsMapper.replaceBatchKsFundGetBalance(reportDaily);
+    }
+    log.info("账户余额========获取完成:accountId:{}", advertiserId);
 
   }
 

+ 29 - 0
job-kuaishou/src/main/java/cn/com/ctop/job/kuaishou/handler/AdUnitReportJob.java

@@ -225,6 +225,35 @@ public class AdUnitReportJob {
 
 
 
+  /**
+   * 快手-账户余额
+   *
+   * @throws Exception
+   */
+  @XxlJob("kuaishouFundGetBalanceJob")
+  public void kuaishouFundGetBalanceJob() 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;
+    }
+    dailyExecutorService.submit(
+            new Runnable() {
+              @Override
+              public void run() {
+                adUnitReportService.getKuaishouDailyFundGet(Long.valueOf(param), token);
+              }
+            });
+  }
+
+
+
+