Quellcode durchsuchen

获取账户余额信息

yumeng vor 2 Tagen
Ursprung
Commit
bd1ef99848

+ 124 - 0
job-adq/src/main/java/cn/com/ctop/job/adq/data/entity/AdqAdvertiserFundsDaily.java

@@ -0,0 +1,124 @@
+package cn.com.ctop.job.adq.data.entity;
+
+/**
+ * 广点通账户资金余额日快照对象 media_adq_advertiser_funds_daily
+ */
+public class AdqAdvertiserFundsDaily {
+
+    /**
+     * 广告主ID
+     */
+    private Long advertiserId;
+
+    /**
+     * 快照日期(yyyyMMdd)
+     */
+    private Long statTimeDay;
+
+    /**
+     * 资金账户类型(如GENERAL_CASH/GENERAL_SHARED/GENERAL_GIFT/BANK)
+     */
+    private String fundType;
+
+    /**
+     * 余额(分)
+     */
+    private Long balance;
+
+    /**
+     * 锁定金额(分)
+     */
+    private Long billDepositAmount;
+
+    /**
+     * 资金状态
+     */
+    private String fundStatus;
+
+    /**
+     * 今日消耗(分)
+     */
+    private Long realtimeCost;
+
+    /**
+     * 单条资金账户原始JSON
+     */
+    private String rawResponse;
+
+    public Long getAdvertiserId() {
+        return advertiserId;
+    }
+
+    public void setAdvertiserId(Long advertiserId) {
+        this.advertiserId = advertiserId;
+    }
+
+    public Long getStatTimeDay() {
+        return statTimeDay;
+    }
+
+    public void setStatTimeDay(Long statTimeDay) {
+        this.statTimeDay = statTimeDay;
+    }
+
+    public String getFundType() {
+        return fundType;
+    }
+
+    public void setFundType(String fundType) {
+        this.fundType = fundType;
+    }
+
+    public Long getBalance() {
+        return balance;
+    }
+
+    public void setBalance(Long balance) {
+        this.balance = balance;
+    }
+
+    public Long getBillDepositAmount() {
+        return billDepositAmount;
+    }
+
+    public void setBillDepositAmount(Long billDepositAmount) {
+        this.billDepositAmount = billDepositAmount;
+    }
+
+    public String getFundStatus() {
+        return fundStatus;
+    }
+
+    public void setFundStatus(String fundStatus) {
+        this.fundStatus = fundStatus;
+    }
+
+    public Long getRealtimeCost() {
+        return realtimeCost;
+    }
+
+    public void setRealtimeCost(Long realtimeCost) {
+        this.realtimeCost = realtimeCost;
+    }
+
+    public String getRawResponse() {
+        return rawResponse;
+    }
+
+    public void setRawResponse(String rawResponse) {
+        this.rawResponse = rawResponse;
+    }
+
+    @Override
+    public String toString() {
+        return "AdqAdvertiserFundsDaily{" +
+                "advertiserId=" + advertiserId +
+                ", statTimeDay=" + statTimeDay +
+                ", fundType='" + fundType + '\'' +
+                ", balance=" + balance +
+                ", billDepositAmount=" + billDepositAmount +
+                ", fundStatus='" + fundStatus + '\'' +
+                ", realtimeCost=" + realtimeCost +
+                '}';
+    }
+}

+ 11 - 0
job-adq/src/main/java/cn/com/ctop/job/adq/data/mapper/AdqAdvertiserFundsMapper.java

@@ -0,0 +1,11 @@
+package cn.com.ctop.job.adq.data.mapper;
+
+import cn.com.ctop.job.adq.data.entity.AdqAdvertiserFundsDaily;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
+
+public interface AdqAdvertiserFundsMapper {
+
+    void addAdqAdvertiserFunds(@Param("list") List<AdqAdvertiserFundsDaily> list);
+}

+ 30 - 0
job-adq/src/main/java/cn/com/ctop/job/adq/data/mapper/xml/AdqAdvertiserFundsMapper.xml

@@ -0,0 +1,30 @@
+<?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.adq.data.mapper.AdqAdvertiserFundsMapper">
+
+    <insert id="addAdqAdvertiserFunds">
+        replace into media_adq_advertiser_funds_daily (
+        advertiser_id,
+        stat_time_day,
+        fund_type,
+        balance,
+        bill_deposit_amount,
+        fund_status,
+        realtime_cost,
+        raw_response
+        ) VALUES
+        <foreach collection="list" item="item" separator=",">
+            (
+            #{item.advertiserId},
+            #{item.statTimeDay},
+            #{item.fundType},
+            #{item.balance},
+            #{item.billDepositAmount},
+            #{item.fundStatus},
+            #{item.realtimeCost},
+            #{item.rawResponse}
+            )
+        </foreach>
+    </insert>
+
+</mapper>

+ 5 - 0
job-adq/src/main/java/cn/com/ctop/job/adq/data/service/IAdqAdvertiserFundsService.java

