|
|
@@ -0,0 +1,122 @@
|
|
|
+package cn.com.ctop.job.bytedance.data.service.impl;
|
|
|
+
|
|
|
+import cn.com.ctop.job.bytedance.data.entity.BytedanceErrorLog;
|
|
|
+import cn.com.ctop.job.bytedance.data.entity.BytedanceProductReportDaily;
|
|
|
+import cn.com.ctop.job.bytedance.data.mapper.BytedanceProductReportMapper;
|
|
|
+import cn.com.ctop.job.bytedance.data.mapper.BytedancePromoterReportMapper;
|
|
|
+import cn.com.ctop.job.bytedance.data.service.IBytedanceProductReportService;
|
|
|
+import cn.com.ctop.job.bytedance.data.utils.AlertUtils;
|
|
|
+import cn.com.ctop.job.bytedance.data.utils.Check;
|
|
|
+import cn.com.ctop.job.bytedance.data.utils.FieldsConfig;
|
|
|
+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.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class BytedanceProductReportServiceImpl implements IBytedanceProductReportService {
|
|
|
+ @Autowired
|
|
|
+ private BytedancePromoterReportMapper promoterReportMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private BytedanceProductReportMapper productReportMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void getBytedanceProductDailyReport(Long advertiserId, String accessToken, String startDate, String endDate, Integer page,
|
|
|
+ Long errorId, Integer isLog, Integer isSend) {
|
|
|
+ try {
|
|
|
+ String url = "https://api.oceanengine.com/open_api/v3.0/report/custom/get/";
|
|
|
+ Map<String, Object> paramsMap = new HashMap<>();
|
|
|
+ paramsMap.put("advertiser_id", advertiserId);
|
|
|
+ paramsMap.put("start_time", startDate);
|
|
|
+ paramsMap.put("end_time", endDate);
|
|
|
+ paramsMap.put("data_topic", "PRODUCT_DATA");
|
|
|
+ paramsMap.put("metrics", FieldsConfig.getProductMetrics());
|
|
|
+ paramsMap.put("dimensions", FieldsConfig.getProductDailyDimensions());
|
|
|
+ paramsMap.put("order_by", new JSONArray());
|
|
|
+ paramsMap.put("filters", new JSONArray());
|
|
|
+ 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);
|
|
|
+ insertErrorLog(isLog, advertiserId, startDate, endDate);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ List<BytedanceProductReportDaily> reportDailies = new ArrayList<>();
|
|
|
+ Integer code = result.getInteger("code");
|
|
|
+ int totalPage = 1;
|
|
|
+ if (code == 0) {
|
|
|
+ JSONObject data = result.getJSONObject("data");
|
|
|
+ if (!Check.isNull(data)) {
|
|
|
+ totalPage = data.getJSONObject("page_info").getIntValue("total_page");
|
|
|
+ JSONArray rows = data.getJSONArray("rows");
|
|
|
+ if (!Check.isNull(rows)) {
|
|
|
+ for (int i = 0; i < rows.size(); i++) {
|
|
|
+ JSONObject rowJson = rows.getJSONObject(i);
|
|
|
+ JSONObject metricsJson = rowJson.getJSONObject("metrics");
|
|
|
+ JSONObject dimensionsJson = rowJson.getJSONObject("dimensions");
|
|
|
+ metricsJson.putAll(dimensionsJson);
|
|
|
+ metricsJson.put("stat_time_day", dimensionsJson.getString("stat_time_day").replace("-", ""));
|
|
|
+ BytedanceProductReportDaily reportDaily = JSONObject.parseObject(metricsJson.toJSONString(), BytedanceProductReportDaily.class);
|
|
|
+ reportDaily.setAdvertiserId(advertiserId);
|
|
|
+ reportDailies.add(reportDaily);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ log.info("账户id:{},日期:{}产品数据日报请求结果失败,返回数据:{}", advertiserId, startDate, result);
|
|
|
+ insertErrorLog(isLog, advertiserId, startDate, endDate);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (!Check.isNull(reportDailies)) {
|
|
|
+ productReportMapper.replaceProductReportDailies(reportDailies);
|
|
|
+ log.info("账户ID:{},产品数据日报请求成功,开始日期:{},结束日期:{},page:{}", advertiserId, startDate, endDate, page);
|
|
|
+ }
|
|
|
+ if (page < totalPage) {
|
|
|
+ try {
|
|
|
+ Thread.sleep(500L);
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ getBytedanceProductDailyReport(advertiserId, accessToken, startDate, endDate, page + 1, errorId, isLog, isSend);
|
|
|
+ } else {
|
|
|
+ log.info("账户ID:{},日期:{},产品数据日报请求成功", advertiserId, startDate);
|
|
|
+ if (errorId != null) {
|
|
|
+ promoterReportMapper.deleteInfoByErrorId(errorId);
|
|
|
+ }
|
|
|
+ if (isSend != null && 1 == isSend) {
|
|
|
+ AlertUtils.sendMessage(advertiserId, startDate, endDate, 1, "product_daily", 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void insertErrorLog(Integer isLog, Long advertiserId, String startDate, String endDate) {
|
|
|
+ if (isLog != null && 1 == isLog) {
|
|
|
+ BytedanceErrorLog errorInfo = new BytedanceErrorLog();
|
|
|
+ errorInfo.setAdvertiserId(advertiserId);
|
|
|
+ errorInfo.setStartDate(startDate);
|
|
|
+ errorInfo.setEndDate(endDate);
|
|
|
+ errorInfo.setDataDimension("product");
|
|
|
+ errorInfo.setDataType("daily");
|
|
|
+ promoterReportMapper.replaceErrorInfo(errorInfo);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|