|
|
@@ -1,7 +1,9 @@
|
|
|
package cn.com.ctop.job.kuaishou.data.service.impl;
|
|
|
|
|
|
+import cn.com.ctop.job.kuaishou.data.constant.KuaishouLinkConstants;
|
|
|
import cn.com.ctop.job.kuaishou.data.entity.KuaishouAccountReportDaily;
|
|
|
import cn.com.ctop.job.kuaishou.data.entity.KuaishouAccountReportHour;
|
|
|
+import cn.com.ctop.job.kuaishou.data.entity.KuaishouAdvertiserFund;
|
|
|
import cn.com.ctop.job.kuaishou.data.entity.KuaishouDailyFlows;
|
|
|
import cn.com.ctop.job.kuaishou.data.mapper.KuaiShouAccountReportMapper;
|
|
|
import cn.com.ctop.job.kuaishou.data.service.IKuaiShouAccountReportService;
|
|
|
@@ -9,6 +11,7 @@ import cn.com.ctop.job.kuaishou.data.service.IKuaiShouErrorLogService;
|
|
|
import cn.com.ctop.job.kuaishou.data.utils.Check;
|
|
|
import cn.com.ctop.job.kuaishou.data.utils.DateUtils;
|
|
|
import cn.com.ctop.job.kuaishou.data.utils.HttpUtils;
|
|
|
+import cn.com.ctop.job.kuaishou.data.utils.RateLimitUtil;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
@@ -29,16 +32,13 @@ public class KuaiShouAccountReportServiceImpl implements IKuaiShouAccountReportS
|
|
|
private IKuaiShouErrorLogService errorLogService;
|
|
|
@Autowired
|
|
|
private KuaiShouAccountReportMapper accountReportMapper;
|
|
|
+ @Autowired
|
|
|
+ private RateLimitUtil rateLimitUtil;
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public void queryAccountReportHourly(String accessToken, Long advertiserId, String startDate, String endDate, int page) {
|
|
|
- try {
|
|
|
- Thread.sleep(1500);
|
|
|
- } catch (InterruptedException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- }
|
|
|
String url = "https://ad.e.kuaishou.com/rest/openapi/v1/report/account_report";
|
|
|
Map<String, String> headers = new HashMap<>();
|
|
|
headers.put("Content-Type", "application/json");
|
|
|
@@ -54,7 +54,14 @@ public class KuaiShouAccountReportServiceImpl implements IKuaiShouAccountReportS
|
|
|
param.put("temporal_granularity", "HOURLY");
|
|
|
param.put("page_size", 2000);
|
|
|
param.put("page", page);
|
|
|
- String result = HttpUtils.httpPostRequest(url, param, headers);
|
|
|
+ boolean acquired = rateLimitUtil.acquire(url);
|
|
|
+ String result = acquired ? HttpUtils.httpPostRequest(url, param, headers) : null;
|
|
|
+ if (Check.isNull(result)) {
|
|
|
+ String errMsg = acquired ? "返回结果为空" : "频控等待超时";
|
|
|
+ log.error("获取快手账号时报报表异常:{},accountId:{}", errMsg, advertiserId);
|
|
|
+ errorLogService.addError(advertiserId, startDate, endDate, "account", "hour", errMsg, 2);
|
|
|
+ return;
|
|
|
+ }
|
|
|
JSONObject resultJson = JSONObject.parseObject(result);
|
|
|
Integer code = resultJson.getInteger("code");
|
|
|
String message = resultJson.getString("message");
|
|
|
@@ -89,11 +96,6 @@ public class KuaiShouAccountReportServiceImpl implements IKuaiShouAccountReportS
|
|
|
|
|
|
@Override
|
|
|
public void queryAccountReportDaily(String accessToken, Long advertiserId, String startDate, String endDate, int page) {
|
|
|
- try {
|
|
|
- Thread.sleep(1500);
|
|
|
- } catch (InterruptedException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- }
|
|
|
String url = "https://ad.e.kuaishou.com/rest/openapi/v1/report/account_report";
|
|
|
Map<String, String> headers = new HashMap<>();
|
|
|
headers.put("Content-Type", "application/json");
|
|
|
@@ -109,7 +111,14 @@ public class KuaiShouAccountReportServiceImpl implements IKuaiShouAccountReportS
|
|
|
param.put("temporal_granularity", "DAILY");
|
|
|
param.put("page_size", 2000);
|
|
|
param.put("page", page);
|
|
|
- String result = HttpUtils.httpPostRequest(url, param, headers);
|
|
|
+ boolean acquired = rateLimitUtil.acquire(url);
|
|
|
+ String result = acquired ? HttpUtils.httpPostRequest(url, param, headers) : null;
|
|
|
+ if (Check.isNull(result)) {
|
|
|
+ String errMsg = acquired ? "返回结果为空" : "频控等待超时";
|
|
|
+ log.error("获取快手账号日报报表异常:{},accountId:{}", errMsg, advertiserId);
|
|
|
+ errorLogService.addError(advertiserId, startDate, endDate, "account", "daily", errMsg, 2);
|
|
|
+ return;
|
|
|
+ }
|
|
|
JSONObject resultJson = JSONObject.parseObject(result);
|
|
|
Integer code = resultJson.getInteger("code");
|
|
|
String message = resultJson.getString("message");
|
|
|
@@ -142,7 +151,7 @@ public class KuaiShouAccountReportServiceImpl implements IKuaiShouAccountReportS
|
|
|
|
|
|
@Override
|
|
|
public void queryAccountDailyFlows(String accessToken, Long advertiserId, String startDate, String endDate, int page, Integer isLog, Long errId, Integer isSend, int retry) {
|
|
|
- String url = "https://ad.e.kuaishou.com/rest/openapi/v1/advertiser/fund/daily_flows";
|
|
|
+ String url = KuaishouLinkConstants.KS_PREFIX + KuaishouLinkConstants.V1_ADVERTISER_FUND_DAILY_FLOWS;
|
|
|
Map<String, String> headers = new HashMap<>();
|
|
|
headers.put("Content-Type", "application/json");
|
|
|
headers.put("Access-Token", accessToken);
|
|
|
@@ -157,11 +166,13 @@ public class KuaiShouAccountReportServiceImpl implements IKuaiShouAccountReportS
|
|
|
param.put("advertiser_id", advertiserId);
|
|
|
param.put("page_size", pageSize);
|
|
|
param.put("page", page);
|
|
|
- String result = HttpUtils.httpPostRequest(url, param, headers);
|
|
|
+ boolean acquired = rateLimitUtil.acquire(url);
|
|
|
+ String result = acquired ? HttpUtils.httpPostRequest(url, param, headers) : null;
|
|
|
if (Check.isNull(result)) {
|
|
|
- log.error("获取快手广告账户流水信息异常:{},accountId:{} {}~{}", "返回结果为空", advertiserId,startDate,endDate);
|
|
|
+ String errMsg = acquired ? "返回结果为空" : "频控等待超时";
|
|
|
+ log.error("获取快手广告账户流水信息异常:{},accountId:{} {}~{}", errMsg, advertiserId,startDate,endDate);
|
|
|
if (isLog != null && isLog == 1) {
|
|
|
- errorLogService.addError(advertiserId, startDate, endDate, "daily_flows", "daily", "返回结果为空", 2);
|
|
|
+ errorLogService.addError(advertiserId, startDate, endDate, "daily_flows", "daily", errMsg, 2);
|
|
|
}
|
|
|
return;
|
|
|
}
|
|
|
@@ -170,10 +181,6 @@ public class KuaiShouAccountReportServiceImpl implements IKuaiShouAccountReportS
|
|
|
String message = resultJson.getString("message");
|
|
|
if (null == code || code != 0) {
|
|
|
if (retry <= 3) {
|
|
|
- try {
|
|
|
- Thread.sleep(800);
|
|
|
- } catch (InterruptedException ignored) {
|
|
|
- }
|
|
|
queryAccountDailyFlows(accessToken, advertiserId, startDate, endDate, page,isLog, errId, isSend, retry + 1);
|
|
|
}else {
|
|
|
log.error("获取快手广告账户流水信息异常:{},accountId:{} {}~{}", message, advertiserId,startDate,endDate);
|
|
|
@@ -197,9 +204,9 @@ public class KuaiShouAccountReportServiceImpl implements IKuaiShouAccountReportS
|
|
|
addList.add(accountReportDaily);
|
|
|
}
|
|
|
}
|
|
|
- //if (!Check.isNull(addList)) {
|
|
|
- // accountReportMapper.replaceKuaiShouDailyFlows(addList);
|
|
|
- //}
|
|
|
+ if (!Check.isNull(addList)) {
|
|
|
+ accountReportMapper.replaceKuaiShouDailyFlows(addList);
|
|
|
+ }
|
|
|
if (page < totalPage) {
|
|
|
queryAccountDailyFlows(accessToken, advertiserId, startDate, endDate, page + 1,isLog, errId, isSend, 1);
|
|
|
} else {
|
|
|
@@ -216,6 +223,53 @@ public class KuaiShouAccountReportServiceImpl implements IKuaiShouAccountReportS
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
+ public void queryAccountFund(String accessToken, Long advertiserId, String statDate, Long errId, Integer isSend) {
|
|
|
+ String url = KuaishouLinkConstants.KS_PREFIX + KuaishouLinkConstants.V1_ADVERTISER_FUND_GET;
|
|
|
+ Map<String, String> headers = new HashMap<>();
|
|
|
+ headers.put("Content-Type", "application/json");
|
|
|
+ headers.put("Access-Token", accessToken);
|
|
|
+ Map<String, Object> param = new HashMap<>();
|
|
|
+ param.put("advertiser_id", advertiserId);
|
|
|
+ boolean acquired = rateLimitUtil.acquire(url);
|
|
|
+ String result = acquired ? HttpUtils.httpPostRequest(url, param, headers) : null;
|
|
|
+ if (Check.isNull(result)) {
|
|
|
+ String errMsg = acquired ? "返回结果为空" : "频控等待超时";
|
|
|
+ log.error("获取快手账户余额异常:{},accountId:{}", errMsg, advertiserId);
|
|
|
+ if (errId == null) {
|
|
|
+ errorLogService.addError(advertiserId, statDate, statDate, "fund", "daily", errMsg, 2);
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ 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);
|
|
|
+ // 400002 为账户未授权资金scope,属预期情况不记错误
|
|
|
+ if (errId == null && (null == code || code != 400002)) {
|
|
|
+ errorLogService.addError(advertiserId, statDate, statDate, "fund", "daily", message, 2);
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ JSONObject data = resultJson.getJSONObject("data");
|
|
|
+ if (Check.isNull(data)) {
|
|
|
+ log.error("获取快手账户余额异常:{},accountId:{}", "返回数据为空", advertiserId);
|
|
|
+ if (errId == null) {
|
|
|
+ errorLogService.addError(advertiserId, statDate, statDate, "fund", "daily", "返回数据为空", 2);
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ KuaishouAdvertiserFund fund = JSONObject.toJavaObject(data, KuaishouAdvertiserFund.class);
|
|
|
+ fund.setAdvertiserId(advertiserId);
|
|
|
+ fund.setStatDate(DateUtils.getIntegerDate(statDate));
|
|
|
+ accountReportMapper.replaceKuaiShouAdvertiserFund(Collections.singletonList(fund));
|
|
|
+ if (errId != null) {
|
|
|
+ errorLogService.deleteInfoByErrorId(errId);
|
|
|
+ }
|
|
|
+ log.info("【{}】录入快手账户余额完成,statDate:{}", advertiserId, statDate);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
public void updateKsAccountStatus() {
|
|
|
accountReportMapper.updateKsAccountStatus();
|
|
|
}
|