@@ -0,0 +1,5 @@
+package cn.com.ctop.job.adq.data.service;
+
+public interface IAdqAdvertiserFundsService {
+    void getAdqAccountFunds(Long advertiserId, String accessToken, String fundsDate, Integer isLog, Long errId, Integer isSend);
+}

+ 107 - 0
job-adq/src/main/java/cn/com/ctop/job/adq/data/service/impl/AdqAdvertiserFundsServiceImpl.java

@@ -0,0 +1,107 @@
+package cn.com.ctop.job.adq.data.service.impl;
+
+import cn.com.ctop.job.adq.data.entity.AdqAdvertiserFundsDaily;
+import cn.com.ctop.job.adq.data.entity.AdqErrorLog;
+import cn.com.ctop.job.adq.data.mapper.AdqAdvertiserFundsMapper;
+import cn.com.ctop.job.adq.data.mapper.AdqErrorLogMapper;
+import cn.com.ctop.job.adq.data.service.IAdqAdvertiserFundsService;
+import cn.com.ctop.job.adq.data.utils.AlertUtils;
+import cn.com.ctop.job.adq.data.utils.Check;
+import cn.com.ctop.job.adq.data.utils.HttpUtils;
+import cn.com.ctop.job.adq.utils.RandomStringGenerator;
+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.stereotype.Service;
+
+import java.time.Instant;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+@Slf4j
+@Service
+public class AdqAdvertiserFundsServiceImpl implements IAdqAdvertiserFundsService {
+    @Autowired
+    private AdqAdvertiserFundsMapper adqAdvertiserFundsMapper;
+    @Autowired
+    private AdqErrorLogMapper adqErrorLogMapper;
+
+    @Override
+    public void getAdqAccountFunds(Long advertiserId, String accessToken, String fundsDate, Integer isLog, Long errId, Integer isSend) {
+        try {
+            Map<String, Object> paramsMap = new HashMap<>();
+            paramsMap.put("access_token", accessToken);
+            long timestamp = Instant.now().getEpochSecond();
+            paramsMap.put("timestamp", String.valueOf(timestamp));
+            paramsMap.put("nonce", RandomStringGenerator.generateRandomString(32));
+            paramsMap.put("account_id", advertiserId);
+            List<AdqAdvertiserFundsDaily> fundsList = new ArrayList<>();
+            String s = HttpUtils.httpGet("https://api.e.qq.com/v3.0/funds/get", paramsMap, null);
+            JSONObject jsonObject = JSONObject.parseObject(s);
+            if (Check.isNull(jsonObject)) {
+                log.error("请求账户资金余额失败,account_id:{},返回为空。", advertiserId);
+                insertErrorLog(isLog, advertiserId, fundsDate, null);
+                return;
+            }
+            Integer code = jsonObject.getInteger("code");
+            if (code != 0) {
+                log.error("请求账户资金余额失败,account_id:{},返回信息:{}", advertiserId, jsonObject);
+                insertErrorLog(isLog, advertiserId, fundsDate, null);
+                return;
+            }
+            JSONObject data = jsonObject.getJSONObject("data");
+            if (!Check.isNull(data)) {
+                JSONArray list = data.getJSONArray("list");
+                if (!Check.isNull(list)) {
+                    Long statTimeDay = Long.valueOf(fundsDate.replace("-", ""));
+                    for (int i = 0; i < list.size(); i++) {
+                        JSONObject fundJson = list.getJSONObject(i);
+                        AdqAdvertiserFundsDaily funds = new AdqAdvertiserFundsDaily();
+                        funds.setAdvertiserId(advertiserId);
+                        funds.setStatTimeDay(statTimeDay);
+                        funds.setFundType(fundJson.getString("fund_type"));
+                        funds.setBalance(fundJson.getLong("balance"));
+                        funds.setBillDepositAmount(fundJson.getLong("bill_deposit_amount"));
+                        funds.setFundStatus(fundJson.getString("fund_status"));
+                        funds.setRealtimeCost(fundJson.getLong("realtime_cost"));
+                        funds.setRawResponse(fundJson.toJSONString());
+                        fundsList.add(funds);
+                    }
+                }
+            }
+            if (!Check.isNull(fundsList)) {
+                adqAdvertiserFundsMapper.addAdqAdvertiserFunds(fundsList);
+            }
+            log.info("请求账户资金余额完成,account_id:{},日期:{},条数:{}", advertiserId, fundsDate, fundsList.size());
+            if (errId != null) {
+                adqErrorLogMapper.deleteInfoByErrorId(errId);
+            }
+            if (isSend != null && isSend == 1) {
+                AlertUtils.sendMessage(advertiserId, fundsDate, fundsDate, 3, "daily", 1, "funds");
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+            insertErrorLog(isLog, advertiserId, fundsDate, e.getMessage());
+        }
+    }
+
+    private void insertErrorLog(Integer isLog, Long advertiserId, String fundsDate, String msg) {
+        if (isLog != null && isLog == 1) {
+            try {
+                AdqErrorLog errorInfo = new AdqErrorLog();
+                errorInfo.setAdvertiserId(advertiserId);
+                errorInfo.setStartDate(fundsDate);
+                errorInfo.setEndDate(fundsDate);
+                errorInfo.setDataDimension("funds");
+                errorInfo.setDataType("daily");
+                errorInfo.setMsg(msg);
+                adqErrorLogMapper.replaceErrorInfo(errorInfo);
+            } catch (Exception ex) {
+                log.error("记录账户资金余额失败日志异常,account_id:{}", advertiserId, ex);
+            }
+        }
+    }
+}

+ 42 - 0
job-adq/src/main/java/cn/com/ctop/job/adq/handler/AdqAccountReportJob.java

@@ -28,10 +28,13 @@ public class AdqAccountReportJob {
     private IAdqAccountReportService accountReportService;
     @Autowired
     private IAdqMaterialReportService adqMaterialReportService;
+    @Autowired
+    private IAdqAdvertiserFundsService adqAdvertiserFundsService;
     private static ExecutorService nowDateHourExecutorService = Executors.newFixedThreadPool(5);
     private static ExecutorService yesDateHourExecutorService = Executors.newFixedThreadPool(5);
     private static ExecutorService dailyExecutorService = Executors.newFixedThreadPool(5);
     private static ExecutorService balanceExecutorService = Executors.newFixedThreadPool(2);
+    private static ExecutorService fundsExecutorService = Executors.newFixedThreadPool(2);
     @Autowired
     private IDailyBalanceReportService dailyBalanceReportService;
 
@@ -165,6 +168,45 @@ public class AdqAccountReportJob {
         });
     }
 
