|
@@ -0,0 +1,711 @@
|
|
|
|
+package org.jeecg.modules.kuaishou.service.impl;
|
|
|
|
+
|
|
|
|
+import cn.com.ctop.common.utils.Check;
|
|
|
|
+import cn.com.ctop.common.utils.HttpUtils;
|
|
|
|
+import cn.com.ctop.common.utils.PropertiesUtils;
|
|
|
|
+import cn.com.ctop.kuaishou.modules.material.entity.KuaishouResult;
|
|
|
|
+import cn.com.ctop.kuaishou.modules.material.entity.KuaishouResultToken;
|
|
|
|
+import cn.com.ctop.kuaishou.modules.report.entity.*;
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import com.fasterxml.jackson.core.JsonParseException;
|
|
|
|
+import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
|
+import com.fasterxml.jackson.databind.DeserializationFeature;
|
|
|
|
+import com.fasterxml.jackson.databind.JavaType;
|
|
|
|
+import com.fasterxml.jackson.databind.JsonMappingException;
|
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
+import com.fasterxml.jackson.databind.type.TypeFactory;
|
|
|
|
+import constant.KuaishouInterfaceConstant;
|
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
|
+import org.jeecg.modules.kuaishou.entity.*;
|
|
|
|
+import org.jeecg.modules.kuaishou.mapper.*;
|
|
|
|
+import org.jeecg.modules.kuaishou.service.IKuaishouInterfaceService;
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.lang.reflect.ParameterizedType;
|
|
|
|
+import java.lang.reflect.Type;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.Map;
|
|
|
|
+
|
|
|
|
+@Service
|
|
|
|
+public class KuaishouInterfaceServiceImpl implements IKuaishouInterfaceService {
|
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(KuaishouInterfaceServiceImpl.class);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public KuaishouResult<KuaishouReportHourlyAccount> getAccountHourlyReport(String accessToken, Long advertiserId, String startDate, String endDate, int pageSize, int pageNum) {
|
|
|
|
+ String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.ACCOUNT_REPORT;
|
|
|
|
+ Map<String, String> headers = new HashMap<String, String>();
|
|
|
|
+ headers.put("Access-Token", accessToken);
|
|
|
|
+ Map<String, Object> param = new HashMap<String, Object>();
|
|
|
|
+ param.put("start_date", startDate);
|
|
|
|
+ param.put("end_date", endDate);
|
|
|
|
+ param.put("advertiser_id", advertiserId);
|
|
|
|
+ param.put("temporal_granularity", "HOURLY");
|
|
|
|
+ param.put("page_size", pageSize);
|
|
|
|
+ param.put("page", pageNum);
|
|
|
|
+ KuaishouResult<KuaishouReportHourlyAccount> kuaishouResult = null;
|
|
|
|
+ try {
|
|
|
|
+ String result = HttpUtils.httpPostRequest(url, param, headers);
|
|
|
|
+ JSONObject resultJson = JSONArray.parseObject(result);
|
|
|
|
+ if (!Check.isNull(resultJson)) {
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ System.out.println(result);
|
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
|
+ mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
|
|
|
+ kuaishouResult = mapper.readValue(result, new TypeReference<KuaishouResult<KuaishouReportHourlyAccount>>() {
|
|
|
|
+ });
|
|
|
|
+// kuaishouResult = (KuaishouResult<KuaishouReportHourlyAccount>)jsonToObj(KuaishouResult.class,result);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return kuaishouResult;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public KuaishouResult<KuaishouReportDailyAccount> getAccountDailyReport(String accessToken, Long advertiserId, String startDate, String endDate, int pageSize, int pageNum) {
|
|
|
|
+ String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.ACCOUNT_REPORT;
|
|
|
|
+ Map<String, String> headers = new HashMap<String, String>();
|
|
|
|
+ headers.put("Content-Type", "application/json");
|
|
|
|
+ headers.put("Access-Token", accessToken);
|
|
|
|
+ Map<String, Object> param = new HashMap<String, Object>();
|
|
|
|
+ param.put("start_date", startDate);
|
|
|
|
+ param.put("end_date", endDate);
|
|
|
|
+ param.put("advertiser_id", advertiserId);
|
|
|
|
+ param.put("temporal_granularity", "DAILY");
|
|
|
|
+ param.put("page_size", pageSize);
|
|
|
|
+ param.put("page", pageNum);
|
|
|
|
+ KuaishouResult<KuaishouReportDailyAccount> kuaishouResult = null;
|
|
|
|
+ try {
|
|
|
|
+ String result = HttpUtils.httpPostRequest(url, param, headers);
|
|
|
|
+ System.out.println(result);
|
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
|
+ mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
|
|
|
+ kuaishouResult = mapper.readValue(result, new TypeReference<KuaishouResult<KuaishouReportDailyAccount>>() {
|
|
|
|
+ });
|
|
|
|
+// kuaishouResult = (KuaishouResult<KuaishouReportDailyAccount>)jsonToObj(KuaishouResult.class,result);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return kuaishouResult;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public KuaishouResult<KuaishouReportDailyCampaign> getCampaignDailyReport(String accessToken, Long advertiserId, String startDate, String endDate, int pageSize, int pageNum) {
|
|
|
|
+ String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.CAMPAIGN_REPORT;
|
|
|
|
+ Map<String, String> headers = new HashMap<String, String>();
|
|
|
|
+ headers.put("Content-Type", "application/json");
|
|
|
|
+ headers.put("Access-Token", accessToken);
|
|
|
|
+ Map<String, Object> param = new HashMap<String, Object>();
|
|
|
|
+ param.put("start_date", startDate);
|
|
|
|
+ param.put("end_date", endDate);
|
|
|
|
+ param.put("advertiser_id", advertiserId);
|
|
|
|
+ param.put("temporal_granularity", "DAILY");
|
|
|
|
+ param.put("page_size", pageSize);
|
|
|
|
+ param.put("page", pageNum);
|
|
|
|
+ KuaishouResult<KuaishouReportDailyCampaign> kuaishouResult = null;
|
|
|
|
+ try {
|
|
|
|
+ String result = HttpUtils.httpPostRequest(url, param, headers);
|
|
|
|
+ System.out.println(result);
|
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
|
+ mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
|
|
|
+ kuaishouResult = mapper.readValue(result, new TypeReference<KuaishouResult<KuaishouReportDailyCampaign>>() {
|
|
|
|
+ });
|
|
|
|
+// kuaishouResult = (KuaishouResult<KuaishouReportDailyCampaign>)jsonToObj(KuaishouResult.class,result);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return kuaishouResult;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public KuaishouResult<KuaishouReportHourlyCampaign> getCampaignHourlyReport(String accessToken, Long advertiserId, String startDate, String endDate, int pageSize, int pageNum) {
|
|
|
|
+ String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.CAMPAIGN_REPORT;
|
|
|
|
+ Map<String, String> headers = new HashMap<String, String>();
|
|
|
|
+ headers.put("Content-Type", "application/json");
|
|
|
|
+ headers.put("Access-Token", accessToken);
|
|
|
|
+ Map<String, Object> param = new HashMap<String, Object>();
|
|
|
|
+ param.put("start_date", startDate);
|
|
|
|
+ param.put("end_date", endDate);
|
|
|
|
+ param.put("advertiser_id", advertiserId);
|
|
|
|
+ param.put("temporal_granularity", "HOURLY");
|
|
|
|
+ param.put("page_size", pageSize);
|
|
|
|
+ param.put("page", pageNum);
|
|
|
|
+ KuaishouResult<KuaishouReportHourlyCampaign> kuaishouResult = null;
|
|
|
|
+ try {
|
|
|
|
+ String result = HttpUtils.httpPostRequest(url, param, headers);
|
|
|
|
+ System.out.println(result);
|
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
|
+ mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
|
|
|
+ kuaishouResult = mapper.readValue(result, new TypeReference<KuaishouResult<KuaishouReportHourlyCampaign>>() {
|
|
|
|
+ });
|
|
|
|
+// kuaishouResult = (KuaishouResult<KuaishouReportDailyCampaign>)jsonToObj(KuaishouResult.class,result);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return kuaishouResult;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public KuaishouResult<KuaishouReportDailyGroup> getGroupDailyReport(String accessToken, Long advertiserId, String startDate, String endDate, int pageSize, int pageNum) {
|
|
|
|
+ String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.GTOUP_REPORT;
|
|
|
|
+ Map<String, String> headers = new HashMap<String, String>();
|
|
|
|
+ headers.put("Content-Type", "application/json");
|
|
|
|
+ headers.put("Access-Token", accessToken);
|
|
|
|
+ Map<String, Object> param = new HashMap<String, Object>();
|
|
|
|
+ param.put("start_date", startDate);
|
|
|
|
+ param.put("end_date", endDate);
|
|
|
|
+ param.put("advertiser_id", advertiserId);
|
|
|
|
+ param.put("temporal_granularity", "DAILY");
|
|
|
|
+ param.put("page_size", pageSize);
|
|
|
|
+ param.put("page", pageNum);
|
|
|
|
+ KuaishouResult<KuaishouReportDailyGroup> kuaishouResult = null;
|
|
|
|
+ try {
|
|
|
|
+ String result = HttpUtils.httpPostRequest(url, param, headers);
|
|
|
|
+ System.out.println(result);
|
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
|
+ mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
|
|
|
+ kuaishouResult = mapper.readValue(result, new TypeReference<KuaishouResult<KuaishouReportDailyGroup>>() {
|
|
|
|
+ });
|
|
|
|
+// kuaishouResult = (KuaishouResult<KuaishouReportDailyGroup>)jsonToObj(KuaishouResult.class,result);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return kuaishouResult;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public KuaishouResult<KuaishouReportHourlyGroup> getGroupHourlyReport(String accessToken, Long advertiserId, String startDate, String endDate, int pageSize, int pageNum) {
|
|
|
|
+ String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.GTOUP_REPORT;
|
|
|
|
+ Map<String, String> headers = new HashMap<String, String>();
|
|
|
|
+ headers.put("Content-Type", "application/json");
|
|
|
|
+ headers.put("Access-Token", accessToken);
|
|
|
|
+ Map<String, Object> param = new HashMap<String, Object>();
|
|
|
|
+ param.put("start_date", startDate);
|
|
|
|
+ param.put("end_date", endDate);
|
|
|
|
+ param.put("advertiser_id", advertiserId);
|
|
|
|
+ param.put("temporal_granularity", "HOURLY");
|
|
|
|
+ param.put("page_size", pageSize);
|
|
|
|
+ param.put("page", pageNum);
|
|
|
|
+ KuaishouResult<KuaishouReportHourlyGroup> kuaishouResult = null;
|
|
|
|
+ try {
|
|
|
|
+ String result = HttpUtils.httpPostRequest(url, param, headers);
|
|
|
|
+ System.out.println(result);
|
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
|
+ mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
|
|
|
+ kuaishouResult = mapper.readValue(result, new TypeReference<KuaishouResult<KuaishouReportHourlyGroup>>() {
|
|
|
|
+ });
|
|
|
|
+// kuaishouResult = (KuaishouResult<KuaishouReportDailyGroup>)jsonToObj(KuaishouResult.class,result);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return kuaishouResult;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public KuaishouResult<KuaishouReportDailyCreative> getCreativeDailyReport(String accessToken, Long advertiserId, String startDate, String endDate, int pageSize, int pageNum) {
|
|
|
|
+ String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.CREATIVE_REPORT;
|
|
|
|
+ Map<String, String> headers = new HashMap<String, String>();
|
|
|
|
+ headers.put("Content-Type", "application/json");
|
|
|
|
+ headers.put("Access-Token", accessToken);
|
|
|
|
+ Map<String, Object> param = new HashMap<String, Object>();
|
|
|
|
+ param.put("start_date", startDate);
|
|
|
|
+ param.put("end_date", endDate);
|
|
|
|
+ param.put("advertiser_id", advertiserId);
|
|
|
|
+ param.put("temporal_granularity", "DAILY");
|
|
|
|
+ param.put("page_size", pageSize);
|
|
|
|
+ param.put("page", pageNum);
|
|
|
|
+ KuaishouResult<KuaishouReportDailyCreative> kuaishouResult = null;
|
|
|
|
+ try {
|
|
|
|
+ String result = HttpUtils.httpPostRequest(url, param, headers);
|
|
|
|
+ System.out.println(result);
|
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
|
+ mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
|
|
|
+ kuaishouResult = mapper.readValue(result, new TypeReference<KuaishouResult<KuaishouReportDailyCreative>>() {
|
|
|
|
+ });
|
|
|
|
+// kuaishouResult = (KuaishouResult<KuaishouReportDailyCreative>)jsonToObj(KuaishouResult.class,result);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return kuaishouResult;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public KuaishouResult<KuaishouReportHourlyCreative> getCreativeHourlyReport(String accessToken, Long advertiserId, String startDate, String endDate, int pageSize, int pageNum) {
|
|
|
|
+ String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.CREATIVE_REPORT;
|
|
|
|
+ Map<String, String> headers = new HashMap<String, String>();
|
|
|
|
+ headers.put("Content-Type", "application/json");
|
|
|
|
+ headers.put("Access-Token", accessToken);
|
|
|
|
+ Map<String, Object> param = new HashMap<String, Object>();
|
|
|
|
+ param.put("start_date", startDate);
|
|
|
|
+ param.put("end_date", endDate);
|
|
|
|
+ param.put("advertiser_id", advertiserId);
|
|
|
|
+ param.put("temporal_granularity", "HOURLY");
|
|
|
|
+ param.put("page_size", pageSize);
|
|
|
|
+ param.put("page", pageNum);
|
|
|
|
+ KuaishouResult<KuaishouReportHourlyCreative> kuaishouResult = null;
|
|
|
|
+ try {
|
|
|
|
+ String result = HttpUtils.httpPostRequest(url, param, headers);
|
|
|
|
+ System.out.println(result);
|
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
|
+ mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
|
|
|
+ kuaishouResult = mapper.readValue(result, new TypeReference<KuaishouResult<KuaishouReportHourlyCreative>>() {
|
|
|
|
+ });
|
|
|
|
+// kuaishouResult = (KuaishouResult<KuaishouReportDailyCreative>)jsonToObj(KuaishouResult.class,result);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return kuaishouResult;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 创建广告计划
|
|
|
|
+ *
|
|
|
|
+ * @param accessToken
|
|
|
|
+ * @param advertiser_id
|
|
|
|
+ * @param requestJson
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ @Autowired
|
|
|
|
+ private KuaiShouCampaignCreateMapper kuaiShouCampaignCreateMapper;
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public Map<String, Object> campaignCreate(String accessToken, Long advertiser_id, JSONObject requestJson) {
|
|
|
|
+ Map<String, Object> returnMap = new HashMap<>();
|
|
|
|
+ try {
|
|
|
|
+ String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.CAMPAIGN_CREATE;
|
|
|
|
+ Map<String, String> headers = new HashMap<String, String>();
|
|
|
|
+ headers.put("Access-Token", accessToken);
|
|
|
|
+ requestJson.put("advertiser_id", advertiser_id);
|
|
|
|
+ String result = HttpUtils.kuaiShouhttpPostRequest(url, requestJson.toJSONString(), headers);
|
|
|
|
+ JSONObject resultJson = JSONObject.parseObject(result);
|
|
|
|
+ if (!Check.isNull(resultJson)) {
|
|
|
|
+ Integer code = resultJson.getInteger("code");
|
|
|
|
+ if (code == 0) {
|
|
|
|
+ KuaiShouCampaignCreate campaignCreate = new KuaiShouCampaignCreate();
|
|
|
|
+ JSONObject dataJson = resultJson.getJSONObject("data");
|
|
|
|
+ Long campaign_id = dataJson.getLong("campaign_id");
|
|
|
|
+ campaignCreate.setCampaignId(campaign_id);
|
|
|
|
+ campaignCreate.setAccountId(advertiser_id);
|
|
|
|
+ campaignCreate.setCampaignName(requestJson.getString("campaign_name"));
|
|
|
|
+ campaignCreate.setDayBudget(requestJson.getInteger("day_budget"));
|
|
|
|
+ campaignCreate.setType(requestJson.getInteger("type"));
|
|
|
|
+ int i = kuaiShouCampaignCreateMapper.insert(campaignCreate);
|
|
|
|
+ if (i > 0) {
|
|
|
|
+ logger.info("创建广告信息入库完成,advertiser_id:{}", advertiser_id);
|
|
|
|
+ }
|
|
|
|
+ returnMap.put("code", 0);
|
|
|
|
+ returnMap.put("message", "success");
|
|
|
|
+
|
|
|
|
+ } else {
|
|
|
|
+ logger.error("创建广告信息失败,advertiser_id:{},返回信息:{}", advertiser_id, resultJson);
|
|
|
|
+ returnMap.put("code", -1);
|
|
|
|
+ returnMap.put("message", "error");
|
|
|
|
+ returnMap.put("desc", resultJson.getString("message"));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ } else {
|
|
|
|
+ logger.error("快手创建广告计划返回内容为空,advertiser_id:{}", advertiser_id);
|
|
|
|
+ returnMap.put("code", -1);
|
|
|
|
+ returnMap.put("message", "error");
|
|
|
|
+ returnMap.put("desc", "result is null");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ logger.error("创建广告计划失败,advertiser_id:{}", advertiser_id);
|
|
|
|
+ returnMap.put("code", -1);
|
|
|
|
+ returnMap.put("message", "error");
|
|
|
|
+ returnMap.put("desc", "Interface exception");
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return returnMap;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 创建 广告组
|
|
|
|
+ *
|
|
|
|
+ * @param accessToken
|
|
|
|
+ * @param advertiser_id
|
|
|
|
+ * @param requestJson
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public Map<String, Object> adUnitCreate(String accessToken, Long advertiser_id, JSONObject requestJson) {
|
|
|
|
+ Map<String, Object> returnMap = new HashMap<>();
|
|
|
|
+ try {
|
|
|
|
+ String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.AD_UNIT_CREATE;
|
|
|
|
+ Map<String, String> headers = new HashMap<String, String>();
|
|
|
|
+ requestJson.put("advertiser_id", advertiser_id);
|
|
|
|
+ headers.put("Access-Token", accessToken);
|
|
|
|
+
|
|
|
|
+ String result = HttpUtils.kuaiShouhttpPostRequest(url, requestJson.toJSONString(), headers);
|
|
|
|
+ JSONObject resultJson = JSONObject.parseObject(result);
|
|
|
|
+ if (!Check.isNull(resultJson)) {
|
|
|
|
+ Integer code = resultJson.getInteger("code");
|
|
|
|
+ if (code == 0) {
|
|
|
|
+ //TODO 未写完--------------------------
|
|
|
|
+ returnMap.put("code", 0);
|
|
|
|
+ returnMap.put("message", "success");
|
|
|
|
+
|
|
|
|
+ } else {
|
|
|
|
+ logger.error("创建广告信息失败,advertiser_id:{},返回信息:{}", advertiser_id, resultJson);
|
|
|
|
+ returnMap.put("code", -1);
|
|
|
|
+ returnMap.put("message", "error");
|
|
|
|
+ returnMap.put("desc", resultJson.getString("message"));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ } else {
|
|
|
|
+ logger.error("快手创建广告计划返回内容为空,advertiser_id:{}", advertiser_id);
|
|
|
|
+ returnMap.put("code", -1);
|
|
|
|
+ returnMap.put("message", "error");
|
|
|
|
+ returnMap.put("desc", "result is null");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ logger.error("创建广告计划失败,advertiser_id:{}", advertiser_id);
|
|
|
|
+ returnMap.put("code", -1);
|
|
|
|
+ returnMap.put("message", "error");
|
|
|
|
+ returnMap.put("desc", "Interface exception");
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return returnMap;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取广告计划信息
|
|
|
|
+ *
|
|
|
|
+ * @param accessToken
|
|
|
|
+ * @param advertiserId
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ @Autowired
|
|
|
|
+ private KuaiShouCampaignMapper campaignMapper;
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void getCampaignList(String accessToken, Long advertiserId, Integer page) {
|
|
|
|
+ if (page == 1) { // 第一次请求删除历史数据
|
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
|
+ map.put("account_id", advertiserId);
|
|
|
|
+ campaignMapper.deleteByMap(map);
|
|
|
|
+ }
|
|
|
|
+ String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.CAMPAIGN_LIST;
|
|
|
|
+ Map<String, String> headers = new HashMap<>();
|
|
|
|
+ headers.put("Access-Token", accessToken);
|
|
|
|
+ JSONObject param = new JSONObject();
|
|
|
|
+ param.put("advertiser_id", advertiserId);
|
|
|
|
+ param.put("page", page);
|
|
|
|
+ param.put("page_size", 200);
|
|
|
|
+ try {
|
|
|
|
+ String result = HttpUtils.kuaiShouhttpPostRequest(url, param.toJSONString(), headers);
|
|
|
|
+ JSONObject resultJson = JSONObject.parseObject(result);
|
|
|
|
+ if (!Check.isNull(resultJson)) {
|
|
|
|
+ Integer code = resultJson.getInteger("code");
|
|
|
|
+ if (code == 0) {
|
|
|
|
+ JSONObject dataJson = resultJson.getJSONObject("data");
|
|
|
|
+ JSONArray details = dataJson.getJSONArray("details");
|
|
|
|
+ if (!Check.isNull(details)) {
|
|
|
|
+ for (int i = 0; i < details.size(); i++) {
|
|
|
|
+ JSONObject detail = JSONObject.parseObject(details.get(i).toString());
|
|
|
|
+ if (!Check.isNull(detail)) {
|
|
|
|
+ KuaiShouCampaign campaign = new KuaiShouCampaign();
|
|
|
|
+ campaign.setAccountId(advertiserId);
|
|
|
|
+ campaign.setCampaignId(detail.getLong("campaign_id"));
|
|
|
|
+ campaign.setCampaignName(detail.getString("campaign_name"));
|
|
|
|
+ campaign.setDayBudget(detail.getLong("day_budget"));
|
|
|
|
+ campaign.setStatus(detail.getInteger("status"));
|
|
|
|
+ campaignMapper.insert(campaign);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ getCampaignList(accessToken, advertiserId, page + 1);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ } else {
|
|
|
|
+ logger.error("获取广告计划信息返回结果异常,advertiserId:{},异常信息:{}", advertiserId, resultJson);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ } else {
|
|
|
|
+ logger.error("获取广告计划信息返回结果为空,advertiserId:{}", advertiserId);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取广告组信息
|
|
|
|
+ *
|
|
|
|
+ * @param accessToken
|
|
|
|
+ * @param advertiserId
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ @Autowired
|
|
|
|
+ private KuaiShouGroupMapper groupMapper;
|
|
|
|
+ @Autowired
|
|
|
|
+ private KuaiShouAppInfoMapper appInfoMapper;
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void getGroupList(String accessToken, Long advertiserId, Integer page) {
|
|
|
|
+ if (page == 1) { // 第一次请求删除历史数据
|
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
|
+ map.put("account_id", advertiserId);
|
|
|
|
+ groupMapper.deleteByMap(map);
|
|
|
|
+ appInfoMapper.deleteByMap(map);
|
|
|
|
+ }
|
|
|
|
+ String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.GROUP_LIST;
|
|
|
|
+ JSONObject param = new JSONObject();
|
|
|
|
+ param.put("advertiser_id", advertiserId);
|
|
|
|
+ param.put("page_size", 200);
|
|
|
|
+ param.put("page", page);
|
|
|
|
+ Map<String, String> headers = new HashMap<>();
|
|
|
|
+ headers.put("Access-Token", accessToken);
|
|
|
|
+ try {
|
|
|
|
+ String result = HttpUtils.kuaiShouhttpPostRequest(url, param.toJSONString(), headers);
|
|
|
|
+ JSONObject resultJson = JSONObject.parseObject(result);
|
|
|
|
+ if (!Check.isNull(resultJson)) {
|
|
|
|
+ Integer code = resultJson.getInteger("code");
|
|
|
|
+ if (code == 0) {
|
|
|
|
+ JSONObject dataJson = resultJson.getJSONObject("data");
|
|
|
|
+ if (!Check.isNull(dataJson)) {
|
|
|
|
+ JSONArray details = dataJson.getJSONArray("details");
|
|
|
|
+ if (!Check.isNull(details)) {
|
|
|
|
+ for (int i = 0; i < details.size(); i++) {
|
|
|
|
+ JSONObject detail = JSONObject.parseObject(details.get(i).toString());
|
|
|
|
+ if (!Check.isNull(detail)) {
|
|
|
|
+ KuaiShouGroup group = new KuaiShouGroup();
|
|
|
|
+ group.setAccountId(advertiserId);
|
|
|
|
+ group.setStatus(detail.getInteger("status"));
|
|
|
|
+ group.setBid(detail.getLong("bid"));
|
|
|
|
+ group.setSpeed(detail.getInteger("speed"));
|
|
|
|
+ group.setSchedule(detail.getString("schedule"));
|
|
|
|
+ group.setUrl(detail.getString("url"));
|
|
|
|
+ group.setCampaignId(detail.getLong("campaign_id"));
|
|
|
|
+ Long unitId = detail.getLong("unit_id");
|
|
|
|
+ group.setUnitId(unitId);
|
|
|
|
+ group.setUnitName(detail.getString("unit_name"));
|
|
|
|
+ group.setBidType(detail.getInteger("bid_type"));
|
|
|
|
+ group.setReviewDetail(detail.getString("review_detail"));
|
|
|
|
+ group.setCpaBid(detail.getLong("cpa_bid"));
|
|
|
|
+ group.setOcpxActionType(detail.getInteger("ocpx_action_type"));
|
|
|
|
+ group.setDayBudget(detail.getLong("day_budget"));
|
|
|
|
+ group.setBeginTime(detail.getString("begin_time"));
|
|
|
|
+ group.setEndTime(detail.getString("end_time"));
|
|
|
|
+ group.setShowModel(detail.getInteger("show_mode"));
|
|
|
|
+ group.setUrlType(detail.getInteger("url_type"));
|
|
|
|
+ group.setAppId(detail.getLong("app_id"));
|
|
|
|
+ group.setAppIconUrl(detail.getString("app_icon_url"));
|
|
|
|
+ groupMapper.insert(group);
|
|
|
|
+
|
|
|
|
+ // 添加应用信息
|
|
|
|
+ JSONObject diverseJson = detail.getJSONObject("diverse_data");
|
|
|
|
+ if (!Check.isNull(diverseJson)) {
|
|
|
|
+ KuaiShouAppInfo appInfo = new KuaiShouAppInfo();
|
|
|
|
+ appInfo.setAccountId(advertiserId);
|
|
|
|
+ appInfo.setAppName(diverseJson.getString("app_name"));
|
|
|
|
+ appInfo.setAppPackageName(diverseJson.getString("app_package_name"));
|
|
|
|
+ appInfo.setDeviceOsType(diverseJson.getInteger("device_os_type"));
|
|
|
|
+ appInfo.setUnitId(unitId);
|
|
|
|
+ appInfoMapper.insert(appInfo);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ getGroupList(accessToken, advertiserId, page + 1);
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ } else {
|
|
|
|
+ logger.error("获取广告组返回结果异常,advertiserId:{},异常信息:{}", advertiserId, resultJson);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ } else {
|
|
|
|
+ logger.error("获取广告组返回结果为空,advertiserId:{}", advertiserId);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取 创意信息
|
|
|
|
+ *
|
|
|
|
+ * @param accessToken
|
|
|
|
+ * @param advertiserId
|
|
|
|
+ */
|
|
|
|
+ @Autowired
|
|
|
|
+ private KuaiShouCreativeMapper creativeMapper;
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void getCreativeList(String accessToken, Long advertiserId, Integer page) {
|
|
|
|
+ if (page == 1) { // 第一次请求删除历史数据
|
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
|
+ map.put("account_id", advertiserId);
|
|
|
|
+ creativeMapper.deleteByMap(map);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.CREATIVE_LIST;
|
|
|
|
+ Map<String, String> headers = new HashMap<String, String>();
|
|
|
|
+ headers.put("Content-Type", "application/json");
|
|
|
|
+ headers.put("Access-Token", accessToken);
|
|
|
|
+ JSONObject param = new JSONObject();
|
|
|
|
+ param.put("advertiser_id", advertiserId);
|
|
|
|
+ param.put("page_size", 200);
|
|
|
|
+ param.put("page", page);
|
|
|
|
+ try {
|
|
|
|
+ String result = HttpUtils.kuaiShouhttpPostRequest(url, param.toJSONString(), headers);
|
|
|
|
+ JSONObject resultJson = JSONObject.parseObject(result);
|
|
|
|
+ if (!Check.isNull(resultJson)) {
|
|
|
|
+ Integer code = resultJson.getInteger("code");
|
|
|
|
+ if (code == 0) {
|
|
|
|
+ JSONObject dataJson = resultJson.getJSONObject("data");
|
|
|
|
+ if (!Check.isNull(dataJson)) {
|
|
|
|
+ JSONArray details = dataJson.getJSONArray("details");
|
|
|
|
+ if (!Check.isNull(details)) {
|
|
|
|
+ for (int i = 0; i < details.size(); i++) {
|
|
|
|
+ JSONObject detailJson = JSONObject.parseObject(details.get(i).toString());
|
|
|
|
+ if (!Check.isNull(detailJson)) {
|
|
|
|
+ KuaiShouCreative creative = new KuaiShouCreative();
|
|
|
|
+ creative.setAccountId(advertiserId);
|
|
|
|
+ creative.setStatus(detailJson.getInteger("status"));
|
|
|
|
+ creative.setCampaignId(dataJson.getLong("campaign_id"));
|
|
|
|
+ creative.setUnitId(dataJson.getLong("unit_id"));
|
|
|
|
+ creative.setCreativeId(dataJson.getLong("creative_id"));
|
|
|
|
+ creative.setCreativeName(dataJson.getString("creative_name"));
|
|
|
|
+ creative.setPhotoId(dataJson.getString("photo_id"));
|
|
|
|
+ creative.setReviewDetail(dataJson.getString("review_detail"));
|
|
|
|
+ creative.setCoverUrl(dataJson.getString("cover_url"));
|
|
|
|
+ creative.setCoverHeight(dataJson.getString("cover_height"));
|
|
|
|
+ creative.setCoverWidth(dataJson.getString("cover_width"));
|
|
|
|
+ creative.setClickTrackUrl(dataJson.getString("click_track_url"));
|
|
|
|
+ JSONObject displayInfoJson = detailJson.getJSONObject("display_info");
|
|
|
|
+ if (!Check.isNull(displayInfoJson)) {
|
|
|
|
+ creative.setDescription(displayInfoJson.getString("description"));
|
|
|
|
+ creative.setActionBarText(displayInfoJson.getString("action_bar_text"));
|
|
|
|
+ }
|
|
|
|
+ creativeMapper.insert(creative);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ getCreativeList(accessToken, advertiserId, page + 1);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ } else {
|
|
|
|
+ logger.error("获取广告创意组返回结果异常,advertiserId:{},异常信息:{}", advertiserId, resultJson);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ } else {
|
|
|
|
+ logger.error("获取广告创意返回结果异常,advertiserId:{},异常信息:{}", advertiserId, resultJson);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取token
|
|
|
|
+ *
|
|
|
|
+ * @param authCode
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public KuaishouResultToken getAccessToken(String authCode) {
|
|
|
|
+ String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.AUTH_TOKEN;
|
|
|
|
+ JSONObject requestJson = new JSONObject();
|
|
|
|
+ requestJson.put("auth_code", authCode);
|
|
|
|
+ requestJson.put("app_id", PropertiesUtils.getConfig("kuaishou_appid"));
|
|
|
|
+ requestJson.put("secret", PropertiesUtils.getConfig("kuaishou_secret"));
|
|
|
|
+ KuaishouResultToken kuaiShouResult = null;
|
|
|
|
+ try {
|
|
|
|
+ String result = HttpUtils.kuaiShouhttpPostRequest(url, requestJson.toJSONString(), null);
|
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
|
+ mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
|
|
|
+ kuaiShouResult = mapper.readValue(result, new TypeReference<KuaishouResultToken>() {
|
|
|
|
+ });
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return kuaiShouResult;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 刷新token
|
|
|
|
+ *
|
|
|
|
+ * @param refreshToken
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public KuaishouResultToken getRefreshToken(String refreshToken) {
|
|
|
|
+ String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.REFRESH_TOKEN;
|
|
|
|
+ Map<String, String> headers = new HashMap<String, String>();
|
|
|
|
+ headers.put("Content-Type", "application/json");
|
|
|
|
+ Map<String, Object> param = new HashMap<String, Object>();
|
|
|
|
+ param.put("app_id", PropertiesUtils.getConfig("kuaishou_appid"));
|
|
|
|
+ param.put("secret", PropertiesUtils.getConfig("kuaishou_secret"));
|
|
|
|
+ param.put("refresh_token", refreshToken);
|
|
|
|
+ KuaishouResultToken kuaishouResult = null;
|
|
|
|
+ try {
|
|
|
|
+ String result = HttpUtils.httpPostRequest(url, param, headers);
|
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
|
+ mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
|
|
|
+ kuaishouResult = mapper.readValue(result, new TypeReference<KuaishouResultToken>() {
|
|
|
|
+ });
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return kuaishouResult;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public Object jsonToObj(Object t, String jsonStr) throws
|
|
|
|
+ JsonParseException, JsonMappingException, IOException {
|
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
|
+
|
|
|
|
+ mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
|
|
|
+ return mapper.readValue(jsonStr, new TypeReference<Object>() {
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public JavaType getJavaType(Type type) {
|
|
|
|
+ //判断是否带有泛型
|
|
|
|
+ if (type instanceof ParameterizedType) {
|
|
|
|
+ Type[] actualTypeArguments = ((ParameterizedType) type).getActualTypeArguments();
|
|
|
|
+ //获取泛型类型
|
|
|
|
+ Class rowClass = (Class) ((ParameterizedType) type).getRawType();
|
|
|
|
+ JavaType[] javaTypes = new JavaType[actualTypeArguments.length];
|
|
|
|
+ for (int i = 0; i < actualTypeArguments.length; i++) {
|
|
|
|
+ //泛型也可能带有泛型,递归获取
|
|
|
|
+ javaTypes[i] = getJavaType(actualTypeArguments[i]);
|
|
|
|
+ }
|
|
|
|
+ return TypeFactory.defaultInstance().constructParametricType(rowClass, javaTypes);
|
|
|
|
+ } else {
|
|
|
|
+ //简单类型直接用该类构建
|
|
|
|
+ Class cla = (Class) type;
|
|
|
|
+ return TypeFactory.defaultInstance().constructParametricType(cla, new JavaType[0]);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+}
|