|
@@ -0,0 +1,112 @@
|
|
|
|
|
+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.mapper.BytedancePromoterReportMapper;
|
|
|
|
|
+import cn.com.ctop.job.bytedance.data.service.IBytedanceMaterialReportService;
|
|
|
|
|
+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 cn.com.ctop.job.bytedance.utils.doris.sink.DorisMultiTableStreamLoadManager;
|
|
|
|
|
+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.HashMap;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+
|
|
|
|
|
+@Service
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+public class BytedanceMaterialReportServiceImpl implements IBytedanceMaterialReportService {
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private BytedancePromoterReportMapper promoterReportMapper;
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private DorisMultiTableStreamLoadManager dorisLoadManager;
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void getBytedanceMaterialDailyReport(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", "BASIC_DATA");
|
|
|
|
|
+ paramsMap.put("metrics", FieldsConfig.getMaterialMetrics());
|
|
|
|
|
+ paramsMap.put("dimensions", FieldsConfig.getMaterialDailyDimensions());
|
|
|
|
|
+ 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);
|
|
|
|
|
+ if (isLog == 1) {
|
|
|
|
|
+ BytedanceErrorLog errorInfo = new BytedanceErrorLog();
|
|
|
|
|
+ errorInfo.setAdvertiserId(advertiserId);
|
|
|
|
|
+ errorInfo.setStartDate(startDate);
|
|
|
|
|
+ errorInfo.setEndDate(endDate);
|
|
|
|
|
+ errorInfo.setDataDimension("material");
|
|
|
|
|
+ errorInfo.setDataType("daily");
|
|
|
|
|
+ errorInfo.setMsg("请求结果返回为空");
|
|
|
|
|
+ promoterReportMapper.replaceErrorInfo(errorInfo);
|
|
|
|
|
+ }
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ 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");
|
|
|
|
|
+
|
|
|
|
|
+ dimensionsJson.putAll(metricsJson);
|
|
|
|
|
+ dimensionsJson.put("stat_time_day", dimensionsJson.getString("stat_time_day").replace("-", ""));
|
|
|
|
|
+ dimensionsJson.put("advertiser_id", advertiserId);
|
|
|
|
|
+ dorisLoadManager.add("media_api", "bytedance_material_report_daily", dimensionsJson);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.info("账户id:{},日期:{}素材日报请求结果失败,返回数据:{}", advertiserId, startDate, result);
|
|
|
|
|
+ if (isLog == 1) {
|
|
|
|
|
+ BytedanceErrorLog errorInfo = new BytedanceErrorLog();
|
|
|
|
|
+ errorInfo.setAdvertiserId(advertiserId);
|
|
|
|
|
+ errorInfo.setStartDate(startDate);
|
|
|
|
|
+ errorInfo.setEndDate(endDate);
|
|
|
|
|
+ errorInfo.setDataDimension("material");
|
|
|
|
|
+ errorInfo.setDataType("daily");
|
|
|
|
|
+ errorInfo.setMsg(result.getString("message"));
|
|
|
|
|
+ promoterReportMapper.replaceErrorInfo(errorInfo);
|
|
|
|
|
+ }
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (page < totalPage) {
|
|
|
|
|
+ getBytedanceMaterialDailyReport(advertiserId, accessToken, startDate, endDate, page + 1, errorId, isLog, isSend);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.info("账户ID:{}, 开始日期:{}, 结束日期:{}, totalPage:{}, 素材日报数据请求成功", advertiserId, startDate, endDate, totalPage);
|
|
|
|
|
+ if (errorId != null) {
|
|
|
|
|
+ promoterReportMapper.deleteInfoByErrorId(errorId);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (isSend != null && 1 == isSend) {
|
|
|
|
|
+ AlertUtils.sendMessage(advertiserId, startDate, endDate, 1, "material", 1, null);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|