Browse Source

头条日报消耗拉取

lining 1 year ago
parent
commit
577fbbd3c5

+ 11 - 4
job-bytedance/src/main/java/cn/com/ctop/job/bytedance/controller/BytedanceController.java

@@ -1,9 +1,7 @@
 package cn.com.ctop.job.bytedance.controller;
 
-import cn.com.ctop.job.bytedance.data.service.IAdminAdvertiserListService;
-import cn.com.ctop.job.bytedance.data.service.IBytedanceAccountReportService;
-import cn.com.ctop.job.bytedance.data.service.IBytedanceProductReportService;
-import cn.com.ctop.job.bytedance.data.service.IBytedancePromoterReportService;
+import cn.com.ctop.job.bytedance.data.enums.AccountTypeEnum;
+import cn.com.ctop.job.bytedance.data.service.*;
 import cn.com.ctop.job.bytedance.data.utils.Check;
 import com.alibaba.fastjson.JSONObject;
 import io.swagger.annotations.Api;
@@ -38,6 +36,9 @@ public class BytedanceController {
     private IAdminAdvertiserListService adminAdvertiserListService;
 
     @Autowired
+    private IByteDanceFlowDetailsReportService byteDanceFlowDetailsReportService;
+
+    @Autowired
     private IBytedanceAccountReportService accountReportService;
 
     @Autowired
@@ -93,5 +94,11 @@ public class BytedanceController {
 
     }
 
+    @GetMapping("/flowDetailsReport")
+    public void flowDetailsReport(@RequestParam(required = false) Long advertiserId, String accessToken, String startDate, String endDate) {
+
+        byteDanceFlowDetailsReportService.getBytedanceFlowDetailsDailyReport(advertiserId, accessToken, AccountTypeEnum.AD.getValue(), startDate, endDate, 1);
+
+    }
 
 }

+ 78 - 0
job-bytedance/src/main/java/cn/com/ctop/job/bytedance/data/entity/ByteDanceFlowDetailsReportDaily.java

@@ -0,0 +1,78 @@
+package cn.com.ctop.job.bytedance.data.entity;
+
+import lombok.Builder;
+import lombok.Data;
+
+import java.math.BigDecimal;
+
+/**
+ * @author lining
+ * @ClassName ByteDanceFlowDetailsReport
+ * @description: TODO
+ * @date 2025年04月16日
+ * @version: 1.0
+ */
+@Data
+@Builder
+public class ByteDanceFlowDetailsReportDaily {
+    /**
+     * 广告主ID
+     */
+    private Long advertiserId;
+    /**
+     * 数据起始时间
+     */
+    private Integer statTimeDay;
+
+    /**
+     * 日终结余(单位元)
+     */
+    private BigDecimal balance;
+    /**
+     * 日终赠款结余(单位元)。包括冻结和实结订单pending部分
+     */
+    private BigDecimal grantBalance;
+    /**
+     * 日终非赠款结余(单位元)。包括冻结和实结订单pending部分
+     */
+    private BigDecimal nonGrantBalance;
+    /**
+     * 现金支出(单位元)
+     */
+    private BigDecimal cashCost;
+    /**
+     * 总支出(单位元)
+     */
+    private BigDecimal cost;
+
+    /**
+     * 冻结(单位元)
+     */
+    private BigDecimal frozen;
+    /**
+     * 总存入(单位元)
+     */
+    private BigDecimal income;
+    /**
+     * 赠款支出(单位元)
+     */
+    private BigDecimal rewardCost;
+
+    /**
+     * 共享返货支出(单位元)
+     */
+    private BigDecimal sharedWalletCost;
+    /**
+     * 账户消耗的子钱包的共享余额(单位元)
+     */
+    private BigDecimal companyWalletCost;
+    /**
+     * 总转入(单位元)
+     */
+    private BigDecimal transferIn;
+
+    /**
+     * 总转出(单位元)
+     */
+    private BigDecimal transferOut;
+}

+ 22 - 0
job-bytedance/src/main/java/cn/com/ctop/job/bytedance/data/enums/AccountTypeEnum.java

@@ -0,0 +1,22 @@
+package cn.com.ctop.job.bytedance.data.enums;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+/**
+ * @author lining
+ * @ClassName FlowTransactionTypeEnum
+ * @description: TODO
+ * @date 2025年04月21日
+ * @version: 1.0
+ */
+@AllArgsConstructor
+@Getter
+public enum AccountTypeEnum {
+    AD("AD"),
+    LOCAL("LOCAL"),
+    STAR("STAR")
+    ;
+
+    private String value;
+}

+ 18 - 0
job-bytedance/src/main/java/cn/com/ctop/job/bytedance/data/mapper/ByteDanceFlowDetailsReportMapper.java

