|
|
@@ -0,0 +1,84 @@
|
|
|
+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.KuaishouAdgroupReportHourly;
|
|
|
+import cn.com.ctop.job.kuaishou.data.mapper.GroupReportMapper;
|
|
|
+import cn.com.ctop.job.kuaishou.data.mapper.KuaiShouRefreshTokenMapper;
|
|
|
+import cn.com.ctop.job.kuaishou.data.service.GroupReportService;
|
|
|
+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 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;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class GroupReportServiceImpl implements GroupReportService {
|
|
|
+ @Autowired
|
|
|
+ private GroupReportMapper groupReportMapper;
|
|
|
+ @Autowired
|
|
|
+ private KuaiShouRefreshTokenMapper kuaiShouRefreshTokenMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void queryGroupReportHourly(String accessToken, Long advertiserId, String startDate, String endDate, int page) {
|
|
|
+ //广告组数据
|
|
|
+ String url = KuaishouLinkConstants.KS_PREFIX + KuaishouLinkConstants.V1_GROUP_REPORT;
|
|
|
+ Map<String, String> headers = new HashMap<>();
|
|
|
+ headers.put("Content-Type", "application/json");
|
|
|
+ headers.put("Access-Token", accessToken);
|
|
|
+ Map<String, Object> param = new HashMap<>();
|
|
|
+ if (!Check.isNull(startDate)) {
|
|
|
+ param.put("start_date", startDate);
|
|
|
+ }
|
|
|
+ if (!Check.isNull(endDate)) {
|
|
|
+ param.put("end_date", endDate);
|
|
|
+ }
|
|
|
+ param.put("advertiser_id", advertiserId);
|
|
|
+ param.put("temporal_granularity", "HOURLY");
|
|
|
+ param.put("page_size", 2000);
|
|
|
+ param.put("page", page);
|
|
|
+ String result = HttpUtils.httpPostRequest(url, param, headers);
|
|
|
+ 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);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ JSONArray details = resultJson.getJSONObject("data").getJSONArray("details");
|
|
|
+ List<KuaishouAdgroupReportHourly> addList = new ArrayList<>();
|
|
|
+ if (!Check.isNull(details)) {
|
|
|
+ for (int i = 0; i < details.size(); i++) {
|
|
|
+ JSONObject detailJson = details.getJSONObject(i);
|
|
|
+ detailJson.put("ad_show", detailJson.getString("show"));
|
|
|
+ detailJson.put("hour", detailJson.getLong("stat_hour"));
|
|
|
+ detailJson.put("stat_date", Integer.valueOf(DateUtils.getIntegerDate(detailJson.getString("stat_date"))));
|
|
|
+ detailJson.put("adgroupId", detailJson.getLong("unit_id"));
|
|
|
+ detailJson.put("adgroupName", detailJson.getString("unit_name"));
|
|
|
+ KuaishouAdgroupReportHourly dailyGroup = JSONObject.toJavaObject(detailJson, KuaishouAdgroupReportHourly.class);
|
|
|
+ dailyGroup.setAdvertiserId(advertiserId);
|
|
|
+ addList.add(dailyGroup);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!Check.isNull(addList)) {
|
|
|
+ groupReportMapper.replaceBatchGroupReportHourly(addList);
|
|
|
+ }
|
|
|
+ if (details.size() == 2000) {
|
|
|
+ queryGroupReportHourly(accessToken, advertiserId, startDate, endDate, page + 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<Long> getAdvertiserIdList(Integer type) {
|
|
|
+ return groupReportMapper.getAdvertiserIdList(type);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|