|
@@ -2,10 +2,7 @@ package cn.com.ctop.toutiao.modules.material.service.impl;
|
|
|
|
|
|
import cn.com.ctop.common.module.entity.CtopOauthToken;
|
|
|
import cn.com.ctop.common.module.service.ICtopOauthTokenService;
|
|
|
-import cn.com.ctop.common.module.service.IFileInfoService;
|
|
|
-import cn.com.ctop.common.module.utils.HttpUtils;
|
|
|
-import cn.com.ctop.common.module.utils.PropertiesUtils;
|
|
|
-import cn.com.ctop.common.module.utils.StringUtils;
|
|
|
+import cn.com.ctop.common.module.utils.*;
|
|
|
import cn.com.ctop.toutiao.modules.material.entity.ByteDanceCreative;
|
|
|
import cn.com.ctop.toutiao.modules.material.mapper.ByteDanceCreativeMapper;
|
|
|
import cn.com.ctop.toutiao.modules.material.service.IByteDanceCreativeService;
|
|
@@ -13,20 +10,10 @@ import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.apache.http.client.ClientProtocolException;
|
|
|
-import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
-import org.apache.http.client.methods.HttpPost;
|
|
|
-import org.apache.http.entity.ContentType;
|
|
|
-import org.apache.http.entity.StringEntity;
|
|
|
-import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
-import org.apache.http.impl.client.HttpClientBuilder;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.context.annotation.Primary;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import java.io.BufferedReader;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStreamReader;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
@@ -43,18 +30,23 @@ import java.util.TreeMap;
|
|
|
@Primary
|
|
|
public class ByteDanceCreativeServiceImpl extends ServiceImpl<ByteDanceCreativeMapper, ByteDanceCreative> implements IByteDanceCreativeService {
|
|
|
@Autowired
|
|
|
- private IFileInfoService fileInfoService;
|
|
|
- @Autowired
|
|
|
private ICtopOauthTokenService tokenService;
|
|
|
@Autowired
|
|
|
private ByteDanceCreativeMapper creativeMapper;
|
|
|
|
|
|
+ /**
|
|
|
+ * 更改创意状态
|
|
|
+ * @param accountId
|
|
|
+ * @param creativeIds
|
|
|
+ * @param optStatus
|
|
|
+ * @return
|
|
|
+ */
|
|
|
@Override
|
|
|
public Map<String, Object> advertiserCreativeUpdateStatus(String accountId, String creativeIds, String optStatus) {
|
|
|
Map<String, Object> resultMap = new HashMap<>();
|
|
|
JSONArray ids = new JSONArray();
|
|
|
String[] getCreativeIds = creativeIds.split(StringUtils.COMMA);
|
|
|
- if (null != getCreativeIds && getCreativeIds.length > 0) {
|
|
|
+ if (getCreativeIds.length > 0) {
|
|
|
for (int i = 0; i < getCreativeIds.length; i++) {
|
|
|
ids.add(Long.parseLong(getCreativeIds[i]));
|
|
|
}
|
|
@@ -88,8 +80,8 @@ public class ByteDanceCreativeServiceImpl extends ServiceImpl<ByteDanceCreativeM
|
|
|
return resultMap;
|
|
|
}
|
|
|
JSONArray returnCreativeIds = data.getJSONArray("creative_ids");
|
|
|
- if (null != returnCreativeIds && returnCreativeIds.size() > 0) {
|
|
|
- getAdvertiserCreative(accountId, creativeIds, null);
|
|
|
+ if (null != returnCreativeIds && !returnCreativeIds.isEmpty()) {
|
|
|
+ getAdvertiserCreative(cTopOauthToken, creativeIds, null);
|
|
|
}
|
|
|
resultMap.put("code", 0);
|
|
|
resultMap.put("message", "广告创意状态修改成功");
|
|
@@ -101,22 +93,28 @@ public class ByteDanceCreativeServiceImpl extends ServiceImpl<ByteDanceCreativeM
|
|
|
creativeMapper.replaceBatch(creatives);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取创意列表,入库
|
|
|
+ * @param token
|
|
|
+ * @param ids
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ */
|
|
|
@Override
|
|
|
- public Map<String, Object> getAdvertiserCreative(String accountId, String ids, String date) {
|
|
|
+ public Map<String, Object> getAdvertiserCreative(CtopOauthToken token, String ids, String date) {
|
|
|
Map<String, Object> resultMap = new HashMap<>();
|
|
|
- getAdvertiserCreativeByPageNumber(accountId, 1, ids, date);
|
|
|
+ getAdvertiserCreativeByPageNumber(token, 1, ids, date);
|
|
|
resultMap.put("code", 0);
|
|
|
resultMap.put("message", "获取广告主广告创意信息完成");
|
|
|
return resultMap;
|
|
|
}
|
|
|
|
|
|
- public void getAdvertiserCreativeByPageNumber(String accountId, Integer pageNumber, String ids, String date) {
|
|
|
- CtopOauthToken cTopOauthToken = tokenService.getOauthTokenByAccountId(accountId);
|
|
|
+ public void getAdvertiserCreativeByPageNumber(CtopOauthToken token, Integer pageNumber, String ids, String date) {
|
|
|
//2: 根据token以及用户id获取用户信息数据
|
|
|
String url = PropertiesUtils.getValue("bytedance_config", "bytedance_api_url") + PropertiesUtils.getValue("bytedance_config", "bytedance_v2_creative_get");
|
|
|
Map<String, String> headers = new HashMap<>();
|
|
|
headers.put("Content-Type", "application/json");
|
|
|
- headers.put("Access-Token", cTopOauthToken.getAccessToken());
|
|
|
+ headers.put("Access-Token", token.getAccessToken());
|
|
|
|
|
|
TreeMap<String, Object> params = new TreeMap<>();
|
|
|
JSONObject filtering = new JSONObject();
|
|
@@ -133,38 +131,45 @@ public class ByteDanceCreativeServiceImpl extends ServiceImpl<ByteDanceCreativeM
|
|
|
filtering.put("creative_create_time", date);
|
|
|
}
|
|
|
params.put("filtering", filtering.toJSONString());
|
|
|
- params.put("advertiser_id", cTopOauthToken.getAccountId());
|
|
|
+ params.put("advertiser_id", token.getAccountId());
|
|
|
params.put("page", pageNumber + "");
|
|
|
String result = HttpUtils.httpGetRequest(url, headers, params);
|
|
|
JSONObject jsonObject = JSONObject.parseObject(result);
|
|
|
Integer code = jsonObject.getInteger("code");
|
|
|
|
|
|
if (null == code || !code.equals(0)) {
|
|
|
- log.info("获取广告主广告创意信息接口异常==》accountId:{},message:{}", accountId, jsonObject.getString("message"));
|
|
|
+ log.info("获取广告主广告创意信息接口异常==》accountId:{},message:{}", token.getAccountId(), jsonObject.getString("message"));
|
|
|
return;
|
|
|
}
|
|
|
JSONArray data = jsonObject.getJSONObject("data").getJSONArray("list");
|
|
|
- if (null == data || data.size() <= 0) {
|
|
|
- log.info("广告主广告创意信息不存在==》accountId:{},message:{}", accountId, jsonObject.getString("message"));
|
|
|
+ if (null == data || data.isEmpty()) {
|
|
|
+ log.info("广告主广告创意信息不存在==》accountId:{},message:{}", token.getAccountId(), jsonObject.getString("message"));
|
|
|
return;
|
|
|
}
|
|
|
for (int i = 0; i < data.size(); i++) {
|
|
|
JSONObject dataObject = data.getJSONObject(i);
|
|
|
- ByteDanceCreative creative = new ByteDanceCreative(dataObject, accountId);
|
|
|
+ ByteDanceCreative creative = new ByteDanceCreative(dataObject, token);
|
|
|
this.saveOrUpdate(creative);
|
|
|
}
|
|
|
- getAdvertiserCreativeByPageNumber(accountId, pageNumber + 1, ids, date);
|
|
|
+ getAdvertiserCreativeByPageNumber(token, pageNumber + 1, ids, date);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 创建创意
|
|
|
+ * @param accountId
|
|
|
+ * @param adId
|
|
|
+ * @param data
|
|
|
+ * @return
|
|
|
+ */
|
|
|
@Override
|
|
|
- public Map<String, Object> creativeCreate(String accountId, Long adId, String dataString) {
|
|
|
+ public Map<String, Object> creativeCreate(String accountId, Long adId, JSONObject data) {
|
|
|
Map<String, Object> resultMap = new HashMap<>();
|
|
|
//1:获取token
|
|
|
CtopOauthToken token = tokenService.getOauthTokenByAccountId(accountId);
|
|
|
- JSONObject data = JSONObject.parseObject(dataString);
|
|
|
data.put("advertiser_id", token.getAccountId());
|
|
|
data.put("ad_id", adId);
|
|
|
- JSONObject result = createCreative(data, token.getAccessToken());
|
|
|
+ String url = PropertiesUtils.getValue("bytedance_config", "bytedance_api_url") + PropertiesUtils.getValue("bytedance_config", "bytedance_v2_creative_create_v2");
|
|
|
+ JSONObject result = HttpUtils.bytedancePostRequest(token.getAccessToken(),url,data);
|
|
|
Integer code = result.getInteger("code");
|
|
|
String message = result.getString("message");
|
|
|
if (null == code || code != 0) {
|
|
@@ -180,45 +185,86 @@ public class ByteDanceCreativeServiceImpl extends ServiceImpl<ByteDanceCreativeM
|
|
|
return resultMap;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取创意详细信息
|
|
|
+ * @return
|
|
|
+ */
|
|
|
@Override
|
|
|
- public JSONObject createCreative(JSONObject data, String token) {
|
|
|
- // 构造请求
|
|
|
- String url = PropertiesUtils.getValue("bytedance_config", "bytedance_api_url") + PropertiesUtils.getValue("bytedance_config", "bytedance_v2_creative_create_v2");
|
|
|
- HttpPost httpEntity = new HttpPost(url);
|
|
|
- httpEntity.setHeader("Access-Token", token);
|
|
|
- CloseableHttpResponse response = null;
|
|
|
- CloseableHttpClient client = null;
|
|
|
- try {
|
|
|
- client = HttpClientBuilder.create().build();
|
|
|
- httpEntity.setEntity(new StringEntity(data.toJSONString(), ContentType.APPLICATION_JSON));
|
|
|
- response = client.execute(httpEntity);
|
|
|
- if (response != null && response.getStatusLine().getStatusCode() == 200) {
|
|
|
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
|
|
|
- StringBuffer result = new StringBuffer();
|
|
|
- String line = "";
|
|
|
- while ((line = bufferedReader.readLine()) != null) {
|
|
|
- result.append(line);
|
|
|
- }
|
|
|
- bufferedReader.close();
|
|
|
- return JSONObject.parseObject(result.toString());
|
|
|
- }
|
|
|
+ public Map<String, Object> getCreativeDetail(Long accountId, Long adId){
|
|
|
+ Map<String,Object>resultMap = new HashMap<>();
|
|
|
+ CtopOauthToken token = tokenService.getTokenByAccountId(accountId);
|
|
|
+ // 请求地址
|
|
|
+ String url = PropertiesUtils.getValue("bytedance_config", "bytedance_api_url") + PropertiesUtils.getValue("bytedance_config", "bytedance_v2_creative_read");
|
|
|
+ // 请求参数
|
|
|
+ JSONObject data = new JSONObject();
|
|
|
+ data.put("advertiser_id", token.getAccountId());
|
|
|
+ data.put("ad_id",adId);
|
|
|
+ JSONObject result = HttpUtils.bytedanceGetRequest(token.getAccessToken(),url,data);
|
|
|
+ Integer code = result.getInteger("code");
|
|
|
+ String message = result.getString("message");
|
|
|
+ if (null == code || code != 0) {
|
|
|
+ log.info("广告创意获取详情失败,accountId:{},message:{}", accountId, message);
|
|
|
+ resultMap.put("success", false);
|
|
|
+ resultMap.put("code", -1);
|
|
|
+ resultMap.put("message", message);
|
|
|
+ return resultMap;
|
|
|
+ }
|
|
|
+ resultMap.put("data",result.getJSONObject("data"));
|
|
|
+ ResultMapUtils.setResultMap(resultMap, StatusCode.COMMON_SUCCESS);
|
|
|
+ return resultMap;
|
|
|
+ }
|
|
|
|
|
|
- } catch (ClientProtocolException e) {
|
|
|
- e.printStackTrace();
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- } finally {
|
|
|
- try {
|
|
|
- if (response != null) {
|
|
|
- response.close();
|
|
|
- }
|
|
|
- if (null != client) {
|
|
|
- client.close();
|
|
|
- }
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * TODO 后续需要拼接参数
|
|
|
+ * @param accountId
|
|
|
+ * @param data
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Map<String,Object> updateCreative(Long accountId,JSONObject data){
|
|
|
+ Map<String,Object>resultMap = new HashMap<>();
|
|
|
+ CtopOauthToken token = tokenService.getTokenByAccountId(accountId);
|
|
|
+ // 请求地址
|
|
|
+ String url = PropertiesUtils.getValue("bytedance_config", "bytedance_api_url") + PropertiesUtils.getValue("bytedance_config", "bytedance_v2_creative_update");
|
|
|
+ JSONObject result = HttpUtils.bytedancePostRequest(token.getAccessToken(),url,data);
|
|
|
+ Integer code = result.getInteger("code");
|
|
|
+ String message = result.getString("message");
|
|
|
+ if (null == code || code != 0) {
|
|
|
+ log.info("广告创意更新失败,accountId:{},message:{}", accountId, message);
|
|
|
+ resultMap.put("success", false);
|
|
|
+ resultMap.put("code", -1);
|
|
|
+ resultMap.put("message", message);
|
|
|
+ return resultMap;
|
|
|
+ }
|
|
|
+ resultMap.put("data",result.getJSONObject("data"));
|
|
|
+ ResultMapUtils.setResultMap(resultMap, StatusCode.COMMON_SUCCESS);
|
|
|
+ return resultMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String,Object> getCreativeRejectReason(Long accountId, Long creativeId){
|
|
|
+ Map<String,Object>resultMap = new HashMap<>();
|
|
|
+ CtopOauthToken token = tokenService.getTokenByAccountId(accountId);
|
|
|
+ JSONArray creativeIds = new JSONArray();
|
|
|
+ creativeIds.add(creativeId);
|
|
|
+ // 请求地址
|
|
|
+ String url = PropertiesUtils.getValue("bytedance_config", "bytedance_api_url") + PropertiesUtils.getValue("bytedance_config", "bytedance_v2_creative_reject_reason");
|
|
|
+ // 请求参数
|
|
|
+ JSONObject data = new JSONObject();
|
|
|
+ data.put("advertiser_id", accountId);
|
|
|
+ data.put("creative_ids", creativeIds);
|
|
|
+ JSONObject result = HttpUtils.bytedancePostRequest(token.getAccessToken(),url,data);
|
|
|
+ Integer code = result.getInteger("code");
|
|
|
+ String message = result.getString("message");
|
|
|
+ if (null == code || code != 0) {
|
|
|
+ log.info("广告创意创意审核建议获取失败,accountId:{},message:{}", accountId, message);
|
|
|
+ resultMap.put("success", false);
|
|
|
+ resultMap.put("code", -1);
|
|
|
+ resultMap.put("message", message);
|
|
|
+ return resultMap;
|
|
|
}
|
|
|
- return null;
|
|
|
+ resultMap.put("data",result.getJSONObject("data"));
|
|
|
+ ResultMapUtils.setResultMap(resultMap, StatusCode.COMMON_SUCCESS);
|
|
|
+ return resultMap;
|
|
|
}
|
|
|
}
|