@@ -0,0 +1,18 @@
+package cn.com.ctop.job.bytedance.data.mapper;
+
+import cn.com.ctop.job.bytedance.data.entity.ByteDanceFlowDetailsReportDaily;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
+
+/**
+ * @author lining
+ * @ClassName ByteDanceFlowDetailsReportMapper
+ * @description: TODO
+ * @date 2025年04月17日
+ * @version: 1.0
+ */
+public interface ByteDanceFlowDetailsReportMapper {
+
+    void replaceDanceFlowDetailsReportDailies(@Param("list") List<ByteDanceFlowDetailsReportDaily> dailyList);
+}

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

@@ -0,0 +1,42 @@
+<?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.ByteDanceFlowDetailsReportMapper">
+    <insert id="replaceDanceFlowDetailsReportDailies">
+        replace into bytedance_flow_details_report_daily (
+            advertiser_id,
+            stat_time_day,
+        balance,
+        grant_balance,
+        non_grant_balance,
+        cash_cost,
+        cost,
+        frozen,
+        income,
+        reward_cost,
+        shared_wallet_cost,
+        company_wallet_cost,
+        transfer_in,
+        transfer_out
+        )
+        values
+        <foreach collection="list" item="item" separator=",">
+            (
+            #{item.advertiserId},
+            #{item.statTimeDay},
+            #{item.balance},
+            #{item.grantBalance},
+            #{item.nonGrantBalance},
+            #{item.cashCost},
+            #{item.cost},
+            #{item.frozen},
+            #{item.income},
+            #{item.rewardCost},
+            #{item.sharedWalletCost},
+            #{item.companyWalletCost},
+            #{item.transferIn},
+            #{item.transferOut}
+            )
+        </foreach>
+    </insert>
+</mapper>
+

+ 13 - 0
job-bytedance/src/main/java/cn/com/ctop/job/bytedance/data/service/IByteDanceFlowDetailsReportService.java

@@ -0,0 +1,13 @@
+package cn.com.ctop.job.bytedance.data.service;
+
+/**
+ * @author lining
+ * @ClassName IByteDanceFlowDetailsReportService
+ * @description: TODO
+ * @date 2025年04月16日
+ * @version: 1.0
+ */
+public interface IByteDanceFlowDetailsReportService {
+
+    void getBytedanceFlowDetailsDailyReport(Long advertiserId, String accessToken, String transactionType, String startDate, String endDate, Integer page);
+}

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

