|
@@ -0,0 +1,243 @@
|
|
|
|
|
+package cn.com.ctop.job.adq.data.service.impl;
|
|
|
|
|
+
|
|
|
|
|
+import cn.com.ctop.job.adq.data.entity.AdqAssetInfo;
|
|
|
|
|
+import cn.com.ctop.job.adq.data.entity.AdqAssetReportDaily;
|
|
|
|
|
+import cn.com.ctop.job.adq.data.entity.AdqErrorLog;
|
|
|
|
|
+import cn.com.ctop.job.adq.data.mapper.AdqAssetReportMapper;
|
|
|
|
|
+import cn.com.ctop.job.adq.data.mapper.AdqErrorLogMapper;
|
|
|
|
|
+import cn.com.ctop.job.adq.data.service.IAdqAssetReportService;
|
|
|
|
|
+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.*;
|
|
|
|
|
+
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+@Service
|
|
|
|
|
+public class AdqAssetReportServiceImpl implements IAdqAssetReportService {
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private AdqErrorLogMapper adqErrorLogMapper;
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private AdqAssetReportMapper adqAssetReportMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void getAdqAssetDailyReport(Long advertiserId, String accessToken, String startDate, String endDate, Integer page, Integer isLog, Long errId, Integer isSend) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ Map<String, Object> paramsMap = new HashMap<>();
|
|
|
|
|
+ paramsMap.put("access_token", accessToken);
|
|
|
|
|
+ paramsMap.put("timestamp", String.valueOf(Instant.now().getEpochSecond()));
|
|
|
|
|
+ paramsMap.put("nonce", RandomStringGenerator.generateRandomString(32));
|
|
|
|
|
+ paramsMap.put("page", page);
|
|
|
|
|
+ paramsMap.put("page_size", 1000);
|
|
|
|
|
+ paramsMap.put("account_id", advertiserId);
|
|
|
|
|
+ paramsMap.put("level", "REPORT_LEVEL_MARKETING_ASSET");
|
|
|
|
|
+ paramsMap.put("date_range", new JSONObject().fluentPut("start_date", startDate).fluentPut("end_date", endDate));
|
|
|
|
|
+ paramsMap.put("group_by", new JSONArray(Arrays.asList("date", "adgroup_id", "marketing_target_type", "marketing_asset_id", "site_set")));
|
|
|
|
|
+ paramsMap.put("fields", new JSONArray(Arrays.asList("date", "account_id", "adgroup_id", "adgroup_name", "marketing_target_type", "marketing_asset_id", "site_set", "cost", "view_count", "valid_click_count", "conversions_count", "deep_conversions_count", "activated_count", "reg_pv", "retention_count", "video_outer_play_count", "video_outer_play100_count", "key_behavior_conversions_count", "purchase_pv", "purchase_amount", "first_pay_count")));
|
|
|
|
|
+ List<AdqAssetReportDaily> dailies = new ArrayList<>();
|
|
|
|
|
+ HashMap<String, Set<String>> assetIdsMap = new HashMap<>();
|
|
|
|
|
+ String s = HttpUtils.httpGet("https://api.e.qq.com/v3.0/daily_reports/get", paramsMap, null);
|
|
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(s);
|
|
|
|
|
+ int totalPage = 1;
|
|
|
|
|
+ if (!Check.isNull(jsonObject)) {
|
|
|
|
|
+ Integer code = jsonObject.getInteger("code");
|
|
|
|
|
+ if (code == 0) {
|
|
|
|
|
+ JSONObject data = jsonObject.getJSONObject("data");
|
|
|
|
|
+ if (!Check.isNull(data)) {
|
|
|
|
|
+ JSONObject pageInfo = data.getJSONObject("page_info");
|
|
|
|
|
+ if (!Check.isNull(pageInfo)) {
|
|
|
|
|
+ totalPage = pageInfo.getInteger("total_page");
|
|
|
|
|
+ }
|
|
|
|
|
+ JSONArray list = data.getJSONArray("list");
|
|
|
|
|
+ if (!Check.isNull(list)) {
|
|
|
|
|
+ for (int i = 0; i < list.size(); i++) {
|
|
|
|
|
+ JSONObject dataJson = list.getJSONObject(i);
|
|
|
|
|
+ AdqAssetReportDaily report = JSONObject.parseObject(dataJson.toJSONString(), AdqAssetReportDaily.class);
|
|
|
|
|
+ report.setAdvertiserId(advertiserId);
|
|
|
|
|
+ report.setStatTimeDay(Integer.valueOf(dataJson.getString("date").replace("-", "")));
|
|
|
|
|
+ dailies.add(report);
|
|
|
|
|
+
|
|
|
|
|
+ String marketingTargetType = report.getMarketingTargetType();
|
|
|
|
|
+ Set<String> orDefault = assetIdsMap.getOrDefault(marketingTargetType, new HashSet<>());
|
|
|
|
|
+ orDefault.add(report.getMarketingAssetId().toString());
|
|
|
|
|
+ assetIdsMap.put(marketingTargetType, orDefault);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.error("请求产品日报失败,account_id:{},返回信息:{}", advertiserId, jsonObject);
|
|
|
|
|
+ insertErrorLog(isLog, advertiserId, "asset", startDate, endDate, jsonObject.getString("message_cn"));
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.error("请求产品日报失败,account_id:{},返回为空。", advertiserId);
|
|
|
|
|
+ insertErrorLog(isLog, advertiserId, "asset", startDate, endDate, "请求返回结果为空");
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!Check.isNull(dailies)) {
|
|
|
|
|
+ adqAssetReportMapper.insertAdqAssetReportDaily(dailies);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!Check.isNullMap(assetIdsMap)) {
|
|
|
|
|
+ getAdqAssetInfo(assetIdsMap, advertiserId, accessToken, 1, null, null);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (page < totalPage) {
|
|
|
|
|
+ getAdqAssetDailyReport(advertiserId, accessToken, startDate, endDate, page + 1, isLog, errId, isSend);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.error("请求产品日报完成,account_id:{},日期:{}{}", advertiserId, startDate, endDate);
|
|
|
|
|
+ if (errId != null) {
|
|
|
|
|
+ adqErrorLogMapper.deleteInfoByErrorId(errId);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (isSend != null && isSend == 1) {
|
|
|
|
|
+ AlertUtils.sendMessage(advertiserId, startDate, endDate, 3, "asset_daily", 1);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void getAdqAssetInfo(Map<String, Set<String>> assetIdsMap, Long advertiserId, String accessToken, Integer isLog, Long errId, Integer isSend) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ Set<Map.Entry<String, Set<String>>> entries = assetIdsMap.entrySet();
|
|
|
|
|
+ for (Map.Entry<String, Set<String>> entry : entries) {
|
|
|
|
|
+ String type = entry.getKey();
|
|
|
|
|
+ List<String> assetIds = new ArrayList<>(entry.getValue());
|
|
|
|
|
+ getAdqAssetInfo(type, assetIds, advertiserId, accessToken, isLog, errId, isSend);
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void getAdqAssetInfo(String type, List<String> assetIds, Long advertiserId, String accessToken, Integer isLog, Long errId, Integer isSend) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ int page = 1;
|
|
|
|
|
+ int totalPage = 1;
|
|
|
|
|
+ while (page <= totalPage) {
|
|
|
|
|
+ Map<String, Object> paramsMap = new HashMap<>();
|
|
|
|
|
+ paramsMap.put("account_id", advertiserId);
|
|
|
|
|
+ paramsMap.put("access_token", accessToken);
|
|
|
|
|
+ paramsMap.put("timestamp", String.valueOf(Instant.now().getEpochSecond()));
|
|
|
|
|
+ paramsMap.put("nonce", RandomStringGenerator.generateRandomString(32));
|
|
|
|
|
+ paramsMap.put("marketing_target_type", type);
|
|
|
|
|
+ paramsMap.put("page", page);
|
|
|
|
|
+ paramsMap.put("page_size", 100);
|
|
|
|
|
+
|
|
|
|
|
+ if (!Check.isNull(assetIds)) {
|
|
|
|
|
+ JSONObject filter = new JSONObject()
|
|
|
|
|
+ .fluentPut("field", "marketing_asset_id")
|
|
|
|
|
+ .fluentPut("operator", "IN")
|
|
|
|
|
+ .fluentPut("values", assetIds);
|
|
|
|
|
+ paramsMap.put("filtering", new JSONArray().fluentAdd(filter));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ List<AdqAssetInfo> infos = new ArrayList<>();
|
|
|
|
|
+
|
|
|
|
|
+ String s = HttpUtils.httpGet("https://api.e.qq.com/v3.0/marketing_target_assets/get", paramsMap, null);
|
|
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(s);
|
|
|
|
|
+
|
|
|
|
|
+ if (!Check.isNull(jsonObject)) {
|
|
|
|
|
+ Integer code = jsonObject.getInteger("code");
|
|
|
|
|
+ if (code == 0) {
|
|
|
|
|
+ JSONObject data = jsonObject.getJSONObject("data");
|
|
|
|
|
+ if (!Check.isNull(data)) {
|
|
|
|
|
+ JSONObject pageInfo = data.getJSONObject("page_info");
|
|
|
|
|
+ if (!Check.isNull(pageInfo)) {
|
|
|
|
|
+ totalPage = pageInfo.getInteger("total_page");
|
|
|
|
|
+ }
|
|
|
|
|
+ JSONArray list = data.getJSONArray("list");
|
|
|
|
|
+ if (!Check.isNull(list)) {
|
|
|
|
|
+ for (int i = 0; i < list.size(); i++) {
|
|
|
|
|
+ JSONObject dataJson = list.getJSONObject(i);
|
|
|
|
|
+ AdqAssetInfo info = JSONObject.parseObject(dataJson.toJSONString(), AdqAssetInfo.class);
|
|
|
|
|
+ info.setAdvertiserId(advertiserId);
|
|
|
|
|
+ info.setMarketingTargetType(type);
|
|
|
|
|
+ JSONArray properties = dataJson.getJSONArray("properties");
|
|
|
|
|
+ if (!Check.isNull(properties)) {
|
|
|
|
|
+ for (int j = 0; j < properties.size(); j++) {
|
|
|
|
|
+ JSONObject propertyJson = properties.getJSONObject(j);
|
|
|
|
|
+ String propertyName = propertyJson.getString("property_name");
|
|
|
|
|
+ JSONArray propertyValue = propertyJson.getJSONArray("property_value");
|
|
|
|
|
+ if ("PROMOTED_ASSET_ATTR_KEY_SHORT_DRAMA_DRAMA_MALL".equals(propertyName) && !Check.isNull(propertyValue)) {
|
|
|
|
|
+ info.setPromotedAssetAttrKeyShortDramaDramaMall(propertyValue.getString(0));
|
|
|
|
|
+ } else if ("PROMOTED_ASSET_ATTR_KEY_SKU_ID".equals(propertyName) && !Check.isNull(propertyValue)) {
|
|
|
|
|
+ info.setPromotedAssetAttrKeySkuId(propertyValue.getString(0));
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!Check.isNull(info.getPromotedAssetAttrKeyShortDramaDramaMall()) && !Check.isNull(info.getPromotedAssetAttrKeySkuId())) {
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ infos.add(info);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.error("请求产品信息列表失败,account_id:{},返回信息:{}", advertiserId, jsonObject);
|
|
|
|
|
+ insertErrorLog(isLog, advertiserId, "asset_info", "-", "-", new JSONObject()
|
|
|
|
|
+ .fluentPut("msg", jsonObject.getString("message_cn"))
|
|
|
|
|
+ .fluentPut("marketing_target_type", type)
|
|
|
|
|
+ .fluentPut("marketing_asset_ids", assetIds)
|
|
|
|
|
+ .toJSONString()
|
|
|
|
|
+ );
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.error("请求产品信息列表失败,account_id:{},返回为空。", advertiserId);
|
|
|
|
|
+ insertErrorLog(isLog, advertiserId, "asset_info", "-", "-", new JSONObject()
|
|
|
|
|
+ .fluentPut("msg", "请求返回结果为空")
|
|
|
|
|
+ .fluentPut("marketing_target_type", type)
|
|
|
|
|
+ .fluentPut("marketing_asset_ids", assetIds)
|
|
|
|
|
+ .toJSONString()
|
|
|
|
|
+ );
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!Check.isNull(infos)) {
|
|
|
|
|
+ adqAssetReportMapper.insertAdqAssetInfo(infos);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (page == totalPage) {
|
|
|
|
|
+ log.error("请求产品信息列表完成,account_id:{}, assetIds:{} ", advertiserId, assetIds);
|
|
|
|
|
+ if (errId != null) {
|
|
|
|
|
+ adqErrorLogMapper.deleteInfoByErrorId(errId);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (isSend != null && isSend == 1) {
|
|
|
|
|
+ AlertUtils.sendMessage(advertiserId, "-", "-", 3, "asset_info", 1);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ page = page + 1;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void insertErrorLog(Integer isLog, Long advertiserId, String dataDimension, String startDate, String endDate, String msg) {
|
|
|
|
|
+ if (isLog != null && isLog == 1) {
|
|
|
|
|
+ AdqErrorLog errorInfo = new AdqErrorLog();
|
|
|
|
|
+ errorInfo.setAdvertiserId(advertiserId);
|
|
|
|
|
+ errorInfo.setStartDate(startDate);
|
|
|
|
|
+ errorInfo.setEndDate(endDate);
|
|
|
|
|
+ errorInfo.setDataDimension(dataDimension);
|
|
|
|
|
+ errorInfo.setDataType("daily");
|
|
|
|
|
+ errorInfo.setMsg(msg);
|
|
|
|
|
+ adqErrorLogMapper.replaceErrorInfo(errorInfo);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+}
|