+    @XxlJob("nowDateAccountFunds")
+    public void nowDateAccountFunds() throws Exception {
+        String param = XxlJobHelper.getJobParam(); // 执行参数
+        if (Check.isNull(param)) {
+            log.error("入参为空");
+            return;
+        }
+        String nowDate = DateUtils.getNowDate("yyyy-MM-dd");
+        String[] s = param.split("_");
+        Long advertiserId = Long.valueOf(s[0]);
+        String accessToken = s[1];
+        fundsExecutorService.submit(new Runnable() {
+            @Override
+            public void run() {
+                adqAdvertiserFundsService.getAdqAccountFunds(advertiserId, accessToken, nowDate, 1, null, 0);
+            }
+        });
+    }
+
+    @XxlJob("yesDateAccountFunds")
+    public void yesDateAccountFunds() throws Exception {
+        String param = XxlJobHelper.getJobParam(); // 执行参数
+        if (Check.isNull(param)) {
+            log.error("入参为空");
+            return;
+        }
+        String nowDate = DateUtils.getNowDate("yyyy-MM-dd");
+        String yesDate = DateUtils.getAnotherDay("yyyy-MM-dd", nowDate, -1);
+        String[] s = param.split("_");
+        Long advertiserId = Long.valueOf(s[0]);
+        String accessToken = s[1];
+        fundsExecutorService.submit(new Runnable() {
+            @Override
+            public void run() {
+                adqAdvertiserFundsService.getAdqAccountFunds(advertiserId, accessToken, yesDate, 1, null, 0);
+            }
+        });
+    }
+
     @XxlJob("yesDateBalance")
     public void yesDateBalance() throws Exception {
         String param = XxlJobHelper.getJobParam(); // 执行参数

+ 13 - 0
job-adq/src/main/java/cn/com/ctop/job/adq/handler/AdqRetryReportJob.java

@@ -35,6 +35,8 @@ public class AdqRetryReportJob {
 
     private static ExecutorService flowExecutorService = Executors.newFixedThreadPool(2);
 
+    private static ExecutorService fundsExecutorService = Executors.newFixedThreadPool(2);
+
     @Autowired
     private IAdqCreativeReportService adqReportService;
     @Autowired
@@ -46,6 +48,8 @@ public class AdqRetryReportJob {
     @Autowired
     private IAdqMaterialReportService adqMaterialReportService;
     @Autowired
+    private IAdqAdvertiserFundsService adqAdvertiserFundsService;
+    @Autowired
     private IDailyBalanceReportService dailyBalanceReportService;
 
     @XxlJob("adqRetryReport")
@@ -158,6 +162,15 @@ public class AdqRetryReportJob {
                             }
                         }
                     });
+                } else if ("funds".equals(dataDimension)) {
+                    fundsExecutorService.submit(new Runnable() {
+                        @Override
+                        public void run() {
+                            if (!Check.isNull(accessToken)) {
+                                adqAdvertiserFundsService.getAdqAccountFunds(advertiserId, accessToken, startDate, null, id, isSend);
+                            }
+                        }
+                    });
                 }else if("daily_balance_report".equals(dataDimension)){
                     blanceExecutorService.submit(new Runnable() {
                         @Override