@@ -0,0 +1,124 @@
+package cn.com.ctop.job.bytedance.data.service.impl;
+
+import cn.com.ctop.job.bytedance.data.entity.ByteDanceFlowDetailsReportDaily;
+import cn.com.ctop.job.bytedance.data.mapper.ByteDanceFlowDetailsReportMapper;
+import cn.com.ctop.job.bytedance.data.service.IByteDanceFlowDetailsReportService;
+import cn.com.ctop.job.bytedance.data.utils.Check;
+import cn.com.ctop.job.bytedance.data.utils.HttpUtils;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import java.math.BigDecimal;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author lining
+ * @ClassName ByteDanceFlowDetailsReportServiceImpl
+ * @description: TODO
+ * @date 2025年04月16日
+ * @version: 1.0
+ */
+@Service
+@Slf4j
+public class ByteDanceFlowDetailsReportServiceImpl implements IByteDanceFlowDetailsReportService {
+
+    @Resource
+    private ByteDanceFlowDetailsReportMapper byteDanceFlowDetailsReportMapper;
+
+    @Override
+    public void getBytedanceFlowDetailsDailyReport(Long advertiserId, String accessToken, String accountType, String startDate, String endDate, Integer page) {
+        String url = "https://ad.oceanengine.com/open_api/2/advertiser/fund/daily_stat/";
+        Map<String, Object> paramsMap = new HashMap<>();
+        paramsMap.put("advertiser_id", advertiserId);
+        paramsMap.put("start_date", startDate);
+        paramsMap.put("end_date", endDate);
+        /**
+         * 账户业务类型,可选值:
+         * AD AD广告主(默认)
+         * LOCAL 本地推
+         * STAR 星图
+         */
+        paramsMap.put("account_type", accountType);
+        paramsMap.put("page", page);
+        paramsMap.put("page_size", 100);
+        Map<String, String> headers = new HashMap<>();
+        headers.put("Access-Token", accessToken);
+        headers.put("Content-Type", "application/json");
+        String s = HttpUtils.httpGet(url, paramsMap, headers);
+        JSONObject result = JSONObject.parseObject(s);
+        if (Check.isNull(result)) {
+            log.info("账户id:{},广告流水详情请求结果返回为空", advertiserId);
+            return;
+        }
+
+        List<ByteDanceFlowDetailsReportDaily> dailies = new ArrayList<>();
+        Integer code = result.getInteger("code");
+        Integer totalPage = 1;
+        if (code == 0) {
+            JSONObject data = result.getJSONObject("data");
+            if (!Check.isNull(data)) {
+                JSONArray list = data.getJSONArray("list");
+                totalPage = data.getJSONObject("page_info").getIntValue("total_page");
+                if (!Check.isNull(list)) {
+                    for (int i = 0; i < list.size(); i++) {
+                        JSONObject object = list.getJSONObject(i);
+                        String advertiser_id = object.getString("advertiser_id");
+                        String date = object.getString("date").replace("-", "");
+                        BigDecimal balance = object.getBigDecimal("balance");
+                        BigDecimal grant_balance = object.getBigDecimal("grant_balance");
+                        BigDecimal non_grant_balance = object.getBigDecimal("non_grant_balance");
+                        BigDecimal cash_cost = object.getBigDecimal("cash_cost");
+                        BigDecimal cost = object.getBigDecimal("cost");
+                        BigDecimal frozen = object.getBigDecimal("frozen");
+                        BigDecimal income = object.getBigDecimal("income");
+                        BigDecimal reward_cost = object.getBigDecimal("reward_cost");
+                        BigDecimal shared_wallet_cost = object.getBigDecimal("shared_wallet_cost");
+                        BigDecimal company_wallet_cost = object.getBigDecimal("company_wallet_cost");
+                        BigDecimal transfer_in = object.getBigDecimal("transfer_in");
+                        BigDecimal transfer_out = object.getBigDecimal("transfer_out");
+
+
+                        ByteDanceFlowDetailsReportDaily daily = ByteDanceFlowDetailsReportDaily.builder()
+                                .advertiserId(Long.parseLong(advertiser_id))
+                                .statTimeDay(Integer.parseInt(date))
+                                .balance(balance)
+                                .grantBalance(grant_balance)
+                                .nonGrantBalance(non_grant_balance)
+                                .cashCost(cash_cost)
+                                .cost(cost)
+                                .frozen(frozen)
+                                .income(income)
+                                .rewardCost(reward_cost)
+                                .sharedWalletCost(shared_wallet_cost)
+                                .companyWalletCost(company_wallet_cost)
+                                .transferIn(transfer_in)
+                                .transferOut(transfer_out)
+                                .build();
+                        dailies.add(daily);
+                    }
+
+                    if (!Check.isNull(dailies)) {
+                        log.info("账户id:{},流水日报请求结果返回成功", advertiserId);
+                        byteDanceFlowDetailsReportMapper.replaceDanceFlowDetailsReportDailies(dailies);
+                    }
+                }
+
+            }
+        }
+
+        if (page < totalPage) {
+            try {
+                Thread.sleep(500L);
+            } catch (InterruptedException e) {
+                throw new RuntimeException(e);
+            }
+            getBytedanceFlowDetailsDailyReport(advertiserId, accessToken, accountType, startDate, endDate, page + 1);
+        }
+    }
+}

+ 55 - 0
job-bytedance/src/main/java/cn/com/ctop/job/bytedance/handler/ByteDanceFlowDetailsReportJob.java

@@ -0,0 +1,55 @@
+package cn.com.ctop.job.bytedance.handler;
+
+import cn.com.ctop.job.bytedance.data.enums.AccountTypeEnum;
+import cn.com.ctop.job.bytedance.data.service.IByteDanceFlowDetailsReportService;
+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.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+/**
+ * @author lining
+ * @ClassName ByteDanceFlowDetailsReportJob
+ * @description: TODO
+ * @date 2025年04月16日
+ * @version: 1.0
+ */
+@Slf4j
+@Component
+public class ByteDanceFlowDetailsReportJob {
+
+    @Autowired
+    private IByteDanceFlowDetailsReportService byteDanceFlowDetailsReportService;
+
+    private static ExecutorService executorService = Executors.newFixedThreadPool(2);
+
+    @XxlJob("flowDetailsDaily")
+    public void flowDetailsDaily() throws Exception {
+        String param = XxlJobHelper.getJobParam(); // 执行参数
+        if (Check.isNull(param)) {
+            log.error("入参为空");
+            return;
+        }
+
+        String nowDate = DateUtils.getNowDate("yyyy-MM-dd");
+        String data = DateUtils.getAnotherDay("yyyy-MM-dd", nowDate, -1);
+        String[] s = param.split("_");
+        Long advertiserId = Long.valueOf(s[0]);
+        String accessToken = s[1];
+
+        executorService.submit(new Runnable() {
+            @Override
+            public void run() {
+                if (!Check.isNull(accessToken)) {
+                    byteDanceFlowDetailsReportService.getBytedanceFlowDetailsDailyReport(advertiserId, accessToken, AccountTypeEnum.AD.getValue(), data, data, 1);
+                }
+            }
+        });
+    }
+}