|
@@ -0,0 +1,182 @@
|
|
|
|
|
+package cn.com.ctop.job.bytedance.data.service.impl;
|
|
|
|
|
+
|
|
|
|
|
+import cn.com.ctop.job.bytedance.data.entity.BytedancePromoterReportDaily;
|
|
|
|
|
+import cn.com.ctop.job.bytedance.data.entity.BytedancePromoterReportHour;
|
|
|
|
|
+import cn.com.ctop.job.bytedance.data.mapper.BytedancePromoterReportMapper;
|
|
|
|
|
+import cn.com.ctop.job.bytedance.data.service.IBytedancePromoterReportService;
|
|
|
|
|
+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 BytedancePromoterReportServiceImpl implements IBytedancePromoterReportService {
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private BytedancePromoterReportMapper promoterReportMapper;
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<JSONObject> getAccountList() {
|
|
|
|
|
+ return promoterReportMapper.getAccountList();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void getBytedancePromoterHourReport(Long errorId, Long advertiserId, String accessToken, String startDate, String endDate, Integer page) {
|
|
|
|
|
+ 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", "BASIC_DATA");
|
|
|
|
|
+ paramsMap.put("metrics", FieldsConfig.getpromoterMetrics());
|
|
|
|
|
+ paramsMap.put("dimensions", FieldsConfig.getpromoterHoutDimensions());
|
|
|
|
|
+ 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;charset=UTF-8");
|
|
|
|
|
+ String s = HttpUtils.httpGet(url, paramsMap, headers);
|
|
|
|
|
+ JSONObject result = JSONObject.parseObject(s);
|
|
|
|
|
+ if (Check.isNull(result)) {
|
|
|
|
|
+ log.info("账户id:{},日期:{},广告时报(最细粒度)请求结果返回为空", advertiserId, startDate);
|
|
|
|
|
+ }
|
|
|
|
|
+ List<BytedancePromoterReportHour> hours = new ArrayList<>();
|
|
|
|
|
+ Integer code = result.getInteger("code");
|
|
|
|
|
+ if (code == 0) {
|
|
|
|
|
+ JSONObject data = result.getJSONObject("data");
|
|
|
|
|
+ if (!Check.isNull(data)) {
|
|
|
|
|
+ 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");
|
|
|
|
|
+ Long cdpProjectId = dimensionsJson.getLong("cdp_project_id");
|
|
|
|
|
+ String cdpProjectName = dimensionsJson.getString("cdp_project_name");
|
|
|
|
|
+ Long cdpPromotionId = dimensionsJson.getLong("cdp_promotion_id");
|
|
|
|
|
+ String cdpPromotionName = dimensionsJson.getString("cdp_promotion_name");
|
|
|
|
|
+ Integer statTimeDay = Integer.valueOf(dimensionsJson.getString("stat_time_day").replace("-", ""));
|
|
|
|
|
+ Integer hour = Integer.valueOf(dimensionsJson.getString("stat_time_hour").substring(11, 13));
|
|
|
|
|
+ BytedancePromoterReportHour bytedancePromoterReportHour = JSONObject.parseObject(metricsJson.toJSONString(), BytedancePromoterReportHour.class);
|
|
|
|
|
+ bytedancePromoterReportHour.setAdvertiserId(advertiserId);
|
|
|
|
|
+ bytedancePromoterReportHour.setCdpProjectId(cdpProjectId);
|
|
|
|
|
+ bytedancePromoterReportHour.setCdpProjectName(cdpProjectName);
|
|
|
|
|
+ bytedancePromoterReportHour.setCdpPromotionId(cdpPromotionId);
|
|
|
|
|
+ bytedancePromoterReportHour.setCdpPromotionName(cdpPromotionName);
|
|
|
|
|
+ bytedancePromoterReportHour.setStatTimeDay(statTimeDay);
|
|
|
|
|
+ bytedancePromoterReportHour.setHour(hour);
|
|
|
|
|
+ hours.add(bytedancePromoterReportHour);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.info("账户id:{},日期:{}广告时报(最细粒度)请求结果失败,返回数据:{}", advertiserId, startDate, result);
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!Check.isNull(hours)) {
|
|
|
|
|
+ promoterReportMapper.replacePromoterReportHour(hours);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (hours.size() >= 100) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ Thread.sleep(500L);
|
|
|
|
|
+ } catch (InterruptedException e) {
|
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
|
+ }
|
|
|
|
|
+ getBytedancePromoterHourReport(errorId,advertiserId, accessToken, startDate, endDate, page + 1);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.info("账户ID:{},日期:{},广告时报(最细粒度)数据请求成功", advertiserId, startDate);
|
|
|
|
|
+ promoterReportMapper.deleteInfoByErrorId(errorId);
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void getBytedancePromorteDailyReport(Long errorId, Long advertiserId, String accessToken, String startDate, String endDate, Integer page) {
|
|
|
|
|
+ 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", "BASIC_DATA");
|
|
|
|
|
+ paramsMap.put("metrics", FieldsConfig.getpromoterMetrics());
|
|
|
|
|
+ paramsMap.put("dimensions", FieldsConfig.getpromoterDailyDimensions());
|
|
|
|
|
+ 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);
|
|
|
|
|
+ }
|
|
|
|
|
+ List<BytedancePromoterReportDaily> promoterReportDailies = new ArrayList<>();
|
|
|
|
|
+ Integer code = result.getInteger("code");
|
|
|
|
|
+ if (code == 0) {
|
|
|
|
|
+ JSONObject data = result.getJSONObject("data");
|
|
|
|
|
+ if (!Check.isNull(data)) {
|
|
|
|
|
+ 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");
|
|
|
|
|
+ Long cdpProjectId = dimensionsJson.getLong("cdp_project_id");
|
|
|
|
|
+ String cdpProjectName = dimensionsJson.getString("cdp_project_name");
|
|
|
|
|
+ Long cdpPromotionId = dimensionsJson.getLong("cdp_promotion_id");
|
|
|
|
|
+ String cdpPromotionName = dimensionsJson.getString("cdp_promotion_name");
|
|
|
|
|
+ Integer statTimeDay = Integer.valueOf(dimensionsJson.getString("stat_time_day").replace("-", ""));
|
|
|
|
|
+ BytedancePromoterReportDaily promoterReportDaily = JSONObject.parseObject(metricsJson.toJSONString(), BytedancePromoterReportDaily.class);
|
|
|
|
|
+ promoterReportDaily.setAdvertiserId(advertiserId);
|
|
|
|
|
+ promoterReportDaily.setCdpProjectId(cdpProjectId);
|
|
|
|
|
+ promoterReportDaily.setCdpProjectName(cdpProjectName);
|
|
|
|
|
+ promoterReportDaily.setCdpPromotionId(cdpPromotionId);
|
|
|
|
|
+ promoterReportDaily.setCdpPromotionName(cdpPromotionName);
|
|
|
|
|
+ promoterReportDaily.setStatTimeDay(statTimeDay);
|
|
|
|
|
+ promoterReportDailies.add(promoterReportDaily);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.info("账户id:{},日期:{}广告日报(最细粒度)请求结果失败,返回数据:{}", advertiserId, startDate, result);
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!Check.isNull(promoterReportDailies)) {
|
|
|
|
|
+ promoterReportMapper.replacePromoterReportDailies(promoterReportDailies);
|
|
|
|
|
+ log.info("账户ID:{},广告日报(最细粒度)数据请求成功", advertiserId);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (promoterReportDailies.size() >= 100) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ Thread.sleep(500L);
|
|
|
|
|
+ } catch (InterruptedException e) {
|
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
|
+ }
|
|
|
|
|
+ getBytedancePromorteDailyReport(errorId,advertiserId, accessToken, startDate, endDate, page + 1);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.info("账户ID:{},日期:{},广告日报(最细粒度)数据请求成功", advertiserId, startDate);
|
|
|
|
|
+ promoterReportMapper.deleteInfoByErrorId(errorId);
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|